alternative-fix-for-CVE-2022-4304.patch 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. From 0372649a943fb23f7f08c7acdbc01464b9df03f0 Mon Sep 17 00:00:00 2001
  2. From: Bernd Edlinger <bernd.edlinger@hotmail.de>
  3. Date: Tue, 14 Feb 2023 14:28:47 +0100
  4. Subject: Revert "Fix Timing Oracle in RSA decryption"
  5. This reverts commit 43d8f88511991533f53680a751e9326999a6a31f.
  6. Reviewed-by: Paul Dale <pauli@openssl.org>
  7. Reviewed-by: Tomas Mraz <tomas@openssl.org>
  8. (Merged from https://github.com/openssl/openssl/pull/20284)
  9. ---
  10. crypto/bn/bn_blind.c | 14 ++++++++++++++
  11. crypto/bn/bn_err.c | 2 --
  12. crypto/bn/bn_local.h | 14 --------------
  13. crypto/rsa/rsa_ossl.c | 17 ++++-------------
  14. include/crypto/bn.h | 5 -----
  15. include/openssl/bnerr.h | 1 -
  16. 6 files changed, 18 insertions(+), 35 deletions(-)
  17. diff --git a/crypto/bn/bn_blind.c b/crypto/bn/bn_blind.c
  18. index dd5beea7c9..15d9e0a544 100644
  19. --- a/crypto/bn/bn_blind.c
  20. +++ b/crypto/bn/bn_blind.c
  21. @@ -13,6 +13,20 @@
  22. #define BN_BLINDING_COUNTER 32
  23. +struct bn_blinding_st {
  24. + BIGNUM *A;
  25. + BIGNUM *Ai;
  26. + BIGNUM *e;
  27. + BIGNUM *mod; /* just a reference */
  28. + CRYPTO_THREAD_ID tid;
  29. + int counter;
  30. + unsigned long flags;
  31. + BN_MONT_CTX *m_ctx;
  32. + int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  33. + const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  34. + CRYPTO_RWLOCK *lock;
  35. +};
  36. +
  37. BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
  38. {
  39. BN_BLINDING *ret = NULL;
  40. diff --git a/crypto/bn/bn_err.c b/crypto/bn/bn_err.c
  41. index 6f5464b540..6ea5fc3d5c 100644
  42. --- a/crypto/bn/bn_err.c
  43. +++ b/crypto/bn/bn_err.c
  44. @@ -73,8 +73,6 @@ static const ERR_STRING_DATA BN_str_functs[] = {
  45. {ERR_PACK(ERR_LIB_BN, BN_F_BN_SET_WORDS, 0), "bn_set_words"},
  46. {ERR_PACK(ERR_LIB_BN, BN_F_BN_STACK_PUSH, 0), "BN_STACK_push"},
  47. {ERR_PACK(ERR_LIB_BN, BN_F_BN_USUB, 0), "BN_usub"},
  48. - {ERR_PACK(ERR_LIB_BN, BN_F_OSSL_BN_RSA_DO_UNBLIND, 0),
  49. - "ossl_bn_rsa_do_unblind"},
  50. {0, NULL}
  51. };
  52. diff --git a/crypto/bn/bn_local.h b/crypto/bn/bn_local.h
  53. index 30b7614fdb..ee6342b60c 100644
  54. --- a/crypto/bn/bn_local.h
  55. +++ b/crypto/bn/bn_local.h
  56. @@ -283,20 +283,6 @@ struct bn_gencb_st {
  57. } cb;
  58. };
  59. -struct bn_blinding_st {
  60. - BIGNUM *A;
  61. - BIGNUM *Ai;
  62. - BIGNUM *e;
  63. - BIGNUM *mod; /* just a reference */
  64. - CRYPTO_THREAD_ID tid;
  65. - int counter;
  66. - unsigned long flags;
  67. - BN_MONT_CTX *m_ctx;
  68. - int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  69. - const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  70. - CRYPTO_RWLOCK *lock;
  71. -};
  72. -
  73. /*-
  74. * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
  75. *
  76. diff --git a/crypto/rsa/rsa_ossl.c b/crypto/rsa/rsa_ossl.c
  77. index 2e3ee4ab33..53cf2d03c9 100644
  78. --- a/crypto/rsa/rsa_ossl.c
  79. +++ b/crypto/rsa/rsa_ossl.c
  80. @@ -465,20 +465,11 @@ static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
  81. BN_free(d);
  82. }
  83. - if (blinding) {
  84. - /*
  85. - * ossl_bn_rsa_do_unblind() combines blinding inversion and
  86. - * 0-padded BN BE serialization
  87. - */
  88. - j = ossl_bn_rsa_do_unblind(ret, blinding, unblind, rsa->n, ctx,
  89. - buf, num);
  90. - if (j == 0)
  91. - goto err;
  92. - } else {
  93. - j = BN_bn2binpad(ret, buf, num);
  94. - if (j < 0)
  95. + if (blinding)
  96. + if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
  97. goto err;
  98. - }
  99. +
  100. + j = BN_bn2binpad(ret, buf, num);
  101. switch (padding) {
  102. case RSA_PKCS1_PADDING:
  103. diff --git a/include/crypto/bn.h b/include/crypto/bn.h
  104. index 9f866ed71a..250914c46a 100644
  105. --- a/include/crypto/bn.h
  106. +++ b/include/crypto/bn.h
  107. @@ -86,10 +86,5 @@ int bn_lshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n);
  108. int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n);
  109. int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
  110. const BIGNUM *d, BN_CTX *ctx);
  111. -int ossl_bn_rsa_do_unblind(const BIGNUM *intermediate,
  112. - const BN_BLINDING *blinding,
  113. - const BIGNUM *possible_arg2,
  114. - const BIGNUM *to_mod, BN_CTX *ctx,
  115. - unsigned char *buf, int num);
  116. #endif
  117. diff --git a/include/openssl/bnerr.h b/include/openssl/bnerr.h
  118. index 5c83777f9f..a703efc92b 100644
  119. --- a/include/openssl/bnerr.h
  120. +++ b/include/openssl/bnerr.h
  121. @@ -72,7 +72,6 @@ int ERR_load_BN_strings(void);
  122. # define BN_F_BN_SET_WORDS 144
  123. # define BN_F_BN_STACK_PUSH 148
  124. # define BN_F_BN_USUB 115
  125. -# define BN_F_OSSL_BN_RSA_DO_UNBLIND 151
  126. /*
  127. * BN reason codes.
  128. --
  129. 2.25.1
  130. From 3f499b24f3bcd66db022074f7e8b4f6ee266a3ae Mon Sep 17 00:00:00 2001
  131. From: Bernd Edlinger <bernd.edlinger@hotmail.de>
  132. Date: Mon, 13 Feb 2023 17:46:41 +0100
  133. Subject: Alternative fix for CVE-2022-4304
  134. This is about a timing leak in the topmost limb
  135. of the internal result of RSA_private_decrypt,
  136. before the padding check.
  137. There are in fact at least three bugs together that
  138. caused the timing leak:
  139. First and probably most important is the fact that
  140. the blinding did not use the constant time code path
  141. at all when the RSA object was used for a private
  142. decrypt, due to the fact that the Montgomery context
  143. rsa->_method_mod_n was not set up early enough in
  144. rsa_ossl_private_decrypt, when BN_BLINDING_create_param
  145. needed it, and that was persisted as blinding->m_ctx,
  146. although the RSA object creates the Montgomery context
  147. just a bit later.
  148. Then the infamous bn_correct_top was used on the
  149. secret value right after the blinding was removed.
  150. And finally the function BN_bn2binpad did not use
  151. the constant-time code path since the BN_FLG_CONSTTIME
  152. was not set on the secret value.
  153. In order to address the first problem, this patch
  154. makes sure that the rsa->_method_mod_n is initialized
  155. right before the blinding context.
  156. And to fix the second problem, we add a new utility
  157. function bn_correct_top_consttime, a const-time
  158. variant of bn_correct_top.
  159. Together with the fact, that BN_bn2binpad is already
  160. constant time if the flag BN_FLG_CONSTTIME is set,
  161. this should eliminate the timing oracle completely.
  162. In addition the no-asm variant may also have
  163. branches that depend on secret values, because the last
  164. invocation of bn_sub_words in bn_from_montgomery_word
  165. had branches when the function is compiled by certain
  166. gcc compiler versions, due to the clumsy coding style.
  167. So additionally this patch stream-lined the no-asm
  168. C-code in order to avoid branches where possible and
  169. improve the resulting code quality.
  170. Reviewed-by: Paul Dale <pauli@openssl.org>
  171. Reviewed-by: Tomas Mraz <tomas@openssl.org>
  172. (Merged from https://github.com/openssl/openssl/pull/20284)
  173. ---
  174. crypto/bn/bn_asm.c | 106 +++++++++++++++++++++++-------------------
  175. crypto/bn/bn_blind.c | 3 +-
  176. crypto/bn/bn_lib.c | 22 +++++++++
  177. crypto/bn/bn_local.h | 26 +++++------
  178. crypto/rsa/rsa_ossl.c | 13 +++---
  179. 5 files changed, 101 insertions(+), 69 deletions(-)
  180. diff --git a/crypto/bn/bn_asm.c b/crypto/bn/bn_asm.c
  181. index 4d83a8cf11..177558c647 100644
  182. --- a/crypto/bn/bn_asm.c
  183. +++ b/crypto/bn/bn_asm.c
  184. @@ -381,25 +381,33 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  185. #ifndef OPENSSL_SMALL_FOOTPRINT
  186. while (n & ~3) {
  187. t1 = a[0];
  188. - t2 = b[0];
  189. - r[0] = (t1 - t2 - c) & BN_MASK2;
  190. - if (t1 != t2)
  191. - c = (t1 < t2);
  192. + t2 = (t1 - c) & BN_MASK2;
  193. + c = (t2 > t1);
  194. + t1 = b[0];
  195. + t1 = (t2 - t1) & BN_MASK2;
  196. + r[0] = t1;
  197. + c += (t1 > t2);
  198. t1 = a[1];
  199. - t2 = b[1];
  200. - r[1] = (t1 - t2 - c) & BN_MASK2;
  201. - if (t1 != t2)
  202. - c = (t1 < t2);
  203. + t2 = (t1 - c) & BN_MASK2;
  204. + c = (t2 > t1);
  205. + t1 = b[1];
  206. + t1 = (t2 - t1) & BN_MASK2;
  207. + r[1] = t1;
  208. + c += (t1 > t2);
  209. t1 = a[2];
  210. - t2 = b[2];
  211. - r[2] = (t1 - t2 - c) & BN_MASK2;
  212. - if (t1 != t2)
  213. - c = (t1 < t2);
  214. + t2 = (t1 - c) & BN_MASK2;
  215. + c = (t2 > t1);
  216. + t1 = b[2];
  217. + t1 = (t2 - t1) & BN_MASK2;
  218. + r[2] = t1;
  219. + c += (t1 > t2);
  220. t1 = a[3];
  221. - t2 = b[3];
  222. - r[3] = (t1 - t2 - c) & BN_MASK2;
  223. - if (t1 != t2)
  224. - c = (t1 < t2);
  225. + t2 = (t1 - c) & BN_MASK2;
  226. + c = (t2 > t1);
  227. + t1 = b[3];
  228. + t1 = (t2 - t1) & BN_MASK2;
  229. + r[3] = t1;
  230. + c += (t1 > t2);
  231. a += 4;
  232. b += 4;
  233. r += 4;
  234. @@ -408,10 +416,12 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  235. #endif
  236. while (n) {
  237. t1 = a[0];
  238. - t2 = b[0];
  239. - r[0] = (t1 - t2 - c) & BN_MASK2;
  240. - if (t1 != t2)
  241. - c = (t1 < t2);
  242. + t2 = (t1 - c) & BN_MASK2;
  243. + c = (t2 > t1);
  244. + t1 = b[0];
  245. + t1 = (t2 - t1) & BN_MASK2;
  246. + r[0] = t1;
  247. + c += (t1 > t2);
  248. a++;
  249. b++;
  250. r++;
  251. @@ -446,7 +456,7 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  252. t += c0; /* no carry */ \
  253. c0 = (BN_ULONG)Lw(t); \
  254. hi = (BN_ULONG)Hw(t); \
  255. - c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
  256. + c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
  257. } while(0)
  258. # define mul_add_c2(a,b,c0,c1,c2) do { \
  259. @@ -455,11 +465,11 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  260. BN_ULLONG tt = t+c0; /* no carry */ \
  261. c0 = (BN_ULONG)Lw(tt); \
  262. hi = (BN_ULONG)Hw(tt); \
  263. - c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
  264. + c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
  265. t += c0; /* no carry */ \
  266. c0 = (BN_ULONG)Lw(t); \
  267. hi = (BN_ULONG)Hw(t); \
  268. - c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
  269. + c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
  270. } while(0)
  271. # define sqr_add_c(a,i,c0,c1,c2) do { \
  272. @@ -468,7 +478,7 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  273. t += c0; /* no carry */ \
  274. c0 = (BN_ULONG)Lw(t); \
  275. hi = (BN_ULONG)Hw(t); \
  276. - c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
  277. + c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
  278. } while(0)
  279. # define sqr_add_c2(a,i,j,c0,c1,c2) \
  280. @@ -483,26 +493,26 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  281. BN_ULONG ta = (a), tb = (b); \
  282. BN_ULONG lo, hi; \
  283. BN_UMULT_LOHI(lo,hi,ta,tb); \
  284. - c0 += lo; hi += (c0<lo)?1:0; \
  285. - c1 += hi; c2 += (c1<hi)?1:0; \
  286. + c0 += lo; hi += (c0<lo); \
  287. + c1 += hi; c2 += (c1<hi); \
  288. } while(0)
  289. # define mul_add_c2(a,b,c0,c1,c2) do { \
  290. BN_ULONG ta = (a), tb = (b); \
  291. BN_ULONG lo, hi, tt; \
  292. BN_UMULT_LOHI(lo,hi,ta,tb); \
  293. - c0 += lo; tt = hi+((c0<lo)?1:0); \
  294. - c1 += tt; c2 += (c1<tt)?1:0; \
  295. - c0 += lo; hi += (c0<lo)?1:0; \
  296. - c1 += hi; c2 += (c1<hi)?1:0; \
  297. + c0 += lo; tt = hi + (c0<lo); \
  298. + c1 += tt; c2 += (c1<tt); \
  299. + c0 += lo; hi += (c0<lo); \
  300. + c1 += hi; c2 += (c1<hi); \
  301. } while(0)
  302. # define sqr_add_c(a,i,c0,c1,c2) do { \
  303. BN_ULONG ta = (a)[i]; \
  304. BN_ULONG lo, hi; \
  305. BN_UMULT_LOHI(lo,hi,ta,ta); \
  306. - c0 += lo; hi += (c0<lo)?1:0; \
  307. - c1 += hi; c2 += (c1<hi)?1:0; \
  308. + c0 += lo; hi += (c0<lo); \
  309. + c1 += hi; c2 += (c1<hi); \
  310. } while(0)
  311. # define sqr_add_c2(a,i,j,c0,c1,c2) \
  312. @@ -517,26 +527,26 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  313. BN_ULONG ta = (a), tb = (b); \
  314. BN_ULONG lo = ta * tb; \
  315. BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
  316. - c0 += lo; hi += (c0<lo)?1:0; \
  317. - c1 += hi; c2 += (c1<hi)?1:0; \
  318. + c0 += lo; hi += (c0<lo); \
  319. + c1 += hi; c2 += (c1<hi); \
  320. } while(0)
  321. # define mul_add_c2(a,b,c0,c1,c2) do { \
  322. BN_ULONG ta = (a), tb = (b), tt; \
  323. BN_ULONG lo = ta * tb; \
  324. BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
  325. - c0 += lo; tt = hi + ((c0<lo)?1:0); \
  326. - c1 += tt; c2 += (c1<tt)?1:0; \
  327. - c0 += lo; hi += (c0<lo)?1:0; \
  328. - c1 += hi; c2 += (c1<hi)?1:0; \
  329. + c0 += lo; tt = hi + (c0<lo); \
  330. + c1 += tt; c2 += (c1<tt); \
  331. + c0 += lo; hi += (c0<lo); \
  332. + c1 += hi; c2 += (c1<hi); \
  333. } while(0)
  334. # define sqr_add_c(a,i,c0,c1,c2) do { \
  335. BN_ULONG ta = (a)[i]; \
  336. BN_ULONG lo = ta * ta; \
  337. BN_ULONG hi = BN_UMULT_HIGH(ta,ta); \
  338. - c0 += lo; hi += (c0<lo)?1:0; \
  339. - c1 += hi; c2 += (c1<hi)?1:0; \
  340. + c0 += lo; hi += (c0<lo); \
  341. + c1 += hi; c2 += (c1<hi); \
  342. } while(0)
  343. # define sqr_add_c2(a,i,j,c0,c1,c2) \
  344. @@ -551,8 +561,8 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  345. BN_ULONG lo = LBITS(a), hi = HBITS(a); \
  346. BN_ULONG bl = LBITS(b), bh = HBITS(b); \
  347. mul64(lo,hi,bl,bh); \
  348. - c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
  349. - c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
  350. + c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
  351. + c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
  352. } while(0)
  353. # define mul_add_c2(a,b,c0,c1,c2) do { \
  354. @@ -561,17 +571,17 @@ BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  355. BN_ULONG bl = LBITS(b), bh = HBITS(b); \
  356. mul64(lo,hi,bl,bh); \
  357. tt = hi; \
  358. - c0 = (c0+lo)&BN_MASK2; if (c0<lo) tt++; \
  359. - c1 = (c1+tt)&BN_MASK2; if (c1<tt) c2++; \
  360. - c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
  361. - c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
  362. + c0 = (c0+lo)&BN_MASK2; tt += (c0<lo); \
  363. + c1 = (c1+tt)&BN_MASK2; c2 += (c1<tt); \
  364. + c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
  365. + c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
  366. } while(0)
  367. # define sqr_add_c(a,i,c0,c1,c2) do { \
  368. BN_ULONG lo, hi; \
  369. sqr64(lo,hi,(a)[i]); \
  370. - c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
  371. - c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
  372. + c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
  373. + c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
  374. } while(0)
  375. # define sqr_add_c2(a,i,j,c0,c1,c2) \
  376. diff --git a/crypto/bn/bn_blind.c b/crypto/bn/bn_blind.c
  377. index 15d9e0a544..e76f6107a7 100644
  378. --- a/crypto/bn/bn_blind.c
  379. +++ b/crypto/bn/bn_blind.c
  380. @@ -191,7 +191,8 @@ int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
  381. n->top = (int)(rtop & ~mask) | (ntop & mask);
  382. n->flags |= (BN_FLG_FIXED_TOP & ~mask);
  383. }
  384. - ret = BN_mod_mul_montgomery(n, n, r, b->m_ctx, ctx);
  385. + ret = bn_mul_mont_fixed_top(n, n, r, b->m_ctx, ctx);
  386. + bn_correct_top_consttime(n);
  387. } else {
  388. ret = BN_mod_mul(n, n, r, b->mod, ctx);
  389. }
  390. diff --git a/crypto/bn/bn_lib.c b/crypto/bn/bn_lib.c
  391. index eb4a31849b..fe6fb0e40f 100644
  392. --- a/crypto/bn/bn_lib.c
  393. +++ b/crypto/bn/bn_lib.c
  394. @@ -1001,6 +1001,28 @@ BIGNUM *bn_wexpand(BIGNUM *a, int words)
  395. return (words <= a->dmax) ? a : bn_expand2(a, words);
  396. }
  397. +void bn_correct_top_consttime(BIGNUM *a)
  398. +{
  399. + int j, atop;
  400. + BN_ULONG limb;
  401. + unsigned int mask;
  402. +
  403. + for (j = 0, atop = 0; j < a->dmax; j++) {
  404. + limb = a->d[j];
  405. + limb |= 0 - limb;
  406. + limb >>= BN_BITS2 - 1;
  407. + limb = 0 - limb;
  408. + mask = (unsigned int)limb;
  409. + mask &= constant_time_msb(j - a->top);
  410. + atop = constant_time_select_int(mask, j + 1, atop);
  411. + }
  412. +
  413. + mask = constant_time_eq_int(atop, 0);
  414. + a->top = atop;
  415. + a->neg = constant_time_select_int(mask, 0, a->neg);
  416. + a->flags &= ~BN_FLG_FIXED_TOP;
  417. +}
  418. +
  419. void bn_correct_top(BIGNUM *a)
  420. {
  421. BN_ULONG *ftl;
  422. diff --git a/crypto/bn/bn_local.h b/crypto/bn/bn_local.h
  423. index ee6342b60c..818e34348e 100644
  424. --- a/crypto/bn/bn_local.h
  425. +++ b/crypto/bn/bn_local.h
  426. @@ -515,10 +515,10 @@ unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  427. ret = (r); \
  428. BN_UMULT_LOHI(low,high,w,tmp); \
  429. ret += (c); \
  430. - (c) = (ret<(c))?1:0; \
  431. + (c) = (ret<(c)); \
  432. (c) += high; \
  433. ret += low; \
  434. - (c) += (ret<low)?1:0; \
  435. + (c) += (ret<low); \
  436. (r) = ret; \
  437. }
  438. @@ -527,7 +527,7 @@ unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  439. BN_UMULT_LOHI(low,high,w,ta); \
  440. ret = low + (c); \
  441. (c) = high; \
  442. - (c) += (ret<low)?1:0; \
  443. + (c) += (ret<low); \
  444. (r) = ret; \
  445. }
  446. @@ -543,10 +543,10 @@ unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  447. high= BN_UMULT_HIGH(w,tmp); \
  448. ret += (c); \
  449. low = (w) * tmp; \
  450. - (c) = (ret<(c))?1:0; \
  451. + (c) = (ret<(c)); \
  452. (c) += high; \
  453. ret += low; \
  454. - (c) += (ret<low)?1:0; \
  455. + (c) += (ret<low); \
  456. (r) = ret; \
  457. }
  458. @@ -556,7 +556,7 @@ unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  459. high= BN_UMULT_HIGH(w,ta); \
  460. ret = low + (c); \
  461. (c) = high; \
  462. - (c) += (ret<low)?1:0; \
  463. + (c) += (ret<low); \
  464. (r) = ret; \
  465. }
  466. @@ -589,10 +589,10 @@ unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  467. lt=(bl)*(lt); \
  468. m1=(bl)*(ht); \
  469. ht =(bh)*(ht); \
  470. - m=(m+m1)&BN_MASK2; if (m < m1) ht+=L2HBITS((BN_ULONG)1); \
  471. + m=(m+m1)&BN_MASK2; ht += L2HBITS((BN_ULONG)(m < m1)); \
  472. ht+=HBITS(m); \
  473. m1=L2HBITS(m); \
  474. - lt=(lt+m1)&BN_MASK2; if (lt < m1) ht++; \
  475. + lt=(lt+m1)&BN_MASK2; ht += (lt < m1); \
  476. (l)=lt; \
  477. (h)=ht; \
  478. }
  479. @@ -609,7 +609,7 @@ unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  480. h*=h; \
  481. h+=(m&BN_MASK2h1)>>(BN_BITS4-1); \
  482. m =(m&BN_MASK2l)<<(BN_BITS4+1); \
  483. - l=(l+m)&BN_MASK2; if (l < m) h++; \
  484. + l=(l+m)&BN_MASK2; h += (l < m); \
  485. (lo)=l; \
  486. (ho)=h; \
  487. }
  488. @@ -623,9 +623,9 @@ unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  489. mul64(l,h,(bl),(bh)); \
  490. \
  491. /* non-multiply part */ \
  492. - l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
  493. + l=(l+(c))&BN_MASK2; h += (l < (c)); \
  494. (c)=(r); \
  495. - l=(l+(c))&BN_MASK2; if (l < (c)) h++; \
  496. + l=(l+(c))&BN_MASK2; h += (l < (c)); \
  497. (c)=h&BN_MASK2; \
  498. (r)=l; \
  499. }
  500. @@ -639,7 +639,7 @@ unsigned __int64 _umul128(unsigned __int64 a, unsigned __int64 b,
  501. mul64(l,h,(bl),(bh)); \
  502. \
  503. /* non-multiply part */ \
  504. - l+=(c); if ((l&BN_MASK2) < (c)) h++; \
  505. + l+=(c); h += ((l&BN_MASK2) < (c)); \
  506. (c)=h&BN_MASK2; \
  507. (r)=l&BN_MASK2; \
  508. }
  509. @@ -669,7 +669,7 @@ BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
  510. int cl, int dl);
  511. int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
  512. const BN_ULONG *np, const BN_ULONG *n0, int num);
  513. -
  514. +void bn_correct_top_consttime(BIGNUM *a);
  515. BIGNUM *int_bn_mod_inverse(BIGNUM *in,
  516. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
  517. int *noinv);
  518. diff --git a/crypto/rsa/rsa_ossl.c b/crypto/rsa/rsa_ossl.c
  519. index 53cf2d03c9..cf5a10ab43 100644
  520. --- a/crypto/rsa/rsa_ossl.c
  521. +++ b/crypto/rsa/rsa_ossl.c
  522. @@ -226,6 +226,7 @@ static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
  523. * will only read the modulus from BN_BLINDING. In both cases it's safe
  524. * to access the blinding without a lock.
  525. */
  526. + BN_set_flags(f, BN_FLG_CONSTTIME);
  527. return BN_BLINDING_invert_ex(f, unblind, b, ctx);
  528. }
  529. @@ -412,6 +413,11 @@ static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
  530. goto err;
  531. }
  532. + if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  533. + if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
  534. + rsa->n, ctx))
  535. + goto err;
  536. +
  537. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  538. blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
  539. if (blinding == NULL) {
  540. @@ -449,13 +455,6 @@ static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
  541. goto err;
  542. }
  543. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  544. -
  545. - if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  546. - if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,
  547. - rsa->n, ctx)) {
  548. - BN_free(d);
  549. - goto err;
  550. - }
  551. if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
  552. rsa->_method_mod_n)) {
  553. BN_free(d);
  554. --
  555. 2.25.1