blob: 9c92d0a1460855d486079148c37d7e76b86ca381 [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
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020027typedef int32_t sp_digit;
28
29/* The code below is taken from parts of
30 * wolfssl-3.15.3/wolfcrypt/src/sp_c32.c
31 * and heavily modified.
32 * Header comment is kept intact:
33 */
34
35/* sp.c
36 *
37 * Copyright (C) 2006-2018 wolfSSL Inc.
38 *
39 * This file is part of wolfSSL.
40 *
41 * wolfSSL is free software; you can redistribute it and/or modify
42 * it under the terms of the GNU General Public License as published by
43 * the Free Software Foundation; either version 2 of the License, or
44 * (at your option) any later version.
45 *
46 * wolfSSL is distributed in the hope that it will be useful,
47 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 * GNU General Public License for more details.
50 *
51 * You should have received a copy of the GNU General Public License
52 * along with this program; if not, write to the Free Software
53 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
54 */
55
56/* Implementation by Sean Parkinson. */
57
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020058typedef struct sp_point {
59 sp_digit x[2 * 10];
60 sp_digit y[2 * 10];
61 sp_digit z[2 * 10];
62 int infinity;
63} sp_point;
64
65/* The modulus (prime) of the curve P256. */
66static const sp_digit p256_mod[10] = {
67 0x3ffffff,0x3ffffff,0x3ffffff,0x003ffff,0x0000000,
68 0x0000000,0x0000000,0x0000400,0x3ff0000,0x03fffff,
69};
70
71#define p256_mp_mod ((sp_digit)0x000001)
72
Denys Vlasenko77145182021-10-01 13:51:39 +020073/* Normalize the values in each word to 26 bits. */
74static void sp_256_norm_10(sp_digit* a)
75{
76 int i;
77 for (i = 0; i < 9; i++) {
78 a[i+1] += a[i] >> 26;
79 a[i] &= 0x3ffffff;
80 }
81}
82
Denys Vlasenkoe7305052021-10-05 13:30:48 +020083/* Write r as big endian to byte array.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020084 * Fixed length number of bytes written: 32
85 *
86 * r A single precision integer.
87 * a Byte array.
88 */
Denys Vlasenkoe7305052021-10-05 13:30:48 +020089static void sp_256_to_bin_10(sp_digit* r, uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020090{
Denys Vlasenko12040122021-04-26 20:24:34 +020091 int i, j, s = 0, b;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020092
Denys Vlasenko77145182021-10-01 13:51:39 +020093 sp_256_norm_10(r);
94
Denys Vlasenko12040122021-04-26 20:24:34 +020095 j = 256 / 8 - 1;
96 a[j] = 0;
97 for (i = 0; i < 10 && j >= 0; i++) {
98 b = 0;
99 a[j--] |= r[i] << s; b += 8 - s;
100 if (j < 0)
101 break;
102 while (b < 26) {
103 a[j--] = r[i] >> b; b += 8;
104 if (j < 0)
105 break;
106 }
107 s = 8 - (b - 26);
108 if (j >= 0)
109 a[j] = 0;
110 if (s != 0)
111 j++;
112 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200113}
114
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200115/* Read big endian unsigned byte array into r.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200116 *
117 * r A single precision integer.
118 * a Byte array.
119 * n Number of bytes in array to read.
120 */
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200121static void sp_256_from_bin_10(sp_digit* r, const uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200122{
Denys Vlasenko12040122021-04-26 20:24:34 +0200123 int i, j = 0, s = 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200124
Denys Vlasenko12040122021-04-26 20:24:34 +0200125 r[0] = 0;
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200126 for (i = 32 - 1; i >= 0; i--) {
Denys Vlasenko12040122021-04-26 20:24:34 +0200127 r[j] |= ((sp_digit)a[i]) << s;
128 if (s >= 18) {
129 r[j] &= 0x3ffffff;
130 s = 26 - s;
Denys Vlasenko12040122021-04-26 20:24:34 +0200131 r[++j] = a[i] >> s;
132 s = 8 - s;
133 }
134 else
135 s += 8;
136 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200137}
138
139/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
140static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
141{
Denys Vlasenko12040122021-04-26 20:24:34 +0200142 memset(p, 0, sizeof(*p));
143 /*p->infinity = 0;*/
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200144 sp_256_from_bin_10(p->x, bin2x32);
145 sp_256_from_bin_10(p->y, bin2x32 + 32);
146 p->z[0] = 1; /* p->z = 1 */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200147}
148
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200149/* Compare a with b.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200150 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200151 * return -ve, 0 or +ve if a is less than, equal to or greater than b
152 * respectively.
153 */
154static sp_digit sp_256_cmp_10(const sp_digit* a, const sp_digit* b)
155{
Denys Vlasenko12040122021-04-26 20:24:34 +0200156 sp_digit r;
157 int i;
158 for (i = 9; i >= 0; i--) {
159 r = a[i] - b[i];
160 if (r != 0)
161 break;
162 }
163 return r;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200164}
165
166/* Compare two numbers to determine if they are equal.
167 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200168 * return 1 when equal and 0 otherwise.
169 */
170static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b)
171{
Denys Vlasenko12040122021-04-26 20:24:34 +0200172 return sp_256_cmp_10(a, b) == 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200173}
174
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200175/* Add b to a into r. (r = a + b) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200176static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
177{
Denys Vlasenko12040122021-04-26 20:24:34 +0200178 int i;
179 for (i = 0; i < 10; i++)
180 r[i] = a[i] + b[i];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200181}
182
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200183/* Sub b from a into r. (r = a - b) */
184static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200185{
Denys Vlasenko12040122021-04-26 20:24:34 +0200186 int i;
187 for (i = 0; i < 10; i++)
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200188 r[i] = a[i] - b[i];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200189}
190
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200191/* Multiply a and b into r. (r = a * b) */
192static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
193{
194 int i, j, k;
195 int64_t c;
196
197 c = ((int64_t)a[9]) * b[9];
198 r[19] = (sp_digit)(c >> 26);
199 c = (c & 0x3ffffff) << 26;
200 for (k = 17; k >= 0; k--) {
201 for (i = 9; i >= 0; i--) {
202 j = k - i;
203 if (j >= 10)
204 break;
205 if (j < 0)
206 continue;
207 c += ((int64_t)a[i]) * b[j];
208 }
209 r[k + 2] += c >> 52;
210 r[k + 1] = (c >> 26) & 0x3ffffff;
211 c = (c & 0x3ffffff) << 26;
212 }
213 r[0] = (sp_digit)(c >> 26);
214}
215
216/* Square a and put result in r. (r = a * a) */
217static void sp_256_sqr_10(sp_digit* r, const sp_digit* a)
218{
219 int i, j, k;
220 int64_t c;
221
222 c = ((int64_t)a[9]) * a[9];
223 r[19] = (sp_digit)(c >> 26);
224 c = (c & 0x3ffffff) << 26;
225 for (k = 17; k >= 0; k--) {
226 for (i = 9; i >= 0; i--) {
227 j = k - i;
228 if (j >= 10 || i <= j)
229 break;
230 if (j < 0)
231 continue;
232 c += ((int64_t)a[i]) * a[j] * 2;
233 }
234 if (i == j)
235 c += ((int64_t)a[i]) * a[i];
236 r[k + 2] += c >> 52;
237 r[k + 1] = (c >> 26) & 0x3ffffff;
238 c = (c & 0x3ffffff) << 26;
239 }
240 r[0] = (sp_digit)(c >> 26);
241}
242
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200243/* Shift number left one bit. Bottom bit is lost. */
244static void sp_256_rshift1_10(sp_digit* r, sp_digit* a)
245{
246 int i;
247 for (i = 0; i < 9; i++)
248 r[i] = ((a[i] >> 1) | (a[i + 1] << 25)) & 0x3ffffff;
249 r[9] = a[9] >> 1;
250}
251
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200252/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
253static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
254{
255 if (a[0] & 1)
256 sp_256_add_10(r, a, m);
257 sp_256_norm_10(r);
258 sp_256_rshift1_10(r, r);
259}
260
261/* Add two Montgomery form numbers (r = a + b % m) */
262static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
263 const sp_digit* m)
264{
265 sp_256_add_10(r, a, b);
266 sp_256_norm_10(r);
267 if ((r[9] >> 22) > 0)
268 sp_256_sub_10(r, r, m);
269 sp_256_norm_10(r);
270}
271
272/* Subtract two Montgomery form numbers (r = a - b % m) */
273static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
274 const sp_digit* m)
275{
276 sp_256_sub_10(r, a, b);
277 if (r[9] >> 22)
278 sp_256_add_10(r, r, m);
279 sp_256_norm_10(r);
280}
281
282/* Double a Montgomery form number (r = a + a % m) */
283static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
284{
285 sp_256_add_10(r, a, a);
286 sp_256_norm_10(r);
287 if ((r[9] >> 22) > 0)
288 sp_256_sub_10(r, r, m);
289 sp_256_norm_10(r);
290}
291
292/* Triple a Montgomery form number (r = a + a + a % m) */
293static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
294{
295 sp_256_add_10(r, a, a);
296 sp_256_norm_10(r);
297 if ((r[9] >> 22) > 0)
298 sp_256_sub_10(r, r, m);
299 sp_256_norm_10(r);
300 sp_256_add_10(r, r, a);
301 sp_256_norm_10(r);
302 if ((r[9] >> 22) > 0)
303 sp_256_sub_10(r, r, m);
304 sp_256_norm_10(r);
305}
306
307/* Shift the result in the high 256 bits down to the bottom. */
308static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a)
309{
310 int i;
311 sp_digit n, s;
312
313 s = a[10];
314 n = a[9] >> 22;
315 for (i = 0; i < 9; i++) {
316 n += (s & 0x3ffffff) << 4;
317 r[i] = n & 0x3ffffff;
318 n >>= 26;
319 s = a[11 + i] + (s >> 26);
320 }
321 n += s << 4;
322 r[9] = n;
323 memset(&r[10], 0, sizeof(*r) * 10);
324}
325
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200326/* Mul a by scalar b and add into r. (r += a * b) */
327static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b)
328{
329 int64_t t = 0;
330 int i;
331
332 for (i = 0; i < 10; i++) {
333 t += ((int64_t)b * a[i]) + r[i];
334 r[i] = t & 0x3ffffff;
335 t >>= 26;
336 }
337 r[10] += t;
338}
339
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200340/* Reduce the number back to 256 bits using Montgomery reduction.
341 *
342 * a A single precision number to reduce in place.
343 * m The single precision number representing the modulus.
344 * mp The digit representing the negative inverse of m mod 2^n.
345 */
346static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp)
347{
348 int i;
349 sp_digit mu;
350
351 if (mp != 1) {
352 for (i = 0; i < 9; i++) {
353 mu = (a[i] * mp) & 0x3ffffff;
354 sp_256_mul_add_10(a+i, m, mu);
355 a[i+1] += a[i] >> 26;
356 }
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200357 mu = (a[i] * mp) & 0x03fffff;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200358 sp_256_mul_add_10(a+i, m, mu);
359 a[i+1] += a[i] >> 26;
360 a[i] &= 0x3ffffff;
361 }
362 else {
363 for (i = 0; i < 9; i++) {
364 mu = a[i] & 0x3ffffff;
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200365 sp_256_mul_add_10(a+i, m, mu);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200366 a[i+1] += a[i] >> 26;
367 }
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200368 mu = a[i] & 0x03fffff;
369 sp_256_mul_add_10(a+i, m, mu);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200370 a[i+1] += a[i] >> 26;
371 a[i] &= 0x3ffffff;
372 }
373
374 sp_256_mont_shift_10(a, a);
375 if ((a[9] >> 22) > 0)
376 sp_256_sub_10(a, a, m);
377 sp_256_norm_10(a);
378}
379
380/* Multiply two Montogmery form numbers mod the modulus (prime).
381 * (r = a * b mod m)
382 *
383 * r Result of multiplication.
384 * a First number to multiply in Montogmery form.
385 * b Second number to multiply in Montogmery form.
386 * m Modulus (prime).
387 * mp Montogmery mulitplier.
388 */
389static void sp_256_mont_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
390 const sp_digit* m, sp_digit mp)
391{
392 sp_256_mul_10(r, a, b);
393 sp_256_mont_reduce_10(r, m, mp);
394}
395
396/* Square the Montgomery form number. (r = a * a mod m)
397 *
398 * r Result of squaring.
399 * a Number to square in Montogmery form.
400 * m Modulus (prime).
401 * mp Montogmery mulitplier.
402 */
403static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m,
404 sp_digit mp)
405{
406 sp_256_sqr_10(r, a);
407 sp_256_mont_reduce_10(r, m, mp);
408}
409
410/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
411 * P256 curve. (r = 1 / a mod m)
412 *
413 * r Inverse result.
414 * a Number to invert.
415 */
416#if 0
417/* Mod-2 for the P256 curve. */
418static const uint32_t p256_mod_2[8] = {
419 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
420 0x00000000,0x00000000,0x00000001,0xffffffff,
421};
422//Bit pattern:
423//2 2 2 2 2 2 2 1...1
424//5 5 4 3 2 1 0 9...0 9...1
425//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
426//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
427#endif
428static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a)
429{
430 sp_digit t[2*10]; //can be just [10]?
431 int i;
432
433 memcpy(t, a, sizeof(sp_digit) * 10);
434 for (i = 254; i >= 0; i--) {
435 sp_256_mont_sqr_10(t, t, p256_mod, p256_mp_mod);
436 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
437 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
438 sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod);
439 }
440 memcpy(r, t, sizeof(sp_digit) * 10);
441}
442
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200443/* Multiply a number by Montogmery normalizer mod modulus (prime).
444 *
445 * r The resulting Montgomery form number.
446 * a The number to convert.
447 */
448static void sp_256_mod_mul_norm_10(sp_digit* r, const sp_digit* a)
449{
Denys Vlasenko12040122021-04-26 20:24:34 +0200450 int64_t t[8];
Denys Vlasenko12040122021-04-26 20:24:34 +0200451 int64_t o;
Denys Vlasenko646e8562021-04-27 13:09:44 +0200452 uint32_t a32;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200453
Denys Vlasenko12040122021-04-26 20:24:34 +0200454 /* 1 1 0 -1 -1 -1 -1 0 */
Denys Vlasenko12040122021-04-26 20:24:34 +0200455 /* 0 1 1 0 -1 -1 -1 -1 */
Denys Vlasenko12040122021-04-26 20:24:34 +0200456 /* 0 0 1 1 0 -1 -1 -1 */
Denys Vlasenko12040122021-04-26 20:24:34 +0200457 /* -1 -1 0 2 2 1 0 -1 */
Denys Vlasenko12040122021-04-26 20:24:34 +0200458 /* 0 -1 -1 0 2 2 1 0 */
Denys Vlasenko12040122021-04-26 20:24:34 +0200459 /* 0 0 -1 -1 0 2 2 1 */
Denys Vlasenko12040122021-04-26 20:24:34 +0200460 /* -1 -1 0 0 0 1 3 2 */
Denys Vlasenko12040122021-04-26 20:24:34 +0200461 /* 1 0 -1 -1 -1 -1 0 3 */
Denys Vlasenko646e8562021-04-27 13:09:44 +0200462 // t[] should be calculated from "a" (converted from 26-bit to 32-bit vector a32[8])
463 // according to the above matrix:
464 //t[0] = 0 + a32[0] + a32[1] - a32[3] - a32[4] - a32[5] - a32[6] ;
465 //t[1] = 0 + a32[1] + a32[2] - a32[4] - a32[5] - a32[6] - a32[7] ;
466 //t[2] = 0 + a32[2] + a32[3] - a32[5] - a32[6] - a32[7] ;
467 //t[3] = 0 - a32[0] - a32[1] + 2*a32[3] + 2*a32[4] + a32[5] - a32[7] ;
468 //t[4] = 0 - a32[1] - a32[2] + 2*a32[4] + 2*a32[5] + a32[6] ;
469 //t[5] = 0 - a32[2] - a32[3] + 2*a32[5] + 2*a32[6] + a32[7] ;
470 //t[6] = 0 - a32[0] - a32[1] + a32[5] + 3*a32[6] + 2*a32[7];
471 //t[7] = 0 + a32[0] - a32[2] - a32[3] - a32[4] - a32[5] + 3*a32[7];
472 // We can do it "piecemeal" after each a32[i] is known, no need to store entire a32[8] vector:
473
474#define A32 (int64_t)a32
475 a32 = a[0] | (a[1] << 26);
476 t[0] = 0 + A32;
477 t[3] = 0 - A32;
478 t[6] = 0 - A32;
479 t[7] = 0 + A32;
480
481 a32 = (a[1] >> 6) | (a[2] << 20);
482 t[0] += A32 ;
483 t[1] = 0 + A32;
484 t[3] -= A32 ;
485 t[4] = 0 - A32;
486 t[6] -= A32 ;
487
488 a32 = (a[2] >> 12) | (a[3] << 14);
489 t[1] += A32 ;
490 t[2] = 0 + A32;
491 t[4] -= A32 ;
492 t[5] = 0 - A32;
493 t[7] -= A32 ;
494
495 a32 = (a[3] >> 18) | (a[4] << 8);
496 t[0] -= A32 ;
497 t[2] += A32 ;
498 t[3] += 2*A32;
499 t[5] -= A32 ;
500 t[7] -= A32 ;
501
502 a32 = (a[4] >> 24) | (a[5] << 2) | (a[6] << 28);
503 t[0] -= A32 ;
504 t[1] -= A32 ;
505 t[3] += 2*A32;
506 t[4] += 2*A32;
507 t[7] -= A32 ;
508
509 a32 = (a[6] >> 4) | (a[7] << 22);
510 t[0] -= A32 ;
511 t[1] -= A32 ;
512 t[2] -= A32 ;
513 t[3] += A32 ;
514 t[4] += 2*A32;
515 t[5] += 2*A32;
516 t[6] += A32 ;
517 t[7] -= A32 ;
518
519 a32 = (a[7] >> 10) | (a[8] << 16);
520 t[0] -= A32 ;
521 t[1] -= A32 ;
522 t[2] -= A32 ;
523 t[4] += A32 ;
524 t[5] += 2*A32;
525 t[6] += 3*A32;
526
527 a32 = (a[8] >> 16) | (a[9] << 10);
528 t[1] -= A32 ;
529 t[2] -= A32 ;
530 t[3] -= A32 ;
531 t[5] += A32 ;
532 t[6] += 2*A32;
533 t[7] += 3*A32;
534#undef A32
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200535
Denys Vlasenko12040122021-04-26 20:24:34 +0200536 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
537 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
538 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
539 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
540 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
541 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
542 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
543 o = t[7] >> 32; t[7] &= 0xffffffff;
544 t[0] += o;
545 t[3] -= o;
546 t[6] -= o;
547 t[7] += o;
Denys Vlasenko840ae692021-04-27 13:31:26 +0200548 t[1] += t[0] >> 32; //t[0] &= 0xffffffff;
549 t[2] += t[1] >> 32; //t[1] &= 0xffffffff;
550 t[3] += t[2] >> 32; //t[2] &= 0xffffffff;
551 t[4] += t[3] >> 32; //t[3] &= 0xffffffff;
552 t[5] += t[4] >> 32; //t[4] &= 0xffffffff;
553 t[6] += t[5] >> 32; //t[5] &= 0xffffffff;
554 t[7] += t[6] >> 32; //t[6] &= 0xffffffff; - (uint32_t)t[i] casts below accomplish masking
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200555
Denys Vlasenko840ae692021-04-27 13:31:26 +0200556 r[0] = 0x3ffffff & ((sp_digit)((uint32_t)t[0]));
557 r[1] = 0x3ffffff & ((sp_digit)((uint32_t)t[0] >> 26) | ((sp_digit)t[1] << 6));
558 r[2] = 0x3ffffff & ((sp_digit)((uint32_t)t[1] >> 20) | ((sp_digit)t[2] << 12));
559 r[3] = 0x3ffffff & ((sp_digit)((uint32_t)t[2] >> 14) | ((sp_digit)t[3] << 18));
560 r[4] = 0x3ffffff & ((sp_digit)((uint32_t)t[3] >> 8) | ((sp_digit)t[4] << 24));
561 r[5] = 0x3ffffff & ((sp_digit)((uint32_t)t[4] >> 2));
562 r[6] = 0x3ffffff & ((sp_digit)((uint32_t)t[4] >> 28) | ((sp_digit)t[5] << 4));
563 r[7] = 0x3ffffff & ((sp_digit)((uint32_t)t[5] >> 22) | ((sp_digit)t[6] << 10));
564 r[8] = 0x3ffffff & ((sp_digit)((uint32_t)t[6] >> 16) | ((sp_digit)t[7] << 16));
565 r[9] = ((sp_digit)((uint32_t)t[7] >> 10));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200566}
567
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200568/* Map the Montgomery form projective co-ordinate point to an affine point.
569 *
570 * r Resulting affine co-ordinate point.
571 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200572 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200573static void sp_256_map_10(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200574{
Denys Vlasenko12040122021-04-26 20:24:34 +0200575 sp_digit t1[2*10];
576 sp_digit t2[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200577
Denys Vlasenko12040122021-04-26 20:24:34 +0200578 sp_256_mont_inv_10(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200579
Denys Vlasenko12040122021-04-26 20:24:34 +0200580 sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod);
581 sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200582
Denys Vlasenko12040122021-04-26 20:24:34 +0200583 /* x /= z^2 */
584 sp_256_mont_mul_10(r->x, p->x, t2, p256_mod, p256_mp_mod);
585 memset(r->x + 10, 0, sizeof(r->x) / 2);
586 sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod);
587 /* Reduce x to less than modulus */
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200588 if (sp_256_cmp_10(r->x, p256_mod) >= 0)
589 sp_256_sub_10(r->x, r->x, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +0200590 sp_256_norm_10(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200591
Denys Vlasenko12040122021-04-26 20:24:34 +0200592 /* y /= z^3 */
593 sp_256_mont_mul_10(r->y, p->y, t1, p256_mod, p256_mp_mod);
594 memset(r->y + 10, 0, sizeof(r->y) / 2);
595 sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod);
596 /* Reduce y to less than modulus */
Denys Vlasenko9a40be42021-04-26 21:58:04 +0200597 if (sp_256_cmp_10(r->y, p256_mod) >= 0)
598 sp_256_sub_10(r->y, r->y, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +0200599 sp_256_norm_10(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200600
Denys Vlasenko12040122021-04-26 20:24:34 +0200601 memset(r->z, 0, sizeof(r->z));
602 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200603}
604
605/* Double the Montgomery form projective point p.
606 *
607 * r Result of doubling point.
608 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200609 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200610static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200611{
Denys Vlasenko12040122021-04-26 20:24:34 +0200612 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 Vlasenkoe7305052021-10-05 13:30:48 +0200619 if (r->infinity) /* If infinity, don't double */
620 return;
621
Denys Vlasenko12040122021-04-26 20:24:34 +0200622 /* T1 = Z * Z */
623 sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod);
624 /* Z = Y * Z */
625 sp_256_mont_mul_10(r->z, r->y, r->z, p256_mod, p256_mp_mod);
626 /* Z = 2Z */
627 sp_256_mont_dbl_10(r->z, r->z, p256_mod);
628 /* T2 = X - T1 */
629 sp_256_mont_sub_10(t2, r->x, t1, p256_mod);
630 /* T1 = X + T1 */
631 sp_256_mont_add_10(t1, r->x, t1, p256_mod);
632 /* T2 = T1 * T2 */
633 sp_256_mont_mul_10(t2, t1, t2, p256_mod, p256_mp_mod);
634 /* T1 = 3T2 */
635 sp_256_mont_tpl_10(t1, t2, p256_mod);
636 /* Y = 2Y */
637 sp_256_mont_dbl_10(r->y, r->y, p256_mod);
638 /* Y = Y * Y */
639 sp_256_mont_sqr_10(r->y, r->y, p256_mod, p256_mp_mod);
640 /* T2 = Y * Y */
641 sp_256_mont_sqr_10(t2, r->y, p256_mod, p256_mp_mod);
642 /* T2 = T2/2 */
643 sp_256_div2_10(t2, t2, p256_mod);
644 /* Y = Y * X */
645 sp_256_mont_mul_10(r->y, r->y, r->x, p256_mod, p256_mp_mod);
646 /* X = T1 * T1 */
647 sp_256_mont_mul_10(r->x, t1, t1, p256_mod, p256_mp_mod);
648 /* X = X - Y */
649 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
650 /* X = X - Y */
651 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
652 /* Y = Y - X */
653 sp_256_mont_sub_10(r->y, r->y, r->x, p256_mod);
654 /* Y = Y * T1 */
655 sp_256_mont_mul_10(r->y, r->y, t1, p256_mod, p256_mp_mod);
656 /* Y = Y - T2 */
657 sp_256_mont_sub_10(r->y, r->y, t2, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200658}
659
660/* Add two Montgomery form projective points.
661 *
662 * r Result of addition.
663 * p Frist point to add.
664 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200665 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200666static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200667{
Denys Vlasenko12040122021-04-26 20:24:34 +0200668 sp_digit t1[2*10];
669 sp_digit t2[2*10];
670 sp_digit t3[2*10];
671 sp_digit t4[2*10];
672 sp_digit t5[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200673
Denys Vlasenko12040122021-04-26 20:24:34 +0200674 /* Ensure only the first point is the same as the result. */
675 if (q == r) {
676 sp_point* a = p;
677 p = q;
678 q = a;
679 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200680
Denys Vlasenko12040122021-04-26 20:24:34 +0200681 /* Check double */
682 sp_256_sub_10(t1, p256_mod, q->y);
683 sp_256_norm_10(t1);
684 if (sp_256_cmp_equal_10(p->x, q->x)
685 && sp_256_cmp_equal_10(p->z, q->z)
686 && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1))
687 ) {
688 sp_256_proj_point_dbl_10(r, p);
689 }
690 else {
691 sp_point tp;
692 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200693
Denys Vlasenko12040122021-04-26 20:24:34 +0200694 v = r;
695 if (p->infinity | q->infinity) {
696 memset(&tp, 0, sizeof(tp));
697 v = &tp;
698 }
Denys Vlasenko772e1872021-04-26 17:25:27 +0200699
Denys Vlasenko12040122021-04-26 20:24:34 +0200700 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200701
Denys Vlasenko12040122021-04-26 20:24:34 +0200702 /* U1 = X1*Z2^2 */
703 sp_256_mont_sqr_10(t1, q->z, p256_mod, p256_mp_mod);
704 sp_256_mont_mul_10(t3, t1, q->z, p256_mod, p256_mp_mod);
705 sp_256_mont_mul_10(t1, t1, v->x, p256_mod, p256_mp_mod);
706 /* U2 = X2*Z1^2 */
707 sp_256_mont_sqr_10(t2, v->z, p256_mod, p256_mp_mod);
708 sp_256_mont_mul_10(t4, t2, v->z, p256_mod, p256_mp_mod);
709 sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod);
710 /* S1 = Y1*Z2^3 */
711 sp_256_mont_mul_10(t3, t3, v->y, p256_mod, p256_mp_mod);
712 /* S2 = Y2*Z1^3 */
713 sp_256_mont_mul_10(t4, t4, q->y, p256_mod, p256_mp_mod);
714 /* H = U2 - U1 */
715 sp_256_mont_sub_10(t2, t2, t1, p256_mod);
716 /* R = S2 - S1 */
717 sp_256_mont_sub_10(t4, t4, t3, p256_mod);
718 /* Z3 = H*Z1*Z2 */
719 sp_256_mont_mul_10(v->z, v->z, q->z, p256_mod, p256_mp_mod);
720 sp_256_mont_mul_10(v->z, v->z, t2, p256_mod, p256_mp_mod);
721 /* X3 = R^2 - H^3 - 2*U1*H^2 */
722 sp_256_mont_sqr_10(v->x, t4, p256_mod, p256_mp_mod);
723 sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod);
724 sp_256_mont_mul_10(v->y, t1, t5, p256_mod, p256_mp_mod);
725 sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod);
726 sp_256_mont_sub_10(v->x, v->x, t5, p256_mod);
727 sp_256_mont_dbl_10(t1, v->y, p256_mod);
728 sp_256_mont_sub_10(v->x, v->x, t1, p256_mod);
729 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
730 sp_256_mont_sub_10(v->y, v->y, v->x, p256_mod);
731 sp_256_mont_mul_10(v->y, v->y, t4, p256_mod, p256_mp_mod);
732 sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod);
733 sp_256_mont_sub_10(v->y, v->y, t5, p256_mod);
734 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200735}
736
737/* Multiply the point by the scalar and return the result.
738 * If map is true then convert result to affine co-ordinates.
739 *
740 * r Resulting point.
741 * g Point to multiply.
742 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200743 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200744 */
745static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/)
746{
Denys Vlasenko12040122021-04-26 20:24:34 +0200747 enum { map = 1 }; /* we always convert result to affine coordinates */
748 sp_point t[3];
749 sp_digit n;
750 int i;
751 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200752
Denys Vlasenko12040122021-04-26 20:24:34 +0200753 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200754
Denys Vlasenko12040122021-04-26 20:24:34 +0200755 /* t[0] = {0, 0, 1} * norm */
756 t[0].infinity = 1;
757 /* t[1] = {g->x, g->y, g->z} * norm */
758 sp_256_mod_mul_norm_10(t[1].x, g->x);
759 sp_256_mod_mul_norm_10(t[1].y, g->y);
760 sp_256_mod_mul_norm_10(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200761
Denys Vlasenko12040122021-04-26 20:24:34 +0200762 i = 9;
763 c = 22;
764 n = k[i--] << (26 - c);
765 for (; ; c--) {
766 if (c == 0) {
767 if (i == -1)
768 break;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200769
Denys Vlasenko12040122021-04-26 20:24:34 +0200770 n = k[i--];
771 c = 26;
772 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200773
Denys Vlasenko12040122021-04-26 20:24:34 +0200774 y = (n >> 25) & 1;
775 n <<= 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200776
Denys Vlasenko12040122021-04-26 20:24:34 +0200777 sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]);
778 memcpy(&t[2], &t[y], sizeof(sp_point));
779 sp_256_proj_point_dbl_10(&t[2], &t[2]);
780 memcpy(&t[y], &t[2], sizeof(sp_point));
781 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200782
Denys Vlasenko12040122021-04-26 20:24:34 +0200783 if (map)
784 sp_256_map_10(r, &t[0]);
785 else
786 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200787
Denys Vlasenko12040122021-04-26 20:24:34 +0200788 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200789}
790
791/* Multiply the base point of P256 by the scalar and return the result.
792 * If map is true then convert result to affine co-ordinates.
793 *
794 * r Resulting point.
795 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200796 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200797 */
798static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
799{
Denys Vlasenko39a3ef52021-04-27 01:31:51 +0200800 /* Since this function is called only once, save space:
801 * don't have "static const sp_point p256_base = {...}",
802 * it would have more zeros than data.
803 */
Denys Vlasenko48a18d12021-04-27 12:24:21 +0200804 static const uint8_t p256_base_bin[] = {
805 /* x (big-endian) */
806 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,
807 /* y */
808 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,
Denys Vlasenko646e8562021-04-27 13:09:44 +0200809 /* z will be set to 1, infinity flag to "false" */
Denys Vlasenko39a3ef52021-04-27 01:31:51 +0200810 };
811 sp_point p256_base;
812
Denys Vlasenko48a18d12021-04-27 12:24:21 +0200813 sp_256_point_from_bin2x32(&p256_base, p256_base_bin);
Denys Vlasenko39a3ef52021-04-27 01:31:51 +0200814
Denys Vlasenko12040122021-04-26 20:24:34 +0200815 sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200816}
817
818/* Multiply the point by the scalar and serialize the X ordinate.
819 * The number is 0 padded to maximum size on output.
820 *
821 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200822 * pub2x32 Point to multiply.
823 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200824 */
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200825static 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 +0200826{
Denys Vlasenko12040122021-04-26 20:24:34 +0200827 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200828
829#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +0200830 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200831#endif
Denys Vlasenko12040122021-04-26 20:24:34 +0200832 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
833 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200834
Denys Vlasenko12040122021-04-26 20:24:34 +0200835 sp_256_point_from_bin2x32(point, pub2x32);
836 dump_hex("point->x %s\n", point->x, sizeof(point->x));
837 dump_hex("point->y %s\n", point->y, sizeof(point->y));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200838
Denys Vlasenko12040122021-04-26 20:24:34 +0200839 sp_256_ecc_mulmod_10(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200840
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200841 sp_256_to_bin_10(point->x, out32);
Denys Vlasenko12040122021-04-26 20:24:34 +0200842 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200843}
844
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200845/* Generates a scalar that is in the range 1..order-1. */
846#define SIMPLIFY 1
847/* Add 1 to a. (a = a + 1) */
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200848static void sp_256_add_one_10(sp_digit* a)
849{
Denys Vlasenko12040122021-04-26 20:24:34 +0200850 a[0]++;
851 sp_256_norm_10(a);
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200852}
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200853static void sp_256_ecc_gen_k_10(sp_digit k[10])
854{
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200855#if !SIMPLIFY
856 /* The order of the curve P256 minus 2. */
857 static const sp_digit p256_order2[10] = {
858 0x063254f,0x272b0bf,0x1e84f3b,0x2b69c5e,0x3bce6fa,
859 0x3ffffff,0x3ffffff,0x00003ff,0x3ff0000,0x03fffff,
860 };
861#endif
862 uint8_t buf[32];
863
864 for (;;) {
865 tls_get_random(buf, sizeof(buf));
866#if FIXED_SECRET
867 memset(buf, 0x77, sizeof(buf));
868#endif
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200869 sp_256_from_bin_10(k, buf);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200870#if !SIMPLIFY
871 if (sp_256_cmp_10(k, p256_order2) < 0)
872 break;
873#else
874 /* non-loopy version (and not needing p256_order2[]):
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200875 * if most-significant word seems that k can be larger
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200876 * than p256_order2, fix it up:
877 */
878 if (k[9] >= 0x03fffff)
879 k[9] = 0x03ffffe;
880 break;
881#endif
882 }
883 sp_256_add_one_10(k);
884#undef SIMPLIFY
885}
886
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200887/* Makes a random EC key pair. */
888static void sp_ecc_make_key_256(sp_digit privkey[10], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200889{
890 sp_point point[1];
891
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200892 sp_256_ecc_gen_k_10(privkey);
893 sp_256_ecc_mulmod_base_10(point, privkey);
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200894 sp_256_to_bin_10(point->x, pubkey);
895 sp_256_to_bin_10(point->y, pubkey + 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200896
897 memset(point, 0, sizeof(point)); //paranoia
898}
899
900void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200901 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200902 const uint8_t *peerkey2x32)
903{
904 sp_digit privkey[10];
905
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200906 sp_ecc_make_key_256(privkey, pubkey2x32);
907 dump_hex("pubkey: %s\n", pubkey2x32, 32);
908 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200909
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200910 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200911 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
912 dump_hex("premaster: %s\n", premaster32, 32);
913}