-
Notifications
You must be signed in to change notification settings - Fork 31
misc: ecdsa raw signature #1457
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
Changes from all commits
1e9f2a5
c171b30
8ee1fbf
bb849dc
dc4ba74
79d9a60
ff8fe78
2dc6f6f
17215bb
2ef9679
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 |
|---|---|---|
|
|
@@ -5,6 +5,41 @@ | |
| package aws.smithy.kotlin.runtime.hashing | ||
|
|
||
| /** | ||
| * ECDSA on the SECP256R1 curve. | ||
| * ECDSA on the SECP256R1 curve returning ASN.1 DER format. | ||
| */ | ||
| public expect fun ecdsaSecp256r1(key: ByteArray, message: ByteArray): ByteArray | ||
|
|
||
| /** | ||
| * ECDSA on the SECP256R1 curve returning raw r||s format. | ||
| */ | ||
| public fun ecdsaSecp256r1Rs(key: ByteArray, message: ByteArray): ByteArray { | ||
| val derSignature = ecdsaSecp256r1(key, message) | ||
| return parseDerSignature(derSignature) | ||
| } | ||
|
Comment on lines
+12
to
+18
Contributor
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. Can we create some unit tests for this?
Contributor
Author
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. Yes, tests added. |
||
|
|
||
| /** | ||
| * Parses an ASN.1 DER encoded ECDSA signature and converts it to raw r||s format. | ||
| */ | ||
| private fun parseDerSignature(derSignature: ByteArray): ByteArray { | ||
| var index = 2 // Skip SEQUENCE tag and length | ||
|
|
||
| // Read r | ||
| index++ // Skip INTEGER tag | ||
| val rLength = derSignature[index++].toInt() and 0xFF | ||
| val r = derSignature.sliceArray(index until index + rLength) | ||
| index += rLength | ||
|
|
||
| // Read s | ||
| index++ // Skip INTEGER tag | ||
| val sLength = derSignature[index++].toInt() and 0xFF | ||
| val s = derSignature.sliceArray(index until index + sLength) | ||
|
|
||
| // Remove leading zero bytes and pad to 32 bytes | ||
| val rFixed = r.dropWhile { it == 0.toByte() }.toByteArray() | ||
| val sFixed = s.dropWhile { it == 0.toByte() }.toByteArray() | ||
|
|
||
| val rPadded = if (rFixed.size < 32) ByteArray(32 - rFixed.size) + rFixed else rFixed | ||
| val sPadded = if (sFixed.size < 32) ByteArray(32 - sFixed.size) + sFixed else sFixed | ||
|
|
||
| return rPadded + sPadded | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Existing
ecdsaSecp256r1(key, message)calls work unchanged, should not be a breaking changeThere 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.
This is definitely a breaking change because you've altered the method signature. Older consumers of runtime-core will encounter a
NoSuchMethodErrorexception at runtime. The only way to do this safely without breaking binary compatibility is to maintain the old method signature and add a new one.I recommend one of the following approaches:
ecdsaSecp256r1Rswhich invokesecdsaSecp256r1Rsand extracts therandsvalues. This could be implemented in common since it's just byte manipulationasn1DerToRswhich converts anyByteArrayfrom ASN.1 DER format to r||s. Callers would then invokeecdsaSecp256r1Rsfirst and thenasn1DerToRs. This could also be implemented in common.