diff options
| author | tb <> | 2025-01-21 15:44:22 +0000 |
|---|---|---|
| committer | tb <> | 2025-01-21 15:44:22 +0000 |
| commit | 797cf6b23360385bf34a89fce562aa5cb0a0e978 (patch) | |
| tree | 0553e257e45ee4491e27d615f802c6508976e5c4 /src/lib/libcrypto/bn/bn_exp.c | |
| parent | d2ee6a6329cb6b037f3949082396fa72b53b5ee8 (diff) | |
| download | openbsd-797cf6b23360385bf34a89fce562aa5cb0a0e978.tar.gz openbsd-797cf6b23360385bf34a89fce562aa5cb0a0e978.tar.bz2 openbsd-797cf6b23360385bf34a89fce562aa5cb0a0e978.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_exp.c')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_exp.c | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c index c51296c3c3..8ff518e938 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.53 2024/04/10 14:58:06 beck Exp $ */ | 1 | /* $OpenBSD: bn_exp.c,v 1.54 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 | * |
| @@ -972,7 +972,7 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
| 972 | BIGNUM *aa, *q; | 972 | BIGNUM *aa, *q; |
| 973 | /* Table of variables obtained from 'ctx' */ | 973 | /* Table of variables obtained from 'ctx' */ |
| 974 | BIGNUM *val[TABLE_SIZE]; | 974 | BIGNUM *val[TABLE_SIZE]; |
| 975 | BN_RECP_CTX recp; | 975 | BN_RECP_CTX *recp = NULL; |
| 976 | int ret = 0; | 976 | int ret = 0; |
| 977 | 977 | ||
| 978 | if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) { | 978 | if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) { |
| @@ -992,8 +992,6 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
| 992 | return ret; | 992 | return ret; |
| 993 | } | 993 | } |
| 994 | 994 | ||
| 995 | BN_RECP_CTX_init(&recp); | ||
| 996 | |||
| 997 | BN_CTX_start(ctx); | 995 | BN_CTX_start(ctx); |
| 998 | if ((aa = BN_CTX_get(ctx)) == NULL) | 996 | if ((aa = BN_CTX_get(ctx)) == NULL) |
| 999 | goto err; | 997 | goto err; |
| @@ -1007,10 +1005,10 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
| 1007 | if (!bn_copy(aa, m)) | 1005 | if (!bn_copy(aa, m)) |
| 1008 | goto err; | 1006 | goto err; |
| 1009 | aa->neg = 0; | 1007 | aa->neg = 0; |
| 1010 | if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0) | 1008 | if ((recp = BN_RECP_CTX_create(aa)) == 0) |
| 1011 | goto err; | 1009 | goto err; |
| 1012 | } else { | 1010 | } else { |
| 1013 | if (BN_RECP_CTX_set(&recp, m, ctx) <= 0) | 1011 | if ((recp = BN_RECP_CTX_create(m)) == 0) |
| 1014 | goto err; | 1012 | goto err; |
| 1015 | } | 1013 | } |
| 1016 | 1014 | ||
| @@ -1025,13 +1023,13 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
| 1025 | 1023 | ||
| 1026 | window = BN_window_bits_for_exponent_size(bits); | 1024 | window = BN_window_bits_for_exponent_size(bits); |
| 1027 | if (window > 1) { | 1025 | if (window > 1) { |
| 1028 | if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx)) | 1026 | if (!BN_mod_mul_reciprocal(aa, val[0], val[0], recp, ctx)) |
| 1029 | goto err; | 1027 | goto err; |
| 1030 | j = 1 << (window - 1); | 1028 | j = 1 << (window - 1); |
| 1031 | for (i = 1; i < j; i++) { | 1029 | for (i = 1; i < j; i++) { |
| 1032 | if (((val[i] = BN_CTX_get(ctx)) == NULL) || | 1030 | if (((val[i] = BN_CTX_get(ctx)) == NULL) || |
| 1033 | !BN_mod_mul_reciprocal(val[i], val[i - 1], | 1031 | !BN_mod_mul_reciprocal(val[i], val[i - 1], |
| 1034 | aa, &recp, ctx)) | 1032 | aa, recp, ctx)) |
| 1035 | goto err; | 1033 | goto err; |
| 1036 | } | 1034 | } |
| 1037 | } | 1035 | } |
| @@ -1049,7 +1047,7 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
| 1049 | for (;;) { | 1047 | for (;;) { |
| 1050 | if (BN_is_bit_set(q, wstart) == 0) { | 1048 | if (BN_is_bit_set(q, wstart) == 0) { |
| 1051 | if (!start) | 1049 | if (!start) |
| 1052 | if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx)) | 1050 | if (!BN_mod_mul_reciprocal(r, r, r, recp, ctx)) |
| 1053 | goto err; | 1051 | goto err; |
| 1054 | if (wstart == 0) | 1052 | if (wstart == 0) |
| 1055 | break; | 1053 | break; |
| @@ -1078,12 +1076,12 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
| 1078 | /* add the 'bytes above' */ | 1076 | /* add the 'bytes above' */ |
| 1079 | if (!start) | 1077 | if (!start) |
| 1080 | for (i = 0; i < j; i++) { | 1078 | for (i = 0; i < j; i++) { |
| 1081 | if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx)) | 1079 | if (!BN_mod_mul_reciprocal(r, r, r, recp, ctx)) |
| 1082 | goto err; | 1080 | goto err; |
| 1083 | } | 1081 | } |
| 1084 | 1082 | ||
| 1085 | /* wvalue will be an odd number < 2^window */ | 1083 | /* wvalue will be an odd number < 2^window */ |
| 1086 | if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx)) | 1084 | if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], recp, ctx)) |
| 1087 | goto err; | 1085 | goto err; |
| 1088 | 1086 | ||
| 1089 | /* move the 'window' down further */ | 1087 | /* move the 'window' down further */ |
| @@ -1099,7 +1097,7 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
| 1099 | 1097 | ||
| 1100 | err: | 1098 | err: |
| 1101 | BN_CTX_end(ctx); | 1099 | BN_CTX_end(ctx); |
| 1102 | BN_RECP_CTX_free(&recp); | 1100 | BN_RECP_CTX_free(recp); |
| 1103 | 1101 | ||
| 1104 | return ret; | 1102 | return ret; |
| 1105 | } | 1103 | } |
