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 #include <openssl/err.h>
17 #include <openssl/mem.h>
18
19 #include "internal.h"
20 #include "../bn/internal.h"
21 #include "../../internal.h"
22
23
ec_bignum_to_scalar(const EC_GROUP * group,EC_SCALAR * out,const BIGNUM * in)24 int ec_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
25 const BIGNUM *in) {
26 if (!bn_copy_words(out->words, group->order.width, in) ||
27 !bn_less_than_words(out->words, group->order.d, group->order.width)) {
28 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_SCALAR);
29 return 0;
30 }
31 return 1;
32 }
33
ec_scalar_equal_vartime(const EC_GROUP * group,const EC_SCALAR * a,const EC_SCALAR * b)34 int ec_scalar_equal_vartime(const EC_GROUP *group, const EC_SCALAR *a,
35 const EC_SCALAR *b) {
36 return OPENSSL_memcmp(a->words, b->words,
37 group->order.width * sizeof(BN_ULONG)) == 0;
38 }
39
ec_scalar_is_zero(const EC_GROUP * group,const EC_SCALAR * a)40 int ec_scalar_is_zero(const EC_GROUP *group, const EC_SCALAR *a) {
41 BN_ULONG mask = 0;
42 for (int i = 0; i < group->order.width; i++) {
43 mask |= a->words[i];
44 }
45 return mask == 0;
46 }
47
ec_random_nonzero_scalar(const EC_GROUP * group,EC_SCALAR * out,const uint8_t additional_data[32])48 int ec_random_nonzero_scalar(const EC_GROUP *group, EC_SCALAR *out,
49 const uint8_t additional_data[32]) {
50 return bn_rand_range_words(out->words, 1, group->order.d, group->order.width,
51 additional_data);
52 }
53
ec_scalar_to_bytes(const EC_GROUP * group,uint8_t * out,size_t * out_len,const EC_SCALAR * in)54 void ec_scalar_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len,
55 const EC_SCALAR *in) {
56 size_t len = BN_num_bytes(&group->order);
57 bn_words_to_big_endian(out, len, in->words, group->order.width);
58 *out_len = len;
59 }
60
ec_scalar_from_bytes(const EC_GROUP * group,EC_SCALAR * out,const uint8_t * in,size_t len)61 int ec_scalar_from_bytes(const EC_GROUP *group, EC_SCALAR *out,
62 const uint8_t *in, size_t len) {
63 if (len != BN_num_bytes(&group->order)) {
64 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_SCALAR);
65 return 0;
66 }
67
68 bn_big_endian_to_words(out->words, group->order.width, in, len);
69
70 if (!bn_less_than_words(out->words, group->order.d, group->order.width)) {
71 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_SCALAR);
72 return 0;
73 }
74
75 return 1;
76 }
77
ec_scalar_reduce(const EC_GROUP * group,EC_SCALAR * out,const BN_ULONG * words,size_t num)78 void ec_scalar_reduce(const EC_GROUP *group, EC_SCALAR *out,
79 const BN_ULONG *words, size_t num) {
80 // Convert "from" Montgomery form so the value is reduced modulo the order.
81 bn_from_montgomery_small(out->words, group->order.width, words, num,
82 group->order_mont);
83 // Convert "to" Montgomery form to remove the R^-1 factor added.
84 ec_scalar_to_montgomery(group, out, out);
85 }
86
ec_scalar_add(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a,const EC_SCALAR * b)87 void ec_scalar_add(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a,
88 const EC_SCALAR *b) {
89 const BIGNUM *order = &group->order;
90 BN_ULONG tmp[EC_MAX_WORDS];
91 bn_mod_add_words(r->words, a->words, b->words, order->d, tmp, order->width);
92 OPENSSL_cleanse(tmp, sizeof(tmp));
93 }
94
ec_scalar_sub(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a,const EC_SCALAR * b)95 void ec_scalar_sub(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a,
96 const EC_SCALAR *b) {
97 const BIGNUM *order = &group->order;
98 BN_ULONG tmp[EC_MAX_WORDS];
99 bn_mod_sub_words(r->words, a->words, b->words, order->d, tmp, order->width);
100 OPENSSL_cleanse(tmp, sizeof(tmp));
101 }
102
ec_scalar_neg(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a)103 void ec_scalar_neg(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a) {
104 EC_SCALAR zero;
105 OPENSSL_memset(&zero, 0, sizeof(EC_SCALAR));
106 ec_scalar_sub(group, r, &zero, a);
107 }
108
ec_scalar_select(const EC_GROUP * group,EC_SCALAR * out,BN_ULONG mask,const EC_SCALAR * a,const EC_SCALAR * b)109 void ec_scalar_select(const EC_GROUP *group, EC_SCALAR *out, BN_ULONG mask,
110 const EC_SCALAR *a, const EC_SCALAR *b) {
111 const BIGNUM *order = &group->order;
112 bn_select_words(out->words, mask, a->words, b->words, order->width);
113 }
114
ec_scalar_to_montgomery(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a)115 void ec_scalar_to_montgomery(const EC_GROUP *group, EC_SCALAR *r,
116 const EC_SCALAR *a) {
117 const BIGNUM *order = &group->order;
118 bn_to_montgomery_small(r->words, a->words, order->width, group->order_mont);
119 }
120
ec_scalar_from_montgomery(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a)121 void ec_scalar_from_montgomery(const EC_GROUP *group, EC_SCALAR *r,
122 const EC_SCALAR *a) {
123 const BIGNUM *order = &group->order;
124 bn_from_montgomery_small(r->words, order->width, a->words, order->width,
125 group->order_mont);
126 }
127
ec_scalar_mul_montgomery(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a,const EC_SCALAR * b)128 void ec_scalar_mul_montgomery(const EC_GROUP *group, EC_SCALAR *r,
129 const EC_SCALAR *a, const EC_SCALAR *b) {
130 const BIGNUM *order = &group->order;
131 bn_mod_mul_montgomery_small(r->words, a->words, b->words, order->width,
132 group->order_mont);
133 }
134
ec_simple_scalar_inv0_montgomery(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a)135 void ec_simple_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r,
136 const EC_SCALAR *a) {
137 const BIGNUM *order = &group->order;
138 bn_mod_inverse0_prime_mont_small(r->words, a->words, order->width,
139 group->order_mont);
140 }
141
ec_simple_scalar_to_montgomery_inv_vartime(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a)142 int ec_simple_scalar_to_montgomery_inv_vartime(const EC_GROUP *group,
143 EC_SCALAR *r,
144 const EC_SCALAR *a) {
145 if (ec_scalar_is_zero(group, a)) {
146 return 0;
147 }
148
149 // This implementation (in fact) runs in constant time,
150 // even though for this interface it is not mandatory.
151
152 // r = a^-1 in the Montgomery domain. This is
153 // |ec_scalar_to_montgomery| followed by |ec_scalar_inv0_montgomery|, but
154 // |ec_scalar_inv0_montgomery| followed by |ec_scalar_from_montgomery| is
155 // equivalent and slightly more efficient.
156 ec_scalar_inv0_montgomery(group, r, a);
157 ec_scalar_from_montgomery(group, r, r);
158 return 1;
159 }
160
ec_scalar_inv0_montgomery(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a)161 void ec_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r,
162 const EC_SCALAR *a) {
163 group->meth->scalar_inv0_montgomery(group, r, a);
164 }
165
ec_scalar_to_montgomery_inv_vartime(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a)166 int ec_scalar_to_montgomery_inv_vartime(const EC_GROUP *group, EC_SCALAR *r,
167 const EC_SCALAR *a) {
168 return group->meth->scalar_to_montgomery_inv_vartime(group, r, a);
169 }
170