2 * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
11 #include "internal/cryptlib.h"
14 #define BN_BLINDING_COUNTER 32
16 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
18 BN_BLINDING *ret = NULL;
22 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
23 BNerr(BN_F_BN_BLINDING_NEW, ERR_R_MALLOC_FAILURE);
27 ret->lock = CRYPTO_THREAD_lock_new();
28 if (ret->lock == NULL) {
29 BNerr(BN_F_BN_BLINDING_NEW, ERR_R_MALLOC_FAILURE);
34 BN_BLINDING_set_current_thread(ret);
37 if ((ret->A = BN_dup(A)) == NULL)
42 if ((ret->Ai = BN_dup(Ai)) == NULL)
46 /* save a copy of mod in the BN_BLINDING structure */
47 if ((ret->mod = BN_dup(mod)) == NULL)
50 if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
51 BN_set_flags(ret->mod, BN_FLG_CONSTTIME);
54 * Set the counter to the special value -1 to indicate that this is
55 * never-used fresh blinding that does not need updating before first
63 BN_BLINDING_free(ret);
67 void BN_BLINDING_free(BN_BLINDING *r)
75 CRYPTO_THREAD_lock_free(r->lock);
79 int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
83 if ((b->A == NULL) || (b->Ai == NULL)) {
84 BNerr(BN_F_BN_BLINDING_UPDATE, BN_R_NOT_INITIALIZED);
91 if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL &&
92 !(b->flags & BN_BLINDING_NO_RECREATE)) {
93 /* re-create blinding parameters */
94 if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
96 } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) {
97 if (b->m_ctx != NULL) {
98 if (!bn_mul_mont_fixed_top(b->Ai, b->Ai, b->Ai, b->m_ctx, ctx)
99 || !bn_mul_mont_fixed_top(b->A, b->A, b->A, b->m_ctx, ctx))
102 if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx)
103 || !BN_mod_mul(b->A, b->A, b->A, b->mod, ctx))
110 if (b->counter == BN_BLINDING_COUNTER)
115 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
117 return BN_BLINDING_convert_ex(n, NULL, b, ctx);
120 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
126 if ((b->A == NULL) || (b->Ai == NULL)) {
127 BNerr(BN_F_BN_BLINDING_CONVERT_EX, BN_R_NOT_INITIALIZED);
131 if (b->counter == -1)
132 /* Fresh blinding, doesn't need updating. */
134 else if (!BN_BLINDING_update(b, ctx))
137 if (r != NULL && (BN_copy(r, b->Ai) == NULL))
140 if (b->m_ctx != NULL)
141 ret = BN_mod_mul_montgomery(n, n, b->A, b->m_ctx, ctx);
143 ret = BN_mod_mul(n, n, b->A, b->mod, ctx);
148 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
150 return BN_BLINDING_invert_ex(n, NULL, b, ctx);
153 int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
160 if (r == NULL && (r = b->Ai) == NULL) {
161 BNerr(BN_F_BN_BLINDING_INVERT_EX, BN_R_NOT_INITIALIZED);
165 if (b->m_ctx != NULL) {
166 /* ensure that BN_mod_mul_montgomery takes pre-defined path */
167 if (n->dmax >= r->top) {
168 size_t i, rtop = r->top, ntop = n->top;
171 for (i = 0; i < rtop; i++) {
172 mask = (BN_ULONG)0 - ((i - ntop) >> (8 * sizeof(i) - 1));
175 mask = (BN_ULONG)0 - ((rtop - ntop) >> (8 * sizeof(ntop) - 1));
176 /* always true, if (rtop >= ntop) n->top = r->top; */
177 n->top = (int)(rtop & ~mask) | (ntop & mask);
178 n->flags |= (BN_FLG_FIXED_TOP & ~mask);
180 ret = BN_mod_mul_montgomery(n, n, r, b->m_ctx, ctx);
182 ret = BN_mod_mul(n, n, r, b->mod, ctx);
189 int BN_BLINDING_is_current_thread(BN_BLINDING *b)
191 return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid);
194 void BN_BLINDING_set_current_thread(BN_BLINDING *b)
196 b->tid = CRYPTO_THREAD_get_current_id();
199 int BN_BLINDING_lock(BN_BLINDING *b)
201 return CRYPTO_THREAD_write_lock(b->lock);
204 int BN_BLINDING_unlock(BN_BLINDING *b)
206 return CRYPTO_THREAD_unlock(b->lock);
209 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b)
214 void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags)
219 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
220 const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
221 int (*bn_mod_exp) (BIGNUM *r,
229 int retry_counter = 32;
230 BN_BLINDING *ret = NULL;
233 ret = BN_BLINDING_new(NULL, NULL, m);
240 if (ret->A == NULL && (ret->A = BN_new()) == NULL)
242 if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)
252 if (bn_mod_exp != NULL)
253 ret->bn_mod_exp = bn_mod_exp;
259 if (!BN_priv_rand_range(ret->A, ret->mod))
261 if (int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv))
265 * this should almost never happen for good RSA keys
270 if (retry_counter-- == 0) {
271 BNerr(BN_F_BN_BLINDING_CREATE_PARAM, BN_R_TOO_MANY_ITERATIONS);
276 if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) {
277 if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx))
280 if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx))
284 if (ret->m_ctx != NULL) {
285 if (!bn_to_mont_fixed_top(ret->Ai, ret->Ai, ret->m_ctx, ctx)
286 || !bn_to_mont_fixed_top(ret->A, ret->A, ret->m_ctx, ctx))
293 BN_BLINDING_free(ret);