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

Bignum: Extract secp384r1 fast reduction from the prototype #7222

Conversation

minosgalanakis
Copy link
Contributor

@minosgalanakis minosgalanakis commented Mar 7, 2023

Description

Extract Secp384r1 fast reduction from the prototype (replace the pre-existing implementation)

Implements the requirements set by #6851:

  • Extract the method from the prototype.
  • Implement it usings the MBEDTLS_STATIC_TESTABLE so we can test it in isolation.
  • Implementes tests in cannonical representation using Add function to fix quasi-reduction #6375.

Gatekeeper checklist

  • changelog Not required( New Interface )
  • backport Not required( New Interface )
  • tests Provided

Resolves #6851

@minosgalanakis minosgalanakis added needs-review Every commit must be reviewed by at least two team members, component-crypto Crypto primitives and low-level interfaces needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review size-s Estimated task size: small (~2d) priority-high High priority - will be reviewed soon labels Mar 7, 2023
@minosgalanakis minosgalanakis force-pushed the bignum/6851_extract_Secp384r1_fast_reduction branch from 2c25858 to 92513db Compare March 7, 2023 16:48
@minosgalanakis minosgalanakis removed the needs-ci Needs to pass CI tests label Mar 8, 2023
@gabor-mezei-arm gabor-mezei-arm self-requested a review March 8, 2023 15:00
library/ecp_curves.c Outdated Show resolved Hide resolved
library/ecp_invasive.h Outdated Show resolved Hide resolved
scripts/mbedtls_dev/ecp.py Outdated Show resolved Hide resolved
scripts/mbedtls_dev/ecp.py Outdated Show resolved Hide resolved
scripts/mbedtls_dev/ecp.py Outdated Show resolved Hide resolved
tests/suites/test_suite_ecp.function Outdated Show resolved Hide resolved
library/ecp_curves.c Outdated Show resolved Hide resolved
library/ecp_curves.c Outdated Show resolved Hide resolved
scripts/mbedtls_dev/ecp.py Show resolved Hide resolved
library/ecp_curves.c Show resolved Hide resolved
@minosgalanakis minosgalanakis force-pushed the bignum/6851_extract_Secp384r1_fast_reduction branch from 5e85a48 to dd0dc74 Compare March 9, 2023 14:51
@gabor-mezei-arm gabor-mezei-arm self-requested a review March 14, 2023 11:35
Copy link
Contributor

@gabor-mezei-arm gabor-mezei-arm left a comment

Choose a reason for hiding this comment

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

Only some minor issues found otherwise looks good to me.

library/ecp_invasive.h Outdated Show resolved Hide resolved
library/ecp_curves.c Outdated Show resolved Hide resolved
scripts/mbedtls_dev/ecp.py Show resolved Hide resolved
scripts/mbedtls_dev/ecp.py Outdated Show resolved Hide resolved
@minosgalanakis minosgalanakis force-pushed the bignum/6851_extract_Secp384r1_fast_reduction branch 3 times, most recently from 99efac9 to 187a80e Compare March 15, 2023 13:11
Copy link
Contributor

@gabor-mezei-arm gabor-mezei-arm left a comment

Choose a reason for hiding this comment

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

LGTM

@yanesca
Copy link
Contributor

yanesca commented Mar 17, 2023

Ideally we would be testing the second round of carry propagation in the end, but triggering it is not trivial and we shouldn't spend more time on it. I have raised an issue for tracking this and added to the tech debt epic: #7309

yanesca
yanesca previously approved these changes Mar 17, 2023
Copy link
Contributor

@yanesca yanesca left a comment

Choose a reason for hiding this comment

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

Needs rebase, but other than that, it looks good to me.

@gabor-mezei-arm
Copy link
Contributor

I have checked and found a number which needs the second round of carry reduction for P384:

000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000FFFFFFFF00000000000000000000000100000000000000000000000000000000FFFFFFFF00000001

Please add it to the testing.

@minosgalanakis
Copy link
Contributor Author

Adding needs review because recent merges caused conflicts.

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
This patch adjusts formatting, documentation and testing.

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
@minosgalanakis minosgalanakis force-pushed the bignum/6851_extract_Secp384r1_fast_reduction branch from 5f8f200 to 4af90bb Compare March 21, 2023 15:50
This patch re-introduces `mbedtls_ecp_fix_negative` and
appropriately adjusts its' define guards.

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
@minosgalanakis
Copy link
Contributor Author

minosgalanakis commented Mar 22, 2023

