diff options
| author | jsing <> | 2023-04-14 11:04:24 +0000 |
|---|---|---|
| committer | jsing <> | 2023-04-14 11:04:24 +0000 |
| commit | 6f526230f296603cd753cd19889d3decae20a3aa (patch) | |
| tree | 0f764786f9e25280c167a02154679325aee0ea70 /src | |
| parent | 3e308f6a6cd5c9ecce0f92a26ab43e5f3caa6a0c (diff) | |
| download | openbsd-6f526230f296603cd753cd19889d3decae20a3aa.tar.gz openbsd-6f526230f296603cd753cd19889d3decae20a3aa.tar.bz2 openbsd-6f526230f296603cd753cd19889d3decae20a3aa.zip | |
Provide and use bn_copy_words() in BN_copy().
This is simpler than the current code, while still being well optimised by
compilers, across a range of architectures. In many cases we even get a
performance gain for the BN sizes that we primarily care about.
Joint work with tb@
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_lib.c | 46 |
1 files changed, 15 insertions, 31 deletions
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c index 3ca2b7f14b..f25caa04af 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.80 2023/04/01 12:44:56 tb Exp $ */ | 1 | /* $OpenBSD: bn_lib.c,v 1.81 2023/04/14 11:04:24 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 | * |
| @@ -344,50 +344,34 @@ BN_dup(const BIGNUM *a) | |||
| 344 | return t; | 344 | return t; |
| 345 | } | 345 | } |
| 346 | 346 | ||
| 347 | static inline void | ||
| 348 | bn_copy_words(BN_ULONG *ap, const BN_ULONG *bp, int n) | ||
| 349 | { | ||
| 350 | while (n > 0) { | ||
| 351 | ap[0] = bp[0]; | ||
| 352 | ap++; | ||
| 353 | bp++; | ||
| 354 | n--; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 347 | BIGNUM * | 358 | BIGNUM * |
| 348 | BN_copy(BIGNUM *a, const BIGNUM *b) | 359 | BN_copy(BIGNUM *a, const BIGNUM *b) |
| 349 | { | 360 | { |
| 350 | int i; | ||
| 351 | BN_ULONG *A; | ||
| 352 | const BN_ULONG *B; | ||
| 353 | |||
| 354 | |||
| 355 | if (a == b) | 361 | if (a == b) |
| 356 | return (a); | 362 | return (a); |
| 363 | |||
| 357 | if (!bn_wexpand(a, b->top)) | 364 | if (!bn_wexpand(a, b->top)) |
| 358 | return (NULL); | 365 | return (NULL); |
| 359 | 366 | ||
| 360 | #if 1 | 367 | bn_copy_words(a->d, b->d, b->top); |
| 361 | A = a->d; | ||
| 362 | B = b->d; | ||
| 363 | for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { | ||
| 364 | BN_ULONG a0, a1, a2, a3; | ||
| 365 | a0 = B[0]; | ||
| 366 | a1 = B[1]; | ||
| 367 | a2 = B[2]; | ||
| 368 | a3 = B[3]; | ||
| 369 | A[0] = a0; | ||
| 370 | A[1] = a1; | ||
| 371 | A[2] = a2; | ||
| 372 | A[3] = a3; | ||
| 373 | } | ||
| 374 | switch (b->top & 3) { | ||
| 375 | case 3: | ||
| 376 | A[2] = B[2]; | ||
| 377 | case 2: | ||
| 378 | A[1] = B[1]; | ||
| 379 | case 1: | ||
| 380 | A[0] = B[0]; | ||
| 381 | } | ||
| 382 | #else | ||
| 383 | memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); | ||
| 384 | #endif | ||
| 385 | 368 | ||
| 386 | /* Copy constant time flag from b, but make it sticky on a. */ | 369 | /* Copy constant time flag from b, but make it sticky on a. */ |
| 387 | a->flags |= b->flags & BN_FLG_CONSTTIME; | 370 | a->flags |= b->flags & BN_FLG_CONSTTIME; |
| 388 | 371 | ||
| 389 | a->top = b->top; | 372 | a->top = b->top; |
| 390 | a->neg = b->neg; | 373 | a->neg = b->neg; |
| 374 | |||
| 391 | return (a); | 375 | return (a); |
| 392 | } | 376 | } |
| 393 | 377 | ||
