Skip to content

Commit

Permalink
Remove use of bssl::ScopedHMAC_CTX
Browse files Browse the repository at this point in the history
bssl::ScopedHMAC_CTX simplifies HMAC context management.
But it does not seem available in legacy builds.
(The CB AccpCiLegacyTests fails.)
  • Loading branch information
Fabrice Benhamouda committed Aug 29, 2024
1 parent 8097f53 commit cf3430a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions csrc/hmac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,21 @@ JNIEXPORT void JNICALL Java_com_amazon_corretto_crypto_provider_HmacWithPrecompu
JBinaryBlob result(pEnv, nullptr, jOutput);
JBinaryBlob key(pEnv, nullptr, jKey);

bssl::ScopedHMAC_CTX ctx;
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);

if (unlikely(HMAC_Init_ex(ctx.get(),
if (unlikely(HMAC_Init_ex(&ctx,
key.get(), // key
keyLen, // keyLen
reinterpret_cast<const EVP_MD*>(evpMd), // EVP_MD
nullptr /* ENGINE */)
!= 1)) {
HMAC_CTX_cleanup(&ctx);
throw_openssl("Unable to initialize HMAC_CTX");
}

if (unlikely(HMAC_set_precomputed_key_export(ctx.get()) != 1)) {
if (unlikely(HMAC_set_precomputed_key_export(&ctx) != 1)) {
HMAC_CTX_cleanup(&ctx);
throw_openssl("Unable to call HMAC_set_precomputed_key_export");
}

Expand All @@ -254,12 +257,16 @@ JNIEXPORT void JNICALL Java_com_amazon_corretto_crypto_provider_HmacWithPrecompu
// The Java caller always selects the right buffer size, so we should not have any error.
// But we do a sanity check that this is the case.
size_t actualOutputLen = outputLen;
if (unlikely(HMAC_get_precomputed_key(ctx.get(), result.get(), &actualOutputLen) != 1)) {
if (unlikely(HMAC_get_precomputed_key(&ctx, result.get(), &actualOutputLen) != 1)) {
HMAC_CTX_cleanup(&ctx);
throw_openssl("Unable to call HMAC_get_precomputed_key");
}
if (unlikely(outputLen < 0 || (size_t)outputLen != actualOutputLen)) {
HMAC_CTX_cleanup(&ctx);
throw_java_ex(EX_ERROR, "THIS SHOULD NOT BE REACHABLE. invalid output precomputed key length.");
}

HMAC_CTX_cleanup(&ctx);
#else
throw_java_ex(EX_ERROR, "Precomputed keys are not supported on this platform/build");
#endif
Expand Down

0 comments on commit cf3430a

Please sign in to comment.