| 1 |
// Originally written by Bodo Moeller for the OpenSSL project. |
| 2 |
// Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 |
// |
| 5 |
// The elliptic curve binary polynomial software is originally written by |
| 6 |
// Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems |
| 7 |
// Laboratories. |
| 8 |
// SPDX-License-Identifier: Apache-2.0 |
| 9 |
|
| 10 |
#include <openssl/ec.h> |
| 11 |
|
| 12 |
#include <assert.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include <openssl/bn.h> |
| 16 |
#include <openssl/err.h> |
| 17 |
#include <openssl/mem.h> |
| 18 |
#include <openssl/thread.h> |
| 19 |
|
| 20 |
#include "internal.h" |
| 21 |
#include "../bn/internal.h" |
| 22 |
#include "../../internal.h" |
| 23 |
|
| 24 |
|
| 25 |
// This file implements the wNAF-based interleaving multi-exponentiation method |
| 26 |
// at: |
| 27 |
// http://link.springer.com/chapter/10.1007%2F3-540-45537-X_13 |
| 28 |
// http://www.bmoeller.de/pdf/TI-01-08.multiexp.pdf |
| 29 |
|
| 30 |
void ec_compute_wNAF(int8_t *out, const EC_SCALAR *scalar, size_t bits, int w) { |
| 31 |
// 'int8_t' can represent integers with absolute values less than 2^7. |
| 32 |
assert(0 < w && w <= 7); |
| 33 |
assert(bits != 0); |
| 34 |
int bit = 1 << w; // 2^w, at most 128 |
| 35 |
int next_bit = bit << 1; // 2^(w+1), at most 256 |
| 36 |
int mask = next_bit - 1; // at most 255 |
| 37 |
|
| 38 |
int window_val = scalar->words[0] & mask; |
| 39 |
for (size_t j = 0; j < bits + 1; j++) { |
| 40 |
assert(0 <= window_val && window_val <= next_bit); |
| 41 |
int digit = 0; |
| 42 |
if (window_val & 1) { |
| 43 |
assert(0 < window_val && window_val < next_bit); |
| 44 |
if (window_val & bit) { |
| 45 |
digit = window_val - next_bit; |
| 46 |
// We know -next_bit < digit < 0 and window_val - digit = next_bit. |
| 47 |
|
| 48 |
// modified wNAF |
| 49 |
if (j + w + 1 >= bits) { |
| 50 |
// special case for generating modified wNAFs: |
| 51 |
// no new bits will be added into window_val, |
| 52 |
// so using a positive digit here will decrease |
| 53 |
// the total length of the representation |
| 54 |
|
| 55 |
digit = window_val & (mask >> 1); |
| 56 |
// We know 0 < digit < bit and window_val - digit = bit. |
| 57 |
} |
| 58 |
} else { |
| 59 |
digit = window_val; |
| 60 |
// We know 0 < digit < bit and window_val - digit = 0. |
| 61 |
} |
| 62 |
|
| 63 |
window_val -= digit; |
| 64 |
|
| 65 |
// Now window_val is 0 or 2^(w+1) in standard wNAF generation. |
| 66 |
// For modified window NAFs, it may also be 2^w. |
| 67 |
// |
| 68 |
// See the comments above for the derivation of each of these bounds. |
| 69 |
assert(window_val == 0 || window_val == next_bit || window_val == bit); |
| 70 |
assert(-bit < digit && digit < bit); |
| 71 |
|
| 72 |
// window_val was odd, so digit is also odd. |
| 73 |
assert(digit & 1); |
| 74 |
} |
| 75 |
|
| 76 |
out[j] = digit; |
| 77 |
|
| 78 |
// Incorporate the next bit. Previously, |window_val| <= |next_bit|, so if |
| 79 |
// we shift and add at most one copy of |bit|, this will continue to hold |
| 80 |
// afterwards. |
| 81 |
window_val >>= 1; |
| 82 |
const size_t bits_per_word = sizeof(scalar->words[0]) * 8; |
| 83 |
const size_t num_words = (bits + bits_per_word - 1) / bits_per_word; |
| 84 |
window_val += bit * bn_is_bit_set_words(scalar->words, num_words, j + w + 1); |
| 85 |
assert(window_val <= next_bit); |
| 86 |
} |
| 87 |
|
| 88 |
// bits + 1 entries should be sufficient to consume all bits. |
| 89 |
assert(window_val == 0); |
| 90 |
} |
| 91 |
|
| 92 |
// compute_precomp sets |out[i]| to (2*i+1)*p, for i from 0 to |len|. |
| 93 |
static void compute_precomp(const EC_GROUP *group, EC_JACOBIAN *out, |
| 94 |
const EC_JACOBIAN *p, size_t len) { |
| 95 |
ec_GFp_simple_point_copy(&out[0], p); |
| 96 |
EC_JACOBIAN two_p; |
| 97 |
ec_GFp_mont_dbl(group, &two_p, p); |
| 98 |
for (size_t i = 1; i < len; i++) { |
| 99 |
ec_GFp_mont_add(group, &out[i], &out[i - 1], &two_p); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
static void lookup_precomp(const EC_GROUP *group, EC_JACOBIAN *out, |
| 104 |
const EC_JACOBIAN *precomp, int digit) { |
| 105 |
if (digit < 0) { |
| 106 |
digit = -digit; |
| 107 |
ec_GFp_simple_point_copy(out, &precomp[digit >> 1]); |
| 108 |
ec_GFp_simple_invert(group, out); |
| 109 |
} else { |
| 110 |
ec_GFp_simple_point_copy(out, &precomp[digit >> 1]); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// EC_WNAF_WINDOW_BITS is the window size to use for |ec_GFp_mont_mul_public|. |
| 115 |
#define EC_WNAF_WINDOW_BITS 4 |
| 116 |
|
| 117 |
// EC_WNAF_TABLE_SIZE is the table size to use for |ec_GFp_mont_mul_public|. |
| 118 |
#define EC_WNAF_TABLE_SIZE (1 << (EC_WNAF_WINDOW_BITS - 1)) |
| 119 |
|
| 120 |
// EC_WNAF_STACK is the number of points worth of data to stack-allocate and |
| 121 |
// avoid a malloc. |
| 122 |
#define EC_WNAF_STACK 3 |
| 123 |
|
| 124 |
int ec_GFp_mont_mul_public_batch(const EC_GROUP *group, EC_JACOBIAN *r, |
| 125 |
const EC_SCALAR *g_scalar, |
| 126 |
const EC_JACOBIAN *points, |
| 127 |
const EC_SCALAR *scalars, size_t num) { |
| 128 |
size_t bits = EC_GROUP_order_bits(group); |
| 129 |
size_t wNAF_len = bits + 1; |
| 130 |
|
| 131 |
int ret = 0; |
| 132 |
int8_t wNAF_stack[EC_WNAF_STACK][EC_MAX_BYTES * 8 + 1]; |
| 133 |
int8_t (*wNAF_alloc)[EC_MAX_BYTES * 8 + 1] = NULL; |
| 134 |
int8_t (*wNAF)[EC_MAX_BYTES * 8 + 1]; |
| 135 |
EC_JACOBIAN precomp_stack[EC_WNAF_STACK][EC_WNAF_TABLE_SIZE]; |
| 136 |
EC_JACOBIAN (*precomp_alloc)[EC_WNAF_TABLE_SIZE] = NULL; |
| 137 |
EC_JACOBIAN (*precomp)[EC_WNAF_TABLE_SIZE]; |
| 138 |
if (num <= EC_WNAF_STACK) { |
| 139 |
wNAF = wNAF_stack; |
| 140 |
precomp = precomp_stack; |
| 141 |
} else { |
| 142 |
wNAF_alloc = OPENSSL_calloc(num, sizeof(wNAF_alloc[0])); |
| 143 |
precomp_alloc = OPENSSL_calloc(num, sizeof(precomp_alloc[0])); |
| 144 |
if (wNAF_alloc == NULL || precomp_alloc == NULL) { |
| 145 |
goto err; |
| 146 |
} |
| 147 |
wNAF = wNAF_alloc; |
| 148 |
precomp = precomp_alloc; |
| 149 |
} |
| 150 |
|
| 151 |
int8_t g_wNAF[EC_MAX_BYTES * 8 + 1]; |
| 152 |
EC_JACOBIAN g_precomp[EC_WNAF_TABLE_SIZE]; |
| 153 |
assert(wNAF_len <= OPENSSL_ARRAY_SIZE(g_wNAF)); |
| 154 |
const EC_JACOBIAN *g = &group->generator.raw; |
| 155 |
if (g_scalar != NULL) { |
| 156 |
ec_compute_wNAF(g_wNAF, g_scalar, bits, EC_WNAF_WINDOW_BITS); |
| 157 |
compute_precomp(group, g_precomp, g, EC_WNAF_TABLE_SIZE); |
| 158 |
} |
| 159 |
|
| 160 |
for (size_t i = 0; i < num; i++) { |
| 161 |
assert(wNAF_len <= OPENSSL_ARRAY_SIZE(wNAF[i])); |
| 162 |
ec_compute_wNAF(wNAF[i], &scalars[i], bits, EC_WNAF_WINDOW_BITS); |
| 163 |
compute_precomp(group, precomp[i], &points[i], EC_WNAF_TABLE_SIZE); |
| 164 |
} |
| 165 |
|
| 166 |
EC_JACOBIAN tmp; |
| 167 |
int r_is_at_infinity = 1; |
| 168 |
for (size_t k = wNAF_len - 1; k < wNAF_len; k--) { |
| 169 |
if (!r_is_at_infinity) { |
| 170 |
ec_GFp_mont_dbl(group, r, r); |
| 171 |
} |
| 172 |
|
| 173 |
if (g_scalar != NULL && g_wNAF[k] != 0) { |
| 174 |
lookup_precomp(group, &tmp, g_precomp, g_wNAF[k]); |
| 175 |
if (r_is_at_infinity) { |
| 176 |
ec_GFp_simple_point_copy(r, &tmp); |
| 177 |
r_is_at_infinity = 0; |
| 178 |
} else { |
| 179 |
ec_GFp_mont_add(group, r, r, &tmp); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
for (size_t i = 0; i < num; i++) { |
| 184 |
if (wNAF[i][k] != 0) { |
| 185 |
lookup_precomp(group, &tmp, precomp[i], wNAF[i][k]); |
| 186 |
if (r_is_at_infinity) { |
| 187 |
ec_GFp_simple_point_copy(r, &tmp); |
| 188 |
r_is_at_infinity = 0; |
| 189 |
} else { |
| 190 |
ec_GFp_mont_add(group, r, r, &tmp); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if (r_is_at_infinity) { |
| 197 |
ec_GFp_simple_point_set_to_infinity(group, r); |
| 198 |
} |
| 199 |
|
| 200 |
ret = 1; |
| 201 |
|
| 202 |
err: |
| 203 |
OPENSSL_free(wNAF_alloc); |
| 204 |
OPENSSL_free(precomp_alloc); |
| 205 |
return ret; |
| 206 |
} |