Extract Secp256r1 from the prototype

Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
This commit is contained in:
Gabor Mezei 2023-03-01 16:01:52 +01:00
parent ed7b5978cd
commit ab6ac91a0a
No known key found for this signature in database
GPG key ID: FEE76C0CF8C6267D
3 changed files with 79 additions and 43 deletions

View file

@ -4580,6 +4580,7 @@ int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs);
#endif #endif
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
static int ecp_mod_p256(mbedtls_mpi *); static int ecp_mod_p256(mbedtls_mpi *);
static int ecp_mod_p256_raw(mbedtls_mpi_uint *Np, size_t Nn);
#endif #endif
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
static int ecp_mod_p384(mbedtls_mpi *); static int ecp_mod_p384(mbedtls_mpi *);
@ -5098,6 +5099,81 @@ int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs)
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
/*
* Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
*/
static int ecp_mod_p256(mbedtls_mpi *N)
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
size_t expected_width = 2 * ((256 + biL - 1) / biL);
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(N, expected_width));
ret = ecp_mod_p256_raw(N->p, expected_width);
cleanup:
return ret;
}
static int ecp_mod_p256_raw(mbedtls_mpi_uint *Np, size_t Nn)
{
if (Nn != 2*((256 + biL - 1)/biL)) {
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
}
INIT(256);
ADD(8); ADD(9);
SUB(11); SUB(12); SUB(13); SUB(14); NEXT; // A0
ADD(9); ADD(10);
SUB(12); SUB(13); SUB(14); SUB(15); NEXT; // A1
ADD(10); ADD(11);
SUB(13); SUB(14); SUB(15); NEXT; // A2
ADD(11); ADD(11); ADD(12); ADD(12); ADD(13);
SUB(15); SUB(8); SUB(9); NEXT; // A3
ADD(12); ADD(12); ADD(13); ADD(13); ADD(14);
SUB(9); SUB(10); NEXT; // A4
ADD(13); ADD(13); ADD(14); ADD(14); ADD(15);
SUB(10); SUB(11); NEXT; // A5
ADD(14); ADD(14); ADD(15); ADD(15); ADD(14); ADD(13);
SUB(8); SUB(9); NEXT; // A6
ADD(15); ADD(15); ADD(15); ADD(8);
SUB(10); SUB(11); SUB(12); SUB(13); // A7
RESET;
ADD_LAST; NEXT; // A0
; NEXT; // A1
; NEXT; // A2
SUB_LAST; NEXT; // A3
; NEXT; // A4
; NEXT; // A5
SUB_LAST; NEXT; // A6
ADD_LAST; // A7
RESET;
ADD_LAST; NEXT; // A0
; NEXT; // A1
; NEXT; // A2
SUB_LAST; NEXT; // A3
; NEXT; // A4
; NEXT; // A5
SUB_LAST; NEXT; // A6
ADD_LAST; // A7
LAST;
return 0;
}
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
#undef LOAD32 #undef LOAD32
#undef MAX32 #undef MAX32
#undef A #undef A
@ -5118,8 +5194,7 @@ int mbedtls_ecp_mod_p224_raw(mbedtls_mpi_uint *X, size_t X_limbs)
MBEDTLS_ECP_DP_SECP256R1_ENABLED || MBEDTLS_ECP_DP_SECP256R1_ENABLED ||
MBEDTLS_ECP_DP_SECP384R1_ENABLED */ MBEDTLS_ECP_DP_SECP384R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
/* /*
* The reader is advised to first understand ecp_mod_p192() since the same * The reader is advised to first understand ecp_mod_p192() since the same
* general structure is used here, but with additional complications: * general structure is used here, but with additional complications:
@ -5240,43 +5315,6 @@ void mbedtls_ecp_fix_negative(mbedtls_mpi *N, signed char c, size_t bits)
N->p[bits / 8 / sizeof(mbedtls_mpi_uint)] += msw; N->p[bits / 8 / sizeof(mbedtls_mpi_uint)] += msw;
} }
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
/*
* Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
*/
static int ecp_mod_p256(mbedtls_mpi *N)
{
INIT(256);
ADD(8); ADD(9);
SUB(11); SUB(12); SUB(13); SUB(14); NEXT; // A0
ADD(9); ADD(10);
SUB(12); SUB(13); SUB(14); SUB(15); NEXT; // A1
ADD(10); ADD(11);
SUB(13); SUB(14); SUB(15); NEXT; // A2
ADD(11); ADD(11); ADD(12); ADD(12); ADD(13);
SUB(15); SUB(8); SUB(9); NEXT; // A3
ADD(12); ADD(12); ADD(13); ADD(13); ADD(14);
SUB(9); SUB(10); NEXT; // A4
ADD(13); ADD(13); ADD(14); ADD(14); ADD(15);
SUB(10); SUB(11); NEXT; // A5
ADD(14); ADD(14); ADD(15); ADD(15); ADD(14); ADD(13);
SUB(8); SUB(9); NEXT; // A6
ADD(15); ADD(15); ADD(15); ADD(8);
SUB(10); SUB(11); SUB(12); SUB(13); LAST; // A7
cleanup:
return ret;
}
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
/* /*
* Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4) * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)

View file

@ -33,8 +33,7 @@
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C) #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C)
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
/* Preconditions: /* Preconditions:
* - bits is a multiple of 64 or is 224 * - bits is a multiple of 64 or is 224
* - c is -1 or -2 * - c is -1 or -2

View file

@ -9,8 +9,7 @@
#include "bignum_mod_raw_invasive.h" #include "bignum_mod_raw_invasive.h"
#if defined(MBEDTLS_TEST_HOOKS) && \ #if defined(MBEDTLS_TEST_HOOKS) && \
(defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED))
#define HAVE_FIX_NEGATIVE #define HAVE_FIX_NEGATIVE
#endif #endif