Skip to content

Commit bb88c9a

Browse files
author
jsing
committed
Remove BIGNUM consistency macros.
Compiling with BN_DEBUG (and if you want to take it further, BN_DEBUG_RAND) supposedly adds consistency checks to the BN code. These are rarely if ever used and introduce a bunch of clutter in the code. Furthermore, there are hacks in place to undo things that the debugging code does. Remove all of this mess and instead rely on always enabled checks, more readable code and proper regress coverage to ensure correct behaviour. "Good riddance." tb@
1 parent 77090b7 commit bb88c9a

23 files changed

+24
-328
lines changed

src/lib/libcrypto/bn/bn_add.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bn_add.c,v 1.14 2022/11/24 01:30:01 jsing Exp $ */
1+
/* $OpenBSD: bn_add.c,v 1.15 2022/11/26 13:56:33 jsing Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -67,8 +67,6 @@ BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
6767
{
6868
int ret, r_neg;
6969

70-
bn_check_top(a);
71-
bn_check_top(b);
7270

7371
if (a->neg == b->neg) {
7472
r_neg = a->neg;
@@ -90,7 +88,6 @@ BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
9088
}
9189

9290
r->neg = r_neg;
93-
bn_check_top(r);
9491
return ret;
9592
}
9693

@@ -101,8 +98,6 @@ BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
10198
const BN_ULONG *ap, *bp;
10299
BN_ULONG *rp, carry, t1, t2;
103100

104-
bn_check_top(a);
105-
bn_check_top(b);
106101

107102
if (a->top < b->top) {
108103
const BIGNUM *tmp;
@@ -139,7 +134,6 @@ BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
139134
r->top += carry;
140135

141136
r->neg = 0;
142-
bn_check_top(r);
143137
return 1;
144138
}
145139

@@ -150,8 +144,6 @@ BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
150144
const BN_ULONG *ap, *bp;
151145
BN_ULONG t1, t2, borrow, *rp;
152146

153-
bn_check_top(a);
154-
bn_check_top(b);
155147

156148
max = a->top;
157149
min = b->top;
@@ -195,8 +187,6 @@ BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
195187
{
196188
int ret, r_neg;
197189

198-
bn_check_top(a);
199-
bn_check_top(b);
200190

201191
if (a->neg != b->neg) {
202192
r_neg = a->neg;
@@ -218,6 +208,5 @@ BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
218208
}
219209

220210
r->neg = r_neg;
221-
bn_check_top(r);
222211
return ret;
223212
}

src/lib/libcrypto/bn/bn_blind.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bn_blind.c,v 1.17 2017/01/29 17:49:22 beck Exp $ */
1+
/* $OpenBSD: bn_blind.c,v 1.18 2022/11/26 13:56:33 jsing Exp $ */
22
/* ====================================================================
33
* Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
44
*
@@ -141,7 +141,6 @@ BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
141141
{
142142
BN_BLINDING *ret = NULL;
143143

144-
bn_check_top(mod);
145144

146145
if ((ret = calloc(1, sizeof(BN_BLINDING))) == NULL) {
147146
BNerror(ERR_R_MALLOC_FAILURE);
@@ -232,7 +231,6 @@ BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
232231
{
233232
int ret = 1;
234233

235-
bn_check_top(n);
236234

237235
if ((b->A == NULL) || (b->Ai == NULL)) {
238236
BNerror(BN_R_NOT_INITIALIZED);
@@ -267,7 +265,6 @@ BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
267265
{
268266
int ret;
269267

270-
bn_check_top(n);
271268

272269
if (r != NULL)
273270
ret = BN_mod_mul(n, n, r, b->mod, ctx);
@@ -279,7 +276,6 @@ BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
279276
ret = BN_mod_mul(n, n, b->Ai, b->mod, ctx);
280277
}
281278

282-
bn_check_top(n);
283279
return (ret);
284280
}
285281

src/lib/libcrypto/bn/bn_ctx.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bn_ctx.c,v 1.16 2019/08/20 10:59:09 schwarze Exp $ */
1+
/* $OpenBSD: bn_ctx.c,v 1.17 2022/11/26 13:56:33 jsing Exp $ */
22
/* Written by Ulf Moeller for the OpenSSL project. */
33
/* ====================================================================
44
* Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
@@ -471,7 +471,6 @@ BN_POOL_release(BN_POOL *p, unsigned int num)
471471

472472
p->used -= num;
473473
while (num--) {
474-
bn_check_top(p->current->vals + offset);
475474
if (!offset) {
476475
offset = BN_CTX_POOL_SIZE - 1;
477476
p->current = p->current->prev;

src/lib/libcrypto/bn/bn_div.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bn_div.c,v 1.26 2022/11/24 01:30:01 jsing Exp $ */
1+
/* $OpenBSD: bn_div.c,v 1.27 2022/11/26 13:56:33 jsing Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -127,23 +127,16 @@ BN_div_internal(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor
127127
int num_n, div_n;
128128
int no_branch = 0;
129129

130-
/* Invalid zero-padding would have particularly bad consequences
131-
* in the case of 'num', so don't just rely on bn_check_top() for this one
132-
* (bn_check_top() works only for BN_DEBUG builds) */
130+
/* Invalid zero-padding would have particularly bad consequences. */
133131
if (num->top > 0 && num->d[num->top - 1] == 0) {
134132
BNerror(BN_R_NOT_INITIALIZED);
135133
return 0;
136134
}
137135

