/src/openssl30/crypto/bn/bn_sqr.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2018 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 |  | /* r must not be a */ | 
| 14 |  | /* | 
| 15 |  |  * I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96 | 
| 16 |  |  */ | 
| 17 |  | int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) | 
| 18 | 52.9M | { | 
| 19 | 52.9M |     int ret = bn_sqr_fixed_top(r, a, ctx); | 
| 20 |  |  | 
| 21 | 52.9M |     bn_correct_top(r); | 
| 22 | 52.9M |     bn_check_top(r); | 
| 23 |  |  | 
| 24 | 52.9M |     return ret; | 
| 25 | 52.9M | } | 
| 26 |  |  | 
| 27 |  | int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) | 
| 28 | 54.1M | { | 
| 29 | 54.1M |     int max, al; | 
| 30 | 54.1M |     int ret = 0; | 
| 31 | 54.1M |     BIGNUM *tmp, *rr; | 
| 32 |  |  | 
| 33 | 54.1M |     bn_check_top(a); | 
| 34 |  |  | 
| 35 | 54.1M |     al = a->top; | 
| 36 | 54.1M |     if (al <= 0) { | 
| 37 | 84.7k |         r->top = 0; | 
| 38 | 84.7k |         r->neg = 0; | 
| 39 | 84.7k |         return 1; | 
| 40 | 84.7k |     } | 
| 41 |  |  | 
| 42 | 54.0M |     BN_CTX_start(ctx); | 
| 43 | 54.0M |     rr = (a != r) ? r : BN_CTX_get(ctx); | 
| 44 | 54.0M |     tmp = BN_CTX_get(ctx); | 
| 45 | 54.0M |     if (rr == NULL || tmp == NULL) | 
| 46 | 0 |         goto err; | 
| 47 |  |  | 
| 48 | 54.0M |     max = 2 * al;               /* Non-zero (from above) */ | 
| 49 | 54.0M |     if (bn_wexpand(rr, max) == NULL) | 
| 50 | 0 |         goto err; | 
| 51 |  |  | 
| 52 | 54.0M |     if (al == 4) { | 
| 53 |  | #ifndef BN_SQR_COMBA | 
| 54 |  |         BN_ULONG t[8]; | 
| 55 |  |         bn_sqr_normal(rr->d, a->d, 4, t); | 
| 56 |  | #else | 
| 57 | 48.1M |         bn_sqr_comba4(rr->d, a->d); | 
| 58 | 48.1M | #endif | 
| 59 | 48.1M |     } else if (al == 8) { | 
| 60 |  | #ifndef BN_SQR_COMBA | 
| 61 |  |         BN_ULONG t[16]; | 
| 62 |  |         bn_sqr_normal(rr->d, a->d, 8, t); | 
| 63 |  | #else | 
| 64 | 22.2k |         bn_sqr_comba8(rr->d, a->d); | 
| 65 | 22.2k | #endif | 
| 66 | 5.94M |     } else { | 
| 67 | 5.94M | #if defined(BN_RECURSION) | 
| 68 | 5.94M |         if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) { | 
| 69 | 5.67M |             BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2]; | 
| 70 | 5.67M |             bn_sqr_normal(rr->d, a->d, al, t); | 
| 71 | 5.67M |         } else { | 
| 72 | 265k |             int j, k; | 
| 73 |  |  | 
| 74 | 265k |             j = BN_num_bits_word((BN_ULONG)al); | 
| 75 | 265k |             j = 1 << (j - 1); | 
| 76 | 265k |             k = j + j; | 
| 77 | 265k |             if (al == j) { | 
| 78 | 116k |                 if (bn_wexpand(tmp, k * 2) == NULL) | 
| 79 | 0 |                     goto err; | 
| 80 | 116k |                 bn_sqr_recursive(rr->d, a->d, al, tmp->d); | 
| 81 | 149k |             } else { | 
| 82 | 149k |                 if (bn_wexpand(tmp, max) == NULL) | 
| 83 | 0 |                     goto err; | 
| 84 | 149k |                 bn_sqr_normal(rr->d, a->d, al, tmp->d); | 
| 85 | 149k |             } | 
| 86 | 265k |         } | 
| 87 |  | #else | 
| 88 |  |         if (bn_wexpand(tmp, max) == NULL) | 
| 89 |  |             goto err; | 
| 90 |  |         bn_sqr_normal(rr->d, a->d, al, tmp->d); | 
| 91 |  | #endif | 
| 92 | 5.94M |     } | 
| 93 |  |  | 
| 94 | 54.0M |     rr->neg = 0; | 
| 95 | 54.0M |     rr->top = max; | 
| 96 | 54.0M |     rr->flags |= BN_FLG_FIXED_TOP; | 
| 97 | 54.0M |     if (r != rr && BN_copy(r, rr) == NULL) | 
| 98 | 0 |         goto err; | 
| 99 |  |  | 
| 100 | 54.0M |     ret = 1; | 
| 101 | 54.0M |  err: | 
| 102 | 54.0M |     bn_check_top(rr); | 
| 103 | 54.0M |     bn_check_top(tmp); | 
| 104 | 54.0M |     BN_CTX_end(ctx); | 
| 105 | 54.0M |     return ret; | 
| 106 | 54.0M | } | 
| 107 |  |  | 
| 108 |  | /* tmp must have 2*n words */ | 
| 109 |  | void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) | 
| 110 | 5.82M | { | 
| 111 | 5.82M |     int i, j, max; | 
| 112 | 5.82M |     const BN_ULONG *ap; | 
| 113 | 5.82M |     BN_ULONG *rp; | 
| 114 |  |  | 
| 115 | 5.82M |     max = n * 2; | 
| 116 | 5.82M |     ap = a; | 
| 117 | 5.82M |     rp = r; | 
| 118 | 5.82M |     rp[0] = rp[max - 1] = 0; | 
| 119 | 5.82M |     rp++; | 
| 120 | 5.82M |     j = n; | 
| 121 |  |  | 
| 122 | 5.82M |     if (--j > 0) { | 
| 123 | 988k |         ap++; | 
| 124 | 988k |         rp[j] = bn_mul_words(rp, ap, j, ap[-1]); | 
| 125 | 988k |         rp += 2; | 
| 126 | 988k |     } | 
| 127 |  |  | 
| 128 | 35.3M |     for (i = n - 2; i > 0; i--) { | 
| 129 | 29.5M |         j--; | 
| 130 | 29.5M |         ap++; | 
| 131 | 29.5M |         rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); | 
| 132 | 29.5M |         rp += 2; | 
| 133 | 29.5M |     } | 
| 134 |  |  | 
| 135 | 5.82M |     bn_add_words(r, r, r, max); | 
| 136 |  |  | 
| 137 |  |     /* There will not be a carry */ | 
| 138 |  |  | 
| 139 | 5.82M |     bn_sqr_words(tmp, a, n); | 
| 140 |  |  | 
| 141 | 5.82M |     bn_add_words(r, r, tmp, max); | 
| 142 | 5.82M | } | 
| 143 |  |  | 
| 144 |  | #ifdef BN_RECURSION | 
| 145 |  | /*- | 
| 146 |  |  * r is 2*n words in size, | 
| 147 |  |  * a and b are both n words in size.    (There's not actually a 'b' here ...) | 
| 148 |  |  * n must be a power of 2. | 
| 149 |  |  * We multiply and return the result. | 
| 150 |  |  * t must be 2*n words in size | 
| 151 |  |  * We calculate | 
| 152 |  |  * a[0]*b[0] | 
| 153 |  |  * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0]) | 
| 154 |  |  * a[1]*b[1] | 
| 155 |  |  */ | 
| 156 |  | void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) | 
| 157 | 12.7M | { | 
| 158 | 12.7M |     int n = n2 / 2; | 
| 159 | 12.7M |     int zero, c1; | 
| 160 | 12.7M |     BN_ULONG ln, lo, *p; | 
| 161 |  |  | 
| 162 | 12.7M |     if (n2 == 4) { | 
| 163 |  | # ifndef BN_SQR_COMBA | 
| 164 |  |         bn_sqr_normal(r, a, 4, t); | 
| 165 |  | # else | 
| 166 | 0 |         bn_sqr_comba4(r, a); | 
| 167 | 0 | # endif | 
| 168 | 0 |         return; | 
| 169 | 12.7M |     } else if (n2 == 8) { | 
| 170 |  | # ifndef BN_SQR_COMBA | 
| 171 |  |         bn_sqr_normal(r, a, 8, t); | 
| 172 |  | # else | 
| 173 | 8.54M |         bn_sqr_comba8(r, a); | 
| 174 | 8.54M | # endif | 
| 175 | 8.54M |         return; | 
| 176 | 8.54M |     } | 
| 177 | 4.23M |     if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) { | 
| 178 | 0 |         bn_sqr_normal(r, a, n2, t); | 
| 179 | 0 |         return; | 
| 180 | 0 |     } | 
| 181 |  |     /* r=(a[0]-a[1])*(a[1]-a[0]) */ | 
| 182 | 4.23M |     c1 = bn_cmp_words(a, &(a[n]), n); | 
| 183 | 4.23M |     zero = 0; | 
| 184 | 4.23M |     if (c1 > 0) | 
| 185 | 2.43M |         bn_sub_words(t, a, &(a[n]), n); | 
| 186 | 1.80M |     else if (c1 < 0) | 
| 187 | 1.76M |         bn_sub_words(t, &(a[n]), a, n); | 
| 188 | 38.0k |     else | 
| 189 | 38.0k |         zero = 1; | 
| 190 |  |  | 
| 191 |  |     /* The result will always be negative unless it is zero */ | 
| 192 | 4.23M |     p = &(t[n2 * 2]); | 
| 193 |  |  | 
| 194 | 4.23M |     if (!zero) | 
| 195 | 4.19M |         bn_sqr_recursive(&(t[n2]), t, n, p); | 
| 196 | 38.0k |     else | 
| 197 | 38.0k |         memset(&t[n2], 0, sizeof(*t) * n2); | 
| 198 | 4.23M |     bn_sqr_recursive(r, a, n, p); | 
| 199 | 4.23M |     bn_sqr_recursive(&(r[n2]), &(a[n]), n, p); | 
| 200 |  |  | 
| 201 |  |     /*- | 
| 202 |  |      * t[32] holds (a[0]-a[1])*(a[1]-a[0]), it is negative or zero | 
| 203 |  |      * r[10] holds (a[0]*b[0]) | 
| 204 |  |      * r[32] holds (b[1]*b[1]) | 
| 205 |  |      */ | 
| 206 |  |  | 
| 207 | 4.23M |     c1 = (int)(bn_add_words(t, r, &(r[n2]), n2)); | 
| 208 |  |  | 
| 209 |  |     /* t[32] is negative */ | 
| 210 | 4.23M |     c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2)); | 
| 211 |  |  | 
| 212 |  |     /*- | 
| 213 |  |      * t[32] holds (a[0]-a[1])*(a[1]-a[0])+(a[0]*a[0])+(a[1]*a[1]) | 
| 214 |  |      * r[10] holds (a[0]*a[0]) | 
| 215 |  |      * r[32] holds (a[1]*a[1]) | 
| 216 |  |      * c1 holds the carry bits | 
| 217 |  |      */ | 
| 218 | 4.23M |     c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2)); | 
| 219 | 4.23M |     if (c1) { | 
| 220 | 1.62M |         p = &(r[n + n2]); | 
| 221 | 1.62M |         lo = *p; | 
| 222 | 1.62M |         ln = (lo + c1) & BN_MASK2; | 
| 223 | 1.62M |         *p = ln; | 
| 224 |  |  | 
| 225 |  |         /* | 
| 226 |  |          * The overflow will stop before we over write words we should not | 
| 227 |  |          * overwrite | 
| 228 |  |          */ | 
| 229 | 1.62M |         if (ln < (BN_ULONG)c1) { | 
| 230 | 15.8k |             do { | 
| 231 | 15.8k |                 p++; | 
| 232 | 15.8k |                 lo = *p; | 
| 233 | 15.8k |                 ln = (lo + 1) & BN_MASK2; | 
| 234 | 15.8k |                 *p = ln; | 
| 235 | 15.8k |             } while (ln == 0); | 
| 236 | 4.90k |         } | 
| 237 | 1.62M |     } | 
| 238 | 4.23M | } | 
| 239 |  | #endif |