blob: 8059f6e103088ac3e3b24dc9f42dd2d16b25cc5f [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{
Denys Vlasenko12040122021-04-26 20:24:34 +020095 int i, j, s = 0, b;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020096
Denys Vlasenko12040122021-04-26 20:24:34 +020097 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 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200119}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200129 int i, j = 0, s = 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200130
Denys Vlasenko12040122021-04-26 20:24:34 +0200131 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 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200145
Denys Vlasenko12040122021-04-26 20:24:34 +0200146 for (j++; j < max; j++)
147 r[j] = 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200148}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200153 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;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200160}
161
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200162/* Compare a with b.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200163 *
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200169 sp_digit r;
170 int i;
171 for (i = 9; i >= 0; i--) {
172 r = a[i] - b[i];
173 if (r != 0)
174 break;
175 }
176 return r;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200177}
178
179/* Compare two numbers to determine if they are equal.
180 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200181 * return 1 when equal and 0 otherwise.
182 */
183static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b)
184{
Denys Vlasenko12040122021-04-26 20:24:34 +0200185 return sp_256_cmp_10(a, b) == 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200186}
187
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200188/* Normalize the values in each word to 26 bits. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200189static void sp_256_norm_10(sp_digit* a)
190{
Denys Vlasenko12040122021-04-26 20:24:34 +0200191 int i;
192 for (i = 0; i < 9; i++) {
193 a[i+1] += a[i] >> 26;
194 a[i] &= 0x3ffffff;
195 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200196}
197
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200198/* Add b to a into r. (r = a + b) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200199static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
200{
Denys Vlasenko12040122021-04-26 20:24:34 +0200201 int i;
202 for (i = 0; i < 10; i++)
203 r[i] = a[i] + b[i];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200204}
205
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200206/* Sub b from a into r. (r = a - b) */
207static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200208{
Denys Vlasenko12040122021-04-26 20:24:34 +0200209 int i;
210 for (i = 0; i < 10; i++)
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200211 r[i] = a[i] - b[i];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200212}
213
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200214/* Shift number left one bit. Bottom bit is lost. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200215static void sp_256_rshift1_10(sp_digit* r, sp_digit* a)
216{
Denys Vlasenko12040122021-04-26 20:24:34 +0200217 int i;
218 for (i = 0; i < 9; i++)
219 r[i] = ((a[i] >> 1) | (a[i + 1] << 25)) & 0x3ffffff;
220 r[9] = a[9] >> 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200221}
222
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200223/* Mul a by scalar b and add into r. (r += a * b) */
224static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b)
225{
226 int64_t tb = b;
227 int64_t t = 0;
228 int i;
229
230 for (i = 0; i < 10; i++) {
231 t += (tb * a[i]) + r[i];
232 r[i] = t & 0x3ffffff;
233 t >>= 26;
234 }
235 r[10] += t;
236}
237
238/* Multiply a and b into r. (r = a * b) */
239static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
240{
241 int i, j, k;
242 int64_t c;
243
244 c = ((int64_t)a[9]) * b[9];
245 r[19] = (sp_digit)(c >> 26);
246 c = (c & 0x3ffffff) << 26;
247 for (k = 17; k >= 0; k--) {
248 for (i = 9; i >= 0; i--) {
249 j = k - i;
250 if (j >= 10)
251 break;
252 if (j < 0)
253 continue;
254 c += ((int64_t)a[i]) * b[j];
255 }
256 r[k + 2] += c >> 52;
257 r[k + 1] = (c >> 26) & 0x3ffffff;
258 c = (c & 0x3ffffff) << 26;
259 }
260 r[0] = (sp_digit)(c >> 26);
261}
262
263/* Square a and put result in r. (r = a * a) */
264static void sp_256_sqr_10(sp_digit* r, const sp_digit* a)
265{
266 int i, j, k;
267 int64_t c;
268
269 c = ((int64_t)a[9]) * a[9];
270 r[19] = (sp_digit)(c >> 26);
271 c = (c & 0x3ffffff) << 26;
272 for (k = 17; k >= 0; k--) {
273 for (i = 9; i >= 0; i--) {
274 j = k - i;
275 if (j >= 10 || i <= j)
276 break;
277 if (j < 0)
278 continue;
279 c += ((int64_t)a[i]) * a[j] * 2;
280 }
281 if (i == j)
282 c += ((int64_t)a[i]) * a[i];
283 r[k + 2] += c >> 52;
284 r[k + 1] = (c >> 26) & 0x3ffffff;
285 c = (c & 0x3ffffff) << 26;
286 }
287 r[0] = (sp_digit)(c >> 26);
288}
289
290/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
291static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
292{
293 if (a[0] & 1)
294 sp_256_add_10(r, a, m);
295 sp_256_norm_10(r);
296 sp_256_rshift1_10(r, r);
297}
298
299/* Add two Montgomery form numbers (r = a + b % m) */
300static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
301 const sp_digit* m)
302{
303 sp_256_add_10(r, a, b);
304 sp_256_norm_10(r);
305 if ((r[9] >> 22) > 0)
306 sp_256_sub_10(r, r, m);
307 sp_256_norm_10(r);
308}
309
310/* Subtract two Montgomery form numbers (r = a - b % m) */
311static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
312 const sp_digit* m)
313{
314 sp_256_sub_10(r, a, b);
315 if (r[9] >> 22)
316 sp_256_add_10(r, r, m);
317 sp_256_norm_10(r);
318}
319
320/* Double a Montgomery form number (r = a + a % m) */
321static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
322{
323 sp_256_add_10(r, a, a);
324 sp_256_norm_10(r);
325 if ((r[9] >> 22) > 0)
326 sp_256_sub_10(r, r, m);
327 sp_256_norm_10(r);
328}
329
330/* Triple a Montgomery form number (r = a + a + a % m) */
331static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
332{
333 sp_256_add_10(r, a, a);
334 sp_256_norm_10(r);
335 if ((r[9] >> 22) > 0)
336 sp_256_sub_10(r, r, m);
337 sp_256_norm_10(r);
338 sp_256_add_10(r, r, a);
339 sp_256_norm_10(r);
340 if ((r[9] >> 22) > 0)
341 sp_256_sub_10(r, r, m);
342 sp_256_norm_10(r);
343}
344
345/* Shift the result in the high 256 bits down to the bottom. */
346static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a)
347{
348 int i;
349 sp_digit n, s;
350
351 s = a[10];
352 n = a[9] >> 22;
353 for (i = 0; i < 9; i++) {
354 n += (s & 0x3ffffff) << 4;
355 r[i] = n & 0x3ffffff;
356 n >>= 26;
357 s = a[11 + i] + (s >> 26);
358 }
359 n += s << 4;
360 r[9] = n;
361 memset(&r[10], 0, sizeof(*r) * 10);
362}
363
364/* Reduce the number back to 256 bits using Montgomery reduction.
365 *
366 * a A single precision number to reduce in place.
367 * m The single precision number representing the modulus.
368 * mp The digit representing the negative inverse of m mod 2^n.
369 */
370static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp)
371{
372 int i;
373 sp_digit mu;
374
375 if (mp != 1) {
376 for (i = 0; i < 9; i++) {
377 mu = (a[i] * mp) & 0x3ffffff;
378 sp_256_mul_add_10(a+i, m, mu);
379 a[i+1] += a[i] >> 26;
380 }
381 mu = (a[i] * mp) & 0x3fffffl;
382 sp_256_mul_add_10(a+i, m, mu);
383 a[i+1] += a[i] >> 26;
384 a[i] &= 0x3ffffff;
385 }
386 else {
387 for (i = 0; i < 9; i++) {
388 mu = a[i] & 0x3ffffff;
389 sp_256_mul_add_10(a+i, p256_mod, mu);
390 a[i+1] += a[i] >> 26;
391 }
392 mu = a[i] & 0x3fffffl;
393 sp_256_mul_add_10(a+i, p256_mod, mu);
394 a[i+1] += a[i] >> 26;
395 a[i] &= 0x3ffffff;
396 }
397
398 sp_256_mont_shift_10(a, a);
399 if ((a[9] >> 22) > 0)
400 sp_256_sub_10(a, a, m);
401 sp_256_norm_10(a);
402}
403
404/* Multiply two Montogmery form numbers mod the modulus (prime).
405 * (r = a * b mod m)
406 *
407 * r Result of multiplication.
408 * a First number to multiply in Montogmery form.
409 * b Second number to multiply in Montogmery form.
410 * m Modulus (prime).
411 * mp Montogmery mulitplier.
412 */
413static void sp_256_mont_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
414 const sp_digit* m, sp_digit mp)
415{
416 sp_256_mul_10(r, a, b);
417 sp_256_mont_reduce_10(r, m, mp);
418}
419
420/* Square the Montgomery form number. (r = a * a mod m)
421 *
422 * r Result of squaring.
423 * a Number to square in Montogmery form.
424 * m Modulus (prime).
425 * mp Montogmery mulitplier.
426 */
427static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m,
428 sp_digit mp)
429{
430 sp_256_sqr_10(r, a);
431 sp_256_mont_reduce_10(r, m, mp);
432}
433
434/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
435 * P256 curve. (r = 1 / a mod m)
436 *
437 * r Inverse result.
438 * a Number to invert.
439 */
440#if 0
441/* Mod-2 for the P256 curve. */
442static const uint32_t p256_mod_2[8] = {
443 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
444 0x00000000,0x00000000,0x00000001,0xffffffff,
445};
446//Bit pattern:
447//2 2 2 2 2 2 2 1...1
448//5 5 4 3 2 1 0 9...0 9...1
449//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
450//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
451#endif
452static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a)
453{
454 sp_digit t[2*10]; //can be just [10]?
455 int i;
456
457 memcpy(t, a, sizeof(sp_digit) * 10);
458 for (i = 254; i >= 0; i--) {
459 sp_256_mont_sqr_10(t, t, p256_mod, p256_mp_mod);
460 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
461 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
462 sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod);
463 }
464 memcpy(r, t, sizeof(sp_digit) * 10);
465}
466
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200467/* Multiply a number by Montogmery normalizer mod modulus (prime).
468 *
469 * r The resulting Montgomery form number.
470 * a The number to convert.
471 */
472static void sp_256_mod_mul_norm_10(sp_digit* r, const sp_digit* a)
473{
Denys Vlasenko12040122021-04-26 20:24:34 +0200474 int64_t t[8];
475 int64_t a32[8];
476 int64_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200477
Denys Vlasenko12040122021-04-26 20:24:34 +0200478 a32[0] = a[0];
479 a32[0] |= a[1] << 26;
480 a32[0] &= 0xffffffff;
481 a32[1] = (sp_digit)(a[1] >> 6);
482 a32[1] |= a[2] << 20;
483 a32[1] &= 0xffffffff;
484 a32[2] = (sp_digit)(a[2] >> 12);
485 a32[2] |= a[3] << 14;
486 a32[2] &= 0xffffffff;
487 a32[3] = (sp_digit)(a[3] >> 18);
488 a32[3] |= a[4] << 8;
489 a32[3] &= 0xffffffff;
490 a32[4] = (sp_digit)(a[4] >> 24);
491 a32[4] |= a[5] << 2;
492 a32[4] |= a[6] << 28;
493 a32[4] &= 0xffffffff;
494 a32[5] = (sp_digit)(a[6] >> 4);
495 a32[5] |= a[7] << 22;
496 a32[5] &= 0xffffffff;
497 a32[6] = (sp_digit)(a[7] >> 10);
498 a32[6] |= a[8] << 16;
499 a32[6] &= 0xffffffff;
500 a32[7] = (sp_digit)(a[8] >> 16);
501 a32[7] |= a[9] << 10;
502 a32[7] &= 0xffffffff;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200503
Denys Vlasenko12040122021-04-26 20:24:34 +0200504 /* 1 1 0 -1 -1 -1 -1 0 */
505 t[0] = 0 + a32[0] + a32[1] - a32[3] - a32[4] - a32[5] - a32[6];
506 /* 0 1 1 0 -1 -1 -1 -1 */
507 t[1] = 0 + a32[1] + a32[2] - a32[4] - a32[5] - a32[6] - a32[7];
508 /* 0 0 1 1 0 -1 -1 -1 */
509 t[2] = 0 + a32[2] + a32[3] - a32[5] - a32[6] - a32[7];
510 /* -1 -1 0 2 2 1 0 -1 */
511 t[3] = 0 - a32[0] - a32[1] + 2 * a32[3] + 2 * a32[4] + a32[5] - a32[7];
512 /* 0 -1 -1 0 2 2 1 0 */
513 t[4] = 0 - a32[1] - a32[2] + 2 * a32[4] + 2 * a32[5] + a32[6];
514 /* 0 0 -1 -1 0 2 2 1 */
515 t[5] = 0 - a32[2] - a32[3] + 2 * a32[5] + 2 * a32[6] + a32[7];
516 /* -1 -1 0 0 0 1 3 2 */
517 t[6] = 0 - a32[0] - a32[1] + a32[5] + 3 * a32[6] + 2 * a32[7];
518 /* 1 0 -1 -1 -1 -1 0 3 */
519 t[7] = 0 + a32[0] - a32[2] - a32[3] - a32[4] - a32[5] + 3 * a32[7];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200520
Denys Vlasenko12040122021-04-26 20:24:34 +0200521 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;
528 o = t[7] >> 32; t[7] &= 0xffffffff;
529 t[0] += o;
530 t[3] -= o;
531 t[6] -= o;
532 t[7] += o;
533 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
534 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
535 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
536 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
537 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
538 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
539 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200540
Denys Vlasenko12040122021-04-26 20:24:34 +0200541 r[0] = (sp_digit)(t[0]) & 0x3ffffff;
542 r[1] = (sp_digit)(t[0] >> 26);
543 r[1] |= t[1] << 6;
544 r[1] &= 0x3ffffff;
545 r[2] = (sp_digit)(t[1] >> 20);
546 r[2] |= t[2] << 12;
547 r[2] &= 0x3ffffff;
548 r[3] = (sp_digit)(t[2] >> 14);
549 r[3] |= t[3] << 18;
550 r[3] &= 0x3ffffff;
551 r[4] = (sp_digit)(t[3] >> 8);
552 r[4] |= t[4] << 24;
553 r[4] &= 0x3ffffff;
554 r[5] = (sp_digit)(t[4] >> 2) & 0x3ffffff;
555 r[6] = (sp_digit)(t[4] >> 28);
556 r[6] |= t[5] << 4;
557 r[6] &= 0x3ffffff;
558 r[7] = (sp_digit)(t[5] >> 22);
559 r[7] |= t[6] << 10;
560 r[7] &= 0x3ffffff;
561 r[8] = (sp_digit)(t[6] >> 16);
562 r[8] |= t[7] << 16;
563 r[8] &= 0x3ffffff;
564 r[9] = (sp_digit)(t[7] >> 10);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200565}
566
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200567/* Map the Montgomery form projective co-ordinate point to an affine point.
568 *
569 * r Resulting affine co-ordinate point.
570 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200571 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200572static void sp_256_map_10(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200573{
Denys Vlasenko12040122021-04-26 20:24:34 +0200574 sp_digit t1[2*10];
575 sp_digit t2[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200576
Denys Vlasenko12040122021-04-26 20:24:34 +0200577 sp_256_mont_inv_10(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200578
Denys Vlasenko12040122021-04-26 20:24:34 +0200579 sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod);
580 sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200581
Denys Vlasenko12040122021-04-26 20:24:34 +0200582 /* x /= z^2 */
583 sp_256_mont_mul_10(r->x, p->x, t2, p256_mod, p256_mp_mod);
584 memset(r->x + 10, 0, sizeof(r->x) / 2);
585 sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod);
586 /* Reduce x to less than modulus */
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200587 if (sp_256_cmp_10(r->x, p256_mod) >= 0)
588 sp_256_sub_10(r->x, r->x, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +0200589 sp_256_norm_10(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200590
Denys Vlasenko12040122021-04-26 20:24:34 +0200591 /* y /= z^3 */
592 sp_256_mont_mul_10(r->y, p->y, t1, p256_mod, p256_mp_mod);
593 memset(r->y + 10, 0, sizeof(r->y) / 2);
594 sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod);
595 /* Reduce y to less than modulus */
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200596 if (sp_256_cmp_10(r->y, p256_mod) >= 0)
597 sp_256_sub_10(r->y, r->y, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +0200598 sp_256_norm_10(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200599
Denys Vlasenko12040122021-04-26 20:24:34 +0200600 memset(r->z, 0, sizeof(r->z));
601 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200602}
603
604/* Double the Montgomery form projective point p.
605 *
606 * r Result of doubling point.
607 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200608 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200609static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200610{
Denys Vlasenko12040122021-04-26 20:24:34 +0200611 sp_point tp;
612 sp_digit t1[2*10];
613 sp_digit t2[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200614
Denys Vlasenko12040122021-04-26 20:24:34 +0200615 /* Put point to double into result */
616 if (r != p)
617 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200618
Denys Vlasenko12040122021-04-26 20:24:34 +0200619 if (r->infinity) {
620 /* If infinity, don't double (work on dummy value) */
621 r = &tp;
622 }
623 /* T1 = Z * Z */
624 sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod);
625 /* Z = Y * Z */
626 sp_256_mont_mul_10(r->z, r->y, r->z, p256_mod, p256_mp_mod);
627 /* Z = 2Z */
628 sp_256_mont_dbl_10(r->z, r->z, p256_mod);
629 /* T2 = X - T1 */
630 sp_256_mont_sub_10(t2, r->x, t1, p256_mod);
631 /* T1 = X + T1 */
632 sp_256_mont_add_10(t1, r->x, t1, p256_mod);
633 /* T2 = T1 * T2 */
634 sp_256_mont_mul_10(t2, t1, t2, p256_mod, p256_mp_mod);
635 /* T1 = 3T2 */
636 sp_256_mont_tpl_10(t1, t2, p256_mod);
637 /* Y = 2Y */
638 sp_256_mont_dbl_10(r->y, r->y, p256_mod);
639 /* Y = Y * Y */
640 sp_256_mont_sqr_10(r->y, r->y, p256_mod, p256_mp_mod);
641 /* T2 = Y * Y */
642 sp_256_mont_sqr_10(t2, r->y, p256_mod, p256_mp_mod);
643 /* T2 = T2/2 */
644 sp_256_div2_10(t2, t2, p256_mod);
645 /* Y = Y * X */
646 sp_256_mont_mul_10(r->y, r->y, r->x, p256_mod, p256_mp_mod);
647 /* X = T1 * T1 */
648 sp_256_mont_mul_10(r->x, t1, t1, p256_mod, p256_mp_mod);
649 /* X = X - Y */
650 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
651 /* X = X - Y */
652 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
653 /* Y = Y - X */
654 sp_256_mont_sub_10(r->y, r->y, r->x, p256_mod);
655 /* Y = Y * T1 */
656 sp_256_mont_mul_10(r->y, r->y, t1, p256_mod, p256_mp_mod);
657 /* Y = Y - T2 */
658 sp_256_mont_sub_10(r->y, r->y, t2, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200659}
660
661/* Add two Montgomery form projective points.
662 *
663 * r Result of addition.
664 * p Frist point to add.
665 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200666 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200667static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200668{
Denys Vlasenko12040122021-04-26 20:24:34 +0200669 sp_digit t1[2*10];
670 sp_digit t2[2*10];
671 sp_digit t3[2*10];
672 sp_digit t4[2*10];
673 sp_digit t5[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200674
Denys Vlasenko12040122021-04-26 20:24:34 +0200675 /* Ensure only the first point is the same as the result. */
676 if (q == r) {
677 sp_point* a = p;
678 p = q;
679 q = a;
680 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200681
Denys Vlasenko12040122021-04-26 20:24:34 +0200682 /* Check double */
683 sp_256_sub_10(t1, p256_mod, q->y);
684 sp_256_norm_10(t1);
685 if (sp_256_cmp_equal_10(p->x, q->x)
686 && sp_256_cmp_equal_10(p->z, q->z)
687 && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1))
688 ) {
689 sp_256_proj_point_dbl_10(r, p);
690 }
691 else {
692 sp_point tp;
693 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200694
Denys Vlasenko12040122021-04-26 20:24:34 +0200695 v = r;
696 if (p->infinity | q->infinity) {
697 memset(&tp, 0, sizeof(tp));
698 v = &tp;
699 }
Denys Vlasenko772e1872021-04-26 17:25:27 +0200700
Denys Vlasenko12040122021-04-26 20:24:34 +0200701 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200702
Denys Vlasenko12040122021-04-26 20:24:34 +0200703 /* U1 = X1*Z2^2 */
704 sp_256_mont_sqr_10(t1, q->z, p256_mod, p256_mp_mod);
705 sp_256_mont_mul_10(t3, t1, q->z, p256_mod, p256_mp_mod);
706 sp_256_mont_mul_10(t1, t1, v->x, p256_mod, p256_mp_mod);
707 /* U2 = X2*Z1^2 */
708 sp_256_mont_sqr_10(t2, v->z, p256_mod, p256_mp_mod);
709 sp_256_mont_mul_10(t4, t2, v->z, p256_mod, p256_mp_mod);
710 sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod);
711 /* S1 = Y1*Z2^3 */
712 sp_256_mont_mul_10(t3, t3, v->y, p256_mod, p256_mp_mod);
713 /* S2 = Y2*Z1^3 */
714 sp_256_mont_mul_10(t4, t4, q->y, p256_mod, p256_mp_mod);
715 /* H = U2 - U1 */
716 sp_256_mont_sub_10(t2, t2, t1, p256_mod);
717 /* R = S2 - S1 */
718 sp_256_mont_sub_10(t4, t4, t3, p256_mod);
719 /* Z3 = H*Z1*Z2 */
720 sp_256_mont_mul_10(v->z, v->z, q->z, p256_mod, p256_mp_mod);
721 sp_256_mont_mul_10(v->z, v->z, t2, p256_mod, p256_mp_mod);
722 /* X3 = R^2 - H^3 - 2*U1*H^2 */
723 sp_256_mont_sqr_10(v->x, t4, p256_mod, p256_mp_mod);
724 sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod);
725 sp_256_mont_mul_10(v->y, t1, t5, p256_mod, p256_mp_mod);
726 sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod);
727 sp_256_mont_sub_10(v->x, v->x, t5, p256_mod);
728 sp_256_mont_dbl_10(t1, v->y, p256_mod);
729 sp_256_mont_sub_10(v->x, v->x, t1, p256_mod);
730 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
731 sp_256_mont_sub_10(v->y, v->y, v->x, p256_mod);
732 sp_256_mont_mul_10(v->y, v->y, t4, p256_mod, p256_mp_mod);
733 sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod);
734 sp_256_mont_sub_10(v->y, v->y, t5, p256_mod);
735 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200736}
737
738/* Multiply the point by the scalar and return the result.
739 * If map is true then convert result to affine co-ordinates.
740 *
741 * r Resulting point.
742 * g Point to multiply.
743 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200744 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200745 */
746static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/)
747{
Denys Vlasenko12040122021-04-26 20:24:34 +0200748 enum { map = 1 }; /* we always convert result to affine coordinates */
749 sp_point t[3];
750 sp_digit n;
751 int i;
752 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200753
Denys Vlasenko12040122021-04-26 20:24:34 +0200754 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200755
Denys Vlasenko12040122021-04-26 20:24:34 +0200756 /* t[0] = {0, 0, 1} * norm */
757 t[0].infinity = 1;
758 /* t[1] = {g->x, g->y, g->z} * norm */
759 sp_256_mod_mul_norm_10(t[1].x, g->x);
760 sp_256_mod_mul_norm_10(t[1].y, g->y);
761 sp_256_mod_mul_norm_10(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200762
Denys Vlasenko12040122021-04-26 20:24:34 +0200763 i = 9;
764 c = 22;
765 n = k[i--] << (26 - c);
766 for (; ; c--) {
767 if (c == 0) {
768 if (i == -1)
769 break;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200770
Denys Vlasenko12040122021-04-26 20:24:34 +0200771 n = k[i--];
772 c = 26;
773 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200774
Denys Vlasenko12040122021-04-26 20:24:34 +0200775 y = (n >> 25) & 1;
776 n <<= 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200777
Denys Vlasenko12040122021-04-26 20:24:34 +0200778 sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]);
779 memcpy(&t[2], &t[y], sizeof(sp_point));
780 sp_256_proj_point_dbl_10(&t[2], &t[2]);
781 memcpy(&t[y], &t[2], sizeof(sp_point));
782 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200783
Denys Vlasenko12040122021-04-26 20:24:34 +0200784 if (map)
785 sp_256_map_10(r, &t[0]);
786 else
787 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200788
Denys Vlasenko12040122021-04-26 20:24:34 +0200789 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200790}
791
792/* Multiply the base point of P256 by the scalar and return the result.
793 * If map is true then convert result to affine co-ordinates.
794 *
795 * r Resulting point.
796 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200797 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200798 */
799static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
800{
Denys Vlasenko12040122021-04-26 20:24:34 +0200801 sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200802}
803
804/* Multiply the point by the scalar and serialize the X ordinate.
805 * The number is 0 padded to maximum size on output.
806 *
807 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200808 * pub2x32 Point to multiply.
809 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200810 */
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200811static 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 +0200812{
Denys Vlasenko12040122021-04-26 20:24:34 +0200813 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200814
815#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +0200816 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200817#endif
Denys Vlasenko12040122021-04-26 20:24:34 +0200818 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
819 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200820
Denys Vlasenko12040122021-04-26 20:24:34 +0200821 sp_256_point_from_bin2x32(point, pub2x32);
822 dump_hex("point->x %s\n", point->x, sizeof(point->x));
823 dump_hex("point->y %s\n", point->y, sizeof(point->y));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200824
Denys Vlasenko12040122021-04-26 20:24:34 +0200825 sp_256_ecc_mulmod_10(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200826
Denys Vlasenko12040122021-04-26 20:24:34 +0200827 sp_256_to_bin(point->x, out32);
828 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200829}
830
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200831/* Generates a scalar that is in the range 1..order-1. */
832#define SIMPLIFY 1
833/* Add 1 to a. (a = a + 1) */
834#if !SIMPLIFY
835static void sp_256_add_one_10(sp_digit* a)
836{
Denys Vlasenko12040122021-04-26 20:24:34 +0200837 a[0]++;
838 sp_256_norm_10(a);
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200839}
840#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200841static void sp_256_ecc_gen_k_10(sp_digit k[10])
842{
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200843#if !SIMPLIFY
844 /* The order of the curve P256 minus 2. */
845 static const sp_digit p256_order2[10] = {
846 0x063254f,0x272b0bf,0x1e84f3b,0x2b69c5e,0x3bce6fa,
847 0x3ffffff,0x3ffffff,0x00003ff,0x3ff0000,0x03fffff,
848 };
849#endif
850 uint8_t buf[32];
851
852 for (;;) {
853 tls_get_random(buf, sizeof(buf));
854#if FIXED_SECRET
855 memset(buf, 0x77, sizeof(buf));
856#endif
857 sp_256_from_bin(k, 10, buf, sizeof(buf));
858#if !SIMPLIFY
859 if (sp_256_cmp_10(k, p256_order2) < 0)
860 break;
861#else
862 /* non-loopy version (and not needing p256_order2[]):
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200863 * if most-significant word seems that k can be larger
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200864 * than p256_order2, fix it up:
865 */
866 if (k[9] >= 0x03fffff)
867 k[9] = 0x03ffffe;
868 break;
869#endif
870 }
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200871#if !SIMPLIFY
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200872 sp_256_add_one_10(k);
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200873#else
874 if (k[0] == 0)
875 k[0] = 1;
876#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200877#undef SIMPLIFY
878}
879
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200880/* Makes a random EC key pair. */
881static void sp_ecc_make_key_256(sp_digit privkey[10], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200882{
883 sp_point point[1];
884
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200885 sp_256_ecc_gen_k_10(privkey);
886 sp_256_ecc_mulmod_base_10(point, privkey);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200887 sp_256_to_bin(point->x, pubkey);
888 sp_256_to_bin(point->y, pubkey + 32);
889
890 memset(point, 0, sizeof(point)); //paranoia
891}
892
893void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200894 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200895 const uint8_t *peerkey2x32)
896{
897 sp_digit privkey[10];
898
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200899 sp_ecc_make_key_256(privkey, pubkey2x32);
900 dump_hex("pubkey: %s\n", pubkey2x32, 32);
901 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200902
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200903 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200904 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
905 dump_hex("premaster: %s\n", premaster32, 32);
906}