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 support for POINT_CONVERSION_HYBRID #1936

Merged
merged 4 commits into from
Oct 31, 2024

Conversation

samuel40791765
Copy link
Contributor

@samuel40791765 samuel40791765 commented Oct 19, 2024

Issues:

Resolves CryptoAlg-2697

Description of changes:

This is the first time it has come up, but Ruby depends on the POINT_CONVERSION_HYBRID format in its tests and expects the underlying libcrypto to support it.

There's not too much a difference between POINT_CONVERSION_HYBRID and POINT_CONVERSION_UNCOMPRESSED except that the first bit contains information about the quadratic equation y is at. We have the building blocks to support and can get this working with some tweaking around.

Call-outs:

POINT_CONVERSION_HYBRID works very similar to POINT_CONVERSION_UNCOMPRESSED so we're reusing a lot of code. Most of this change is working with the first byte POINT_CONVERSION_HYBRID does differently and point the encoding to the right places for parsing.

Testing:

  • Testing parsing from and back with POINT_CONVERSION_HYBRID. What we're parsing to and from hybrid doesn't really matter here, they're already covered by other code paths.

  • Test vectors were generated from our test cases and ran against OpenSSL with this brief test program:

int main() {
  // Generate |ec_key|.
  EC_KEY *ec_key = EC_KEY_new();
  EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp521r1);
  EC_KEY_set_group(ec_key, group);

  const uint8_t *inp = kECPublicKey_uncompressed;
  // Decoding an EC point.
  o2i_ECPublicKey(&ec_key, &inp, sizeof(kECPublicKey_uncompressed));
  // On successful exit of |o2i_ECPublicKey|, |*inp| is advanced by |len| bytes.
  if(kECPublicKey_uncompressed + sizeof(kECPublicKey_uncompressed) != inp) {
    return -1;
  }
  // Set |conv_form| of |ec_key|.
  EC_KEY_set_conv_form(ec_key, POINT_CONVERSION_HYBRID);
  uint8_t *p = nullptr;
  size_t len2 = i2o_ECPublicKey(ec_key, &p);
   for(uint32_t i=0; i < len2; i++) {
      if(i%12 == 0 && i != 0){
        printf("\n");
      }
      printf("0x%02x, ", p[i]);
    }
       printf("\n");
  return 0;
}

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 point-hybrid branch 3 times, most recently from f3872e8 to 8721a2c Compare October 21, 2024 21:38
@samuel40791765 samuel40791765 marked this pull request as ready for review October 21, 2024 21:38
@samuel40791765 samuel40791765 requested a review from a team as a code owner October 21, 2024 21:38
@codecov-commenter
Copy link

codecov-commenter commented Oct 21, 2024

Codecov Report

Attention: Patch coverage is 81.48148% with 5 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@8d9809e). Learn more about missing BASE report.
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
crypto/fipsmodule/ec/oct.c 81.48% 5 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1936   +/-   ##
=======================================
  Coverage        ?   78.74%           
=======================================
  Files           ?      590           
  Lines           ?   101428           
  Branches        ?    14383           
=======================================
  Hits            ?    79867           
  Misses          ?    20924           
  Partials        ?      637           

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

