summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/bn_add.c13
-rw-r--r--src/lib/libcrypto/bn/bn_blind.c6
-rw-r--r--src/lib/libcrypto/bn/bn_ctx.c3
-rw-r--r--src/lib/libcrypto/bn/bn_div.c17
-rw-r--r--src/lib/libcrypto/bn/bn_exp.c19
-rw-r--r--src/lib/libcrypto/bn/bn_exp2.c8
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c17
-rw-r--r--src/lib/libcrypto/bn/bn_gf2m.c55
-rw-r--r--src/lib/libcrypto/bn/bn_kron.c4
-rw-r--r--src/lib/libcrypto/bn/bn_lcl.h97
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c33
-rw-r--r--src/lib/libcrypto/bn/bn_mod.c10
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c6
-rw-r--r--src/lib/libcrypto/bn/bn_mpi.c3
-rw-r--r--src/lib/libcrypto/bn/bn_mul.c6
-rw-r--r--src/lib/libcrypto/bn/bn_prime.c6
-rw-r--r--src/lib/libcrypto/bn/bn_print.c4
-rw-r--r--src/lib/libcrypto/bn/bn_rand.c4
-rw-r--r--src/lib/libcrypto/bn/bn_recp.c6
-rw-r--r--src/lib/libcrypto/bn/bn_shift.c14
-rw-r--r--src/lib/libcrypto/bn/bn_sqr.c5
-rw-r--r--src/lib/libcrypto/bn/bn_sqrt.c5
-rw-r--r--src/lib/libcrypto/bn/bn_word.c11
23 files changed, 24 insertions, 328 deletions
diff --git a/src/lib/libcrypto/bn/bn_add.c b/src/lib/libcrypto/bn/bn_add.c
index 3a8c0e847a..a81dd0ce75 100644
--- a/src/lib/libcrypto/bn/bn_add.c
+++ b/src/lib/libcrypto/bn/bn_add.c
@@ -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 $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -67,8 +67,6 @@ BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
67{ 67{
68 int ret, r_neg; 68 int ret, r_neg;
69 69
70 bn_check_top(a);
71 bn_check_top(b);
72 70
73 if (a->neg == b->neg) { 71 if (a->neg == b->neg) {
74 r_neg = a->neg; 72 r_neg = a->neg;
@@ -90,7 +88,6 @@ BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
90 } 88 }
91 89
92 r->neg = r_neg; 90 r->neg = r_neg;
93 bn_check_top(r);
94 return ret; 91 return ret;
95} 92}
96 93
@@ -101,8 +98,6 @@ BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
101 const BN_ULONG *ap, *bp; 98 const BN_ULONG *ap, *bp;
102 BN_ULONG *rp, carry, t1, t2; 99 BN_ULONG *rp, carry, t1, t2;
103 100
104 bn_check_top(a);
105 bn_check_top(b);
106 101
107 if (a->top < b->top) { 102 if (a->top < b->top) {
108 const BIGNUM *tmp; 103 const BIGNUM *tmp;
@@ -139,7 +134,6 @@ BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
139 r->top += carry; 134 r->top += carry;
140 135
141 r->neg = 0; 136 r->neg = 0;
142 bn_check_top(r);
143 return 1; 137 return 1;
144} 138}
145 139
@@ -150,8 +144,6 @@ BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
150 const BN_ULONG *ap, *bp; 144 const BN_ULONG *ap, *bp;
151 BN_ULONG t1, t2, borrow, *rp; 145 BN_ULONG t1, t2, borrow, *rp;
152 146
153 bn_check_top(a);
154 bn_check_top(b);
155 147
156 max = a->top; 148 max = a->top;
157 min = b->top; 149 min = b->top;
@@ -195,8 +187,6 @@ BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
195{ 187{
196 int ret, r_neg; 188 int ret, r_neg;
197 189
198 bn_check_top(a);
199 bn_check_top(b);
200 190
201 if (a->neg != b->neg) { 191 if (a->neg != b->neg) {
202 r_neg = a->neg; 192 r_neg = a->neg;
@@ -218,6 +208,5 @@ BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
218 } 208 }
219 209
220 r->neg = r_neg; 210 r->neg = r_neg;
221 bn_check_top(r);
222 return ret; 211 return ret;
223} 212}
diff --git a/src/lib/libcrypto/bn/bn_blind.c b/src/lib/libcrypto/bn/bn_blind.c
index ecd6718279..412338ec02 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.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 $ */
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 *
@@ -141,7 +141,6 @@ BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
141{ 141{
142 BN_BLINDING *ret = NULL; 142 BN_BLINDING *ret = NULL;
143 143
144 bn_check_top(mod);
145 144
146 if ((ret = calloc(1, sizeof(BN_BLINDING))) == NULL) { 145 if ((ret = calloc(1, sizeof(BN_BLINDING))) == NULL) {
147 BNerror(ERR_R_MALLOC_FAILURE); 146 BNerror(ERR_R_MALLOC_FAILURE);
@@ -232,7 +231,6 @@ BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
232{ 231{
233 int ret = 1; 232 int ret = 1;
234 233
235 bn_check_top(n);
236 234
237 if ((b->A == NULL) || (b->Ai == NULL)) { 235 if ((b->A == NULL) || (b->Ai == NULL)) {
238 BNerror(BN_R_NOT_INITIALIZED); 236 BNerror(BN_R_NOT_INITIALIZED);
@@ -267,7 +265,6 @@ BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
267{ 265{
268 int ret; 266 int ret;
269 267
270 bn_check_top(n);
271 268
272 if (r != NULL) 269 if (r != NULL)
273 ret = BN_mod_mul(n, n, r, b->mod, ctx); 270 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)
279 ret = BN_mod_mul(n, n, b->Ai, b->mod, ctx); 276 ret = BN_mod_mul(n, n, b->Ai, b->mod, ctx);
280 } 277 }
281 278
282 bn_check_top(n);
283 return (ret); 279 return (ret);
284} 280}
285 281
diff --git a/src/lib/libcrypto/bn/bn_ctx.c b/src/lib/libcrypto/bn/bn_ctx.c
index 0d64ccab93..8ac1685a00 100644
--- a/src/lib/libcrypto/bn/bn_ctx.c
+++ b/src/lib/libcrypto/bn/bn_ctx.c
@@ -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 $ */
2/* Written by Ulf Moeller for the OpenSSL project. */ 2/* Written by Ulf Moeller for the OpenSSL project. */
3/* ==================================================================== 3/* ====================================================================
4 * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved. 4 * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
@@ -471,7 +471,6 @@ BN_POOL_release(BN_POOL *p, unsigned int num)
471 471
472 p->used -= num; 472 p->used -= num;
473 while (num--) { 473 while (num--) {
474 bn_check_top(p->current->vals + offset);
475 if (!offset) { 474 if (!offset) {
476 offset = BN_CTX_POOL_SIZE - 1; 475 offset = BN_CTX_POOL_SIZE - 1;
477 p->current = p->current->prev; 476 p->current = p->current->prev;
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index f641386eb8..288ec92ef4 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -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 $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -127,23 +127,16 @@ BN_div_internal(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor
127 int num_n, div_n; 127 int num_n, div_n;
128 int no_branch = 0; 128 int no_branch = 0;
129 129
130 /* Invalid zero-padding would have particularly bad consequences 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) */
133 if (num->top > 0 && num->d[num->top - 1] == 0) { 131 if (num->top > 0 && num->d[num->top - 1] == 0) {
134 BNerror(BN_R_NOT_INITIALIZED); 132 BNerror(BN_R_NOT_INITIALIZED);
135 return 0; 133 return 0;
136 } 134 }
137 135
138 bn_check_top(num);
139 136
140 if (ct) 137 if (ct)
141 no_branch = 1; 138 no_branch = 1;
142 139
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);
147 140
148 if (BN_is_zero(divisor)) { 141 if (BN_is_zero(divisor)) {
149 BNerror(BN_R_DIV_BY_ZERO); 142 BNerror(BN_R_DIV_BY_ZERO);
@@ -234,10 +227,6 @@ BN_div_internal(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor
234 227
235 if (!no_branch) { 228 if (!no_branch) {
236 if (BN_ucmp(&wnum, sdiv) >= 0) { 229 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);
241 bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n); 230 bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
242 *resp = 1; 231 *resp = 1;
243 } else 232 } else
@@ -365,7 +354,6 @@ BN_div_internal(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor
365 BN_rshift(rm, snum, norm_shift); 354 BN_rshift(rm, snum, norm_shift);
366 if (!BN_is_zero(rm)) 355 if (!BN_is_zero(rm))
367 rm->neg = neg; 356 rm->neg = neg;
368 bn_check_top(rm);
369 } 357 }
370 if (no_branch) 358 if (no_branch)
371 bn_correct_top(res); 359 bn_correct_top(res);
@@ -373,7 +361,6 @@ BN_div_internal(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor
373 return (1); 361 return (1);
374 362
375err: 363err:
376 bn_check_top(rm);
377 BN_CTX_end(ctx); 364 BN_CTX_end(ctx);
378 return (0); 365 return (0);
379} 366}
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c
index 64156f716f..3bb0dd1304 100644
--- a/src/lib/libcrypto/bn/bn_exp.c
+++ b/src/lib/libcrypto/bn/bn_exp.c
@@ -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 $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -168,7 +168,6 @@ err:
168 if (r != rr && rr != NULL) 168 if (r != rr && rr != NULL)
169 BN_copy(r, rr); 169 BN_copy(r, rr);
170 BN_CTX_end(ctx); 170 BN_CTX_end(ctx);
171 bn_check_top(r);
172 return (ret); 171 return (ret);
173} 172}
174 173
@@ -178,9 +177,6 @@ BN_mod_exp_internal(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m
178{ 177{
179 int ret; 178 int ret;
180 179
181 bn_check_top(a);
182 bn_check_top(p);
183 bn_check_top(m);
184 180
185 /* For even modulus m = 2^k*m_odd, it might make sense to compute 181 /* For even modulus m = 2^k*m_odd, it might make sense to compute
186 * a^p mod m_odd and a^p mod 2^k separately (with Montgomery 182 * 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
222 ret = BN_mod_exp_recp(r, a,p, m, ctx); 218 ret = BN_mod_exp_recp(r, a,p, m, ctx);
223 } 219 }
224 220
225 bn_check_top(r);
226 return (ret); 221 return (ret);
227} 222}
228 223
@@ -381,7 +376,6 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
381err: 376err:
382 BN_CTX_end(ctx); 377 BN_CTX_end(ctx);
383 BN_RECP_CTX_free(&recp); 378 BN_RECP_CTX_free(&recp);
384 bn_check_top(r);
385 return (ret); 379 return (ret);
386} 380}
387 381
@@ -401,9 +395,6 @@ BN_mod_exp_mont_internal(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIG
401 return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont); 395 return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
402 } 396 }
403 397
404 bn_check_top(a);
405 bn_check_top(p);
406 bn_check_top(m);
407 398
408 if (!BN_is_odd(m)) { 399 if (!BN_is_odd(m)) {
409 BNerror(BN_R_CALLED_WITH_EVEN_MODULUS); 400 BNerror(BN_R_CALLED_WITH_EVEN_MODULUS);
@@ -533,7 +524,6 @@ err:
533 if ((in_mont == NULL) && (mont != NULL)) 524 if ((in_mont == NULL) && (mont != NULL))
534 BN_MONT_CTX_free(mont); 525 BN_MONT_CTX_free(mont);
535 BN_CTX_end(ctx); 526 BN_CTX_end(ctx);
536 bn_check_top(rr);
537 return (ret); 527 return (ret);
538} 528}
539 529
@@ -658,9 +648,6 @@ BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
658 unsigned char *powerbuf = NULL; 648 unsigned char *powerbuf = NULL;
659 BIGNUM tmp, am; 649 BIGNUM tmp, am;
660 650
661 bn_check_top(a);
662 bn_check_top(p);
663 bn_check_top(m);
664 651
665 if (!BN_is_odd(m)) { 652 if (!BN_is_odd(m)) {
666 BNerror(BN_R_CALLED_WITH_EVEN_MODULUS); 653 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,
937 return -1; 924 return -1;
938 } 925 }
939 926
940 bn_check_top(p);
941 bn_check_top(m);
942 927
943 if (!BN_is_odd(m)) { 928 if (!BN_is_odd(m)) {
944 BNerror(BN_R_CALLED_WITH_EVEN_MODULUS); 929 BNerror(BN_R_CALLED_WITH_EVEN_MODULUS);
@@ -1052,7 +1037,6 @@ err:
1052 if ((in_mont == NULL) && (mont != NULL)) 1037 if ((in_mont == NULL) && (mont != NULL))
1053 BN_MONT_CTX_free(mont); 1038 BN_MONT_CTX_free(mont);
1054 BN_CTX_end(ctx); 1039 BN_CTX_end(ctx);
1055 bn_check_top(rr);
1056 return (ret); 1040 return (ret);
1057} 1041}
1058 1042
@@ -1172,6 +1156,5 @@ BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
1172 1156
1173err: 1157err:
1174 BN_CTX_end(ctx); 1158 BN_CTX_end(ctx);
1175 bn_check_top(r);
1176 return (ret); 1159 return (ret);
1177} 1160}
diff --git a/src/lib/libcrypto/bn/bn_exp2.c b/src/lib/libcrypto/bn/bn_exp2.c
index c63503f941..b2fd53e971 100644
--- a/src/lib/libcrypto/bn/bn_exp2.c
+++ b/src/lib/libcrypto/bn/bn_exp2.c
@@ -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 $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -130,11 +130,6 @@ BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
130 BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE]; 130 BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];
131 BN_MONT_CTX *mont = NULL; 131 BN_MONT_CTX *mont = NULL;
132 132
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);
138 133
139 if (!BN_is_odd(m)) { 134 if (!BN_is_odd(m)) {
140 BNerror(BN_R_CALLED_WITH_EVEN_MODULUS); 135 BNerror(BN_R_CALLED_WITH_EVEN_MODULUS);
@@ -303,6 +298,5 @@ err:
303 if ((in_mont == NULL) && (mont != NULL)) 298 if ((in_mont == NULL) && (mont != NULL))
304 BN_MONT_CTX_free(mont); 299 BN_MONT_CTX_free(mont);
305 BN_CTX_end(ctx); 300 BN_CTX_end(ctx);
306 bn_check_top(rr);
307 return (ret); 301 return (ret);
308} 302}
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
index d756398c8f..3d92a43cef 100644
--- a/src/lib/libcrypto/bn/bn_gcd.c
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -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 $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -123,8 +123,6 @@ BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
123 BIGNUM *a, *b, *t; 123 BIGNUM *a, *b, *t;
124 int ret = 0; 124 int ret = 0;
125 125
126 bn_check_top(in_a);
127 bn_check_top(in_b);
128 126
129 BN_CTX_start(ctx); 127 BN_CTX_start(ctx);
130 if ((a = BN_CTX_get(ctx)) == NULL) 128 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)
154 152
155err: 153err:
156 BN_CTX_end(ctx); 154 BN_CTX_end(ctx);
157 bn_check_top(r);
158 return (ret); 155 return (ret);
159} 156}
160 157
@@ -179,8 +176,6 @@ euclid(BIGNUM *a, BIGNUM *b)
179 BIGNUM *t; 176 BIGNUM *t;
180 int shifts = 0; 177 int shifts = 0;
181 178
182 bn_check_top(a);
183 bn_check_top(b);
184 179
185 /* 0 <= b <= a */ 180 /* 0 <= b <= a */
186 while (!BN_is_zero(b)) { 181 while (!BN_is_zero(b)) {
@@ -236,7 +231,6 @@ euclid(BIGNUM *a, BIGNUM *b)
236 if (!BN_lshift(a, a, shifts)) 231 if (!BN_lshift(a, a, shifts))
237 goto err; 232 goto err;
238 } 233 }
239 bn_check_top(a);
240 return (a); 234 return (a);
241 235
242err: 236err:
@@ -259,8 +253,6 @@ BN_mod_inverse_internal(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ct
259 if (ct) 253 if (ct)
260 return BN_mod_inverse_no_branch(in, a, n, ctx); 254 return BN_mod_inverse_no_branch(in, a, n, ctx);
261 255
262 bn_check_top(a);
263 bn_check_top(n);
264 256
265 BN_CTX_start(ctx); 257 BN_CTX_start(ctx);
266 if ((A = BN_CTX_get(ctx)) == NULL) 258 if ((A = BN_CTX_get(ctx)) == NULL)
@@ -536,7 +528,6 @@ err:
536 if ((ret == NULL) && (in == NULL)) 528 if ((ret == NULL) && (in == NULL))
537 BN_free(R); 529 BN_free(R);
538 BN_CTX_end(ctx); 530 BN_CTX_end(ctx);
539 bn_check_top(ret);
540 return (ret); 531 return (ret);
541} 532}
542 533
@@ -573,8 +564,6 @@ BN_mod_inverse_no_branch(BIGNUM *in, const BIGNUM *a, const BIGNUM *n,
573 BIGNUM *ret = NULL; 564 BIGNUM *ret = NULL;
574 int sign; 565 int sign;
575 566
576 bn_check_top(a);
577 bn_check_top(n);
578 567
579 BN_init(&local_A); 568 BN_init(&local_A);
580 BN_init(&local_B); 569 BN_init(&local_B);
@@ -725,7 +714,6 @@ err:
725 if ((ret == NULL) && (in == NULL)) 714 if ((ret == NULL) && (in == NULL))
726 BN_free(R); 715 BN_free(R);
727 BN_CTX_end(ctx); 716 BN_CTX_end(ctx);
728 bn_check_top(ret);
729 return (ret); 717 return (ret);
730} 718}
731 719
@@ -750,8 +738,6 @@ BN_gcd_no_branch(BIGNUM *in, const BIGNUM *a, const BIGNUM *n,
750 BN_init(&local_A); 738 BN_init(&local_A);
751 BN_init(&local_B); 739 BN_init(&local_B);
752 740
753 bn_check_top(a);
754 bn_check_top(n);
755 741
756 BN_CTX_start(ctx); 742 BN_CTX_start(ctx);
757 if ((A = BN_CTX_get(ctx)) == NULL) 743 if ((A = BN_CTX_get(ctx)) == NULL)
@@ -871,6 +857,5 @@ err:
871 if ((ret == NULL) && (in == NULL)) 857 if ((ret == NULL) && (in == NULL))
872 BN_free(R); 858 BN_free(R);
873 BN_CTX_end(ctx); 859 BN_CTX_end(ctx);
874 bn_check_top(ret);
875 return (ret); 860 return (ret);
876} 861}
diff --git a/src/lib/libcrypto/bn/bn_gf2m.c b/src/lib/libcrypto/bn/bn_gf2m.c
index eceaba47c3..8adbbeb040 100644
--- a/src/lib/libcrypto/bn/bn_gf2m.c
+++ b/src/lib/libcrypto/bn/bn_gf2m.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_gf2m.c,v 1.26 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_gf2m.c,v 1.27 2022/11/26 13:56:33 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 * 4 *
@@ -325,8 +325,6 @@ BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
325 int i; 325 int i;
326 const BIGNUM *at, *bt; 326 const BIGNUM *at, *bt;
327 327
328 bn_check_top(a);
329 bn_check_top(b);
330 328
331 if (a->top < b->top) { 329 if (a->top < b->top) {
332 at = b; 330 at = b;
@@ -368,7 +366,6 @@ BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])
368 int n, dN, d0, d1; 366 int n, dN, d0, d1;
369 BN_ULONG zz, *z; 367 BN_ULONG zz, *z;
370 368
371 bn_check_top(a);
372 369
373 if (!p[0]) { 370 if (!p[0]) {
374 /* reduction mod 1 => return 0 */ 371 /* reduction mod 1 => return 0 */
@@ -467,8 +464,6 @@ BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
467 const int max = BN_num_bits(p) + 1; 464 const int max = BN_num_bits(p) + 1;
468 int *arr = NULL; 465 int *arr = NULL;
469 466
470 bn_check_top(a);
471 bn_check_top(p);
472 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) 467 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
473 goto err; 468 goto err;
474 ret = BN_GF2m_poly2arr(p, arr, max); 469 ret = BN_GF2m_poly2arr(p, arr, max);
@@ -477,7 +472,6 @@ BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
477 goto err; 472 goto err;
478 } 473 }
479 ret = BN_GF2m_mod_arr(r, a, arr); 474 ret = BN_GF2m_mod_arr(r, a, arr);
480 bn_check_top(r);
481 475
482 err: 476 err:
483 free(arr); 477 free(arr);
@@ -496,8 +490,6 @@ BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[],
496 BIGNUM *s; 490 BIGNUM *s;
497 BN_ULONG x1, x0, y1, y0, zz[4]; 491 BN_ULONG x1, x0, y1, y0, zz[4];
498 492
499 bn_check_top(a);
500 bn_check_top(b);
501 493
502 if (a == b) { 494 if (a == b) {
503 return BN_GF2m_mod_sqr_arr(r, a, p, ctx); 495 return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
@@ -530,7 +522,6 @@ BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[],
530 bn_correct_top(s); 522 bn_correct_top(s);
531 if (BN_GF2m_mod_arr(r, s, p)) 523 if (BN_GF2m_mod_arr(r, s, p))
532 ret = 1; 524 ret = 1;
533 bn_check_top(r);
534 525
535err: 526err:
536 BN_CTX_end(ctx); 527 BN_CTX_end(ctx);
@@ -552,9 +543,6 @@ BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p,
552 const int max = BN_num_bits(p) + 1; 543 const int max = BN_num_bits(p) + 1;
553 int *arr = NULL; 544 int *arr = NULL;
554 545
555 bn_check_top(a);
556 bn_check_top(b);
557 bn_check_top(p);
558 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) 546 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
559 goto err; 547 goto err;
560 ret = BN_GF2m_poly2arr(p, arr, max); 548 ret = BN_GF2m_poly2arr(p, arr, max);
@@ -563,7 +551,6 @@ BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p,
563 goto err; 551 goto err;
564 } 552 }
565 ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx); 553 ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
566 bn_check_top(r);
567 554
568err: 555err:
569 free(arr); 556 free(arr);
@@ -578,7 +565,6 @@ BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx)
578 int i, ret = 0; 565 int i, ret = 0;
579 BIGNUM *s; 566 BIGNUM *s;
580 567
581 bn_check_top(a);
582 BN_CTX_start(ctx); 568 BN_CTX_start(ctx);
583 if ((s = BN_CTX_get(ctx)) == NULL) 569 if ((s = BN_CTX_get(ctx)) == NULL)
584 goto err; 570 goto err;
@@ -594,7 +580,6 @@ BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx)
594 bn_correct_top(s); 580 bn_correct_top(s);
595 if (!BN_GF2m_mod_arr(r, s, p)) 581 if (!BN_GF2m_mod_arr(r, s, p))
596 goto err; 582 goto err;
597 bn_check_top(r);
598 ret = 1; 583 ret = 1;
599 584
600err: 585err:
@@ -615,8 +600,6 @@ BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
615 const int max = BN_num_bits(p) + 1; 600 const int max = BN_num_bits(p) + 1;
616 int *arr = NULL; 601 int *arr = NULL;
617 602
618 bn_check_top(a);
619 bn_check_top(p);
620 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) 603 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
621 goto err; 604 goto err;
622 ret = BN_GF2m_poly2arr(p, arr, max); 605 ret = BN_GF2m_poly2arr(p, arr, max);
@@ -625,7 +608,6 @@ BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
625 goto err; 608 goto err;
626 } 609 }
627 ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx); 610 ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
628 bn_check_top(r);
629 611
630err: 612err:
631 free(arr); 613 free(arr);
@@ -644,8 +626,6 @@ BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
644 BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp; 626 BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
645 int ret = 0; 627 int ret = 0;
646 628
647 bn_check_top(a);
648 bn_check_top(p);
649 629
650 BN_CTX_start(ctx); 630 BN_CTX_start(ctx);
651 631
@@ -795,7 +775,6 @@ BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
795 775
796 if (!BN_copy(r, b)) 776 if (!BN_copy(r, b))
797 goto err; 777 goto err;
798 bn_check_top(r);
799 ret = 1; 778 ret = 1;
800 779
801err: 780err:
@@ -820,7 +799,6 @@ BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[], BN_CTX *ctx)
820 BIGNUM *field; 799 BIGNUM *field;
821 int ret = 0; 800 int ret = 0;
822 801
823 bn_check_top(xx);
824 BN_CTX_start(ctx); 802 BN_CTX_start(ctx);
825 if ((field = BN_CTX_get(ctx)) == NULL) 803 if ((field = BN_CTX_get(ctx)) == NULL)
826 goto err; 804 goto err;
@@ -828,7 +806,6 @@ BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[], BN_CTX *ctx)
828 goto err; 806 goto err;
829 807
830 ret = BN_GF2m_mod_inv(r, xx, field, ctx); 808 ret = BN_GF2m_mod_inv(r, xx, field, ctx);
831 bn_check_top(r);
832 809
833err: 810err:
834 BN_CTX_end(ctx); 811 BN_CTX_end(ctx);
@@ -847,9 +824,6 @@ BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p,
847 BIGNUM *xinv = NULL; 824 BIGNUM *xinv = NULL;
848 int ret = 0; 825 int ret = 0;
849 826
850 bn_check_top(y);
851 bn_check_top(x);
852 bn_check_top(p);
853 827
854 BN_CTX_start(ctx); 828 BN_CTX_start(ctx);
855 if ((xinv = BN_CTX_get(ctx)) == NULL) 829 if ((xinv = BN_CTX_get(ctx)) == NULL)
@@ -859,7 +833,6 @@ BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p,
859 goto err; 833 goto err;
860 if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx)) 834 if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
861 goto err; 835 goto err;
862 bn_check_top(r);
863 ret = 1; 836 ret = 1;
864 837
865err: 838err:
@@ -880,9 +853,6 @@ BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p,
880 BIGNUM *a, *b, *u, *v; 853 BIGNUM *a, *b, *u, *v;
881 int ret = 0; 854 int ret = 0;
882 855
883 bn_check_top(y);
884 bn_check_top(x);
885 bn_check_top(p);
886 856
887 BN_CTX_start(ctx); 857 BN_CTX_start(ctx);
888 858
@@ -949,7 +919,6 @@ BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p,
949 919
950 if (!BN_copy(r, u)) 920 if (!BN_copy(r, u))
951 goto err; 921 goto err;
952 bn_check_top(r);
953 ret = 1; 922 ret = 1;
954 923
955err: 924err:
@@ -972,8 +941,6 @@ BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
972 BIGNUM *field; 941 BIGNUM *field;
973 int ret = 0; 942 int ret = 0;
974 943
975 bn_check_top(yy);
976 bn_check_top(xx);
977 944
978 BN_CTX_start(ctx); 945 BN_CTX_start(ctx);
979 if ((field = BN_CTX_get(ctx)) == NULL) 946 if ((field = BN_CTX_get(ctx)) == NULL)
@@ -982,7 +949,6 @@ BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
982 goto err; 949 goto err;
983 950
984 ret = BN_GF2m_mod_div(r, yy, xx, field, ctx); 951 ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
985 bn_check_top(r);
986 952
987err: 953err:
988 BN_CTX_end(ctx); 954 BN_CTX_end(ctx);
@@ -1001,8 +967,6 @@ BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[],
1001 int ret = 0, i, n; 967 int ret = 0, i, n;
1002 BIGNUM *u; 968 BIGNUM *u;
1003 969
1004 bn_check_top(a);
1005 bn_check_top(b);
1006 970
1007 if (BN_is_zero(b)) 971 if (BN_is_zero(b))
1008 return (BN_one(r)); 972 return (BN_one(r));
@@ -1028,7 +992,6 @@ BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[],
1028 } 992 }
1029 if (!BN_copy(r, u)) 993 if (!BN_copy(r, u))
1030 goto err; 994 goto err;
1031 bn_check_top(r);
1032 ret = 1; 995 ret = 1;
1033 996
1034err: 997err:
@@ -1051,9 +1014,6 @@ BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p,
1051 const int max = BN_num_bits(p) + 1; 1014 const int max = BN_num_bits(p) + 1;
1052 int *arr = NULL; 1015 int *arr = NULL;
1053 1016
1054 bn_check_top(a);
1055 bn_check_top(b);
1056 bn_check_top(p);
1057 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) 1017 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
1058 goto err; 1018 goto err;
1059 ret = BN_GF2m_poly2arr(p, arr, max); 1019 ret = BN_GF2m_poly2arr(p, arr, max);
@@ -1062,7 +1022,6 @@ BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p,
1062 goto err; 1022 goto err;
1063 } 1023 }
1064 ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx); 1024 ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
1065 bn_check_top(r);
1066 1025
1067err: 1026err:
1068 free(arr); 1027 free(arr);
@@ -1079,7 +1038,6 @@ BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx)
1079 int ret = 0; 1038 int ret = 0;
1080 BIGNUM *u; 1039 BIGNUM *u;
1081 1040
1082 bn_check_top(a);
1083 1041
1084 if (!p[0]) { 1042 if (!p[0]) {
1085 /* reduction mod 1 => return 0 */ 1043 /* reduction mod 1 => return 0 */
@@ -1094,7 +1052,6 @@ BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx)
1094 if (!BN_set_bit(u, p[0] - 1)) 1052 if (!BN_set_bit(u, p[0] - 1))
1095 goto err; 1053 goto err;
1096 ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx); 1054 ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
1097 bn_check_top(r);
1098 1055
1099err: 1056err:
1100 BN_CTX_end(ctx); 1057 BN_CTX_end(ctx);
@@ -1114,8 +1071,6 @@ BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1114 int ret = 0; 1071 int ret = 0;
1115 const int max = BN_num_bits(p) + 1; 1072 const int max = BN_num_bits(p) + 1;
1116 int *arr = NULL; 1073 int *arr = NULL;
1117 bn_check_top(a);
1118 bn_check_top(p);
1119 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) 1074 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
1120 goto err; 1075 goto err;
1121 ret = BN_GF2m_poly2arr(p, arr, max); 1076 ret = BN_GF2m_poly2arr(p, arr, max);
@@ -1124,7 +1079,6 @@ BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1124 goto err; 1079 goto err;
1125 } 1080 }
1126 ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx); 1081 ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
1127 bn_check_top(r);
1128 1082
1129err: 1083err:
1130 free(arr); 1084 free(arr);
@@ -1141,7 +1095,6 @@ BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1141 int ret = 0, count = 0, j; 1095 int ret = 0, count = 0, j;
1142 BIGNUM *a, *z, *rho, *w, *w2, *tmp; 1096 BIGNUM *a, *z, *rho, *w, *w2, *tmp;
1143 1097
1144 bn_check_top(a_);
1145 1098
1146 if (!p[0]) { 1099 if (!p[0]) {
1147 /* reduction mod 1 => return 0 */ 1100 /* reduction mod 1 => return 0 */
@@ -1228,7 +1181,6 @@ BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1228 1181
1229 if (!BN_copy(r, z)) 1182 if (!BN_copy(r, z))
1230 goto err; 1183 goto err;
1231 bn_check_top(r);
1232 1184
1233 ret = 1; 1185 ret = 1;
1234 1186
@@ -1250,8 +1202,6 @@ BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1250 const int max = BN_num_bits(p) + 1; 1202 const int max = BN_num_bits(p) + 1;
1251 int *arr = NULL; 1203 int *arr = NULL;
1252 1204
1253 bn_check_top(a);
1254 bn_check_top(p);
1255 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL) 1205 if ((arr = reallocarray(NULL, max, sizeof(int))) == NULL)
1256 goto err; 1206 goto err;
1257 ret = BN_GF2m_poly2arr(p, arr, max); 1207 ret = BN_GF2m_poly2arr(p, arr, max);
@@ -1260,7 +1210,6 @@ BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1260 goto err; 1210 goto err;
1261 } 1211 }
1262 ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx); 1212 ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1263 bn_check_top(r);
1264 1213
1265err: 1214err:
1266 free(arr); 1215 free(arr);
@@ -1312,13 +1261,11 @@ BN_GF2m_arr2poly(const int p[], BIGNUM *a)
1312{ 1261{
1313 int i; 1262 int i;
1314 1263
1315 bn_check_top(a);
1316 BN_zero(a); 1264 BN_zero(a);
1317 for (i = 0; p[i] != -1; i++) { 1265 for (i = 0; p[i] != -1; i++) {
1318 if (BN_set_bit(a, p[i]) == 0) 1266 if (BN_set_bit(a, p[i]) == 0)
1319 return 0; 1267 return 0;
1320 } 1268 }
1321 bn_check_top(a);
1322 1269
1323 return 1; 1270 return 1;
1324} 1271}
diff --git a/src/lib/libcrypto/bn/bn_kron.c b/src/lib/libcrypto/bn/bn_kron.c
index 998adedcd8..8629892a72 100644
--- a/src/lib/libcrypto/bn/bn_kron.c
+++ b/src/lib/libcrypto/bn/bn_kron.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_kron.c,v 1.10 2022/07/12 16:08:19 tb Exp $ */ 1/* $OpenBSD: bn_kron.c,v 1.11 2022/11/26 13:56:33 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -71,8 +71,6 @@ BN_kronecker(const BIGNUM *A, const BIGNUM *B, BN_CTX *ctx)
71 int k, v; 71 int k, v;
72 int ret = -2; 72 int ret = -2;
73 73
74 bn_check_top(A);
75 bn_check_top(B);
76 74
77 BN_CTX_start(ctx); 75 BN_CTX_start(ctx);
78 76
diff --git a/src/lib/libcrypto/bn/bn_lcl.h b/src/lib/libcrypto/bn/bn_lcl.h
index d5f1250cfd..64855115f2 100644
--- a/src/lib/libcrypto/bn/bn_lcl.h
+++ b/src/lib/libcrypto/bn/bn_lcl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lcl.h,v 1.38 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_lcl.h,v 1.39 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -327,18 +327,6 @@ struct bn_gencb_st {
327#define Lw(t) (((BN_ULONG)(t))&BN_MASK2) 327#define Lw(t) (((BN_ULONG)(t))&BN_MASK2)
328#define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2) 328#define Hw(t) (((BN_ULONG)((t)>>BN_BITS2))&BN_MASK2)
329 329
330#ifdef BN_DEBUG_RAND
331#define bn_clear_top2max(a) \
332 { \
333 int ind = (a)->dmax - (a)->top; \
334 BN_ULONG *ftl = &(a)->d[(a)->top-1]; \
335 for (; ind != 0; ind--) \
336 *(++ftl) = 0x0; \
337 }
338#else
339#define bn_clear_top2max(a)
340#endif
341
342#ifdef BN_LLONG 330#ifdef BN_LLONG
343#define mul_add(r,a,w,c) { \ 331#define mul_add(r,a,w,c) { \
344 BN_ULLONG t; \ 332 BN_ULLONG t; \
@@ -524,88 +512,6 @@ int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_U
524int bn_expand(BIGNUM *a, int bits); 512int bn_expand(BIGNUM *a, int bits);
525int bn_wexpand(BIGNUM *a, int words); 513int bn_wexpand(BIGNUM *a, int words);
526 514
527/* Bignum consistency macros
528 * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from
529 * bignum data after direct manipulations on the data. There is also an
530 * "internal" macro, bn_check_top(), for verifying that there are no leading
531 * zeroes. Unfortunately, some auditing is required due to the fact that
532 * bn_fix_top() has become an overabused duct-tape because bignum data is
533 * occasionally passed around in an inconsistent state. So the following
534 * changes have been made to sort this out;
535 * - bn_fix_top()s implementation has been moved to bn_correct_top()
536 * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and
537 * bn_check_top() is as before.
538 * - if BN_DEBUG *is* defined;
539 * - bn_check_top() tries to pollute unused words even if the bignum 'top' is
540 * consistent. (ed: only if BN_DEBUG_RAND is defined)
541 * - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.
542 * The idea is to have debug builds flag up inconsistent bignums when they
543 * occur. If that occurs in a bn_fix_top(), we examine the code in question; if
544 * the use of bn_fix_top() was appropriate (ie. it follows directly after code
545 * that manipulates the bignum) it is converted to bn_correct_top(), and if it
546 * was not appropriate, we convert it permanently to bn_check_top() and track
547 * down the cause of the bug. Eventually, no internal code should be using the
548 * bn_fix_top() macro. External applications and libraries should try this with
549 * their own code too, both in terms of building against the openssl headers
550 * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it
551 * defined. This not only improves external code, it provides more test
552 * coverage for openssl's own code.
553 */
554
555#ifdef BN_DEBUG
556
557/* We only need assert() when debugging */
558#include <assert.h>
559
560#ifdef BN_DEBUG_RAND
561#define bn_pollute(a) \
562 do { \
563 const BIGNUM *_bnum1 = (a); \
564 if(_bnum1->top < _bnum1->dmax) { \
565 unsigned char _tmp_char; \
566 /* We cast away const without the compiler knowing, any \
567 * *genuinely* constant variables that aren't mutable \
568 * wouldn't be constructed with top!=dmax. */ \
569 BN_ULONG *_not_const; \
570 memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
571 arc4random_buf(&_tmp_char, 1); \
572 memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
573 (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
574 } \
575 } while(0)
576#else
577#define bn_pollute(a)
578#endif
579
580#define bn_check_top(a) \
581 do { \
582 const BIGNUM *_bnum2 = (a); \
583 if (_bnum2 != NULL) { \
584 assert((_bnum2->top == 0) || \
585 (_bnum2->d[_bnum2->top - 1] != 0)); \
586 bn_pollute(_bnum2); \
587 } \
588 } while(0)
589
590#define bn_fix_top(a) bn_check_top(a)
591
592#define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
593#define bn_wcheck_size(bn, words) \
594 do { \
595 const BIGNUM *_bnum2 = (bn); \
596 assert(words <= (_bnum2)->dmax && words >= (_bnum2)->top); \
597 } while(0)
598
599#else /* !BN_DEBUG */
600
601#define bn_pollute(a)
602#define bn_check_top(a)
603#define bn_fix_top(a) bn_correct_top(a)
604#define bn_check_size(bn, bits)
605#define bn_wcheck_size(bn, words)
606
607#endif
608
609#define bn_correct_top(a) \ 515#define bn_correct_top(a) \
610 { \ 516 { \
611 BN_ULONG *ftl; \ 517 BN_ULONG *ftl; \
@@ -616,7 +522,6 @@ int bn_wexpand(BIGNUM *a, int words);
616 if (*(ftl--)) break; \ 522 if (*(ftl--)) break; \
617 (a)->top = tmp_top; \ 523 (a)->top = tmp_top; \
618 } \ 524 } \
619 bn_pollute(a); \
620 } 525 }
621 526
622BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w); 527BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index e67abf90b1..a3b6811986 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lib.c,v 1.61 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_lib.c,v 1.62 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -86,7 +86,6 @@ BN_new(void)
86 ret->neg = 0; 86 ret->neg = 0;
87 ret->dmax = 0; 87 ret->dmax = 0;
88 ret->d = NULL; 88 ret->d = NULL;
89 bn_check_top(ret);
90 return (ret); 89 return (ret);
91} 90}
92 91
@@ -94,13 +93,11 @@ void
94BN_init(BIGNUM *a) 93BN_init(BIGNUM *a)
95{ 94{
96 memset(a, 0, sizeof(BIGNUM)); 95 memset(a, 0, sizeof(BIGNUM));
97 bn_check_top(a);
98} 96}
99 97
100void 98void
101BN_clear(BIGNUM *a) 99BN_clear(BIGNUM *a)
102{ 100{
103 bn_check_top(a);
104 if (a->d != NULL) 101 if (a->d != NULL)
105 explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); 102 explicit_bzero(a->d, a->dmax * sizeof(a->d[0]));
106 a->top = 0; 103 a->top = 0;
@@ -114,7 +111,6 @@ BN_clear_free(BIGNUM *a)
114 111
115 if (a == NULL) 112 if (a == NULL)
116 return; 113 return;
117 bn_check_top(a);
118 if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) 114 if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA)))
119 freezero(a->d, a->dmax * sizeof(a->d[0])); 115 freezero(a->d, a->dmax * sizeof(a->d[0]));
120 i = BN_get_flags(a, BN_FLG_MALLOCED); 116 i = BN_get_flags(a, BN_FLG_MALLOCED);
@@ -256,7 +252,6 @@ BN_num_bits(const BIGNUM *a)
256{ 252{
257 int i = a->top - 1; 253 int i = a->top - 1;
258 254
259 bn_check_top(a);
260 255
261 if (BN_is_zero(a)) 256 if (BN_is_zero(a))
262 return 0; 257 return 0;
@@ -271,7 +266,6 @@ bn_expand_internal(const BIGNUM *b, int words)
271 const BN_ULONG *B; 266 const BN_ULONG *B;
272 int i; 267 int i;
273 268
274 bn_check_top(b);
275 269
276 if (words > (INT_MAX/(4*BN_BITS2))) { 270 if (words > (INT_MAX/(4*BN_BITS2))) {
277 BNerror(BN_R_BIGNUM_TOO_LONG); 271 BNerror(BN_R_BIGNUM_TOO_LONG);
@@ -337,7 +331,6 @@ bn_expand_internal(const BIGNUM *b, int words)
337static int 331static int
338bn_expand2(BIGNUM *b, int words) 332bn_expand2(BIGNUM *b, int words)
339{ 333{
340 bn_check_top(b);
341 334
342 if (words > b->dmax) { 335 if (words > b->dmax) {
343 BN_ULONG *a = bn_expand_internal(b, words); 336 BN_ULONG *a = bn_expand_internal(b, words);
@@ -370,7 +363,6 @@ bn_expand2(BIGNUM *b, int words)
370 assert(A == &(b->d[b->dmax])); 363 assert(A == &(b->d[b->dmax]));
371 } 364 }
372#endif 365#endif
373 bn_check_top(b);
374 return 1; 366 return 1;
375} 367}
376 368
@@ -408,7 +400,6 @@ BN_dup(const BIGNUM *a)
408 400
409 if (a == NULL) 401 if (a == NULL)
410 return NULL; 402 return NULL;
411 bn_check_top(a);
412 403
413 t = BN_new(); 404 t = BN_new();
414 if (t == NULL) 405 if (t == NULL)
@@ -417,7 +408,6 @@ BN_dup(const BIGNUM *a)
417 BN_free(t); 408 BN_free(t);
418 return NULL; 409 return NULL;
419 } 410 }
420 bn_check_top(t);
421 return t; 411 return t;
422} 412}
423 413
@@ -428,7 +418,6 @@ BN_copy(BIGNUM *a, const BIGNUM *b)
428 BN_ULONG *A; 418 BN_ULONG *A;
429 const BN_ULONG *B; 419 const BN_ULONG *B;
430 420
431 bn_check_top(b);
432 421
433 if (a == b) 422 if (a == b)
434 return (a); 423 return (a);
@@ -463,7 +452,6 @@ BN_copy(BIGNUM *a, const BIGNUM *b)
463 452
464 a->top = b->top; 453 a->top = b->top;
465 a->neg = b->neg; 454 a->neg = b->neg;
466 bn_check_top(a);
467 return (a); 455 return (a);
468} 456}
469 457
@@ -474,8 +462,6 @@ BN_swap(BIGNUM *a, BIGNUM *b)
474 BN_ULONG *tmp_d; 462 BN_ULONG *tmp_d;
475 int tmp_top, tmp_dmax, tmp_neg; 463 int tmp_top, tmp_dmax, tmp_neg;
476 464
477 bn_check_top(a);
478 bn_check_top(b);
479 465
480 flags_old_a = a->flags; 466 flags_old_a = a->flags;
481 flags_old_b = b->flags; 467 flags_old_b = b->flags;
@@ -499,8 +485,6 @@ BN_swap(BIGNUM *a, BIGNUM *b)
499 (flags_old_b & BN_FLG_STATIC_DATA); 485 (flags_old_b & BN_FLG_STATIC_DATA);
500 b->flags = (flags_old_b & BN_FLG_MALLOCED) | 486 b->flags = (flags_old_b & BN_FLG_MALLOCED) |
501 (flags_old_a & BN_FLG_STATIC_DATA); 487 (flags_old_a & BN_FLG_STATIC_DATA);
502 bn_check_top(a);
503 bn_check_top(b);
504} 488}
505 489
506BN_ULONG 490BN_ULONG
@@ -517,13 +501,11 @@ BN_get_word(const BIGNUM *a)
517int 501int
518BN_set_word(BIGNUM *a, BN_ULONG w) 502BN_set_word(BIGNUM *a, BN_ULONG w)
519{ 503{
520 bn_check_top(a);
521 if (!bn_wexpand(a, 1)) 504 if (!bn_wexpand(a, 1))
522 return (0); 505 return (0);
523 a->neg = 0; 506 a->neg = 0;
524 a->d[0] = w; 507 a->d[0] = w;
525 a->top = (w ? 1 : 0); 508 a->top = (w ? 1 : 0);
526 bn_check_top(a);
527 return (1); 509 return (1);
528} 510}
529 511
@@ -541,7 +523,6 @@ BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
541 ret = bn = BN_new(); 523 ret = bn = BN_new();
542 if (ret == NULL) 524 if (ret == NULL)
543 return (NULL); 525 return (NULL);
544 bn_check_top(ret);
545 l = 0; 526 l = 0;
546 n = len; 527 n = len;
547 if (n == 0) { 528 if (n == 0) {
@@ -658,7 +639,6 @@ BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
658 if (ret == NULL) 639 if (ret == NULL)
659 return NULL; 640 return NULL;
660 641
661 bn_check_top(ret);
662 642
663 s += len; 643 s += len;
664 /* Skip trailing zeroes. */ 644 /* Skip trailing zeroes. */
@@ -715,8 +695,6 @@ BN_ucmp(const BIGNUM *a, const BIGNUM *b)
715 int i; 695 int i;
716 BN_ULONG t1, t2, *ap, *bp; 696 BN_ULONG t1, t2, *ap, *bp;
717 697
718 bn_check_top(a);
719 bn_check_top(b);
720 698
721 i = a->top - b->top; 699 i = a->top - b->top;
722 if (i != 0) 700 if (i != 0)
@@ -748,8 +726,6 @@ BN_cmp(const BIGNUM *a, const BIGNUM *b)
748 return (0); 726 return (0);
749 } 727 }
750 728
751 bn_check_top(a);
752 bn_check_top(b);
753 729
754 if (a->neg != b->neg) { 730 if (a->neg != b->neg) {
755 if (a->neg) 731 if (a->neg)
@@ -799,7 +775,6 @@ BN_set_bit(BIGNUM *a, int n)
799 } 775 }
800 776
801 a->d[i] |= (((BN_ULONG)1) << j); 777 a->d[i] |= (((BN_ULONG)1) << j);
802 bn_check_top(a);
803 return (1); 778 return (1);
804} 779}
805 780
@@ -808,7 +783,6 @@ BN_clear_bit(BIGNUM *a, int n)
808{ 783{
809 int i, j; 784 int i, j;
810 785
811 bn_check_top(a);
812 if (n < 0) 786 if (n < 0)
813 return 0; 787 return 0;
814 788
@@ -827,7 +801,6 @@ BN_is_bit_set(const BIGNUM *a, int n)
827{ 801{
828 int i, j; 802 int i, j;
829 803
830 bn_check_top(a);
831 if (n < 0) 804 if (n < 0)
832 return 0; 805 return 0;
833 i = n / BN_BITS2; 806 i = n / BN_BITS2;
@@ -842,7 +815,6 @@ BN_mask_bits(BIGNUM *a, int n)
842{ 815{
843 int b, w; 816 int b, w;
844 817
845 bn_check_top(a);
846 if (n < 0) 818 if (n < 0)
847 return 0; 819 return 0;
848 820
@@ -932,9 +904,6 @@ BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
932 BN_ULONG t; 904 BN_ULONG t;
933 int i; 905 int i;
934 906
935 bn_wcheck_size(a, nwords);
936 bn_wcheck_size(b, nwords);
937
938 assert(a != b); 907 assert(a != b);
939 assert((condition & (condition - 1)) == 0); 908 assert((condition & (condition - 1)) == 0);
940 assert(sizeof(BN_ULONG) >= sizeof(int)); 909 assert(sizeof(BN_ULONG) >= sizeof(int));
diff --git a/src/lib/libcrypto/bn/bn_mod.c b/src/lib/libcrypto/bn/bn_mod.c
index 897ff434e9..5be8252f2d 100644
--- a/src/lib/libcrypto/bn/bn_mod.c
+++ b/src/lib/libcrypto/bn/bn_mod.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mod.c,v 1.12 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: bn_mod.c,v 1.13 2022/11/26 13:56:33 jsing Exp $ */
2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project. */ 3 * for the OpenSSL project. */
4/* ==================================================================== 4/* ====================================================================
@@ -182,9 +182,6 @@ BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
182 BIGNUM *t; 182 BIGNUM *t;
183 int ret = 0; 183 int ret = 0;
184 184
185 bn_check_top(a);
186 bn_check_top(b);
187 bn_check_top(m);
188 185
189 BN_CTX_start(ctx); 186 BN_CTX_start(ctx);
190 if ((t = BN_CTX_get(ctx)) == NULL) 187 if ((t = BN_CTX_get(ctx)) == NULL)
@@ -198,7 +195,6 @@ BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
198 } 195 }
199 if (!BN_nnmod(r, t,m, ctx)) 196 if (!BN_nnmod(r, t,m, ctx))
200 goto err; 197 goto err;
201 bn_check_top(r);
202 ret = 1; 198 ret = 1;
203 199
204err: 200err:
@@ -220,7 +216,6 @@ BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
220{ 216{
221 if (!BN_lshift1(r, a)) 217 if (!BN_lshift1(r, a))
222 return 0; 218 return 0;
223 bn_check_top(r);
224 return BN_nnmod(r, r, m, ctx); 219 return BN_nnmod(r, r, m, ctx);
225} 220}
226 221
@@ -231,7 +226,6 @@ BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m)
231{ 226{
232 if (!BN_lshift1(r, a)) 227 if (!BN_lshift1(r, a))
233 return 0; 228 return 0;
234 bn_check_top(r);
235 if (BN_cmp(r, m) >= 0) 229 if (BN_cmp(r, m) >= 0)
236 return BN_sub(r, r, m); 230 return BN_sub(r, r, m);
237 return 1; 231 return 1;
@@ -254,7 +248,6 @@ BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx)
254 } 248 }
255 249
256 ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m)); 250 ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
257 bn_check_top(r);
258 251
259 BN_free(abs_m); 252 BN_free(abs_m);
260 return ret; 253 return ret;
@@ -302,7 +295,6 @@ BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)
302 return 0; 295 return 0;
303 } 296 }
304 } 297 }
305 bn_check_top(r);
306 298
307 return 1; 299 return 1;
308} 300}
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
index 251c67b89d..24bc41e886 100644
--- a/src/lib/libcrypto/bn/bn_mont.c
+++ b/src/lib/libcrypto/bn/bn_mont.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mont.c,v 1.30 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_mont.c,v 1.31 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -152,7 +152,6 @@ BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
152 if ((tmp = BN_CTX_get(ctx)) == NULL) 152 if ((tmp = BN_CTX_get(ctx)) == NULL)
153 goto err; 153 goto err;
154 154
155 bn_check_top(tmp);
156 if (a == b) { 155 if (a == b) {
157 if (!BN_sqr(tmp, a, ctx)) 156 if (!BN_sqr(tmp, a, ctx))
158 goto err; 157 goto err;
@@ -168,7 +167,6 @@ BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
168 if (!BN_from_montgomery(r, tmp, mont, ctx)) 167 if (!BN_from_montgomery(r, tmp, mont, ctx))
169 goto err; 168 goto err;
170#endif 169#endif
171 bn_check_top(r);
172 ret = 1; 170 ret = 1;
173err: 171err:
174 BN_CTX_end(ctx); 172 BN_CTX_end(ctx);
@@ -272,7 +270,6 @@ BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
272#endif 270#endif
273 bn_correct_top(r); 271 bn_correct_top(r);
274 bn_correct_top(ret); 272 bn_correct_top(ret);
275 bn_check_top(ret);
276 273
277 return (1); 274 return (1);
278} 275}
@@ -318,7 +315,6 @@ BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx)
318 goto err; 315 goto err;
319 } 316 }
320 retn = 1; 317 retn = 1;
321 bn_check_top(ret);
322 318
323err: 319err:
324 BN_CTX_end(ctx); 320 BN_CTX_end(ctx);
diff --git a/src/lib/libcrypto/bn/bn_mpi.c b/src/lib/libcrypto/bn/bn_mpi.c
index 9b743cca8c..75b34511fe 100644
--- a/src/lib/libcrypto/bn/bn_mpi.c
+++ b/src/lib/libcrypto/bn/bn_mpi.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mpi.c,v 1.9 2022/11/09 01:05:45 tobhe Exp $ */ 1/* $OpenBSD: bn_mpi.c,v 1.10 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -131,6 +131,5 @@ BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain)
131 if (neg) { 131 if (neg) {
132 BN_clear_bit(a, BN_num_bits(a) - 1); 132 BN_clear_bit(a, BN_num_bits(a) - 1);
133 } 133 }
134 bn_check_top(a);
135 return (a); 134 return (a);
136} 135}
diff --git a/src/lib/libcrypto/bn/bn_mul.c b/src/lib/libcrypto/bn/bn_mul.c
index fa9d559da9..0d8da8a6f6 100644
--- a/src/lib/libcrypto/bn/bn_mul.c
+++ b/src/lib/libcrypto/bn/bn_mul.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mul.c,v 1.21 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_mul.c,v 1.22 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -954,9 +954,6 @@ BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
954 fprintf(stderr, "BN_mul %d * %d\n",a->top,b->top); 954 fprintf(stderr, "BN_mul %d * %d\n",a->top,b->top);
955#endif 955#endif
956 956
957 bn_check_top(a);
958 bn_check_top(b);
959 bn_check_top(r);
960 957
961 al = a->top; 958 al = a->top;
962 bl = b->top; 959 bl = b->top;
@@ -1092,7 +1089,6 @@ end:
1092 BN_copy(r, rr); 1089 BN_copy(r, rr);
1093 ret = 1; 1090 ret = 1;
1094err: 1091err:
1095 bn_check_top(r);
1096 BN_CTX_end(ctx); 1092 BN_CTX_end(ctx);
1097 return (ret); 1093 return (ret);
1098} 1094}
diff --git a/src/lib/libcrypto/bn/bn_prime.c b/src/lib/libcrypto/bn/bn_prime.c
index bf3f931644..0ba288c46a 100644
--- a/src/lib/libcrypto/bn/bn_prime.c
+++ b/src/lib/libcrypto/bn/bn_prime.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_prime.c,v 1.26 2022/11/09 22:52:51 tb Exp $ */ 1/* $OpenBSD: bn_prime.c,v 1.27 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -232,7 +232,6 @@ BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
232 err: 232 err:
233 BN_CTX_end(ctx); 233 BN_CTX_end(ctx);
234 BN_CTX_free(ctx); 234 BN_CTX_free(ctx);
235 bn_check_top(ret);
236 235
237 return found; 236 return found;
238} 237}
@@ -288,7 +287,6 @@ loop:
288 } 287 }
289 if (!BN_add_word(rnd, delta)) 288 if (!BN_add_word(rnd, delta))
290 return (0); 289 return (0);
291 bn_check_top(rnd);
292 return (1); 290 return (1);
293} 291}
294 292
@@ -338,7 +336,6 @@ loop:
338 336
339err: 337err:
340 BN_CTX_end(ctx); 338 BN_CTX_end(ctx);
341 bn_check_top(rnd);
342 return (ret); 339 return (ret);
343} 340}
344 341
@@ -406,6 +403,5 @@ loop:
406 403
407err: 404err:
408 BN_CTX_end(ctx); 405 BN_CTX_end(ctx);
409 bn_check_top(p);
410 return (ret); 406 return (ret);
411} 407}
diff --git a/src/lib/libcrypto/bn/bn_print.c b/src/lib/libcrypto/bn/bn_print.c
index ad2e3ba16c..4576e25f49 100644
--- a/src/lib/libcrypto/bn/bn_print.c
+++ b/src/lib/libcrypto/bn/bn_print.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_print.c,v 1.35 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_print.c,v 1.36 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -254,7 +254,6 @@ BN_hex2bn(BIGNUM **bn, const char *a)
254 ret->neg = neg; 254 ret->neg = neg;
255 255
256 *bn = ret; 256 *bn = ret;
257 bn_check_top(ret);
258 return (num); 257 return (num);
259 258
260err: 259err:
@@ -322,7 +321,6 @@ BN_dec2bn(BIGNUM **bn, const char *a)
322 321
323 bn_correct_top(ret); 322 bn_correct_top(ret);
324 *bn = ret; 323 *bn = ret;
325 bn_check_top(ret);
326 return (num); 324 return (num);
327 325
328err: 326err:
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c
index b21692c949..17f1868350 100644
--- a/src/lib/libcrypto/bn/bn_rand.c
+++ b/src/lib/libcrypto/bn/bn_rand.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_rand.c,v 1.25 2021/08/31 11:19:19 tb Exp $ */ 1/* $OpenBSD: bn_rand.c,v 1.26 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -190,7 +190,6 @@ bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
190 190
191err: 191err:
192 freezero(buf, bytes); 192 freezero(buf, bytes);
193 bn_check_top(rnd);
194 return (ret); 193 return (ret);
195} 194}
196 195
@@ -272,7 +271,6 @@ bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range)
272 } while (BN_cmp(r, range) >= 0); 271 } while (BN_cmp(r, range) >= 0);
273 } 272 }
274 273
275 bn_check_top(r);
276 return 1; 274 return 1;
277} 275}
278 276
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c
index 6588d33033..8959f6bd3c 100644
--- a/src/lib/libcrypto/bn/bn_recp.c
+++ b/src/lib/libcrypto/bn/bn_recp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_recp.c,v 1.15 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: bn_recp.c,v 1.16 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -134,7 +134,6 @@ BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
134 134
135err: 135err:
136 BN_CTX_end(ctx); 136 BN_CTX_end(ctx);
137 bn_check_top(r);
138 return (ret); 137 return (ret);
139} 138}
140 139
@@ -228,8 +227,6 @@ BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp,
228 227
229err: 228err:
230 BN_CTX_end(ctx); 229 BN_CTX_end(ctx);
231 bn_check_top(dv);
232 bn_check_top(rem);
233 return (ret); 230 return (ret);
234} 231}
235 232
@@ -257,7 +254,6 @@ BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
257 ret = len; 254 ret = len;
258 255
259err: 256err:
260 bn_check_top(r);
261 BN_CTX_end(ctx); 257 BN_CTX_end(ctx);
262 return (ret); 258 return (ret);
263} 259}
diff --git a/src/lib/libcrypto/bn/bn_shift.c b/src/lib/libcrypto/bn/bn_shift.c
index e2612d1e9d..6dbaffb194 100644
--- a/src/lib/libcrypto/bn/bn_shift.c
+++ b/src/lib/libcrypto/bn/bn_shift.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_shift.c,v 1.15 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_shift.c,v 1.16 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -69,8 +69,6 @@ BN_lshift1(BIGNUM *r, const BIGNUM *a)
69 BN_ULONG *ap, *rp, t, c; 69 BN_ULONG *ap, *rp, t, c;
70 int i; 70 int i;
71 71
72 bn_check_top(r);
73 bn_check_top(a);
74 72
75 if (r != a) { 73 if (r != a) {
76 r->neg = a->neg; 74 r->neg = a->neg;
@@ -93,7 +91,6 @@ BN_lshift1(BIGNUM *r, const BIGNUM *a)
93 *rp = 1; 91 *rp = 1;
94 r->top++; 92 r->top++;
95 } 93 }
96 bn_check_top(r);
97 return (1); 94 return (1);
98} 95}
99 96
@@ -103,8 +100,6 @@ BN_rshift1(BIGNUM *r, const BIGNUM *a)
103 BN_ULONG *ap, *rp, t, c; 100 BN_ULONG *ap, *rp, t, c;
104 int i, j; 101 int i, j;
105 102
106 bn_check_top(r);
107 bn_check_top(a);
108 103
109 if (BN_is_zero(a)) { 104 if (BN_is_zero(a)) {
110 BN_zero(r); 105 BN_zero(r);
@@ -129,7 +124,6 @@ BN_rshift1(BIGNUM *r, const BIGNUM *a)
129 c = (t & 1) ? BN_TBIT : 0; 124 c = (t & 1) ? BN_TBIT : 0;
130 } 125 }
131 r->top = j; 126 r->top = j;
132 bn_check_top(r);
133 return (1); 127 return (1);
134} 128}
135 129
@@ -145,8 +139,6 @@ BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
145 return 0; 139 return 0;
146 } 140 }
147 141
148 bn_check_top(r);
149 bn_check_top(a);
150 142
151 r->neg = a->neg; 143 r->neg = a->neg;
152 nw = n / BN_BITS2; 144 nw = n / BN_BITS2;
@@ -171,7 +163,6 @@ BN_lshift(BIGNUM *r, const BIGNUM *a, int n)
171 t[i]=0;*/ 163 t[i]=0;*/
172 r->top = a->top + nw + 1; 164 r->top = a->top + nw + 1;
173 bn_correct_top(r); 165 bn_correct_top(r);
174 bn_check_top(r);
175 return (1); 166 return (1);
176} 167}
177 168
@@ -187,8 +178,6 @@ BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
187 return 0; 178 return 0;
188 } 179 }
189 180
190 bn_check_top(r);
191 bn_check_top(a);
192 181
193 nw = n / BN_BITS2; 182 nw = n / BN_BITS2;
194 rb = n % BN_BITS2; 183 rb = n % BN_BITS2;
@@ -225,6 +214,5 @@ BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
225 if ((l = (l >> rb) & BN_MASK2)) 214 if ((l = (l >> rb) & BN_MASK2))
226 *(t) = l; 215 *(t) = l;
227 } 216 }
228 bn_check_top(r);
229 return (1); 217 return (1);
230} 218}
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c
index 36b3965207..80070831ab 100644
--- a/src/lib/libcrypto/bn/bn_sqr.c
+++ b/src/lib/libcrypto/bn/bn_sqr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_sqr.c,v 1.14 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_sqr.c,v 1.15 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -72,7 +72,6 @@ BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
72#ifdef BN_COUNT 72#ifdef BN_COUNT
73 fprintf(stderr, "BN_sqr %d * %d\n", a->top, a->top); 73 fprintf(stderr, "BN_sqr %d * %d\n", a->top, a->top);
74#endif 74#endif
75 bn_check_top(a);
76 75
77 al = a->top; 76 al = a->top;
78 if (al <= 0) { 77 if (al <= 0) {
@@ -145,8 +144,6 @@ BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
145 ret = 1; 144 ret = 1;
146 145
147err: 146err:
148 bn_check_top(rr);
149 bn_check_top(tmp);
150 BN_CTX_end(ctx); 147 BN_CTX_end(ctx);
151 return (ret); 148 return (ret);
152} 149}
diff --git a/src/lib/libcrypto/bn/bn_sqrt.c b/src/lib/libcrypto/bn/bn_sqrt.c
index d9ab545496..e964c578e3 100644
--- a/src/lib/libcrypto/bn/bn_sqrt.c
+++ b/src/lib/libcrypto/bn/bn_sqrt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_sqrt.c,v 1.12 2022/11/19 12:25:23 tb Exp $ */ 1/* $OpenBSD: bn_sqrt.c,v 1.13 2022/11/26 13:56:33 jsing Exp $ */
2/* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2/* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * and Bodo Moeller for the OpenSSL project. */ 3 * and Bodo Moeller for the OpenSSL project. */
4/* ==================================================================== 4/* ====================================================================
@@ -87,7 +87,6 @@ BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
87 BN_free(ret); 87 BN_free(ret);
88 return NULL; 88 return NULL;
89 } 89 }
90 bn_check_top(ret);
91 return ret; 90 return ret;
92 } 91 }
93 92
@@ -105,7 +104,6 @@ BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
105 BN_free(ret); 104 BN_free(ret);
106 return NULL; 105 return NULL;
107 } 106 }
108 bn_check_top(ret);
109 return ret; 107 return ret;
110 } 108 }
111 109
@@ -407,6 +405,5 @@ end:
407 ret = NULL; 405 ret = NULL;
408 } 406 }
409 BN_CTX_end(ctx); 407 BN_CTX_end(ctx);
410 bn_check_top(ret);
411 return ret; 408 return ret;
412} 409}
diff --git a/src/lib/libcrypto/bn/bn_word.c b/src/lib/libcrypto/bn/bn_word.c
index 683668c52d..9719808520 100644
--- a/src/lib/libcrypto/bn/bn_word.c
+++ b/src/lib/libcrypto/bn/bn_word.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_word.c,v 1.14 2022/11/24 01:30:01 jsing Exp $ */ 1/* $OpenBSD: bn_word.c,v 1.15 2022/11/26 13:56:33 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -87,7 +87,6 @@ BN_mod_word(const BIGNUM *a, BN_ULONG w)
87 } 87 }
88#endif 88#endif
89 89
90 bn_check_top(a);
91 w &= BN_MASK2; 90 w &= BN_MASK2;
92 for (i = a->top - 1; i >= 0; i--) { 91 for (i = a->top - 1; i >= 0; i--) {
93#ifndef BN_LLONG 92#ifndef BN_LLONG
@@ -108,7 +107,6 @@ BN_div_word(BIGNUM *a, BN_ULONG w)
108 BN_ULONG ret = 0; 107 BN_ULONG ret = 0;
109 int i, j; 108 int i, j;
110 109
111 bn_check_top(a);
112 w &= BN_MASK2; 110 w &= BN_MASK2;
113 111
114 if (!w) 112 if (!w)
@@ -134,7 +132,6 @@ BN_div_word(BIGNUM *a, BN_ULONG w)
134 if ((a->top > 0) && (a->d[a->top - 1] == 0)) 132 if ((a->top > 0) && (a->d[a->top - 1] == 0))
135 a->top--; 133 a->top--;
136 ret >>= j; 134 ret >>= j;
137 bn_check_top(a);
138 return (ret); 135 return (ret);
139} 136}
140 137
@@ -144,7 +141,6 @@ BN_add_word(BIGNUM *a, BN_ULONG w)
144 BN_ULONG l; 141 BN_ULONG l;
145 int i; 142 int i;
146 143
147 bn_check_top(a);
148 w &= BN_MASK2; 144 w &= BN_MASK2;
149 145
150 /* degenerate case: w is zero */ 146 /* degenerate case: w is zero */
@@ -171,7 +167,6 @@ BN_add_word(BIGNUM *a, BN_ULONG w)
171 a->top++; 167 a->top++;
172 a->d[i] = w; 168 a->d[i] = w;
173 } 169 }
174 bn_check_top(a);
175 return (1); 170 return (1);
176} 171}
177 172
@@ -180,7 +175,6 @@ BN_sub_word(BIGNUM *a, BN_ULONG w)
180{ 175{
181 int i; 176 int i;
182 177
183 bn_check_top(a);
184 w &= BN_MASK2; 178 w &= BN_MASK2;
185 179
186 /* degenerate case: w is zero */ 180 /* degenerate case: w is zero */
@@ -219,7 +213,6 @@ BN_sub_word(BIGNUM *a, BN_ULONG w)
219 } 213 }
220 if ((a->d[i] == 0) && (i == (a->top - 1))) 214 if ((a->d[i] == 0) && (i == (a->top - 1)))
221 a->top--; 215 a->top--;
222 bn_check_top(a);
223 return (1); 216 return (1);
224} 217}
225 218
@@ -228,7 +221,6 @@ BN_mul_word(BIGNUM *a, BN_ULONG w)
228{ 221{
229 BN_ULONG ll; 222 BN_ULONG ll;
230 223
231 bn_check_top(a);
232 w &= BN_MASK2; 224 w &= BN_MASK2;
233 if (a->top) { 225 if (a->top) {
234 if (w == 0) 226 if (w == 0)
@@ -242,6 +234,5 @@ BN_mul_word(BIGNUM *a, BN_ULONG w)
242 } 234 }
243 } 235 }
244 } 236 }
245 bn_check_top(a);
246 return (1); 237 return (1);
247} 238}