Skip to content

Commit da19058

Browse files
committed
CHANGE: updated Mbed-TLS sources to version 3.5.2
1 parent 97a08a1 commit da19058

File tree

6 files changed

+80
-40
lines changed

6 files changed

+80
-40
lines changed

NOTICE

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ See included LICENSE file for details
99
Credits for Non-REBOL orginated C files and modules
1010
---------------------------------------------------
1111

12-
* MbedTLS parts (version 3.5.1):
12+
* MbedTLS parts (version 3.5.2):
1313
Copyright The Mbed TLS Contributors
1414
Apache-2.0 - https://github.com/Mbed-TLS/mbedtls/releases
1515

src/core/mbedtls/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
In this directory is cherry-picked code from the Mbed TLS library.
2-
Used version: 3.4.0 (https://github.com/Mbed-TLS/mbedtls/releases)
2+
Used version: 3.5.2 (https://github.com/Mbed-TLS/mbedtls/releases)

src/core/mbedtls/rsa.c

+58-34
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#if defined(MBEDTLS_RSA_C)
2929

3030
#include "mbedtls/rsa.h"
31+
#include "bignum_core.h"
3132
#include "rsa_alt_helpers.h"
3233
#include "mbedtls/oid.h"
3334
#include "mbedtls/platform_util.h"
@@ -969,6 +970,45 @@ static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
969970
return ret;
970971
}
971972

973+
/*
974+
* Unblind
975+
* T = T * Vf mod N
976+
*/
977+
static int rsa_unblind(mbedtls_mpi *T, mbedtls_mpi *Vf, const mbedtls_mpi *N)
978+
{
979+
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
980+
const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N->p);
981+
const size_t nlimbs = N->n;
982+
const size_t tlimbs = mbedtls_mpi_core_montmul_working_limbs(nlimbs);
983+
mbedtls_mpi RR, M_T;
984+
985+
mbedtls_mpi_init(&RR);
986+
mbedtls_mpi_init(&M_T);
987+
988+
MBEDTLS_MPI_CHK(mbedtls_mpi_core_get_mont_r2_unsafe(&RR, N));
989+
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(&M_T, tlimbs));
990+
991+
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(T, nlimbs));
992+
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Vf, nlimbs));
993+
994+
/* T = T * Vf mod N
995+
* Reminder: montmul(A, B, N) = A * B * R^-1 mod N
996+
* Usually both operands are multiplied by R mod N beforehand (by calling
997+
* `to_mont_rep()` on them), yielding a result that's also * R mod N (aka
998+
* "in the Montgomery domain"). Here we only multiply one operand by R mod
999+
* N, so the result is directly what we want - no need to call
1000+
* `from_mont_rep()` on it. */
1001+
mbedtls_mpi_core_to_mont_rep(T->p, T->p, N->p, nlimbs, mm, RR.p, M_T.p);
1002+
mbedtls_mpi_core_montmul(T->p, T->p, Vf->p, nlimbs, N->p, nlimbs, mm, M_T.p);
1003+
1004+
cleanup:
1005+
1006+
mbedtls_mpi_free(&RR);
1007+
mbedtls_mpi_free(&M_T);
1008+
1009+
return ret;
1010+
}
1011+
9721012
/*
9731013
* Exponent blinding supposed to prevent side-channel attacks using multiple
9741014
* traces of measurements to recover the RSA key. The more collisions are there,
@@ -1016,23 +1056,14 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
10161056
/* Temporaries holding the blinded exponents for
10171057
* the mod p resp. mod q computation (if used). */
10181058
mbedtls_mpi DP_blind, DQ_blind;
1019-
1020-
/* Pointers to actual exponents to be used - either the unblinded
1021-
* or the blinded ones, depending on the presence of a PRNG. */
1022-
mbedtls_mpi *DP = &ctx->DP;
1023-
mbedtls_mpi *DQ = &ctx->DQ;
10241059
#else
10251060
/* Temporary holding the blinded exponent (if used). */
10261061
mbedtls_mpi D_blind;
1027-
1028-
/* Pointer to actual exponent to be used - either the unblinded
1029-
* or the blinded one, depending on the presence of a PRNG. */
1030-
mbedtls_mpi *D = &ctx->D;
10311062
#endif /* MBEDTLS_RSA_NO_CRT */
10321063

