-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for HMAC Precomputed Key (only for non-FIPS builds)
This commit adds support for HMAC Precomputed Keys in ACCP. See aws/aws-lc#1574
- Loading branch information
Fabrice Benhamouda
committed
Aug 28, 2024
1 parent
47e9d89
commit 8097f53
Showing
11 changed files
with
851 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule aws-lc
updated
from 057477 to 2f1879
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
examples/gradle-kt-dsl/lib/src/test/kotlin/com/amazon/corretto/crypto/examples/Hmac.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.amazon.corretto.crypto.examples | ||
|
||
import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider | ||
import java.util.* | ||
import javax.crypto.Mac | ||
import javax.crypto.spec.SecretKeySpec | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
class Hmac { | ||
@Test | ||
fun hmacTest() { | ||
val accpProviderName = "AmazonCorrettoCryptoProvider" | ||
AmazonCorrettoCryptoProvider.install() | ||
|
||
val mac = Mac.getInstance("HmacSHA384") | ||
assertEquals(accpProviderName, mac.provider.name) | ||
|
||
// An arbitrary 32-bytes key in base64 for the example | ||
val keyBase64 = "62lKZjLXnX4yGvNyd3/M3q+T6yfREHgbIoJidXCEzGw=" | ||
val key = Base64.getDecoder().decode(keyBase64) | ||
val keySpec = SecretKeySpec(key, "Generic") | ||
|
||
val message = "Hello, this is just an example." | ||
|
||
// Compute the MAC | ||
mac.init(keySpec); | ||
val macResult = mac.doFinal(message.toByteArray()) | ||
|
||
// Verify the result matches what we expect | ||
val expectedResultBase64 = | ||
"w72DBgWvjTDqlv+EzOc1/R+K9Qq1jrNCHCQewXXhaOQ8Joi2jPPQdAT+HDc65KMM" | ||
val expectedResult = Base64.getDecoder().decode(expectedResultBase64) | ||
assertContentEquals(expectedResult, macResult) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...-kt-dsl/lib/src/test/kotlin/com/amazon/corretto/crypto/examples/HmacWithPrecomputedKey.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.amazon.corretto.crypto.examples | ||
|
||
import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider | ||
import java.security.SecureRandom | ||
import java.util.* | ||
import javax.crypto.Cipher | ||
import javax.crypto.Mac | ||
import javax.crypto.SecretKeyFactory | ||
import javax.crypto.spec.GCMParameterSpec | ||
import javax.crypto.spec.SecretKeySpec | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
class HmacWithPrecomputedKey { | ||
@Test | ||
fun hmacWithPrecomputedKeyTest() { | ||
// EXPERT-ONLY use | ||
// This example is most likely NOT what you want to use. | ||
// If you need to use Hmac, see the Hmac.kt example. | ||
// This example shows how to use precomputed keys, which is not standard in JCA/JCE. | ||
// See ACCP README.md for details. | ||
|
||
val accpProviderName = "AmazonCorrettoCryptoProvider" | ||
AmazonCorrettoCryptoProvider.install() | ||
|
||
val mac = Mac.getInstance("HmacSHA384WithPrecomputedKey") | ||
assertEquals(accpProviderName, mac.provider.name) | ||
|
||
val skf = SecretKeyFactory.getInstance("HmacSHA384WithPrecomputedKey") | ||
assertEquals(accpProviderName, skf.provider.name) | ||
|
||
// An arbitrary 32-bytes key in base64 for the example | ||
val keyBase64 = "62lKZjLXnX4yGvNyd3/M3q+T6yfREHgbIoJidXCEzGw="; | ||
val key = Base64.getDecoder().decode(keyBase64); | ||
val keySpec = SecretKeySpec(key, "Generic"); | ||
|
||
val message = "Hello, this is just an example." | ||
|
||
// Compute the HMAC precomputed key | ||
val precomputedKey = skf.generateSecret(keySpec) | ||
|
||
// Compute the HMAC using the precomputed key | ||
mac.init(precomputedKey); | ||
val macResult = mac.doFinal(message.toByteArray()) | ||
|
||
// Verify the result matches what we expect | ||
val expectedResultBase64 = | ||
"w72DBgWvjTDqlv+EzOc1/R+K9Qq1jrNCHCQewXXhaOQ8Joi2jPPQdAT+HDc65KMM" | ||
val expectedResult = Base64.getDecoder().decode(expectedResultBase64) | ||
assertContentEquals(expectedResult, macResult) | ||
} | ||
} |
Oops, something went wrong.