blob: f9c66b186c76707cd182fcc14f5f9a0ce0c46766 [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/* Write r as big endian to byte aray.
76 * Fixed length number of bytes written: 32
77 *
78 * r A single precision integer.
79 * a Byte array.
80 */
81static void sp_256_to_bin(sp_digit* r, uint8_t* a)
82{
Denys Vlasenko12040122021-04-26 20:24:34 +020083 int i, j, s = 0, b;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020084
Denys Vlasenko12040122021-04-26 20:24:34 +020085 for (i = 0; i < 9; i++) {
86 r[i+1] += r[i] >> 26;
87 r[i] &= 0x3ffffff;
88 }
89 j = 256 / 8 - 1;
90 a[j] = 0;
91 for (i = 0; i < 10 && j >= 0; i++) {
92 b = 0;
93 a[j--] |= r[i] << s; b += 8 - s;
94 if (j < 0)
95 break;
96 while (b < 26) {
97 a[j--] = r[i] >> b; b += 8;
98 if (j < 0)
99 break;
100 }
101 s = 8 - (b - 26);
102 if (j >= 0)
103 a[j] = 0;
104 if (s != 0)
105 j++;
106 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200107}
108
109/* Read big endian unsigned byte aray into r.
110 *
111 * r A single precision integer.
112 * a Byte array.
113 * n Number of bytes in array to read.
114 */
115static void sp_256_from_bin(sp_digit* r, int max, const uint8_t* a, int n)
116{
Denys Vlasenko12040122021-04-26 20:24:34 +0200117 int i, j = 0, s = 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200118
Denys Vlasenko12040122021-04-26 20:24:34 +0200119 r[0] = 0;
120 for (i = n-1; i >= 0; i--) {
121 r[j] |= ((sp_digit)a[i]) << s;
122 if (s >= 18) {
123 r[j] &= 0x3ffffff;
124 s = 26 - s;
125 if (j + 1 >= max)
126 break;
127 r[++j] = a[i] >> s;
128 s = 8 - s;
129 }
130 else
131 s += 8;
132 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200133
Denys Vlasenko12040122021-04-26 20:24:34 +0200134 for (j++; j < max; j++)
135 r[j] = 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200136}
137
138/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
139static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
140{
Denys Vlasenko12040122021-04-26 20:24:34 +0200141 memset(p, 0, sizeof(*p));
142 /*p->infinity = 0;*/
143 sp_256_from_bin(p->x, 2 * 10, bin2x32, 32);
144 sp_256_from_bin(p->y, 2 * 10, bin2x32 + 32, 32);
145 //static const uint8_t one[1] = { 1 };
146 //sp_256_from_bin(p->z, 2 * 10, one, 1);
147 p->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200148}
149
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200150/* Compare a with b.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200151 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200152 * return -ve, 0 or +ve if a is less than, equal to or greater than b
153 * respectively.
154 */
155static sp_digit sp_256_cmp_10(const sp_digit* a, const sp_digit* b)
156{
Denys Vlasenko12040122021-04-26 20:24:34 +0200157 sp_digit r;
158 int i;
159 for (i = 9; i >= 0; i--) {
160 r = a[i] - b[i];
161 if (r != 0)
162 break;
163 }
164 return r;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200165}
166
167/* Compare two numbers to determine if they are equal.
168 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200169 * return 1 when equal and 0 otherwise.
170 */
171static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b)
172{
Denys Vlasenko12040122021-04-26 20:24:34 +0200173 return sp_256_cmp_10(a, b) == 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200174}
175
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200176/* Normalize the values in each word to 26 bits. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200177static void sp_256_norm_10(sp_digit* a)
178{
Denys Vlasenko12040122021-04-26 20:24:34 +0200179 int i;
180 for (i = 0; i < 9; i++) {
181 a[i+1] += a[i] >> 26;
182 a[i] &= 0x3ffffff;
183 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200184}
185
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200186/* Add b to a into r. (r = a + b) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200187static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
188{
Denys Vlasenko12040122021-04-26 20:24:34 +0200189 int i;
190 for (i = 0; i < 10; i++)
191 r[i] = a[i] + b[i];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200192}
193
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200194/* Sub b from a into r. (r = a - b) */
195static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200196{
Denys Vlasenko12040122021-04-26 20:24:34 +0200197 int i;
198 for (i = 0; i < 10; i++)
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200199 r[i] = a[i] - b[i];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200200}
201
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200202/* Shift number left one bit. Bottom bit is lost. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200203static void sp_256_rshift1_10(sp_digit* r, sp_digit* a)
204{
Denys Vlasenko12040122021-04-26 20:24:34 +0200205 int i;
206 for (i = 0; i < 9; i++)
207 r[i] = ((a[i] >> 1) | (a[i + 1] << 25)) & 0x3ffffff;
208 r[9] = a[9] >> 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200209}
210
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200211/* Mul a by scalar b and add into r. (r += a * b) */
212static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b)
213{
214 int64_t tb = b;
215 int64_t t = 0;
216 int i;
217
218 for (i = 0; i < 10; i++) {
219 t += (tb * a[i]) + r[i];
220 r[i] = t & 0x3ffffff;
221 t >>= 26;
222 }
223 r[10] += t;
224}
225
226/* Multiply a and b into r. (r = a * b) */
227static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
228{
229 int i, j, k;
230 int64_t c;
231
232 c = ((int64_t)a[9]) * b[9];
233 r[19] = (sp_digit)(c >> 26);
234 c = (c & 0x3ffffff) << 26;
235 for (k = 17; k >= 0; k--) {
236 for (i = 9; i >= 0; i--) {
237 j = k - i;
238 if (j >= 10)
239 break;
240 if (j < 0)
241 continue;
242 c += ((int64_t)a[i]) * b[j];
243 }
244 r[k + 2] += c >> 52;
245 r[k + 1] = (c >> 26) & 0x3ffffff;
246 c = (c & 0x3ffffff) << 26;
247 }
248 r[0] = (sp_digit)(c >> 26);
249}
250
251/* Square a and put result in r. (r = a * a) */
252static void sp_256_sqr_10(sp_digit* r, const sp_digit* a)
253{
254 int i, j, k;
255 int64_t c;
256
257 c = ((int64_t)a[9]) * a[9];
258 r[19] = (sp_digit)(c >> 26);
259 c = (c & 0x3ffffff) << 26;
260 for (k = 17; k >= 0; k--) {
261 for (i = 9; i >= 0; i--) {
262 j = k - i;
263 if (j >= 10 || i <= j)
264 break;
265 if (j < 0)
266 continue;
267 c += ((int64_t)a[i]) * a[j] * 2;
268 }
269 if (i == j)
270 c += ((int64_t)a[i]) * a[i];
271 r[k + 2] += c >> 52;
272 r[k + 1] = (c >> 26) & 0x3ffffff;
273 c = (c & 0x3ffffff) << 26;
274 }
275 r[0] = (sp_digit)(c >> 26);
276}
277
278/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
279static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
280{
281 if (a[0] & 1)
282 sp_256_add_10(r, a, m);
283 sp_256_norm_10(r);
284 sp_256_rshift1_10(r, r);
285}
286
287/* Add two Montgomery form numbers (r = a + b % m) */
288static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
289 const sp_digit* m)
290{
291 sp_256_add_10(r, a, b);
292 sp_256_norm_10(r);
293 if ((r[9] >> 22) > 0)
294 sp_256_sub_10(r, r, m);
295 sp_256_norm_10(r);
296}
297
298/* Subtract two Montgomery form numbers (r = a - b % m) */
299static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
300 const sp_digit* m)
301{
302 sp_256_sub_10(r, a, b);
303 if (r[9] >> 22)
304 sp_256_add_10(r, r, m);
305 sp_256_norm_10(r);
306}
307
308/* Double a Montgomery form number (r = a + a % m) */
309static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
310{
311 sp_256_add_10(r, a, a);
312 sp_256_norm_10(r);
313 if ((r[9] >> 22) > 0)
314 sp_256_sub_10(r, r, m);
315 sp_256_norm_10(r);
316}
317
318/* Triple a Montgomery form number (r = a + a + a % m) */
319static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
320{
321 sp_256_add_10(r, a, a);
322 sp_256_norm_10(r);
323 if ((r[9] >> 22) > 0)
324 sp_256_sub_10(r, r, m);
325 sp_256_norm_10(r);
326 sp_256_add_10(r, r, a);
327 sp_256_norm_10(r);
328 if ((r[9] >> 22) > 0)
329 sp_256_sub_10(r, r, m);
330 sp_256_norm_10(r);
331}
332
333/* Shift the result in the high 256 bits down to the bottom. */
334static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a)
335{
336 int i;
337 sp_digit n, s;
338
339 s = a[10];
340 n = a[9] >> 22;
341 for (i = 0; i < 9; i++) {
342 n += (s & 0x3ffffff) << 4;
343 r[i] = n & 0x3ffffff;
344 n >>= 26;
345 s = a[11 + i] + (s >> 26);
346 }
347 n += s << 4;
348 r[9] = n;
349 memset(&r[10], 0, sizeof(*r) * 10);
350}
351
352/* Reduce the number back to 256 bits using Montgomery reduction.
353 *
354 * a A single precision number to reduce in place.
355 * m The single precision number representing the modulus.
356 * mp The digit representing the negative inverse of m mod 2^n.
357 */
358static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp)
359{
360 int i;
361 sp_digit mu;
362
363 if (mp != 1) {
364 for (i = 0; i < 9; i++) {
365 mu = (a[i] * mp) & 0x3ffffff;
366 sp_256_mul_add_10(a+i, m, mu);
367 a[i+1] += a[i] >> 26;
368 }
369 mu = (a[i] * mp) & 0x3fffffl;
370 sp_256_mul_add_10(a+i, m, mu);
371 a[i+1] += a[i] >> 26;
372 a[i] &= 0x3ffffff;
373 }
374 else {
375 for (i = 0; i < 9; i++) {
376 mu = a[i] & 0x3ffffff;
377 sp_256_mul_add_10(a+i, p256_mod, mu);
378 a[i+1] += a[i] >> 26;
379 }
380 mu = a[i] & 0x3fffffl;
381 sp_256_mul_add_10(a+i, p256_mod, mu);
382 a[i+1] += a[i] >> 26;
383 a[i] &= 0x3ffffff;
384 }
385
386 sp_256_mont_shift_10(a, a);
387 if ((a[9] >> 22) > 0)
388 sp_256_sub_10(a, a, m);
389 sp_256_norm_10(a);
390}
391
392/* Multiply two Montogmery form numbers mod the modulus (prime).
393 * (r = a * b mod m)
394 *
395 * r Result of multiplication.
396 * a First number to multiply in Montogmery form.
397 * b Second number to multiply in Montogmery form.
398 * m Modulus (prime).
399 * mp Montogmery mulitplier.
400 */
401static void sp_256_mont_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
402 const sp_digit* m, sp_digit mp)
403{
404 sp_256_mul_10(r, a, b);
405 sp_256_mont_reduce_10(r, m, mp);
406}
407
408/* Square the Montgomery form number. (r = a * a mod m)
409 *
410 * r Result of squaring.
411 * a Number to square in Montogmery form.
412 * m Modulus (prime).
413 * mp Montogmery mulitplier.
414 */
415static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m,
416 sp_digit mp)
417{
418 sp_256_sqr_10(r, a);
419 sp_256_mont_reduce_10(r, m, mp);
420}
421
422/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
423 * P256 curve. (r = 1 / a mod m)
424 *
425 * r Inverse result.
426 * a Number to invert.
427 */
428#if 0
429/* Mod-2 for the P256 curve. */
430static const uint32_t p256_mod_2[8] = {
431 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
432 0x00000000,0x00000000,0x00000001,0xffffffff,
433};
434//Bit pattern:
435//2 2 2 2 2 2 2 1...1
436//5 5 4 3 2 1 0 9...0 9...1
437//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
438//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
439#endif
440static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a)
441{
442 sp_digit t[2*10]; //can be just [10]?
443 int i;
444
445 memcpy(t, a, sizeof(sp_digit) * 10);
446 for (i = 254; i >= 0; i--) {
447 sp_256_mont_sqr_10(t, t, p256_mod, p256_mp_mod);
448 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
449 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
450 sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod);
451 }
452 memcpy(r, t, sizeof(sp_digit) * 10);
453}
454
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200455/* Multiply a number by Montogmery normalizer mod modulus (prime).
456 *
457 * r The resulting Montgomery form number.
458 * a The number to convert.
459 */
460static void sp_256_mod_mul_norm_10(sp_digit* r, const sp_digit* a)
461{
Denys Vlasenko12040122021-04-26 20:24:34 +0200462 int64_t t[8];
463 int64_t a32[8];
464 int64_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200465
Denys Vlasenko12040122021-04-26 20:24:34 +0200466 a32[0] = a[0];
467 a32[0] |= a[1] << 26;
468 a32[0] &= 0xffffffff;
469 a32[1] = (sp_digit)(a[1] >> 6);
470 a32[1] |= a[2] << 20;
471 a32[1] &= 0xffffffff;
472 a32[2] = (sp_digit)(a[2] >> 12);
473 a32[2] |= a[3] << 14;
474 a32[2] &= 0xffffffff;
475 a32[3] = (sp_digit)(a[3] >> 18);
476 a32[3] |= a[4] << 8;
477 a32[3] &= 0xffffffff;
478 a32[4] = (sp_digit)(a[4] >> 24);
479 a32[4] |= a[5] << 2;
480 a32[4] |= a[6] << 28;
481 a32[4] &= 0xffffffff;
482 a32[5] = (sp_digit)(a[6] >> 4);
483 a32[5] |= a[7] << 22;
484 a32[5] &= 0xffffffff;
485 a32[6] = (sp_digit)(a[7] >> 10);
486 a32[6] |= a[8] << 16;
487 a32[6] &= 0xffffffff;
488 a32[7] = (sp_digit)(a[8] >> 16);
489 a32[7] |= a[9] << 10;
490 a32[7] &= 0xffffffff;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200491
Denys Vlasenko12040122021-04-26 20:24:34 +0200492 /* 1 1 0 -1 -1 -1 -1 0 */
493 t[0] = 0 + a32[0] + a32[1] - a32[3] - a32[4] - a32[5] - a32[6];
494 /* 0 1 1 0 -1 -1 -1 -1 */
495 t[1] = 0 + a32[1] + a32[2] - a32[4] - a32[5] - a32[6] - a32[7];
496 /* 0 0 1 1 0 -1 -1 -1 */
497 t[2] = 0 + a32[2] + a32[3] - a32[5] - a32[6] - a32[7];
498 /* -1 -1 0 2 2 1 0 -1 */
499 t[3] = 0 - a32[0] - a32[1] + 2 * a32[3] + 2 * a32[4] + a32[5] - a32[7];
500 /* 0 -1 -1 0 2 2 1 0 */
501 t[4] = 0 - a32[1] - a32[2] + 2 * a32[4] + 2 * a32[5] + a32[6];
502 /* 0 0 -1 -1 0 2 2 1 */
503 t[5] = 0 - a32[2] - a32[3] + 2 * a32[5] + 2 * a32[6] + a32[7];
504 /* -1 -1 0 0 0 1 3 2 */
505 t[6] = 0 - a32[0] - a32[1] + a32[5] + 3 * a32[6] + 2 * a32[7];
506 /* 1 0 -1 -1 -1 -1 0 3 */
507 t[7] = 0 + a32[0] - a32[2] - a32[3] - a32[4] - a32[5] + 3 * a32[7];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200508
Denys Vlasenko12040122021-04-26 20:24:34 +0200509 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
510 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
511 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
512 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
513 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
514 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
515 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
516 o = t[7] >> 32; t[7] &= 0xffffffff;
517 t[0] += o;
518 t[3] -= o;
519 t[6] -= o;
520 t[7] += o;
521 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
522 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
523 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
524 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
525 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
526 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
527 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200528
Denys Vlasenko12040122021-04-26 20:24:34 +0200529 r[0] = (sp_digit)(t[0]) & 0x3ffffff;
530 r[1] = (sp_digit)(t[0] >> 26);
531 r[1] |= t[1] << 6;
532 r[1] &= 0x3ffffff;
533 r[2] = (sp_digit)(t[1] >> 20);
534 r[2] |= t[2] << 12;
535 r[2] &= 0x3ffffff;
536 r[3] = (sp_digit)(t[2] >> 14);
537 r[3] |= t[3] << 18;
538 r[3] &= 0x3ffffff;
539 r[4] = (sp_digit)(t[3] >> 8);
540 r[4] |= t[4] << 24;
541 r[4] &= 0x3ffffff;
542 r[5] = (sp_digit)(t[4] >> 2) & 0x3ffffff;
543 r[6] = (sp_digit)(t[4] >> 28);
544 r[6] |= t[5] << 4;
545 r[6] &= 0x3ffffff;
546 r[7] = (sp_digit)(t[5] >> 22);
547 r[7] |= t[6] << 10;
548 r[7] &= 0x3ffffff;
549 r[8] = (sp_digit)(t[6] >> 16);
550 r[8] |= t[7] << 16;
551 r[8] &= 0x3ffffff;
552 r[9] = (sp_digit)(t[7] >> 10);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200553}
554
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200555/* Map the Montgomery form projective co-ordinate point to an affine point.
556 *
557 * r Resulting affine co-ordinate point.
558 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200559 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200560static void sp_256_map_10(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200561{
Denys Vlasenko12040122021-04-26 20:24:34 +0200562 sp_digit t1[2*10];
563 sp_digit t2[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200564
Denys Vlasenko12040122021-04-26 20:24:34 +0200565 sp_256_mont_inv_10(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200566
Denys Vlasenko12040122021-04-26 20:24:34 +0200567 sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod);
568 sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200569
Denys Vlasenko12040122021-04-26 20:24:34 +0200570 /* x /= z^2 */
571 sp_256_mont_mul_10(r->x, p->x, t2, p256_mod, p256_mp_mod);
572 memset(r->x + 10, 0, sizeof(r->x) / 2);
573 sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod);
574 /* Reduce x to less than modulus */
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200575 if (sp_256_cmp_10(r->x, p256_mod) >= 0)
576 sp_256_sub_10(r->x, r->x, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +0200577 sp_256_norm_10(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200578
Denys Vlasenko12040122021-04-26 20:24:34 +0200579 /* y /= z^3 */
580 sp_256_mont_mul_10(r->y, p->y, t1, p256_mod, p256_mp_mod);
581 memset(r->y + 10, 0, sizeof(r->y) / 2);
582 sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod);
583 /* Reduce y to less than modulus */
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200584 if (sp_256_cmp_10(r->y, p256_mod) >= 0)
585 sp_256_sub_10(r->y, r->y, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +0200586 sp_256_norm_10(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200587
Denys Vlasenko12040122021-04-26 20:24:34 +0200588 memset(r->z, 0, sizeof(r->z));
589 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200590}
591
592/* Double the Montgomery form projective point p.
593 *
594 * r Result of doubling point.
595 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200596 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200597static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200598{
Denys Vlasenko12040122021-04-26 20:24:34 +0200599 sp_point tp;
600 sp_digit t1[2*10];
601 sp_digit t2[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200602
Denys Vlasenko12040122021-04-26 20:24:34 +0200603 /* Put point to double into result */
604 if (r != p)
605 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200606
Denys Vlasenko12040122021-04-26 20:24:34 +0200607 if (r->infinity) {
608 /* If infinity, don't double (work on dummy value) */
609 r = &tp;
610 }
611 /* T1 = Z * Z */
612 sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod);
613 /* Z = Y * Z */
614 sp_256_mont_mul_10(r->z, r->y, r->z, p256_mod, p256_mp_mod);
615 /* Z = 2Z */
616 sp_256_mont_dbl_10(r->z, r->z, p256_mod);
617 /* T2 = X - T1 */
618 sp_256_mont_sub_10(t2, r->x, t1, p256_mod);
619 /* T1 = X + T1 */
620 sp_256_mont_add_10(t1, r->x, t1, p256_mod);
621 /* T2 = T1 * T2 */
622 sp_256_mont_mul_10(t2, t1, t2, p256_mod, p256_mp_mod);
623 /* T1 = 3T2 */
624 sp_256_mont_tpl_10(t1, t2, p256_mod);
625 /* Y = 2Y */
626 sp_256_mont_dbl_10(r->y, r->y, p256_mod);
627 /* Y = Y * Y */
628 sp_256_mont_sqr_10(r->y, r->y, p256_mod, p256_mp_mod);
629 /* T2 = Y * Y */
630 sp_256_mont_sqr_10(t2, r->y, p256_mod, p256_mp_mod);
631 /* T2 = T2/2 */
632 sp_256_div2_10(t2, t2, p256_mod);
633 /* Y = Y * X */
634 sp_256_mont_mul_10(r->y, r->y, r->x, p256_mod, p256_mp_mod);
635 /* X = T1 * T1 */
636 sp_256_mont_mul_10(r->x, t1, t1, p256_mod, p256_mp_mod);
637 /* X = X - Y */
638 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
639 /* X = X - Y */
640 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
641 /* Y = Y - X */
642 sp_256_mont_sub_10(r->y, r->y, r->x, p256_mod);
643 /* Y = Y * T1 */
644 sp_256_mont_mul_10(r->y, r->y, t1, p256_mod, p256_mp_mod);
645 /* Y = Y - T2 */
646 sp_256_mont_sub_10(r->y, r->y, t2, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200647}
648
649/* Add two Montgomery form projective points.
650 *
651 * r Result of addition.
652 * p Frist point to add.
653 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200654 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200655static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200656{
Denys Vlasenko12040122021-04-26 20:24:34 +0200657 sp_digit t1[2*10];
658 sp_digit t2[2*10];
659 sp_digit t3[2*10];
660 sp_digit t4[2*10];
661 sp_digit t5[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200662
Denys Vlasenko12040122021-04-26 20:24:34 +0200663 /* Ensure only the first point is the same as the result. */
664 if (q == r) {
665 sp_point* a = p;
666 p = q;
667 q = a;
668 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200669
Denys Vlasenko12040122021-04-26 20:24:34 +0200670 /* Check double */
671 sp_256_sub_10(t1, p256_mod, q->y);
672 sp_256_norm_10(t1);
673 if (sp_256_cmp_equal_10(p->x, q->x)
674 && sp_256_cmp_equal_10(p->z, q->z)
675 && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1))
676 ) {
677 sp_256_proj_point_dbl_10(r, p);
678 }
679 else {
680 sp_point tp;
681 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200682
Denys Vlasenko12040122021-04-26 20:24:34 +0200683 v = r;
684 if (p->infinity | q->infinity) {
685 memset(&tp, 0, sizeof(tp));
686 v = &tp;
687 }
Denys Vlasenko772e1872021-04-26 17:25:27 +0200688
Denys Vlasenko12040122021-04-26 20:24:34 +0200689 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200690
Denys Vlasenko12040122021-04-26 20:24:34 +0200691 /* U1 = X1*Z2^2 */
692 sp_256_mont_sqr_10(t1, q->z, p256_mod, p256_mp_mod);
693 sp_256_mont_mul_10(t3, t1, q->z, p256_mod, p256_mp_mod);
694 sp_256_mont_mul_10(t1, t1, v->x, p256_mod, p256_mp_mod);
695 /* U2 = X2*Z1^2 */
696 sp_256_mont_sqr_10(t2, v->z, p256_mod, p256_mp_mod);
697 sp_256_mont_mul_10(t4, t2, v->z, p256_mod, p256_mp_mod);
698 sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod);
699 /* S1 = Y1*Z2^3 */
700 sp_256_mont_mul_10(t3, t3, v->y, p256_mod, p256_mp_mod);
701 /* S2 = Y2*Z1^3 */
702 sp_256_mont_mul_10(t4, t4, q->y, p256_mod, p256_mp_mod);
703 /* H = U2 - U1 */
704 sp_256_mont_sub_10(t2, t2, t1, p256_mod);
705 /* R = S2 - S1 */
706 sp_256_mont_sub_10(t4, t4, t3, p256_mod);
707 /* Z3 = H*Z1*Z2 */
708 sp_256_mont_mul_10(v->z, v->z, q->z, p256_mod, p256_mp_mod);
709 sp_256_mont_mul_10(v->z, v->z, t2, p256_mod, p256_mp_mod);
710 /* X3 = R^2 - H^3 - 2*U1*H^2 */
711 sp_256_mont_sqr_10(v->x, t4, p256_mod, p256_mp_mod);
712 sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod);
713 sp_256_mont_mul_10(v->y, t1, t5, p256_mod, p256_mp_mod);
714 sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod);
715 sp_256_mont_sub_10(v->x, v->x, t5, p256_mod);
716 sp_256_mont_dbl_10(t1, v->y, p256_mod);
717 sp_256_mont_sub_10(v->x, v->x, t1, p256_mod);
718 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
719 sp_256_mont_sub_10(v->y, v->y, v->x, p256_mod);
720 sp_256_mont_mul_10(v->y, v->y, t4, p256_mod, p256_mp_mod);
721 sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod);
722 sp_256_mont_sub_10(v->y, v->y, t5, p256_mod);
723 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200724}
725
726/* Multiply the point by the scalar and return the result.
727 * If map is true then convert result to affine co-ordinates.
728 *
729 * r Resulting point.
730 * g Point to multiply.
731 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200732 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200733 */
734static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/)
735{
Denys Vlasenko12040122021-04-26 20:24:34 +0200736 enum { map = 1 }; /* we always convert result to affine coordinates */
737 sp_point t[3];
738 sp_digit n;
739 int i;
740 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200741
Denys Vlasenko12040122021-04-26 20:24:34 +0200742 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200743
Denys Vlasenko12040122021-04-26 20:24:34 +0200744 /* t[0] = {0, 0, 1} * norm */
745 t[0].infinity = 1;
746 /* t[1] = {g->x, g->y, g->z} * norm */
747 sp_256_mod_mul_norm_10(t[1].x, g->x);
748 sp_256_mod_mul_norm_10(t[1].y, g->y);
749 sp_256_mod_mul_norm_10(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200750
Denys Vlasenko12040122021-04-26 20:24:34 +0200751 i = 9;
752 c = 22;
753 n = k[i--] << (26 - c);
754 for (; ; c--) {
755 if (c == 0) {
756 if (i == -1)
757 break;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200758
Denys Vlasenko12040122021-04-26 20:24:34 +0200759 n = k[i--];
760 c = 26;
761 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200762
Denys Vlasenko12040122021-04-26 20:24:34 +0200763 y = (n >> 25) & 1;
764 n <<= 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200765
Denys Vlasenko12040122021-04-26 20:24:34 +0200766 sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]);
767 memcpy(&t[2], &t[y], sizeof(sp_point));
768 sp_256_proj_point_dbl_10(&t[2], &t[2]);
769 memcpy(&t[y], &t[2], sizeof(sp_point));
770 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200771
Denys Vlasenko12040122021-04-26 20:24:34 +0200772 if (map)
773 sp_256_map_10(r, &t[0]);
774 else
775 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200776
Denys Vlasenko12040122021-04-26 20:24:34 +0200777 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200778}
779
780/* Multiply the base point of P256 by the scalar and return the result.
781 * If map is true then convert result to affine co-ordinates.
782 *
783 * r Resulting point.
784 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200785 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200786 */
787static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
788{
Denys Vlasenko39a3ef52021-04-27 01:31:51 +0200789 /* Since this function is called only once, save space:
790 * don't have "static const sp_point p256_base = {...}",
791 * it would have more zeros than data.
792 */
Denys Vlasenko48a18d12021-04-27 12:24:21 +0200793 static const uint8_t p256_base_bin[] = {
794 /* x (big-endian) */
795 0x6b,0x17,0xd1,0xf2,0xe1,0x2c,0x42,0x47,0xf8,0xbc,0xe6,0xe5,0x63,0xa4,0x40,0xf2,0x77,0x03,0x7d,0x81,0x2d,0xeb,0x33,0xa0,0xf4,0xa1,0x39,0x45,0xd8,0x98,0xc2,0x96,
796 /* y */
797 0x4f,0xe3,0x42,0xe2,0xfe,0x1a,0x7f,0x9b,0x8e,0xe7,0xeb,0x4a,0x7c,0x0f,0x9e,0x16,0x2b,0xce,0x33,0x57,0x6b,0x31,0x5e,0xce,0xcb,0xb6,0x40,0x68,0x37,0xbf,0x51,0xf5,
798 /* z will be set to 0, infinity flag to "false" */
Denys Vlasenko39a3ef52021-04-27 01:31:51 +0200799 };
800 sp_point p256_base;
801
Denys Vlasenko48a18d12021-04-27 12:24:21 +0200802 sp_256_point_from_bin2x32(&p256_base, p256_base_bin);
Denys Vlasenko39a3ef52021-04-27 01:31:51 +0200803
Denys Vlasenko12040122021-04-26 20:24:34 +0200804 sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200805}
806
807/* Multiply the point by the scalar and serialize the X ordinate.
808 * The number is 0 padded to maximum size on output.
809 *
810 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200811 * pub2x32 Point to multiply.
812 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200813 */
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200814static void sp_ecc_secret_gen_256(const sp_digit priv[10], const uint8_t *pub2x32, uint8_t* out32)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200815{
Denys Vlasenko12040122021-04-26 20:24:34 +0200816 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200817
818#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +0200819 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200820#endif
Denys Vlasenko12040122021-04-26 20:24:34 +0200821 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
822 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200823
Denys Vlasenko12040122021-04-26 20:24:34 +0200824 sp_256_point_from_bin2x32(point, pub2x32);
825 dump_hex("point->x %s\n", point->x, sizeof(point->x));
826 dump_hex("point->y %s\n", point->y, sizeof(point->y));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200827
Denys Vlasenko12040122021-04-26 20:24:34 +0200828 sp_256_ecc_mulmod_10(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200829
Denys Vlasenko12040122021-04-26 20:24:34 +0200830 sp_256_to_bin(point->x, out32);
831 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200832}
833
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200834/* Generates a scalar that is in the range 1..order-1. */
835#define SIMPLIFY 1
836/* Add 1 to a. (a = a + 1) */
837#if !SIMPLIFY
838static void sp_256_add_one_10(sp_digit* a)
839{
Denys Vlasenko12040122021-04-26 20:24:34 +0200840 a[0]++;
841 sp_256_norm_10(a);
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200842}
843#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200844static void sp_256_ecc_gen_k_10(sp_digit k[10])
845{
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200846#if !SIMPLIFY
847 /* The order of the curve P256 minus 2. */
848 static const sp_digit p256_order2[10] = {
849 0x063254f,0x272b0bf,0x1e84f3b,0x2b69c5e,0x3bce6fa,
850 0x3ffffff,0x3ffffff,0x00003ff,0x3ff0000,0x03fffff,
851 };
852#endif
853 uint8_t buf[32];
854
855 for (;;) {
856 tls_get_random(buf, sizeof(buf));
857#if FIXED_SECRET
858 memset(buf, 0x77, sizeof(buf));
859#endif
860 sp_256_from_bin(k, 10, buf, sizeof(buf));
861#if !SIMPLIFY
862 if (sp_256_cmp_10(k, p256_order2) < 0)
863 break;
864#else
865 /* non-loopy version (and not needing p256_order2[]):
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200866 * if most-significant word seems that k can be larger
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200867 * than p256_order2, fix it up:
868 */
869 if (k[9] >= 0x03fffff)
870 k[9] = 0x03ffffe;
871 break;
872#endif
873 }
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200874#if !SIMPLIFY
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200875 sp_256_add_one_10(k);
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200876#else
877 if (k[0] == 0)
878 k[0] = 1;
879#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200880#undef SIMPLIFY
881}
882
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200883/* Makes a random EC key pair. */
884static void sp_ecc_make_key_256(sp_digit privkey[10], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200885{
886 sp_point point[1];
887
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200888 sp_256_ecc_gen_k_10(privkey);
889 sp_256_ecc_mulmod_base_10(point, privkey);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200890 sp_256_to_bin(point->x, pubkey);
891 sp_256_to_bin(point->y, pubkey + 32);
892
893 memset(point, 0, sizeof(point)); //paranoia
894}
895
896void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200897 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200898 const uint8_t *peerkey2x32)
899{
900 sp_digit privkey[10];
901
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200902 sp_ecc_make_key_256(privkey, pubkey2x32);
903 dump_hex("pubkey: %s\n", pubkey2x32, 32);
904 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200905
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200906 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200907 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
908 dump_hex("premaster: %s\n", premaster32, 32);
909}