10331064
/* Temporaries holding the initial input and the double
10341065
* checked result; should be the same in the end. */
1035-
mbedtls_mpi I, C;
1066+
mbedtls_mpi input_blinded, check_result_blinded;
10361067

10371068
if (f_rng == NULL) {
10381069
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
@@ -1067,8 +1098,8 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
10671098
mbedtls_mpi_init(&TP); mbedtls_mpi_init(&TQ);
10681099
#endif
10691100

1070-
mbedtls_mpi_init(&I);
1071-
mbedtls_mpi_init(&C);
1101+
mbedtls_mpi_init(&input_blinded);
1102+
mbedtls_mpi_init(&check_result_blinded);
10721103

10731104
/* End of MPI initialization */
10741105

@@ -1078,8 +1109,6 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
10781109
goto cleanup;
10791110
}
10801111

1081-
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&I, &T));
1082-
10831112
/*
10841113
* Blinding
10851114
* T = T * Vi mod N
@@ -1088,6 +1117,8 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
10881117
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vi));
10891118
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
10901119

1120+
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&input_blinded, &T));
1121+
10911122
/*
10921123
* Exponent blinding
10931124
*/
@@ -1103,8 +1134,6 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
11031134
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &P1, &Q1));
11041135
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&D_blind, &D_blind, &R));
11051136
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&D_blind, &D_blind, &ctx->D));
1106-
1107-
D = &D_blind;
11081137
#else
11091138
/*
11101139
* DP_blind = ( P - 1 ) * R + DP
@@ -1115,8 +1144,6 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
11151144
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DP_blind, &DP_blind,
11161145
&ctx->DP));
11171146

1118-
DP = &DP_blind;
1119-
11201147
/*
11211148
* DQ_blind = ( Q - 1 ) * R + DQ
11221149
*/
@@ -1125,12 +1152,10 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
11251152
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&DQ_blind, &Q1, &R));
11261153
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&DQ_blind, &DQ_blind,
11271154
&ctx->DQ));
1128-
1129-
DQ = &DQ_blind;
11301155
#endif /* MBEDTLS_RSA_NO_CRT */
11311156

11321157
#if defined(MBEDTLS_RSA_NO_CRT)
1133-
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, D, &ctx->N, &ctx->RN));
1158+
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&T, &T, &D_blind, &ctx->N, &ctx->RN));
11341159
#else
11351160
/*
11361161
* Faster decryption using the CRT
@@ -1139,8 +1164,8 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
11391164
* TQ = input ^ dQ mod Q
11401165
*/
11411166

1142-
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, DP, &ctx->P, &ctx->RP));
1143-
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, DQ, &ctx->Q, &ctx->RQ));
1167+
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
1168+
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));
11441169

11451170
/*
11461171
* T = (TP - TQ) * (Q^-1 mod P) mod P
@@ -1156,21 +1181,20 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
11561181
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&T, &TQ, &TP));
11571182
#endif /* MBEDTLS_RSA_NO_CRT */
11581183

1159-
/*
1160-
* Unblind
1161-
* T = T * Vf mod N
1162-
*/
1163-
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&T, &T, &ctx->Vf));
1164-
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&T, &T, &ctx->N));
1165-
11661184
/* Verify the result to prevent glitching attacks. */
1167-
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&C, &T, &ctx->E,
1185+
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&check_result_blinded, &T, &ctx->E,
11681186
&ctx->N, &ctx->RN));
1169-
if (mbedtls_mpi_cmp_mpi(&C, &I) != 0) {
1187+
if (mbedtls_mpi_cmp_mpi(&check_result_blinded, &input_blinded) != 0) {
11701188
ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
11711189
goto cleanup;
11721190
}
11731191

1192+
/*
1193+
* Unblind
1194+
* T = T * Vf mod N
1195+
*/
1196+
MBEDTLS_MPI_CHK(rsa_unblind(&T, &ctx->Vf, &ctx->N));
1197+
11741198
olen = ctx->len;
11751199
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&T, output, olen));
11761200

