aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/Crypto
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-10-24 11:56:18 +0530
committerRoopesh Chander <roop@roopc.net>2018-10-27 15:13:01 +0530
commitc689be7effe4a9f91de8a05d3862dca6a9839a85 (patch)
tree5d331cbc5486739e771dd60e0b265c661172252c /WireGuard/WireGuard/Crypto
parentCrypto: Curve25519: Add explicit cast to supress conversion warning. (diff)
downloadwireguard-apple-c689be7effe4a9f91de8a05d3862dca6a9839a85.tar.xz
wireguard-apple-c689be7effe4a9f91de8a05d3862dca6a9839a85.zip
Crypto: Swift wrapper for the Curve25519 C code
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/Crypto')
-rw-r--r--WireGuard/WireGuard/Crypto/Curve25519.swift27
1 files changed, 27 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/Crypto/Curve25519.swift b/WireGuard/WireGuard/Crypto/Curve25519.swift
new file mode 100644
index 00000000..83074e44
--- /dev/null
+++ b/WireGuard/WireGuard/Crypto/Curve25519.swift
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All rights reserved.
+
+import UIKit
+
+struct Curve25519 {
+ static func generatePrivateKey() -> Data {
+ var privateKey = Data(repeating: 0, count: 32)
+ privateKey.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) in
+ curve25519_generate_private_key(bytes)
+ }
+ assert(privateKey.count == 32)
+ return privateKey
+ }
+
+ static func generatePublicKey(fromPrivateKey privateKey: Data) -> Data {
+ assert(privateKey.count == 32)
+ var publicKey = Data(repeating: 0, count: 32)
+ privateKey.withUnsafeBytes { (privateKeyBytes: UnsafePointer<UInt8>) in
+ publicKey.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) in
+ curve25519_derive_public_key(bytes, privateKeyBytes)
+ }
+ }
+ assert(publicKey.count == 32)
+ return publicKey
+ }
+}