Repository Public
1import Foundation2import QyvoraMath3import Testing45@testable import QyvoraIdentity67/// Regression tests of QYV-FUZ-001: `Identity(privateEncoding:)` used to accept an ML-DSA-87 private key whose t08/// component was damaged, because its check was one test signature and t0 only enters the hint.9@Suite struct IdentityPrivateKeyConsistencyTests {10 /// First byte of the ML-DSA-87 private key in the private encoding, and of its t0 component.11 static let mldsaStart = 4 + 1 + 4 + 4278 + 212 static let t0Start = mldsaStart + 32 + 32 + 64 + 672 + 76813 static let t0Size = 33281415 @Test func aDamagedT0IsRefusedWhereverTheDamageIs() throws {16 var encoding = People.alice.privateEncoding()17 defer { SecureMemory.zero(&encoding) }18 #expect(Self.t0Start + Self.t0Size == Self.mldsaStart + 4896)19 _ = try Identity(privateEncoding: encoding)20 for step in 0..<40 {21 var damaged = encoding22 damaged[Self.t0Start + (step * 83) % Self.t0Size] ^= UInt8(1 << (step % 8))23 #expect(throws: IdentityError.invalidKey) { _ = try Identity(privateEncoding: damaged) }24 }25 // the other components of the ML-DSA key, K excepted (it has no public counterpart: see QyvoraMLDSA)26 for offset in [0, 31, 64, 127, 128, 500, 800, 1_535] {27 var damaged = encoding28 damaged[Self.mldsaStart + offset] ^= 0x0129 #expect(throws: IdentityError.invalidKey) { _ = try Identity(privateEncoding: damaged) }30 }31 }3233 /// The input found by the fuzzer (a throw-away identity generated by it, t0 damaged at offset 6816 of the34 /// encoding): it was accepted and then signed invalidly.35 @Test func theRecordedFuzzerInputIsRefused() throws {36 let url = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()37 .appendingPathComponent("security/fuzzing/crashes/identity-private/0078fcc14a60fae97f42b858598d932604e3a8924da8aad4eb5edcffb19ea38c.bin")38 let input = Bytes(try Data(contentsOf: url))39 #expect(input.count == 12_519)40 #expect(throws: IdentityError.invalidKey) { _ = try Identity(privateEncoding: input) }41 }42}43