aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard
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
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 '')
-rw-r--r--WireGuard/WireGuard.xcodeproj/project.pbxproj4
-rw-r--r--WireGuard/WireGuard/Crypto/Curve25519.swift27
2 files changed, 31 insertions, 0 deletions
diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj
index c5b7631..e7edbe4 100644
--- a/WireGuard/WireGuard.xcodeproj/project.pbxproj
+++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj
@@ -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>";
@@ -236,6 +239,7 @@
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
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
+ }
+}