-
Notifications
You must be signed in to change notification settings - Fork 25
Add secp256k1 & brainpool EC curves #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
06133ed
be5b988
5469b0f
7495a8f
b62df0a
4114959
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,40 +24,94 @@ abstract class EcdsaTest(provider: CryptographyProvider) : AlgorithmTest<ECDSA>( | |
data class EcdsaSize( | ||
val curve: EC.Curve, | ||
val rawSignatureSize: Int, | ||
val derSignatureSizes: List<Int>, | ||
val derSignatureSizes: IntRange, | ||
val publicKeySize: Int, | ||
val privateKeySizes: List<Int>, | ||
) | ||
|
||
@Test | ||
fun testSizes() = testWithAlgorithm { | ||
listOf( | ||
EcdsaSize(EC.Curve.P256, 64, listOf(68, 69, 70, 71, 72), 91, listOf(67, 138, 150)), | ||
EcdsaSize(EC.Curve.P384, 96, listOf(100, 101, 102, 103, 104), 120, listOf(80, 185, 194)), | ||
EcdsaSize(EC.Curve.P521, 132, listOf(136, 137, 138, 139), 158, listOf(98, 241, 250)), | ||
EcdsaSize(EC.Curve("secp256k1"), 64, listOf(68, 69, 70, 71, 72), 88, listOf(135, 144)), | ||
// NIST curves | ||
EcdsaSize(EC.Curve.P256, 64, 68.rangeTo(72), 91, listOf(67, 138, 150)), | ||
EcdsaSize(EC.Curve.P384, 96, 100.rangeTo(104), 120, listOf(80, 185, 194)), | ||
EcdsaSize(EC.Curve.P521, 132, 136.rangeTo(139), 158, listOf(98, 241, 250)), | ||
|
||
// Note "private key sizes": smaller = openssl, larger = BouncyCastle | ||
|
||
// SECP256k1 | ||
EcdsaSize(EC.Curve.secp256k1, 64, 68.rangeTo(72), 88, listOf(135, 144)), | ||
|
||
// Brainpool curves | ||
EcdsaSize(EC.Curve.brainpoolP256r1, 64, 68.rangeTo(72), 92, listOf(139, 152)), | ||
EcdsaSize(EC.Curve.brainpoolP384r1, 96, 100.rangeTo(104), 124, listOf(189, 202)), | ||
EcdsaSize( | ||
EC.Curve.brainpoolP512r1, | ||
128, | ||
132.rangeTo(139), | ||
158, | ||
listOf(239, 252) | ||
) // Raw 128, DER sig slightly larger; PubKey ~154; PrivKey ~P521 | ||
|
||
|
||
).forEach { (curve, rawSignatureSize, derSignatureSizes, publicKeySize, privateKeySizes) -> | ||
if (!supportsCurve(curve)) return@forEach | ||
if (!supportsCurve(curve)) { | ||
println("Skipping size test for unsupported curve: ${curve.name}") | ||
return@forEach | ||
} | ||
|
||
println("\nRunning size test for curve: ${curve.name}") | ||
val keyPair = algorithm.keyPairGenerator(curve).generateKey() | ||
|
||
assertEquals(publicKeySize, keyPair.publicKey.encodeToByteString(EC.PublicKey.Format.DER).size) | ||
assertContains(privateKeySizes, keyPair.privateKey.encodeToByteString(EC.PrivateKey.Format.DER).size) | ||
val actualPublicKeySize = keyPair.publicKey.encodeToByteString(EC.PublicKey.Format.DER).size | ||
println("Got ${curve.name} public key size: $actualPublicKeySize (expected $publicKeySize)") | ||
assertEquals( | ||
publicKeySize, | ||
actualPublicKeySize, | ||
"Public key size mismatch for ${curve.name}, expected: $publicKeySize, but got $actualPublicKeySize" | ||
) | ||
val actualPrivateKeySize = keyPair.privateKey.encodeToByteString(EC.PrivateKey.Format.DER).size | ||
println("Got ${curve.name} private key size: $actualPrivateKeySize (allowed $privateKeySizes)") | ||
assertContains( | ||
privateKeySizes, | ||
actualPrivateKeySize, | ||
"Private key size mismatch for ${curve.name}, expected one of $privateKeySizes, but got $actualPrivateKeySize" | ||
) | ||
|
||
generateDigests { digest, _ -> | ||
if (!supportsDigest(digest)) return@generateDigests | ||
if (!supportsDigest(digest)) { | ||
println("Skipping digest $digest for curve ${curve.name}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please remove all such In case you think that the logging is necessary, please use |
||
return@generateDigests | ||
} | ||
|
||
// RAW signature | ||
run { | ||
val verifier = keyPair.publicKey.signatureVerifier(digest, ECDSA.SignatureFormat.RAW) | ||
keyPair.privateKey.signatureGenerator(digest, ECDSA.SignatureFormat.RAW).run { | ||
assertEquals(rawSignatureSize, generateSignature(ByteArray(0)).size) | ||
val sigEmpty = generateSignature(ByteArray(0)) | ||
assertEquals( | ||
rawSignatureSize, | ||
sigEmpty.size, | ||
"RAW signature size mismatch for empty data on ${curve.name} / ${digest.name}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Thanks for adding those! |
||
) | ||
assertTrue( | ||
verifier.tryVerifySignature(ByteArray(0), sigEmpty), | ||
"RAW signature verification failed for empty data on ${curve.name} / ${digest.name}" | ||
) | ||
|
||
repeat(8) { n -> | ||
val size = 10.0.pow(n).toInt() | ||
val data = CryptographyRandom.nextBytes(size) | ||
val signature = generateSignature(data) | ||
assertEquals(rawSignatureSize, signature.size) | ||
assertTrue(verifier.tryVerifySignature(data, signature)) | ||
assertEquals( | ||
rawSignatureSize, | ||
signature.size, | ||
"RAW signature size mismatch for data size $size on ${curve.name} / ${digest.name}" | ||
) | ||
assertTrue( | ||
verifier.tryVerifySignature(data, signature), | ||
"RAW signature verification failed for data size $size on ${curve.name} / ${digest.name}" | ||
) | ||
} | ||
} | ||
} | ||
|
@@ -68,7 +122,12 @@ abstract class EcdsaTest(provider: CryptographyProvider) : AlgorithmTest<ECDSA>( | |
fun assertSignatureSize(signature: ByteArray) { | ||
if (signature.size in derSignatureSizes) return | ||
// enhance a message with Base64 encoded signature | ||
assertContains(derSignatureSizes, signature.size, "DER: ${Base64.encode(signature)}") | ||
|
||
assertContains( | ||
derSignatureSizes, signature.size, "DER signature size mismatch on ${curve.name} / ${digest.name}. " + | ||
"Expected one of $derSignatureSizes, got ${signature.size}. " + | ||
"Signature (Base64): ${Base64.encode(signature)}" | ||
) | ||
} | ||
|
||
assertSignatureSize(generateSignature(ByteArray(0))) | ||
|
@@ -77,7 +136,10 @@ abstract class EcdsaTest(provider: CryptographyProvider) : AlgorithmTest<ECDSA>( | |
val data = CryptographyRandom.nextBytes(size) | ||
val signature = generateSignature(data) | ||
assertSignatureSize(signature) | ||
assertTrue(verifier.tryVerifySignature(data, signature)) | ||
assertTrue( | ||
verifier.tryVerifySignature(data, signature), | ||
"DER signature verification failed for data size $size on ${curve.name} / ${digest.name}" | ||
) | ||
} | ||
} | ||
} | ||
|
@@ -87,27 +149,41 @@ abstract class EcdsaTest(provider: CryptographyProvider) : AlgorithmTest<ECDSA>( | |
|
||
@Test | ||
fun testFunctions() = testWithAlgorithm { | ||
if (!supportsFunctions()) return@testWithAlgorithm | ||
if (!supportsFunctions()) { | ||
println("Skipping function test because functions are not supported by provider") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. save here about |
||
return@testWithAlgorithm | ||
} | ||
|
||
listOf( | ||
EC.Curve.P256, | ||
EC.Curve.P384, | ||
EC.Curve.P521, | ||
EC.Curve("secp256k1"), | ||
EC.Curve.secp256k1, | ||
EC.Curve.brainpoolP256r1, | ||
EC.Curve.brainpoolP384r1, | ||
EC.Curve.brainpoolP512r1, | ||
).forEach { curve -> | ||
if (!supportsCurve(curve)) return@forEach | ||
if (!supportsCurve(curve)) { | ||
println("Skipping function test for unsupported curve: ${curve.name}") | ||
return@forEach | ||
} | ||
println("Running function test for curve: ${curve.name}") | ||
|
||
val keyPair = algorithm.keyPairGenerator(curve).generateKey() | ||
|
||
generateDigests { digest, _ -> | ||
if (!supportsDigest(digest)) return@generateDigests | ||
if (!supportsDigest(digest)) { | ||
println("Skipping digest $digest for curve ${curve.name}") | ||
return@generateDigests | ||
} | ||
|
||
ECDSA.SignatureFormat.entries.forEach { format -> | ||
println("Testing format $format for ${curve.name} / ${digest.name}") | ||
val signatureGenerator = keyPair.privateKey.signatureGenerator(digest, format) | ||
val signatureVerifier = keyPair.publicKey.signatureVerifier(digest, format) | ||
|
||
repeat(10) { | ||
val size = CryptographyRandom.nextInt(20000) | ||
val size = CryptographyRandom.nextInt(1024, 20000) // Ensure non-trivial size | ||
val data = ByteString(CryptographyRandom.nextBytes(size)) | ||
assertSignaturesViaFunction(signatureGenerator, signatureVerifier, data) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice idea!