Skip to content

Commit ee52c24

Browse files
author
tb
committed
Make BN_BLINDING respect some invariants
Pass e and mod into BN_BLINDING_new() for now and unconditionally allocate A and Ai. This way non-NULL blindings always have these four members set. This allows removing several unnecessary checks in the update, convert and parameter creation code paths. Fix exit BN_BLINDING_create_param() so as to signal errors to the caller if a non-NULL blinding was passed. This fixes a long standing bug. ok jsing
1 parent 83b348b commit ee52c24

File tree

1 file changed

+17
-48
lines changed

1 file changed

+17
-48
lines changed

src/lib/libcrypto/bn/bn_blind.c

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bn_blind.c,v 1.32 2023/08/02 09:25:36 tb Exp $ */
1+
/* $OpenBSD: bn_blind.c,v 1.33 2023/08/08 13:59:04 tb Exp $ */
22
/* ====================================================================
33
* Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
44
*
@@ -132,24 +132,20 @@ struct bn_blinding_st {
132132
};
133133

134134
static BN_BLINDING *
135-
BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
135+
BN_BLINDING_new(const BIGNUM *e, const BIGNUM *mod)
136136
{
137137
BN_BLINDING *ret = NULL;
138138

139139
if ((ret = calloc(1, sizeof(BN_BLINDING))) == NULL) {
140140
BNerror(ERR_R_MALLOC_FAILURE);
141-
return NULL;
142-
}
143-
if (A != NULL) {
144-
if ((ret->A = BN_dup(A)) == NULL)
145-
goto err;
146-
}
147-
if (Ai != NULL) {
148-
if ((ret->Ai = BN_dup(Ai)) == NULL)
149-
goto err;
141+
goto err;
150142
}
151-
152-
/* save a copy of mod in the BN_BLINDING structure */
143+
if ((ret->A = BN_new()) == NULL)
144+
goto err;
145+
if ((ret->Ai = BN_new()) == NULL)
146+
goto err;
147+
if ((ret->e = BN_dup(e)) == NULL)
148+
goto err;
153149
if ((ret->mod = BN_dup(mod)) == NULL)
154150
goto err;
155151
if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
@@ -160,11 +156,11 @@ BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
160156
* that does not need updating before first use. */
161157
ret->counter = -1;
162158
CRYPTO_THREADID_current(&ret->tid);
163-
return (ret);
159+
160+
return ret;
164161

165162
err:
166-
if (ret != NULL)
167-
BN_BLINDING_free(ret);
163+
BN_BLINDING_free(ret);
168164

169165
return NULL;
170166
}
@@ -187,15 +183,10 @@ BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
187183
{
188184
int ret = 0;
189185

190-
if (b->A == NULL || b->Ai == NULL) {
191-
BNerror(BN_R_NOT_INITIALIZED);
192-
goto err;
193-
}
194-
195186
if (b->counter == -1)
196187
b->counter = 0;
197188

198-
if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL) {
189+
if (++b->counter == BN_BLINDING_COUNTER) {
199190
/* re-create blinding parameters */
200191
if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
201192
goto err;
@@ -220,11 +211,6 @@ BN_BLINDING_convert(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
220211
{
221212
int ret = 1;
222213

223-
if (b->A == NULL || b->Ai == NULL) {
224-
BNerror(BN_R_NOT_INITIALIZED);
225-
return 0;
226-
}
227-
228214
if (b->counter == -1)
229215
/* Fresh blinding, doesn't need updating. */
230216
b->counter = 0;
@@ -274,26 +260,11 @@ BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, BN_CTX *ctx
274260
BN_BLINDING *ret = NULL;
275261
int retry_counter = 32;
276262

277-
if (b == NULL)
278-
ret = BN_BLINDING_new(NULL, NULL, m);
279-
else
280-
ret = b;
281-
263+
if ((ret = b) == NULL)
264+
ret = BN_BLINDING_new(e, m);
282265
if (ret == NULL)
283266
goto err;
284267

285-
if (ret->A == NULL && (ret->A = BN_new()) == NULL)
286-
goto err;
287-
if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)
288-
goto err;
289-
290-
if (e != NULL) {
291-
BN_free(ret->e);
292-
ret->e = BN_dup(e);
293-
}
294-
if (ret->e == NULL)
295-
goto err;
296-
297268
if (bn_mod_exp != NULL)
298269
ret->bn_mod_exp = bn_mod_exp;
299270
if (m_ctx != NULL)
@@ -329,10 +300,8 @@ BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, BN_CTX *ctx
329300
return ret;
330301

331302
err:
332-
if (b == NULL && ret != NULL) {
303+
if (ret != b)
333304
BN_BLINDING_free(ret);
334-
ret = NULL;
335-
}
336305

337-
return ret;
306+
return NULL;
338307
}

0 commit comments

Comments
 (0)