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