bn_blind.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 1998-2021 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_mod_mul_montgomery(n, n, r, b->m_ctx, ctx);
  158. } else {
  159. ret = BN_mod_mul(n, n, r, b->mod, ctx);
  160. }
  161. bn_check_top(n);
  162. return ret;
  163. }
  164. int BN_BLINDING_is_current_thread(BN_BLINDING *b)
  165. {
  166. return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid);
  167. }
  168. void BN_BLINDING_set_current_thread(BN_BLINDING *b)
  169. {
  170. b->tid = CRYPTO_THREAD_get_current_id();
  171. }
  172. int BN_BLINDING_lock(BN_BLINDING *b)
  173. {
  174. return CRYPTO_THREAD_write_lock(b->lock);
  175. }
  176. int BN_BLINDING_unlock(BN_BLINDING *b)
  177. {
  178. return CRYPTO_THREAD_unlock(b->lock);
  179. }
  180. unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b)
  181. {
  182. return b->flags;
  183. }
  184. void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags)
  185. {
  186. b->flags = flags;
  187. }
  188. BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
  189. const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
  190. int (*bn_mod_exp) (BIGNUM *r,
  191. const BIGNUM *a,
  192. const BIGNUM *p,
  193. const BIGNUM *m,
  194. BN_CTX *ctx,
  195. BN_MONT_CTX *m_ctx),
  196. BN_MONT_CTX *m_ctx)
  197. {
  198. int retry_counter = 32;
  199. BN_BLINDING *ret = NULL;
  200. if (b == NULL)
  201. ret = BN_BLINDING_new(NULL, NULL, m);
  202. else
  203. ret = b;
  204. if (ret == NULL)
  205. goto err;
  206. if (ret->A == NULL && (ret->A = BN_new()) == NULL)
  207. goto err;
  208. if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)
  209. goto err;
  210. if (e != NULL) {
  211. BN_free(ret->e);
  212. ret->e = BN_dup(e);
  213. }
  214. if (ret->e == NULL)
  215. goto err;
  216. if (bn_mod_exp != NULL)
  217. ret->bn_mod_exp = bn_mod_exp;
  218. if (m_ctx != NULL)
  219. ret->m_ctx = m_ctx;
  220. do {
  221. int rv;
  222. if (!BN_priv_rand_range_ex(ret->A, ret->mod, 0, ctx))
  223. goto err;
  224. if (int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv))
  225. break;
  226. /*
  227. * this should almost never happen for good RSA keys
  228. */
  229. if (!rv)
  230. goto err;
  231. if (retry_counter-- == 0) {
  232. ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
  233. goto err;
  234. }
  235. } while (1);
  236. if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {
  237. if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))
  238. goto err;
  239. } else {
  240. if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))
  241. goto err;
  242. }
  243. if (ret->m_ctx != NULL) {
  244. if (!bn_to_mont_fixed_top(ret->Ai, ret->Ai, ret->m_ctx, ctx)
  245. || !bn_to_mont_fixed_top(ret->A, ret->A, ret->m_ctx, ctx))
  246. goto err;
  247. }
  248. return ret;
  249. err:
  250. if (b == NULL) {
  251. BN_BLINDING_free(ret);
  252. ret = NULL;
  253. }
  254. return ret;
  255. }