From bf40b76d438c166626720d220f35189cacc243f0 Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Thu, 12 Dec 2024 16:20:03 +0100 Subject: [PATCH] Add proper namespace for SHA2_128s parameter set --- .../_CryptoExtras/SLHDSA/SLHDSA_boring.swift | 31 +++++++++++-------- Tests/_CryptoExtrasTests/SLHDSATests.swift | 18 +++++------ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Sources/_CryptoExtras/SLHDSA/SLHDSA_boring.swift b/Sources/_CryptoExtras/SLHDSA/SLHDSA_boring.swift index ebe39af3..bd738a12 100644 --- a/Sources/_CryptoExtras/SLHDSA/SLHDSA_boring.swift +++ b/Sources/_CryptoExtras/SLHDSA/SLHDSA_boring.swift @@ -20,6 +20,11 @@ import Foundation public enum SLHDSA {} extension SLHDSA { + /// The SLH-DSA-SHA2-128s parameter set. + public enum SHA2_128s {} +} + +extension SLHDSA.SHA2_128s { /// A SLH-DSA-SHA2-128s private key. public struct PrivateKey: Sendable { private var backing: Backing @@ -68,10 +73,10 @@ extension SLHDSA { /// Initialize a SLH-DSA-SHA2-128s private key from a random seed. init() { - self.pointer = UnsafeMutablePointer.allocate(capacity: SLHDSA.PrivateKey.Backing.bytesCount) + self.pointer = UnsafeMutablePointer.allocate(capacity: SLHDSA.SHA2_128s.PrivateKey.Backing.bytesCount) withUnsafeTemporaryAllocation( - of: UInt8.self, capacity: SLHDSA.PublicKey.Backing.bytesCount + of: UInt8.self, capacity: SLHDSA.SHA2_128s.PublicKey.Backing.bytesCount ) { publicKeyPtr in CCryptoBoringSSL_SLHDSA_SHA2_128S_generate_key(publicKeyPtr.baseAddress, self.pointer) } @@ -83,20 +88,20 @@ extension SLHDSA { /// /// - Throws: `CryptoKitError.incorrectKeySize` if the raw representation is not the correct size. init(rawRepresentation: some DataProtocol) throws { - guard rawRepresentation.count == SLHDSA.PrivateKey.Backing.bytesCount else { + guard rawRepresentation.count == SLHDSA.SHA2_128s.PrivateKey.Backing.bytesCount else { throw CryptoKitError.incorrectKeySize } - self.pointer = UnsafeMutablePointer.allocate(capacity: SLHDSA.PrivateKey.Backing.bytesCount) + self.pointer = UnsafeMutablePointer.allocate(capacity: SLHDSA.SHA2_128s.PrivateKey.Backing.bytesCount) self.pointer.initialize( from: Array(rawRepresentation), - count: SLHDSA.PrivateKey.Backing.bytesCount + count: SLHDSA.SHA2_128s.PrivateKey.Backing.bytesCount ) } /// The raw representation of the private key. var rawRepresentation: Data { - Data(UnsafeBufferPointer(start: self.pointer, count: SLHDSA.PrivateKey.Backing.bytesCount)) + Data(UnsafeBufferPointer(start: self.pointer, count: SLHDSA.SHA2_128s.PrivateKey.Backing.bytesCount)) } /// The public key associated with this private key. @@ -151,7 +156,7 @@ extension SLHDSA { } } -extension SLHDSA { +extension SLHDSA.SHA2_128s { /// A SLH-DSA-SHA2-128s public key. public struct PublicKey: Sendable { private var backing: Backing @@ -190,7 +195,7 @@ extension SLHDSA { private let pointer: UnsafeMutablePointer init(privateKeyBacking: PrivateKey.Backing) { - self.pointer = UnsafeMutablePointer.allocate(capacity: SLHDSA.PublicKey.Backing.bytesCount) + self.pointer = UnsafeMutablePointer.allocate(capacity: SLHDSA.SHA2_128s.PublicKey.Backing.bytesCount) privateKeyBacking.withUnsafePointer { privateKeyPtr in CCryptoBoringSSL_SLHDSA_SHA2_128S_public_from_private(self.pointer, privateKeyPtr) } @@ -202,20 +207,20 @@ extension SLHDSA { /// /// - Throws: `CryptoKitError.incorrectKeySize` if the raw representation is not the correct size. init(rawRepresentation: some DataProtocol) throws { - guard rawRepresentation.count == SLHDSA.PublicKey.Backing.bytesCount else { + guard rawRepresentation.count == SLHDSA.SHA2_128s.PublicKey.Backing.bytesCount else { throw CryptoKitError.incorrectKeySize } - self.pointer = UnsafeMutablePointer.allocate(capacity: SLHDSA.PublicKey.Backing.bytesCount) + self.pointer = UnsafeMutablePointer.allocate(capacity: SLHDSA.SHA2_128s.PublicKey.Backing.bytesCount) self.pointer.initialize( from: Array(rawRepresentation), - count: SLHDSA.PublicKey.Backing.bytesCount + count: SLHDSA.SHA2_128s.PublicKey.Backing.bytesCount ) } /// The raw representation of the public key. var rawRepresentation: Data { - Data(UnsafeBufferPointer(start: self.pointer, count: SLHDSA.PublicKey.Backing.bytesCount)) + Data(UnsafeBufferPointer(start: self.pointer, count: SLHDSA.SHA2_128s.PublicKey.Backing.bytesCount)) } /// Verify a signature for the given data. @@ -262,7 +267,7 @@ extension SLHDSA { } } -extension SLHDSA { +extension SLHDSA.SHA2_128s { /// A SLH-DSA-SHA2-128s signature. public struct Signature: Sendable, ContiguousBytes { /// The raw binary representation of the signature. diff --git a/Tests/_CryptoExtrasTests/SLHDSATests.swift b/Tests/_CryptoExtrasTests/SLHDSATests.swift index 94e97b4a..932f1ab2 100644 --- a/Tests/_CryptoExtrasTests/SLHDSATests.swift +++ b/Tests/_CryptoExtrasTests/SLHDSATests.swift @@ -17,8 +17,8 @@ import XCTest @testable import _CryptoExtras final class SLHDSATests: XCTestCase { - func testSLHDSASigning() throws { - let key = SLHDSA.PrivateKey() + func testSLHDSA_SHA2_128sSigning() throws { + let key = SLHDSA.SHA2_128s.PrivateKey() let test = Data("Hello, World!".utf8) let signature = try key.signature(for: test) let context = Data("ctx".utf8) @@ -49,18 +49,16 @@ final class SLHDSATests: XCTestCase { func testSignatureSerialization() throws { let data = Array("Hello, World!".utf8) - let key = SLHDSA.PrivateKey() + let key = SLHDSA.SHA2_128s.PrivateKey() let signature = try key.signature(for: data) - let roundTripped = SLHDSA.Signature(rawRepresentation: signature.rawRepresentation) + let roundTripped = SLHDSA.SHA2_128s.Signature(rawRepresentation: signature.rawRepresentation) XCTAssertEqual(signature.rawRepresentation, roundTripped.rawRepresentation) XCTAssertTrue(key.publicKey.isValidSignature(roundTripped, for: data)) } - func testBitFlips() throws { - throw XCTSkip("This test is very slow, so it is disabled by default.") - + func _testBitFlips() throws { let message = "Hello, world!".data(using: .utf8)! - let key = SLHDSA.PrivateKey() + let key = SLHDSA.SHA2_128s.PrivateKey() let publicKey = key.publicKey let signature = try key.signature(for: message) XCTAssertTrue(publicKey.isValidSignature(signature, for: message)) @@ -69,7 +67,7 @@ final class SLHDSATests: XCTestCase { for i in 0..