Skip to content

Commit

Permalink
Add proper namespace for SHA2_128s parameter set
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Dec 12, 2024
1 parent b6a93d6 commit bf40b76
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
31 changes: 18 additions & 13 deletions Sources/_CryptoExtras/SLHDSA/SLHDSA_boring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,10 +73,10 @@ extension SLHDSA {

/// Initialize a SLH-DSA-SHA2-128s private key from a random seed.
init() {
self.pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: SLHDSA.PrivateKey.Backing.bytesCount)
self.pointer = UnsafeMutablePointer<UInt8>.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)
}
Expand All @@ -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<UInt8>.allocate(capacity: SLHDSA.PrivateKey.Backing.bytesCount)
self.pointer = UnsafeMutablePointer<UInt8>.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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -190,7 +195,7 @@ extension SLHDSA {
private let pointer: UnsafeMutablePointer<UInt8>

init(privateKeyBacking: PrivateKey.Backing) {
self.pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: SLHDSA.PublicKey.Backing.bytesCount)
self.pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: SLHDSA.SHA2_128s.PublicKey.Backing.bytesCount)
privateKeyBacking.withUnsafePointer { privateKeyPtr in
CCryptoBoringSSL_SLHDSA_SHA2_128S_public_from_private(self.pointer, privateKeyPtr)
}
Expand All @@ -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<UInt8>.allocate(capacity: SLHDSA.PublicKey.Backing.bytesCount)
self.pointer = UnsafeMutablePointer<UInt8>.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.
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 8 additions & 10 deletions Tests/_CryptoExtrasTests/SLHDSATests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -69,7 +67,7 @@ final class SLHDSATests: XCTestCase {
for i in 0..<encodedSignature.count {
for j in 0..<8 {
encodedSignature[i] ^= 1 << j
let modifiedSignature = SLHDSA.Signature(rawRepresentation: encodedSignature)
let modifiedSignature = SLHDSA.SHA2_128s.Signature(rawRepresentation: encodedSignature)
XCTAssertFalse(
publicKey.isValidSignature(modifiedSignature, for: message),
"Bit flip in signature at byte \(i) bit \(j) didn't cause a verification failure"
Expand All @@ -82,7 +80,7 @@ final class SLHDSATests: XCTestCase {
func testSignatureIsRandomized() throws {
let message = "Hello, world!".data(using: .utf8)!

let key = SLHDSA.PrivateKey()
let key = SLHDSA.SHA2_128s.PrivateKey()
let publicKey = key.publicKey

let signature1 = try key.signature(for: message)
Expand Down

0 comments on commit bf40b76

Please sign in to comment.