mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
[5.x] Align with CryptoKit APIs from Xcode 27 beta 4 (#453)
## Motivation Xcode 27 beta 4 is now available[^1] with so we can tag a compatible release with the cross-platform API here. ## Modifications - Update shared API surface to align with CryptoKit ## Result - Aligned with CryptoKit APIs in latest Xcode beta release. [^1]: https://developer.apple.com/download/all/?q=Xcode%2027
This commit is contained in:
1 parent
8ee9962f37
commit
ce8db4769d
6 files changed
+89
-104
No files matched your search
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<MLKEM768>
|
||||
internal let impl: MLKEMOneTimePrivateKeyImpl<MLKEM768>
|
||||
|
||||
internal init(_ impl: MLKEMPrivateKeyImpl<MLKEM768>) {
|
||||
internal init(_ impl: MLKEMOneTimePrivateKeyImpl<MLKEM768>) {
|
||||
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<MLKEM768>(
|
||||
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<MLKEM768>.generatePrivateKey()
|
||||
let impl = try MLKEMOneTimePrivateKeyImpl<MLKEM768>.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<MLKEM1024>
|
||||
internal let impl: MLKEMOneTimePrivateKeyImpl<MLKEM1024>
|
||||
|
||||
internal init(_ impl: MLKEMPrivateKeyImpl<MLKEM1024>) {
|
||||
internal init(_ impl: MLKEMOneTimePrivateKeyImpl<MLKEM1024>) {
|
||||
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<MLKEM1024>(
|
||||
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<MLKEM1024>.generatePrivateKey()
|
||||
let impl = try MLKEMOneTimePrivateKeyImpl<MLKEM1024>.generatePrivateKey()
|
||||
return OneTimePrivateKey(impl)
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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] {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user