aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/Crypto/Curve25519.swift
blob: 83074e44cd8557320a10fb3cb0729c7086bd69f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
    }
}