@@ -1198,8 +1222,8 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
11981222
mbedtls_mpi_free(&TP); mbedtls_mpi_free(&TQ);
11991223
#endif
12001224

1201-
mbedtls_mpi_free(&C);
1202-
mbedtls_mpi_free(&I);
1225+
mbedtls_mpi_free(&check_result_blinded);
1226+
mbedtls_mpi_free(&input_blinded);
12031227

12041228
if (ret != 0 && ret >= -0x007f) {
12051229
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_RSA_PRIVATE_FAILED, ret);

src/include/mbedtls/build_info.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
*/
2727
#define MBEDTLS_VERSION_MAJOR 3
2828
#define MBEDTLS_VERSION_MINOR 5
29-
#define MBEDTLS_VERSION_PATCH 1
29+
#define MBEDTLS_VERSION_PATCH 2
3030

3131
/**
3232
* The single version number has the following structure:
3333
* MMNNPP00
3434
* Major version | Minor version | Patch version
3535
*/
36-
#define MBEDTLS_VERSION_NUMBER 0x03050100
37-
#define MBEDTLS_VERSION_STRING "3.5.1"
38-
#define MBEDTLS_VERSION_STRING_FULL "Mbed TLS 3.5.1"
36+
#define MBEDTLS_VERSION_NUMBER 0x03050200
37+
#define MBEDTLS_VERSION_STRING "3.5.2"
38+
#define MBEDTLS_VERSION_STRING_FULL "Mbed TLS 3.5.2"
3939

4040
/* Macros for build-time platform detection */
4141

src/include/mbedtls/rsa.h

+9
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,10 @@ int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
684684
* It is the generic wrapper for performing a PKCS#1 decryption
685685
* operation.
686686
*
687+
* \warning When \p ctx->padding is set to #MBEDTLS_RSA_PKCS_V15,
688+
* mbedtls_rsa_rsaes_pkcs1_v15_decrypt() is called, which is an
689+
* inherently dangerous function (CWE-242).
690+
*
687691
* \note The output buffer length \c output_max_len should be
688692
* as large as the size \p ctx->len of \p ctx->N (for example,
689693
* 128 Bytes if RSA-1024 is used) to be able to hold an
@@ -720,6 +724,11 @@ int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
720724
* \brief This function performs a PKCS#1 v1.5 decryption
721725
* operation (RSAES-PKCS1-v1_5-DECRYPT).
722726
*
727+
* \warning This is an inherently dangerous function (CWE-242). Unless
728+
* it is used in a side channel free and safe way (eg.
729+
* implementing the TLS protocol as per 7.4.7.1 of RFC 5246),
730+
* the calling code is vulnerable.
731+
*
723732
* \note The output buffer length \c output_max_len should be
724733
* as large as the size \p ctx->len of \p ctx->N, for example,
725734
* 128 Bytes if RSA-1024 is used, to be able to hold an

src/include/psa/crypto_values.h

+7
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,13 @@
17361736
0)
17371737

17381738
/** RSA PKCS#1 v1.5 encryption.
1739+
*
1740+
* \warning Calling psa_asymmetric_decrypt() with this algorithm as a
1741+
* parameter is considered an inherently dangerous function
1742+
* (CWE-242). Unless it is used in a side channel free and safe
1743+
* way (eg. implementing the TLS protocol as per 7.4.7.1 of
1744+
* RFC 5246), the calling code is vulnerable.
1745+
*
17391746
*/
17401747
#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t) 0x07000200)
17411748

0 commit comments

Comments
 (0)