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