Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/ec/internal.h

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 //
9 // SPDX-License-Identifier: Apache-2.0
10
11 #ifndef OPENSSL_HEADER_EC_INTERNAL_H
12 #define OPENSSL_HEADER_EC_INTERNAL_H
13
14 #include <openssl/base.h>
15
16 #include <openssl/bn.h>
17 #include <openssl/ec.h>
18 #include <openssl/ex_data.h>
19 #include <openssl/type_check.h>
20
21 #include "../bn/internal.h"
22
23 #if defined(__cplusplus)
24 extern "C" {
25 #endif
26
27 // ECDH_compute_shared_secret calculates the shared key between |pub_key| and
28 // |priv_key|. This function is called internally by |ECDH_compute_key| and
29 // |ECDH_compute_key_fips|. The shared secret is returned in |buf|, the value
30 // stored in |buflen| on entry is expected to be EC_MAX_BYTES or the number of
31 // bytes of the field element of the underlying curve. On exit, |buflen| is set
32 // to the actual number of bytes of the shared secret.
33 int ECDH_compute_shared_secret(uint8_t *buf, size_t *buflen,
34 const EC_POINT *pub_key, const EC_KEY *priv_key);
35
36 // EC internals.
37
38
39 // Cap the size of all field elements and scalars, including custom curves, to
40 // 66 bytes, large enough to fit secp521r1 and brainpoolP512r1, which appear to
41 // be the largest fields anyone plausibly uses.
42 #define EC_MAX_BYTES 66
43 #define EC_MAX_WORDS ((EC_MAX_BYTES + BN_BYTES - 1) / BN_BYTES)
44 #define EC_MAX_COMPRESSED (EC_MAX_BYTES + 1)
45 #define EC_MAX_UNCOMPRESSED (2 * EC_MAX_BYTES + 1)
46
47 #define EC_P521R1_FIELD_ELEM_BYTES 66
48 #define EC_P384R1_FIELD_ELEM_BYTES 48
49 #define EC_P256R1_FIELD_ELEM_BYTES 32
50 #define EC_P224R1_FIELD_ELEM_BYTES 28
51 #define EC_P256K1_FIELD_ELEM_BYTES 32
52
53 OPENSSL_STATIC_ASSERT(EC_MAX_WORDS <= BN_SMALL_MAX_WORDS,
54 bn__small_functions_not_usable)
55
56
57 // Scalars.
58
59 // An EC_SCALAR is an integer fully reduced modulo the order. Only the first
60 // |order->width| words are used. An |EC_SCALAR| is specific to an |EC_GROUP|
61 // and must not be mixed between groups.
62 typedef struct {
63 // words is the representation of the scalar in little-endian order.
64 BN_ULONG words[EC_MAX_WORDS];
65 } EC_SCALAR;
66
67 // ec_bignum_to_scalar converts |in| to an |EC_SCALAR| and writes it to
68 // |*out|. It returns one on success and zero if |in| is out of range.
69 OPENSSL_EXPORT int ec_bignum_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
70 const BIGNUM *in);
71
72 // ec_scalar_to_bytes serializes |in| as a big-endian bytestring to |out| and
73 // sets |*out_len| to the number of bytes written. The number of bytes written
74 // is |BN_num_bytes(&group->order)|, which is at most |EC_MAX_BYTES|.
75 OPENSSL_EXPORT void ec_scalar_to_bytes(const EC_GROUP *group, uint8_t *out,
76 size_t *out_len, const EC_SCALAR *in);
77
78 // ec_scalar_from_bytes deserializes |in| and stores the resulting scalar over
79 // group |group| to |out|. It returns one on success and zero if |in| is
80 // invalid.
81 OPENSSL_EXPORT int ec_scalar_from_bytes(const EC_GROUP *group, EC_SCALAR *out,
82 const uint8_t *in, size_t len);
83
84 // ec_scalar_reduce sets |out| to |words|, reduced modulo the group order.
85 // |words| must be less than order^2. |num| must be at most twice the width of
86 // group order. This function treats |words| as secret.
87 void ec_scalar_reduce(const EC_GROUP *group, EC_SCALAR *out,
88 const BN_ULONG *words, size_t num);
89
90 // ec_random_nonzero_scalar sets |out| to a uniformly selected random value from
91 // 1 to |group->order| - 1. It returns one on success and zero on error.
92 int ec_random_nonzero_scalar(const EC_GROUP *group, EC_SCALAR *out,
93 const uint8_t additional_data[32]);
94
95 // ec_scalar_equal_vartime returns one if |a| and |b| are equal and zero
96 // otherwise. Both values are treated as public.
97 int ec_scalar_equal_vartime(const EC_GROUP *group, const EC_SCALAR *a,
98 const EC_SCALAR *b);
99
100 // ec_scalar_is_zero returns one if |a| is zero and zero otherwise.
101 int ec_scalar_is_zero(const EC_GROUP *group, const EC_SCALAR *a);
102
103 // ec_scalar_add sets |r| to |a| + |b|.
104 void ec_scalar_add(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a,
105 const EC_SCALAR *b);
106
107 // ec_scalar_sub sets |r| to |a| - |b|.
108 void ec_scalar_sub(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a,
109 const EC_SCALAR *b);
110
111 // ec_scalar_neg sets |r| to -|a|.
112 void ec_scalar_neg(const EC_GROUP *group, EC_SCALAR *r, const EC_SCALAR *a);
113
114 // ec_scalar_to_montgomery sets |r| to |a| in Montgomery form.
115 void ec_scalar_to_montgomery(const EC_GROUP *group, EC_SCALAR *r,
116 const EC_SCALAR *a);
117
118 // ec_scalar_to_montgomery sets |r| to |a| converted from Montgomery form.
119 void ec_scalar_from_montgomery(const EC_GROUP *group, EC_SCALAR *r,
120 const EC_SCALAR *a);
121
122 // ec_scalar_mul_montgomery sets |r| to |a| * |b| where inputs and outputs are
123 // in Montgomery form.
124 void ec_scalar_mul_montgomery(const EC_GROUP *group, EC_SCALAR *r,
125 const EC_SCALAR *a, const EC_SCALAR *b);
126
127 // ec_scalar_inv0_montgomery sets |r| to |a|^-1 where inputs and outputs are in
128 // Montgomery form. If |a| is zero, |r| is set to zero.
129 void ec_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r,
130 const EC_SCALAR *a);
131
132 // ec_scalar_to_montgomery_inv_vartime sets |r| to |a|^-1 R. That is, it takes
133 // in |a| not in Montgomery form and computes the inverse in Montgomery form. It
134 // returns one on success and zero if |a| has no inverse. This function assumes
135 // |a| is public and may leak information about it via timing.
136 //
137 // Note this is not the same operation as |ec_scalar_inv0_montgomery|.
138 int ec_scalar_to_montgomery_inv_vartime(const EC_GROUP *group, EC_SCALAR *r,
139 const EC_SCALAR *a);
140
141 // ec_scalar_select, in constant time, sets |out| to |a| if |mask| is all ones
142 // and |b| if |mask| is all zeros.
143 void ec_scalar_select(const EC_GROUP *group, EC_SCALAR *out, BN_ULONG mask,
144 const EC_SCALAR *a, const EC_SCALAR *b);
145
146
147 // Field elements.
148
149 #define P384_EC_FELEM_BYTES (48)
150 #define P384_EC_FELEM_WORDS ((P384_EC_FELEM_BYTES + BN_BYTES - 1) / BN_BYTES)
151
152 #define P521_EC_FELEM_BYTES (66)
153 #define P521_EC_FELEM_WORDS ((P521_EC_FELEM_BYTES + BN_BYTES - 1) / BN_BYTES)
154
155 // An EC_FELEM represents a field element. Only the first |field->width| words
156 // are used. An |EC_FELEM| is specific to an |EC_GROUP| and must not be mixed
157 // between groups. Additionally, the representation (whether or not elements are
158 // represented in Montgomery-form) may vary between |EC_METHOD|s.
159 typedef struct {
160 // words is the representation of the field element in little-endian order.
161 BN_ULONG words[EC_MAX_WORDS];
162 } EC_FELEM;
163
164 // ec_felem_one returns one in |group|'s field.
165 const EC_FELEM *ec_felem_one(const EC_GROUP *group);
166
167 // ec_bignum_to_felem converts |in| to an |EC_FELEM|. It returns one on success
168 // and zero if |in| is out of range.
169 int ec_bignum_to_felem(const EC_GROUP *group, EC_FELEM *out, const BIGNUM *in);
170
171 // ec_felem_to_bignum converts |in| to a |BIGNUM|. It returns one on success and
172 // zero on allocation failure.
173 int ec_felem_to_bignum(const EC_GROUP *group, BIGNUM *out, const EC_FELEM *in);
174
175 // ec_felem_to_bytes serializes |in| as a big-endian bytestring to |out| and
176 // sets |*out_len| to the number of bytes written. The number of bytes written
177 // is |BN_num_bytes(&group->order)|, which is at most |EC_MAX_BYTES|.
178 void ec_felem_to_bytes(const EC_GROUP *group, uint8_t *out, size_t *out_len,
179 const EC_FELEM *in);
180
181 // ec_felem_from_bytes deserializes |in| and stores the resulting field element
182 // to |out|. It returns one on success and zero if |in| is invalid.
183 int ec_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out, const uint8_t *in,
184 size_t len);
185
186 // ec_felem_neg sets |out| to -|a|.
187 void ec_felem_neg(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a);
188
189 // ec_felem_add sets |out| to |a| + |b|.
190 void ec_felem_add(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
191 const EC_FELEM *b);
192
193 // ec_felem_add sets |out| to |a| - |b|.
194 void ec_felem_sub(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
195 const EC_FELEM *b);
196
197 // ec_felem_non_zero_mask returns all ones if |a| is non-zero and all zeros
198 // otherwise.
199 BN_ULONG ec_felem_non_zero_mask(const EC_GROUP *group, const EC_FELEM *a);
200
201 // ec_felem_select, in constant time, sets |out| to |a| if |mask| is all ones
202 // and |b| if |mask| is all zeros.
203 void ec_felem_select(const EC_GROUP *group, EC_FELEM *out, BN_ULONG mask,
204 const EC_FELEM *a, const EC_FELEM *b);
205
206 // ec_felem_equal returns one if |a| and |b| are equal and zero otherwise.
207 int ec_felem_equal(const EC_GROUP *group, const EC_FELEM *a, const EC_FELEM *b);
208
209
210 // Points.
211 //
212 // Points may represented in affine coordinates as |EC_AFFINE| or Jacobian
213 // coordinates as |EC_JACOBIAN|. Affine coordinates directly represent a
214 // point on the curve, but point addition over affine coordinates requires
215 // costly field inversions, so arithmetic is done in Jacobian coordinates.
216 // Converting from affine to Jacobian is cheap, while converting from Jacobian
217 // to affine costs a field inversion. (Jacobian coordinates amortize the field
218 // inversions needed in a sequence of point operations.)
219
220 // An EC_JACOBIAN represents an elliptic curve point in Jacobian coordinates.
221 // Unlike |EC_POINT|, it is a plain struct which can be stack-allocated and
222 // needs no cleanup. It is specific to an |EC_GROUP| and must not be mixed
223 // between groups.
224 typedef struct {
225 // X, Y, and Z are Jacobian projective coordinates. They represent
226 // (X/Z^2, Y/Z^3) if Z != 0 and the point at infinity otherwise.
227 EC_FELEM X, Y, Z;
228 } EC_JACOBIAN;
229
230 // An EC_AFFINE represents an elliptic curve point in affine coordinates.
231 // coordinates. Note the point at infinity cannot be represented in affine
232 // coordinates.
233 typedef struct {
234 EC_FELEM X, Y;
235 } EC_AFFINE;
236
237 // ec_affine_to_jacobian converts |p| to Jacobian form and writes the result to
238 // |*out|. This operation is very cheap and only costs a few copies.
239 void ec_affine_to_jacobian(const EC_GROUP *group, EC_JACOBIAN *out,
240 const EC_AFFINE *p);
241
242 // ec_jacobian_to_affine converts |p| to affine form and writes the result to
243 // |*out|. It returns one on success and zero if |p| was the point at infinity.
244 // This operation performs a field inversion and should only be done once per
245 // point.
246 //
247 // If only extracting the x-coordinate, use |ec_get_x_coordinate_*| which is
248 // slightly faster.
249 OPENSSL_EXPORT int ec_jacobian_to_affine(const EC_GROUP *group, EC_AFFINE *out,
250 const EC_JACOBIAN *p);
251
252 // ec_jacobian_to_affine_batch converts |num| points in |in| from Jacobian
253 // coordinates to affine coordinates and writes the results to |out|. It returns
254 // one on success and zero if any of the input points were infinity.
255 //
256 // This function is not implemented for all curves. Add implementations as
257 // needed.
258 int ec_jacobian_to_affine_batch(const EC_GROUP *group, EC_AFFINE *out,
259 const EC_JACOBIAN *in, size_t num);
260
261 // ec_point_set_affine_coordinates sets |out|'s to a point with affine
262 // coordinates |x| and |y|. It returns one if the point is on the curve and
263 // zero otherwise. If the point is not on the curve, the value of |out| is
264 // undefined.
265 int ec_point_set_affine_coordinates(const EC_GROUP *group, EC_AFFINE *out,
266 const EC_FELEM *x, const EC_FELEM *y);
267
268 // ec_point_mul_no_self_test does the same as |EC_POINT_mul|, but doesn't try to
269 // run the self-test first. This is for use in the self tests themselves, to
270 // prevent an infinite loop.
271 int ec_point_mul_no_self_test(const EC_GROUP *group, EC_POINT *r,
272 const BIGNUM *g_scalar, const EC_POINT *p,
273 const BIGNUM *p_scalar, BN_CTX *ctx);
274
275 // ec_point_mul_scalar sets |r| to |p| * |scalar|. Both inputs are considered
276 // secret.
277 int ec_point_mul_scalar(const EC_GROUP *group, EC_JACOBIAN *r,
278 const EC_JACOBIAN *p, const EC_SCALAR *scalar);
279
280 // ec_point_mul_scalar_base sets |r| to generator * |scalar|. |scalar| is
281 // treated as secret.
282 int ec_point_mul_scalar_base(const EC_GROUP *group, EC_JACOBIAN *r,
283 const EC_SCALAR *scalar);
284
285 // ec_point_mul_scalar_batch sets |r| to |p0| * |scalar0| + |p1| * |scalar1| +
286 // |p2| * |scalar2|. |p2| may be NULL to skip that term.
287 //
288 // The inputs are treated as secret, however, this function leaks information
289 // about whether intermediate computations add a point to itself. Callers must
290 // ensure that discrete logs between |p0|, |p1|, and |p2| are uniformly
291 // distributed and independent of the scalars, which should be uniformly
292 // selected and not under the attackers control. This ensures the doubling case
293 // will occur with negligible probability.
294 //
295 // This function is not implemented for all curves. Add implementations as
296 // needed.
297 //
298 // TODO(davidben): This function does not use base point tables. For now, it is
299 // only used with the generic |EC_GFp_mont_method| implementation which has
300 // none. If generalizing to tuned curves, this may be useful. However, we still
301 // must double up to the least efficient input, so precomputed tables can only
302 // save table setup and allow a wider window size.
303 int ec_point_mul_scalar_batch(const EC_GROUP *group, EC_JACOBIAN *r,
304 const EC_JACOBIAN *p0, const EC_SCALAR *scalar0,
305 const EC_JACOBIAN *p1, const EC_SCALAR *scalar1,
306 const EC_JACOBIAN *p2, const EC_SCALAR *scalar2);
307
308 #define EC_MONT_PRECOMP_COMB_SIZE 5
309
310 // An |EC_PRECOMP| stores precomputed information about a point, to optimize
311 // repeated multiplications involving it. It is a union so different
312 // |EC_METHOD|s can store different information in it.
313 typedef union {
314 EC_AFFINE comb[(1 << EC_MONT_PRECOMP_COMB_SIZE) - 1];
315 } EC_PRECOMP;
316
317 // ec_init_precomp precomputes multiples of |p| and writes the result to |out|.
318 // It returns one on success and zero on error. The resulting table may be used
319 // with |ec_point_mul_scalar_precomp|. This function will fail if |p| is the
320 // point at infinity.
321 //
322 // This function is not implemented for all curves. Add implementations as
323 // needed.
324 int ec_init_precomp(const EC_GROUP *group, EC_PRECOMP *out,
325 const EC_JACOBIAN *p);
326
327 // ec_point_mul_scalar_precomp sets |r| to |p0| * |scalar0| + |p1| * |scalar1| +
328 // |p2| * |scalar2|. |p1| or |p2| may be NULL to skip the corresponding term.
329 // The points are represented as |EC_PRECOMP| and must be initialized with
330 // |ec_init_precomp|. This function runs faster than |ec_point_mul_scalar_batch|
331 // but requires setup work per input point, so it is only appropriate for points
332 // which are used frequently.
333 //
334 // The inputs are treated as secret, however, this function leaks information
335 // about whether intermediate computations add a point to itself. Callers must
336 // ensure that discrete logs between |p0|, |p1|, and |p2| are uniformly
337 // distributed and independent of the scalars, which should be uniformly
338 // selected and not under the attackers control. This ensures the doubling case
339 // will occur with negligible probability.
340 //
341 // This function is not implemented for all curves. Add implementations as
342 // needed.
343 //
344 // TODO(davidben): This function does not use base point tables. For now, it is
345 // only used with the generic |EC_GFp_mont_method| implementation which has
346 // none. If generalizing to tuned curves, we should add a parameter for the base
347 // point and arrange for the generic implementation to have base point tables
348 // available.
349 int ec_point_mul_scalar_precomp(const EC_GROUP *group, EC_JACOBIAN *r,
350 const EC_PRECOMP *p0, const EC_SCALAR *scalar0,
351 const EC_PRECOMP *p1, const EC_SCALAR *scalar1,
352 const EC_PRECOMP *p2, const EC_SCALAR *scalar2);
353
354 // ec_point_mul_scalar_public sets |r| to
355 // generator * |g_scalar| + |p| * |p_scalar|. It assumes that the inputs are
356 // public so there is no concern about leaking their values through timing.
357 OPENSSL_EXPORT int ec_point_mul_scalar_public(const EC_GROUP *group,
358 EC_JACOBIAN *r,
359 const EC_SCALAR *g_scalar,
360 const EC_JACOBIAN *p,
361 const EC_SCALAR *p_scalar);
362
363 // ec_point_mul_scalar_public_batch sets |r| to the sum of generator *
364 // |g_scalar| and |points[i]| * |scalars[i]| where |points| and |scalars| have
365 // |num| elements. It assumes that the inputs are public so there is no concern
366 // about leaking their values through timing. |g_scalar| may be NULL to skip
367 // that term.
368 //
369 // This function is not implemented for all curves. Add implementations as
370 // needed.
371 int ec_point_mul_scalar_public_batch(const EC_GROUP *group, EC_JACOBIAN *r,
372 const EC_SCALAR *g_scalar,
373 const EC_JACOBIAN *points,
374 const EC_SCALAR *scalars, size_t num);
375
376 // ec_point_select, in constant time, sets |out| to |a| if |mask| is all ones
377 // and |b| if |mask| is all zeros.
378 void ec_point_select(const EC_GROUP *group, EC_JACOBIAN *out, BN_ULONG mask,
379 const EC_JACOBIAN *a, const EC_JACOBIAN *b);
380
381 // ec_affine_select behaves like |ec_point_select| but acts on affine points.
382 void ec_affine_select(const EC_GROUP *group, EC_AFFINE *out, BN_ULONG mask,
383 const EC_AFFINE *a, const EC_AFFINE *b);
384
385 // ec_precomp_select behaves like |ec_point_select| but acts on |EC_PRECOMP|.
386 void ec_precomp_select(const EC_GROUP *group, EC_PRECOMP *out, BN_ULONG mask,
387 const EC_PRECOMP *a, const EC_PRECOMP *b);
388
389 // ec_cmp_x_coordinate compares the x (affine) coordinate of |p|, mod the group
390 // order, with |r|. It returns one if the values match and zero if |p| is the
391 // point at infinity of the values do not match. |p| is treated as public.
392 int ec_cmp_x_coordinate(const EC_GROUP *group, const EC_JACOBIAN *p,
393 const EC_SCALAR *r);
394
395 // ec_get_x_coordinate_as_scalar sets |*out| to |p|'s x-coordinate, modulo
396 // |group->order|. It returns one on success and zero if |p| is the point at
397 // infinity.
398 int ec_get_x_coordinate_as_scalar(const EC_GROUP *group, EC_SCALAR *out,
399 const EC_JACOBIAN *p);
400
401 // ec_get_x_coordinate_as_bytes writes |p|'s affine x-coordinate to |out|, which
402 // must have at must |max_out| bytes. It sets |*out_len| to the number of bytes
403 // written. The value is written big-endian and zero-padded to the size of the
404 // field. This function returns one on success and zero on failure.
405 int ec_get_x_coordinate_as_bytes(const EC_GROUP *group, uint8_t *out,
406 size_t *out_len, size_t max_out,
407 const EC_JACOBIAN *p);
408
409 // ec_point_byte_len returns the number of bytes in the byte representation of
410 // a non-infinity point in |group|, encoded according to |form|, or zero if
411 // |form| is invalid.
412 size_t ec_point_byte_len(const EC_GROUP *group, point_conversion_form_t form);
413
414 // ec_point_to_bytes encodes |point| according to |form| and writes the result
415 // |buf|. It returns the size of the output on success or zero on error. At most
416 // |max_out| bytes will be written. The buffer should be at least
417 // |ec_point_byte_len| long to guarantee success.
418 size_t ec_point_to_bytes(const EC_GROUP *group, const EC_AFFINE *point,
419 point_conversion_form_t form, uint8_t *buf,
420 size_t max_out);
421
422 // ec_point_from_uncompressed parses |in| as a point in uncompressed form and
423 // sets the result to |out|. It returns one on success and zero if the input was
424 // invalid.
425 int ec_point_from_uncompressed(const EC_GROUP *group, EC_AFFINE *out,
426 const uint8_t *in, size_t len);
427
428 // ec_set_to_safe_point sets |out| to an arbitrary point on |group|, either the
429 // generator or the point at infinity. This is used to guard against callers of
430 // external APIs not checking the return value.
431 void ec_set_to_safe_point(const EC_GROUP *group, EC_JACOBIAN *out);
432
433 // ec_affine_jacobian_equal returns one if |a| and |b| represent the same point
434 // and zero otherwise. It treats both inputs as secret.
435 int ec_affine_jacobian_equal(const EC_GROUP *group, const EC_AFFINE *a,
436 const EC_JACOBIAN *b);
437
438
439 // Implementation details.
440
441 struct ec_method_st {
442 // point_get_affine_coordinates sets |*x| and |*y| to the affine coordinates
443 // of |p|. Either |x| or |y| may be NULL to omit it. It returns one on success
444 // and zero if |p| is the point at infinity. It leaks whether |p| was the
445 // point at infinity, but otherwise treats |p| as secret.
446 int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_JACOBIAN *p,
447 EC_FELEM *x, EC_FELEM *y);
448
449 // jacobian_to_affine_batch implements |ec_jacobian_to_affine_batch|.
450 int (*jacobian_to_affine_batch)(const EC_GROUP *group, EC_AFFINE *out,
451 const EC_JACOBIAN *in, size_t num);
452
453 // add sets |r| to |a| + |b|.
454 void (*add)(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *a,
455 const EC_JACOBIAN *b);
456 // dbl sets |r| to |a| + |a|.
457 void (*dbl)(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *a);
458
459 // mul sets |r| to |scalar|*|p|.
460 void (*mul)(const EC_GROUP *group, EC_JACOBIAN *r, const EC_JACOBIAN *p,
461 const EC_SCALAR *scalar);
462 // mul_base sets |r| to |scalar|*generator.
463 void (*mul_base)(const EC_GROUP *group, EC_JACOBIAN *r,
464 const EC_SCALAR *scalar);
465 // mul_batch implements |ec_mul_scalar_batch|.
466 void (*mul_batch)(const EC_GROUP *group, EC_JACOBIAN *r,
467 const EC_JACOBIAN *p0, const EC_SCALAR *scalar0,
468 const EC_JACOBIAN *p1, const EC_SCALAR *scalar1,
469 const EC_JACOBIAN *p2, const EC_SCALAR *scalar2);
470 // mul_public sets |r| to |g_scalar|*generator + |p_scalar|*|p|. It assumes
471 // that the inputs are public so there is no concern about leaking their
472 // values through timing.
473 //
474 // This function may be omitted if |mul_public_batch| is provided.
475 void (*mul_public)(const EC_GROUP *group, EC_JACOBIAN *r,
476 const EC_SCALAR *g_scalar, const EC_JACOBIAN *p,
477 const EC_SCALAR *p_scalar);
478 // mul_public_batch implements |ec_point_mul_scalar_public_batch|.
479 int (*mul_public_batch)(const EC_GROUP *group, EC_JACOBIAN *r,
480 const EC_SCALAR *g_scalar, const EC_JACOBIAN *points,
481 const EC_SCALAR *scalars, size_t num);
482
483 // init_precomp implements |ec_init_precomp|.
484 int (*init_precomp)(const EC_GROUP *group, EC_PRECOMP *out,
485 const EC_JACOBIAN *p);
486 // mul_precomp implements |ec_point_mul_scalar_precomp|.
487 void (*mul_precomp)(const EC_GROUP *group, EC_JACOBIAN *r,
488 const EC_PRECOMP *p0, const EC_SCALAR *scalar0,
489 const EC_PRECOMP *p1, const EC_SCALAR *scalar1,
490 const EC_PRECOMP *p2, const EC_SCALAR *scalar2);
491
492 // felem_mul and felem_sqr implement multiplication and squaring,
493 // respectively, so that the generic |EC_POINT_add| and |EC_POINT_dbl|
494 // implementations can work both with |EC_GFp_mont_method| and the tuned
495 // operations.
496 //
497 // TODO(davidben): This constrains |EC_FELEM|'s internal representation, adds
498 // many indirect calls in the middle of the generic code, and a bunch of
499 // conversions. If p224-64.c were easily convertable to Montgomery form, we
500 // could say |EC_FELEM| is always in Montgomery form. If we routed the rest of
501 // simple.c to |EC_METHOD|, we could give |EC_POINT| an |EC_METHOD|-specific
502 // representation and say |EC_FELEM| is purely a |EC_GFp_mont_method| type.
503 void (*felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
504 const EC_FELEM *b);
505 void (*felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a);
506
507 void (*felem_to_bytes)(const EC_GROUP *group, uint8_t *out, size_t *out_len,
508 const EC_FELEM *in);
509 int (*felem_from_bytes)(const EC_GROUP *group, EC_FELEM *out,
510 const uint8_t *in, size_t len);
511
512 // felem_reduce sets |out| to |words|, reduced modulo the field size, p.
513 // |words| must be less than p^2. |num| must be at most twice the width of p.
514 // This function treats |words| as secret.
515 //
516 // This function is only used in hash-to-curve and may be omitted in curves
517 // that do not support it.
518 void (*felem_reduce)(const EC_GROUP *group, EC_FELEM *out,
519 const BN_ULONG *words, size_t num);
520
521 // felem_exp sets |out| to |a|^|exp|. It treats |a| is secret but |exp| as
522 // public.
523 //
524 // This function is used in hash-to-curve and may be NULL in curves not used
525 // with hash-to-curve.
526 //
527 // TODO(https://crbug.com/boringssl/567): hash-to-curve uses this as part of
528 // computing a square root, which is what compressed coordinates ultimately
529 // needs to avoid |BIGNUM|. Can we unify this a bit? By generalizing to
530 // arbitrary exponentiation, we also miss an opportunity to use a specialized
531 // addition chain.
532 void (*felem_exp)(const EC_GROUP *group, EC_FELEM *out, const EC_FELEM *a,
533 const BN_ULONG *exp, size_t num_exp);
534
535 // scalar_inv0_montgomery implements |ec_scalar_inv0_montgomery|.
536 void (*scalar_inv0_montgomery)(const EC_GROUP *group, EC_SCALAR *out,
537 const EC_SCALAR *in);
538
539 // scalar_to_montgomery_inv_vartime implements
540 // |ec_scalar_to_montgomery_inv_vartime|.
541 int (*scalar_to_montgomery_inv_vartime)(const EC_GROUP *group, EC_SCALAR *out,
542 const EC_SCALAR *in);
543
544 // cmp_x_coordinate compares the x (affine) coordinate of |p|, mod the group
545 // order, with |r|. It returns one if the values match and zero if |p| is the
546 // point at infinity of the values do not match.
547 int (*cmp_x_coordinate)(const EC_GROUP *group, const EC_JACOBIAN *p,
548 const EC_SCALAR *r);
549 } /* EC_METHOD */;
550
551 const EC_METHOD *EC_GFp_mont_method(void);
552
553 struct ec_point_st {
554 // group is an owning reference to |group|, unless this is
555 // |group->generator|.
556 EC_GROUP *group;
557 // raw is the group-specific point data. Functions that take |EC_POINT|
558 // typically check consistency with |EC_GROUP| while functions that take
559 // |EC_JACOBIAN| do not. Thus accesses to this field should be externally
560 // checked for consistency.
561 EC_JACOBIAN raw;
562 } /* EC_POINT */;
563
564 struct ec_group_st {
565 const EC_METHOD *meth;
566
567 // Unlike all other |EC_POINT|s, |generator| does not own |generator->group|
568 // to avoid a reference cycle. Additionally, Z is guaranteed to be one, so X
569 // and Y are suitable for use as an |EC_AFFINE|. Before |has_order| is set, Z
570 // is one, but X and Y are uninitialized.
571 EC_POINT generator;
572
573 BN_MONT_CTX order;
574 BN_MONT_CTX field;
575
576 EC_FELEM a, b; // Curve coefficients.
577
578 // comment is a human-readable string describing the curve.
579 const char *comment;
580
581 // curve_name is the optional NID for named curves. |oid| and |oid_len| are
582 // populated with values corresponding to the named curve's NID.
583 // |NID_undef| is used to imply that the curve is a custom explicit curve and
584 // the oid values are empty if so.
585 int curve_name;
586 uint8_t oid[9];
587 uint8_t oid_len;
588
589 // a_is_minus3 is one if |a| is -3 mod |field| and zero otherwise. Point
590 // arithmetic is optimized for -3.
591 int a_is_minus3;
592
593 // has_order is one if |generator| and |order| have been initialized.
594 int has_order;
595
596 // field_greater_than_order is one if |field| is greate than |order| and zero
597 // otherwise.
598 int field_greater_than_order;
599
600 // conv_form represents the encoding format of the elliptic curve point.
601 // The default is |POINT_CONVERSION_UNCOMPRESSED|. This is not changed unless
602 // the user allocates the |EC_GROUP| with |EC_GROUP_new_by_curve_name_mutable|
603 // and customizes |conv_form| with |EC_GROUP_set_point_conversion_form|.
604 point_conversion_form_t conv_form;
605
606 // mutable_ec_group is one if the |EC_GROUP| has been dynamically allocated
607 // with |EC_GROUP_new_by_curve_name_mutable|. The default is zero to indicate
608 // our built-in static curves.
609 int mutable_ec_group;
610 } /* EC_GROUP */;
611
612 EC_GROUP *ec_group_new(const EC_METHOD *meth, const BIGNUM *p, const BIGNUM *a,
613 const BIGNUM *b, BN_CTX *ctx);
614
615 void ec_GFp_mont_mul(const EC_GROUP *group, EC_JACOBIAN *r,
616 const EC_JACOBIAN *p, const EC_SCALAR *scalar);
617 void ec_GFp_mont_mul_base(const EC_GROUP *group, EC_JACOBIAN *r,
618 const EC_SCALAR *scalar);
619 void ec_GFp_mont_mul_batch(const EC_GROUP *group, EC_JACOBIAN *r,
620 const EC_JACOBIAN *p0, const EC_SCALAR *scalar0,
621 const EC_JACOBIAN *p1, const EC_SCALAR *scalar1,
622 const EC_JACOBIAN *p2, const EC_SCALAR *scalar2);
623 int ec_GFp_mont_init_precomp(const EC_GROUP *group, EC_PRECOMP *out,
624 const EC_JACOBIAN *p);
625 void ec_GFp_mont_mul_precomp(const EC_GROUP *group, EC_JACOBIAN *r,
626 const EC_PRECOMP *p0, const EC_SCALAR *scalar0,
627 const EC_PRECOMP *p1, const EC_SCALAR *scalar1,
628 const EC_PRECOMP *p2, const EC_SCALAR *scalar2);
629 void ec_GFp_mont_felem_reduce(const EC_GROUP *group, EC_FELEM *out,
630 const BN_ULONG *words, size_t num);
631 void ec_GFp_mont_felem_exp(const EC_GROUP *group, EC_FELEM *out,
632 const EC_FELEM *a, const BN_ULONG *exp,
633 size_t num_exp);
634
635 // ec_compute_wNAF writes the modified width-(w+1) Non-Adjacent Form (wNAF) of
636 // |scalar| to |out|. |out| must have room for |bits| + 1 elements, each of
637 // which will be either zero or odd with an absolute value less than 2^w
638 // satisfying
639 // scalar = \sum_j out[j]*2^j
640 // where at most one of any w+1 consecutive digits is non-zero
641 // with the exception that the most significant digit may be only
642 // w-1 zeros away from that next non-zero digit.
643 void ec_compute_wNAF(int8_t *out, const EC_SCALAR *scalar, size_t bits, int w);
644
645 int ec_GFp_mont_mul_public_batch(const EC_GROUP *group, EC_JACOBIAN *r,
646 const EC_SCALAR *g_scalar,
647 const EC_JACOBIAN *points,
648 const EC_SCALAR *scalars, size_t num);
649
650 // method functions in simple.c
651 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
652 const BIGNUM *b, BN_CTX *);
653 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
654 BIGNUM *b);
655 void ec_GFp_simple_point_init(EC_JACOBIAN *);
656 void ec_GFp_simple_point_copy(EC_JACOBIAN *, const EC_JACOBIAN *);
657 void ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_JACOBIAN *);
658 void ec_GFp_mont_add(const EC_GROUP *, EC_JACOBIAN *r, const EC_JACOBIAN *a,
659 const EC_JACOBIAN *b);
660 void ec_GFp_mont_dbl(const EC_GROUP *, EC_JACOBIAN *r, const EC_JACOBIAN *a);
661 void ec_GFp_simple_invert(const EC_GROUP *, EC_JACOBIAN *);
662 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_JACOBIAN *);
663 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_JACOBIAN *);
664 int ec_GFp_simple_points_equal(const EC_GROUP *, const EC_JACOBIAN *a,
665 const EC_JACOBIAN *b);
666 void ec_simple_scalar_inv0_montgomery(const EC_GROUP *group, EC_SCALAR *r,
667 const EC_SCALAR *a);
668
669 int ec_simple_scalar_to_montgomery_inv_vartime(const EC_GROUP *group,
670 EC_SCALAR *r,
671 const EC_SCALAR *a);
672
673 int ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_JACOBIAN *p,
674 const EC_SCALAR *r);
675
676 void ec_GFp_simple_felem_to_bytes(const EC_GROUP *group, uint8_t *out,
677 size_t *out_len, const EC_FELEM *in);
678 int ec_GFp_simple_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out,
679 const uint8_t *in, size_t len);
680
681 // method functions in montgomery.c
682 void ec_GFp_mont_felem_mul(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
683 const EC_FELEM *b);
684 void ec_GFp_mont_felem_sqr(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a);
685
686 void ec_GFp_mont_felem_to_bytes(const EC_GROUP *group, uint8_t *out,
687 size_t *out_len, const EC_FELEM *in);
688 int ec_GFp_mont_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out,
689 const uint8_t *in, size_t len);
690
691 void ec_GFp_nistp_recode_scalar_bits(crypto_word_t *sign, crypto_word_t *digit,
692 crypto_word_t in);
693
694 const EC_METHOD *EC_GFp_nistp224_method(void);
695 const EC_METHOD *EC_GFp_nistp256_method(void);
696 const EC_METHOD *EC_GFp_nistp384_method(void);
697 const EC_METHOD *EC_GFp_nistp521_method(void);
698
699 // EC_GFp_nistz256_method is a GFp method using montgomery multiplication, with
700 // x86-64 optimized P256. See http://eprint.iacr.org/2013/816.
701 const EC_METHOD *EC_GFp_nistz256_method(void);
702
703
704 // EC_KEY_METHOD
705
706 // ec_key_method_st is a structure of function pointers implementing EC_KEY
707 // operations. Currently, AWS-LC only allows consumers to use the |init|,
708 // |finish|, |sign|, |sign_sig|, and |flag| fields. This struct replaces the
709 // older variant |ECDSA_METHOD|.
710 //
711 // We do not support the |verify|, |verify_sig|, |compute_key|, or |keygen|
712 // fields at all. If this struct is made public in the future, to maintain
713 // OpenSSL compatability and match the struct size, they should be added in.
714 struct ec_key_method_st {
715 int (*init)(EC_KEY *key);
716 void (*finish)(EC_KEY *key);
717
718 // AWS-LC doesn't support custom values for EC_KEY operations
719 // as of now. |k_inv| and |r| must be NULL parameters.
720 // The |type| parameter is ignored in OpenSSL, we pass in zero for it.
721 // The default behavior for |sign| is implemented in |ECDSA_sign|. If custom
722 // functionality is provided, |sign| will be invoked within |ECDSA_sign|.
723 int (*sign)(int type, const uint8_t *digest, int digest_len,
724 uint8_t *sig, unsigned int *siglen, const BIGNUM *k_inv,
725 const BIGNUM *r, EC_KEY *eckey);
726
727 // AWS-LC doesn't support custom values for EC_KEY operations
728 // as of now. |k_inv| and |r| must be NULL parameters. The default behavior
729 // for |sign_sig| is implemented in |ECDSA_do_sign|. If custom functionality
730 // is provided, |sign_sig| will be invoked within |ECDSA_do_sign|.
731 ECDSA_SIG *(*sign_sig)(const uint8_t *digest, int digest_len,
732 const BIGNUM *in_kinv, const BIGNUM *in_r,
733 EC_KEY *eckey);
734
735 // Currently, |EC_KEY_METHOD| only supports |ECDSA_FLAG_OPAQUE|. It is
736 // not set by default.
737 int flags;
738
739 // AWS-LC currently does not support these fields directly. However, they
740 // are left commented out here because the associated setter
741 // functions (macros) still include support for them in their signatures.
742 // Note: Compile-time checks (static asserts) are in place to ensure that
743 // these fields cannot be set by consumers, enforcing the requirement that
744 // NULL must be passed for these parameters.
745 // int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **k_inv,
746 // BIGNUM **r);
747 // int (*copy)(EC_KEY *dest, const EC_KEY *src);
748 // int (*set_group)(EC_KEY *key, const EC_GROUP *group);
749 // int (*set_private)(EC_KEY *key, const BIGNUM *priv_key);
750 // int (*set_public)(EC_KEY *key, const EC_POINT *pub_key);
751 };
752
753 // An EC_WRAPPED_SCALAR is an |EC_SCALAR| with a parallel |BIGNUM|
754 // representation. It exists to support the |EC_KEY_get0_private_key| API.
755 typedef struct {
756 BIGNUM bignum;
757 EC_SCALAR scalar;
758 } EC_WRAPPED_SCALAR;
759
760 struct ec_key_st {
761 EC_GROUP *group;
762
763 // Ideally |pub_key| would be an |EC_AFFINE| so serializing it does not pay an
764 // inversion each time, but the |EC_KEY_get0_public_key| API implies public
765 // keys are stored in an |EC_POINT|-compatible form.
766 EC_POINT *pub_key;
767 EC_WRAPPED_SCALAR *priv_key;
768
769 unsigned int enc_flag;
770 point_conversion_form_t conv_form;
771
772 CRYPTO_refcount_t references;
773
774 const EC_KEY_METHOD *eckey_method;
775
776 int group_decoded_from_explicit_params;
777
778 CRYPTO_EX_DATA ex_data;
779 } /* EC_KEY */;
780
781 // d2i_ECPKParameters deserializes the |ECPKParameters| specified in RFC 3279
782 // to an |EC_GROUP| from |inp|. Only deserialization of namedCurves or
783 // explicitly-encoded versions of namedCurves are supported. If |*out_group| is
784 // non-null, the original |*out_group| is freed and the returned |EC_GROUP| is
785 // also written to |*out_group|. The user continues to maintain the memory
786 // assigned to |*out_group| if non-null.
787 EC_GROUP *d2i_ECPKParameters(EC_GROUP **out_group, const uint8_t **inp,
788 long len);
789
790 // i2d_ECPKParameters serializes an |EC_GROUP| from |outp| according to the
791 // |ECPKParameters| specified in RFC 3279. Only serialization of namedCurves
792 // are supported.
793 int i2d_ECPKParameters(const EC_GROUP *group, uint8_t **outp);
794
795 #if defined(__cplusplus)
796 } // extern C
797 #endif
798
799 #endif // OPENSSL_HEADER_EC_INTERNAL_H