diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_sqr.c')
-rw-r--r-- | src/lib/libcrypto/bn/bn_sqr.c | 108 |
1 files changed, 1 insertions, 107 deletions
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c index d5da775200..d414800feb 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.29 2023/03/30 14:28:56 tb Exp $ */ | 1 | /* $OpenBSD: bn_sqr.c,v 1.30 2023/04/19 10:51:22 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 | * |
@@ -228,90 +228,6 @@ bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) | |||
228 | bn_add_words(r, r, tmp, max); | 228 | bn_add_words(r, r, tmp, max); |
229 | } | 229 | } |
230 | 230 | ||
231 | #ifdef BN_RECURSION | ||
232 | /* r is 2*n words in size, | ||
233 | * a and b are both n words in size. (There's not actually a 'b' here ...) | ||
234 | * n must be a power of 2. | ||
235 | * We multiply and return the result. | ||
236 | * t must be 2*n words in size | ||
237 | * We calculate | ||
238 | * a[0]*b[0] | ||
239 | * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0]) | ||
240 | * a[1]*b[1] | ||
241 | */ | ||
242 | void | ||
243 | bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) | ||
244 | { | ||
245 | int n = n2 / 2; | ||
246 | int zero, c1; | ||
247 | BN_ULONG ln, lo, *p; | ||
248 | |||
249 | if (n2 == 4) { | ||
250 | bn_sqr_comba4(r, a); | ||
251 | return; | ||
252 | } else if (n2 == 8) { | ||
253 | bn_sqr_comba8(r, a); | ||
254 | return; | ||
255 | } | ||
256 | if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) { | ||
257 | bn_sqr_normal(r, a, n2, t); | ||
258 | return; | ||
259 | } | ||
260 | /* r=(a[0]-a[1])*(a[1]-a[0]) */ | ||
261 | c1 = bn_cmp_words(a, &(a[n]), n); | ||
262 | zero = 0; | ||
263 | if (c1 > 0) | ||
264 | bn_sub_words(t, a, &(a[n]), n); | ||
265 | else if (c1 < 0) | ||
266 | bn_sub_words(t, &(a[n]), a, n); | ||
267 | else | ||
268 | zero = 1; | ||
269 | |||
270 | /* The result will always be negative unless it is zero */ | ||
271 | p = &(t[n2*2]); | ||
272 | |||
273 | if (!zero) | ||
274 | bn_sqr_recursive(&(t[n2]), t, n, p); | ||
275 | else | ||
276 | memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG)); | ||
277 | bn_sqr_recursive(r, a, n, p); | ||
278 | bn_sqr_recursive(&(r[n2]), &(a[n]), n, p); | ||
279 | |||
280 | /* t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero | ||
281 | * r[10] holds (a[0]*b[0]) | ||
282 | * r[32] holds (b[1]*b[1]) | ||
283 | */ | ||
284 | |||
285 | c1 = (int)(bn_add_words(t, r, &(r[n2]), n2)); | ||
286 | |||
287 | /* t[32] is negative */ | ||
288 | c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2)); | ||
289 | |||
290 | /* t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1]) | ||
291 | * r[10] holds (a[0]*a[0]) | ||
292 | * r[32] holds (a[1]*a[1]) | ||
293 | * c1 holds the carry bits | ||
294 | */ | ||
295 | c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2)); | ||
296 | if (c1) { | ||
297 | p = &(r[n + n2]); | ||
298 | lo= *p; | ||
299 | ln = (lo + c1) & BN_MASK2; | ||
300 | *p = ln; | ||
301 | |||
302 | /* The overflow will stop before we over write | ||
303 | * words we should not overwrite */ | ||
304 | if (ln < (BN_ULONG)c1) { | ||
305 | do { | ||
306 | p++; | ||
307 | lo= *p; | ||
308 | ln = (lo + 1) & BN_MASK2; | ||
309 | *p = ln; | ||
310 | } while (ln == 0); | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | #endif | ||
315 | 231 | ||
316 | /* | 232 | /* |
317 | * bn_sqr() computes a * a, storing the result in r. The caller must ensure that | 233 | * bn_sqr() computes a * a, storing the result in r. The caller must ensure that |
@@ -330,31 +246,9 @@ bn_sqr(BIGNUM *r, const BIGNUM *a, int rn, BN_CTX *ctx) | |||
330 | if ((tmp = BN_CTX_get(ctx)) == NULL) | 246 | if ((tmp = BN_CTX_get(ctx)) == NULL) |
331 | goto err; | 247 | goto err; |
332 | 248 | ||
333 | #if defined(BN_RECURSION) | ||
334 | if (a->top < BN_SQR_RECURSIVE_SIZE_NORMAL) { | ||
335 | BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2]; | ||
336 | bn_sqr_normal(r->d, a->d, a->top, t); | ||
337 | } else { | ||
338 | int j, k; | ||
339 | |||
340 | j = BN_num_bits_word((BN_ULONG)a->top); | ||
341 | j = 1 << (j - 1); | ||
342 | k = j + j; | ||
343 | if (a->top == j) { | ||
344 | if (!bn_wexpand(tmp, k * 2)) | ||
345 | goto err; | ||
346 | bn_sqr_recursive(r->d, a->d, a->top, tmp->d); | ||
347 | } else { | ||
348 | if (!bn_wexpand(tmp, rn)) | ||
349 | goto err; | ||
350 | bn_sqr_normal(r->d, a->d, a->top, tmp->d); | ||
351 | } | ||
352 | } | ||
353 | #else | ||
354 | if (!bn_wexpand(tmp, rn)) | 249 | if (!bn_wexpand(tmp, rn)) |
355 | goto err; | 250 | goto err; |
356 | bn_sqr_normal(r->d, a->d, a->top, tmp->d); | 251 | bn_sqr_normal(r->d, a->d, a->top, tmp->d); |
357 | #endif | ||
358 | 252 | ||
359 | ret = 1; | 253 | ret = 1; |
360 | 254 | ||