]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Crypto: Swift wrapper for the Curve25519 C code
authorRoopesh Chander <roop@roopc.net>
Wed, 24 Oct 2018 06:26:18 +0000 (11:56 +0530)
committerRoopesh Chander <roop@roopc.net>
Sat, 27 Oct 2018 09:43:01 +0000 (15:13 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard.xcodeproj/project.pbxproj
WireGuard/WireGuard/Crypto/Curve25519.swift [new file with mode: 0644]

index c5b76312908d2619e1bfda05ea3033d34e530ba0..e7edbe45a343246935355bd1d917f815b8322a72 100644 (file)
@@ -11,6 +11,7 @@
                6F628C3F217F3413003482A3 /* DNSServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C3E217F3413003482A3 /* DNSServer.swift */; };
                6F628C41217F47DB003482A3 /* TunnelDetailTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C40217F47DB003482A3 /* TunnelDetailTableViewController.swift */; };
                6F6899A62180447E0012E523 /* x25519.c in Sources */ = {isa = PBXBuildFile; fileRef = 6F6899A52180447E0012E523 /* x25519.c */; };
+               6F6899A8218044FC0012E523 /* Curve25519.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F6899A7218044FC0012E523 /* Curve25519.swift */; };
                6F693A562179E556008551C1 /* Endpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F693A552179E556008551C1 /* Endpoint.swift */; };
                6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774DF217181B1006A79B3 /* MainViewController.swift */; };
                6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E0217181B1006A79B3 /* AppDelegate.swift */; };
@@ -31,6 +32,7 @@
                6F689999218043390012E523 /* WireGuard-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuard-Bridging-Header.h"; sourceTree = "<group>"; };
                6F6899A42180447E0012E523 /* x25519.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = x25519.h; sourceTree = "<group>"; };
                6F6899A52180447E0012E523 /* x25519.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = x25519.c; sourceTree = "<group>"; };
+               6F6899A7218044FC0012E523 /* Curve25519.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Curve25519.swift; sourceTree = "<group>"; };
                6F693A552179E556008551C1 /* Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Endpoint.swift; sourceTree = "<group>"; };
                6F7774DF217181B1006A79B3 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; };
                6F7774E0217181B1006A79B3 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -65,6 +67,7 @@
                        children = (
                                6F6899A52180447E0012E523 /* x25519.c */,
                                6F6899A42180447E0012E523 /* x25519.h */,
+                               6F6899A7218044FC0012E523 /* Curve25519.swift */,
                        );
                        path = Crypto;
                        sourceTree = "<group>";
                                6F628C3D217F09E9003482A3 /* TunnelViewModel.swift in Sources */,
                                6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */,
                                6F7774E82172020C006A79B3 /* Configuration.swift in Sources */,
+                               6F6899A8218044FC0012E523 /* Curve25519.swift in Sources */,
                                6F628C41217F47DB003482A3 /* TunnelDetailTableViewController.swift in Sources */,
                                6F7774F321774263006A79B3 /* TunnelEditTableViewController.swift in Sources */,
                                6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */,
diff --git a/WireGuard/WireGuard/Crypto/Curve25519.swift b/WireGuard/WireGuard/Crypto/Curve25519.swift
new file mode 100644 (file)
index 0000000..83074e4
--- /dev/null
@@ -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
+    }
+}