Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 75 | #include <openssl/thread.h> |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 76 | #include <openssl/type_check.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 77 | |
| 78 | #include "internal.h" |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 79 | #include "../bn/internal.h" |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 80 | #include "../../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 81 | |
| 82 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 83 | // 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 87 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 88 | int ec_compute_wNAF(const EC_GROUP *group, int8_t *out, const EC_SCALAR *scalar, |
| 89 | size_t bits, int w) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 90 | // 'int8_t' can represent integers with absolute values less than 2^7. |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 91 | if (w <= 0 || w > 7 || bits == 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 92 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 93 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 94 | } |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 95 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 98 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 99 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 103 | int digit = 0; |
| 104 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 105 | // 0 <= window_val <= 2^(w+1) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 106 | |
| 107 | if (window_val & 1) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 108 | // 0 < window_val < 2^(w+1) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 109 | |
| 110 | if (window_val & bit) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 111 | digit = window_val - next_bit; // -2^w < digit < 0 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 112 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 113 | #if 1 // modified wNAF |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 114 | if (j + w + 1 >= bits) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 115 | // 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 119 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 120 | digit = window_val & (mask >> 1); // 0 < digit < 2^w |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 121 | } |
| 122 | #endif |
| 123 | } else { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 124 | digit = window_val; // 0 < digit < 2^w |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | if (digit <= -bit || digit >= bit || !(digit & 1)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 128 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 129 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | window_val -= digit; |
| 133 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 134 | // 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 136 | if (window_val != 0 && window_val != next_bit && window_val != bit) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 137 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 138 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 142 | out[j++] = digit; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 143 | |
| 144 | window_val >>= 1; |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 145 | window_val += |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 146 | bit * bn_is_bit_set_words(scalar->words, group->order.width, j + w); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 147 | |
| 148 | if (window_val > next_bit) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 149 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 150 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 154 | // Fill the rest of the wNAF with zeros. |
| 155 | if (j > bits + 1) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 156 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 157 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 158 | } |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 159 | for (size_t i = j; i < bits + 1; i++) { |
| 160 | out[i] = 0; |
| 161 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 162 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 163 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 166 | // 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 Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 169 | static size_t window_bits_for_scalar_size(size_t b) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 170 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 184 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 185 | // 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. |
| 192 | static 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 | |
| 217 | err: |
| 218 | EC_POINT_free(two_p); |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static 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 | |
| 233 | int 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 235 | BN_CTX *new_ctx = NULL; |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 236 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 240 | EC_POINT *tmp = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 241 | int ret = 0; |
| 242 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 243 | if (ctx == NULL) { |
| 244 | ctx = new_ctx = BN_CTX_new(); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 245 | if (ctx == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 246 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 247 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 248 | } |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 249 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 250 | 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 Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 254 | |
| 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 Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 259 | if (wNAF_len > OPENSSL_ARRAY_SIZE(g_wNAF) || |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 260 | 2 * precomp_len > OPENSSL_ARRAY_SIZE(precomp_storage)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 261 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 262 | goto err; |
| 263 | } |
| 264 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 265 | // 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 Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 277 | if (!ec_compute_wNAF(group, g_wNAF, g_scalar, bits, wsize) || |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 278 | !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 Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 286 | if (!ec_compute_wNAF(group, p_wNAF, p_scalar, bits, wsize) || |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 287 | !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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 297 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 298 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 299 | 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 Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 302 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 305 | 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 Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 318 | } |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 319 | } |
| 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 332 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 333 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 338 | if (r_is_at_infinity && |
| 339 | !EC_POINT_set_to_infinity(group, r)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 340 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | ret = 1; |
| 344 | |
| 345 | err: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 346 | BN_CTX_free(new_ctx); |
| 347 | EC_POINT_free(tmp); |
Robert Sloan | 309a31e | 2018-01-29 10:22:47 -0800 | [diff] [blame] | 348 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 352 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 353 | return ret; |
| 354 | } |