From 1f166c8d3e6c7f551695a85597747874dbe82272 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 24 Oct 2018 11:56:18 +0530 Subject: [PATCH] Crypto: Swift wrapper for the Curve25519 C code --- WireGuard/WireGuard.xcodeproj/project.pbxproj | 4 +++ WireGuard/WireGuard/Crypto/Curve25519.swift | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 WireGuard/WireGuard/Crypto/Curve25519.swift 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 = ""; }; 6F6899A42180447E0012E523 /* x25519.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = x25519.h; sourceTree = ""; }; 6F6899A52180447E0012E523 /* x25519.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = x25519.c; sourceTree = ""; }; + 6F6899A7218044FC0012E523 /* Curve25519.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Curve25519.swift; sourceTree = ""; }; 6F693A552179E556008551C1 /* Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Endpoint.swift; sourceTree = ""; }; 6F7774DF217181B1006A79B3 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; 6F7774E0217181B1006A79B3 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -65,6 +67,7 @@ children = ( 6F6899A52180447E0012E523 /* x25519.c */, 6F6899A42180447E0012E523 /* x25519.h */, + 6F6899A7218044FC0012E523 /* Curve25519.swift */, ); path = Crypto; sourceTree = ""; @@ -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) 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) in + publicKey.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer) in + curve25519_derive_public_key(bytes, privateKeyBytes) + } + } + assert(publicKey.count == 32) + return publicKey + } +}