1 /* Originally written by Bodo Moeller for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55 /* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57 *
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60 *
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
63 *
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66 * Laboratories. */
67
68 #include <openssl/ec.h>
69
70 #include <string.h>
71
72 #include <openssl/bn.h>
73 #include <openssl/err.h>
74 #include <openssl/mem.h>
75 #include <openssl/thread.h>
76
77 #include "internal.h"
78 #include "../bn/internal.h"
79 #include "../../internal.h"
80
81
82 // This file implements the wNAF-based interleaving multi-exponentiation method
83 // at:
84 // http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
85 // http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
86
87 // compute_wNAF writes the modified width-(w+1) Non-Adjacent Form (wNAF) of
88 // |scalar| to |out| and returns one on success or zero on internal error. |out|
89 // must have room for |bits| + 1 elements, each of which will be either zero or
90 // odd with an absolute value less than 2^w satisfying
91 // scalar = \sum_j out[j]*2^j
92 // where at most one of any w+1 consecutive digits is non-zero
93 // with the exception that the most significant digit may be only
94 // w-1 zeros away from that next non-zero digit.
compute_wNAF(const EC_GROUP * group,int8_t * out,const EC_SCALAR * scalar,size_t bits,int w)95 static int compute_wNAF(const EC_GROUP *group, int8_t *out,
96 const EC_SCALAR *scalar, size_t bits, int w) {
97 // 'int8_t' can represent integers with absolute values less than 2^7.
98 if (w <= 0 || w > 7 || bits == 0) {
99 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
100 return 0;
101 }
102 int bit = 1 << w; // at most 128
103 int next_bit = bit << 1; // at most 256
104 int mask = next_bit - 1; // at most 255
105
106 int window_val = scalar->words[0] & mask;
107 size_t j = 0;
108 // If j+w+1 >= bits, window_val will not increase.
109 while (window_val != 0 || j + w + 1 < bits) {
110 int digit = 0;
111
112 // 0 <= window_val <= 2^(w+1)
113
114 if (window_val & 1) {
115 // 0 < window_val < 2^(w+1)
116
117 if (window_val & bit) {
118 digit = window_val - next_bit; // -2^w < digit < 0
119
120 #if 1 // modified wNAF
121 if (j + w + 1 >= bits) {
122 // special case for generating modified wNAFs:
123 // no new bits will be added into window_val,
124 // so using a positive digit here will decrease
125 // the total length of the representation
126
127 digit = window_val & (mask >> 1); // 0 < digit < 2^w
128 }
129 #endif
130 } else {
131 digit = window_val; // 0 < digit < 2^w
132 }
133
134 if (digit <= -bit || digit >= bit || !(digit & 1)) {
135 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
136 return 0;
137 }
138
139 window_val -= digit;
140
141 // Now window_val is 0 or 2^(w+1) in standard wNAF generation;
142 // for modified window NAFs, it may also be 2^w.
143 if (window_val != 0 && window_val != next_bit && window_val != bit) {
144 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
145 return 0;
146 }
147 }
148
149 out[j++] = digit;
150
151 window_val >>= 1;
152 window_val +=
153 bit * bn_is_bit_set_words(scalar->words, group->order.top, j + w);
154
155 if (window_val > next_bit) {
156 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
157 return 0;
158 }
159 }
160
161 // Fill the rest of the wNAF with zeros.
162 if (j > bits + 1) {
163 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
164 return 0;
165 }
166 for (size_t i = j; i < bits + 1; i++) {
167 out[i] = 0;
168 }
169
170 return 1;
171 }
172
173 // TODO: table should be optimised for the wNAF-based implementation,
174 // sometimes smaller windows will give better performance
175 // (thus the boundaries should be increased)
window_bits_for_scalar_size(size_t b)176 static size_t window_bits_for_scalar_size(size_t b) {
177 if (b >= 300) {
178 return 4;
179 }
180
181 if (b >= 70) {
182 return 3;
183 }
184
185 if (b >= 20) {
186 return 2;
187 }
188
189 return 1;
190 }
191
192 // EC_WNAF_MAX_WINDOW_BITS is the largest value returned by
193 // |window_bits_for_scalar_size|.
194 #define EC_WNAF_MAX_WINDOW_BITS 4
195
196 // compute_precomp sets |out[i]| to a newly-allocated |EC_POINT| containing
197 // (2*i+1)*p, for i from 0 to |len|. It returns one on success and
198 // zero on error.
compute_precomp(const EC_GROUP * group,EC_POINT ** out,const EC_POINT * p,size_t len,BN_CTX * ctx)199 static int compute_precomp(const EC_GROUP *group, EC_POINT **out,
200 const EC_POINT *p, size_t len, BN_CTX *ctx) {
201 out[0] = EC_POINT_new(group);
202 if (out[0] == NULL ||
203 !EC_POINT_copy(out[0], p)) {
204 return 0;
205 }
206
207 int ret = 0;
208 EC_POINT *two_p = EC_POINT_new(group);
209 if (two_p == NULL ||
210 !EC_POINT_dbl(group, two_p, p, ctx)) {
211 goto err;
212 }
213
214 for (size_t i = 1; i < len; i++) {
215 out[i] = EC_POINT_new(group);
216 if (out[i] == NULL ||
217 !EC_POINT_add(group, out[i], out[i - 1], two_p, ctx)) {
218 goto err;
219 }
220 }
221
222 ret = 1;
223
224 err:
225 EC_POINT_free(two_p);
226 return ret;
227 }
228
lookup_precomp(const EC_GROUP * group,EC_POINT * out,EC_POINT * const * precomp,int digit,BN_CTX * ctx)229 static int lookup_precomp(const EC_GROUP *group, EC_POINT *out,
230 EC_POINT *const *precomp, int digit, BN_CTX *ctx) {
231 if (digit < 0) {
232 digit = -digit;
233 return EC_POINT_copy(out, precomp[digit >> 1]) &&
234 EC_POINT_invert(group, out, ctx);
235 }
236
237 return EC_POINT_copy(out, precomp[digit >> 1]);
238 }
239
ec_wNAF_mul(const EC_GROUP * group,EC_POINT * r,const EC_SCALAR * g_scalar,const EC_POINT * p,const EC_SCALAR * p_scalar,BN_CTX * ctx)240 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const EC_SCALAR *g_scalar,
241 const EC_POINT *p, const EC_SCALAR *p_scalar, BN_CTX *ctx) {
242 BN_CTX *new_ctx = NULL;
243 EC_POINT *precomp_storage[2 * (1 << (EC_WNAF_MAX_WINDOW_BITS - 1))] = {NULL};
244 EC_POINT **g_precomp = NULL, **p_precomp = NULL;
245 int8_t g_wNAF[EC_MAX_SCALAR_BYTES * 8 + 1];
246 int8_t p_wNAF[EC_MAX_SCALAR_BYTES * 8 + 1];
247 EC_POINT *tmp = NULL;
248 int ret = 0;
249
250 if (ctx == NULL) {
251 ctx = new_ctx = BN_CTX_new();
252 if (ctx == NULL) {
253 goto err;
254 }
255 }
256
257 size_t bits = BN_num_bits(&group->order);
258 size_t wsize = window_bits_for_scalar_size(bits);
259 size_t wNAF_len = bits + 1;
260 size_t precomp_len = (size_t)1 << (wsize - 1);
261 if (wNAF_len > OPENSSL_ARRAY_SIZE(g_wNAF) ||
262 wNAF_len > OPENSSL_ARRAY_SIZE(p_wNAF) ||
263 2 * precomp_len > OPENSSL_ARRAY_SIZE(precomp_storage)) {
264 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
265 goto err;
266 }
267
268 // TODO(davidben): |mul_public| is for ECDSA verification which can assume
269 // non-NULL inputs, but this code is also used for |mul| which cannot. It's
270 // not constant-time, so replace the generic |mul| and remove the NULL checks.
271 size_t total_precomp = 0;
272 if (g_scalar != NULL) {
273 const EC_POINT *g = EC_GROUP_get0_generator(group);
274 if (g == NULL) {
275 OPENSSL_PUT_ERROR(EC, EC_R_UNDEFINED_GENERATOR);
276 goto err;
277 }
278 g_precomp = precomp_storage + total_precomp;
279 total_precomp += precomp_len;
280 if (!compute_wNAF(group, g_wNAF, g_scalar, bits, wsize) ||
281 !compute_precomp(group, g_precomp, g, precomp_len, ctx)) {
282 goto err;
283 }
284 }
285
286 if (p_scalar != NULL) {
287 p_precomp = precomp_storage + total_precomp;
288 total_precomp += precomp_len;
289 if (!compute_wNAF(group, p_wNAF, p_scalar, bits, wsize) ||
290 !compute_precomp(group, p_precomp, p, precomp_len, ctx)) {
291 goto err;
292 }
293 }
294
295 tmp = EC_POINT_new(group);
296 if (tmp == NULL ||
297 // |window_bits_for_scalar_size| assumes we do this step.
298 !EC_POINTs_make_affine(group, total_precomp, precomp_storage, ctx)) {
299 goto err;
300 }
301
302 int r_is_at_infinity = 1;
303 for (size_t k = wNAF_len - 1; k < wNAF_len; k--) {
304 if (!r_is_at_infinity && !EC_POINT_dbl(group, r, r, ctx)) {
305 goto err;
306 }
307
308 if (g_scalar != NULL) {
309 if (g_wNAF[k] != 0) {
310 if (!lookup_precomp(group, tmp, g_precomp, g_wNAF[k], ctx)) {
311 goto err;
312 }
313 if (r_is_at_infinity) {
314 if (!EC_POINT_copy(r, tmp)) {
315 goto err;
316 }
317 r_is_at_infinity = 0;
318 } else if (!EC_POINT_add(group, r, r, tmp, ctx)) {
319 goto err;
320 }
321 }
322 }
323
324 if (p_scalar != NULL) {
325 if (p_wNAF[k] != 0) {
326 if (!lookup_precomp(group, tmp, p_precomp, p_wNAF[k], ctx)) {
327 goto err;
328 }
329 if (r_is_at_infinity) {
330 if (!EC_POINT_copy(r, tmp)) {
331 goto err;
332 }
333 r_is_at_infinity = 0;
334 } else if (!EC_POINT_add(group, r, r, tmp, ctx)) {
335 goto err;
336 }
337 }
338 }
339 }
340
341 if (r_is_at_infinity &&
342 !EC_POINT_set_to_infinity(group, r)) {
343 goto err;
344 }
345
346 ret = 1;
347
348 err:
349 BN_CTX_free(new_ctx);
350 EC_POINT_free(tmp);
351 OPENSSL_cleanse(&g_wNAF, sizeof(g_wNAF));
352 OPENSSL_cleanse(&p_wNAF, sizeof(p_wNAF));
353 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(precomp_storage); i++) {
354 EC_POINT_free(precomp_storage[i]);
355 }
356 return ret;
357 }
358