| // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 |
| 16 | #include <openssl/ec.h> |
| 17 |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/bn.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/thread.h> |
| 25 |
| 26 | #include "../../internal.h" |
| 27 | #include "../bn/internal.h" |
| 28 | #include "internal.h" |
| 29 |
| 30 |
| 31 | // This file implements the wNAF-based interleaving multi-exponentiation method |
| 32 | // at: |
| 33 | // http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13 |
| 34 | // http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf |
| 35 |
| 36 | void ec_compute_wNAF(const EC_GROUP *group, int8_t *out, |
| 37 | const EC_SCALAR *scalar, size_t bits, int w) { |
| 38 | // 'int8_t' can represent integers with absolute values less than 2^7. |
| 39 | assert(0 < w && w <= 7); |
| 40 | assert(bits != 0); |
| 41 | int bit = 1 << w; // 2^w, at most 128 |
| 42 | int next_bit = bit << 1; // 2^(w+1), at most 256 |
| 43 | int mask = next_bit - 1; // at most 255 |
| 44 |
| 45 | int window_val = scalar->words[0] & mask; |
| 46 | for (size_t j = 0; j < bits + 1; j++) { |
| 47 | assert(0 <= window_val && window_val <= next_bit); |
| 48 | int digit = 0; |
| 49 | if (window_val & 1) { |
| 50 | assert(0 < window_val && window_val < next_bit); |
| 51 | if (window_val & bit) { |
| 52 | digit = window_val - next_bit; |
| 53 | // We know -next_bit < digit < 0 and window_val - digit = next_bit. |
| 54 |
| 55 | // modified wNAF |
| 56 | if (j + w + 1 >= bits) { |
| 57 | // special case for generating modified wNAFs: |
| 58 | // no new bits will be added into window_val, |
| 59 | // so using a positive digit here will decrease |
| 60 | // the total length of the representation |
| 61 |
| 62 | digit = window_val & (mask >> 1); |
| 63 | // We know 0 < digit < bit and window_val - digit = bit. |
| 64 | } |
| 65 | } else { |
| 66 | digit = window_val; |
| 67 | // We know 0 < digit < bit and window_val - digit = 0. |
| 68 | } |
| 69 |
| 70 | window_val -= digit; |
| 71 |
| 72 | // Now window_val is 0 or 2^(w+1) in standard wNAF generation. |
| 73 | // For modified window NAFs, it may also be 2^w. |
| 74 | // |
| 75 | // See the comments above for the derivation of each of these bounds. |
| 76 | assert(window_val == 0 || window_val == next_bit || window_val == bit); |
| 77 | assert(-bit < digit && digit < bit); |
| 78 |
| 79 | // window_val was odd, so digit is also odd. |
| 80 | assert(digit & 1); |
| 81 | } |
| 82 |
| 83 | out[j] = digit; |
| 84 |
| 85 | // Incorporate the next bit. Previously, |window_val| <= |next_bit|, so if |
| 86 | // we shift and add at most one copy of |bit|, this will continue to hold |
| 87 | // afterwards. |
| 88 | window_val >>= 1; |
| 89 | window_val += bit * bn_is_bit_set_words(scalar->words, group->order.N.width, |
| 90 | j + w + 1); |
| 91 | assert(window_val <= next_bit); |
| 92 | } |
| 93 |
| 94 | // bits + 1 entries should be sufficient to consume all bits. |
| 95 | assert(window_val == 0); |
| 96 | } |
| 97 |
| 98 | // compute_precomp sets |out[i]| to (2*i+1)*p, for i from 0 to |len|. |
| 99 | static void compute_precomp(const EC_GROUP *group, EC_JACOBIAN *out, |
| 100 | const EC_JACOBIAN *p, size_t len) { |
| 101 | ec_GFp_simple_point_copy(&out[0], p); |
| 102 | EC_JACOBIAN two_p; |
| 103 | ec_GFp_mont_dbl(group, &two_p, p); |
| 104 | for (size_t i = 1; i < len; i++) { |
| 105 | ec_GFp_mont_add(group, &out[i], &out[i - 1], &two_p); |
| 106 | } |
| 107 | } |
| 108 |
| 109 | static void lookup_precomp(const EC_GROUP *group, EC_JACOBIAN *out, |
| 110 | const EC_JACOBIAN *precomp, int digit) { |
| 111 | if (digit < 0) { |
| 112 | digit = -digit; |
| 113 | ec_GFp_simple_point_copy(out, &precomp[digit >> 1]); |
| 114 | ec_GFp_simple_invert(group, out); |
| 115 | } else { |
| 116 | ec_GFp_simple_point_copy(out, &precomp[digit >> 1]); |
| 117 | } |
| 118 | } |
| 119 |
| 120 | // EC_WNAF_WINDOW_BITS is the window size to use for |ec_GFp_mont_mul_public|. |
| 121 | #define EC_WNAF_WINDOW_BITS 4 |
| 122 |
| 123 | // EC_WNAF_TABLE_SIZE is the table size to use for |ec_GFp_mont_mul_public|. |
| 124 | #define EC_WNAF_TABLE_SIZE (1 << (EC_WNAF_WINDOW_BITS - 1)) |
| 125 |
| 126 | // EC_WNAF_STACK is the number of points worth of data to stack-allocate and |
| 127 | // avoid a malloc. |
| 128 | #define EC_WNAF_STACK 3 |
| 129 |
| 130 | int ec_GFp_mont_mul_public_batch(const EC_GROUP *group, EC_JACOBIAN *r, |
| 131 | const EC_SCALAR *g_scalar, |
| 132 | const EC_JACOBIAN *points, |
| 133 | const EC_SCALAR *scalars, size_t num) { |
| 134 | size_t bits = EC_GROUP_order_bits(group); |
| 135 | size_t wNAF_len = bits + 1; |
| 136 |
| 137 | // Stack-allocated space, which will be used if the task is small enough. |
| 138 | int8_t wNAF_stack[EC_WNAF_STACK][EC_MAX_BYTES * 8 + 1]; |
| 139 | EC_JACOBIAN precomp_stack[EC_WNAF_STACK][EC_WNAF_TABLE_SIZE]; |
| 140 |
| 141 | // Allocated pointers, which will remain NULL unless needed. |
| 142 | EC_JACOBIAN(*precomp_alloc)[EC_WNAF_TABLE_SIZE] = NULL; |
| 143 | int8_t(*wNAF_alloc)[EC_MAX_BYTES * 8 + 1] = NULL; |
| 144 |
| 145 | // These fields point either to the stack or heap buffers of the same name. |
| 146 | int8_t(*wNAF)[EC_MAX_BYTES * 8 + 1]; |
| 147 | EC_JACOBIAN(*precomp)[EC_WNAF_TABLE_SIZE]; |
| 148 |
| 149 | if (num <= EC_WNAF_STACK) { |
| 150 | wNAF = wNAF_stack; |
| 151 | precomp = precomp_stack; |
| 152 | } else { |
| 153 | wNAF_alloc = reinterpret_cast<decltype(wNAF_alloc)>( |
| 154 | OPENSSL_calloc(num, sizeof(wNAF_alloc[0]))); |
| 155 | if (wNAF_alloc == NULL) { |
| 156 | return 0; |
| 157 | } |
| 158 | precomp_alloc = reinterpret_cast<decltype(precomp_alloc)>( |
| 159 | OPENSSL_calloc(num, sizeof(precomp_alloc[0]))); |
| 160 | if (precomp_alloc == NULL) { |
| 161 | OPENSSL_free(wNAF_alloc); |
| 162 | return 0; |
| 163 | } |
| 164 |
| 165 | wNAF = wNAF_alloc; |
| 166 | precomp = precomp_alloc; |
| 167 | } |
| 168 |
| 169 | int8_t g_wNAF[EC_MAX_BYTES * 8 + 1]; |
| 170 | EC_JACOBIAN g_precomp[EC_WNAF_TABLE_SIZE]; |
| 171 | assert(wNAF_len <= OPENSSL_ARRAY_SIZE(g_wNAF)); |
| 172 | const EC_JACOBIAN *g = &group->generator.raw; |
| 173 | if (g_scalar != NULL) { |
| 174 | ec_compute_wNAF(group, g_wNAF, g_scalar, bits, EC_WNAF_WINDOW_BITS); |
| 175 | compute_precomp(group, g_precomp, g, EC_WNAF_TABLE_SIZE); |
| 176 | } |
| 177 |
| 178 | for (size_t i = 0; i < num; i++) { |
| 179 | assert(wNAF_len <= OPENSSL_ARRAY_SIZE(wNAF[i])); |
| 180 | ec_compute_wNAF(group, wNAF[i], &scalars[i], bits, EC_WNAF_WINDOW_BITS); |
| 181 | compute_precomp(group, precomp[i], &points[i], EC_WNAF_TABLE_SIZE); |
| 182 | } |
| 183 |
| 184 | EC_JACOBIAN tmp; |
| 185 | int r_is_at_infinity = 1; |
| 186 | for (size_t k = wNAF_len - 1; k < wNAF_len; k--) { |
| 187 | if (!r_is_at_infinity) { |
| 188 | ec_GFp_mont_dbl(group, r, r); |
| 189 | } |
| 190 |
| 191 | if (g_scalar != NULL && g_wNAF[k] != 0) { |
| 192 | lookup_precomp(group, &tmp, g_precomp, g_wNAF[k]); |
| 193 | if (r_is_at_infinity) { |
| 194 | ec_GFp_simple_point_copy(r, &tmp); |
| 195 | r_is_at_infinity = 0; |
| 196 | } else { |
| 197 | ec_GFp_mont_add(group, r, r, &tmp); |
| 198 | } |
| 199 | } |
| 200 |
| 201 | for (size_t i = 0; i < num; i++) { |
| 202 | if (wNAF[i][k] != 0) { |
| 203 | lookup_precomp(group, &tmp, precomp[i], wNAF[i][k]); |
| 204 | if (r_is_at_infinity) { |
| 205 | ec_GFp_simple_point_copy(r, &tmp); |
| 206 | r_is_at_infinity = 0; |
| 207 | } else { |
| 208 | ec_GFp_mont_add(group, r, r, &tmp); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 |
| 214 | if (r_is_at_infinity) { |
| 215 | ec_GFp_simple_point_set_to_infinity(group, r); |
| 216 | } |
| 217 |
| 218 | OPENSSL_free(wNAF_alloc); |
| 219 | OPENSSL_free(precomp_alloc); |
| 220 | return 1; |
| 221 | } |
| 222 |