blob: d3bb36a39d3272a2e6eab15dd327f1807f8dce07 [file] [log] [blame]
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001/*
2 * Copyright (C) 2021 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6#include "tls.h"
7
8#define SP_DEBUG 0
9#define FIXED_SECRET 0
10#define FIXED_PEER_PUBKEY 0
11
12#if SP_DEBUG
13# define dbg(...) fprintf(stderr, __VA_ARGS__)
14static void dump_hex(const char *fmt, const void *vp, int len)
15{
16 char hexbuf[32 * 1024 + 4];
17 const uint8_t *p = vp;
18
19 bin2hex(hexbuf, (void*)p, len)[0] = '\0';
20 dbg(fmt, hexbuf);
21}
22#else
23# define dbg(...) ((void)0)
24# define dump_hex(...) ((void)0)
25#endif
26
27#undef DIGIT_BIT
28#define DIGIT_BIT 32
29typedef int32_t sp_digit;
30
31/* The code below is taken from parts of
32 * wolfssl-3.15.3/wolfcrypt/src/sp_c32.c
33 * and heavily modified.
34 * Header comment is kept intact:
35 */
36
37/* sp.c
38 *
39 * Copyright (C) 2006-2018 wolfSSL Inc.
40 *
41 * This file is part of wolfSSL.
42 *
43 * wolfSSL is free software; you can redistribute it and/or modify
44 * it under the terms of the GNU General Public License as published by
45 * the Free Software Foundation; either version 2 of the License, or
46 * (at your option) any later version.
47 *
48 * wolfSSL is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, write to the Free Software
55 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
56 */
57
58/* Implementation by Sean Parkinson. */
59
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020060typedef struct sp_point {
61 sp_digit x[2 * 10];
62 sp_digit y[2 * 10];
63 sp_digit z[2 * 10];
64 int infinity;
65} sp_point;
66
67/* The modulus (prime) of the curve P256. */
68static const sp_digit p256_mod[10] = {
69 0x3ffffff,0x3ffffff,0x3ffffff,0x003ffff,0x0000000,
70 0x0000000,0x0000000,0x0000400,0x3ff0000,0x03fffff,
71};
72
73#define p256_mp_mod ((sp_digit)0x000001)
74
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020075/* The base point of curve P256. */
76static const sp_point p256_base = {
77 /* X ordinate */
78 { 0x098c296,0x04e5176,0x33a0f4a,0x204b7ac,0x277037d,0x0e9103c,0x3ce6e56,0x1091fe2,0x1f2e12c,0x01ac5f4 },
79 /* Y ordinate */
80 { 0x3bf51f5,0x1901a0d,0x1ececbb,0x15dacc5,0x22bce33,0x303e785,0x27eb4a7,0x1fe6e3b,0x2e2fe1a,0x013f8d0 },
81 /* Z ordinate */
82 { 0x0000001,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000 },
83 /* infinity */
84 0
85};
86
87/* Write r as big endian to byte aray.
88 * Fixed length number of bytes written: 32
89 *
90 * r A single precision integer.
91 * a Byte array.
92 */
93static void sp_256_to_bin(sp_digit* r, uint8_t* a)
94{
95 int i, j, s = 0, b;
96
97 for (i = 0; i < 9; i++) {
98 r[i+1] += r[i] >> 26;
99 r[i] &= 0x3ffffff;
100 }
101 j = 256 / 8 - 1;
102 a[j] = 0;
103 for (i=0; i<10 && j>=0; i++) {
104 b = 0;
105 a[j--] |= r[i] << s; b += 8 - s;
106 if (j < 0)
107 break;
108 while (b < 26) {
109 a[j--] = r[i] >> b; b += 8;
110 if (j < 0)
111 break;
112 }
113 s = 8 - (b - 26);
114 if (j >= 0)
115 a[j] = 0;
116 if (s != 0)
117 j++;
118 }
119}
120
121/* Read big endian unsigned byte aray into r.
122 *
123 * r A single precision integer.
124 * a Byte array.
125 * n Number of bytes in array to read.
126 */
127static void sp_256_from_bin(sp_digit* r, int max, const uint8_t* a, int n)
128{
129 int i, j = 0, s = 0;
130
131 r[0] = 0;
132 for (i = n-1; i >= 0; i--) {
133 r[j] |= ((sp_digit)a[i]) << s;
134 if (s >= 18) {
135 r[j] &= 0x3ffffff;
136 s = 26 - s;
137 if (j + 1 >= max)
138 break;
139 r[++j] = a[i] >> s;
140 s = 8 - s;
141 }
142 else
143 s += 8;
144 }
145
146 for (j++; j < max; j++)
147 r[j] = 0;
148}
149
150/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
151static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
152{
153 memset(p, 0, sizeof(*p));
154 /*p->infinity = 0;*/
155 sp_256_from_bin(p->x, 2 * 10, bin2x32, 32);
156 sp_256_from_bin(p->y, 2 * 10, bin2x32 + 32, 32);
157 //static const uint8_t one[1] = { 1 };
158 //sp_256_from_bin(p->z, 2 * 10, one, 1);
159 p->z[0] = 1;
160}
161
162/* Compare a with b in constant time.
163 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200164 * return -ve, 0 or +ve if a is less than, equal to or greater than b
165 * respectively.
166 */
167static sp_digit sp_256_cmp_10(const sp_digit* a, const sp_digit* b)
168{
169 sp_digit r = 0;
170 int i;
171 for (i = 9; i >= 0; i--)
172 r |= (a[i] - b[i]) & (0 - !r);
173 return r;
174}
175
176/* Compare two numbers to determine if they are equal.
177 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200178 * return 1 when equal and 0 otherwise.
179 */
180static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b)
181{
182#if 1
183 sp_digit r = 0;
184 int i;
185 for (i = 0; i < 10; i++)
186 r |= (a[i] ^ b[i]);
187 return r == 0;
188#else
189 return sp_256_cmp_10(a, b) == 0;
190#endif
191}
192
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200193/* Normalize the values in each word to 26 bits. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200194static void sp_256_norm_10(sp_digit* a)
195{
196 int i;
197 for (i = 0; i < 9; i++) {
198 a[i+1] += a[i] >> 26;
199 a[i] &= 0x3ffffff;
200 }
201}
202
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200203/* Add b to a into r. (r = a + b) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200204static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
205{
206 int i;
207 for (i = 0; i < 10; i++)
208 r[i] = a[i] + b[i];
209}
210
211/* Conditionally add a and b using the mask m.
212 * m is -1 to add and 0 when not.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200213 */
214static void sp_256_cond_add_10(sp_digit* r, const sp_digit* a,
215 const sp_digit* b, const sp_digit m)
216{
217 int i;
218 for (i = 0; i < 10; i++)
219 r[i] = a[i] + (b[i] & m);
220}
221
222/* Conditionally subtract b from a using the mask m.
223 * m is -1 to subtract and 0 when not.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200224 */
225static void sp_256_cond_sub_10(sp_digit* r, const sp_digit* a,
226 const sp_digit* b, const sp_digit m)
227{
228 int i;
229 for (i = 0; i < 10; i++)
230 r[i] = a[i] - (b[i] & m);
231}
232
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200233/* Shift number left one bit. Bottom bit is lost. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200234static void sp_256_rshift1_10(sp_digit* r, sp_digit* a)
235{
236 int i;
237 for (i = 0; i < 9; i++)
238 r[i] = ((a[i] >> 1) | (a[i + 1] << 25)) & 0x3ffffff;
239 r[9] = a[9] >> 1;
240}
241
242/* Multiply a number by Montogmery normalizer mod modulus (prime).
243 *
244 * r The resulting Montgomery form number.
245 * a The number to convert.
246 */
247static void sp_256_mod_mul_norm_10(sp_digit* r, const sp_digit* a)
248{
249 int64_t t[8];
250 int64_t a32[8];
251 int64_t o;
252
253 a32[0] = a[0];
254 a32[0] |= a[1] << 26;
255 a32[0] &= 0xffffffff;
256 a32[1] = (sp_digit)(a[1] >> 6);
257 a32[1] |= a[2] << 20;
258 a32[1] &= 0xffffffff;
259 a32[2] = (sp_digit)(a[2] >> 12);
260 a32[2] |= a[3] << 14;
261 a32[2] &= 0xffffffff;
262 a32[3] = (sp_digit)(a[3] >> 18);
263 a32[3] |= a[4] << 8;
264 a32[3] &= 0xffffffff;
265 a32[4] = (sp_digit)(a[4] >> 24);
266 a32[4] |= a[5] << 2;
267 a32[4] |= a[6] << 28;
268 a32[4] &= 0xffffffff;
269 a32[5] = (sp_digit)(a[6] >> 4);
270 a32[5] |= a[7] << 22;
271 a32[5] &= 0xffffffff;
272 a32[6] = (sp_digit)(a[7] >> 10);
273 a32[6] |= a[8] << 16;
274 a32[6] &= 0xffffffff;
275 a32[7] = (sp_digit)(a[8] >> 16);
276 a32[7] |= a[9] << 10;
277 a32[7] &= 0xffffffff;
278
279 /* 1 1 0 -1 -1 -1 -1 0 */
280 t[0] = 0 + a32[0] + a32[1] - a32[3] - a32[4] - a32[5] - a32[6];
281 /* 0 1 1 0 -1 -1 -1 -1 */
282 t[1] = 0 + a32[1] + a32[2] - a32[4] - a32[5] - a32[6] - a32[7];
283 /* 0 0 1 1 0 -1 -1 -1 */
284 t[2] = 0 + a32[2] + a32[3] - a32[5] - a32[6] - a32[7];
285 /* -1 -1 0 2 2 1 0 -1 */
286 t[3] = 0 - a32[0] - a32[1] + 2 * a32[3] + 2 * a32[4] + a32[5] - a32[7];
287 /* 0 -1 -1 0 2 2 1 0 */
288 t[4] = 0 - a32[1] - a32[2] + 2 * a32[4] + 2 * a32[5] + a32[6];
289 /* 0 0 -1 -1 0 2 2 1 */
290 t[5] = 0 - a32[2] - a32[3] + 2 * a32[5] + 2 * a32[6] + a32[7];
291 /* -1 -1 0 0 0 1 3 2 */
292 t[6] = 0 - a32[0] - a32[1] + a32[5] + 3 * a32[6] + 2 * a32[7];
293 /* 1 0 -1 -1 -1 -1 0 3 */
294 t[7] = 0 + a32[0] - a32[2] - a32[3] - a32[4] - a32[5] + 3 * a32[7];
295
296 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
297 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
298 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
299 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
300 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
301 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
302 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
303 o = t[7] >> 32; t[7] &= 0xffffffff;
304 t[0] += o;
305 t[3] -= o;
306 t[6] -= o;
307 t[7] += o;
308 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
309 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
310 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
311 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
312 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
313 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
314 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
315
316 r[0] = (sp_digit)(t[0]) & 0x3ffffff;
317 r[1] = (sp_digit)(t[0] >> 26);
318 r[1] |= t[1] << 6;
319 r[1] &= 0x3ffffff;
320 r[2] = (sp_digit)(t[1] >> 20);
321 r[2] |= t[2] << 12;
322 r[2] &= 0x3ffffff;
323 r[3] = (sp_digit)(t[2] >> 14);
324 r[3] |= t[3] << 18;
325 r[3] &= 0x3ffffff;
326 r[4] = (sp_digit)(t[3] >> 8);
327 r[4] |= t[4] << 24;
328 r[4] &= 0x3ffffff;
329 r[5] = (sp_digit)(t[4] >> 2) & 0x3ffffff;
330 r[6] = (sp_digit)(t[4] >> 28);
331 r[6] |= t[5] << 4;
332 r[6] &= 0x3ffffff;
333 r[7] = (sp_digit)(t[5] >> 22);
334 r[7] |= t[6] << 10;
335 r[7] &= 0x3ffffff;
336 r[8] = (sp_digit)(t[6] >> 16);
337 r[8] |= t[7] << 16;
338 r[8] &= 0x3ffffff;
339 r[9] = (sp_digit)(t[7] >> 10);
340}
341
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200342/* Mul a by scalar b and add into r. (r += a * b) */
343static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200344{
345 int64_t tb = b;
346 int64_t t = 0;
347 int i;
348
349 for (i = 0; i < 10; i++) {
350 t += (tb * a[i]) + r[i];
351 r[i] = t & 0x3ffffff;
352 t >>= 26;
353 }
354 r[10] += t;
355}
356
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200357/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200358static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
359{
360 sp_256_cond_add_10(r, a, m, 0 - (a[0] & 1));
361 sp_256_norm_10(r);
362 sp_256_rshift1_10(r, r);
363}
364
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200365/* Shift the result in the high 256 bits down to the bottom. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200366static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a)
367{
368 int i;
369 sp_digit n, s;
370
371 s = a[10];
372 n = a[9] >> 22;
373 for (i = 0; i < 9; i++) {
374 n += (s & 0x3ffffff) << 4;
375 r[i] = n & 0x3ffffff;
376 n >>= 26;
377 s = a[11 + i] + (s >> 26);
378 }
379 n += s << 4;
380 r[9] = n;
381 memset(&r[10], 0, sizeof(*r) * 10);
382}
383
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200384/* Add two Montgomery form numbers (r = a + b % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200385static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
386 const sp_digit* m)
387{
388 sp_256_add_10(r, a, b);
389 sp_256_norm_10(r);
390 sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
391 sp_256_norm_10(r);
392}
393
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200394/* Double a Montgomery form number (r = a + a % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200395static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
396{
397 sp_256_add_10(r, a, a);
398 sp_256_norm_10(r);
399 sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
400 sp_256_norm_10(r);
401}
402
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200403/* Triple a Montgomery form number (r = a + a + a % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200404static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
405{
406 sp_256_add_10(r, a, a);
407 sp_256_norm_10(r);
408 sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
409 sp_256_norm_10(r);
410 sp_256_add_10(r, r, a);
411 sp_256_norm_10(r);
412 sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
413 sp_256_norm_10(r);
414}
415
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200416/* Sub b from a into r. (r = a - b) */
417static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200418{
419 int i;
420 for (i = 0; i < 10; i++)
421 r[i] = a[i] - b[i];
422}
423
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200424/* Subtract two Montgomery form numbers (r = a - b % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200425static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
426 const sp_digit* m)
427{
428 sp_256_sub_10(r, a, b);
429 sp_256_cond_add_10(r, r, m, r[9] >> 22);
430 sp_256_norm_10(r);
431}
432
433/* Reduce the number back to 256 bits using Montgomery reduction.
434 *
435 * a A single precision number to reduce in place.
436 * m The single precision number representing the modulus.
437 * mp The digit representing the negative inverse of m mod 2^n.
438 */
439static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp)
440{
441 int i;
442 sp_digit mu;
443
444 if (mp != 1) {
445 for (i = 0; i < 9; i++) {
446 mu = (a[i] * mp) & 0x3ffffff;
447 sp_256_mul_add_10(a+i, m, mu);
448 a[i+1] += a[i] >> 26;
449 }
450 mu = (a[i] * mp) & 0x3fffffl;
451 sp_256_mul_add_10(a+i, m, mu);
452 a[i+1] += a[i] >> 26;
453 a[i] &= 0x3ffffff;
454 }
455 else {
456 for (i = 0; i < 9; i++) {
457 mu = a[i] & 0x3ffffff;
458 sp_256_mul_add_10(a+i, p256_mod, mu);
459 a[i+1] += a[i] >> 26;
460 }
461 mu = a[i] & 0x3fffffl;
462 sp_256_mul_add_10(a+i, p256_mod, mu);
463 a[i+1] += a[i] >> 26;
464 a[i] &= 0x3ffffff;
465 }
466
467 sp_256_mont_shift_10(a, a);
468 sp_256_cond_sub_10(a, a, m, 0 - ((a[9] >> 22) > 0));
469 sp_256_norm_10(a);
470}
471
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200472/* Multiply a and b into r. (r = a * b) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200473static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
474{
475 int i, j, k;
476 int64_t c;
477
478 c = ((int64_t)a[9]) * b[9];
479 r[19] = (sp_digit)(c >> 26);
480 c = (c & 0x3ffffff) << 26;
481 for (k = 17; k >= 0; k--) {
482 for (i = 9; i >= 0; i--) {
483 j = k - i;
484 if (j >= 10)
485 break;
486 if (j < 0)
487 continue;
488 c += ((int64_t)a[i]) * b[j];
489 }
490 r[k + 2] += c >> 52;
491 r[k + 1] = (c >> 26) & 0x3ffffff;
492 c = (c & 0x3ffffff) << 26;
493 }
494 r[0] = (sp_digit)(c >> 26);
495}
496
497/* Multiply two Montogmery form numbers mod the modulus (prime).
498 * (r = a * b mod m)
499 *
500 * r Result of multiplication.
501 * a First number to multiply in Montogmery form.
502 * b Second number to multiply in Montogmery form.
503 * m Modulus (prime).
504 * mp Montogmery mulitplier.
505 */
506static void sp_256_mont_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
507 const sp_digit* m, sp_digit mp)
508{
509 sp_256_mul_10(r, a, b);
510 sp_256_mont_reduce_10(r, m, mp);
511}
512
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200513/* Square a and put result in r. (r = a * a) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200514static void sp_256_sqr_10(sp_digit* r, const sp_digit* a)
515{
516 int i, j, k;
517 int64_t c;
518
519 c = ((int64_t)a[9]) * a[9];
520 r[19] = (sp_digit)(c >> 26);
521 c = (c & 0x3ffffff) << 26;
522 for (k = 17; k >= 0; k--) {
523 for (i = 9; i >= 0; i--) {
524 j = k - i;
525 if (j >= 10 || i <= j)
526 break;
527 if (j < 0)
528 continue;
529
530 c += ((int64_t)a[i]) * a[j] * 2;
531 }
532 if (i == j)
533 c += ((int64_t)a[i]) * a[i];
534
535 r[k + 2] += c >> 52;
536 r[k + 1] = (c >> 26) & 0x3ffffff;
537 c = (c & 0x3ffffff) << 26;
538 }
539 r[0] = (sp_digit)(c >> 26);
540}
541
542/* Square the Montgomery form number. (r = a * a mod m)
543 *
544 * r Result of squaring.
545 * a Number to square in Montogmery form.
546 * m Modulus (prime).
547 * mp Montogmery mulitplier.
548 */
549static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m,
550 sp_digit mp)
551{
552 sp_256_sqr_10(r, a);
553 sp_256_mont_reduce_10(r, m, mp);
554}
555
556/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
557 * P256 curve. (r = 1 / a mod m)
558 *
559 * r Inverse result.
560 * a Number to invert.
561 * td Temporary data.
562 */
563/* Mod-2 for the P256 curve. */
564static const uint32_t p256_mod_2[8] = {
565 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
566 0x00000000,0x00000000,0x00000001,0xffffffff,
567};
568static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a, sp_digit* td)
569{
570 sp_digit* t = td;
571 int i;
572
573 memcpy(t, a, sizeof(sp_digit) * 10);
574 for (i = 254; i >= 0; i--) {
575 sp_256_mont_sqr_10(t, t, p256_mod, p256_mp_mod);
576 if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))
577 sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod);
578 }
579 memcpy(r, t, sizeof(sp_digit) * 10);
580}
581
582/* Map the Montgomery form projective co-ordinate point to an affine point.
583 *
584 * r Resulting affine co-ordinate point.
585 * p Montgomery form projective co-ordinate point.
586 * t Temporary ordinate data.
587 */
588static void sp_256_map_10(sp_point* r, sp_point* p, sp_digit* t)
589{
590 sp_digit* t1 = t;
591 sp_digit* t2 = t + 2*10;
592 int32_t n;
593
594 sp_256_mont_inv_10(t1, p->z, t + 2*10);
595
596 sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod);
597 sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod);
598
599 /* x /= z^2 */
600 sp_256_mont_mul_10(r->x, p->x, t2, p256_mod, p256_mp_mod);
601 memset(r->x + 10, 0, sizeof(r->x) / 2);
602 sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod);
603 /* Reduce x to less than modulus */
604 n = sp_256_cmp_10(r->x, p256_mod);
605 sp_256_cond_sub_10(r->x, r->x, p256_mod, 0 - (n >= 0));
606 sp_256_norm_10(r->x);
607
608 /* y /= z^3 */
609 sp_256_mont_mul_10(r->y, p->y, t1, p256_mod, p256_mp_mod);
610 memset(r->y + 10, 0, sizeof(r->y) / 2);
611 sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod);
612 /* Reduce y to less than modulus */
613 n = sp_256_cmp_10(r->y, p256_mod);
614 sp_256_cond_sub_10(r->y, r->y, p256_mod, 0 - (n >= 0));
615 sp_256_norm_10(r->y);
616
617 memset(r->z, 0, sizeof(r->z));
618 r->z[0] = 1;
619}
620
621/* Double the Montgomery form projective point p.
622 *
623 * r Result of doubling point.
624 * p Point to double.
625 * t Temporary ordinate data.
626 */
627static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p, sp_digit* t)
628{
629 sp_point *rp[2];
630 sp_point tp;
631 sp_digit* t1 = t;
632 sp_digit* t2 = t + 2*10;
633 sp_digit* x;
634 sp_digit* y;
635 sp_digit* z;
636 int i;
637
638 /* When infinity don't double point passed in - constant time. */
639 rp[0] = r;
640 rp[1] = &tp;
641 x = rp[p->infinity]->x;
642 y = rp[p->infinity]->y;
643 z = rp[p->infinity]->z;
644 /* Put point to double into result - good for infinity. */
645 if (r != p) {
646 for (i = 0; i < 10; i++)
647 r->x[i] = p->x[i];
648 for (i = 0; i < 10; i++)
649 r->y[i] = p->y[i];
650 for (i = 0; i < 10; i++)
651 r->z[i] = p->z[i];
652 r->infinity = p->infinity;
653 }
654
655 /* T1 = Z * Z */
656 sp_256_mont_sqr_10(t1, z, p256_mod, p256_mp_mod);
657 /* Z = Y * Z */
658 sp_256_mont_mul_10(z, y, z, p256_mod, p256_mp_mod);
659 /* Z = 2Z */
660 sp_256_mont_dbl_10(z, z, p256_mod);
661 /* T2 = X - T1 */
662 sp_256_mont_sub_10(t2, x, t1, p256_mod);
663 /* T1 = X + T1 */
664 sp_256_mont_add_10(t1, x, t1, p256_mod);
665 /* T2 = T1 * T2 */
666 sp_256_mont_mul_10(t2, t1, t2, p256_mod, p256_mp_mod);
667 /* T1 = 3T2 */
668 sp_256_mont_tpl_10(t1, t2, p256_mod);
669 /* Y = 2Y */
670 sp_256_mont_dbl_10(y, y, p256_mod);
671 /* Y = Y * Y */
672 sp_256_mont_sqr_10(y, y, p256_mod, p256_mp_mod);
673 /* T2 = Y * Y */
674 sp_256_mont_sqr_10(t2, y, p256_mod, p256_mp_mod);
675 /* T2 = T2/2 */
676 sp_256_div2_10(t2, t2, p256_mod);
677 /* Y = Y * X */
678 sp_256_mont_mul_10(y, y, x, p256_mod, p256_mp_mod);
679 /* X = T1 * T1 */
680 sp_256_mont_mul_10(x, t1, t1, p256_mod, p256_mp_mod);
681 /* X = X - Y */
682 sp_256_mont_sub_10(x, x, y, p256_mod);
683 /* X = X - Y */
684 sp_256_mont_sub_10(x, x, y, p256_mod);
685 /* Y = Y - X */
686 sp_256_mont_sub_10(y, y, x, p256_mod);
687 /* Y = Y * T1 */
688 sp_256_mont_mul_10(y, y, t1, p256_mod, p256_mp_mod);
689 /* Y = Y - T2 */
690 sp_256_mont_sub_10(y, y, t2, p256_mod);
691}
692
693/* Add two Montgomery form projective points.
694 *
695 * r Result of addition.
696 * p Frist point to add.
697 * q Second point to add.
698 * t Temporary ordinate data.
699 */
700static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q,
701 sp_digit* t)
702{
703 sp_point *ap[2];
704 sp_point *rp[2];
705 sp_point tp;
706 sp_digit* t1 = t;
707 sp_digit* t2 = t + 2*10;
708 sp_digit* t3 = t + 4*10;
709 sp_digit* t4 = t + 6*10;
710 sp_digit* t5 = t + 8*10;
711 sp_digit* x;
712 sp_digit* y;
713 sp_digit* z;
714 int i;
715
716 /* Ensure only the first point is the same as the result. */
717 if (q == r) {
718 sp_point* a = p;
719 p = q;
720 q = a;
721 }
722
723 /* Check double */
724 sp_256_sub_10(t1, p256_mod, q->y);
725 sp_256_norm_10(t1);
726 if (sp_256_cmp_equal_10(p->x, q->x)
727 & sp_256_cmp_equal_10(p->z, q->z)
728 & (sp_256_cmp_equal_10(p->y, q->y) | sp_256_cmp_equal_10(p->y, t1))
729 ) {
730 sp_256_proj_point_dbl_10(r, p, t);
731 }
732 else {
733 rp[0] = r;
734 rp[1] = &tp;
735 memset(&tp, 0, sizeof(tp));
736 x = rp[p->infinity | q->infinity]->x;
737 y = rp[p->infinity | q->infinity]->y;
738 z = rp[p->infinity | q->infinity]->z;
739
740 ap[0] = p;
741 ap[1] = q;
742 for (i=0; i<10; i++)
743 r->x[i] = ap[p->infinity]->x[i];
744 for (i=0; i<10; i++)
745 r->y[i] = ap[p->infinity]->y[i];
746 for (i=0; i<10; i++)
747 r->z[i] = ap[p->infinity]->z[i];
748 r->infinity = ap[p->infinity]->infinity;
749
750 /* U1 = X1*Z2^2 */
751 sp_256_mont_sqr_10(t1, q->z, p256_mod, p256_mp_mod);
752 sp_256_mont_mul_10(t3, t1, q->z, p256_mod, p256_mp_mod);
753 sp_256_mont_mul_10(t1, t1, x, p256_mod, p256_mp_mod);
754 /* U2 = X2*Z1^2 */
755 sp_256_mont_sqr_10(t2, z, p256_mod, p256_mp_mod);
756 sp_256_mont_mul_10(t4, t2, z, p256_mod, p256_mp_mod);
757 sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod);
758 /* S1 = Y1*Z2^3 */
759 sp_256_mont_mul_10(t3, t3, y, p256_mod, p256_mp_mod);
760 /* S2 = Y2*Z1^3 */
761 sp_256_mont_mul_10(t4, t4, q->y, p256_mod, p256_mp_mod);
762 /* H = U2 - U1 */
763 sp_256_mont_sub_10(t2, t2, t1, p256_mod);
764 /* R = S2 - S1 */
765 sp_256_mont_sub_10(t4, t4, t3, p256_mod);
766 /* Z3 = H*Z1*Z2 */
767 sp_256_mont_mul_10(z, z, q->z, p256_mod, p256_mp_mod);
768 sp_256_mont_mul_10(z, z, t2, p256_mod, p256_mp_mod);
769 /* X3 = R^2 - H^3 - 2*U1*H^2 */
770 sp_256_mont_sqr_10(x, t4, p256_mod, p256_mp_mod);
771 sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod);
772 sp_256_mont_mul_10(y, t1, t5, p256_mod, p256_mp_mod);
773 sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod);
774 sp_256_mont_sub_10(x, x, t5, p256_mod);
775 sp_256_mont_dbl_10(t1, y, p256_mod);
776 sp_256_mont_sub_10(x, x, t1, p256_mod);
777 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
778 sp_256_mont_sub_10(y, y, x, p256_mod);
779 sp_256_mont_mul_10(y, y, t4, p256_mod, p256_mp_mod);
780 sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod);
781 sp_256_mont_sub_10(y, y, t5, p256_mod);
782 }
783}
784
785/* Multiply the point by the scalar and return the result.
786 * If map is true then convert result to affine co-ordinates.
787 *
788 * r Resulting point.
789 * g Point to multiply.
790 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200791 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200792 */
793static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/)
794{
795 enum { map = 1 }; /* we always convert result to affine coordinates */
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200796 sp_point t[3];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200797 sp_digit tmp[2 * 10 * 5];
798 sp_digit n;
799 int i;
800 int c, y;
801
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200802 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200803
804 /* t[0] = {0, 0, 1} * norm */
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200805 t[0].infinity = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200806 /* t[1] = {g->x, g->y, g->z} * norm */
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200807 sp_256_mod_mul_norm_10(t[1].x, g->x);
808 sp_256_mod_mul_norm_10(t[1].y, g->y);
809 sp_256_mod_mul_norm_10(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200810
811 i = 9;
812 c = 22;
813 n = k[i--] << (26 - c);
814 for (; ; c--) {
815 if (c == 0) {
816 if (i == -1)
817 break;
818
819 n = k[i--];
820 c = 26;
821 }
822
823 y = (n >> 25) & 1;
824 n <<= 1;
825
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200826//FIXME: what's "tmp" and why do we pass it down?
827//is it scratch space for "sensitive" data, to be memset(0) after we are done?
828 sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1], tmp);
829 memcpy(&t[2], &t[y], sizeof(sp_point));
830 sp_256_proj_point_dbl_10(&t[2], &t[2], tmp);
831 memcpy(&t[y], &t[2], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200832 }
833
834 if (map)
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200835 sp_256_map_10(r, &t[0], tmp);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200836 else
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200837 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200838
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200839 memset(tmp, 0, sizeof(tmp)); //paranoia
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200840 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200841}
842
843/* Multiply the base point of P256 by the scalar and return the result.
844 * If map is true then convert result to affine co-ordinates.
845 *
846 * r Resulting point.
847 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200848 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200849 */
850static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
851{
852 sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/);
853}
854
855/* Multiply the point by the scalar and serialize the X ordinate.
856 * The number is 0 padded to maximum size on output.
857 *
858 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200859 * pub2x32 Point to multiply.
860 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200861 */
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200862static void sp_ecc_secret_gen_256(sp_digit priv[10], const uint8_t *pub2x32, uint8_t* out32)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200863{
864 sp_point point[1];
865
866#if FIXED_PEER_PUBKEY
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200867 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200868#endif
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200869 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
870 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200871
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200872 sp_256_point_from_bin2x32(point, pub2x32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200873 dump_hex("point->x %s\n", point->x, sizeof(point->x));
874 dump_hex("point->y %s\n", point->y, sizeof(point->y));
875
876 sp_256_ecc_mulmod_10(point, point, priv);
877
878 sp_256_to_bin(point->x, out32);
879 dump_hex("out32: %s\n", out32, 32);
880}
881
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200882/* Generates a scalar that is in the range 1..order-1. */
883#define SIMPLIFY 1
884/* Add 1 to a. (a = a + 1) */
885#if !SIMPLIFY
886static void sp_256_add_one_10(sp_digit* a)
887{
888 a[0]++;
889 sp_256_norm_10(a);
890}
891#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200892static void sp_256_ecc_gen_k_10(sp_digit k[10])
893{
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200894#if !SIMPLIFY
895 /* The order of the curve P256 minus 2. */
896 static const sp_digit p256_order2[10] = {
897 0x063254f,0x272b0bf,0x1e84f3b,0x2b69c5e,0x3bce6fa,
898 0x3ffffff,0x3ffffff,0x00003ff,0x3ff0000,0x03fffff,
899 };
900#endif
901 uint8_t buf[32];
902
903 for (;;) {
904 tls_get_random(buf, sizeof(buf));
905#if FIXED_SECRET
906 memset(buf, 0x77, sizeof(buf));
907#endif
908 sp_256_from_bin(k, 10, buf, sizeof(buf));
909#if !SIMPLIFY
910 if (sp_256_cmp_10(k, p256_order2) < 0)
911 break;
912#else
913 /* non-loopy version (and not needing p256_order2[]):
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200914 * if most-significant word seems that k can be larger
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200915 * than p256_order2, fix it up:
916 */
917 if (k[9] >= 0x03fffff)
918 k[9] = 0x03ffffe;
919 break;
920#endif
921 }
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200922#if !SIMPLIFY
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200923 sp_256_add_one_10(k);
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200924#else
925 if (k[0] == 0)
926 k[0] = 1;
927#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200928#undef SIMPLIFY
929}
930
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200931/* Makes a random EC key pair. */
932static void sp_ecc_make_key_256(sp_digit privkey[10], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200933{
934 sp_point point[1];
935
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200936 sp_256_ecc_gen_k_10(privkey);
937 sp_256_ecc_mulmod_base_10(point, privkey);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200938 sp_256_to_bin(point->x, pubkey);
939 sp_256_to_bin(point->y, pubkey + 32);
940
941 memset(point, 0, sizeof(point)); //paranoia
942}
943
944void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200945 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200946 const uint8_t *peerkey2x32)
947{
948 sp_digit privkey[10];
949
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200950 sp_ecc_make_key_256(privkey, pubkey2x32);
951 dump_hex("pubkey: %s\n", pubkey2x32, 32);
952 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200953
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200954 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200955 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
956 dump_hex("premaster: %s\n", premaster32, 32);
957}