diff options
author | tb <> | 2025-01-21 15:44:22 +0000 |
---|---|---|
committer | tb <> | 2025-01-21 15:44:22 +0000 |
commit | bc6dc1ff28111bbeaccd1e4fbf2ba86016af9563 (patch) | |
tree | 0553e257e45ee4491e27d615f802c6508976e5c4 /src/lib/libcrypto/bn/bn_recp.c | |
parent | 17178449b414247a16e12c6127a0d4007ff5790b (diff) | |
download | openbsd-bc6dc1ff28111bbeaccd1e4fbf2ba86016af9563.tar.gz openbsd-bc6dc1ff28111bbeaccd1e4fbf2ba86016af9563.tar.bz2 openbsd-bc6dc1ff28111bbeaccd1e4fbf2ba86016af9563.zip |
Move BN_RECP_CTX to the heap
This introduces a BN_RECP_CTX_create() function that allocates and
populates the BN_RECP_CTX in a single call, without taking an unused
BN_CTX argument.
At the same time, make the N and Nr members BIGNUMs on the heap which
are allocated by BN_RECP_CTX_create() and freed by BN_RECP_CTX_free()
and remove the unnecessary flags argument.
Garbage collect the now unused BN_RECP_CTX_{new,init,set}().
ok jsing
Diffstat (limited to 'src/lib/libcrypto/bn/bn_recp.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_recp.c | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c index 44c5b05e4d..e7484f9f4b 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.25 2025/01/08 20:21:28 tb Exp $ */ | 1 | /* $OpenBSD: bn_recp.c,v 1.26 2025/01/21 15:44:22 tb 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 | * |
@@ -62,26 +62,34 @@ | |||
62 | 62 | ||
63 | #include "bn_local.h" | 63 | #include "bn_local.h" |
64 | 64 | ||
65 | void | 65 | struct bn_recp_ctx_st { |
66 | BN_RECP_CTX_init(BN_RECP_CTX *recp) | 66 | BIGNUM *N; /* the divisor */ |
67 | { | 67 | BIGNUM *Nr; /* the reciprocal 2^shift / N */ |
68 | BN_init(&recp->N); | 68 | int num_bits; /* number of bits in N */ |
69 | BN_init(&recp->Nr); | 69 | int shift; |
70 | recp->num_bits = 0; | 70 | } /* BN_RECP_CTX */; |
71 | recp->flags = 0; | ||
72 | } | ||
73 | 71 | ||
74 | BN_RECP_CTX * | 72 | BN_RECP_CTX * |
75 | BN_RECP_CTX_new(void) | 73 | BN_RECP_CTX_create(const BIGNUM *N) |
76 | { | 74 | { |
77 | BN_RECP_CTX *ret; | 75 | BN_RECP_CTX *recp; |
78 | 76 | ||
79 | if ((ret = malloc(sizeof(BN_RECP_CTX))) == NULL) | 77 | if ((recp = calloc(1, sizeof(*recp))) == NULL) |
80 | return NULL; | 78 | goto err; |
81 | 79 | ||
82 | BN_RECP_CTX_init(ret); | 80 | if ((recp->N = BN_dup(N)) == NULL) |
83 | ret->flags = BN_FLG_MALLOCED; | 81 | goto err; |
84 | return ret; | 82 | recp->num_bits = BN_num_bits(recp->N); |
83 | |||
84 | if ((recp->Nr = BN_new()) == NULL) | ||
85 | goto err; | ||
86 | |||
87 | return recp; | ||
88 | |||
89 | err: | ||
90 | BN_RECP_CTX_free(recp); | ||
91 | |||
92 | return NULL; | ||
85 | } | 93 | } |
86 | 94 | ||
87 | void | 95 | void |
@@ -90,23 +98,9 @@ BN_RECP_CTX_free(BN_RECP_CTX *recp) | |||
90 | if (recp == NULL) | 98 | if (recp == NULL) |
91 | return; | 99 | return; |
92 | 100 | ||
93 | BN_free(&recp->N); | 101 | BN_free(recp->N); |
94 | BN_free(&recp->Nr); | 102 | BN_free(recp->Nr); |
95 | if (recp->flags & BN_FLG_MALLOCED) | 103 | freezero(recp, sizeof(*recp)); |
96 | free(recp); | ||
97 | } | ||
98 | |||
99 | int | ||
100 | BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx) | ||
101 | { | ||
102 | if (!bn_copy(&recp->N, d)) | ||
103 | return 0; | ||
104 | recp->num_bits = BN_num_bits(&recp->N); | ||
105 | |||
106 | BN_zero(&recp->Nr); | ||
107 | recp->shift = 0; | ||
108 | |||
109 | return 1; | ||
110 | } | 104 | } |
111 | 105 | ||
112 | /* len is the expected size of the result | 106 | /* len is the expected size of the result |
@@ -158,7 +152,7 @@ BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | |||
158 | if (a == NULL || b == NULL || d == NULL || r == NULL) | 152 | if (a == NULL || b == NULL || d == NULL || r == NULL) |
159 | goto err; | 153 | goto err; |
160 | 154 | ||
161 | if (BN_ucmp(m, &recp->N) < 0) { | 155 | if (BN_ucmp(m, recp->N) < 0) { |
162 | BN_zero(d); | 156 | BN_zero(d); |
163 | if (!bn_copy(r, m)) { | 157 | if (!bn_copy(r, m)) { |
164 | BN_CTX_end(ctx); | 158 | BN_CTX_end(ctx); |
@@ -182,7 +176,7 @@ BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | |||
182 | 176 | ||
183 | /* Nr := round(2^i / N) */ | 177 | /* Nr := round(2^i / N) */ |
184 | if (i != recp->shift) | 178 | if (i != recp->shift) |
185 | recp->shift = BN_reciprocal(&recp->Nr, &recp->N, i, ctx); | 179 | recp->shift = BN_reciprocal(recp->Nr, recp->N, i, ctx); |
186 | 180 | ||
187 | /* BN_reciprocal returns i, or -1 for an error */ | 181 | /* BN_reciprocal returns i, or -1 for an error */ |
188 | if (recp->shift == -1) | 182 | if (recp->shift == -1) |
@@ -195,13 +189,13 @@ BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | |||
195 | */ | 189 | */ |
196 | if (!BN_rshift(a, m, recp->num_bits)) | 190 | if (!BN_rshift(a, m, recp->num_bits)) |
197 | goto err; | 191 | goto err; |
198 | if (!BN_mul(b, a, &recp->Nr, ctx)) | 192 | if (!BN_mul(b, a, recp->Nr, ctx)) |
199 | goto err; | 193 | goto err; |
200 | if (!BN_rshift(d, b, i - recp->num_bits)) | 194 | if (!BN_rshift(d, b, i - recp->num_bits)) |
201 | goto err; | 195 | goto err; |
202 | d->neg = 0; | 196 | d->neg = 0; |
203 | 197 | ||
204 | if (!BN_mul(b, &recp->N, d, ctx)) | 198 | if (!BN_mul(b, recp->N, d, ctx)) |
205 | goto err; | 199 | goto err; |
206 | if (!BN_usub(r, m, b)) | 200 | if (!BN_usub(r, m, b)) |
207 | goto err; | 201 | goto err; |
@@ -209,12 +203,12 @@ BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | |||
209 | 203 | ||
210 | #if 1 | 204 | #if 1 |
211 | j = 0; | 205 | j = 0; |
212 | while (BN_ucmp(r, &recp->N) >= 0) { | 206 | while (BN_ucmp(r, recp->N) >= 0) { |
213 | if (j++ > 2) { | 207 | if (j++ > 2) { |
214 | BNerror(BN_R_BAD_RECIPROCAL); | 208 | BNerror(BN_R_BAD_RECIPROCAL); |
215 | goto err; | 209 | goto err; |
216 | } | 210 | } |
217 | if (!BN_usub(r, r, &recp->N)) | 211 | if (!BN_usub(r, r, recp->N)) |
218 | goto err; | 212 | goto err; |
219 | if (!BN_add_word(d, 1)) | 213 | if (!BN_add_word(d, 1)) |
220 | goto err; | 214 | goto err; |
@@ -222,7 +216,7 @@ BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | |||
222 | #endif | 216 | #endif |
223 | 217 | ||
224 | BN_set_negative(r, m->neg); | 218 | BN_set_negative(r, m->neg); |
225 | BN_set_negative(d, m->neg ^ recp->N.neg); | 219 | BN_set_negative(d, m->neg ^ recp->N->neg); |
226 | 220 | ||
227 | ret = 1; | 221 | ret = 1; |
228 | 222 | ||