/src/openssl30/crypto/bn/bn_word.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2016 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 |  | BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) | 
| 14 | 122k | { | 
| 15 | 122k | #ifndef BN_LLONG | 
| 16 | 122k |     BN_ULONG ret = 0; | 
| 17 |  | #else | 
| 18 |  |     BN_ULLONG ret = 0; | 
| 19 |  | #endif | 
| 20 | 122k |     int i; | 
| 21 |  |  | 
| 22 | 122k |     if (w == 0) | 
| 23 | 0 |         return (BN_ULONG)-1; | 
| 24 |  |  | 
| 25 | 122k | #ifndef BN_LLONG | 
| 26 |  |     /* | 
| 27 |  |      * If |w| is too long and we don't have BN_ULLONG then we need to fall | 
| 28 |  |      * back to using BN_div_word | 
| 29 |  |      */ | 
| 30 | 122k |     if (w > ((BN_ULONG)1 << BN_BITS4)) { | 
| 31 | 0 |         BIGNUM *tmp = BN_dup(a); | 
| 32 | 0 |         if (tmp == NULL) | 
| 33 | 0 |             return (BN_ULONG)-1; | 
| 34 |  |  | 
| 35 | 0 |         ret = BN_div_word(tmp, w); | 
| 36 | 0 |         BN_free(tmp); | 
| 37 |  | 
 | 
| 38 | 0 |         return ret; | 
| 39 | 0 |     } | 
| 40 | 122k | #endif | 
| 41 |  |  | 
| 42 | 122k |     bn_check_top(a); | 
| 43 | 122k |     w &= BN_MASK2; | 
| 44 | 5.77M |     for (i = a->top - 1; i >= 0; i--) { | 
| 45 | 5.65M | #ifndef BN_LLONG | 
| 46 |  |         /* | 
| 47 |  |          * We can assume here that | w <= ((BN_ULONG)1 << BN_BITS4) | and so | 
| 48 |  |          * | ret < ((BN_ULONG)1 << BN_BITS4) | and therefore the shifts here are | 
| 49 |  |          * safe and will not overflow | 
| 50 |  |          */ | 
| 51 | 5.65M |         ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w; | 
| 52 | 5.65M |         ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w; | 
| 53 |  | #else | 
| 54 |  |         ret = (BN_ULLONG) (((ret << (BN_ULLONG) BN_BITS2) | a->d[i]) % | 
| 55 |  |                            (BN_ULLONG) w); | 
| 56 |  | #endif | 
| 57 | 5.65M |     } | 
| 58 | 122k |     return (BN_ULONG)ret; | 
| 59 | 122k | } | 
| 60 |  |  | 
| 61 |  | BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) | 
| 62 | 1.57M | { | 
| 63 | 1.57M |     BN_ULONG ret = 0; | 
| 64 | 1.57M |     int i, j; | 
| 65 |  |  | 
| 66 | 1.57M |     bn_check_top(a); | 
| 67 | 1.57M |     w &= BN_MASK2; | 
| 68 |  |  | 
| 69 | 1.57M |     if (!w) | 
| 70 |  |         /* actually this an error (division by zero) */ | 
| 71 | 0 |         return (BN_ULONG)-1; | 
| 72 | 1.57M |     if (a->top == 0) | 
| 73 | 0 |         return 0; | 
| 74 |  |  | 
| 75 |  |     /* normalize input (so bn_div_words doesn't complain) */ | 
| 76 | 1.57M |     j = BN_BITS2 - BN_num_bits_word(w); | 
| 77 | 1.57M |     w <<= j; | 
| 78 | 1.57M |     if (!BN_lshift(a, a, j)) | 
| 79 | 0 |         return (BN_ULONG)-1; | 
| 80 |  |  | 
| 81 | 3.84M |     for (i = a->top - 1; i >= 0; i--) { | 
| 82 | 2.26M |         BN_ULONG l, d; | 
| 83 |  |  | 
| 84 | 2.26M |         l = a->d[i]; | 
| 85 | 2.26M |         d = bn_div_words(ret, l, w); | 
| 86 | 2.26M |         ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2; | 
| 87 | 2.26M |         a->d[i] = d; | 
| 88 | 2.26M |     } | 
| 89 | 1.57M |     if ((a->top > 0) && (a->d[a->top - 1] == 0)) | 
| 90 | 1.56M |         a->top--; | 
| 91 | 1.57M |     ret >>= j; | 
| 92 | 1.57M |     if (!a->top) | 
| 93 | 1.38M |         a->neg = 0; /* don't allow negative zero */ | 
| 94 | 1.57M |     bn_check_top(a); | 
| 95 | 1.57M |     return ret; | 
| 96 | 1.57M | } | 
| 97 |  |  | 
| 98 |  | int BN_add_word(BIGNUM *a, BN_ULONG w) | 
| 99 | 2.33M | { | 
| 100 | 2.33M |     BN_ULONG l; | 
| 101 | 2.33M |     int i; | 
| 102 |  |  | 
| 103 | 2.33M |     bn_check_top(a); | 
| 104 | 2.33M |     w &= BN_MASK2; | 
| 105 |  |  | 
| 106 |  |     /* degenerate case: w is zero */ | 
| 107 | 2.33M |     if (!w) | 
| 108 | 571k |         return 1; | 
| 109 |  |     /* degenerate case: a is zero */ | 
| 110 | 1.76M |     if (BN_is_zero(a)) | 
| 111 | 22.3k |         return BN_set_word(a, w); | 
| 112 |  |     /* handle 'a' when negative */ | 
| 113 | 1.74M |     if (a->neg) { | 
| 114 | 0 |         a->neg = 0; | 
| 115 | 0 |         i = BN_sub_word(a, w); | 
| 116 | 0 |         if (!BN_is_zero(a)) | 
| 117 | 0 |             a->neg = !(a->neg); | 
| 118 | 0 |         return i; | 
| 119 | 0 |     } | 
| 120 | 3.60M |     for (i = 0; w != 0 && i < a->top; i++) { | 
| 121 | 1.85M |         a->d[i] = l = (a->d[i] + w) & BN_MASK2; | 
| 122 | 1.85M |         w = (w > l) ? 1 : 0; | 
| 123 | 1.85M |     } | 
| 124 | 1.74M |     if (w && i == a->top) { | 
| 125 | 836 |         if (bn_wexpand(a, a->top + 1) == NULL) | 
| 126 | 0 |             return 0; | 
| 127 | 836 |         a->top++; | 
| 128 | 836 |         a->d[i] = w; | 
| 129 | 836 |     } | 
| 130 | 1.74M |     bn_check_top(a); | 
| 131 | 1.74M |     return 1; | 
| 132 | 1.74M | } | 
| 133 |  |  | 
| 134 |  | int BN_sub_word(BIGNUM *a, BN_ULONG w) | 
| 135 | 511k | { | 
| 136 | 511k |     int i; | 
| 137 |  |  | 
| 138 | 511k |     bn_check_top(a); | 
| 139 | 511k |     w &= BN_MASK2; | 
| 140 |  |  | 
| 141 |  |     /* degenerate case: w is zero */ | 
| 142 | 511k |     if (!w) | 
| 143 | 0 |         return 1; | 
| 144 |  |     /* degenerate case: a is zero */ | 
| 145 | 511k |     if (BN_is_zero(a)) { | 
| 146 | 214 |         i = BN_set_word(a, w); | 
| 147 | 214 |         if (i != 0) | 
| 148 | 214 |             BN_set_negative(a, 1); | 
| 149 | 214 |         return i; | 
| 150 | 214 |     } | 
| 151 |  |     /* handle 'a' when negative */ | 
| 152 | 511k |     if (a->neg) { | 
| 153 | 0 |         a->neg = 0; | 
| 154 | 0 |         i = BN_add_word(a, w); | 
| 155 | 0 |         a->neg = 1; | 
| 156 | 0 |         return i; | 
| 157 | 0 |     } | 
| 158 |  |  | 
| 159 | 511k |     if ((a->top == 1) && (a->d[0] < w)) { | 
| 160 | 5 |         a->d[0] = w - a->d[0]; | 
| 161 | 5 |         a->neg = 1; | 
| 162 | 5 |         return 1; | 
| 163 | 5 |     } | 
| 164 | 511k |     i = 0; | 
| 165 | 1.00M |     for (;;) { | 
| 166 | 1.00M |         if (a->d[i] >= w) { | 
| 167 | 511k |             a->d[i] -= w; | 
| 168 | 511k |             break; | 
| 169 | 511k |         } else { | 
| 170 | 492k |             a->d[i] = (a->d[i] - w) & BN_MASK2; | 
| 171 | 492k |             i++; | 
| 172 | 492k |             w = 1; | 
| 173 | 492k |         } | 
| 174 | 1.00M |     } | 
| 175 | 511k |     if ((a->d[i] == 0) && (i == (a->top - 1))) | 
| 176 | 96.8k |         a->top--; | 
| 177 | 511k |     bn_check_top(a); | 
| 178 | 511k |     return 1; | 
| 179 | 511k | } | 
| 180 |  |  | 
| 181 |  | int BN_mul_word(BIGNUM *a, BN_ULONG w) | 
| 182 | 2.74M | { | 
| 183 | 2.74M |     BN_ULONG ll; | 
| 184 |  |  | 
| 185 | 2.74M |     bn_check_top(a); | 
| 186 | 2.74M |     w &= BN_MASK2; | 
| 187 | 2.74M |     if (a->top) { | 
| 188 | 2.73M |         if (w == 0) | 
| 189 | 0 |             BN_zero(a); | 
| 190 | 2.73M |         else { | 
| 191 | 2.73M |             ll = bn_mul_words(a->d, a->d, a->top, w); | 
| 192 | 2.73M |             if (ll) { | 
| 193 | 2.68M |                 if (bn_wexpand(a, a->top + 1) == NULL) | 
| 194 | 0 |                     return 0; | 
| 195 | 2.68M |                 a->d[a->top++] = ll; | 
| 196 | 2.68M |             } | 
| 197 | 2.73M |         } | 
| 198 | 2.73M |     } | 
| 199 | 2.74M |     bn_check_top(a); | 
| 200 | 2.74M |     return 1; | 
| 201 | 2.74M | } |