Files
+6 431013afaf Update to WWDC25 release (#417)
This change updates Crypto to WWDC25 equivalent code.

Each of these changes were individually merged to the WWDC25 branch.
This includes:

- #359
- #363
- #365
- #367
- #368
- #397
- #403
- #410
- #407
- #401
- #415

---------

Co-authored-by: YourMJK <37852512+YourMJK@users.noreply.github.com>
Co-authored-by: Si Beaumont <beaumont@apple.com>
Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
Co-authored-by: Raphael <josephnoir@users.noreply.github.com>
Co-authored-by: Evan Wilde <ewilde@apple.com>
Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
Co-authored-by: Gus Cairo <gus.cairo@apple.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Fabrice de Gans <Steelskin@users.noreply.github.com>
Co-authored-by: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com>
Co-authored-by: Jesse L. Zamora <xtremekforever@gmail.com>
Co-authored-by: aryan-25 <aryan_shah@apple.com>
Co-authored-by: Gwynne Raskind <gwynne@vapor.codes>
Co-authored-by: 3405691582 <dsk@google.com>
Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com>
2025-09-26 11:55:11 +01:00

128 lines
5.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftCrypto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if !canImport(Darwin) || canImport(CryptoKit, _version: 324.0.4)
import Crypto
import XCTest
@testable import CryptoExtras
final class MLKEMTests: XCTestCase {
func testMLKEM768() throws {
guard #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, visionOS 3.0, *) else {
throw XCTSkip("MLDSA is only available on iOS 19.0+, macOS 16.0+, watchOS 12.0+, tvOS 19.0+, visionOS 3.0+")
}
// Generate a key pair
let privateKey = try MLKEM768.PrivateKey()
let publicKey = privateKey.publicKey
// Serialize and deserialize the private key
let seed = privateKey.seedRepresentation
let privateKey2 = try MLKEM768.PrivateKey(seedRepresentation: seed, publicKey: nil)
XCTAssertEqual(privateKey.seedRepresentation, privateKey2.seedRepresentation)
// Serialize and deserialize the public key
let publicKeyBytes = publicKey.rawRepresentation
var modifiedPublicKeyBytes = publicKeyBytes
modifiedPublicKeyBytes[0] = 0xff
modifiedPublicKeyBytes[1] = 0xff
// Parsing should fail because the first coefficient is >= kPrime. The check may be delayed
// to the encapsulation stage.
XCTAssertThrowsError(
try {
let pk = try MLKEM768.PublicKey(rawRepresentation: modifiedPublicKeyBytes)
_ = try pk.encapsulate()
}()
)
let publicKey2 = try MLKEM768.PublicKey(rawRepresentation: publicKeyBytes)
XCTAssertEqual(publicKeyBytes, publicKey2.rawRepresentation)
// Ensure public key derived from private key matches the original public key
let derivedPublicKey = privateKey.publicKey
XCTAssertEqual(publicKeyBytes, derivedPublicKey.rawRepresentation)
// Serialize and deserialize the private key with modifications
var modifiedSeed = privateKey.seedRepresentation
modifiedSeed[0] = 0xff
modifiedSeed[1] = 0xff
XCTAssertNotEqual(
try MLKEM768.PrivateKey(seedRepresentation: modifiedSeed, publicKey: nil).publicKey.rawRepresentation,
publicKeyBytes
)
// Encapsulation and decapsulation
let encapsulationResult = try publicKey.encapsulate()
let sharedSecret1 = encapsulationResult.sharedSecret
let ciphertext = encapsulationResult.encapsulated
let sharedSecret2 = try privateKey.decapsulate(ciphertext)
XCTAssertEqual(sharedSecret1, sharedSecret2)
}
func testMLKEM1024() throws {
guard #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, visionOS 3.0, *) else {
throw XCTSkip("MLDSA is only available on iOS 19.0+, macOS 16.0+, watchOS 12.0+, tvOS 19.0+, visionOS 3.0+")
}
// Generate a key pair
let privateKey = try MLKEM1024.PrivateKey()
let publicKey = privateKey.publicKey
// Serialize and deserialize the private key
let seed = privateKey.seedRepresentation
let privateKey2 = try MLKEM1024.PrivateKey(seedRepresentation: seed, publicKey: nil)
XCTAssertEqual(privateKey.seedRepresentation, privateKey2.seedRepresentation)
// Serialize and deserialize the public key
let publicKeyBytes = publicKey.rawRepresentation
var modifiedPublicKeyBytes = publicKeyBytes
modifiedPublicKeyBytes[0] = 0xff
modifiedPublicKeyBytes[1] = 0xff
// Parsing should fail because the first coefficient is >= kPrime. The check may be delayed
// to the encapsulation stage.
XCTAssertThrowsError(
try {
let pk = try MLKEM1024.PublicKey(rawRepresentation: modifiedPublicKeyBytes)
_ = try pk.encapsulate()
}()
)
let publicKey2 = try MLKEM1024.PublicKey(rawRepresentation: publicKeyBytes)
XCTAssertEqual(publicKeyBytes, publicKey2.rawRepresentation)
// Ensure public key derived from private key matches the original public key
let derivedPublicKey = privateKey.publicKey
XCTAssertEqual(publicKeyBytes, derivedPublicKey.rawRepresentation)
// Serialize and deserialize the private key with modifications
var modifiedSeed = privateKey.seedRepresentation
modifiedSeed[0] = 0xff
modifiedSeed[1] = 0xff
XCTAssertNotEqual(
try MLKEM1024.PrivateKey(seedRepresentation: modifiedSeed, publicKey: nil).publicKey.rawRepresentation,
publicKeyBytes
)
// Encapsulation and decapsulation
let encapsulationResult = try publicKey.encapsulate()
let sharedSecret1 = encapsulationResult.sharedSecret
let ciphertext = encapsulationResult.encapsulated
let sharedSecret2 = try privateKey.decapsulate(ciphertext)
XCTAssertEqual(sharedSecret1, sharedSecret2)
}
}
#endif // SDK has MLKEM