diff --git a/Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift b/Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift index 9e3b6d2a..fd5526ad 100644 --- a/Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift +++ b/Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift @@ -88,21 +88,6 @@ extension AES { ) } - // Note: historical version of the above, which should be removed once - // the above is API and clients move over to it. - #if swift(<6.3) - @_lifetime(message: copy message) - #endif - internal static func seal( - inplace message: inout MutableRawSpan, - using key: SymmetricKey, - nonce: RawSpan, - authenticating authenticatedData: RawSpan? = nil, - tag: inout OutputRawSpan - ) throws(CryptoKitMetaError) { - return try AESGCMImpl.seal(key: key, message: &message, nonce: nonce, authenticatedData: authenticatedData, tag: &tag) - } - /// Decrypts the message and verifies the authenticity of both the /// encrypted message and additional data. /// @@ -161,28 +146,6 @@ extension AES { tag: tag ) } - - // Note: historical version of the above, which should be removed once - // the above is API and clients move over to it. - #if swift(<6.3) - @_lifetime(message: copy message) - #endif - internal static func open( - inplace message: inout MutableRawSpan, - using key: SymmetricKey, - nonce: RawSpan, - tag: RawSpan, - authenticating authenticatedData: RawSpan? = nil - ) throws(CryptoKitMetaError) { - try AESGCMImpl - .open( - key: key, - message: &message, - nonce: nonce, - authenticatedData: authenticatedData, - tag: tag - ) - } } } diff --git a/Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift b/Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift index db63ca5c..b1661e5b 100644 --- a/Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift +++ b/Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift @@ -83,21 +83,6 @@ public enum ChaChaPoly: Cipher, Sendable { try ChaChaPolyImpl.encrypt(key: key, inPlace: &message, nonce: nonce.bytes, authenticatedData: authenticatedData, tag: &tag) } - // Note: historical version of the above, which should be removed once - // the above is API and clients move over to it. - #if swift(<6.3) - @_lifetime(message: copy message) - #endif - internal static func seal( - inplace message: inout MutableRawSpan, - using key: SymmetricKey, - nonce: RawSpan, - authenticating authenticatedData: RawSpan? = nil, - tag: inout OutputRawSpan - ) throws(CryptoKitMetaError) { - try ChaChaPolyImpl.encrypt(key: key, inPlace: &message, nonce: nonce, authenticatedData: authenticatedData, tag: &tag) - } - /// Decrypts the message and verifies the authenticity of both the encrypted /// message and additional data. /// @@ -151,21 +136,6 @@ public enum ChaChaPoly: Cipher, Sendable { ) throws(CryptoKitMetaError) { try ChaChaPolyImpl.decrypt(key: key, inPlace: &message, nonce: nonce.bytes, tag: tag, authenticatedData: authenticatedData) } - - // Note: historical version of the above, which should be removed once - // the above is API and clients move over to it. - #if swift(<6.3) - @_lifetime(message: copy message) - #endif - public static func open( - inplace message: inout MutableRawSpan, - using key: SymmetricKey, - nonce: RawSpan, - tag: RawSpan, - authenticating authenticatedData: RawSpan? = nil - ) throws(CryptoKitMetaError) { - try ChaChaPolyImpl.decrypt(key: key, inPlace: &message, nonce: nonce, tag: tag, authenticatedData: authenticatedData) - } } extension ChaChaPoly { diff --git a/Sources/Crypto/KEM/MLKEM.swift b/Sources/Crypto/KEM/MLKEM.swift index 2cccab71..16585ae3 100644 --- a/Sources/Crypto/KEM/MLKEM.swift +++ b/Sources/Crypto/KEM/MLKEM.swift @@ -23,6 +23,7 @@ public import Foundation typealias MLKEMPublicKeyImpl = OpenSSLMLKEMPublicKeyImpl typealias MLKEMPrivateKeyImpl = OpenSSLMLKEMPrivateKeyImpl +typealias MLKEMOneTimePrivateKeyImpl = OpenSSLMLKEMPrivateKeyImpl /// The Module-Lattice key encapsulation mechanism (KEM). @@ -150,15 +151,25 @@ extension MLKEM768 { /// The associated decapsulation function can be multiple times faster than the one implemented for PrivateKey, /// but this private key can only be used to decapsulate a shared secret once. public struct OneTimePrivateKey: KEMOneTimePrivateKey, ~Copyable { - internal let impl: MLKEMPrivateKeyImpl + internal let impl: MLKEMOneTimePrivateKeyImpl - internal init(_ impl: MLKEMPrivateKeyImpl) { + internal init(_ impl: MLKEMOneTimePrivateKeyImpl) { self.impl = impl } + /// Creates a one-time-use private key that reuses the key material of an existing private key. + /// + /// This is used by the tests to check that the one-time decapsulation function is consistent with the regular one. + internal init(reusingForTestingOnly privateKey: MLKEM768.PrivateKey) { + self.impl = try! MLKEMOneTimePrivateKeyImpl( + seedRepresentation: privateKey.impl.seedRepresentation, + publicKeyRawRepresentation: privateKey.impl.publicKey.rawRepresentation + ) + } + /// Generates a new, random one-time-use private key. public static func generate() throws -> MLKEM768.OneTimePrivateKey { - let impl = try MLKEMPrivateKeyImpl.generatePrivateKey() + let impl = try MLKEMOneTimePrivateKeyImpl.generatePrivateKey() return OneTimePrivateKey(impl) } @@ -311,15 +322,25 @@ extension MLKEM1024 { /// The associated decapsulation function can be multiple times faster than the one implemented for PrivateKey, /// but this private key can only be used to decapsulate a shared secret once. public struct OneTimePrivateKey: KEMOneTimePrivateKey, ~Copyable { - internal let impl: MLKEMPrivateKeyImpl + internal let impl: MLKEMOneTimePrivateKeyImpl - internal init(_ impl: MLKEMPrivateKeyImpl) { + internal init(_ impl: MLKEMOneTimePrivateKeyImpl) { self.impl = impl } + /// Creates a one-time-use private key that reuses the key material of an existing private key. + /// + /// This is used by the tests to check that the one-time decapsulation function is consistent with the regular one. + internal init(reusingForTestingOnly privateKey: MLKEM1024.PrivateKey) { + self.impl = try! MLKEMOneTimePrivateKeyImpl( + seedRepresentation: privateKey.impl.seedRepresentation, + publicKeyRawRepresentation: privateKey.impl.publicKey.rawRepresentation + ) + } + /// Generates a new, random one-time-use private key. public static func generate() throws -> MLKEM1024.OneTimePrivateKey { - let impl = try MLKEMPrivateKeyImpl.generatePrivateKey() + let impl = try MLKEMOneTimePrivateKeyImpl.generatePrivateKey() return OneTimePrivateKey(impl) } diff --git a/Sources/Crypto/KEM/XWing.swift b/Sources/Crypto/KEM/XWing.swift index 0cdb7ef6..435bac51 100644 --- a/Sources/Crypto/KEM/XWing.swift +++ b/Sources/Crypto/KEM/XWing.swift @@ -58,7 +58,7 @@ extension XWingMLKEM768X25519 { } public struct PrivateKey: KEMPrivateKey { - private var impl: XWingPrivateKeyImpl + var impl: XWingPrivateKeyImpl public var seedRepresentation: Data { get { @@ -106,6 +106,13 @@ extension XWingMLKEM768X25519 { self.impl = impl } + /// Creates a one-time-use private key that reuses the key material of an existing private key. + /// + /// This is used by the tests to check that the one-time decapsulation function is consistent with the regular one. + internal init(reusingForTestingOnly privateKey: XWingMLKEM768X25519.PrivateKey) { + self.impl = privateKey.impl + } + /// Generates a new, random one-time-use private key. public static func generate() throws -> XWingMLKEM768X25519.OneTimePrivateKey { let impl = try XWingPrivateKeyImpl.generate() diff --git a/Tests/CryptoTests/KEM/MLKEMTests.swift b/Tests/CryptoTests/KEM/MLKEMTests.swift index ca7479f7..b5899a9d 100644 --- a/Tests/CryptoTests/KEM/MLKEMTests.swift +++ b/Tests/CryptoTests/KEM/MLKEMTests.swift @@ -19,6 +19,34 @@ import XCTest @testable import Crypto final class MLKEMTests: XCTestCase { + /// Decapsulates with the regular private key and also with a one-time private key built from the + /// same key material, asserting that both paths recover the same shared secret. Returns the shared + /// secret so callers can run their own assertions (e.g. against a known-answer value), guaranteeing + /// the one-time and regular decapsulation functions are tested identically everywhere. + private func decapsulate( + with privateKey: MLKEM768.PrivateKey, + _ encapsulated: Data, + file: StaticString = #filePath, + line: UInt = #line + ) throws -> SymmetricKey { + let sharedSecret = try privateKey.decapsulate(encapsulated) + let oneTimeSharedSecret = try MLKEM768.OneTimePrivateKey(reusingForTestingOnly: privateKey).decapsulate(encapsulated) + XCTAssertEqual(sharedSecret, oneTimeSharedSecret, "one-time decapsulation diverged from regular decapsulation", file: file, line: line) + return sharedSecret + } + + private func decapsulate( + with privateKey: MLKEM1024.PrivateKey, + _ encapsulated: Data, + file: StaticString = #filePath, + line: UInt = #line + ) throws -> SymmetricKey { + let sharedSecret = try privateKey.decapsulate(encapsulated) + let oneTimeSharedSecret = try MLKEM1024.OneTimePrivateKey(reusingForTestingOnly: privateKey).decapsulate(encapsulated) + XCTAssertEqual(sharedSecret, oneTimeSharedSecret, "one-time decapsulation diverged from regular decapsulation", file: file, line: line) + return sharedSecret + } + func testMLKEM768() throws { let privateKey = try MLKEM768.PrivateKey.generate() let publicKey = privateKey.publicKey @@ -31,7 +59,7 @@ final class MLKEMTests: XCTestCase { try XCTAssert(privateKey.integrityCheckedRepresentation == MLKEM768.PrivateKey(integrityCheckedRepresentation: privateKey.integrityCheckedRepresentation).integrityCheckedRepresentation) let er = try publicKey.encapsulate() - let ss = try privateKey.decapsulate(er.encapsulated) + let ss = try decapsulate(with: privateKey, er.encapsulated) XCTAssert(er.sharedSecret == ss) } @@ -48,24 +76,18 @@ final class MLKEMTests: XCTestCase { try XCTAssert(privateKey.integrityCheckedRepresentation == MLKEM1024.PrivateKey(integrityCheckedRepresentation: privateKey.integrityCheckedRepresentation).integrityCheckedRepresentation) let er = try publicKey.encapsulate() - let ss = try privateKey.decapsulate(er.encapsulated) + let ss = try decapsulate(with: privateKey, er.encapsulated) XCTAssert(er.sharedSecret == ss) } - func testOneTimeKeys() throws { - let privateKey = try MLKEM1024.OneTimePrivateKey.generate() - let publicKey = privateKey.publicKey + // Test that the private key API function throws on an invalid seed + func testPrivateKeyInitThrowsOnInvalidSeedLength768() { + XCTAssertThrowsError(try MLKEM768.PrivateKey(seedRepresentation: Data(count: 1), publicKey: nil)) + } - // Test encapsulation and decapsulation - let er = try publicKey.encapsulate() - let ss = try privateKey.decapsulate(er.encapsulated) - XCTAssert(er.sharedSecret == ss) - - // The following would (and should) produce a compile-time error - // let er2 = try publicKey.encapsulate() - // let ss2 = try privateKey.decapsulate(er.encapsulated) - // XCTAssert(er2.sharedSecret == ss2) + func testPrivateKeyInitThrowsOnInvalidSeedLength1024() { + XCTAssertThrowsError(try MLKEM1024.PrivateKey(seedRepresentation: Data(count: 1), publicKey: nil)) } func processKATFile(filename: String) throws -> [MLKEMKAT] { diff --git a/Tests/CryptoTests/KEM/XWingTests.swift b/Tests/CryptoTests/KEM/XWingTests.swift index 4df48b1c..d1dc89f7 100644 --- a/Tests/CryptoTests/KEM/XWingTests.swift +++ b/Tests/CryptoTests/KEM/XWingTests.swift @@ -19,6 +19,22 @@ import XCTest @testable import Crypto final class XWingTests: XCTestCase { + /// Decapsulates with the regular private key and also with a one-time private key built from the + /// same key material, asserting that both paths recover the same shared secret. Returns the shared + /// secret so callers can run their own assertions (e.g. against a known-answer value), guaranteeing + /// the one-time and regular decapsulation functions are tested identically everywhere. + private func decapsulate( + with privateKey: XWingMLKEM768X25519.PrivateKey, + _ encapsulated: Data, + file: StaticString = #filePath, + line: UInt = #line + ) throws -> SymmetricKey { + let sharedSecret = try privateKey.decapsulate(encapsulated) + let oneTimeSharedSecret = try XWingMLKEM768X25519.OneTimePrivateKey(reusingForTestingOnly: privateKey).decapsulate(encapsulated) + XCTAssertEqual(sharedSecret, oneTimeSharedSecret, "one-time decapsulation diverged from regular decapsulation", file: file, line: line) + return sharedSecret + } + func testKEM() throws { let privateKey = try XWingMLKEM768X25519.PrivateKey.generate() @@ -30,24 +46,10 @@ final class XWingTests: XCTestCase { try XCTAssert(privateKey.integrityCheckedRepresentation == XWingMLKEM768X25519.PrivateKey(integrityCheckedRepresentation: privateKey.integrityCheckedRepresentation).integrityCheckedRepresentation) let er = try privateKey.publicKey.encapsulate() - let ss = try privateKey.decapsulate(er.encapsulated) + let ss = try decapsulate(with: privateKey, er.encapsulated) XCTAssert(er.sharedSecret == ss) } - - func testOneTimeKeys() throws { - let privateKey = try XWingMLKEM768X25519.OneTimePrivateKey.generate() - let publicKey = privateKey.publicKey - - let er = try publicKey.encapsulate() - let ss = try privateKey.decapsulate(er.encapsulated) - XCTAssert(er.sharedSecret == ss) - - // The following would (and should) produce a compile-time error - // let er2 = try publicKey.encapsulate() - // let ss2 = try privateKey.decapsulate(er.encapsulated) - // XCTAssert(er2.sharedSecret == ss2) - } func processKATFile(filename: String) throws -> [XWingKAT] { let bundle = Bundle.module @@ -69,7 +71,7 @@ final class XWingTests: XCTestCase { let encapsulatedKey = try privateKey.publicKey.encapsulateWithRng(rngState: encapDrbg) XCTAssertEqual(encapsulatedKey.encapsulated, katTest.ct) XCTAssertEqual(encapsulatedKey.sharedSecret.dataRepresentation, katTest.ss) - let retrievedSharedSecret = try privateKey.decapsulate(encapsulatedKey.encapsulated) + let retrievedSharedSecret = try decapsulate(with: privateKey, encapsulatedKey.encapsulated) XCTAssertEqual(retrievedSharedSecret.dataRepresentation, katTest.ss) } }