Skip to content

Commit 00b62ee

Browse files
committed
optimize with nMinusTwo
1 parent 52df61d commit 00b62ee

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

runtime/auth/aws-signing-default/common/src/aws/smithy/kotlin/runtime/auth/awssigning/SignatureCalculator.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,8 @@ internal class SigV4aSignatureCalculator(override val sha256Provider: HashSuppli
152152
var counter: Byte = 1
153153
var privateKey: ByteArray
154154

155-
// N value from NIST P-256 curve
156-
// FIXME optimization: n is never used by itself, only n-2. Subtract two from the const.
157-
val nBytes = "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551".decodeHexBytes()
158-
val n = BigInteger(nBytes)
155+
// N value from NIST P-256 curve, minus two.
156+
val nMinusTwo = BigInteger("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC63254f".decodeHexBytes())
159157

160158
// FIXME Public docs say secret access key needs to be Base64 encoded, that's not right.
161159
// (or maybe it's already base64-encoded, and they are just repeating it)
@@ -170,18 +168,18 @@ internal class SigV4aSignatureCalculator(override val sha256Provider: HashSuppli
170168

171169
privateKey = (c + BigInteger("1")).toByteArray()
172170

173-
if (counter == MAX_KDF_COUNTER_ITERATIONS && c > n - BigInteger("2")) {
171+
if (counter == MAX_KDF_COUNTER_ITERATIONS && c > nMinusTwo) {
174172
throw IllegalStateException("Counter exceeded maximum length")
175173
} else {
176174
counter++
177175
}
178-
} while (c > n - BigInteger("2"))
176+
} while (c > nMinusTwo)
179177

180178
return privateKey
181179
}
182180

183181
/**
184-
* Computes the fixed input string used for KDF
182+
* Computes the fixed input string used for ECDSA private key derivation
185183
* The final output looks like:
186184
* 0x00000001 || "AWS4-ECDSA-P256-SHA256" || 0x00 || AccessKeyId || counter || 0x00000100
187185
*/

0 commit comments

Comments
 (0)