Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back support for EVP_PKEY_HMAC #1324

Merged
merged 12 commits into from
Dec 26, 2023

Conversation

samuel40791765
Copy link
Contributor

@samuel40791765 samuel40791765 commented Nov 27, 2023

Issues:

Resolves CryptoAlg-1704

Description of changes:

Some legacy libraries still depend on EVP_PKEY_HMAC unfortunately, so we're adding back support. OpenSSL 3.0 hasn't deprecated support for this either, so we still need to support this for the SHIM layer long term. Much of logic was reapplied from the original commit removal, but I've cleaned some unneeded branching logic to makes things more straight forward.

The original EVP_PKEY_HMAC code from OpenSSL 1.1.1 exists in these two files:

The code has remained relatively untouched since the removal in BoringSSL.

We've also made modifications to remove the need for additional EVP_PKEY function pointers and ctrl logic. Since HMAC needs to be specially handled in many of the EVP_DigestSign* APIs, the additional function pointers make much less sense. We can just directly make calls to HMAC_CTX without the EVP_PKEY_METHOD redirections.

Call-outs:

  • Reapply the original removal
  • Reapply tests from the original removal.
  • Clean up code
  • Add tests against HMAC wycheproof test vectors
  • Update documentation that mentions the deprecation of EVP_PKEY_HMAC and add follow up comments for service indicator adjustments
  • Migrate ASN1 usage to use CBB/CBS
  • Add test using EVP_PKEY_CTX_dup with EVP_PKEY_HMAC

Testing:

New tests

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@samuel40791765 samuel40791765 force-pushed the evp-pkey-hmac branch 2 times, most recently from d63dd27 to 99c9474 Compare November 27, 2023 23:22
@codecov-commenter
Copy link

codecov-commenter commented Nov 27, 2023

Codecov Report

Attention: 31 lines in your changes are missing coverage. Please review.

Comparison is base (b201aea) 76.78% compared to head (52ea5f9) 76.79%.

Files Patch % Lines
crypto/fipsmodule/evp/evp.c 47.82% 12 Missing ⚠️
crypto/fipsmodule/evp/p_hmac.c 86.44% 8 Missing ⚠️
crypto/fipsmodule/digest/digest.c 82.14% 5 Missing ⚠️
crypto/fipsmodule/evp/digestsign.c 91.11% 4 Missing ⚠️
crypto/evp_extra/p_hmac_asn1.c 66.66% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #1324    +/-   ##
========================================
  Coverage   76.78%   76.79%            
========================================
  Files         422      424     +2     
  Lines       71267    71392   +125     
========================================
+ Hits        54725    54826   +101     
- Misses      16542    16566    +24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@samuel40791765 samuel40791765 marked this pull request as ready for review November 29, 2023 00:47
@samuel40791765 samuel40791765 requested a review from a team as a code owner November 29, 2023 00:47
@torben-hansen torben-hansen self-requested a review November 29, 2023 15:23
@samuel40791765
Copy link
Contributor Author

samuel40791765 commented Nov 29, 2023

Bind9 consumes EVP_PKEY_HMAC from the EVP_PKEY_new_raw_private_key function, which this PR doesn't quite resolve yet. There are some subsequent ASN1 functions (hmac_set_priv_key and hmac_get_priv_key) here that we'll need to import to support this.
This change is pretty large already, so I'm leaning towards opening up a follow up commit to support this. We can let this PR focus on backporting what's in the original upstream commit removal.

@samuel40791765 samuel40791765 force-pushed the evp-pkey-hmac branch 3 times, most recently from 3486667 to 75caba2 Compare December 12, 2023 01:35
return 0;
if (pkey->type == EVP_PKEY_HMAC) {
// |ctx->update| gets repurposed as a hook to call |HMAC_Update|.
// |ctx->update| is normally copied from |mctx->digest->update|, but
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is mctx? or was it supposed to be pctx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to update this, it's ctx->digest->update from the same structure.

}
return 1;
}

