mirror of
https://github.com/apple/swift-crypto
synced 2026-11-25 04:47:47 +00:00
Revert "(split: MLKEM) Enable CheckImplementationOnly and box properties where needed"
This reverts commit 02688bd534232b63581dbfa1a8d39476a0429f19.
This commit is contained in:
1 parent
1c92c8063d
commit
e08fd36acd
2 files changed
+111
-167
No files matched your search
@@ -33,6 +33,7 @@ import Foundation
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM768 {
|
||||
/// A ML-KEM-768 private key.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct InternalPrivateKey: @unchecked Sendable, KEMPrivateKey {
|
||||
private var backing: Backing
|
||||
|
||||
@@ -79,36 +80,29 @@ extension MLKEM768 {
|
||||
}
|
||||
|
||||
fileprivate final class Backing {
|
||||
// @_implementationOnly import => must use OpaquePointer for stored property.
|
||||
private let _storage: OpaquePointer
|
||||
var key: UnsafeMutablePointer<MLKEM768_private_key> {
|
||||
UnsafeMutablePointer(self._storage)
|
||||
}
|
||||
var key: MLKEM768_private_key
|
||||
var seed: Data
|
||||
|
||||
/// This is the only designated initializer, responsible for the allocation. Deallocation happens in deinit.
|
||||
/// To reduce mistakes (e.g. double-free), all other initializers should be convenience initializers.
|
||||
private init(takingOwnershipOf pointer: UnsafeMutablePointer<MLKEM768_private_key>, seed: Data) {
|
||||
self._storage = OpaquePointer(pointer)
|
||||
self.seed = seed
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.key.deinitialize(count: 1)
|
||||
self.key.deallocate()
|
||||
}
|
||||
/// Initialize a ML-KEM-768 private key from a random seed.
|
||||
convenience init() {
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM768_private_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM768_private_key())
|
||||
self.init(takingOwnershipOf: keyPtr, seed: Data(repeating: 0, count: MLKEM.seedByteCount))
|
||||
init() {
|
||||
self.key = .init()
|
||||
self.seed = Data()
|
||||
|
||||
seed.withUnsafeMutableBytes { seedPtr in
|
||||
self.seed = withUnsafeTemporaryAllocation(
|
||||
of: UInt8.self,
|
||||
capacity: MLKEM.seedByteCount
|
||||
) { seedPtr in
|
||||
withUnsafeTemporaryAllocation(
|
||||
of: UInt8.self,
|
||||
capacity: MLKEM768.InternalPublicKey.byteCount
|
||||
) { publicKeyPtr in
|
||||
CCryptoBoringSSL_MLKEM768_generate_key(publicKeyPtr.baseAddress, seedPtr.baseAddress, keyPtr)
|
||||
CCryptoBoringSSL_MLKEM768_generate_key(
|
||||
publicKeyPtr.baseAddress,
|
||||
seedPtr.baseAddress,
|
||||
&self.key
|
||||
)
|
||||
|
||||
return Data(bytes: seedPtr.baseAddress!, count: MLKEM.seedByteCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,19 +112,21 @@ extension MLKEM768 {
|
||||
/// - Parameter seedRepresentation: The seed to use to generate the private key.
|
||||
///
|
||||
/// - Throws: `CryptoKitError.incorrectKeySize` if the seed is not 64 bytes long.
|
||||
convenience init(seedRepresentation: some DataProtocol) throws {
|
||||
init(seedRepresentation: some DataProtocol) throws {
|
||||
guard seedRepresentation.count == MLKEM.seedByteCount else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM768_private_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM768_private_key())
|
||||
|
||||
self.init(takingOwnershipOf: keyPtr, seed: Data(seedRepresentation))
|
||||
self.key = .init()
|
||||
self.seed = Data(seedRepresentation)
|
||||
|
||||
guard
|
||||
self.seed.withUnsafeBytes({ seedPtr in
|
||||
CCryptoBoringSSL_MLKEM768_private_key_from_seed(keyPtr, seedPtr.baseAddress, seedPtr.count)
|
||||
CCryptoBoringSSL_MLKEM768_private_key_from_seed(
|
||||
&self.key,
|
||||
seedPtr.baseAddress,
|
||||
seedPtr.count
|
||||
)
|
||||
}) == 1
|
||||
else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
@@ -166,7 +162,7 @@ extension MLKEM768 {
|
||||
symmetricKeyDataPtr.baseAddress,
|
||||
encapsulatedPtr.baseAddress,
|
||||
encapsulatedPtr.count,
|
||||
self.key
|
||||
&self.key
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -184,6 +180,7 @@ extension MLKEM768 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM768 {
|
||||
/// A ML-KEM-768 public key.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct InternalPublicKey: @unchecked Sendable, KEMPublicKey {
|
||||
private var backing: Backing
|
||||
|
||||
@@ -216,26 +213,11 @@ extension MLKEM768 {
|
||||
static let byteCount = Backing.byteCount
|
||||
|
||||
fileprivate final class Backing {
|
||||
// @_implementationOnly import => must use OpaquePointer for stored property.
|
||||
private let _storage: OpaquePointer
|
||||
var key: UnsafeMutablePointer<MLKEM768_public_key> { UnsafeMutablePointer(self._storage) }
|
||||
var key: MLKEM768_public_key
|
||||
|
||||
/// This is the only designated initializer, responsible for the allocation. Deallocation happens in deinit.
|
||||
/// To reduce mistakes (e.g. double-free), all other initializers should be convenience initializers.
|
||||
private init(takingOwnershipOf pointer: UnsafeMutablePointer<MLKEM768_public_key>) {
|
||||
self._storage = OpaquePointer(pointer)
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.key.deinitialize(count: 1)
|
||||
self.key.deallocate()
|
||||
}
|
||||
|
||||
convenience init(privateKeyBacking: InternalPrivateKey.Backing) {
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM768_public_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM768_public_key())
|
||||
CCryptoBoringSSL_MLKEM768_public_from_private(keyPtr, privateKeyBacking.key)
|
||||
self.init(takingOwnershipOf: keyPtr)
|
||||
init(privateKeyBacking: InternalPrivateKey.Backing) {
|
||||
self.key = .init()
|
||||
CCryptoBoringSSL_MLKEM768_public_from_private(&self.key, &privateKeyBacking.key)
|
||||
}
|
||||
|
||||
/// Initialize a ML-KEM-768 public key from a raw representation.
|
||||
@@ -243,13 +225,12 @@ extension MLKEM768 {
|
||||
/// - Parameter rawRepresentation: The public key bytes.
|
||||
///
|
||||
/// - Throws: `CryptoKitError.incorrectKeySize` if the raw representation is not the correct size.
|
||||
convenience init(rawRepresentation: some DataProtocol) throws {
|
||||
init(rawRepresentation: some DataProtocol) throws {
|
||||
guard rawRepresentation.count == MLKEM768.InternalPublicKey.byteCount else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM768_public_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM768_public_key())
|
||||
self.init(takingOwnershipOf: keyPtr)
|
||||
|
||||
self.key = .init()
|
||||
|
||||
let bytes: ContiguousBytes =
|
||||
rawRepresentation.regions.count == 1
|
||||
@@ -258,7 +239,7 @@ extension MLKEM768 {
|
||||
try bytes.withUnsafeBytes { rawBuffer in
|
||||
try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in
|
||||
var cbs = CBS(data: buffer.baseAddress, len: buffer.count)
|
||||
guard CCryptoBoringSSL_MLKEM768_parse_public_key(self.key, &cbs) == 1 else {
|
||||
guard CCryptoBoringSSL_MLKEM768_parse_public_key(&self.key, &cbs) == 1 else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
@@ -271,7 +252,7 @@ extension MLKEM768 {
|
||||
// The following BoringSSL functions can only fail on allocation failure, which we define as impossible.
|
||||
CCryptoBoringSSL_CBB_init(&cbb, MLKEM768.InternalPublicKey.Backing.byteCount)
|
||||
defer { CCryptoBoringSSL_CBB_cleanup(&cbb) }
|
||||
CCryptoBoringSSL_MLKEM768_marshal_public_key(&cbb, self.key)
|
||||
CCryptoBoringSSL_MLKEM768_marshal_public_key(&cbb, &self.key)
|
||||
return Data(bytes: CCryptoBoringSSL_CBB_data(&cbb), count: CCryptoBoringSSL_CBB_len(&cbb))
|
||||
}
|
||||
|
||||
@@ -290,7 +271,7 @@ extension MLKEM768 {
|
||||
CCryptoBoringSSL_MLKEM768_encap(
|
||||
encapsulatedPtr.baseAddress,
|
||||
secretPtr.baseAddress,
|
||||
self.key
|
||||
&self.key
|
||||
)
|
||||
|
||||
return KEM.EncapsulationResult(
|
||||
@@ -321,6 +302,7 @@ extension MLKEM768 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM1024 {
|
||||
/// A ML-KEM-1024 private key.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct InternalPrivateKey: @unchecked Sendable, KEMPrivateKey {
|
||||
private var backing: Backing
|
||||
|
||||
@@ -367,36 +349,29 @@ extension MLKEM1024 {
|
||||
}
|
||||
|
||||
fileprivate final class Backing {
|
||||
// @_implementationOnly import => must use OpaquePointer for stored property.
|
||||
private let _storage: OpaquePointer
|
||||
var key: UnsafeMutablePointer<MLKEM1024_private_key> {
|
||||
UnsafeMutablePointer(self._storage)
|
||||
}
|
||||
var key: MLKEM1024_private_key
|
||||
var seed: Data
|
||||
|
||||
/// This is the only designated initializer, responsible for the allocation. Deallocation happens in deinit.
|
||||
/// To reduce mistakes (e.g. double-free), all other initializers should be convenience initializers.
|
||||
private init(takingOwnershipOf pointer: UnsafeMutablePointer<MLKEM1024_private_key>, seed: Data) {
|
||||
self._storage = OpaquePointer(pointer)
|
||||
self.seed = seed
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.key.deinitialize(count: 1)
|
||||
self.key.deallocate()
|
||||
}
|
||||
/// Initialize a ML-KEM-1024 private key from a random seed.
|
||||
convenience init() {
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM1024_private_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM1024_private_key())
|
||||
self.init(takingOwnershipOf: keyPtr, seed: Data(repeating: 0, count: MLKEM.seedByteCount))
|
||||
init() {
|
||||
self.key = .init()
|
||||
self.seed = Data()
|
||||
|
||||
seed.withUnsafeMutableBytes { seedPtr in
|
||||
self.seed = withUnsafeTemporaryAllocation(
|
||||
of: UInt8.self,
|
||||
capacity: MLKEM.seedByteCount
|
||||
) { seedPtr in
|
||||
withUnsafeTemporaryAllocation(
|
||||
of: UInt8.self,
|
||||
capacity: MLKEM1024.InternalPublicKey.byteCount
|
||||
) { publicKeyPtr in
|
||||
CCryptoBoringSSL_MLKEM1024_generate_key(publicKeyPtr.baseAddress, seedPtr.baseAddress, keyPtr)
|
||||
CCryptoBoringSSL_MLKEM1024_generate_key(
|
||||
publicKeyPtr.baseAddress,
|
||||
seedPtr.baseAddress,
|
||||
&self.key
|
||||
)
|
||||
|
||||
return Data(bytes: seedPtr.baseAddress!, count: MLKEM.seedByteCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -406,19 +381,21 @@ extension MLKEM1024 {
|
||||
/// - Parameter seedRepresentation: The seed to use to generate the private key.
|
||||
///
|
||||
/// - Throws: `CryptoKitError.incorrectKeySize` if the seed is not 64 bytes long.
|
||||
convenience init(seedRepresentation: some DataProtocol) throws {
|
||||
init(seedRepresentation: some DataProtocol) throws {
|
||||
guard seedRepresentation.count == MLKEM.seedByteCount else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM1024_private_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM1024_private_key())
|
||||
|
||||
self.init(takingOwnershipOf: keyPtr, seed: Data(seedRepresentation))
|
||||
self.key = .init()
|
||||
self.seed = Data(seedRepresentation)
|
||||
|
||||
guard
|
||||
self.seed.withUnsafeBytes({ seedPtr in
|
||||
CCryptoBoringSSL_MLKEM1024_private_key_from_seed(keyPtr, seedPtr.baseAddress, seedPtr.count)
|
||||
CCryptoBoringSSL_MLKEM1024_private_key_from_seed(
|
||||
&self.key,
|
||||
seedPtr.baseAddress,
|
||||
seedPtr.count
|
||||
)
|
||||
}) == 1
|
||||
else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
@@ -454,7 +431,7 @@ extension MLKEM1024 {
|
||||
symmetricKeyDataPtr.baseAddress,
|
||||
encapsulatedPtr.baseAddress,
|
||||
encapsulatedPtr.count,
|
||||
self.key
|
||||
&self.key
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -472,6 +449,7 @@ extension MLKEM1024 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM1024 {
|
||||
/// A ML-KEM-1024 public key.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct InternalPublicKey: @unchecked Sendable, KEMPublicKey {
|
||||
private var backing: Backing
|
||||
|
||||
@@ -504,26 +482,11 @@ extension MLKEM1024 {
|
||||
static let byteCount = Backing.byteCount
|
||||
|
||||
fileprivate final class Backing {
|
||||
// @_implementationOnly import => must use OpaquePointer for stored property.
|
||||
private let _storage: OpaquePointer
|
||||
var key: UnsafeMutablePointer<MLKEM1024_public_key> { UnsafeMutablePointer(self._storage) }
|
||||
var key: MLKEM1024_public_key
|
||||
|
||||
/// This is the only designated initializer, responsible for the allocation. Deallocation happens in deinit.
|
||||
/// To reduce mistakes (e.g. double-free), all other initializers should be convenience initializers.
|
||||
private init(takingOwnershipOf pointer: UnsafeMutablePointer<MLKEM1024_public_key>) {
|
||||
self._storage = OpaquePointer(pointer)
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.key.deinitialize(count: 1)
|
||||
self.key.deallocate()
|
||||
}
|
||||
|
||||
convenience init(privateKeyBacking: InternalPrivateKey.Backing) {
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM1024_public_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM1024_public_key())
|
||||
CCryptoBoringSSL_MLKEM1024_public_from_private(keyPtr, privateKeyBacking.key)
|
||||
self.init(takingOwnershipOf: keyPtr)
|
||||
init(privateKeyBacking: InternalPrivateKey.Backing) {
|
||||
self.key = .init()
|
||||
CCryptoBoringSSL_MLKEM1024_public_from_private(&self.key, &privateKeyBacking.key)
|
||||
}
|
||||
|
||||
/// Initialize a ML-KEM-1024 public key from a raw representation.
|
||||
@@ -531,13 +494,12 @@ extension MLKEM1024 {
|
||||
/// - Parameter rawRepresentation: The public key bytes.
|
||||
///
|
||||
/// - Throws: `CryptoKitError.incorrectKeySize` if the raw representation is not the correct size.
|
||||
convenience init(rawRepresentation: some DataProtocol) throws {
|
||||
init(rawRepresentation: some DataProtocol) throws {
|
||||
guard rawRepresentation.count == MLKEM1024.InternalPublicKey.byteCount else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM1024_public_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM1024_public_key())
|
||||
self.init(takingOwnershipOf: keyPtr)
|
||||
|
||||
self.key = .init()
|
||||
|
||||
let bytes: ContiguousBytes =
|
||||
rawRepresentation.regions.count == 1
|
||||
@@ -546,7 +508,7 @@ extension MLKEM1024 {
|
||||
try bytes.withUnsafeBytes { rawBuffer in
|
||||
try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in
|
||||
var cbs = CBS(data: buffer.baseAddress, len: buffer.count)
|
||||
guard CCryptoBoringSSL_MLKEM1024_parse_public_key(self.key, &cbs) == 1 else {
|
||||
guard CCryptoBoringSSL_MLKEM1024_parse_public_key(&self.key, &cbs) == 1 else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
@@ -559,7 +521,7 @@ extension MLKEM1024 {
|
||||
// The following BoringSSL functions can only fail on allocation failure, which we define as impossible.
|
||||
CCryptoBoringSSL_CBB_init(&cbb, MLKEM1024.InternalPublicKey.Backing.byteCount)
|
||||
defer { CCryptoBoringSSL_CBB_cleanup(&cbb) }
|
||||
CCryptoBoringSSL_MLKEM1024_marshal_public_key(&cbb, self.key)
|
||||
CCryptoBoringSSL_MLKEM1024_marshal_public_key(&cbb, &self.key)
|
||||
return Data(bytes: CCryptoBoringSSL_CBB_data(&cbb), count: CCryptoBoringSSL_CBB_len(&cbb))
|
||||
}
|
||||
|
||||
@@ -578,7 +540,7 @@ extension MLKEM1024 {
|
||||
CCryptoBoringSSL_MLKEM1024_encap(
|
||||
encapsulatedPtr.baseAddress,
|
||||
secretPtr.baseAddress,
|
||||
self.key
|
||||
&self.key
|
||||
)
|
||||
|
||||
return KEM.EncapsulationResult(
|
||||
@@ -606,6 +568,7 @@ extension MLKEM1024 {
|
||||
private static let ciphertextByteCount = Int(MLKEM1024_CIPHERTEXT_BYTES)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum MLKEM {
|
||||
/// The size of the seed in bytes.
|
||||
static let seedByteCount = 64
|
||||
|
||||
@@ -83,36 +83,29 @@ extension MLKEM${parameter_set} {
|
||||
}
|
||||
|
||||
fileprivate final class Backing {
|
||||
// @_implementationOnly import => must use OpaquePointer for stored property.
|
||||
private let _storage: OpaquePointer
|
||||
var key: UnsafeMutablePointer<MLKEM${parameter_set}_private_key> {
|
||||
UnsafeMutablePointer(self._storage)
|
||||
}
|
||||
var key: MLKEM${parameter_set}_private_key
|
||||
var seed: Data
|
||||
|
||||
/// This is the only designated initializer, responsible for the allocation. Deallocation happens in deinit.
|
||||
/// To reduce mistakes (e.g. double-free), all other initializers should be convenience initializers.
|
||||
private init(takingOwnershipOf pointer: UnsafeMutablePointer<MLKEM${parameter_set}_private_key>, seed: Data) {
|
||||
self._storage = OpaquePointer(pointer)
|
||||
self.seed = seed
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.key.deinitialize(count: 1)
|
||||
self.key.deallocate()
|
||||
}
|
||||
/// Initialize a ML-KEM-${parameter_set} private key from a random seed.
|
||||
convenience init() {
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM${parameter_set}_private_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM${parameter_set}_private_key())
|
||||
self.init(takingOwnershipOf: keyPtr, seed: Data(repeating:0, count: MLKEM.seedByteCount))
|
||||
init() {
|
||||
self.key = .init()
|
||||
self.seed = Data()
|
||||
|
||||
seed.withUnsafeMutableBytes { seedPtr in
|
||||
self.seed = withUnsafeTemporaryAllocation(
|
||||
of: UInt8.self,
|
||||
capacity: MLKEM.seedByteCount
|
||||
) { seedPtr in
|
||||
withUnsafeTemporaryAllocation(
|
||||
of: UInt8.self,
|
||||
capacity: MLKEM${parameter_set}.InternalPublicKey.byteCount
|
||||
) { publicKeyPtr in
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_generate_key(publicKeyPtr.baseAddress, seedPtr.baseAddress, keyPtr)
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_generate_key(
|
||||
publicKeyPtr.baseAddress,
|
||||
seedPtr.baseAddress,
|
||||
&self.key
|
||||
)
|
||||
|
||||
return Data(bytes: seedPtr.baseAddress!, count: MLKEM.seedByteCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,19 +115,23 @@ extension MLKEM${parameter_set} {
|
||||
/// - Parameter seedRepresentation: The seed to use to generate the private key.
|
||||
///
|
||||
/// - Throws: `CryptoKitError.incorrectKeySize` if the seed is not 64 bytes long.
|
||||
convenience init(seedRepresentation: some DataProtocol) throws {
|
||||
init(seedRepresentation: some DataProtocol) throws {
|
||||
guard seedRepresentation.count == MLKEM.seedByteCount else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM${parameter_set}_private_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM${parameter_set}_private_key())
|
||||
self.key = .init()
|
||||
self.seed = Data(seedRepresentation)
|
||||
|
||||
self.init(takingOwnershipOf: keyPtr, seed: Data(seedRepresentation))
|
||||
|
||||
guard self.seed.withUnsafeBytes({ seedPtr in
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_private_key_from_seed(keyPtr, seedPtr.baseAddress, seedPtr.count)
|
||||
}) == 1 else {
|
||||
guard
|
||||
self.seed.withUnsafeBytes({ seedPtr in
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_private_key_from_seed(
|
||||
&self.key,
|
||||
seedPtr.baseAddress,
|
||||
seedPtr.count
|
||||
)
|
||||
}) == 1
|
||||
else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
@@ -168,7 +165,7 @@ extension MLKEM${parameter_set} {
|
||||
symmetricKeyDataPtr.baseAddress,
|
||||
encapsulatedPtr.baseAddress,
|
||||
encapsulatedPtr.count,
|
||||
self.key
|
||||
&self.key
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -218,26 +215,11 @@ extension MLKEM${parameter_set} {
|
||||
static let byteCount = Backing.byteCount
|
||||
|
||||
fileprivate final class Backing {
|
||||
// @_implementationOnly import => must use OpaquePointer for stored property.
|
||||
private let _storage: OpaquePointer
|
||||
var key: UnsafeMutablePointer<MLKEM${parameter_set}_public_key> { UnsafeMutablePointer(self._storage) }
|
||||
var key: MLKEM${parameter_set}_public_key
|
||||
|
||||
/// This is the only designated initializer, responsible for the allocation. Deallocation happens in deinit.
|
||||
/// To reduce mistakes (e.g. double-free), all other initializers should be convenience initializers.
|
||||
private init(takingOwnershipOf pointer: UnsafeMutablePointer<MLKEM${parameter_set}_public_key>) {
|
||||
self._storage = OpaquePointer(pointer)
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.key.deinitialize(count: 1)
|
||||
self.key.deallocate()
|
||||
}
|
||||
|
||||
convenience init(privateKeyBacking: InternalPrivateKey.Backing) {
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM${parameter_set}_public_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM${parameter_set}_public_key())
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_public_from_private(keyPtr, privateKeyBacking.key)
|
||||
self.init(takingOwnershipOf: keyPtr)
|
||||
init(privateKeyBacking: InternalPrivateKey.Backing) {
|
||||
self.key = .init()
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_public_from_private(&self.key, &privateKeyBacking.key)
|
||||
}
|
||||
|
||||
/// Initialize a ML-KEM-${parameter_set} public key from a raw representation.
|
||||
@@ -245,13 +227,12 @@ extension MLKEM${parameter_set} {
|
||||
/// - Parameter rawRepresentation: The public key bytes.
|
||||
///
|
||||
/// - Throws: `CryptoKitError.incorrectKeySize` if the raw representation is not the correct size.
|
||||
convenience init(rawRepresentation: some DataProtocol) throws {
|
||||
init(rawRepresentation: some DataProtocol) throws {
|
||||
guard rawRepresentation.count == MLKEM${parameter_set}.InternalPublicKey.byteCount else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
let keyPtr = UnsafeMutablePointer<MLKEM${parameter_set}_public_key>.allocate(capacity: 1)
|
||||
keyPtr.initialize(to: MLKEM${parameter_set}_public_key())
|
||||
self.init(takingOwnershipOf: keyPtr)
|
||||
|
||||
self.key = .init()
|
||||
|
||||
let bytes: ContiguousBytes =
|
||||
rawRepresentation.regions.count == 1
|
||||
@@ -260,7 +241,7 @@ extension MLKEM${parameter_set} {
|
||||
try bytes.withUnsafeBytes { rawBuffer in
|
||||
try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in
|
||||
var cbs = CBS(data: buffer.baseAddress, len: buffer.count)
|
||||
guard CCryptoBoringSSL_MLKEM${parameter_set}_parse_public_key(self.key, &cbs) == 1 else {
|
||||
guard CCryptoBoringSSL_MLKEM${parameter_set}_parse_public_key(&self.key, &cbs) == 1 else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
@@ -273,7 +254,7 @@ extension MLKEM${parameter_set} {
|
||||
// The following BoringSSL functions can only fail on allocation failure, which we define as impossible.
|
||||
CCryptoBoringSSL_CBB_init(&cbb, MLKEM${parameter_set}.InternalPublicKey.Backing.byteCount)
|
||||
defer { CCryptoBoringSSL_CBB_cleanup(&cbb) }
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_marshal_public_key(&cbb, self.key)
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_marshal_public_key(&cbb, &self.key)
|
||||
return Data(bytes: CCryptoBoringSSL_CBB_data(&cbb), count: CCryptoBoringSSL_CBB_len(&cbb))
|
||||
}
|
||||
|
||||
@@ -292,7 +273,7 @@ extension MLKEM${parameter_set} {
|
||||
CCryptoBoringSSL_MLKEM${parameter_set}_encap(
|
||||
encapsulatedPtr.baseAddress,
|
||||
secretPtr.baseAddress,
|
||||
self.key
|
||||
&self.key
|
||||
)
|
||||
|
||||
return KEM.EncapsulationResult(
|
||||
|
||||
Reference in new issue
Block a user