blinding.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* ====================================================================
  2. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com).
  52. *
  53. * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  54. * All rights reserved.
  55. *
  56. * This package is an SSL implementation written
  57. * by Eric Young (eay@cryptsoft.com).
  58. * The implementation was written so as to conform with Netscapes SSL.
  59. *
  60. * This library is free for commercial and non-commercial use as long as
  61. * the following conditions are aheared to. The following conditions
  62. * apply to all code found in this distribution, be it the RC4, RSA,
  63. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  64. * included with this distribution is covered by the same copyright terms
  65. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  66. *
  67. * Copyright remains Eric Young's, and as such any Copyright notices in
  68. * the code are not to be removed.
  69. * If this package is used in a product, Eric Young should be given attribution
  70. * as the author of the parts of the library used.
  71. * This can be in the form of a textual message at program startup or
  72. * in documentation (online or textual) provided with the package.
  73. *
  74. * Redistribution and use in source and binary forms, with or without
  75. * modification, are permitted provided that the following conditions
  76. * are met:
  77. * 1. Redistributions of source code must retain the copyright
  78. * notice, this list of conditions and the following disclaimer.
  79. * 2. Redistributions in binary form must reproduce the above copyright
  80. * notice, this list of conditions and the following disclaimer in the
  81. * documentation and/or other materials provided with the distribution.
  82. * 3. All advertising materials mentioning features or use of this software
  83. * must display the following acknowledgement:
  84. * "This product includes cryptographic software written by
  85. * Eric Young (eay@cryptsoft.com)"
  86. * The word 'cryptographic' can be left out if the rouines from the library
  87. * being used are not cryptographic related :-).
  88. * 4. If you include any Windows specific code (or a derivative thereof) from
  89. * the apps directory (application code) you must include an acknowledgement:
  90. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  93. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  95. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  96. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  97. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  98. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  100. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  101. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  102. * SUCH DAMAGE.
  103. *
  104. * The licence and distribution terms for any publically available version or
  105. * derivative of this code cannot be changed. i.e. this code cannot simply be
  106. * copied and put under another distribution licence
  107. * [including the GNU Public Licence.] */
  108. #include <openssl/rsa.h>
  109. #include <string.h>
  110. #include <openssl/bn.h>
  111. #include <openssl/mem.h>
  112. #include <openssl/err.h>
  113. #include "internal.h"
  114. #define BN_BLINDING_COUNTER 32
  115. struct bn_blinding_st {
  116. BIGNUM *A;
  117. BIGNUM *Ai;
  118. BIGNUM *e;
  119. BIGNUM *mod;
  120. int counter;
  121. /* mont is the Montgomery context used for this |BN_BLINDING|. It is not
  122. * owned and must outlive this structure. */
  123. const BN_MONT_CTX *mont;
  124. int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  125. const BIGNUM *m, BN_CTX *ctx, const BN_MONT_CTX *mont);
  126. };
  127. BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod) {
  128. BN_BLINDING *ret = NULL;
  129. ret = (BN_BLINDING*) OPENSSL_malloc(sizeof(BN_BLINDING));
  130. if (ret == NULL) {
  131. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  132. return NULL;
  133. }
  134. memset(ret, 0, sizeof(BN_BLINDING));
  135. if (A != NULL) {
  136. ret->A = BN_dup(A);
  137. if (ret->A == NULL) {
  138. goto err;
  139. }
  140. }
  141. if (Ai != NULL) {
  142. ret->Ai = BN_dup(Ai);
  143. if (ret->Ai == NULL) {
  144. goto err;
  145. }
  146. }
  147. /* save a copy of mod in the BN_BLINDING structure */
  148. ret->mod = BN_dup(mod);
  149. if (ret->mod == NULL) {
  150. goto err;
  151. }
  152. if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) {
  153. BN_set_flags(ret->mod, BN_FLG_CONSTTIME);
  154. }
  155. /* Set the counter to the special value -1
  156. * to indicate that this is never-used fresh blinding
  157. * that does not need updating before first use. */
  158. ret->counter = -1;
  159. return ret;
  160. err:
  161. BN_BLINDING_free(ret);
  162. return NULL;
  163. }
  164. void BN_BLINDING_free(BN_BLINDING *r) {
  165. if (r == NULL) {
  166. return;
  167. }
  168. BN_free(r->A);
  169. BN_free(r->Ai);
  170. BN_free(r->e);
  171. BN_free(r->mod);
  172. OPENSSL_free(r);
  173. }
  174. int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx) {
  175. int ret = 0;
  176. if (b->A == NULL || b->Ai == NULL) {
  177. OPENSSL_PUT_ERROR(RSA, RSA_R_BN_NOT_INITIALIZED);
  178. goto err;
  179. }
  180. if (b->counter == -1) {
  181. b->counter = 0;
  182. }
  183. if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL) {
  184. /* re-create blinding parameters */
  185. if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL)) {
  186. goto err;
  187. }
  188. } else {
  189. if (!BN_mod_mul(b->A, b->A, b->A, b->mod, ctx)) {
  190. goto err;
  191. }
  192. if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx)) {
  193. goto err;
  194. }
  195. }
  196. ret = 1;
  197. err:
  198. if (b->counter == BN_BLINDING_COUNTER) {
  199. b->counter = 0;
  200. }
  201. return ret;
  202. }
  203. int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) {
  204. int ret = 1;
  205. if (b->A == NULL || b->Ai == NULL) {
  206. OPENSSL_PUT_ERROR(RSA, RSA_R_BN_NOT_INITIALIZED);
  207. return 0;
  208. }
  209. if (b->counter == -1) {
  210. /* Fresh blinding, doesn't need updating. */
  211. b->counter = 0;
  212. } else if (!BN_BLINDING_update(b, ctx)) {
  213. return 0;
  214. }
  215. if (!BN_mod_mul(n, n, b->A, b->mod, ctx)) {
  216. ret = 0;
  217. }
  218. return ret;
  219. }
  220. int BN_BLINDING_invert(BIGNUM *n, const BN_BLINDING *b, BN_CTX *ctx) {
  221. if (b->Ai == NULL) {
  222. OPENSSL_PUT_ERROR(RSA, RSA_R_BN_NOT_INITIALIZED);
  223. return 0;
  224. }
  225. return BN_mod_mul(n, n, b->Ai, b->mod, ctx);
  226. }
  227. BN_BLINDING *BN_BLINDING_create_param(
  228. BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
  229. int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  230. const BIGNUM *m, BN_CTX *ctx, const BN_MONT_CTX *mont),
  231. const BN_MONT_CTX *mont) {
  232. int retry_counter = 32;
  233. BN_BLINDING *ret = NULL;
  234. if (b == NULL) {
  235. ret = BN_BLINDING_new(NULL, NULL, m);
  236. } else {
  237. ret = b;
  238. }
  239. if (ret == NULL) {
  240. goto err;
  241. }
  242. if (ret->A == NULL && (ret->A = BN_new()) == NULL) {
  243. goto err;
  244. }
  245. if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL) {
  246. goto err;
  247. }
  248. if (e != NULL) {
  249. BN_free(ret->e);
  250. ret->e = BN_dup(e);
  251. }
  252. if (ret->e == NULL) {
  253. goto err;
  254. }
  255. if (bn_mod_exp != NULL) {
  256. ret->bn_mod_exp = bn_mod_exp;
  257. }
  258. if (mont != NULL) {
  259. ret->mont = mont;
  260. }
  261. do {
  262. if (!BN_rand_range(ret->A, ret->mod)) {
  263. goto err;
  264. }
  265. int no_inverse;
  266. if (BN_mod_inverse_ex(ret->Ai, &no_inverse, ret->A, ret->mod, ctx) == NULL) {
  267. /* this should almost never happen for good RSA keys */
  268. if (no_inverse) {
  269. if (retry_counter-- == 0) {
  270. OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_MANY_ITERATIONS);
  271. goto err;
  272. }
  273. ERR_clear_error();
  274. } else {
  275. goto err;
  276. }
  277. } else {
  278. break;
  279. }
  280. } while (1);
  281. if (ret->bn_mod_exp != NULL && ret->mont != NULL) {
  282. if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->mont)) {
  283. goto err;
  284. }
  285. } else {
  286. if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx)) {
  287. goto err;
  288. }
  289. }
  290. return ret;
  291. err:
  292. if (b == NULL) {
  293. BN_BLINDING_free(ret);
  294. ret = NULL;
  295. }
  296. return ret;
  297. }
  298. static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
  299. const BIGNUM *q, BN_CTX *ctx) {
  300. BIGNUM *ret = NULL, *r0, *r1, *r2;
  301. if (d == NULL || p == NULL || q == NULL) {
  302. return NULL;
  303. }
  304. BN_CTX_start(ctx);
  305. r0 = BN_CTX_get(ctx);
  306. r1 = BN_CTX_get(ctx);
  307. r2 = BN_CTX_get(ctx);
  308. if (r2 == NULL) {
  309. goto err;
  310. }
  311. if (!BN_sub(r1, p, BN_value_one())) {
  312. goto err;
  313. }
  314. if (!BN_sub(r2, q, BN_value_one())) {
  315. goto err;
  316. }
  317. if (!BN_mul(r0, r1, r2, ctx)) {
  318. goto err;
  319. }
  320. ret = BN_mod_inverse(NULL, d, r0, ctx);
  321. err:
  322. BN_CTX_end(ctx);
  323. return ret;
  324. }
  325. BN_BLINDING *rsa_setup_blinding(RSA *rsa, BN_CTX *in_ctx) {
  326. BIGNUM local_n;
  327. BIGNUM *e, *n;
  328. BN_CTX *ctx;
  329. BN_BLINDING *ret = NULL;
  330. BN_MONT_CTX *mont_ctx = NULL;
  331. if (in_ctx == NULL) {
  332. ctx = BN_CTX_new();
  333. if (ctx == NULL) {
  334. return 0;
  335. }
  336. } else {
  337. ctx = in_ctx;
  338. }
  339. if (rsa->e == NULL) {
  340. e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
  341. if (e == NULL) {
  342. OPENSSL_PUT_ERROR(RSA, RSA_R_NO_PUBLIC_EXPONENT);
  343. goto err;
  344. }
  345. } else {
  346. e = rsa->e;
  347. }
  348. n = &local_n;
  349. BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
  350. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  351. mont_ctx = BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx);
  352. if (mont_ctx == NULL) {
  353. goto err;
  354. }
  355. }
  356. ret = BN_BLINDING_create_param(NULL, e, n, ctx, rsa->meth->bn_mod_exp,
  357. mont_ctx);
  358. if (ret == NULL) {
  359. OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
  360. goto err;
  361. }
  362. err:
  363. if (in_ctx == NULL) {
  364. BN_CTX_free(ctx);
  365. }
  366. if (rsa->e == NULL) {
  367. BN_free(e);
  368. }
  369. return ret;
  370. }