ctx->digest->init(ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still required in the case of used_for_hmac(ctx)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's the original EVP_Digest digest code path. HMAC specific logic is contained within used_for_hmac

Comment on lines 252 to 253
// HMAC keys aren't absolutely required for HMAC operations, but the
// combination below isn't allowed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// HMAC keys aren't absolutely required for HMAC operations, but the
// combination below isn't allowed.
// HMAC keys aren't absolutely required for HMAC operations (can be NULL),
// but in that case, their length must be zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added this to the API documentation

@nebeid
Copy link
Contributor

nebeid commented Dec 12, 2023

Should the PR description reflect that the difference in design from the original brought-back commit?

Copy link
Contributor Author

@samuel40791765 samuel40791765 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the PR description reflect that the difference in design from the original brought-back commit?

Sure, I'll update it :)

}
return 1;
}

ctx->digest->init(ctx);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's the original EVP_Digest digest code path. HMAC specific logic is contained within used_for_hmac

return 0;
if (pkey->type == EVP_PKEY_HMAC) {
// |ctx->update| gets repurposed as a hook to call |HMAC_Update|.
// |ctx->update| is normally copied from |mctx->digest->update|, but
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to update this, it's ctx->digest->update from the same structure.

Comment on lines 252 to 253
// HMAC keys aren't absolutely required for HMAC operations, but the
// combination below isn't allowed.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added this to the API documentation

@@ -130,7 +144,7 @@ int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to bail early for these functions if called with an HMAC type pkey?

  • EVP_DigestVerifyFinal
  • EVP_DigestVerify

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice detail, I've added an additional HMACTest.EVP_DigestVerify test to verify that the emitted error codes are what we want.
I was about to add some checks to bail early, but I noticed that the consistent behavior of the APIs was to bail if either verify or verify_message wasn't defined as a EVP_PKEY_METHOD (which is the case for HMAC here). In that case, should we still add the additional HMAC specific logic here? But, no strong opinions either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 984fccc per our discussion :)

torben-hansen
torben-hansen previously approved these changes Dec 15, 2023
andrewhop
andrewhop previously approved these changes Dec 20, 2023
torben-hansen
torben-hansen previously approved these changes Dec 21, 2023
// 1. Set flag |EVP_MD_CTX_FLAG_KEEP_PKEY_CTX|, so as to let |*pctx| refrain
// from being freed when |*pctx| was set externally with
// |EVP_MD_CTX_set_pkey_ctx|.
// 2. Set flag |EVP_MD_CTX_FLAG_NO_INIT| for |EVP_PKEY_HMAC|.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions constant EVP_MD_CTX_FLAG_NO_INIT, but the code checks for EVP_MD_CTX_HMAC. Could it be that EVP_MD_CTX_FLAG_NO_INIT is deprecated? Maybe the comment needs to be updated?

Copy link
Contributor Author

@samuel40791765 samuel40791765 Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should be EVP_MD_CTX_HMAC I'll change this rn

@samuel40791765 samuel40791765 force-pushed the evp-pkey-hmac branch 2 times, most recently from 73f3740 to 0ffe09c Compare December 22, 2023 18:19
@samuel40791765 samuel40791765 enabled auto-merge (squash) December 26, 2023 20:57
@samuel40791765 samuel40791765 merged commit 7226be1 into aws:main Dec 26, 2023
@samuel40791765 samuel40791765 deleted the evp-pkey-hmac branch December 26, 2023 21:14
dougch pushed a commit to dougch/aws-lc that referenced this pull request Jan 30, 2024
Some legacy libraries still depend on `EVP_PKEY_HMAC` unfortunately,
so we're adding back support. OpenSSL 3.0 hasn't deprecated support for
this either, so we still need to support this for the SHIM layer long
term. Much of logic was reapplied from the [original commit removal]
(https://github.com/google/boringssl/commit/
65ee9b7), but I've cleaned up some
unneeded branching logic to makes things more straight forward.

We've also made modifications to remove the need for additional
`EVP_PKEY` function pointers and `ctrl` logic. Since HMAC needs to be
specially handled in many of the `EVP_DigestSign*` APIs, the additional
function pointers make much less sense. We can just directly make calls
to `HMAC_CTX` without the `EVP_PKEY_METHOD` redirections.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants