diff options
| author | tb <> | 2025-01-06 12:35:27 +0000 |
|---|---|---|
| committer | tb <> | 2025-01-06 12:35:27 +0000 |
| commit | a17554dd266dd401e31d7f1f76fdd12969d11aaf (patch) | |
| tree | 4d972561c740c5722770d1761d303404400ad414 /src | |
| parent | b49ed379be5d4e78d2fe9ffefb79f0ec0ddff691 (diff) | |
| download | openbsd-a17554dd266dd401e31d7f1f76fdd12969d11aaf.tar.gz openbsd-a17554dd266dd401e31d7f1f76fdd12969d11aaf.tar.bz2 openbsd-a17554dd266dd401e31d7f1f76fdd12969d11aaf.zip | |
Shuffle functions into a more sensible order
BN_reciprocal() is only called by BN_div_recp() which in turn is only
called by BN_mod_mul_reciprocal(). So use this order and make the first
two static.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_local.h | 6 | ||||
| -rw-r--r-- | src/lib/libcrypto/bn/bn_recp.c | 75 |
2 files changed, 39 insertions, 42 deletions
diff --git a/src/lib/libcrypto/bn/bn_local.h b/src/lib/libcrypto/bn/bn_local.h index 58b5d54903..bfdff56b0e 100644 --- a/src/lib/libcrypto/bn/bn_local.h +++ b/src/lib/libcrypto/bn/bn_local.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_local.h,v 1.43 2024/04/16 13:07:14 jsing Exp $ */ | 1 | /* $OpenBSD: bn_local.h,v 1.44 2025/01/06 12:35:27 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 | * |
| @@ -280,8 +280,6 @@ int bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc); | |||
| 280 | 280 | ||
| 281 | void BN_init(BIGNUM *); | 281 | void BN_init(BIGNUM *); |
| 282 | 282 | ||
| 283 | int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx); | ||
| 284 | |||
| 285 | void BN_RECP_CTX_init(BN_RECP_CTX *recp); | 283 | void BN_RECP_CTX_init(BN_RECP_CTX *recp); |
| 286 | BN_RECP_CTX *BN_RECP_CTX_new(void); | 284 | BN_RECP_CTX *BN_RECP_CTX_new(void); |
| 287 | void BN_RECP_CTX_free(BN_RECP_CTX *recp); | 285 | void BN_RECP_CTX_free(BN_RECP_CTX *recp); |
| @@ -290,8 +288,6 @@ int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, | |||
| 290 | BN_RECP_CTX *recp, BN_CTX *ctx); | 288 | BN_RECP_CTX *recp, BN_CTX *ctx); |
| 291 | int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 289 | int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
| 292 | const BIGNUM *m, BN_CTX *ctx); | 290 | const BIGNUM *m, BN_CTX *ctx); |
| 293 | int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, | ||
| 294 | BN_RECP_CTX *recp, BN_CTX *ctx); | ||
| 295 | 291 | ||
| 296 | /* Explicitly const time / non-const time versions for internal use */ | 292 | /* Explicitly const time / non-const time versions for internal use */ |
| 297 | int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 293 | int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
diff --git a/src/lib/libcrypto/bn/bn_recp.c b/src/lib/libcrypto/bn/bn_recp.c index 35390e30d4..c567a5b017 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.19 2023/03/27 10:25:02 tb Exp $ */ | 1 | /* $OpenBSD: bn_recp.c,v 1.20 2025/01/06 12:35:27 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 | * |
| @@ -107,37 +107,35 @@ BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx) | |||
| 107 | return (1); | 107 | return (1); |
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | int | 110 | /* len is the expected size of the result |
| 111 | BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, | 111 | * We actually calculate with an extra word of precision, so |
| 112 | BN_RECP_CTX *recp, BN_CTX *ctx) | 112 | * we can do faster division if the remainder is not required. |
| 113 | */ | ||
| 114 | /* r := 2^len / m */ | ||
| 115 | static int | ||
| 116 | BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) | ||
| 113 | { | 117 | { |
| 114 | int ret = 0; | 118 | int ret = -1; |
| 115 | BIGNUM *a; | 119 | BIGNUM *t; |
| 116 | const BIGNUM *ca; | ||
| 117 | 120 | ||
| 118 | BN_CTX_start(ctx); | 121 | BN_CTX_start(ctx); |
| 119 | if ((a = BN_CTX_get(ctx)) == NULL) | 122 | if ((t = BN_CTX_get(ctx)) == NULL) |
| 120 | goto err; | 123 | goto err; |
| 121 | if (y != NULL) { | ||
| 122 | if (x == y) { | ||
| 123 | if (!BN_sqr(a, x, ctx)) | ||
| 124 | goto err; | ||
| 125 | } else { | ||
| 126 | if (!BN_mul(a, x, y, ctx)) | ||
| 127 | goto err; | ||
| 128 | } | ||
| 129 | ca = a; | ||
| 130 | } else | ||
| 131 | ca = x; /* Just do the mod */ | ||
| 132 | 124 | ||
| 133 | ret = BN_div_recp(NULL, r, ca, recp, ctx); | 125 | if (!BN_set_bit(t, len)) |
| 126 | goto err; | ||
| 127 | |||
| 128 | if (!BN_div_ct(r, NULL, t,m, ctx)) | ||
| 129 | goto err; | ||
| 130 | |||
| 131 | ret = len; | ||
| 134 | 132 | ||
| 135 | err: | 133 | err: |
| 136 | BN_CTX_end(ctx); | 134 | BN_CTX_end(ctx); |
| 137 | return (ret); | 135 | return (ret); |
| 138 | } | 136 | } |
| 139 | 137 | ||
| 140 | int | 138 | static int |
| 141 | BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, | 139 | BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, BN_RECP_CTX *recp, |
| 142 | BN_CTX *ctx) | 140 | BN_CTX *ctx) |
| 143 | { | 141 | { |
| @@ -231,28 +229,31 @@ err: | |||
| 231 | return (ret); | 229 | return (ret); |
| 232 | } | 230 | } |
| 233 | 231 | ||
| 234 | /* len is the expected size of the result | 232 | |
| 235 | * We actually calculate with an extra word of precision, so | ||
| 236 | * we can do faster division if the remainder is not required. | ||
| 237 | */ | ||
| 238 | /* r := 2^len / m */ | ||
| 239 | int | 233 | int |
| 240 | BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) | 234 | BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, |
| 235 | BN_RECP_CTX *recp, BN_CTX *ctx) | ||
| 241 | { | 236 | { |
| 242 | int ret = -1; | 237 | int ret = 0; |
| 243 | BIGNUM *t; | 238 | BIGNUM *a; |
| 239 | const BIGNUM *ca; | ||
| 244 | 240 | ||
| 245 | BN_CTX_start(ctx); | 241 | BN_CTX_start(ctx); |
| 246 | if ((t = BN_CTX_get(ctx)) == NULL) | 242 | if ((a = BN_CTX_get(ctx)) == NULL) |
| 247 | goto err; | ||
| 248 | |||
| 249 | if (!BN_set_bit(t, len)) | ||
| 250 | goto err; | ||
| 251 | |||
| 252 | if (!BN_div_ct(r, NULL, t,m, ctx)) | ||
| 253 | goto err; | 243 | goto err; |
| 244 | if (y != NULL) { | ||
| 245 | if (x == y) { | ||
| 246 | if (!BN_sqr(a, x, ctx)) | ||
| 247 | goto err; | ||
| 248 | } else { | ||
| 249 | if (!BN_mul(a, x, y, ctx)) | ||
| 250 | goto err; | ||
| 251 | } | ||
| 252 | ca = a; | ||
| 253 | } else | ||
| 254 | ca = x; /* Just do the mod */ | ||
| 254 | 255 | ||
| 255 | ret = len; | 256 | ret = BN_div_recp(NULL, r, ca, recp, ctx); |
| 256 | 257 | ||
| 257 | err: | 258 | err: |
| 258 | BN_CTX_end(ctx); | 259 | BN_CTX_end(ctx); |
