-
Notifications
You must be signed in to change notification settings - Fork 121
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
P-384/521 fallback to small implementation when OPENSSL_SMALL is set #984
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
063c84f
P-384/521 fallback to small implementation when OPENSSL_SMALL is set
07f4ad1
update ec_test.cc
74ec9ff
update ecdh_test.cc
ee2cbae
fix
6933e9a
Merge remote-tracking branch 'upstream/main' into p384-p521-openssl-s…
ab0b9e4
Merge branch 'main' into p384-p521-openssl-small
dkostic adea0db
Merge branch 'main' into p384-p521-openssl-small
dkostic 7961630
Merge remote-tracking branch 'upstream/main' into p384-p521-openssl-s…
59beda7
adapt comments
a9061c2
Update crypto/fipsmodule/ec/ec_test.cc
dkostic 5cdc87c
Update crypto/ecdh_extra/ecdh_test.cc
dkostic 47ae1d5
Merge branch 'main' into p384-p521-openssl-small
dkostic 24da20b
Merge branch 'main' into p384-p521-openssl-small
dkostic 4d7cf61
Merge branch 'main' into p384-p521-openssl-small
dkostic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1845,12 +1845,19 @@ static bool HasSuffix(const char *str, const char *suffix) { | |||||||||||||
return strcmp(str + str_len - suffix_len, suffix) == 0; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
static int has_uint128_and_not_small() { | ||||||||||||||
// Returns 1 if the curve defined by |nid| is using Montgomery representation | ||||||||||||||
// for field elements (based on the build configuration). Returns 0 otherwise. | ||||||||||||||
static int is_curve_using_mont_felem_impl(int nid) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment on aws-lc/crypto/fipsmodule/ec/ec_test.cc Lines 1891 to 1896 in adea0db
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed. |
||||||||||||||
if (nid == NID_secp224r1) { | ||||||||||||||
#if defined(BORINGSSL_HAS_UINT128) && !defined(OPENSSL_SMALL) | ||||||||||||||
return 1; | ||||||||||||||
#else | ||||||||||||||
return 0; | ||||||||||||||
return 0; | ||||||||||||||
#endif | ||||||||||||||
} else if (nid == NID_secp521r1) { | ||||||||||||||
#if !defined(OPENSSL_SMALL) | ||||||||||||||
return 0; | ||||||||||||||
#endif | ||||||||||||||
} | ||||||||||||||
return 1; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// Test for out-of-range coordinates in public-key validation in | ||||||||||||||
|
@@ -1881,12 +1888,10 @@ TEST(ECTest, LargeXCoordinateVectors) { | |||||||||||||
|
||||||||||||||
size_t len = BN_num_bytes(&group.get()->field); // Modulus byte-length | ||||||||||||||
ASSERT_TRUE(EC_KEY_set_group(key.get(), group.get())); | ||||||||||||||
// The following call converts the point to Montgomery form for P-256/384. | ||||||||||||||
// For P-224, when the functions from simple.c are used, i.e. when | ||||||||||||||
// group->meth = EC_GFp_nistp224_method, the coordinate representation | ||||||||||||||
// is not changed. This is determined based on compile flags in ec.c | ||||||||||||||
// that are also used below in has_uint128_and_not_small(). | ||||||||||||||
// For P-521, the plain non-Motgomery representation is always used. | ||||||||||||||
|
||||||||||||||
// |EC_POINT_set_affine_coordinates_GFp| sets given (x, y) according to the | ||||||||||||||
// form the curve is using. If the curve is using Montgomery form, |x| and | ||||||||||||||
// |y| will be converted to Montgomery form. | ||||||||||||||
ASSERT_TRUE(EC_POINT_set_affine_coordinates_GFp( | ||||||||||||||
group.get(), pub_key.get(), x.get(), y.get(), nullptr)); | ||||||||||||||
ASSERT_TRUE(EC_KEY_set_public_key(key.get(), pub_key.get())); | ||||||||||||||
|
@@ -1901,15 +1906,12 @@ TEST(ECTest, LargeXCoordinateVectors) { | |||||||||||||
OPENSSL_memset(key.get()->pub_key->raw.Z.bytes, 0, len); | ||||||||||||||
key.get()->pub_key->raw.Z.bytes[0] = 1; | ||||||||||||||
|
||||||||||||||
// As mentioned, for P-224 and P-521, setting the raw point directly | ||||||||||||||
// with the coordinates still passes |EC_KEY_check_fips|. | ||||||||||||||
// For P-256 and 384, the failure is due to that the coordinates are | ||||||||||||||
// not in Montgomery representation, hence the checks fail earlier in | ||||||||||||||
// |EC_KEY_check_key| in the point-on-the-curve calculations, which use | ||||||||||||||
// Montgomery arithmetic. | ||||||||||||||
// |EC_KEY_check_fips| first calls the |EC_KEY_check_key| function that | ||||||||||||||
// checks if the key point is on the curve (among other checks). If the | ||||||||||||||
// curve uses Montgomery form the point-on-curve check will fail because | ||||||||||||||
// we set the raw point coordinates in regular form above. | ||||||||||||||
int curve_nid = group.get()->curve_name; | ||||||||||||||
if ((has_uint128_and_not_small() && (curve_nid == NID_secp224r1)) || | ||||||||||||||
(curve_nid == NID_secp521r1)) { | ||||||||||||||
if (!is_curve_using_mont_felem_impl(curve_nid)) { | ||||||||||||||
ASSERT_TRUE(EC_KEY_check_fips(key.get())); | ||||||||||||||
} else { | ||||||||||||||
ASSERT_FALSE(EC_KEY_check_fips(key.get())); | ||||||||||||||
|
@@ -1921,14 +1923,12 @@ TEST(ECTest, LargeXCoordinateVectors) { | |||||||||||||
// Now replace the x-coordinate with the larger one, x+p. | ||||||||||||||
OPENSSL_memcpy(key.get()->pub_key->raw.X.bytes, | ||||||||||||||
(const uint8_t *)xpp.get()->d, len); | ||||||||||||||
// We expect |EC_KEY_check_fips| to always fail when given key with x > p. | ||||||||||||||
ASSERT_FALSE(EC_KEY_check_fips(key.get())); | ||||||||||||||
|
||||||||||||||
// |EC_KEY_check_fips| check on coordinate range can only be exercised | ||||||||||||||
// for P-224 and P-521 when the coordinates in the raw point are not | ||||||||||||||
// in Montgomery representation. For the other curves, they fail | ||||||||||||||
// for the same reason as above. | ||||||||||||||
if ((has_uint128_and_not_small() && (curve_nid == NID_secp224r1)) || | ||||||||||||||
(curve_nid == NID_secp521r1)) { | ||||||||||||||
// But the failure is for different reasons in case of curves using the | ||||||||||||||
// Montgomery form versus those that don't, as explained above. | ||||||||||||||
if (!is_curve_using_mont_felem_impl(curve_nid)) { | ||||||||||||||
EXPECT_EQ(EC_R_COORDINATES_OUT_OF_RANGE, | ||||||||||||||
ERR_GET_REASON(ERR_peek_last_error_line(&file, &line))); | ||||||||||||||
EXPECT_PRED2(HasSuffix, file, "ec_key.c"); // within EC_KEY_check_fips | ||||||||||||||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment in
aws-lc/crypto/ecdh_extra/ecdh_test.cc
Lines 180 to 185 in adea0db
Or feel free to edit it to make it clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.