| 1 | /* Copyright (c) 2018, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/ec.h> |
| 16 | |
| 17 | #include <assert.h> |
| 18 | |
| 19 | #include "internal.h" |
| 20 | #include "../bn/internal.h" |
| 21 | #include "../../internal.h" |
| 22 | |
| 23 | |
| 24 | void ec_GFp_mont_mul(const EC_GROUP *group, EC_RAW_POINT *r, |
| 25 | const EC_RAW_POINT *p, const EC_SCALAR *scalar) { |
| 26 | // This is a generic implementation for uncommon curves that not do not |
| 27 | // warrant a tuned one. It uses unsigned digits so that the doubling case in |
| 28 | // |ec_GFp_mont_add| is always unreachable, erring on safety and simplicity. |
| 29 | |
| 30 | // Compute a table of the first 32 multiples of |p| (including infinity). |
| 31 | EC_RAW_POINT precomp[32]; |
| 32 | ec_GFp_simple_point_set_to_infinity(group, &precomp[0]); |
| 33 | ec_GFp_simple_point_copy(&precomp[1], p); |
| 34 | for (size_t j = 2; j < OPENSSL_ARRAY_SIZE(precomp); j++) { |
| 35 | if (j & 1) { |
| 36 | ec_GFp_mont_add(group, &precomp[j], &precomp[1], &precomp[j - 1]); |
| 37 | } else { |
| 38 | ec_GFp_mont_dbl(group, &precomp[j], &precomp[j / 2]); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Divide bits in |scalar| into windows. |
| 43 | unsigned bits = BN_num_bits(&group->order); |
| 44 | int r_is_at_infinity = 1; |
| 45 | for (unsigned i = bits - 1; i < bits; i--) { |
| 46 | if (!r_is_at_infinity) { |
| 47 | ec_GFp_mont_dbl(group, r, r); |
| 48 | } |
| 49 | if (i % 5 == 0) { |
| 50 | // Compute the next window value. |
| 51 | const size_t width = group->order.width; |
| 52 | uint8_t window = bn_is_bit_set_words(scalar->words, width, i + 4) << 4; |
| 53 | window |= bn_is_bit_set_words(scalar->words, width, i + 3) << 3; |
| 54 | window |= bn_is_bit_set_words(scalar->words, width, i + 2) << 2; |
| 55 | window |= bn_is_bit_set_words(scalar->words, width, i + 1) << 1; |
| 56 | window |= bn_is_bit_set_words(scalar->words, width, i); |
| 57 | |
| 58 | // Select the entry in constant-time. |
| 59 | EC_RAW_POINT tmp; |
| 60 | OPENSSL_memset(&tmp, 0, sizeof(EC_RAW_POINT)); |
| 61 | for (size_t j = 0; j < OPENSSL_ARRAY_SIZE(precomp); j++) { |
| 62 | BN_ULONG mask = constant_time_eq_w(j, window); |
| 63 | ec_felem_select(group, &tmp.X, mask, &precomp[j].X, &tmp.X); |
| 64 | ec_felem_select(group, &tmp.Y, mask, &precomp[j].Y, &tmp.Y); |
| 65 | ec_felem_select(group, &tmp.Z, mask, &precomp[j].Z, &tmp.Z); |
| 66 | } |
| 67 | |
| 68 | if (r_is_at_infinity) { |
| 69 | ec_GFp_simple_point_copy(r, &tmp); |
| 70 | r_is_at_infinity = 0; |
| 71 | } else { |
| 72 | ec_GFp_mont_add(group, r, r, &tmp); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | if (r_is_at_infinity) { |
| 77 | ec_GFp_simple_point_set_to_infinity(group, r); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void ec_GFp_mont_mul_base(const EC_GROUP *group, EC_RAW_POINT *r, |
| 82 | const EC_SCALAR *scalar) { |
| 83 | ec_GFp_mont_mul(group, r, &group->generator->raw, scalar); |
| 84 | } |
| 85 | |