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