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 
109 #include <openssl/rsa.h>
110 
111 #include <string.h>
112 
113 #include <openssl/bn.h>
114 #include <openssl/mem.h>
115 #include <openssl/err.h>
116 #include <openssl/thread.h>
117 
118 #include "internal.h"
119 
120 
121 #define BN_BLINDING_COUNTER 32
122 
123 struct bn_blinding_st {
124   BIGNUM *A;
125   BIGNUM *Ai;
126   BIGNUM *e;
127   BIGNUM *mod; /* just a reference */
128   int counter;
129   unsigned long flags;
130   BN_MONT_CTX *m_ctx;
131   int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
132                     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
133 };
134 
BN_BLINDING_new(const BIGNUM * A,const BIGNUM * Ai,BIGNUM * mod)135 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod) {
136   BN_BLINDING *ret = NULL;
137 
138   ret = (BN_BLINDING*) OPENSSL_malloc(sizeof(BN_BLINDING));
139   if (ret == NULL) {
140     OPENSSL_PUT_ERROR(RSA, BN_BLINDING_new, ERR_R_MALLOC_FAILURE);
141     return NULL;
142   }
143   memset(ret, 0, sizeof(BN_BLINDING));
144   if (A != NULL) {
145     ret->A = BN_dup(A);
146     if (ret->A == NULL) {
147       goto err;
148     }
149   }
150   if (Ai != NULL) {
151     ret->Ai = BN_dup(Ai);
152     if (ret->Ai == NULL) {
153       goto err;
154     }
155   }
156 
157   /* save a copy of mod in the BN_BLINDING structure */
158   ret->mod = BN_dup(mod);
159   if (ret->mod == NULL) {
160     goto err;
161   }
162   if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) {
163     BN_set_flags(ret->mod, BN_FLG_CONSTTIME);
164   }
165 
166   /* Set the counter to the special value -1
167    * to indicate that this is never-used fresh blinding
168    * that does not need updating before first use. */
169   ret->counter = -1;
170   return ret;
171 
172 err:
173   BN_BLINDING_free(ret);
174   return NULL;
175 }
176 
BN_BLINDING_free(BN_BLINDING * r)177 void BN_BLINDING_free(BN_BLINDING *r) {
178   if (r == NULL) {
179     return;
180   }
181 
182   BN_free(r->A);
183   BN_free(r->Ai);
184   BN_free(r->e);
185   BN_free(r->mod);
186   OPENSSL_free(r);
187 }
188 
BN_BLINDING_update(BN_BLINDING * b,BN_CTX * ctx)189 int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx) {
190   int ret = 0;
191 
192   if (b->A == NULL || b->Ai == NULL) {
193     OPENSSL_PUT_ERROR(RSA, BN_BLINDING_update, RSA_R_BN_NOT_INITIALIZED);
194     goto err;
195   }
196 
197   if (b->counter == -1) {
198     b->counter = 0;
199   }
200 
201   if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL &&
202       !(b->flags & BN_BLINDING_NO_RECREATE)) {
203     /* re-create blinding parameters */
204     if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL)) {
205       goto err;
206     }
207   } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) {
208     if (!BN_mod_mul(b->A, b->A, b->A, b->mod, ctx)) {
209       goto err;
210     }
211     if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx)) {
212       goto err;
213     }
214   }
215 
216   ret = 1;
217 
218 err:
219   if (b->counter == BN_BLINDING_COUNTER) {
220     b->counter = 0;
221   }
222   return ret;
223 }
224 
BN_BLINDING_convert(BIGNUM * n,BN_BLINDING * b,BN_CTX * ctx)225 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) {
226   return BN_BLINDING_convert_ex(n, NULL, b, ctx);
227 }
228 
BN_BLINDING_convert_ex(BIGNUM * n,BIGNUM * r,BN_BLINDING * b,BN_CTX * ctx)229 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx) {
230   int ret = 1;
231 
232   if (b->A == NULL || b->Ai == NULL) {
233     OPENSSL_PUT_ERROR(RSA, BN_BLINDING_convert_ex, RSA_R_BN_NOT_INITIALIZED);
234     return 0;
235   }
236 
237   if (b->counter == -1) {
238     /* Fresh blinding, doesn't need updating. */
239     b->counter = 0;
240   } else if (!BN_BLINDING_update(b, ctx)) {
241     return 0;
242   }
243 
244   if (r != NULL) {
245     if (!BN_copy(r, b->Ai)) {
246       ret = 0;
247     }
248   }
249 
250   if (!BN_mod_mul(n, n, b->A, b->mod, ctx)) {
251     ret = 0;
252   }
253 
254   return ret;
255 }
256 
BN_BLINDING_invert(BIGNUM * n,BN_BLINDING * b,BN_CTX * ctx)257 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) {
258   return BN_BLINDING_invert_ex(n, NULL, b, ctx);
259 }
260 
BN_BLINDING_invert_ex(BIGNUM * n,const BIGNUM * r,BN_BLINDING * b,BN_CTX * ctx)261 int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
262                           BN_CTX *ctx) {
263   int ret;
264 
265   if (r != NULL) {
266     ret = BN_mod_mul(n, n, r, b->mod, ctx);
267   } else {
268     if (b->Ai == NULL) {
269       OPENSSL_PUT_ERROR(RSA, BN_BLINDING_invert_ex, RSA_R_BN_NOT_INITIALIZED);
270       return 0;
271     }
272     ret = BN_mod_mul(n, n, b->Ai, b->mod, ctx);
273   }
274 
275   return ret;
276 }
277 
BN_BLINDING_get_flags(const BN_BLINDING * b)278 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b) { return b->flags; }
279 
BN_BLINDING_set_flags(BN_BLINDING * b,unsigned long flags)280 void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags) {
281   b->flags = flags;
282 }
283 
BN_BLINDING_create_param(BN_BLINDING * b,const BIGNUM * e,BIGNUM * m,BN_CTX * ctx,int (* bn_mod_exp)(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx),BN_MONT_CTX * m_ctx)284 BN_BLINDING *BN_BLINDING_create_param(
285     BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
286     int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
287                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx),
288     BN_MONT_CTX *m_ctx) {
289   int retry_counter = 32;
290   BN_BLINDING *ret = NULL;
291 
292   if (b == NULL) {
293     ret = BN_BLINDING_new(NULL, NULL, m);
294   } else {
295     ret = b;
296   }
297 
298   if (ret == NULL) {
299     goto err;
300   }
301 
302   if (ret->A == NULL && (ret->A = BN_new()) == NULL) {
303     goto err;
304   }
305   if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL) {
306     goto err;
307   }
308 
309   if (e != NULL) {
310     BN_free(ret->e);
311     ret->e = BN_dup(e);
312   }
313   if (ret->e == NULL) {
314     goto err;
315   }
316 
317   if (bn_mod_exp != NULL) {
318     ret->bn_mod_exp = bn_mod_exp;
319   }
320   if (m_ctx != NULL) {
321     ret->m_ctx = m_ctx;
322   }
323 
324   do {
325     if (!BN_rand_range(ret->A, ret->mod)) {
326       goto err;
327     }
328     if (BN_mod_inverse(ret->Ai, ret->A, ret->mod, ctx) == NULL) {
329       /* this should almost never happen for good RSA keys */
330       uint32_t error = ERR_peek_last_error();
331       if (ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
332         if (retry_counter-- == 0) {
333           OPENSSL_PUT_ERROR(RSA, BN_BLINDING_create_param,
334                             RSA_R_TOO_MANY_ITERATIONS);
335           goto err;
336         }
337         ERR_clear_error();
338       } else {
339         goto err;
340       }
341     } else {
342       break;
343     }
344   } while (1);
345 
346   if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {
347     if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx)) {
348       goto err;
349     }
350   } else {
351     if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx)) {
352       goto err;
353     }
354   }
355 
356   return ret;
357 
358 err:
359   if (b == NULL) {
360     BN_BLINDING_free(ret);
361     ret = NULL;
362   }
363 
364   return ret;
365 }
366 
rsa_get_public_exp(const BIGNUM * d,const BIGNUM * p,const BIGNUM * q,BN_CTX * ctx)367 static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
368                                   const BIGNUM *q, BN_CTX *ctx) {
369   BIGNUM *ret = NULL, *r0, *r1, *r2;
370 
371   if (d == NULL || p == NULL || q == NULL) {
372     return NULL;
373   }
374 
375   BN_CTX_start(ctx);
376   r0 = BN_CTX_get(ctx);
377   r1 = BN_CTX_get(ctx);
378   r2 = BN_CTX_get(ctx);
379   if (r2 == NULL) {
380     goto err;
381   }
382 
383   if (!BN_sub(r1, p, BN_value_one())) {
384     goto err;
385   }
386   if (!BN_sub(r2, q, BN_value_one())) {
387     goto err;
388   }
389   if (!BN_mul(r0, r1, r2, ctx)) {
390     goto err;
391   }
392 
393   ret = BN_mod_inverse(NULL, d, r0, ctx);
394 
395 err:
396   BN_CTX_end(ctx);
397   return ret;
398 }
399 
rsa_setup_blinding(RSA * rsa,BN_CTX * in_ctx)400 BN_BLINDING *rsa_setup_blinding(RSA *rsa, BN_CTX *in_ctx) {
401   BIGNUM local_n;
402   BIGNUM *e, *n;
403   BN_CTX *ctx;
404   BN_BLINDING *ret = NULL;
405   BN_MONT_CTX *mont_ctx = NULL;
406 
407   if (in_ctx == NULL) {
408     ctx = BN_CTX_new();
409     if (ctx == NULL) {
410       return 0;
411     }
412   } else {
413     ctx = in_ctx;
414   }
415 
416   BN_CTX_start(ctx);
417   e = BN_CTX_get(ctx);
418   if (e == NULL) {
419     OPENSSL_PUT_ERROR(RSA, rsa_setup_blinding, ERR_R_MALLOC_FAILURE);
420     goto err;
421   }
422 
423   if (rsa->e == NULL) {
424     e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
425     if (e == NULL) {
426       OPENSSL_PUT_ERROR(RSA, rsa_setup_blinding, RSA_R_NO_PUBLIC_EXPONENT);
427       goto err;
428     }
429   } else {
430     e = rsa->e;
431   }
432 
433   n = &local_n;
434   BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME);
435 
436   if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
437     mont_ctx =
438         BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx);
439     if (mont_ctx == NULL) {
440       goto err;
441     }
442   }
443 
444   ret = BN_BLINDING_create_param(NULL, e, n, ctx, rsa->meth->bn_mod_exp,
445                                  mont_ctx);
446   if (ret == NULL) {
447     OPENSSL_PUT_ERROR(RSA, rsa_setup_blinding, ERR_R_BN_LIB);
448     goto err;
449   }
450 
451 err:
452   BN_CTX_end(ctx);
453   if (in_ctx == NULL) {
454     BN_CTX_free(ctx);
455   }
456   if (rsa->e == NULL) {
457     BN_free(e);
458   }
459 
460   return ret;
461 }
462