Hi @eskimo,
Thank you for your prompt reply. To clarify, at the minimum from my understand so feel free to correct any misinformation if presented, the Rust FastCrypto library use PKCS 1.*, while P256 (and it seems like CryptoKit as well) uses PSS signatures.
PKCS 1.* signatures are deterministic, while PSS are non-deterministic.
Linked here is what flagged to me that CryptoKit utilized PSS, specifically quoted:
The signing algorithm employs randomization to generate a different signature on every call, even for the same data and key.
I'm also sharing an updated code example without external dependencies.
The test case has the expected signature that is derived from Rust's FastCrypto signature function for P256:
import Foundation
import CryptoKit
func main() throws {
let pk = P256.Signing.PrivateKey()
let message = Data("Hello, world!".utf8)
let signature = try pk.signature(for: message)
let expectedSignature = Data([UInt8]([
38, 216, 71, 32, 101, 45, 139, 196, 221, 209, 152,
100, 52, 161, 11, 59, 123, 105, 240, 227, 90, 23,
198, 165, 152, 126, 109, 28, 186, 105, 101, 47, 67,
132, 163, 66, 72, 118, 66, 223, 94, 68, 89, 45,
48, 75, 234, 12, 235, 15, 174, 46, 52, 127, 163,
206, 197, 206, 26, 129, 68, 207, 187, 178
]))
if signature.rawRepresentation == expectedSignature {
print("Signature matches with Rust.")
} else {
print("Signature mismatch.\n")
print("Expected: \([UInt8](expectedSignature))\n")
print("Got: \([UInt8](signature.rawRepresentation))\n")
}
}
try! main()
Here are a couple print outs of the code for context as well to demonstrate the issue at hand:
Signature mismatch.
Expected: [38, 216, 71, 32, 101, 45, 139, 196, 221, 209, 152, 100, 52, 161, 11, 59, 123, 105, 240, 227, 90, 23, 198, 165, 152, 126, 109, 28, 186, 105, 101, 47, 67, 132, 163, 66, 72, 118, 66, 223, 94, 68, 89, 45, 48, 75, 234, 12, 235, 15, 174, 46, 52, 127, 163, 206, 197, 206, 26, 129, 68, 207, 187, 178]
Got: [3, 132, 62, 17, 173, 125, 251, 71, 167, 101, 152, 113, 63, 63, 214, 167, 87, 183, 132, 190, 49, 4, 45, 6, 101, 6, 195, 190, 245, 44, 129, 236, 173, 119, 187, 71, 223, 121, 127, 225, 93, 123, 0, 176, 157, 231, 65, 186, 46, 110, 243, 147, 56, 87, 216, 10, 30, 5, 229, 49, 234, 20, 61, 59]
Signature mismatch.
Expected: [38, 216, 71, 32, 101, 45, 139, 196, 221, 209, 152, 100, 52, 161, 11, 59, 123, 105, 240, 227, 90, 23, 198, 165, 152, 126, 109, 28, 186, 105, 101, 47, 67, 132, 163, 66, 72, 118, 66, 223, 94, 68, 89, 45, 48, 75, 234, 12, 235, 15, 174, 46, 52, 127, 163, 206, 197, 206, 26, 129, 68, 207, 187, 178]
Got: [245, 245, 34, 3, 104, 239, 95, 95, 193, 202, 39, 178, 83, 180, 173, 34, 118, 21, 146, 96, 226, 168, 188, 71, 179, 20, 228, 181, 224, 203, 176, 206, 146, 247, 227, 180, 72, 249, 112, 21, 23, 113, 67, 81, 176, 170, 94, 210, 120, 128, 174, 115, 78, 187, 16, 27, 116, 81, 107, 139, 84, 105, 44, 185]
My main question here is what can be done here to match CryptoKit's P256 implementation with that of Rust's FastCrypto P256 implementation. And if not, what other alternatives can be used to have deterministic signatures with either CryptoKit or SecKey.
Kindly,
MarcoDotIO
Topic:
Programming Languages
SubTopic:
Swift
Tags: