blob: 292552c9b595abecf6bd0226966079bb69ca8c29 (
plain)
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
28
29
30
31
32
33
34
35
36
37
38
|
//
// WireGuardKey.swift
// MullvadRustRuntime
//
// Created by Emils on 2026-04-07.
// Copyright © 2026 Mullvad VPN AB. All rights reserved.
//
import Foundation
import MullvadTypes
private let keyLength = 32
extension WireGuard.PrivateKey {
/// Generate a new random private key via Rust FFI.
public init() {
var keyData = Data(repeating: 0, count: keyLength)
keyData.withUnsafeMutableBytes { buffer in
mullvad_generate_private_key(buffer.baseAddress!.assumingMemoryBound(to: UInt8.self))
}
self.init(rawValue: keyData)!
}
/// Derive the corresponding public key via Rust FFI.
public var publicKey: WireGuard.PublicKey {
rawValue.withUnsafeBytes { privateBuffer in
var publicKeyData = Data(repeating: 0, count: keyLength)
let privateKeyBytes = privateBuffer.baseAddress!.assumingMemoryBound(to: UInt8.self)
publicKeyData.withUnsafeMutableBytes { publicBuffer in
mullvad_derive_public_key(
privateKeyBytes,
publicBuffer.baseAddress!.assumingMemoryBound(to: UInt8.self)
)
}
return WireGuard.PublicKey(rawValue: publicKeyData)!
}
}
}
|