diff --git a/Sources/_CryptoExtras/CMakeLists.txt b/Sources/_CryptoExtras/CMakeLists.txt index 71d8ae30..ae5ff43a 100644 --- a/Sources/_CryptoExtras/CMakeLists.txt +++ b/Sources/_CryptoExtras/CMakeLists.txt @@ -32,7 +32,7 @@ add_library(_CryptoExtras "Key Derivation/PBKDF2/PBKDF2.swift" "Key Derivation/Scrypt/BoringSSL/Scrypt_boring.swift" "Key Derivation/Scrypt/Scrypt.swift" - "MLDSA/MLDSA_boring.swift" + "MLDSA/MLDSA65_boring.swift" "OPRFs/OPRF.swift" "OPRFs/OPRFClient.swift" "OPRFs/OPRFServer.swift" @@ -47,8 +47,8 @@ add_library(_CryptoExtras "Util/CryptoKitErrors_boring.swift" "Util/DigestType.swift" "Util/Error.swift" - "Util/Optional+withUnsafeBytes.swift" "Util/I2OSP.swift" + "Util/Optional+withUnsafeBytes.swift" "Util/PEMDocument.swift" "Util/PrettyBytes.swift" "Util/SubjectPublicKeyInfo.swift" diff --git a/Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift b/Sources/_CryptoExtras/MLDSA/MLDSA65_boring.swift similarity index 95% rename from Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift rename to Sources/_CryptoExtras/MLDSA/MLDSA65_boring.swift index d15e0aa6..3059b3f6 100644 --- a/Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift +++ b/Sources/_CryptoExtras/MLDSA/MLDSA65_boring.swift @@ -17,9 +17,9 @@ import Crypto import Foundation /// A module-lattice-based digital signature algorithm that provides security against quantum computing attacks. -public enum MLDSA {} +public enum MLDSA65 {} -extension MLDSA { +extension MLDSA65 { /// A ML-DSA-65 private key. public struct PrivateKey: Sendable { private var backing: Backing @@ -74,11 +74,11 @@ extension MLDSA { self.seed = try withUnsafeTemporaryAllocation( of: UInt8.self, - capacity: MLDSA.seedSizeInBytes + capacity: MLDSA65.seedSizeInBytes ) { seedPtr in try withUnsafeTemporaryAllocation( of: UInt8.self, - capacity: MLDSA.PublicKey.Backing.bytesCount + capacity: MLDSA65.PublicKey.Backing.bytesCount ) { publicKeyPtr in guard CCryptoBoringSSL_MLDSA65_generate_key( @@ -90,7 +90,7 @@ extension MLDSA { throw CryptoKitError.internalBoringSSLError() } - return Data(bytes: seedPtr.baseAddress!, count: MLDSA.seedSizeInBytes) + return Data(bytes: seedPtr.baseAddress!, count: MLDSA65.seedSizeInBytes) } } } @@ -101,7 +101,7 @@ extension MLDSA { /// /// - Throws: `CryptoKitError.incorrectKeySize` if the seed is not 32 bytes long. init(seed: some DataProtocol) throws { - guard seed.count == MLDSA.seedSizeInBytes else { + guard seed.count == MLDSA65.seedSizeInBytes else { throw CryptoKitError.incorrectKeySize } @@ -113,7 +113,7 @@ extension MLDSA { CCryptoBoringSSL_MLDSA65_private_key_from_seed( &self.key, seedPtr.baseAddress, - MLDSA.seedSizeInBytes + MLDSA65.seedSizeInBytes ) }) == 1 else { @@ -164,7 +164,7 @@ extension MLDSA { } } -extension MLDSA { +extension MLDSA65 { /// A ML-DSA-65 public key. public struct PublicKey: Sendable { private var backing: Backing @@ -216,7 +216,7 @@ extension MLDSA { /// /// - Throws: `CryptoKitError.incorrectKeySize` if the raw representation is not the correct size. init(rawRepresentation: some DataProtocol) throws { - guard rawRepresentation.count == MLDSA.PublicKey.Backing.bytesCount else { + guard rawRepresentation.count == MLDSA65.PublicKey.Backing.bytesCount else { throw CryptoKitError.incorrectKeySize } @@ -240,7 +240,7 @@ extension MLDSA { var rawRepresentation: Data { var cbb = CBB() // The following BoringSSL functions can only fail on allocation failure, which we define as impossible. - CCryptoBoringSSL_CBB_init(&cbb, MLDSA.PublicKey.Backing.bytesCount) + CCryptoBoringSSL_CBB_init(&cbb, MLDSA65.PublicKey.Backing.bytesCount) defer { CCryptoBoringSSL_CBB_cleanup(&cbb) } CCryptoBoringSSL_MLDSA65_marshal_public_key(&cbb, &self.key) return Data(bytes: CCryptoBoringSSL_CBB_data(&cbb), count: CCryptoBoringSSL_CBB_len(&cbb)) @@ -280,7 +280,7 @@ extension MLDSA { } } -extension MLDSA { +extension MLDSA65 { /// A ML-DSA-65 signature. public struct Signature: Sendable, ContiguousBytes { /// The raw binary representation of the signature. @@ -314,7 +314,7 @@ extension MLDSA { } } -extension MLDSA { +extension MLDSA65 { /// The size of the seed in bytes. private static let seedSizeInBytes = 32 } diff --git a/Tests/_CryptoExtrasTests/MLDSATests.swift b/Tests/_CryptoExtrasTests/MLDSA65Tests.swift similarity index 79% rename from Tests/_CryptoExtrasTests/MLDSATests.swift rename to Tests/_CryptoExtrasTests/MLDSA65Tests.swift index c0317425..e95d2634 100644 --- a/Tests/_CryptoExtrasTests/MLDSATests.swift +++ b/Tests/_CryptoExtrasTests/MLDSA65Tests.swift @@ -16,14 +16,14 @@ import XCTest @testable import _CryptoExtras -final class MLDSATests: XCTestCase { - func testMLDSASigning() throws { - try testMLDSASigning(MLDSA.PrivateKey()) +final class MLDSA65Tests: XCTestCase { + func testMLDSA65Signing() throws { + try testMLDSA65Signing(MLDSA65.PrivateKey()) let seed: [UInt8] = (0..<32).map { _ in UInt8.random(in: 0...255) } - try testMLDSASigning(MLDSA.PrivateKey(seed: seed)) + try testMLDSA65Signing(MLDSA65.PrivateKey(seed: seed)) } - private func testMLDSASigning(_ key: MLDSA.PrivateKey) throws { + private func testMLDSA65Signing(_ key: MLDSA65.PrivateKey) throws { let test = "Hello, world!".data(using: .utf8)! try XCTAssertTrue( key.publicKey.isValidSignature( @@ -44,17 +44,17 @@ final class MLDSATests: XCTestCase { func testSignatureSerialization() throws { let data = Array("Hello, World!".utf8) - let key: MLDSA.PrivateKey = try MLDSA.PrivateKey() + let key: MLDSA65.PrivateKey = try MLDSA65.PrivateKey() let signature = try key.signature(for: data) - let roundTripped = MLDSA.Signature(rawRepresentation: signature.rawRepresentation) + let roundTripped = MLDSA65.Signature(rawRepresentation: signature.rawRepresentation) XCTAssertEqual(signature.rawRepresentation, roundTripped.rawRepresentation) XCTAssertTrue(key.publicKey.isValidSignature(roundTripped, for: data)) } func testSeedRoundTripping() throws { - let key = try MLDSA.PrivateKey() + let key = try MLDSA65.PrivateKey() let seed = key.seed - let roundTripped = try MLDSA.PrivateKey(seed: seed) + let roundTripped = try MLDSA65.PrivateKey(seed: seed) XCTAssertEqual(seed, roundTripped.seed) XCTAssertEqual(key.publicKey.rawRepresentation, roundTripped.publicKey.rawRepresentation) } @@ -62,7 +62,7 @@ final class MLDSATests: XCTestCase { // This test is very slow, so it is disabled by default. func _testBitFlips() throws { let message = "Hello, world!".data(using: .utf8)! - let key = try MLDSA.PrivateKey() + let key = try MLDSA65.PrivateKey() let publicKey = key.publicKey let signature = try key.signature(for: message) XCTAssertTrue(publicKey.isValidSignature(signature, for: message)) @@ -71,7 +71,7 @@ final class MLDSATests: XCTestCase { for i in 0..