Debian Codegraph @

src/ed25519.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2022, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 /*
33 * Twisted Edward curve with equation:
34 *
35 * ax² + y² = 1 + dx²y²
36 *
37 * where a = -1 and d = - 121665/121666 over the prime field 2²⁵⁵ - 19.
38 *
39 * Points (x, y) can be represented as extended homogeneous coordinates
40 * (X, Y, Z, T) with x = X/Z, y = Y/Z, and x*y = T/Z for a non-zero Z.
41 *
42 * The PAI (or neutral point) is (0, Z, Z, 0) or equivalently (0, 1).
43 * The point (x, y) can be obtained by normalizing to (x, y, 1, x*y).
44 */
45
46 #include "common.h"
47 #include "endianess.h"
48 #include "ed25519.h"
49
50 FAKE_INIT(ed25519)
51
52 #include "mod25519.c"
53
54 /*
55 * P3 can be P1 or P2
56 */
57 STATIC void ed25519_add_internal(Point *P3, const Point *P1, const Point *P2)
58 {
59 uint32_t A[10], B[10], C[10], D[10];
60 /* d = 37095705934669439343138083508754565189542113879843219016388785533085940283555 */
61 /* k = 2 * d (mod 2²⁵⁵ - 19) */
62 static const uint32_t k[10] = { 0x2B2F159, 0x1A6E509, 0x22ADD7A, 0xD4141D, 0x38052,
63 0xF3D130, 0x3407977, 0x19CE331, 0x1C56DFF, 0x901B67 };
64
65 /* https://www.hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html#addition-add-2008-hwcd-3 */
66
67 sub_25519(A, P1->Y, P1->X); /* (Y1-X1) each limb < 2²⁶ */
68 sub_25519(B, P2->Y, P2->X); /* (Y2-X2) < 2²⁶ */
69 mul_25519(A, A, B); /* A < 2²⁶ */
70 add32(B, P1->Y, P1->X); /* (Y1+X1) < 2²⁷ */
71 add32(C, P2->Y, P2->X); /* (Y2+X2) < 2²⁷ */
72 mul_25519(B, B, C); /* B < 2²⁶ */
73 mul_25519(C, P1->T, P2->T); /* T1*T2 < 2²⁶ */
74 mul_25519(C, C, k); /* C < 2²⁶ */
75 mul_25519(D, P1->Z, P2->Z); /* Z1*Z2 < 2²⁶ */
76 add_25519(D, D, D); /* D < 2²⁶ */
77 sub_25519(P3->T, B, A); /* E=B-A < 2²⁶ */
78 sub_25519(P3->Z, D, C); /* F=D-C < 2²⁶ */
79 add32(D, D, C); /* G=D+C < 2²⁷ */
80 add32(B, B, A); /* H=B+A < 2²⁷ */
81 mul_25519(P3->X, P3->T, P3->Z); /* X3=E*F < 2²⁶ */
82 mul_25519(P3->Y, D, B); /* Y3=G*H < 2²⁶ */
83 mul_25519(P3->T, P3->T, B); /* T3=E*H < 2²⁶ */
84 mul_25519(P3->Z, P3->Z, D); /* Z3=F*G < 2²⁶ */
85 }
86
87 STATIC void ed25519_double_internal(Point *P3, const Point *P1)
88 {
89 uint32_t A[10], B[10], C[10], D[10];
90
91 mul_25519(A, P1->X, P1->X); /* X1^2 each limb < 2²⁶ */
92 mul_25519(B, P1->Y, P1->Y); /* Y1^2 < 2²⁶ */
93 mul_25519(C, P1->Z, P1->Z); /* Z1^2 < 2²⁶ */
94 add_25519(C, C, C); /* C=2*Z1^2 < 2²⁶ */
95 add32(D, A, B); /* H=A+B < 2²⁷ */
96 add32(P3->T, P1->X, P1->Y); /* X1+Y1 < 2²⁷ */
97 mul_25519(P3->T, P3->T, P3->T); /* (X1+Y1)^2 < 2²⁶ */
98 sub_25519(P3->T, D, P3->T); /* E=H-(X1+Y1)^2 < 2²⁶ */
99 sub_25519(P3->Z, A, B); /* G=A-B < 2²⁶ */
100 add_25519(A, C, P3->Z); /* F=C+G < 2²⁶ */
101 mul_25519(P3->X, P3->T, A); /* X3=E*F < 2²⁶ */
102 mul_25519(P3->Y, P3->Z, D); /* Y3=G*H < 2²⁶ */
103 mul_25519(P3->T, P3->T, D); /* T3=E*H < 2²⁶ */
104 mul_25519(P3->Z, A, P3->Z); /* Z3=F*G < 2²⁶ */
105 }
106
107 #if 0
108 static void print_point_le8(const Point *p)
109 {
110 uint8_t bin[32];
111 unsigned i;
112 uint32_t tmp[32];
113 uint32_t invz[32];
114
115 invert_25519(invz, p->Z);
116 mul_25519(tmp, p->X, invz);
117 convert_le25p5_to_le8(bin, tmp);
118 printf("X=");
119 for (i=0; i<32; i++)
120 printf("%02X", bin[i]);
121 printf("\n");
122
123 invert_25519(invz, p->Z);
124 mul_25519(tmp, p->Y, invz);
125 convert_le25p5_to_le8(bin, tmp);
126 printf("Y=");
127 for (i=0; i<32; i++)
128 printf("%02X", bin[i]);
129 printf("\n");
130 }
131 #endif
132
133 /*
134 * Scalar multiplication Q = k*B
135 *
136 * @param[out] xout The X-coordinate of the resulting point Q.
137 * @param[out] yout The Y-coordinate of the resulting point Q.
138 * @param[in] k The scalar encoded in little-endian mode.
139 * @param[in] len Length of the scalar in bytes.
140 * @param[in] xin The X-coordinate of the input point B.
141 * @param[in] yin The Y-coordinate of the input point B.
142 */
143 STATIC void ed25519_scalar_internal(Point *Pout,
144 const uint8_t *k, size_t len,
145 const Point *Pin)
146 {
147 Point R0, R1;
148 unsigned bit_idx, swap;
149 size_t scan;
150
151 /* Point R0 */
152 memset(&R0, 0, sizeof R0);
153 R0.Y[0] = R0.Z[0] = 1;
154
155 /* Point R1 */
156 R1 = *Pin;
157
158 /* https://eprint.iacr.org/2020/956.pdf */
159
160 /* OPTIMIZE: with pre-computed tables in case of fixed-point multiplication */
161
162 /* Scan all bits from MSB to LSB */
163 bit_idx = 7;
164 swap = 0;
165 scan = 0;
166 while (scan<len) {
167 unsigned bit;
168
169 bit = (k[scan] >> bit_idx) & 1;
170 swap ^= bit;
171
172 cswap(R0.X, R0.Y, R1.X, R1.Y, swap);
173 cswap(R0.Z, R0.T, R1.Z, R1.T, swap);
174
175 ed25519_add_internal(&R1, &R0, &R1); /* R1 <-- R0 + R1 */
176 ed25519_double_internal(&R0, &R0); /* R0 <-- 2R0 */
177
178 swap = bit;
179 if (bit_idx-- == 0) {
180 bit_idx = 7;
181 scan++;
182 }
183 }
184 cswap(R0.X, R0.Y, R1.X, R1.Y, swap);
185 cswap(R0.Z, R0.T, R1.Z, R1.T, swap);
186
187 *Pout = R0;
188 }
189
190 /* ---- */
191
192 EXPORT_SYM int ed25519_new_point(Point **out,
193 const uint8_t x[32], const uint8_t y[32],
194 size_t modsize, void *context)
195 {
196 uint32_t A[10], B[10], C[10];
197 const char d[] = "52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3";
198
199 if ((NULL == out) || (NULL == x) || (NULL == y))
200 return ERR_NULL;
201
202 if (modsize != 32)
203 return ERR_MODULUS;
204
205 *out = calloc(1, sizeof(Point));
206 if (NULL == *out)
207 return ERR_MEMORY;
208
209 convert_be8_to_le25p5((*out)->X, x);
210 convert_be8_to_le25p5((*out)->Y, y);
211 (*out)->Z[0] = 1;
212 mul_25519((*out)->T, (*out)->X, (*out)->Y);
213
214 /** Verify that the point is on the Ed25519 curve **/
215 mul_25519(A, (*out)->X, (*out)->X); /* x² */
216 mul_25519(B, (*out)->Y, (*out)->Y); /* y² */
217
218 convert_behex_to_le25p5(C, d); /* d */
219 mul_25519(C, C, B); /* dy² */
220 mul_25519(C, C, A); /* dx²y² */
221 add_25519(C, C, A); /* dx²y² - ax² */
222
223 memset(A, 0, sizeof A);
224 A[0] = 1;
225 add_25519(C, C, A); /* 1 + dx²y² - ax² */
226
227 reduce_25519_le25p5(B);
228 reduce_25519_le25p5(C);
229 if (0 != memcmp(B, C, sizeof B)) {
230 free(*out);
231 *out = NULL;
232 return ERR_EC_POINT;
233 }
234
235 return 0;
236 }
237
238 EXPORT_SYM int ed25519_clone(Point **P, const Point *Q)
239 {
240 if ((NULL == P) || (NULL == Q))
241 return ERR_NULL;
242
243 *P = calloc(1, sizeof(Point));
244 if (NULL == *P)
245 return ERR_MEMORY;
246
247 **P = *Q;
248 return 0;
249 }
250
251 EXPORT_SYM void ed25519_free_point(Point *p)
252 {
253 if (p)
254 free(p);
255 }
256
257 EXPORT_SYM int ed25519_cmp(const Point *p1, const Point *p2)
258 {
259 uint32_t tmp[10];
260 uint8_t bin1[32], bin2[32];
261 unsigned int i;
262 int res = 0;
263
264 mul_25519(tmp, p1->X, p2->Z);
265 convert_le25p5_to_le8(bin1, tmp);
266 mul_25519(tmp, p2->X, p1->Z);
267 convert_le25p5_to_le8(bin2, tmp);
268 for (i=0; i<sizeof bin1; i++) {
269 res |= bin1[i] != bin2[i];
270 }
271
272 mul_25519(tmp, p1->Y, p2->Z);
273 convert_le25p5_to_le8(bin1, tmp);
274 mul_25519(tmp, p2->Y, p1->Z);
275 convert_le25p5_to_le8(bin2, tmp);
276 for (i=0; i<sizeof bin1; i++) {
277 res |= bin1[i] != bin2[i];
278 }
279
280 return res;
281 }
282
283 EXPORT_SYM int ed25519_neg(Point *p)
284 {
285 const uint32_t zero[10] = { 0 };
286
287 sub_25519(p->X, zero, p->X);
288 return 0;
289 }
290
291 EXPORT_SYM int ed25519_get_xy(uint8_t *xb, uint8_t *yb, size_t modsize, Point *p)
292 {
293 uint32_t invz[10], tmp[10];
294
295 if ((NULL == xb) || (NULL == yb) || (NULL == p))
296 return ERR_NULL;
297 if (modsize != 32)
298 return ERR_MODULUS;
299
300 invert_25519(invz, p->Z);
301 mul_25519(tmp, p->X, invz);
302 convert_le25p5_to_be8(xb, tmp);
303 mul_25519(tmp, p->Y, invz);
304 convert_le25p5_to_be8(yb, tmp);
305
306 return 0;
307 }
308
309 EXPORT_SYM int ed25519_double(Point *p)
310 {
311 if (NULL == p)
312 return ERR_NULL;
313 ed25519_double_internal(p, p);
314 return 0;
315 }
316
317 EXPORT_SYM int ed25519_add(Point *P1, const Point *P2)
318 {
319 if ((NULL == P1) || (NULL == P2))
320 return ERR_NULL;
321 ed25519_add_internal(P1, P1, P2);
322 return 0;
323 }
324
325 EXPORT_SYM int ed25519_scalar(Point *P, const uint8_t *scalar, size_t scalar_len, uint64_t seed)
326 {
327 if ((NULL == P) || (NULL == scalar))
328 return ERR_NULL;
329
330 ed25519_scalar_internal(P, scalar, scalar_len, P);
331 return 0;
332 }