aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/Crypto/Curve25519.swift
diff options
context:
space:
mode:
Diffstat (limited to '')
-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 0000000..83074e4
--- /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
+ }
+}