bn_blind.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright 1998-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/opensslconf.h>
  10. #include "internal/cryptlib.h"
  11. #include "bn_local.h"
  12. #define BN_BLINDING_COUNTER 32
  13. struct bn_blinding_st {
  14. BIGNUM *A;
  15. BIGNUM *Ai;
  16. BIGNUM *e;
  17. BIGNUM *mod; /* just a reference */
  18. CRYPTO_THREAD_ID tid;
  19. int counter;
  20. unsigned long flags;
  21. BN_MONT_CTX *m_ctx;
  22. int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  23. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  24. CRYPTO_RWLOCK *lock;
  25. };
  26. BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
  27. {
  28. BN_BLINDING *ret = NULL;
  29. bn_check_top(mod);
  30. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  31. return NULL;
  32. ret->lock = CRYPTO_THREAD_lock_new();
  33. if (ret->lock == NULL) {
  34. ERR_raise(ERR_LIB_BN, ERR_R_CRYPTO_LIB);
  35. OPENSSL_free(ret);
  36. return NULL;
  37. }
  38. BN_BLINDING_set_current_thread(ret);
  39. if (A != NULL) {
  40. if ((ret->A = BN_dup(A)) == NULL)
  41. goto err;
  42. }
  43. if (Ai != NULL) {
  44. if ((ret->Ai = BN_dup(Ai)) == NULL)
  45. goto err;
  46. }
  47. /* save a copy of mod in the BN_BLINDING structure */
  48. if ((ret->mod = BN_dup(mod)) == NULL)
  49. goto err;
  50. if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
  51. BN_set_flags(ret->mod, BN_FLG_CONSTTIME);
  52. /*
  53. * Set the counter to the special value -1 to indicate that this is
  54. * never-used fresh blinding that does not need updating before first
  55. * use.
  56. */
  57. ret->counter = -1;
  58. return ret;
  59. err:
  60. BN_BLINDING_free(ret);
  61. return NULL;
  62. }
  63. void BN_BLINDING_free(BN_BLINDING *r)
  64. {
  65. if (r == NULL)
  66. return;
  67. BN_free(r->A);
  68. BN_free(r->Ai);
  69. BN_free(r->e);
  70. BN_free(r->mod);
  71. CRYPTO_THREAD_lock_free(r->lock);
  72. OPENSSL_free(r);
  73. }
  74. int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
  75. {
  76. int ret = 0;
  77. if ((b->A == NULL) || (b->Ai == NULL)) {
  78. ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
  79. goto err;
  80. }
  81. if (b->counter == -1)
  82. b->counter = 0;
  83. if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL &&
  84. !(b->flags & BN_BLINDING_NO_RECREATE)) {
  85. /* re-create blinding parameters */
  86. if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
  87. goto err;
  88. } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) {
  89. if (b->m_ctx != NULL) {
  90. if (!bn_mul_mont_fixed_top(b->Ai, b->Ai, b->Ai, b->m_ctx, ctx)
  91. || !bn_mul_mont_fixed_top(b->A, b->A, b->A, b->m_ctx, ctx))
  92. goto err;
  93. } else {
  94. if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx)
  95. || !BN_mod_mul(b->A, b->A, b->A, b->mod, ctx))
  96. goto err;
  97. }
  98. }
  99. ret = 1;
  100. err:
  101. if (b->counter == BN_BLINDING_COUNTER)
  102. b->counter = 0;
  103. return ret;
  104. }
  105. int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
  106. {
  107. return BN_BLINDING_convert_ex(n, NULL, b, ctx);
  108. }
  109. int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
  110. {
  111. int ret = 1;
  112. bn_check_top(n);
  113. if ((b->A == NULL) || (b->Ai == NULL)) {
  114. ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
  115. return 0;
  116. }
  117. if (b->counter == -1)
  118. /* Fresh blinding, doesn't need updating. */
  119. b->counter = 0;
  120. else if (!BN_BLINDING_update(b, ctx))
  121. return 0;
  122. if (r != NULL && (BN_copy(r, b->Ai) == NULL))
  123. return 0;
  124. if (b->m_ctx != NULL)
  125. ret = BN_mod_mul_montgomery(n, n, b->A, b->m_ctx, ctx);
  126. else
  127. ret = BN_mod_mul(n, n, b->A, b->mod, ctx);
  128. return ret;
  129. }
  130. int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
  131. {
  132. return BN_BLINDING_invert_ex(n, NULL, b, ctx);
  133. }
  134. int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
  135. BN_CTX *ctx)
  136. {
  137. int ret;
  138. bn_check_top(n);
  139. if (r == NULL && (r = b->Ai) == NULL) {
  140. ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED);
  141. return 0;
  142. }
  143. if (b->m_ctx != NULL) {
  144. /* ensure that BN_mod_mul_montgomery takes pre-defined path */
  145. if (n->dmax >= r->top) {
  146. size_t i, rtop = r->top, ntop = n->top;
  147. BN_ULONG mask;
  148. for (i = 0; i < rtop; i++) {
  149. mask = (BN_ULONG)0 - ((i - ntop) >> (8 * sizeof(i) - 1));
  150. n->d[i] &= mask;
  151. }
  152. mask = (BN_ULONG)0 - ((rtop - ntop) >> (8 * sizeof(ntop) - 1));
  153. /* always true, if (rtop >= ntop) n->top = r->top; */
  154. n->top = (int)(rtop & ~mask) | (ntop & mask);
  155. n->flags |= (BN_FLG_FIXED_TOP & ~mask);
  156. }
  157. ret = bn_mul_mont_fixed_top(n, n, r, b->m_ctx, ctx);
  158. bn_correct_top_consttime(n);
  159. } else {
  160. ret = BN_mod_mul(n, n, r, b->mod, ctx);
  161. }
  162. bn_check_top(n);
  163. return ret;
  164. }
  165. int BN_BLINDING_is_current_thread(BN_BLINDING *b)
  166. {
  167. return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid);
  168. }
  169. void BN_BLINDING_set_current_thread(BN_BLINDING *b)
  170. {
  171. b->tid = CRYPTO_THREAD_get_current_id();
  172. }
  173. int BN_BLINDING_lock(BN_BLINDING *b)
  174. {
  175. return CRYPTO_THREAD_write_lock(b->lock);
  176. }
  177. int BN_BLINDING_unlock(BN_BLINDING *b)
  178. {
  179. return CRYPTO_THREAD_unlock(b->lock);
  180. }
  181. unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b)
  182. {
  183. return b->flags;
  184. }
  185. void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags)
  186. {
  187. b->flags = flags;
  188. }
  189. BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
  190. const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
  191. int (*bn_mod_exp) (BIGNUM *r,
  192. const BIGNUM *a,
  193. const BIGNUM *p,
  194. const BIGNUM *m,
  195. BN_CTX *ctx,
  196. BN_MONT_CTX *m_ctx),
  197. BN_MONT_CTX *m_ctx)
  198. {
  199. int retry_counter = 32;
  200. BN_BLINDING *ret = NULL;
  201. if (b == NULL)
  202. ret = BN_BLINDING_new(NULL, NULL, m);
  203. else
  204. ret = b;
  205. if (ret == NULL)
  206. goto err;
  207. if (ret->A == NULL && (ret->A = BN_new()) == NULL)
  208. goto err;
  209. if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)
  210. goto err;
  211. if (e != NULL) {
  212. BN_free(ret->e);
  213. ret->e = BN_dup(e);
  214. }
  215. if (ret->e == NULL)
  216. goto err;
  217. if (bn_mod_exp != NULL)
  218. ret->bn_mod_exp = bn_mod_exp;
  219. if (m_ctx != NULL)
  220. ret->m_ctx = m_ctx;
  221. do {
  222. int rv;
  223. if (!BN_priv_rand_range_ex(ret->A, ret->mod, 0, ctx))
  224. goto err;
  225. if (int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv))
  226. break;
  227. /*
  228. * this should almost never happen for good RSA keys
  229. */
  230. if (!rv)
  231. goto err;
  232. if (retry_counter-- == 0) {
  233. ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
  234. goto err;
  235. }
  236. } while (1);
  237. if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {
  238. if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))
  239. goto err;
  240. } else {
  241. if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))
  242. goto err;
  243. }
  244. if (ret->m_ctx != NULL) {
  245. if (!bn_to_mont_fixed_top(ret->Ai, ret->Ai, ret->m_ctx, ctx)
  246. || !bn_to_mont_fixed_top(ret->A, ret->A, ret->m_ctx, ctx))
  247. goto err;
  248. }
  249. return ret;
  250. err:
  251. if (b == NULL) {
  252. BN_BLINDING_free(ret);
  253. ret = NULL;
  254. }
  255. return ret;
  256. }