]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/curve448/point_448.h
More style fixes to Curve448 code based on review feedback
[thirdparty/openssl.git] / crypto / ec / curve448 / point_448.h
1 /*
2 * Copyright 2017-2018 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 HEADER_POINT_448_H
14 # define HEADER_POINT_448_H
15
16 # include "curve448utils.h"
17 # include "field.h"
18
19
20 # define C448_SCALAR_LIMBS ((446-1)/C448_WORD_BITS+1)
21
22 /* The number of bits in a scalar */
23 # define C448_SCALAR_BITS 446
24
25 /* Number of bytes in a serialized scalar. */
26 # define C448_SCALAR_BYTES 56
27
28 /* X448 encoding ratio. */
29 # define X448_ENCODE_RATIO 2
30
31 /* Number of bytes in an x448 public key */
32 # define X448_PUBLIC_BYTES 56
33
34 /* Number of bytes in an x448 private key */
35 # define X448_PRIVATE_BYTES 56
36
37 /* Twisted Edwards extended homogeneous coordinates */
38 typedef struct curve448_point_s {
39 gf x, y, z, t;
40 } curve448_point_t[1];
41
42 /* Precomputed table based on a point. Can be trivial implementation. */
43 struct curve448_precomputed_s;
44
45 /* Precomputed table based on a point. Can be trivial implementation. */
46 typedef struct curve448_precomputed_s curve448_precomputed_s;
47
48 /* Scalar is stored packed, because we don't need the speed. */
49 typedef struct curve448_scalar_s {
50 c448_word_t limb[C448_SCALAR_LIMBS];
51 } curve448_scalar_t[1];
52
53 /* A scalar equal to 1. */
54 extern const curve448_scalar_t curve448_scalar_one;
55
56 /* A scalar equal to 0. */
57 extern const curve448_scalar_t curve448_scalar_zero;
58
59 /* The identity point on the curve. */
60 extern const curve448_point_t curve448_point_identity;
61
62 /* Precomputed table for the base point on the curve. */
63 extern const struct curve448_precomputed_s *curve448_precomputed_base;
64
65 /*
66 * Read a scalar from wire format or from bytes.
67 *
68 * ser (in): Serialized form of a scalar.
69 * out (out): Deserialized form.
70 *
71 * Returns:
72 * C448_SUCCESS: The scalar was correctly encoded.
73 * C448_FAILURE: The scalar was greater than the modulus, and has been reduced
74 * modulo that modulus.
75 */
76 __owur c448_error_t curve448_scalar_decode(
77 curve448_scalar_t out,
78 const unsigned char ser[C448_SCALAR_BYTES]);
79
80 /*
81 * Read a scalar from wire format or from bytes. Reduces mod scalar prime.
82 *
83 * ser (in): Serialized form of a scalar.
84 * ser_len (in): Length of serialized form.
85 * out (out): Deserialized form.
86 */
87 void curve448_scalar_decode_long(curve448_scalar_t out,
88 const unsigned char *ser, size_t ser_len);
89
90 /*
91 * Serialize a scalar to wire format.
92 *
93 * ser (out): Serialized form of a scalar.
94 * s (in): Deserialized scalar.
95 */
96 void curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
97 const curve448_scalar_t s);
98
99 /*
100 * Add two scalars. The scalars may use the same memory.
101 *
102 * a (in): One scalar.
103 * b (in): Another scalar.
104 * out (out): a+b.
105 */
106 void curve448_scalar_add(curve448_scalar_t out,
107 const curve448_scalar_t a, const curve448_scalar_t b);
108
109 /*
110 * Subtract two scalars. The scalars may use the same memory.
111 * a (in): One scalar.
112 * b (in): Another scalar.
113 * out (out): a-b.
114 */
115 void curve448_scalar_sub(curve448_scalar_t out,
116 const curve448_scalar_t a, const curve448_scalar_t b);
117
118 /*
119 * Multiply two scalars. The scalars may use the same memory.
120 *
121 * a (in): One scalar.
122 * b (in): Another scalar.
123 * out (out): a*b.
124 */
125 void curve448_scalar_mul(curve448_scalar_t out,
126 const curve448_scalar_t a, const curve448_scalar_t b);
127
128 /*
129 * Halve a scalar. The scalars may use the same memory.
130 *
131 * a (in): A scalar.
132 * out (out): a/2.
133 */
134 void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
135
136 /*
137 * Copy a scalar. The scalars may use the same memory, in which case this
138 * function does nothing.
139 *
140 * a (in): A scalar.
141 * out (out): Will become a copy of a.
142 */
143 static ossl_inline void curve448_scalar_copy(curve448_scalar_t out,
144 const curve448_scalar_t a)
145 {
146 *out = *a;
147 }
148
149 /*
150 * Copy a point. The input and output may alias, in which case this function
151 * does nothing.
152 *
153 * a (out): A copy of the point.
154 * b (in): Any point.
155 */
156 static ossl_inline void curve448_point_copy(curve448_point_t a,
157 const curve448_point_t b)
158 {
159 *a = *b;
160 }
161
162 /*
163 * Test whether two points are equal. If yes, return C448_TRUE, else return
164 * C448_FALSE.
165 *
166 * a (in): A point.
167 * b (in): Another point.
168 *
169 * Returns:
170 * C448_TRUE: The points are equal.
171 * C448_FALSE: The points are not equal.
172 */
173 __owur c448_bool_t curve448_point_eq(const curve448_point_t a,
174 const curve448_point_t b);
175
176 /*
177 * Double a point. Equivalent to curve448_point_add(two_a,a,a), but potentially
178 * faster.
179 *
180 * two_a (out): The sum a+a.
181 * a (in): A point.
182 */
183 void curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
184
185 /*
186 * RFC 7748 Diffie-Hellman scalarmul. This function uses a different
187 * (non-Decaf) encoding.
188 *
189 * out (out): The scaled point base*scalar
190 * base (in): The point to be scaled.
191 * scalar (in): The scalar to multiply by.
192 *
193 * Returns:
194 * C448_SUCCESS: The scalarmul succeeded.
195 * C448_FAILURE: The scalarmul didn't succeed, because the base point is in a
196 * small subgroup.
197 */
198 __owur c448_error_t x448_int(uint8_t out[X448_PUBLIC_BYTES],
199 const uint8_t base[X448_PUBLIC_BYTES],
200 const uint8_t scalar[X448_PRIVATE_BYTES]);
201
202 /*
203 * Multiply a point by X448_ENCODE_RATIO, then encode it like RFC 7748.
204 *
205 * This function is mainly used internally, but is exported in case
206 * it will be useful.
207 *
208 * The ratio is necessary because the internal representation doesn't
209 * track the cofactor information, so on output we must clear the cofactor.
210 * This would multiply by the cofactor, but in fact internally points are always
211 * even, so it multiplies by half the cofactor instead.
212 *
213 * As it happens, this aligns with the base point definitions; that is,
214 * if you pass the Decaf/Ristretto base point to this function, the result
215 * will be X448_ENCODE_RATIO times the X448
216 * base point.
217 *
218 * out (out): The scaled and encoded point.
219 * p (in): The point to be scaled and encoded.
220 */
221 void curve448_point_mul_by_ratio_and_encode_like_x448(
222 uint8_t out[X448_PUBLIC_BYTES],
223 const curve448_point_t p);
224
225 /*
226 * RFC 7748 Diffie-Hellman base point scalarmul. This function uses a different
227 * (non-Decaf) encoding.
228 *
229 * out (out): The scaled point base*scalar
230 * scalar (in): The scalar to multiply by.
231 */
232 void x448_derive_public_key(uint8_t out[X448_PUBLIC_BYTES],
233 const uint8_t scalar[X448_PRIVATE_BYTES]);
234
235 /*
236 * Multiply a precomputed base point by a scalar: out = scalar*base.
237 *
238 * scaled (out): The scaled point base*scalar
239 * base (in): The point to be scaled.
240 * scalar (in): The scalar to multiply by.
241 */
242 void curve448_precomputed_scalarmul(curve448_point_t scaled,
243 const curve448_precomputed_s * base,
244 const curve448_scalar_t scalar);
245
246 /*
247 * Multiply two base points by two scalars:
248 * combo = scalar1*curve448_point_base + scalar2*base2.
249 *
250 * Otherwise equivalent to curve448_point_double_scalarmul, but may be
251 * faster at the expense of being variable time.
252 *
253 * combo (out): The linear combination scalar1*base + scalar2*base2.
254 * scalar1 (in): A first scalar to multiply by.
255 * base2 (in): A second point to be scaled.
256 * scalar2 (in) A second scalar to multiply by.
257 *
258 * Warning: This function takes variable time, and may leak the scalars used.
259 * It is designed for signature verification.
260 */
261 void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
262 const curve448_scalar_t scalar1,
263 const curve448_point_t base2,
264 const curve448_scalar_t scalar2);
265
266 /*
267 * Test that a point is valid, for debugging purposes.
268 *
269 * to_test (in): The point to test.
270 *
271 * Returns:
272 * C448_TRUE The point is valid.
273 * C448_FALSE The point is invalid.
274 */
275 __owur c448_bool_t curve448_point_valid(const curve448_point_t to_test);
276
277 /* Overwrite scalar with zeros. */
278 void curve448_scalar_destroy(curve448_scalar_t scalar);
279
280 /* Overwrite point with zeros. */
281 void curve448_point_destroy(curve448_point_t point);
282
283 #endif /* HEADER_POINT_448_H */