28
28
#if defined(MBEDTLS_RSA_C )
29
29
30
30
#include "mbedtls/rsa.h"
31
+ #include "bignum_core.h"
31
32
#include "rsa_alt_helpers.h"
32
33
#include "mbedtls/oid.h"
33
34
#include "mbedtls/platform_util.h"
@@ -969,6 +970,45 @@ static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
969
970
return ret ;
970
971
}
971
972
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
+
972
1012
/*
973
1013
* Exponent blinding supposed to prevent side-channel attacks using multiple
974
1014
* 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,
1016
1056
/* Temporaries holding the blinded exponents for
1017
1057
* the mod p resp. mod q computation (if used). */
1018
1058
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 ;
1024
1059
#else
1025
1060
/* Temporary holding the blinded exponent (if used). */
1026
1061
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 ;
1031
1062
#endif /* MBEDTLS_RSA_NO_CRT */
1032
1063
1033
1064
/* Temporaries holding the initial input and the double
1034
1065
* checked result; should be the same in the end. */
1035
- mbedtls_mpi I , C ;
1066
+ mbedtls_mpi input_blinded , check_result_blinded ;
1036
1067
1037
1068
if (f_rng == NULL ) {
1038
1069
return MBEDTLS_ERR_RSA_BAD_INPUT_DATA ;
@@ -1067,8 +1098,8 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1067
1098
mbedtls_mpi_init (& TP ); mbedtls_mpi_init (& TQ );
1068
1099
#endif
1069
1100
1070
- mbedtls_mpi_init (& I );
1071
- mbedtls_mpi_init (& C );
1101
+ mbedtls_mpi_init (& input_blinded );
1102
+ mbedtls_mpi_init (& check_result_blinded );
1072
1103
1073
1104
/* End of MPI initialization */
1074
1105
@@ -1078,8 +1109,6 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1078
1109
goto cleanup ;
1079
1110
}
1080
1111
1081
- MBEDTLS_MPI_CHK (mbedtls_mpi_copy (& I , & T ));
1082
-
1083
1112
/*
1084
1113
* Blinding
1085
1114
* T = T * Vi mod N
@@ -1088,6 +1117,8 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1088
1117
MBEDTLS_MPI_CHK (mbedtls_mpi_mul_mpi (& T , & T , & ctx -> Vi ));
1089
1118
MBEDTLS_MPI_CHK (mbedtls_mpi_mod_mpi (& T , & T , & ctx -> N ));
1090
1119
1120
+ MBEDTLS_MPI_CHK (mbedtls_mpi_copy (& input_blinded , & T ));
1121
+
1091
1122
/*
1092
1123
* Exponent blinding
1093
1124
*/
@@ -1103,8 +1134,6 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1103
1134
MBEDTLS_MPI_CHK (mbedtls_mpi_mul_mpi (& D_blind , & P1 , & Q1 ));
1104
1135
MBEDTLS_MPI_CHK (mbedtls_mpi_mul_mpi (& D_blind , & D_blind , & R ));
1105
1136
MBEDTLS_MPI_CHK (mbedtls_mpi_add_mpi (& D_blind , & D_blind , & ctx -> D ));
1106
-
1107
- D = & D_blind ;
1108
1137
#else
1109
1138
/*
1110
1139
* DP_blind = ( P - 1 ) * R + DP
@@ -1115,8 +1144,6 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1115
1144
MBEDTLS_MPI_CHK (mbedtls_mpi_add_mpi (& DP_blind , & DP_blind ,
1116
1145
& ctx -> DP ));
1117
1146
1118
- DP = & DP_blind ;
1119
-
1120
1147
/*
1121
1148
* DQ_blind = ( Q - 1 ) * R + DQ
1122
1149
*/
@@ -1125,12 +1152,10 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1125
1152
MBEDTLS_MPI_CHK (mbedtls_mpi_mul_mpi (& DQ_blind , & Q1 , & R ));
1126
1153
MBEDTLS_MPI_CHK (mbedtls_mpi_add_mpi (& DQ_blind , & DQ_blind ,
1127
1154
& ctx -> DQ ));
1128
-
1129
- DQ = & DQ_blind ;
1130
1155
#endif /* MBEDTLS_RSA_NO_CRT */
1131
1156
1132
1157
#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 ));
1134
1159
#else
1135
1160
/*
1136
1161
* Faster decryption using the CRT
@@ -1139,8 +1164,8 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1139
1164
* TQ = input ^ dQ mod Q
1140
1165
*/
1141
1166
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 ));
1144
1169
1145
1170
/*
1146
1171
* T = (TP - TQ) * (Q^-1 mod P) mod P
@@ -1156,21 +1181,20 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1156
1181
MBEDTLS_MPI_CHK (mbedtls_mpi_add_mpi (& T , & TQ , & TP ));
1157
1182
#endif /* MBEDTLS_RSA_NO_CRT */
1158
1183
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
-
1166
1184
/* 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 ,
1168
1186
& 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 ) {
1170
1188
ret = MBEDTLS_ERR_RSA_VERIFY_FAILED ;
1171
1189
goto cleanup ;
1172
1190
}
1173
1191
1192
+ /*
1193
+ * Unblind
1194
+ * T = T * Vf mod N
1195
+ */
1196
+ MBEDTLS_MPI_CHK (rsa_unblind (& T , & ctx -> Vf , & ctx -> N ));
1197
+
1174
1198
olen = ctx -> len ;
1175
1199
MBEDTLS_MPI_CHK (mbedtls_mpi_write_binary (& T , output , olen ));
1176
1200
@@ -1198,8 +1222,8 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
1198
1222
mbedtls_mpi_free (& TP ); mbedtls_mpi_free (& TQ );
1199
1223
#endif
1200
1224
1201
- mbedtls_mpi_free (& C );
1202
- mbedtls_mpi_free (& I );
1225
+ mbedtls_mpi_free (& check_result_blinded );
1226
+ mbedtls_mpi_free (& input_blinded );
1203
1227
1204
1228
if (ret != 0 && ret >= -0x007f ) {
1205
1229
return MBEDTLS_ERROR_ADD (MBEDTLS_ERR_RSA_PRIVATE_FAILED , ret );
0 commit comments