blob: 7bc0bc7bc5e952c250e052c8e4640ac79cdb702b [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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>
Adam Langleye9ada862015-05-11 17:20:37 -070075#include <openssl/thread.h>
Robert Sloanab8b8882018-03-26 11:39:51 -070076#include <openssl/type_check.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080077
78#include "internal.h"
Robert Sloan309a31e2018-01-29 10:22:47 -080079#include "../bn/internal.h"
Robert Sloan8ff03552017-06-14 12:40:58 -070080#include "../../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080081
82
Robert Sloan8f860b12017-08-28 07:37:06 -070083// This file implements the wNAF-based interleaving multi-exponentiation method
84// at:
85// http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13
86// http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf
Adam Langleyd9e397b2015-01-22 14:27:53 -080087
Robert Sloanab8b8882018-03-26 11:39:51 -070088int ec_compute_wNAF(const EC_GROUP *group, int8_t *out, const EC_SCALAR *scalar,
89 size_t bits, int w) {
Robert Sloan8f860b12017-08-28 07:37:06 -070090 // 'int8_t' can represent integers with absolute values less than 2^7.
Robert Sloan309a31e2018-01-29 10:22:47 -080091 if (w <= 0 || w > 7 || bits == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +000092 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Robert Sloan309a31e2018-01-29 10:22:47 -080093 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -080094 }
Robert Sloan309a31e2018-01-29 10:22:47 -080095 int bit = 1 << w; // at most 128
96 int next_bit = bit << 1; // at most 256
97 int mask = next_bit - 1; // at most 255
Adam Langleyd9e397b2015-01-22 14:27:53 -080098
Robert Sloan309a31e2018-01-29 10:22:47 -080099 int window_val = scalar->words[0] & mask;
100 size_t j = 0;
101 // If j+w+1 >= bits, window_val will not increase.
102 while (window_val != 0 || j + w + 1 < bits) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800103 int digit = 0;
104
Robert Sloan8f860b12017-08-28 07:37:06 -0700105 // 0 <= window_val <= 2^(w+1)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800106
107 if (window_val & 1) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700108 // 0 < window_val < 2^(w+1)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800109
110 if (window_val & bit) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700111 digit = window_val - next_bit; // -2^w < digit < 0
Adam Langleyd9e397b2015-01-22 14:27:53 -0800112
Robert Sloan8f860b12017-08-28 07:37:06 -0700113#if 1 // modified wNAF
Robert Sloan309a31e2018-01-29 10:22:47 -0800114 if (j + w + 1 >= bits) {
Robert Sloan8f860b12017-08-28 07:37:06 -0700115 // special case for generating modified wNAFs:
116 // no new bits will be added into window_val,
117 // so using a positive digit here will decrease
118 // the total length of the representation
Adam Langleyd9e397b2015-01-22 14:27:53 -0800119
Robert Sloan8f860b12017-08-28 07:37:06 -0700120 digit = window_val & (mask >> 1); // 0 < digit < 2^w
Adam Langleyd9e397b2015-01-22 14:27:53 -0800121 }
122#endif
123 } else {
Robert Sloan8f860b12017-08-28 07:37:06 -0700124 digit = window_val; // 0 < digit < 2^w
Adam Langleyd9e397b2015-01-22 14:27:53 -0800125 }
126
127 if (digit <= -bit || digit >= bit || !(digit & 1)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000128 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Robert Sloan309a31e2018-01-29 10:22:47 -0800129 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130 }
131
132 window_val -= digit;
133
Robert Sloan8f860b12017-08-28 07:37:06 -0700134 // Now window_val is 0 or 2^(w+1) in standard wNAF generation;
135 // for modified window NAFs, it may also be 2^w.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800136 if (window_val != 0 && window_val != next_bit && window_val != bit) {
Kenny Rootb8494592015-09-25 02:29:14 +0000137 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Robert Sloan309a31e2018-01-29 10:22:47 -0800138 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800139 }
140 }
141
Robert Sloan309a31e2018-01-29 10:22:47 -0800142 out[j++] = digit;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800143
144 window_val >>= 1;
Robert Sloan309a31e2018-01-29 10:22:47 -0800145 window_val +=
Robert Sloanab8b8882018-03-26 11:39:51 -0700146 bit * bn_is_bit_set_words(scalar->words, group->order.width, j + w);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800147
148 if (window_val > next_bit) {
Kenny Rootb8494592015-09-25 02:29:14 +0000149 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Robert Sloan309a31e2018-01-29 10:22:47 -0800150 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800151 }
152 }
153
Robert Sloan309a31e2018-01-29 10:22:47 -0800154 // Fill the rest of the wNAF with zeros.
155 if (j > bits + 1) {
Kenny Rootb8494592015-09-25 02:29:14 +0000156 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Robert Sloan309a31e2018-01-29 10:22:47 -0800157 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800158 }
Robert Sloan309a31e2018-01-29 10:22:47 -0800159 for (size_t i = j; i < bits + 1; i++) {
160 out[i] = 0;
161 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800162
Robert Sloan309a31e2018-01-29 10:22:47 -0800163 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164}
165
Robert Sloan8f860b12017-08-28 07:37:06 -0700166// TODO: table should be optimised for the wNAF-based implementation,
167// sometimes smaller windows will give better performance
168// (thus the boundaries should be increased)
David Benjamin1b249672016-12-06 18:25:50 -0500169static size_t window_bits_for_scalar_size(size_t b) {
David Benjamin1b249672016-12-06 18:25:50 -0500170 if (b >= 300) {
171 return 4;
172 }
173
174 if (b >= 70) {
175 return 3;
176 }
177
178 if (b >= 20) {
179 return 2;
180 }
181
182 return 1;
183}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800184
Robert Sloan309a31e2018-01-29 10:22:47 -0800185// EC_WNAF_MAX_WINDOW_BITS is the largest value returned by
186// |window_bits_for_scalar_size|.
187#define EC_WNAF_MAX_WINDOW_BITS 4
188
189// compute_precomp sets |out[i]| to a newly-allocated |EC_POINT| containing
190// (2*i+1)*p, for i from 0 to |len|. It returns one on success and
191// zero on error.
192static int compute_precomp(const EC_GROUP *group, EC_POINT **out,
193 const EC_POINT *p, size_t len, BN_CTX *ctx) {
194 out[0] = EC_POINT_new(group);
195 if (out[0] == NULL ||
196 !EC_POINT_copy(out[0], p)) {
197 return 0;
198 }
199
200 int ret = 0;
201 EC_POINT *two_p = EC_POINT_new(group);
202 if (two_p == NULL ||
203 !EC_POINT_dbl(group, two_p, p, ctx)) {
204 goto err;
205 }
206
207 for (size_t i = 1; i < len; i++) {
208 out[i] = EC_POINT_new(group);
209 if (out[i] == NULL ||
210 !EC_POINT_add(group, out[i], out[i - 1], two_p, ctx)) {
211 goto err;
212 }
213 }
214
215 ret = 1;
216
217err:
218 EC_POINT_free(two_p);
219 return ret;
220}
221
222static int lookup_precomp(const EC_GROUP *group, EC_POINT *out,
223 EC_POINT *const *precomp, int digit, BN_CTX *ctx) {
224 if (digit < 0) {
225 digit = -digit;
226 return EC_POINT_copy(out, precomp[digit >> 1]) &&
227 EC_POINT_invert(group, out, ctx);
228 }
229
230 return EC_POINT_copy(out, precomp[digit >> 1]);
231}
232
233int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const EC_SCALAR *g_scalar,
234 const EC_POINT *p, const EC_SCALAR *p_scalar, BN_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800235 BN_CTX *new_ctx = NULL;
Robert Sloan309a31e2018-01-29 10:22:47 -0800236 EC_POINT *precomp_storage[2 * (1 << (EC_WNAF_MAX_WINDOW_BITS - 1))] = {NULL};
237 EC_POINT **g_precomp = NULL, **p_precomp = NULL;
238 int8_t g_wNAF[EC_MAX_SCALAR_BYTES * 8 + 1];
239 int8_t p_wNAF[EC_MAX_SCALAR_BYTES * 8 + 1];
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 EC_POINT *tmp = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800241 int ret = 0;
242
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 if (ctx == NULL) {
244 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700245 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700247 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248 }
Robert Sloan99319a12017-11-27 10:32:46 -0800249
Robert Sloan309a31e2018-01-29 10:22:47 -0800250 size_t bits = BN_num_bits(&group->order);
251 size_t wsize = window_bits_for_scalar_size(bits);
252 size_t wNAF_len = bits + 1;
253 size_t precomp_len = (size_t)1 << (wsize - 1);
Robert Sloanab8b8882018-03-26 11:39:51 -0700254
255 OPENSSL_COMPILE_ASSERT(
256 OPENSSL_ARRAY_SIZE(g_wNAF) == OPENSSL_ARRAY_SIZE(p_wNAF),
257 g_wNAF_and_p_wNAF_are_different_sizes);
258
Robert Sloan309a31e2018-01-29 10:22:47 -0800259 if (wNAF_len > OPENSSL_ARRAY_SIZE(g_wNAF) ||
Robert Sloan309a31e2018-01-29 10:22:47 -0800260 2 * precomp_len > OPENSSL_ARRAY_SIZE(precomp_storage)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000261 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262 goto err;
263 }
264
Robert Sloan309a31e2018-01-29 10:22:47 -0800265 // TODO(davidben): |mul_public| is for ECDSA verification which can assume
266 // non-NULL inputs, but this code is also used for |mul| which cannot. It's
267 // not constant-time, so replace the generic |mul| and remove the NULL checks.
268 size_t total_precomp = 0;
269 if (g_scalar != NULL) {
270 const EC_POINT *g = EC_GROUP_get0_generator(group);
271 if (g == NULL) {
272 OPENSSL_PUT_ERROR(EC, EC_R_UNDEFINED_GENERATOR);
273 goto err;
274 }
275 g_precomp = precomp_storage + total_precomp;
276 total_precomp += precomp_len;
Robert Sloanab8b8882018-03-26 11:39:51 -0700277 if (!ec_compute_wNAF(group, g_wNAF, g_scalar, bits, wsize) ||
Robert Sloan309a31e2018-01-29 10:22:47 -0800278 !compute_precomp(group, g_precomp, g, precomp_len, ctx)) {
279 goto err;
280 }
281 }
282
283 if (p_scalar != NULL) {
284 p_precomp = precomp_storage + total_precomp;
285 total_precomp += precomp_len;
Robert Sloanab8b8882018-03-26 11:39:51 -0700286 if (!ec_compute_wNAF(group, p_wNAF, p_scalar, bits, wsize) ||
Robert Sloan309a31e2018-01-29 10:22:47 -0800287 !compute_precomp(group, p_precomp, p, precomp_len, ctx)) {
288 goto err;
289 }
290 }
291
292 tmp = EC_POINT_new(group);
293 if (tmp == NULL ||
294 // |window_bits_for_scalar_size| assumes we do this step.
295 !EC_POINTs_make_affine(group, total_precomp, precomp_storage, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800296 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700297 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800298
Robert Sloan309a31e2018-01-29 10:22:47 -0800299 int r_is_at_infinity = 1;
300 for (size_t k = wNAF_len - 1; k < wNAF_len; k--) {
301 if (!r_is_at_infinity && !EC_POINT_dbl(group, r, r, ctx)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700302 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800303 }
304
Robert Sloan309a31e2018-01-29 10:22:47 -0800305 if (g_scalar != NULL) {
306 if (g_wNAF[k] != 0) {
307 if (!lookup_precomp(group, tmp, g_precomp, g_wNAF[k], ctx)) {
308 goto err;
309 }
310 if (r_is_at_infinity) {
311 if (!EC_POINT_copy(r, tmp)) {
312 goto err;
313 }
314 r_is_at_infinity = 0;
315 } else if (!EC_POINT_add(group, r, r, tmp, ctx)) {
316 goto err;
317 }
Adam Langleye9ada862015-05-11 17:20:37 -0700318 }
Robert Sloan309a31e2018-01-29 10:22:47 -0800319 }
320
321 if (p_scalar != NULL) {
322 if (p_wNAF[k] != 0) {
323 if (!lookup_precomp(group, tmp, p_precomp, p_wNAF[k], ctx)) {
324 goto err;
325 }
326 if (r_is_at_infinity) {
327 if (!EC_POINT_copy(r, tmp)) {
328 goto err;
329 }
330 r_is_at_infinity = 0;
331 } else if (!EC_POINT_add(group, r, r, tmp, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800332 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700333 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 }
335 }
336 }
337
Robert Sloan309a31e2018-01-29 10:22:47 -0800338 if (r_is_at_infinity &&
339 !EC_POINT_set_to_infinity(group, r)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700340 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800341 }
342
343 ret = 1;
344
345err:
Adam Langleye9ada862015-05-11 17:20:37 -0700346 BN_CTX_free(new_ctx);
347 EC_POINT_free(tmp);
Robert Sloan309a31e2018-01-29 10:22:47 -0800348 OPENSSL_cleanse(&g_wNAF, sizeof(g_wNAF));
349 OPENSSL_cleanse(&p_wNAF, sizeof(p_wNAF));
350 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(precomp_storage); i++) {
351 EC_POINT_free(precomp_storage[i]);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800353 return ret;
354}