Skip to content

Commit bb849dc

Browse files
committed
pr feedback
1 parent 8ee1fbf commit bb849dc

File tree

4 files changed

+42
-60
lines changed

4 files changed

+42
-60
lines changed

runtime/runtime-core/api/runtime-core.api

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -726,16 +726,11 @@ public final class aws/smithy/kotlin/runtime/hashing/Crc32cKt {
726726
}
727727

728728
public final class aws/smithy/kotlin/runtime/hashing/EcdsaJVMKt {
729-
public static final fun ecdsaSecp256r1 ([B[BLaws/smithy/kotlin/runtime/hashing/EcdsaSignatureType;)[B
730-
public static synthetic fun ecdsaSecp256r1$default ([B[BLaws/smithy/kotlin/runtime/hashing/EcdsaSignatureType;ILjava/lang/Object;)[B
729+
public static final fun ecdsaSecp256r1 ([B[B)[B
731730
}
732731

733-
public final class aws/smithy/kotlin/runtime/hashing/EcdsaSignatureType : java/lang/Enum {
734-
public static final field ASN1_DER Laws/smithy/kotlin/runtime/hashing/EcdsaSignatureType;
735-
public static final field RAW_RS Laws/smithy/kotlin/runtime/hashing/EcdsaSignatureType;
736-
public static fun getEntries ()Lkotlin/enums/EnumEntries;
737-
public static fun valueOf (Ljava/lang/String;)Laws/smithy/kotlin/runtime/hashing/EcdsaSignatureType;
738-
public static fun values ()[Laws/smithy/kotlin/runtime/hashing/EcdsaSignatureType;
732+
public final class aws/smithy/kotlin/runtime/hashing/EcdsaKt {
733+
public static final fun ecdsaSecp256r1Rs ([B[B)[B
739734
}
740735

741736
public abstract interface class aws/smithy/kotlin/runtime/hashing/HashFunction {

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/hashing/Ecdsa.kt

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,43 @@
44
*/
55
package aws.smithy.kotlin.runtime.hashing
66

7-
public enum class EcdsaSignatureType {
8-
ASN1_DER,
9-
RAW_RS,
7+
8+
/**
9+
* ECDSA on the SECP256R1 curve returning ASN.1 DER format.
10+
*/
11+
public expect fun ecdsaSecp256r1(key: ByteArray, message: ByteArray): ByteArray
12+
13+
/**
14+
* ECDSA on the SECP256R1 curve returning raw r||s format.
15+
*/
16+
public fun ecdsaSecp256r1Rs(key: ByteArray, message: ByteArray): ByteArray {
17+
val derSignature = ecdsaSecp256r1(key, message)
18+
return parseDerSignature(derSignature)
1019
}
1120

1221
/**
13-
* ECDSA on the SECP256R1 curve.
22+
* Parses an ASN.1 DER encoded ECDSA signature and converts it to raw r||s format.
1423
*/
15-
public expect fun ecdsaSecp256r1(
16-
key: ByteArray,
17-
message: ByteArray,
18-
signatureType: EcdsaSignatureType = EcdsaSignatureType.ASN1_DER,
19-
): ByteArray
24+
private fun parseDerSignature(derSignature: ByteArray): ByteArray {
25+
var index = 2 // Skip SEQUENCE tag and length
26+
27+
// Read r
28+
index++ // Skip INTEGER tag
29+
val rLength = derSignature[index++].toInt() and 0xFF
30+
val r = derSignature.sliceArray(index until index + rLength)
31+
index += rLength
32+
33+
// Read s
34+
index++ // Skip INTEGER tag
35+
val sLength = derSignature[index++].toInt() and 0xFF
36+
val s = derSignature.sliceArray(index until index + sLength)
37+
38+
// Remove leading zero bytes and pad to 32 bytes
39+
val rFixed = r.dropWhile { it == 0.toByte() }.toByteArray()
40+
val sFixed = s.dropWhile { it == 0.toByte() }.toByteArray()
41+
42+
val rPadded = if (rFixed.size < 32) ByteArray(32 - rFixed.size) + rFixed else rFixed
43+
val sPadded = if (sFixed.size < 32) ByteArray(32 - sFixed.size) + sFixed else sFixed
44+
45+
return rPadded + sPadded
46+
}

runtime/runtime-core/jvm/src/aws/smithy/kotlin/runtime/hashing/EcdsaJVM.kt

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import java.security.spec.*
1212
/**
1313
* ECDSA on the SECP256R1 curve.
1414
*/
15-
public actual fun ecdsaSecp256r1(
16-
key: ByteArray,
17-
message: ByteArray,
18-
signatureType: EcdsaSignatureType,
19-
): ByteArray {
15+
public actual fun ecdsaSecp256r1(key: ByteArray, message: ByteArray): ByteArray {
2016
// Convert private key to BigInteger
2117
val d = BigInteger(key)
2218

@@ -32,42 +28,10 @@ public actual fun ecdsaSecp256r1(
3228
val privateKey = keyFactory.generatePrivate(privateKeySpec)
3329

3430
// Sign the message
35-
val derSignature = Signature.getInstance("SHA256withECDSA").apply {
31+
return Signature.getInstance("SHA256withECDSA").apply {
3632
initSign(privateKey)
3733
update(message)
3834
}.sign()
39-
40-
return when (signatureType) {
41-
EcdsaSignatureType.ASN1_DER -> derSignature
42-
EcdsaSignatureType.RAW_RS -> parseDerSignature(derSignature)
43-
}
4435
}
4536

4637
private fun BigInteger.toJvm(): java.math.BigInteger = java.math.BigInteger(1, toByteArray())
47-
48-
/**
49-
* Parses an ASN.1 DER encoded ECDSA signature and converts it to raw r||s format.
50-
*/
51-
private fun parseDerSignature(derSignature: ByteArray): ByteArray {
52-
var index = 2 // Skip SEQUENCE tag and length
53-
54-
// Read r
55-
index++ // Skip INTEGER tag
56-
val rLength = derSignature[index++].toInt() and 0xFF
57-
val r = derSignature.sliceArray(index until index + rLength)
58-
index += rLength
59-
60-
// Read s
61-
index++ // Skip INTEGER tag
62-
val sLength = derSignature[index++].toInt() and 0xFF
63-
val s = derSignature.sliceArray(index until index + sLength)
64-
65-
// Remove leading zero bytes and pad to 32 bytes
66-
val rFixed = r.dropWhile { it == 0.toByte() }.toByteArray()
67-
val sFixed = s.dropWhile { it == 0.toByte() }.toByteArray()
68-
69-
val rPadded = if (rFixed.size < 32) ByteArray(32 - rFixed.size) + rFixed else rFixed
70-
val sPadded = if (sFixed.size < 32) ByteArray(32 - sFixed.size) + sFixed else sFixed
71-
72-
return rPadded + sPadded
73-
}

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/hashing/EcdsaNative.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,4 @@ package aws.smithy.kotlin.runtime.hashing
77
/**
88
* ECDSA on the SECP256R1 curve.
99
*/
10-
public actual fun ecdsaSecp256r1(
11-
key: ByteArray,
12-
message: ByteArray,
13-
signatureType: EcdsaSignatureType,
14-
): ByteArray = error("This function should not be invoked on Native, which uses the CrtAwsSigner.")
10+
public actual fun ecdsaSecp256r1(key: ByteArray, message: ByteArray): ByteArray = error("This function should not be invoked on Native, which uses the CrtAwsSigner.")

0 commit comments

Comments
 (0)