diff options
author | tb <> | 2023-08-09 08:39:46 +0000 |
---|---|---|
committer | tb <> | 2023-08-09 08:39:46 +0000 |
commit | b747bfb735f278053b97b609afd7fa3b35ee1582 (patch) | |
tree | f837488492c0c44bb7f42759688817ef69eb6e76 | |
parent | db85683b2c278e55f061597e4c7c29aa8c3eda49 (diff) | |
download | openbsd-b747bfb735f278053b97b609afd7fa3b35ee1582.tar.gz openbsd-b747bfb735f278053b97b609afd7fa3b35ee1582.tar.bz2 openbsd-b747bfb735f278053b97b609afd7fa3b35ee1582.zip |
Set up the blinding factors on first use
Only call BN_BLINDING_setup() from BN_BLINDING_update(). This allows
another simplification of the counter logic.
ok jsing
-rw-r--r-- | src/lib/libcrypto/bn/bn_blind.c | 41 |
1 files changed, 14 insertions, 27 deletions
diff --git a/src/lib/libcrypto/bn/bn_blind.c b/src/lib/libcrypto/bn/bn_blind.c index cca211fb4f..996b1d6965 100644 --- a/src/lib/libcrypto/bn/bn_blind.c +++ b/src/lib/libcrypto/bn/bn_blind.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_blind.c,v 1.40 2023/08/09 08:35:59 tb Exp $ */ | 1 | /* $OpenBSD: bn_blind.c,v 1.41 2023/08/09 08:39:46 tb Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -151,10 +151,8 @@ BN_BLINDING_new(const BIGNUM *e, const BIGNUM *mod) | |||
151 | if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) | 151 | if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) |
152 | BN_set_flags(ret->mod, BN_FLG_CONSTTIME); | 152 | BN_set_flags(ret->mod, BN_FLG_CONSTTIME); |
153 | 153 | ||
154 | /* Set the counter to the special value -1 | 154 | /* Update on first use. */ |
155 | * to indicate that this is never-used fresh blinding | 155 | ret->counter = BN_BLINDING_COUNTER - 1; |
156 | * that does not need updating before first use. */ | ||
157 | ret->counter = -1; | ||
158 | CRYPTO_THREADID_current(&ret->tid); | 156 | CRYPTO_THREADID_current(&ret->tid); |
159 | 157 | ||
160 | return ret; | 158 | return ret; |
@@ -202,12 +200,10 @@ BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx) | |||
202 | { | 200 | { |
203 | int ret = 0; | 201 | int ret = 0; |
204 | 202 | ||
205 | if (b->counter == -1) | 203 | if (++b->counter >= BN_BLINDING_COUNTER) { |
206 | b->counter = 0; | ||
207 | |||
208 | if (++b->counter == BN_BLINDING_COUNTER) { | ||
209 | if (!BN_BLINDING_setup(b, ctx)) | 204 | if (!BN_BLINDING_setup(b, ctx)) |
210 | goto err; | 205 | goto err; |
206 | b->counter = 0; | ||
211 | } else { | 207 | } else { |
212 | if (!BN_mod_sqr(b->A, b->A, b->mod, ctx)) | 208 | if (!BN_mod_sqr(b->A, b->A, b->mod, ctx)) |
213 | goto err; | 209 | goto err; |
@@ -218,31 +214,25 @@ BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx) | |||
218 | ret = 1; | 214 | ret = 1; |
219 | 215 | ||
220 | err: | 216 | err: |
221 | if (b->counter == BN_BLINDING_COUNTER) | ||
222 | b->counter = 0; | ||
223 | |||
224 | return ret; | 217 | return ret; |
225 | } | 218 | } |
226 | 219 | ||
227 | int | 220 | int |
228 | BN_BLINDING_convert(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx) | 221 | BN_BLINDING_convert(BIGNUM *n, BIGNUM *inv, BN_BLINDING *b, BN_CTX *ctx) |
229 | { | 222 | { |
230 | int ret = 1; | 223 | int ret = 0; |
231 | 224 | ||
232 | if (b->counter == -1) | 225 | if (!BN_BLINDING_update(b, ctx)) |
233 | /* Fresh blinding, doesn't need updating. */ | 226 | goto err; |
234 | b->counter = 0; | ||
235 | else if (!BN_BLINDING_update(b, ctx)) | ||
236 | return 0; | ||
237 | 227 | ||
238 | if (r != NULL) { | 228 | if (inv != NULL) { |
239 | if (!bn_copy(r, b->Ai)) | 229 | if (!bn_copy(inv, b->Ai)) |
240 | ret = 0; | 230 | goto err; |
241 | } | 231 | } |
242 | 232 | ||
243 | if (!BN_mod_mul(n, n, b->A, b->mod, ctx)) | 233 | ret = BN_mod_mul(n, n, b->A, b->mod, ctx); |
244 | ret = 0; | ||
245 | 234 | ||
235 | err: | ||
246 | return ret; | 236 | return ret; |
247 | } | 237 | } |
248 | 238 | ||
@@ -276,9 +266,6 @@ BN_BLINDING_create_param(const BIGNUM *e, BIGNUM *m, BN_CTX *ctx, | |||
276 | if (m_ctx != NULL) | 266 | if (m_ctx != NULL) |
277 | ret->m_ctx = m_ctx; | 267 | ret->m_ctx = m_ctx; |
278 | 268 | ||
279 | if (!BN_BLINDING_setup(ret, ctx)) | ||
280 | goto err; | ||
281 | |||
282 | return ret; | 269 | return ret; |
283 | 270 | ||
284 | err: | 271 | err: |