Skip to content

Commit 7b81166

Browse files
committed
Add some docs
1 parent 23364f7 commit 7b81166

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import aws.smithy.kotlin.runtime.hashing.hmac
1010
import aws.smithy.kotlin.runtime.text.encoding.encodeToHex
1111
import aws.smithy.kotlin.runtime.time.TimestampFormat
1212

13+
/**
14+
* [SignatureCalculator] for the SigV4 ("AWS4-HMAC-SHA256") algorithm
15+
* @param sha256Provider the [HashSupplier] to use for computing SHA-256 hashes
16+
*/
1317
internal class SigV4SignatureCalculator(override val sha256Provider: HashSupplier = ::Sha256) : SigV4xSignatureCalculator(AwsSigningAlgorithm.SIGV4, sha256Provider) {
1418
override fun calculate(signingKey: ByteArray, stringToSign: String): String =
1519
hmac(signingKey, stringToSign.encodeToByteArray(), sha256Provider).encodeToHex()

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,25 @@ import kotlin.time.Duration.Companion.hours
2424
*/
2525
internal const val MAX_KDF_COUNTER_ITERATIONS = 254.toByte()
2626

27+
/**
28+
* A [SignatureCalculator] for the SigV4a ("AWS4-ECDSA-P256-SHA256") algorithm.
29+
* @param sha256Provider the [HashSupplier] to use for computing SHA-256 hashes
30+
*/
2731
internal class SigV4aSignatureCalculator(override val sha256Provider: HashSupplier = ::Sha256) : SigV4xSignatureCalculator(AwsSigningAlgorithm.SIGV4_ASYMMETRIC, sha256Provider) {
2832
private val privateKeyCache = ReadThroughCache<Credentials, ByteArray>(
2933
minimumSweepPeriod = 1.hours, // note: Sweeps are effectively a no-op because expiration is [Instant.MAX_VALUE]
3034
)
3135

3236
override fun calculate(signingKey: ByteArray, stringToSign: String): String = ecdsasecp256r1(signingKey, stringToSign.encodeToByteArray()).encodeToHex()
3337

34-
// See https://github.com/awslabs/aws-c-auth/blob/e8360a65e0f3337d4ac827945e00c3b55a641a5f/source/key_derivation.c#L70 for more details of derivation process
38+
/**
39+
* Retrieve a signing key based on the signing credentials. If not cached, the key will be derived using a counter-based key derivation function (KDF)
40+
* as specified in NIST SP 800-108.
41+
*
42+
* See https://github.com/awslabs/aws-c-auth/blob/e8360a65e0f3337d4ac827945e00c3b55a641a5f/source/key_derivation.c#L70 and
43+
* https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html#derive-signing-key-sigv4a for
44+
* more information on the derivation process.
45+
*/
3546
override fun signingKey(config: AwsSigningConfig): ByteArray = runBlocking {
3647
privateKeyCache.get(config.credentials) {
3748
var counter: Byte = 1
@@ -40,17 +51,12 @@ internal class SigV4aSignatureCalculator(override val sha256Provider: HashSuppli
4051
// N value from NIST P-256 curve, minus two.
4152
val nMinusTwo = "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC63254F".decodeHexBytes().toPositiveBigInteger()
4253

43-
// FIXME Public docs say secret access key needs to be Base64 encoded, that's not right.
44-
// (or maybe it's already base64-encoded, and they are just repeating it)
4554
val inputKey = ("AWS4A" + config.credentials.secretAccessKey).encodeToByteArray()
4655

4756
do {
48-
// 1.2: Compute K0
4957
val k0 = hmac(inputKey, fixedInputString(config.credentials.accessKeyId, counter), sha256Provider)
5058

51-
// 2: Compute the ECC key pair
5259
val c = k0.toPositiveBigInteger()
53-
5460
privateKey = (c + BigInteger("1")).toByteArray()
5561

5662
if (counter == MAX_KDF_COUNTER_ITERATIONS && c > nMinusTwo) {
@@ -65,17 +71,17 @@ internal class SigV4aSignatureCalculator(override val sha256Provider: HashSuppli
6571
}
6672

6773
/**
68-
* Computes the fixed input string used for ECDSA private key derivation
74+
* Forms the fixed input string used for ECDS private key derivation
6975
* The final output looks like:
7076
* 0x00000001 || "AWS4-ECDSA-P256-SHA256" || 0x00 || AccessKeyId || counter || 0x00000100
7177
*/
7278
private fun fixedInputString(accessKeyId: String, counter: Byte): ByteArray =
73-
byteArrayOf(0x00, 0x00, 0x00, 0x01) + // FIXME CRT implementation (4 bytes) and internal docs (1 byte) conflict.
79+
byteArrayOf(0x00, 0x00, 0x00, 0x01) +
7480
"AWS4-ECDSA-P256-SHA256".encodeToByteArray() +
7581
byteArrayOf(0x00) +
7682
accessKeyId.encodeToByteArray() +
7783
counter +
78-
byteArrayOf(0x00, 0x00, 0x01, 0x00) // FIXME CRT implementation (4 bytes) and internal docs (2 bytes) conflict.
84+
byteArrayOf(0x00, 0x00, 0x01, 0x00)
7985
}
8086

8187
// Convert [this] [ByteArray] to a positive [BigInteger]

0 commit comments

Comments
 (0)