Skip to content

Commit 1f2e66d

Browse files
authored
Merge pull request #12560 from Patater/import-mbedcrypto-3.0.1
crypto: Update to Mbed Crypto 3.0.1
2 parents 5aab4c4 + 9ae0868 commit 1f2e66d

File tree

10 files changed

+160
-8
lines changed

10 files changed

+160
-8
lines changed
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mbedcrypto-3.0.0d0
1+
mbedcrypto-3.0.1

features/mbedtls/mbed-crypto/importer/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# Set the Mbed Crypto release to import (this can/should be edited before
3131
# import)
32-
CRYPTO_RELEASE ?= mbedcrypto-3.0.0d0
32+
CRYPTO_RELEASE ?= mbedcrypto-3.0.1
3333
CRYPTO_REPO_URL ?= git@github.com:ARMmbed/mbed-crypto.git
3434

3535
# Translate between Mbed Crypto namespace and Mbed OS namespace

features/mbedtls/mbed-crypto/inc/mbedtls/bignum.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ extern "C" {
185185
*/
186186
typedef struct mbedtls_mpi
187187
{
188-
int s; /*!< integer sign */
188+
int s; /*!< Sign: -1 if the mpi is negative, 1 otherwise */
189189
size_t n; /*!< total # of limbs */
190190
mbedtls_mpi_uint *p; /*!< pointer to limbs */
191191
}
@@ -594,6 +594,24 @@ int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
594594
*/
595595
int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
596596

597+
/**
598+
* \brief Check if an MPI is less than the other in constant time.
599+
*
600+
* \param X The left-hand MPI. This must point to an initialized MPI
601+
* with the same allocated length as Y.
602+
* \param Y The right-hand MPI. This must point to an initialized MPI
603+
* with the same allocated length as X.
604+
* \param ret The result of the comparison:
605+
* \c 1 if \p X is less than \p Y.
606+
* \c 0 if \p X is greater than or equal to \p Y.
607+
*
608+
* \return 0 on success.
609+
* \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
610+
* the two input MPIs is not the same.
611+
*/
612+
int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
613+
unsigned *ret );
614+
597615
/**
598616
* \brief Compare an MPI with an integer.
599617
*

features/mbedtls/mbed-crypto/inc/mbedtls/ctr_drbg.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ typedef struct mbedtls_ctr_drbg_context
177177
* minus one.
178178
* Before the initial seeding, this field
179179
* contains the amount of entropy in bytes
180-
* to use as a nonce for the initial seeding.
180+
* to use as a nonce for the initial seeding,
181+
* or -1 if no nonce length has been explicitly
182+
* set (see mbedtls_ctr_drbg_set_nonce_len()).
181183
*/
182184
int prediction_resistance; /*!< This determines whether prediction
183185
resistance is enabled, that is

features/mbedtls/mbed-crypto/src/aes.c

+24
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,18 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
919919
PUT_UINT32_LE( X2, output, 8 );
920920
PUT_UINT32_LE( X3, output, 12 );
921921

922+
mbedtls_platform_zeroize( &X0, sizeof( X0 ) );
923+
mbedtls_platform_zeroize( &X1, sizeof( X1 ) );
924+
mbedtls_platform_zeroize( &X2, sizeof( X2 ) );
925+
mbedtls_platform_zeroize( &X3, sizeof( X3 ) );
926+
927+
mbedtls_platform_zeroize( &Y0, sizeof( Y0 ) );
928+
mbedtls_platform_zeroize( &Y1, sizeof( Y1 ) );
929+
mbedtls_platform_zeroize( &Y2, sizeof( Y2 ) );
930+
mbedtls_platform_zeroize( &Y3, sizeof( Y3 ) );
931+
932+
mbedtls_platform_zeroize( &RK, sizeof( RK ) );
933+
922934
return( 0 );
923935
}
924936
#endif /* !MBEDTLS_AES_ENCRYPT_ALT */
@@ -987,6 +999,18 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
987999
PUT_UINT32_LE( X2, output, 8 );
9881000
PUT_UINT32_LE( X3, output, 12 );
9891001

1002+
mbedtls_platform_zeroize( &X0, sizeof( X0 ) );
1003+
mbedtls_platform_zeroize( &X1, sizeof( X1 ) );
1004+
mbedtls_platform_zeroize( &X2, sizeof( X2 ) );
1005+
mbedtls_platform_zeroize( &X3, sizeof( X3 ) );
1006+
1007+
mbedtls_platform_zeroize( &Y0, sizeof( Y0 ) );
1008+
mbedtls_platform_zeroize( &Y1, sizeof( Y1 ) );
1009+
mbedtls_platform_zeroize( &Y2, sizeof( Y2 ) );
1010+
mbedtls_platform_zeroize( &Y3, sizeof( Y3 ) );
1011+
1012+
mbedtls_platform_zeroize( &RK, sizeof( RK ) );
1013+
9901014
return( 0 );
9911015
}
9921016
#endif /* !MBEDTLS_AES_DECRYPT_ALT */

features/mbedtls/mbed-crypto/src/bignum.c

+101
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,107 @@ int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
11491149
return( 0 );
11501150
}
11511151

