/src/openssl30/crypto/bn/bn_recp.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | #include "internal/cryptlib.h" | 
| 11 |  | #include "bn_local.h" | 
| 12 |  |  | 
| 13 |  | void BN_RECP_CTX_init(BN_RECP_CTX *recp) | 
| 14 | 6.37k | { | 
| 15 | 6.37k |     memset(recp, 0, sizeof(*recp)); | 
| 16 | 6.37k |     bn_init(&(recp->N)); | 
| 17 | 6.37k |     bn_init(&(recp->Nr)); | 
| 18 | 6.37k | } | 
| 19 |  |  | 
| 20 |  | BN_RECP_CTX *BN_RECP_CTX_new(void) | 
| 21 | 0 | { | 
| 22 | 0 |     BN_RECP_CTX *ret; | 
| 23 |  | 
 | 
| 24 | 0 |     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { | 
| 25 | 0 |         ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE); | 
| 26 | 0 |         return NULL; | 
| 27 | 0 |     } | 
| 28 |  |  | 
| 29 | 0 |     bn_init(&(ret->N)); | 
| 30 | 0 |     bn_init(&(ret->Nr)); | 
| 31 | 0 |     ret->flags = BN_FLG_MALLOCED; | 
| 32 | 0 |     return ret; | 
| 33 | 0 | } | 
| 34 |  |  | 
| 35 |  | void BN_RECP_CTX_free(BN_RECP_CTX *recp) | 
| 36 | 6.37k | { | 
| 37 | 6.37k |     if (recp == NULL) | 
| 38 | 0 |         return; | 
| 39 | 6.37k |     BN_free(&recp->N); | 
| 40 | 6.37k |     BN_free(&recp->Nr); | 
| 41 | 6.37k |     if (recp->flags & BN_FLG_MALLOCED) | 
| 42 | 0 |         OPENSSL_free(recp); | 
| 43 | 6.37k | } | 
| 44 |  |  | 
| 45 |  | int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx) | 
| 46 | 4.75k | { | 
| 47 | 4.75k |     if (BN_is_zero(d) || !BN_copy(&(recp->N), d)) | 
| 48 | 0 |         return 0; | 
| 49 | 4.75k |     BN_zero(&(recp->Nr)); | 
| 50 | 4.75k |     recp->num_bits = BN_num_bits(d); | 
| 51 | 4.75k |     recp->shift = 0; | 
| 52 | 4.75k |     return 1; | 
| 53 | 4.75k | } | 
| 54 |  |  | 
| 55 |  | int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, | 
| 56 |  |                           BN_RECP_CTX *recp, BN_CTX *ctx) | 
| 57 | 4.59M | { | 
| 58 | 4.59M |     int ret = 0; | 
| 59 | 4.59M |     BIGNUM *a; | 
| 60 | 4.59M |     const BIGNUM *ca; | 
| 61 |  |  | 
| 62 | 4.59M |     BN_CTX_start(ctx); | 
| 63 | 4.59M |     if ((a = BN_CTX_get(ctx)) == NULL) | 
| 64 | 0 |         goto err; | 
| 65 | 4.59M |     if (y != NULL) { | 
| 66 | 4.59M |         if (x == y) { | 
| 67 | 4.21M |             if (!BN_sqr(a, x, ctx)) | 
| 68 | 0 |                 goto err; | 
| 69 | 4.21M |         } else { | 
| 70 | 383k |             if (!BN_mul(a, x, y, ctx)) | 
| 71 | 0 |                 goto err; | 
| 72 | 383k |         } | 
| 73 | 4.59M |         ca = a; | 
| 74 | 4.59M |     } else | 
| 75 | 0 |         ca = x;                 /* Just do the mod */ | 
| 76 |  |  | 
| 77 | 4.59M |     ret = BN_div_recp(NULL, r, ca, recp, ctx); | 
| 78 | 4.59M |  err: | 
| 79 | 4.59M |     BN_CTX_end(ctx); | 
| 80 | 4.59M |     bn_check_top(r); | 
| 81 | 4.59M |     return ret; | 
| 82 | 4.59M | } | 
| 83 |  |  | 
| 84 |  | int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, | 
| 85 |  |                 BN_RECP_CTX *recp, BN_CTX *ctx) | 
| 86 | 4.59M | { | 
| 87 | 4.59M |     int i, j, ret = 0; | 
| 88 | 4.59M |     BIGNUM *a, *b, *d, *r; | 
| 89 |  |  | 
| 90 | 4.59M |     BN_CTX_start(ctx); | 
| 91 | 4.59M |     d = (dv != NULL) ? dv : BN_CTX_get(ctx); | 
| 92 | 4.59M |     r = (rem != NULL) ? rem : BN_CTX_get(ctx); | 
| 93 | 4.59M |     a = BN_CTX_get(ctx); | 
| 94 | 4.59M |     b = BN_CTX_get(ctx); | 
| 95 | 4.59M |     if (b == NULL) | 
| 96 | 0 |         goto err; | 
| 97 |  |  | 
| 98 | 4.59M |     if (BN_ucmp(m, &(recp->N)) < 0) { | 
| 99 | 89.0k |         BN_zero(d); | 
| 100 | 89.0k |         if (!BN_copy(r, m)) { | 
| 101 | 0 |             BN_CTX_end(ctx); | 
| 102 | 0 |             return 0; | 
| 103 | 0 |         } | 
| 104 | 89.0k |         BN_CTX_end(ctx); | 
| 105 | 89.0k |         return 1; | 
| 106 | 89.0k |     } | 
| 107 |  |  | 
| 108 |  |     /* | 
| 109 |  |      * We want the remainder Given input of ABCDEF / ab we need multiply | 
| 110 |  |      * ABCDEF by 3 digests of the reciprocal of ab | 
| 111 |  |      */ | 
| 112 |  |  | 
| 113 |  |     /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */ | 
| 114 | 4.50M |     i = BN_num_bits(m); | 
| 115 | 4.50M |     j = recp->num_bits << 1; | 
| 116 | 4.50M |     if (j > i) | 
| 117 | 3.64M |         i = j; | 
| 118 |  |  | 
| 119 |  |     /* Nr := round(2^i / N) */ | 
| 120 | 4.50M |     if (i != recp->shift) | 
| 121 | 6.22k |         recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx); | 
| 122 |  |     /* BN_reciprocal could have returned -1 for an error */ | 
| 123 | 4.50M |     if (recp->shift == -1) | 
| 124 | 0 |         goto err; | 
| 125 |  |  | 
| 126 |  |     /*- | 
| 127 |  |      * d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))| | 
| 128 |  |      *    = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))| | 
| 129 |  |      *   <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)| | 
| 130 |  |      *    = |m/N| | 
| 131 |  |      */ | 
| 132 | 4.50M |     if (!BN_rshift(a, m, recp->num_bits)) | 
| 133 | 0 |         goto err; | 
| 134 | 4.50M |     if (!BN_mul(b, a, &(recp->Nr), ctx)) | 
| 135 | 0 |         goto err; | 
| 136 | 4.50M |     if (!BN_rshift(d, b, i - recp->num_bits)) | 
| 137 | 0 |         goto err; | 
| 138 | 4.50M |     d->neg = 0; | 
| 139 |  |  | 
| 140 | 4.50M |     if (!BN_mul(b, &(recp->N), d, ctx)) | 
| 141 | 0 |         goto err; | 
| 142 | 4.50M |     if (!BN_usub(r, m, b)) | 
| 143 | 0 |         goto err; | 
| 144 | 4.50M |     r->neg = 0; | 
| 145 |  |  | 
| 146 | 4.50M |     j = 0; | 
| 147 | 5.40M |     while (BN_ucmp(r, &(recp->N)) >= 0) { | 
| 148 | 899k |         if (j++ > 2) { | 
| 149 | 0 |             ERR_raise(ERR_LIB_BN, BN_R_BAD_RECIPROCAL); | 
| 150 | 0 |             goto err; | 
| 151 | 0 |         } | 
| 152 | 899k |         if (!BN_usub(r, r, &(recp->N))) | 
| 153 | 0 |             goto err; | 
| 154 | 899k |         if (!BN_add_word(d, 1)) | 
| 155 | 0 |             goto err; | 
| 156 | 899k |     } | 
| 157 |  |  | 
| 158 | 4.50M |     r->neg = BN_is_zero(r) ? 0 : m->neg; | 
| 159 | 4.50M |     d->neg = m->neg ^ recp->N.neg; | 
| 160 | 4.50M |     ret = 1; | 
| 161 | 4.50M |  err: | 
| 162 | 4.50M |     BN_CTX_end(ctx); | 
| 163 | 4.50M |     bn_check_top(dv); | 
| 164 | 4.50M |     bn_check_top(rem); | 
| 165 | 4.50M |     return ret; | 
| 166 | 4.50M | } | 
| 167 |  |  | 
| 168 |  | /* | 
| 169 |  |  * len is the expected size of the result We actually calculate with an extra | 
| 170 |  |  * word of precision, so we can do faster division if the remainder is not | 
| 171 |  |  * required. | 
| 172 |  |  */ | 
| 173 |  | /* r := 2^len / m */ | 
| 174 |  | int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) | 
| 175 | 6.22k | { | 
| 176 | 6.22k |     int ret = -1; | 
| 177 | 6.22k |     BIGNUM *t; | 
| 178 |  |  | 
| 179 | 6.22k |     BN_CTX_start(ctx); | 
| 180 | 6.22k |     if ((t = BN_CTX_get(ctx)) == NULL) | 
| 181 | 0 |         goto err; | 
| 182 |  |  | 
| 183 | 6.22k |     if (!BN_set_bit(t, len)) | 
| 184 | 0 |         goto err; | 
| 185 |  |  | 
| 186 | 6.22k |     if (!BN_div(r, NULL, t, m, ctx)) | 
| 187 | 0 |         goto err; | 
| 188 |  |  | 
| 189 | 6.22k |     ret = len; | 
| 190 | 6.22k |  err: | 
| 191 | 6.22k |     bn_check_top(r); | 
| 192 | 6.22k |     BN_CTX_end(ctx); | 
| 193 | 6.22k |     return ret; | 
| 194 | 6.22k | } |