skmcgrail
skmcgrail previously approved these changes Oct 22, 2024
crypto/fipsmodule/ec/ec.c Show resolved Hide resolved
crypto/fipsmodule/ec/ec_test.cc Show resolved Hide resolved
@@ -110,6 +112,11 @@ size_t ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point,
uint8_t y_buf[EC_MAX_BYTES];
ec_felem_to_bytes(group, y_buf, &field_len, &point->Y);
buf[0] = form + (y_buf[field_len - 1] & 1);
if (form == POINT_CONVERSION_HYBRID) {
// |POINT_CONVERSION_HYBRID| specifies y's solution of the quadratic
// equation, but also encodes the actual point along with it.
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
// equation, but also encodes the actual point along with it.
// equation, but also encodes the y coordinate along with it.

if (len != 1 + 2 * field_len || in[0] != POINT_CONVERSION_UNCOMPRESSED) {
// |POINT_CONVERSION_HYBRID| has the solution of y encoded in the first byte
// as well.
if (len != 1 + 2 * field_len || (in[0] != POINT_CONVERSION_UNCOMPRESSED &&
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we handle the hybrid case in function called ec_point_from_uncompressed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we ignore the first byte (which reveals the encoding format), uncompressed form is essentially identical to hybrid. It seemed like a bit redundant to write another ec_point_from_hybrid function with uncompressed parsing logic.
I could rename this function for a bit more clarity if needed? I was also considering adding a check in the hybrid decoding to see if y actually had the same solution that the first byte indicated, is that something we should do?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer having a separate ec_point_from_hybrid function, it's redundant but simpler.

I was also considering adding a check in the hybrid decoding to see if y actually had the same solution that the first byte indicated, is that something we should do?

That's a great question. Since we are doing this for compatibility with OpenSSL I guess you should see if they are checking the point consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I did a quick look into what OpenSSL was doing. They have a check for consistency that goes both ways.
Our's will look slightly different since we save the date in EC_FELEM, but I'll figure something out here.

// |POINT_CONVERSION_HYBRID| has the solution of y encoded in the first byte
// as well.
if (len != 1 + 2 * field_len || (in[0] != POINT_CONVERSION_UNCOMPRESSED &&
(in[0] & ~1u) != POINT_CONVERSION_HYBRID)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please explain the magic & ~1u?

crypto/fipsmodule/ec/oct.c Show resolved Hide resolved
crypto/fipsmodule/ec/oct.c Outdated Show resolved Hide resolved
crypto/fipsmodule/ec/oct.c Outdated Show resolved Hide resolved
crypto/fipsmodule/ec/oct.c Outdated Show resolved Hide resolved
@samuel40791765 samuel40791765 merged commit 97a6706 into aws:main Oct 31, 2024
114 of 116 checks passed
@samuel40791765 samuel40791765 deleted the point-hybrid branch October 31, 2024 21:33
skmcgrail added a commit that referenced this pull request Nov 7, 2024
## What's Changed
* 800-131Ar1: length of the key-derivation key shall be at least 112
bits. by @skmcgrail in #1924
* Marshalling/Unmarshalling DH public keys by @justsmth in
#1916
* Also prune SSM documents from ec2-test-framework by @samuel40791765 in
#1925
* Use illegal_parameter instead of decode_error for invalid key shares
by @justsmth in #1923
* Add null check in dh testing by @torben-hansen in
#1937
* DH paramgen callback by @justsmth in
#1928
* Upstream merge 2024 10 17 by @torben-hansen in
#1934
* Remove old Intel CPU types by @justsmth in
#1942
* Remove retries on PCT failure in EC and RSA key generation. by @nebeid
in #1938
* Add p4p, bump up time by @justsmth in
#1943
* PQ README by @jakemas in #1932
* bump mysql CI to 9.1.0 by @justsmth in
#1939
* HKDF, HKDF_expand, and PBKDF Truncated SHA2-512 by @skmcgrail in
#1946
* Missing functionality + Adding Nmap to our CI by @smittals2 in
#1915
* Fix FIPS.md typo by @justsmth in
#1950
* Support encode or decode ∞ like OpenSSL by @samuel40791765 in
#1930
* Expand support for EVP_PKEY_HMAC by @justsmth in
#1933
* Add PKCS7-internal BIO_f_cipher by @WillChilds-Klein in
#1836
* Add PKCS7-internal BIO_f_md by @WillChilds-Klein in
#1886
* Ruby Support - DSA custom md by @justsmth in
#1953
* Add support for POINT_CONVERSION_HYBRID by @samuel40791765 in
#1936
* Fixes for Coverity Alerts by @smittals2 in
#1960
* Also test w/ gcc 4.8 by @justsmth in
#1962
* Actually add support for SSL_get_server/peer_tmp_key by
@samuel40791765 in #1945
* Coverity Fix Null Check by @smittals2 in
#1965
* ML-KEM keygen Pairwise Consistency Test by @dkostic in
#1964
* EDDSA PCT by @torben-hansen in #1968
* Expose AES_cfb1_encrypt and AES_cfb8_encrypt by @skmcgrail in
#1967

**Full Changelog**:
v1.37.0...v1.38.0

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.
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.

4 participants