During rebase it has been discovered that even there are a couple of redudant legacy macros which were guarded by

#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
    defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)

Whilst the majority of those macros are deprecated, and no longer needed since the curves have been moved to the protype implementation, mbedtls_ecp_fix_negative is needed by other curves test data.

A commit message has been added to keep this method, but adjust its define guards to

#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C)

Which is the same that was happening indirectly by the previous guards, making it more clear.

@minosgalanakis minosgalanakis added needs-ci Needs to pass CI tests and removed needs-work needs-ci Needs to pass CI tests labels Mar 22, 2023
@minosgalanakis minosgalanakis mentioned this pull request Mar 22, 2023
3 tasks
Copy link
Contributor

@gabor-mezei-arm gabor-mezei-arm left a comment

Choose a reason for hiding this comment

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

LGTM

/*
* Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
*/
static int ecp_mod_p384(mbedtls_mpi *N)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems very weird having this function above the one it calls, given the long discussion we had last year about "I should be able to read the file in order, seeing functions defined before they are used" :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, but there are cases for file consistency > newly tidy code. Since the rest of the file is structured as such, we should keep the public method (first) -> static method (second notation.

/** Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
*
* \param[in,out] X The address of the MPI to be converted.
* Must have exact limb size of `768 / biL`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean Must have exactly '768 / biL' limbs?

(Saying "limb size" doesn't say what units are being used for "size")

static int ecp_mod_p384(mbedtls_mpi *N)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t expected_width = 2 * ((384 + biL - 1) / biL);
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. This is required_width not expected_width
  2. The doc for mbedtls_ecp_mod_p384_raw() says required size is 768 / biL, but I don't see 768 in this calculation (note: 384 is 3 * 128, so even with biL == 128 (which it is never) the + biL - 1) / biL thing wouldn't be needed. But if it is needed, please keep 768 in there, i.e. (768 + biL - 1) / biL. But 768 / biL with a comment that 768 = 3 * 256, so no need to worry about partial limbs even if limb size was 256 bits would be fine

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. Again that is ported as is from the prototype, and has been used in all the similar curves. I would rather we keep it consistent as is.
  2. This wording a request coming from the previous round of review. But I agree that is is confusing.

I will reword it to something like

Must have exact limb size that stores a 768-bit MPI
 *                          (double the bitlength of the modulus).

The size check is not performed in the X parameter but the X_limbs which is enforced to be less than 2* 386bits .

In the public method, a grow operation ensures, that the static method will be called with appropriate sizes:

MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));

/*
* For these primes, we need to handle data in chunks of 32 bits.
* This makes it more complicated if we use 64 bits limbs in MPI,
* which prevents us from using a uniform access method as for p192.
Copy link
Contributor

Choose a reason for hiding this comment

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

So just to be clear: this isn't an issue with the new code, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes and no. We are doing something similar in principle, but also different in some corner cases. Carrying over design comments from removed, curve specific macros is adding confusion.

* So, we define a mini abstraction layer to access 32 bit chunks,
* load them in 'cur' for work, and store them back from 'cur' when done.
*
* While at it, also define the size of N in terms of 32-bit chunks.
Copy link
Contributor

Choose a reason for hiding this comment

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

And we're not doing anything like this any more?

/* Set N := 2^bits - 1 - N. We know that 0 <= N < 2^bits, so
* set the absolute value to 0xfff...fff - N. There is no carry
* since we're subtracting from all-bits-one. */
for (i = 0; i <= bits / 8 / sizeof(mbedtls_mpi_uint); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Repeating bits / 8 / sizeof(mbedtls_mpi_uint) - can we have a variable for this please?

Copy link
Contributor Author

@minosgalanakis minosgalanakis Mar 23, 2023

Choose a reason for hiding this comment

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

This function is not new, it was just moved further down the code. I would rather not modify it since it falls out of the scope of this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
Copy link
Contributor

@tom-cosgrove-arm tom-cosgrove-arm left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@gabor-mezei-arm gabor-mezei-arm left a comment

Choose a reason for hiding this comment

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

LGTM

@gabor-mezei-arm gabor-mezei-arm added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Mar 27, 2023
@yanesca
Copy link
Contributor

yanesca commented Mar 27, 2023

Internal CI passed successfully.

@yanesca yanesca merged commit 445c3bf into Mbed-TLS:development Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports component-crypto Crypto primitives and low-level interfaces priority-high High priority - will be reviewed soon size-s Estimated task size: small (~2d)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extract Secp384r1 fast reduction from the prototype
4 participants