Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/ec/simple.c

raw
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 <string.h>
13
14 #include <openssl/bn.h>
15 #include <openssl/err.h>
16 #include <openssl/mem.h>
17
18 #include "internal.h"
19 #include "../../internal.h"
20
21
22 // Most method functions in this file are designed to work with non-trivial
23 // representations of field elements if necessary (see ecp_mont.c): while
24 // standard modular addition and subtraction are used, the field_mul and
25 // field_sqr methods will be used for multiplication, and field_encode and
26 // field_decode (if defined) will be used for converting between
27 // representations.
28 //
29 // Functions here specifically assume that if a non-trivial representation is
30 // used, it is a Montgomery representation (i.e. 'encoding' means multiplying
31 // by some factor R).
32
33 int ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p,
34 const BIGNUM *a, const BIGNUM *b,
35 BN_CTX *ctx) {
36 // p must be a prime > 3
37 if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
38 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
39 return 0;
40 }
41
42 int ret = 0;
43 BN_CTX_start(ctx);
44 BIGNUM *tmp = BN_CTX_get(ctx);
45 if (tmp == NULL) {
46 goto err;
47 }
48
49 if (!BN_MONT_CTX_set(&group->field, p, ctx) ||
50 !ec_bignum_to_felem(group, &group->a, a) ||
51 !ec_bignum_to_felem(group, &group->b, b) ||
52 // Reuse Z from the generator to cache the value one.
53 !ec_bignum_to_felem(group, &group->generator.raw.Z, BN_value_one())) {
54 goto err;
55 }
56
57 // group->a_is_minus3
58 if (!BN_copy(tmp, a) ||
59 !BN_add_word(tmp, 3)) {
60 goto err;
61 }
62 group->a_is_minus3 = (0 == BN_cmp(tmp, &group->field.N));
63
64 ret = 1;
65
66 err:
67 BN_CTX_end(ctx);
68 return ret;
69 }
70
71 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
72 BIGNUM *b) {
73 if ((p != NULL && !BN_copy(p, &group->field.N)) ||
74 (a != NULL && !ec_felem_to_bignum(group, a, &group->a)) ||
75 (b != NULL && !ec_felem_to_bignum(group, b, &group->b))) {
76 return 0;
77 }
78 return 1;
79 }
80
81 void ec_GFp_simple_point_init(EC_JACOBIAN *point) {
82 OPENSSL_memset(&point->X, 0, sizeof(EC_FELEM));
83 OPENSSL_memset(&point->Y, 0, sizeof(EC_FELEM));
84 OPENSSL_memset(&point->Z, 0, sizeof(EC_FELEM));
85 }
86
87 void ec_GFp_simple_point_copy(EC_JACOBIAN *dest, const EC_JACOBIAN *src) {
88 OPENSSL_memcpy(&dest->X, &src->X, sizeof(EC_FELEM));
89 OPENSSL_memcpy(&dest->Y, &src->Y, sizeof(EC_FELEM));
90 OPENSSL_memcpy(&dest->Z, &src->Z, sizeof(EC_FELEM));
91 }
92
93 void ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
94 EC_JACOBIAN *point) {
95 // Although it is strictly only necessary to zero Z, we zero the entire point
96 // in case |point| was stack-allocated and yet to be initialized.
97 ec_GFp_simple_point_init(point);
98 }
99
100 void ec_GFp_simple_invert(const EC_GROUP *group, EC_JACOBIAN *point) {
101 ec_felem_neg(group, &point->Y, &point->Y);
102 }
103
104 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group,
105 const EC_JACOBIAN *point) {
106 return ec_felem_non_zero_mask(group, &point->Z) == 0;
107 }
108
109 int ec_GFp_simple_is_on_curve(const EC_GROUP *group,
110 const EC_JACOBIAN *point) {
111 // We have a curve defined by a Weierstrass equation
112 // y^2 = x^3 + a*x + b.
113 // The point to consider is given in Jacobian projective coordinates
114 // where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
115 // Substituting this and multiplying by Z^6 transforms the above equation
116 // into
117 // Y^2 = X^3 + a*X*Z^4 + b*Z^6.
118 // To test this, we add up the right-hand side in 'rh'.
119 //
120 // This function may be used when double-checking the secret result of a point
121 // multiplication, so we proceed in constant-time.
122
123 void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
124 const EC_FELEM *b) = group->meth->felem_mul;
125 void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
126 group->meth->felem_sqr;
127
128 // rh := X^2
129 EC_FELEM rh;
130 felem_sqr(group, &rh, &point->X);
131
132 EC_FELEM tmp, Z4, Z6;
133 felem_sqr(group, &tmp, &point->Z);
134 felem_sqr(group, &Z4, &tmp);
135 felem_mul(group, &Z6, &Z4, &tmp);
136
137 // rh := rh + a*Z^4
138 if (group->a_is_minus3) {
139 ec_felem_add(group, &tmp, &Z4, &Z4);
140 ec_felem_add(group, &tmp, &tmp, &Z4);
141 ec_felem_sub(group, &rh, &rh, &tmp);
142 } else {
143 felem_mul(group, &tmp, &Z4, &group->a);
144 ec_felem_add(group, &rh, &rh, &tmp);
145 }
146
147 // rh := (rh + a*Z^4)*X
148 felem_mul(group, &rh, &rh, &point->X);
149
150 // rh := rh + b*Z^6
151 felem_mul(group, &tmp, &group->b, &Z6);
152 ec_felem_add(group, &rh, &rh, &tmp);
153
154 // 'lh' := Y^2
155 felem_sqr(group, &tmp, &point->Y);
156
157 ec_felem_sub(group, &tmp, &tmp, &rh);
158 BN_ULONG not_equal = ec_felem_non_zero_mask(group, &tmp);
159
160 // If Z = 0, the point is infinity, which is always on the curve.
161 BN_ULONG not_infinity = ec_felem_non_zero_mask(group, &point->Z);
162
163 return 1 & ~(not_infinity & not_equal);
164 }
165
166 int ec_GFp_simple_points_equal(const EC_GROUP *group, const EC_JACOBIAN *a,
167 const EC_JACOBIAN *b) {
168 // This function is implemented in constant-time for two reasons. First,
169 // although EC points are usually public, their Jacobian Z coordinates may be
170 // secret, or at least are not obviously public. Second, more complex
171 // protocols will sometimes manipulate secret points.
172 //
173 // This does mean that we pay a 6M+2S Jacobian comparison when comparing two
174 // publicly affine points costs no field operations at all. If needed, we can
175 // restore this optimization by keeping better track of affine vs. Jacobian
176 // forms. See https://crbug.com/boringssl/326.
177
178 // If neither |a| or |b| is infinity, we have to decide whether
179 // (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
180 // or equivalently, whether
181 // (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
182
183 void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
184 const EC_FELEM *b) = group->meth->felem_mul;
185 void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
186 group->meth->felem_sqr;
187
188 EC_FELEM tmp1, tmp2, Za23, Zb23;
189 felem_sqr(group, &Zb23, &b->Z); // Zb23 = Z_b^2
190 felem_mul(group, &tmp1, &a->X, &Zb23); // tmp1 = X_a * Z_b^2
191 felem_sqr(group, &Za23, &a->Z); // Za23 = Z_a^2
192 felem_mul(group, &tmp2, &b->X, &Za23); // tmp2 = X_b * Z_a^2
193 ec_felem_sub(group, &tmp1, &tmp1, &tmp2);
194 const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp1);
195
196 felem_mul(group, &Zb23, &Zb23, &b->Z); // Zb23 = Z_b^3
197 felem_mul(group, &tmp1, &a->Y, &Zb23); // tmp1 = Y_a * Z_b^3
198 felem_mul(group, &Za23, &Za23, &a->Z); // Za23 = Z_a^3
199 felem_mul(group, &tmp2, &b->Y, &Za23); // tmp2 = Y_b * Z_a^3
200 ec_felem_sub(group, &tmp1, &tmp1, &tmp2);
201 const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp1);
202 const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal);
203
204 const BN_ULONG a_not_infinity = ec_felem_non_zero_mask(group, &a->Z);
205 const BN_ULONG b_not_infinity = ec_felem_non_zero_mask(group, &b->Z);
206 const BN_ULONG a_and_b_infinity = ~(a_not_infinity | b_not_infinity);
207
208 const BN_ULONG equal =
209 a_and_b_infinity | (a_not_infinity & b_not_infinity & x_and_y_equal);
210 return equal & 1;
211 }
212
213 int ec_affine_jacobian_equal(const EC_GROUP *group, const EC_AFFINE *a,
214 const EC_JACOBIAN *b) {
215 // If |b| is not infinity, we have to decide whether
216 // (X_a, Y_a) = (X_b/Z_b^2, Y_b/Z_b^3),
217 // or equivalently, whether
218 // (X_a*Z_b^2, Y_a*Z_b^3) = (X_b, Y_b).
219
220 void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
221 const EC_FELEM *b) = group->meth->felem_mul;
222 void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
223 group->meth->felem_sqr;
224
225 EC_FELEM tmp, Zb2;
226 felem_sqr(group, &Zb2, &b->Z); // Zb2 = Z_b^2
227 felem_mul(group, &tmp, &a->X, &Zb2); // tmp = X_a * Z_b^2
228 ec_felem_sub(group, &tmp, &tmp, &b->X);
229 const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp);
230
231 felem_mul(group, &tmp, &a->Y, &Zb2); // tmp = Y_a * Z_b^2
232 felem_mul(group, &tmp, &tmp, &b->Z); // tmp = Y_a * Z_b^3
233 ec_felem_sub(group, &tmp, &tmp, &b->Y);
234 const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp);
235 const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal);
236
237 const BN_ULONG b_not_infinity = ec_felem_non_zero_mask(group, &b->Z);
238
239 const BN_ULONG equal = b_not_infinity & x_and_y_equal;
240 return equal & 1;
241 }
242
243 int ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_JACOBIAN *p,
244 const EC_SCALAR *r) {
245 if (ec_GFp_simple_is_at_infinity(group, p)) {
246 // |ec_get_x_coordinate_as_scalar| will check this internally, but this way
247 // we do not push to the error queue.
248 return 0;
249 }
250
251 EC_SCALAR x;
252 return ec_get_x_coordinate_as_scalar(group, &x, p) &&
253 ec_scalar_equal_vartime(group, &x, r);
254 }
255
256 void ec_GFp_simple_felem_to_bytes(const EC_GROUP *group, uint8_t *out,
257 size_t *out_len, const EC_FELEM *in) {
258 size_t len = BN_num_bytes(&group->field.N);
259 bn_words_to_big_endian(out, len, in->words, group->field.N.width);
260 *out_len = len;
261 }
262
263 int ec_GFp_simple_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out,
264 const uint8_t *in, size_t len) {
265 if (len != BN_num_bytes(&group->field.N)) {
266 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
267 return 0;
268 }
269
270 bn_big_endian_to_words(out->words, group->field.N.width, in, len);
271
272 if (!bn_less_than_words(out->words, group->field.N.d, group->field.N.width)) {
273 OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
274 return 0;
275 }
276
277 return 1;
278 }