138-
bn_check_top(num);
139136

140137
if (ct)
141138
no_branch = 1;
142139

143-
bn_check_top(dv);
144-
bn_check_top(rm);
145-
/* bn_check_top(num); */ /* 'num' has been checked already */
146-
bn_check_top(divisor);
147140

148141
if (BN_is_zero(divisor)) {
149142
BNerror(BN_R_DIV_BY_ZERO);
@@ -234,10 +227,6 @@ BN_div_internal(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor
234227

235228
if (!no_branch) {
236229
if (BN_ucmp(&wnum, sdiv) >= 0) {
237-
/* If BN_DEBUG_RAND is defined BN_ucmp changes (via
238-
* bn_pollute) the const bignum arguments =>
239-
* clean the values between top and max again */
240-
bn_clear_top2max(&wnum);
241230
bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
242231
*resp = 1;
243232
} else
@@ -365,15 +354,13 @@ BN_div_internal(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor
365354
BN_rshift(rm, snum, norm_shift);
366355
if (!BN_is_zero(rm))
367356
rm->neg = neg;
368-
bn_check_top(rm);
369357
}
370358
if (no_branch)
371359
bn_correct_top(res);
372360
BN_CTX_end(ctx);
373361
return (1);
374362

375363
err:
376-
bn_check_top(rm);
377364
BN_CTX_end(ctx);
378365
return (0);
379366
}

src/lib/libcrypto/bn/bn_exp.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bn_exp.c,v 1.33 2022/11/24 01:30:01 jsing Exp $ */
1+
/* $OpenBSD: bn_exp.c,v 1.34 2022/11/26 13:56:33 jsing Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -168,7 +168,6 @@ BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
168168
if (r != rr && rr != NULL)
169169
BN_copy(r, rr);
170170
BN_CTX_end(ctx);
171-
bn_check_top(r);
172171
return (ret);
173172
}
174173

@@ -178,9 +177,6 @@ BN_mod_exp_internal(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m
178177
{
179178
int ret;
180179

181-
bn_check_top(a);
182-
bn_check_top(p);
183-
bn_check_top(m);
184180

185181
/* For even modulus m = 2^k*m_odd, it might make sense to compute
186182
* a^p mod m_odd and a^p mod 2^k separately (with Montgomery
@@ -222,7 +218,6 @@ BN_mod_exp_internal(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m
222218
ret = BN_mod_exp_recp(r, a,p, m, ctx);
223219
}
224220

225-
bn_check_top(r);
226221
return (ret);
227222
}
228223

@@ -381,7 +376,6 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
381376
err:
382377
BN_CTX_end(ctx);
383378
BN_RECP_CTX_free(&recp);
384-
bn_check_top(r);
385379
return (ret);
386380
}
387381

@@ -401,9 +395,6 @@ BN_mod_exp_mont_internal(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIG
401395
return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
402396
}
403397

404-
bn_check_top(a);
405-
bn_check_top(p);
406-
bn_check_top(m);
407398

408399
if (!BN_is_odd(m)) {
409400
BNerror(BN_R_CALLED_WITH_EVEN_MODULUS);
@@ -533,7 +524,6 @@ BN_mod_exp_mont_internal(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIG
533524
if ((in_mont == NULL) && (mont != NULL))
534525
BN_MONT_CTX_free(mont);
535526
BN_CTX_end(ctx);
536-
bn_check_top(rr);
537527
return (ret);
538528
}
539529

@@ -658,9 +648,6 @@ BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
658648
unsigned char *powerbuf = NULL;
659649
BIGNUM tmp, am;
660650

661-
bn_check_top(a);
662-
bn_check_top(p);
663-
bn_check_top(m);
664651

665652
if (!BN_is_odd(m)) {
666653
BNerror(BN_R_CALLED_WITH_EVEN_MODULUS);
@@ -937,8 +924,6 @@ BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p, const BIGNUM *m,
937924
return -1;
938925
}
939926

940-
bn_check_top(p);
941-
bn_check_top(m);
942927

943928
if (!BN_is_odd(m)) {
944929
BNerror(BN_R_CALLED_WITH_EVEN_MODULUS);
@@ -1052,7 +1037,6 @@ BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p, const BIGNUM *m,
10521037
if ((in_mont == NULL) && (mont != NULL))
10531038
BN_MONT_CTX_free(mont);
10541039
BN_CTX_end(ctx);
1055-
bn_check_top(rr);
10561040
return (ret);
10571041
}
10581042

@@ -1172,6 +1156,5 @@ BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
11721156

11731157
err:
11741158
BN_CTX_end(ctx);
1175-
bn_check_top(r);
11761159
return (ret);
11771160
}

src/lib/libcrypto/bn/bn_exp2.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bn_exp2.c,v 1.13 2022/02/07 19:49:56 tb Exp $ */
1+
/* $OpenBSD: bn_exp2.c,v 1.14 2022/11/26 13:56:33 jsing Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -130,11 +130,6 @@ BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
130130
BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];
131131
BN_MONT_CTX *mont = NULL;
132132

133-
bn_check_top(a1);
134-
bn_check_top(p1);
135-
bn_check_top(a2);
136-
bn_check_top(p2);
137-
bn_check_top(m);
138133

139134
if (!BN_is_odd(m)) {
140135
BNerror(BN_R_CALLED_WITH_EVEN_MODULUS);
@@ -303,6 +298,5 @@ BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
303298
if ((in_mont == NULL) && (mont != NULL))
304299
BN_MONT_CTX_free(mont);
305300
BN_CTX_end(ctx);
306-
bn_check_top(rr);
307301
return (ret);
308302
}

src/lib/libcrypto/bn/bn_gcd.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: bn_gcd.c,v 1.16 2021/12/26 15:16:50 tb Exp $ */
1+
/* $OpenBSD: bn_gcd.c,v 1.17 2022/11/26 13:56:33 jsing Exp $ */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -123,8 +123,6 @@ BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
123123
BIGNUM *a, *b, *t;
124124
int ret = 0;
125125

126-
bn_check_top(in_a);
127-
bn_check_top(in_b);
128126

129127
BN_CTX_start(ctx);
130128
if ((a = BN_CTX_get(ctx)) == NULL)
@@ -154,7 +152,6 @@ BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
154152

155153
err:
156154
BN_CTX_end(ctx);
157-
bn_check_top(r);
158155
return (ret);
159156
}
160157

@@ -179,8 +176,6 @@ euclid(BIGNUM *a, BIGNUM *b)
179176
BIGNUM *t;
180177
int shifts = 0;
181178

182-
bn_check_top(a);
183-
bn_check_top(b);
184179

185180
/* 0 <= b <= a */
186181
while (!BN_is_zero(b)) {
@@ -236,7 +231,6 @@ euclid(BIGNUM *a, BIGNUM *b)
236231
if (!BN_lshift(a, a, shifts))
237232
goto err;
238233
}
239-
bn_check_top(a);
240234
return (a);
241235

242236
err:
@@ -259,8 +253,6 @@ BN_mod_inverse_internal(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ct
259253
if (ct)
260254
return BN_mod_inverse_no_branch(in, a, n, ctx);
261255

262-
bn_check_top(a);
263-
bn_check_top(n);
264256

265257
BN_CTX_start(ctx);
266258
if ((A = BN_CTX_get(ctx)) == NULL)
@@ -536,7 +528,6 @@ BN_mod_inverse_internal(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ct
536528
if ((ret == NULL) && (in == NULL))
537529
BN_free(R);
538530
BN_CTX_end(ctx);
539-
bn_check_top(ret);
540531
return (ret);
541532
}
542533

@@ -573,8 +564,6 @@ BN_mod_inverse_no_branch(BIGNUM *in, const BIGNUM *a, const BIGNUM *n,
573564
BIGNUM *ret = NULL;
574565
int sign;
575566

576-
bn_check_top(a);
577-
bn_check_top(n);
578567

579568
BN_init(&local_A);
580569
BN_init(&local_B);
@@ -725,7 +714,6 @@ BN_mod_inverse_no_branch(BIGNUM *in, const BIGNUM *a, const BIGNUM *n,
725714
if ((ret == NULL) && (in == NULL))
726715
BN_free(R);
727716
BN_CTX_end(ctx);
728-
bn_check_top(ret);
729717
return (ret);
730718
}
731719

@@ -750,8 +738,6 @@ BN_gcd_no_branch(BIGNUM *in, const BIGNUM *a, const BIGNUM *n,
750738
BN_init(&local_A);
751739
BN_init(&local_B);
752740

753-
bn_check_top(a);
754-
bn_check_top(n);
755741

756742
BN_CTX_start(ctx);
757743
if ((A = BN_CTX_get(ctx)) == NULL)
@@ -871,6 +857,5 @@ BN_gcd_no_branch(BIGNUM *in, const BIGNUM *a, const BIGNUM *n,
871857
if ((ret == NULL) && (in == NULL))
872858
BN_free(R);
873859
BN_CTX_end(ctx);
874-
bn_check_top(ret);
875860
return (ret);
876861
}

0 commit comments

Comments
 (0)