From c689be7effe4a9f91de8a05d3862dca6a9839a85 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 24 Oct 2018 11:56:18 +0530 Subject: Crypto: Swift wrapper for the Curve25519 C code Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/Crypto/Curve25519.swift | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 WireGuard/WireGuard/Crypto/Curve25519.swift (limited to 'WireGuard/WireGuard/Crypto/Curve25519.swift') 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 + } +} -- cgit v1.2.3-59-g8ed1b