xref: /freebsd-src/crypto/openssl/crypto/ec/curve448/point_448.h (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e71b7053SJung-uk Kim  * Copyright 2015-2016 Cryptography Research, Inc.
4e71b7053SJung-uk Kim  *
5*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
6e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
7e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
8e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
9e71b7053SJung-uk Kim  *
10e71b7053SJung-uk Kim  * Originally written by Mike Hamburg
11e71b7053SJung-uk Kim  */
12e71b7053SJung-uk Kim 
1317f01e99SJung-uk Kim #ifndef OSSL_CRYPTO_EC_CURVE448_POINT_448_H
1417f01e99SJung-uk Kim # define OSSL_CRYPTO_EC_CURVE448_POINT_448_H
15e71b7053SJung-uk Kim 
16e71b7053SJung-uk Kim # include "curve448utils.h"
17e71b7053SJung-uk Kim # include "field.h"
18e71b7053SJung-uk Kim 
19e71b7053SJung-uk Kim /* Comb config: number of combs, n, t, s. */
20e71b7053SJung-uk Kim #define COMBS_N 5
21e71b7053SJung-uk Kim #define COMBS_T 5
22e71b7053SJung-uk Kim #define COMBS_S 18
23e71b7053SJung-uk Kim 
24e71b7053SJung-uk Kim /* Projective Niels coordinates */
25e71b7053SJung-uk Kim typedef struct {
26e71b7053SJung-uk Kim     gf a, b, c;
27e71b7053SJung-uk Kim } niels_s, niels_t[1];
28e71b7053SJung-uk Kim typedef struct {
29e71b7053SJung-uk Kim     niels_t n;
30e71b7053SJung-uk Kim     gf z;
31e71b7053SJung-uk Kim } pniels_t[1];
32e71b7053SJung-uk Kim 
33e71b7053SJung-uk Kim /* Precomputed base */
34e71b7053SJung-uk Kim struct curve448_precomputed_s {
35e71b7053SJung-uk Kim     niels_t table[COMBS_N << (COMBS_T - 1)];
36e71b7053SJung-uk Kim };
37e71b7053SJung-uk Kim 
38e71b7053SJung-uk Kim # define C448_SCALAR_LIMBS ((446-1)/C448_WORD_BITS+1)
39e71b7053SJung-uk Kim 
40e71b7053SJung-uk Kim /* The number of bits in a scalar */
41e71b7053SJung-uk Kim # define C448_SCALAR_BITS 446
42e71b7053SJung-uk Kim 
43e71b7053SJung-uk Kim /* Number of bytes in a serialized scalar. */
44e71b7053SJung-uk Kim # define C448_SCALAR_BYTES 56
45e71b7053SJung-uk Kim 
46e71b7053SJung-uk Kim /* X448 encoding ratio. */
47e71b7053SJung-uk Kim # define X448_ENCODE_RATIO 2
48e71b7053SJung-uk Kim 
49e71b7053SJung-uk Kim /* Number of bytes in an x448 public key */
50e71b7053SJung-uk Kim # define X448_PUBLIC_BYTES 56
51e71b7053SJung-uk Kim 
52e71b7053SJung-uk Kim /* Number of bytes in an x448 private key */
53e71b7053SJung-uk Kim # define X448_PRIVATE_BYTES 56
54e71b7053SJung-uk Kim 
55e71b7053SJung-uk Kim /* Twisted Edwards extended homogeneous coordinates */
56e71b7053SJung-uk Kim typedef struct curve448_point_s {
57e71b7053SJung-uk Kim     gf x, y, z, t;
58e71b7053SJung-uk Kim } curve448_point_t[1];
59e71b7053SJung-uk Kim 
60e71b7053SJung-uk Kim /* Precomputed table based on a point.  Can be trivial implementation. */
61e71b7053SJung-uk Kim struct curve448_precomputed_s;
62e71b7053SJung-uk Kim 
63e71b7053SJung-uk Kim /* Precomputed table based on a point.  Can be trivial implementation. */
64e71b7053SJung-uk Kim typedef struct curve448_precomputed_s curve448_precomputed_s;
65e71b7053SJung-uk Kim 
66e71b7053SJung-uk Kim /* Scalar is stored packed, because we don't need the speed. */
67e71b7053SJung-uk Kim typedef struct curve448_scalar_s {
68e71b7053SJung-uk Kim     c448_word_t limb[C448_SCALAR_LIMBS];
69e71b7053SJung-uk Kim } curve448_scalar_t[1];
70e71b7053SJung-uk Kim 
71e71b7053SJung-uk Kim /* A scalar equal to 1. */
72*b077aed3SPierre Pronchery extern const curve448_scalar_t ossl_curve448_scalar_one;
73e71b7053SJung-uk Kim 
74e71b7053SJung-uk Kim /* A scalar equal to 0. */
75*b077aed3SPierre Pronchery extern const curve448_scalar_t ossl_curve448_scalar_zero;
76e71b7053SJung-uk Kim 
77e71b7053SJung-uk Kim /* The identity point on the curve. */
78*b077aed3SPierre Pronchery extern const curve448_point_t ossl_curve448_point_identity;
79e71b7053SJung-uk Kim 
80e71b7053SJung-uk Kim /* Precomputed table for the base point on the curve. */
81*b077aed3SPierre Pronchery extern const struct curve448_precomputed_s *ossl_curve448_precomputed_base;
82*b077aed3SPierre Pronchery extern const niels_t *ossl_curve448_wnaf_base;
83e71b7053SJung-uk Kim 
84e71b7053SJung-uk Kim /*
85e71b7053SJung-uk Kim  * Read a scalar from wire format or from bytes.
86e71b7053SJung-uk Kim  *
87e71b7053SJung-uk Kim  * ser (in): Serialized form of a scalar.
88e71b7053SJung-uk Kim  * out (out): Deserialized form.
89e71b7053SJung-uk Kim  *
90e71b7053SJung-uk Kim  * Returns:
91e71b7053SJung-uk Kim  * C448_SUCCESS: The scalar was correctly encoded.
92e71b7053SJung-uk Kim  * C448_FAILURE: The scalar was greater than the modulus, and has been reduced
93e71b7053SJung-uk Kim  * modulo that modulus.
94e71b7053SJung-uk Kim  */
95*b077aed3SPierre Pronchery c448_error_t
96*b077aed3SPierre Pronchery ossl_curve448_scalar_decode(curve448_scalar_t out,
97e71b7053SJung-uk Kim                             const unsigned char ser[C448_SCALAR_BYTES]);
98e71b7053SJung-uk Kim 
99e71b7053SJung-uk Kim /*
100e71b7053SJung-uk Kim  * Read a scalar from wire format or from bytes.  Reduces mod scalar prime.
101e71b7053SJung-uk Kim  *
102e71b7053SJung-uk Kim  * ser (in): Serialized form of a scalar.
103e71b7053SJung-uk Kim  * ser_len (in): Length of serialized form.
104e71b7053SJung-uk Kim  * out (out): Deserialized form.
105e71b7053SJung-uk Kim  */
106*b077aed3SPierre Pronchery void
107*b077aed3SPierre Pronchery ossl_curve448_scalar_decode_long(curve448_scalar_t out,
108e71b7053SJung-uk Kim                                  const unsigned char *ser, size_t ser_len);
109e71b7053SJung-uk Kim 
110e71b7053SJung-uk Kim /*
111e71b7053SJung-uk Kim  * Serialize a scalar to wire format.
112e71b7053SJung-uk Kim  *
113e71b7053SJung-uk Kim  * ser (out): Serialized form of a scalar.
114e71b7053SJung-uk Kim  * s (in): Deserialized scalar.
115e71b7053SJung-uk Kim  */
116*b077aed3SPierre Pronchery void
117*b077aed3SPierre Pronchery ossl_curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
118e71b7053SJung-uk Kim                             const curve448_scalar_t s);
119e71b7053SJung-uk Kim 
120e71b7053SJung-uk Kim /*
121e71b7053SJung-uk Kim  * Add two scalars. |a|, |b| and |out| may alias each other.
122e71b7053SJung-uk Kim  *
123e71b7053SJung-uk Kim  * a (in): One scalar.
124e71b7053SJung-uk Kim  * b (in): Another scalar.
125e71b7053SJung-uk Kim  * out (out): a+b.
126e71b7053SJung-uk Kim  */
127*b077aed3SPierre Pronchery void
128*b077aed3SPierre Pronchery ossl_curve448_scalar_add(curve448_scalar_t out,
129e71b7053SJung-uk Kim                          const curve448_scalar_t a, const curve448_scalar_t b);
130e71b7053SJung-uk Kim 
131e71b7053SJung-uk Kim /*
132e71b7053SJung-uk Kim  * Subtract two scalars.  |a|, |b| and |out| may alias each other.
133e71b7053SJung-uk Kim  * a (in): One scalar.
134e71b7053SJung-uk Kim  * b (in): Another scalar.
135e71b7053SJung-uk Kim  * out (out): a-b.
136e71b7053SJung-uk Kim  */
137*b077aed3SPierre Pronchery void
138*b077aed3SPierre Pronchery ossl_curve448_scalar_sub(curve448_scalar_t out,
139e71b7053SJung-uk Kim                          const curve448_scalar_t a, const curve448_scalar_t b);
140e71b7053SJung-uk Kim 
141e71b7053SJung-uk Kim /*
142e71b7053SJung-uk Kim  * Multiply two scalars. |a|, |b| and |out| may alias each other.
143e71b7053SJung-uk Kim  *
144e71b7053SJung-uk Kim  * a (in): One scalar.
145e71b7053SJung-uk Kim  * b (in): Another scalar.
146e71b7053SJung-uk Kim  * out (out): a*b.
147e71b7053SJung-uk Kim  */
148*b077aed3SPierre Pronchery void
149*b077aed3SPierre Pronchery ossl_curve448_scalar_mul(curve448_scalar_t out,
150e71b7053SJung-uk Kim                          const curve448_scalar_t a, const curve448_scalar_t b);
151e71b7053SJung-uk Kim 
152e71b7053SJung-uk Kim /*
153e71b7053SJung-uk Kim * Halve a scalar.  |a| and |out| may alias each other.
154e71b7053SJung-uk Kim *
155e71b7053SJung-uk Kim * a (in): A scalar.
156e71b7053SJung-uk Kim * out (out): a/2.
157e71b7053SJung-uk Kim */
158*b077aed3SPierre Pronchery void
159*b077aed3SPierre Pronchery ossl_curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
160e71b7053SJung-uk Kim 
161e71b7053SJung-uk Kim /*
162e71b7053SJung-uk Kim  * Copy a scalar.  The scalars may alias each other, in which case this
163e71b7053SJung-uk Kim  * function does nothing.
164e71b7053SJung-uk Kim  *
165e71b7053SJung-uk Kim  * a (in): A scalar.
166e71b7053SJung-uk Kim  * out (out): Will become a copy of a.
167e71b7053SJung-uk Kim  */
curve448_scalar_copy(curve448_scalar_t out,const curve448_scalar_t a)168e71b7053SJung-uk Kim static ossl_inline void curve448_scalar_copy(curve448_scalar_t out,
169e71b7053SJung-uk Kim                                              const curve448_scalar_t a)
170e71b7053SJung-uk Kim {
171e71b7053SJung-uk Kim     *out = *a;
172e71b7053SJung-uk Kim }
173e71b7053SJung-uk Kim 
174e71b7053SJung-uk Kim /*
175e71b7053SJung-uk Kim  * Copy a point.  The input and output may alias, in which case this function
176e71b7053SJung-uk Kim  * does nothing.
177e71b7053SJung-uk Kim  *
178e71b7053SJung-uk Kim  * a (out): A copy of the point.
179e71b7053SJung-uk Kim  * b (in): Any point.
180e71b7053SJung-uk Kim  */
curve448_point_copy(curve448_point_t a,const curve448_point_t b)181e71b7053SJung-uk Kim static ossl_inline void curve448_point_copy(curve448_point_t a,
182e71b7053SJung-uk Kim                                             const curve448_point_t b)
183e71b7053SJung-uk Kim {
184e71b7053SJung-uk Kim     *a = *b;
185e71b7053SJung-uk Kim }
186e71b7053SJung-uk Kim 
187e71b7053SJung-uk Kim /*
188e71b7053SJung-uk Kim  * Test whether two points are equal.  If yes, return C448_TRUE, else return
189e71b7053SJung-uk Kim  * C448_FALSE.
190e71b7053SJung-uk Kim  *
191e71b7053SJung-uk Kim  * a (in): A point.
192e71b7053SJung-uk Kim  * b (in): Another point.
193e71b7053SJung-uk Kim  *
194e71b7053SJung-uk Kim  * Returns:
195e71b7053SJung-uk Kim  * C448_TRUE: The points are equal.
196e71b7053SJung-uk Kim  * C448_FALSE: The points are not equal.
197e71b7053SJung-uk Kim  */
198*b077aed3SPierre Pronchery __owur c448_bool_t
199*b077aed3SPierre Pronchery ossl_curve448_point_eq(const curve448_point_t a,
200e71b7053SJung-uk Kim                        const curve448_point_t b);
201e71b7053SJung-uk Kim 
202e71b7053SJung-uk Kim /*
203e71b7053SJung-uk Kim  * Double a point. Equivalent to curve448_point_add(two_a,a,a), but potentially
204e71b7053SJung-uk Kim  * faster.
205e71b7053SJung-uk Kim  *
206e71b7053SJung-uk Kim  * two_a (out): The sum a+a.
207e71b7053SJung-uk Kim  * a (in): A point.
208e71b7053SJung-uk Kim  */
209*b077aed3SPierre Pronchery void
210*b077aed3SPierre Pronchery ossl_curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
211e71b7053SJung-uk Kim 
212e71b7053SJung-uk Kim /*
213e71b7053SJung-uk Kim  * RFC 7748 Diffie-Hellman scalarmul.  This function uses a different
214e71b7053SJung-uk Kim  * (non-Decaf) encoding.
215e71b7053SJung-uk Kim  *
216e71b7053SJung-uk Kim  * out (out): The scaled point base*scalar
217e71b7053SJung-uk Kim  * base (in): The point to be scaled.
218e71b7053SJung-uk Kim  * scalar (in): The scalar to multiply by.
219e71b7053SJung-uk Kim  *
220e71b7053SJung-uk Kim  * Returns:
221e71b7053SJung-uk Kim  * C448_SUCCESS: The scalarmul succeeded.
222e71b7053SJung-uk Kim  * C448_FAILURE: The scalarmul didn't succeed, because the base point is in a
223e71b7053SJung-uk Kim  * small subgroup.
224e71b7053SJung-uk Kim  */
225*b077aed3SPierre Pronchery __owur c448_error_t
226*b077aed3SPierre Pronchery ossl_x448_int(uint8_t out[X448_PUBLIC_BYTES],
227e71b7053SJung-uk Kim               const uint8_t base[X448_PUBLIC_BYTES],
228e71b7053SJung-uk Kim               const uint8_t scalar[X448_PRIVATE_BYTES]);
229e71b7053SJung-uk Kim 
230e71b7053SJung-uk Kim /*
231e71b7053SJung-uk Kim  * Multiply a point by X448_ENCODE_RATIO, then encode it like RFC 7748.
232e71b7053SJung-uk Kim  *
233e71b7053SJung-uk Kim  * This function is mainly used internally, but is exported in case
234e71b7053SJung-uk Kim  * it will be useful.
235e71b7053SJung-uk Kim  *
236e71b7053SJung-uk Kim  * The ratio is necessary because the internal representation doesn't
237e71b7053SJung-uk Kim  * track the cofactor information, so on output we must clear the cofactor.
238e71b7053SJung-uk Kim  * This would multiply by the cofactor, but in fact internally points are always
239e71b7053SJung-uk Kim  * even, so it multiplies by half the cofactor instead.
240e71b7053SJung-uk Kim  *
241e71b7053SJung-uk Kim  * As it happens, this aligns with the base point definitions; that is,
242e71b7053SJung-uk Kim  * if you pass the Decaf/Ristretto base point to this function, the result
243e71b7053SJung-uk Kim  * will be X448_ENCODE_RATIO times the X448
244e71b7053SJung-uk Kim  * base point.
245e71b7053SJung-uk Kim  *
246e71b7053SJung-uk Kim  * out (out): The scaled and encoded point.
247e71b7053SJung-uk Kim  * p (in): The point to be scaled and encoded.
248e71b7053SJung-uk Kim  */
249*b077aed3SPierre Pronchery void
250*b077aed3SPierre Pronchery ossl_curve448_point_mul_by_ratio_and_encode_like_x448(
251e71b7053SJung-uk Kim                                         uint8_t out[X448_PUBLIC_BYTES],
252e71b7053SJung-uk Kim                                         const curve448_point_t p);
253e71b7053SJung-uk Kim 
254e71b7053SJung-uk Kim /*
255e71b7053SJung-uk Kim  * RFC 7748 Diffie-Hellman base point scalarmul.  This function uses a different
256e71b7053SJung-uk Kim  * (non-Decaf) encoding.
257e71b7053SJung-uk Kim  *
258e71b7053SJung-uk Kim  * out (out): The scaled point base*scalar
259e71b7053SJung-uk Kim  * scalar (in): The scalar to multiply by.
260e71b7053SJung-uk Kim  */
261*b077aed3SPierre Pronchery void
262*b077aed3SPierre Pronchery ossl_x448_derive_public_key(uint8_t out[X448_PUBLIC_BYTES],
263e71b7053SJung-uk Kim                             const uint8_t scalar[X448_PRIVATE_BYTES]);
264e71b7053SJung-uk Kim 
265e71b7053SJung-uk Kim /*
266e71b7053SJung-uk Kim  * Multiply a precomputed base point by a scalar: out = scalar*base.
267e71b7053SJung-uk Kim  *
268e71b7053SJung-uk Kim  * scaled (out): The scaled point base*scalar
269e71b7053SJung-uk Kim  * base (in): The point to be scaled.
270e71b7053SJung-uk Kim  * scalar (in): The scalar to multiply by.
271e71b7053SJung-uk Kim  */
272*b077aed3SPierre Pronchery void
273*b077aed3SPierre Pronchery ossl_curve448_precomputed_scalarmul(curve448_point_t scaled,
274e71b7053SJung-uk Kim                                     const curve448_precomputed_s * base,
275e71b7053SJung-uk Kim                                     const curve448_scalar_t scalar);
276e71b7053SJung-uk Kim 
277e71b7053SJung-uk Kim /*
278e71b7053SJung-uk Kim  * Multiply two base points by two scalars:
279e71b7053SJung-uk Kim  * combo = scalar1*curve448_point_base + scalar2*base2.
280e71b7053SJung-uk Kim  *
281e71b7053SJung-uk Kim  * Otherwise equivalent to curve448_point_double_scalarmul, but may be
282e71b7053SJung-uk Kim  * faster at the expense of being variable time.
283e71b7053SJung-uk Kim  *
284e71b7053SJung-uk Kim  * combo (out): The linear combination scalar1*base + scalar2*base2.
285e71b7053SJung-uk Kim  * scalar1 (in): A first scalar to multiply by.
286e71b7053SJung-uk Kim  * base2 (in): A second point to be scaled.
287e71b7053SJung-uk Kim  * scalar2 (in) A second scalar to multiply by.
288e71b7053SJung-uk Kim  *
289e71b7053SJung-uk Kim  * Warning: This function takes variable time, and may leak the scalars used.
290e71b7053SJung-uk Kim  * It is designed for signature verification.
291e71b7053SJung-uk Kim  */
292*b077aed3SPierre Pronchery void
293*b077aed3SPierre Pronchery ossl_curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
294e71b7053SJung-uk Kim                                                const curve448_scalar_t scalar1,
295e71b7053SJung-uk Kim                                                const curve448_point_t base2,
296e71b7053SJung-uk Kim                                                const curve448_scalar_t scalar2);
297e71b7053SJung-uk Kim 
298e71b7053SJung-uk Kim /*
299e71b7053SJung-uk Kim  * Test that a point is valid, for debugging purposes.
300e71b7053SJung-uk Kim  *
301e71b7053SJung-uk Kim  * to_test (in): The point to test.
302e71b7053SJung-uk Kim  *
303e71b7053SJung-uk Kim  * Returns:
304e71b7053SJung-uk Kim  * C448_TRUE The point is valid.
305e71b7053SJung-uk Kim  * C448_FALSE The point is invalid.
306e71b7053SJung-uk Kim  */
307*b077aed3SPierre Pronchery __owur c448_bool_t
308*b077aed3SPierre Pronchery ossl_curve448_point_valid(const curve448_point_t to_test);
309e71b7053SJung-uk Kim 
310e71b7053SJung-uk Kim /* Overwrite scalar with zeros. */
311*b077aed3SPierre Pronchery void ossl_curve448_scalar_destroy(curve448_scalar_t scalar);
312e71b7053SJung-uk Kim 
313e71b7053SJung-uk Kim /* Overwrite point with zeros. */
314*b077aed3SPierre Pronchery void ossl_curve448_point_destroy(curve448_point_t point);
315e71b7053SJung-uk Kim 
31617f01e99SJung-uk Kim #endif                          /* OSSL_CRYPTO_EC_CURVE448_POINT_448_H */
317