mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb4ba815da |
No files matched your search
@@ -236,7 +236,11 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
}
|
||||
|
||||
func decapsulate(_ encapsulated: Data) throws -> SymmetricKey {
|
||||
try SymmetricKey(unsafeUninitializedCapacity: Int(XWING_SHARED_SECRET_BYTES)) { sharedSecretBytes, count in
|
||||
guard encapsulated.count == Int(XWING_CIPHERTEXT_BYTES) else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
return try SymmetricKey(unsafeUninitializedCapacity: Int(XWING_SHARED_SECRET_BYTES)) { sharedSecretBytes, count in
|
||||
try encapsulated.withUnsafeBytes { encapsulatedSecretBytes in
|
||||
let rc = CCryptoBoringSSL_XWING_decap(
|
||||
sharedSecretBytes.baseAddress,
|
||||
|
||||
@@ -84,6 +84,39 @@ final class XWingTests: XCTestCase {
|
||||
let importedKey = try XWingMLKEM768X25519.PrivateKey.init(integrityCheckedRepresentation: exportedFormat)
|
||||
XCTAssertEqual(importedKey.seedRepresentation, privateKey.seedRepresentation)
|
||||
}
|
||||
|
||||
func testDecapsulateInputValidation() throws {
|
||||
let ciphersuite = HPKE.Ciphersuite.XWingMLKEM768X25519_SHA256_AES_GCM_256
|
||||
let skR = try XWingMLKEM768X25519.PrivateKey.generate()
|
||||
|
||||
// Dummy key with the correct size fails with an error from the underlying implementation.
|
||||
let corretlySizedKey = Data(repeating: 0x00, count: 1120)
|
||||
XCTAssertThrowsError(
|
||||
try HPKE.Recipient(
|
||||
privateKey: skR,
|
||||
ciphersuite: ciphersuite,
|
||||
info: Data(),
|
||||
encapsulatedKey: corretlySizedKey
|
||||
),
|
||||
error: CryptoKitError.underlyingCoreCryptoError(error: 0)
|
||||
)
|
||||
|
||||
// Keys with the wrong size fail input validation.
|
||||
let keySizesToTest = [0, 1, 1119, 1221, 2000]
|
||||
for keySize in keySizesToTest {
|
||||
let wronglySizedKey = Data(repeating: 0x00, count: keySize)
|
||||
XCTAssertThrowsError(
|
||||
try HPKE.Recipient(
|
||||
privateKey: skR,
|
||||
ciphersuite: ciphersuite,
|
||||
info: Data(),
|
||||
encapsulatedKey: wronglySizedKey
|
||||
),
|
||||
error: CryptoKitError.incorrectParameterSize,
|
||||
"Unexpectedly returned from malformed decapsulation path for keySize \(keySize)"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Struct to parse KAT file
|
||||
|
||||
Reference in new issue
Block a user