1152+
/** Decide if an integer is less than the other, without branches.
1153+
*
1154+
* \param x First integer.
1155+
* \param y Second integer.
1156+
*
1157+
* \return 1 if \p x is less than \p y, 0 otherwise
1158+
*/
1159+
static unsigned ct_lt_mpi_uint( const mbedtls_mpi_uint x,
1160+
const mbedtls_mpi_uint y )
1161+
{
1162+
mbedtls_mpi_uint ret;
1163+
mbedtls_mpi_uint cond;
1164+
1165+
/*
1166+
* Check if the most significant bits (MSB) of the operands are different.
1167+
*/
1168+
cond = ( x ^ y );
1169+
/*
1170+
* If the MSB are the same then the difference x-y will be negative (and
1171+
* have its MSB set to 1 during conversion to unsigned) if and only if x<y.
1172+
*/
1173+
ret = ( x - y ) & ~cond;
1174+
/*
1175+
* If the MSB are different, then the operand with the MSB of 1 is the
1176+
* bigger. (That is if y has MSB of 1, then x<y is true and it is false if
1177+
* the MSB of y is 0.)
1178+
*/
1179+
ret |= y & cond;
1180+
1181+
1182+
ret = ret >> ( biL - 1 );
1183+
1184+
return (unsigned) ret;
1185+
}
1186+
1187+
/*
1188+
* Compare signed values in constant time
1189+
*/
1190+
int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
1191+
unsigned *ret )
1192+
{
1193+
size_t i;
1194+
/* The value of any of these variables is either 0 or 1 at all times. */
1195+
unsigned cond, done, X_is_negative, Y_is_negative;
1196+
1197+
MPI_VALIDATE_RET( X != NULL );
1198+
MPI_VALIDATE_RET( Y != NULL );
1199+
MPI_VALIDATE_RET( ret != NULL );
1200+
1201+
if( X->n != Y->n )
1202+
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
1203+
1204+
/*
1205+
* Set sign_N to 1 if N >= 0, 0 if N < 0.
1206+
* We know that N->s == 1 if N >= 0 and N->s == -1 if N < 0.
1207+
*/
1208+
X_is_negative = ( X->s & 2 ) >> 1;
1209+
Y_is_negative = ( Y->s & 2 ) >> 1;
1210+
1211+
/*
1212+
* If the signs are different, then the positive operand is the bigger.
1213+
* That is if X is negative (X_is_negative == 1), then X < Y is true and it
1214+
* is false if X is positive (X_is_negative == 0).
1215+
*/
1216+
cond = ( X_is_negative ^ Y_is_negative );
1217+
*ret = cond & X_is_negative;
1218+
1219+
/*
1220+
* This is a constant-time function. We might have the result, but we still
1221+
* need to go through the loop. Record if we have the result already.
1222+
*/
1223+
done = cond;
1224+
1225+
for( i = X->n; i > 0; i-- )
1226+
{
1227+
/*
1228+
* If Y->p[i - 1] < X->p[i - 1] then X < Y is true if and only if both
1229+
* X and Y are negative.
1230+
*
1231+
* Again even if we can make a decision, we just mark the result and
1232+
* the fact that we are done and continue looping.
1233+
*/
1234+
cond = ct_lt_mpi_uint( Y->p[i - 1], X->p[i - 1] );
1235+
*ret |= cond & ( 1 - done ) & X_is_negative;
1236+
done |= cond;
1237+
1238+
/*
1239+
* If X->p[i - 1] < Y->p[i - 1] then X < Y is true if and only if both
1240+
* X and Y are positive.
1241+
*
1242+
* Again even if we can make a decision, we just mark the result and
1243+
* the fact that we are done and continue looping.
1244+
*/
1245+
cond = ct_lt_mpi_uint( X->p[i - 1], Y->p[i - 1] );
1246+
*ret |= cond & ( 1 - done ) & ( 1 - X_is_negative );
1247+
done |= cond;
1248+
}
1249+
1250+
return( 0 );
1251+
}
1252+
11521253
/*
11531254
* Compare signed values
11541255
*/

features/mbedtls/mbed-crypto/src/ctr_drbg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ int mbedtls_ctr_drbg_random_with_add( void *p_rng,
585585
exit:
586586
mbedtls_platform_zeroize( add_input, sizeof( add_input ) );
587587
mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
588-
return( 0 );
588+
return( ret );
589589
}
590590

591591
int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output,

features/mbedtls/mbed-crypto/src/ecdsa.c

+1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
364364
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
365365
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
366366
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pk, pk, &t ) );
367+
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pk, pk, &grp->N ) );
367368
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, pk, &grp->N ) );
368369
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
369370
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );

features/mbedtls/mbed-crypto/src/ecp.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,7 @@ int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
28042804
{
28052805
/* SEC1 3.2.1: Generate d such that 1 <= n < N */
28062806
int count = 0;
2807+
unsigned cmp = 0;
28072808

28082809
/*
28092810
* Match the procedure given in RFC 6979 (deterministic ECDSA):
@@ -2828,9 +2829,14 @@ int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
28282829
*/
28292830
if( ++count > 30 )
28302831
return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
2832+
2833+
ret = mbedtls_mpi_lt_mpi_ct( d, &grp->N, &cmp );
2834+
if( ret != 0 )
2835+
{
2836+
goto cleanup;
2837+
}
28312838
}
2832-
while( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2833-
mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 );
2839+
while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || cmp != 1 );
28342840
}
28352841
#endif /* ECP_SHORTWEIERSTRASS */
28362842

features/mbedtls/mbed-crypto/src/gcm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void gcm_mult( mbedtls_gcm_context *ctx, const unsigned char x[16],
247247
for( i = 15; i >= 0; i-- )
248248
{
249249
lo = x[i] & 0xf;
250-
hi = x[i] >> 4;
250+
hi = ( x[i] >> 4 ) & 0xf;
251251

252252
if( i != 15 )
253253
{

0 commit comments

Comments
 (0)