Skip to content

Commit 1d4f04f

Browse files
committed
Fix double-hashing bug
1 parent b79ea57 commit 1d4f04f

2 files changed

Lines changed: 7 additions & 11 deletions

File tree

Sources/ATCryptography/k256/K256Keypair.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ public struct K256Keypair: ExportableKeypair, Sendable {
9999
///
100100
/// - Throws: An error if signing fails.
101101
public func sign(message: [UInt8]) async throws -> [UInt8] {
102-
let hash = await SHA256Hasher.sha256(message)
103-
let signature = try privateKey.signature(for: Data(hash))
104-
return Array(signature.dataRepresentation)
102+
let signature = try privateKey.signature(for: Data(message))
103+
return Array(try signature.compactRepresentation)
105104
}
106105

107106
/// Exports the private key in raw byte format.

Sources/ATCryptography/k256/K256Operations.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public struct K256Operations {
4444
/// - Throws: An error if signature verification fails.
4545
public static func verifySignature(publicKey: [UInt8], data: [UInt8], signature: [UInt8], options: VerifyOptions? = nil) async throws -> Bool {
4646
let allowMalleable = options?.areMalleableSignaturesAllowed ?? false
47-
let hashedData = await SHA256Hasher.sha256(data)
4847

4948
guard let publicKey = try? secp256k1.Signing.PublicKey(dataRepresentation: publicKey, format: .compressed) else {
5049
throw EllipticalCurveOperationsError.invalidPublicKey
@@ -57,15 +56,15 @@ public struct K256Operations {
5756
throw EllipticalCurveOperationsError.invalidSignatureFormat
5857
}
5958

60-
guard let parsedSignature = try? secp256k1.Signing.ECDSASignature(dataRepresentation: signatureData) else {
59+
guard let parsedSignature = try? secp256k1.Signing.ECDSASignature(compactRepresentation: signatureData) else {
6160
return false
6261
}
6362

6463
guard let correctedSignature = parsedSignature.normalizedForK256() else {
6564
return false
6665
}
6766

68-
return publicKey.isValidSignature(correctedSignature, for: Data(hashedData))
67+
return publicKey.isValidSignature(correctedSignature, for: Data(data))
6968
}
7069

7170
/// Checks if a signature is in compact format.
@@ -75,11 +74,9 @@ public struct K256Operations {
7574
public static func isCompactFormat(_ signature: [UInt8]) -> Bool {
7675
// ECDSA k256 signatures should be exactly 64 bytes in compact form.
7776
do {
78-
// Attempt to initialize a P-256 signature from compact representation.
79-
let ecdsaSignature = try secp256k1.Signing.ECDSASignature(dataRepresentation: signature)
80-
81-
// Convert back to raw representation and compare with input
82-
return ecdsaSignature.dataRepresentation == signature.toData()
77+
// Attempt to parse as a compact representation.
78+
_ = try secp256k1.Signing.ECDSASignature(compactRepresentation: signature)
79+
return true
8380
} catch {
8481
return false
8582
}

0 commit comments

Comments
 (0)