blob: 97b2d3de911f4874734a167a57573f54f0d91131 [file] [log] [blame]
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001/*
2 * Copyright (C) 2021 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6#include "tls.h"
7
8#define SP_DEBUG 0
9#define FIXED_SECRET 0
10#define FIXED_PEER_PUBKEY 0
11
12#if SP_DEBUG
13# define dbg(...) fprintf(stderr, __VA_ARGS__)
14static void dump_hex(const char *fmt, const void *vp, int len)
15{
16 char hexbuf[32 * 1024 + 4];
17 const uint8_t *p = vp;
18
19 bin2hex(hexbuf, (void*)p, len)[0] = '\0';
20 dbg(fmt, hexbuf);
21}
22#else
23# define dbg(...) ((void)0)
24# define dump_hex(...) ((void)0)
25#endif
26
27#undef DIGIT_BIT
28#define DIGIT_BIT 32
29typedef int32_t sp_digit;
30
31/* The code below is taken from parts of
32 * wolfssl-3.15.3/wolfcrypt/src/sp_c32.c
33 * and heavily modified.
34 * Header comment is kept intact:
35 */
36
37/* sp.c
38 *
39 * Copyright (C) 2006-2018 wolfSSL Inc.
40 *
41 * This file is part of wolfSSL.
42 *
43 * wolfSSL is free software; you can redistribute it and/or modify
44 * it under the terms of the GNU General Public License as published by
45 * the Free Software Foundation; either version 2 of the License, or
46 * (at your option) any later version.
47 *
48 * wolfSSL is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, write to the Free Software
55 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
56 */
57
58/* Implementation by Sean Parkinson. */
59
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020060typedef struct sp_point {
61 sp_digit x[2 * 10];
62 sp_digit y[2 * 10];
63 sp_digit z[2 * 10];
64 int infinity;
65} sp_point;
66
67/* The modulus (prime) of the curve P256. */
68static const sp_digit p256_mod[10] = {
69 0x3ffffff,0x3ffffff,0x3ffffff,0x003ffff,0x0000000,
70 0x0000000,0x0000000,0x0000400,0x3ff0000,0x03fffff,
71};
72
73#define p256_mp_mod ((sp_digit)0x000001)
74
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020075/* The base point of curve P256. */
76static const sp_point p256_base = {
77 /* X ordinate */
78 { 0x098c296,0x04e5176,0x33a0f4a,0x204b7ac,0x277037d,0x0e9103c,0x3ce6e56,0x1091fe2,0x1f2e12c,0x01ac5f4 },
79 /* Y ordinate */
80 { 0x3bf51f5,0x1901a0d,0x1ececbb,0x15dacc5,0x22bce33,0x303e785,0x27eb4a7,0x1fe6e3b,0x2e2fe1a,0x013f8d0 },
81 /* Z ordinate */
82 { 0x0000001,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000,0x0000000 },
83 /* infinity */
84 0
85};
86
87/* Write r as big endian to byte aray.
88 * Fixed length number of bytes written: 32
89 *
90 * r A single precision integer.
91 * a Byte array.
92 */
93static void sp_256_to_bin(sp_digit* r, uint8_t* a)
94{
95 int i, j, s = 0, b;
96
97 for (i = 0; i < 9; i++) {
98 r[i+1] += r[i] >> 26;
99 r[i] &= 0x3ffffff;
100 }
101 j = 256 / 8 - 1;
102 a[j] = 0;
103 for (i=0; i<10 && j>=0; i++) {
104 b = 0;
105 a[j--] |= r[i] << s; b += 8 - s;
106 if (j < 0)
107 break;
108 while (b < 26) {
109 a[j--] = r[i] >> b; b += 8;
110 if (j < 0)
111 break;
112 }
113 s = 8 - (b - 26);
114 if (j >= 0)
115 a[j] = 0;
116 if (s != 0)
117 j++;
118 }
119}
120
121/* Read big endian unsigned byte aray into r.
122 *
123 * r A single precision integer.
124 * a Byte array.
125 * n Number of bytes in array to read.
126 */
127static void sp_256_from_bin(sp_digit* r, int max, const uint8_t* a, int n)
128{
129 int i, j = 0, s = 0;
130
131 r[0] = 0;
132 for (i = n-1; i >= 0; i--) {
133 r[j] |= ((sp_digit)a[i]) << s;
134 if (s >= 18) {
135 r[j] &= 0x3ffffff;
136 s = 26 - s;
137 if (j + 1 >= max)
138 break;
139 r[++j] = a[i] >> s;
140 s = 8 - s;
141 }
142 else
143 s += 8;
144 }
145
146 for (j++; j < max; j++)
147 r[j] = 0;
148}
149
150/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
151static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
152{
153 memset(p, 0, sizeof(*p));
154 /*p->infinity = 0;*/
155 sp_256_from_bin(p->x, 2 * 10, bin2x32, 32);
156 sp_256_from_bin(p->y, 2 * 10, bin2x32 + 32, 32);
157 //static const uint8_t one[1] = { 1 };
158 //sp_256_from_bin(p->z, 2 * 10, one, 1);
159 p->z[0] = 1;
160}
161
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 Vlasenkob3b17132021-04-26 16:53:53 +0200169 sp_digit r;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200170 int i;
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200171 for (i = 9; i >= 0; i--) {
172 r = a[i] - b[i];
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200173 if (r != 0)
174 break;
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200175 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200176 return r;
177}
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 Vlasenkof18a1fd2021-04-26 13:25:56 +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{
191 int i;
192 for (i = 0; i < 9; i++) {
193 a[i+1] += a[i] >> 26;
194 a[i] &= 0x3ffffff;
195 }
196}
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{
201 int i;
202 for (i = 0; i < 10; i++)
203 r[i] = a[i] + b[i];
204}
205
206/* Conditionally add a and b using the mask m.
207 * m is -1 to add and 0 when not.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200208 */
209static void sp_256_cond_add_10(sp_digit* r, const sp_digit* a,
210 const sp_digit* b, const sp_digit m)
211{
212 int i;
213 for (i = 0; i < 10; i++)
214 r[i] = a[i] + (b[i] & m);
215}
216
217/* Conditionally subtract b from a using the mask m.
218 * m is -1 to subtract and 0 when not.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200219 */
220static void sp_256_cond_sub_10(sp_digit* r, const sp_digit* a,
221 const sp_digit* b, const sp_digit m)
222{
223 int i;
224 for (i = 0; i < 10; i++)
225 r[i] = a[i] - (b[i] & m);
226}
227
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200228/* Shift number left one bit. Bottom bit is lost. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200229static void sp_256_rshift1_10(sp_digit* r, sp_digit* a)
230{
231 int i;
232 for (i = 0; i < 9; i++)
233 r[i] = ((a[i] >> 1) | (a[i + 1] << 25)) & 0x3ffffff;
234 r[9] = a[9] >> 1;
235}
236
237/* Multiply a number by Montogmery normalizer mod modulus (prime).
238 *
239 * r The resulting Montgomery form number.
240 * a The number to convert.
241 */
242static void sp_256_mod_mul_norm_10(sp_digit* r, const sp_digit* a)
243{
244 int64_t t[8];
245 int64_t a32[8];
246 int64_t o;
247
248 a32[0] = a[0];
249 a32[0] |= a[1] << 26;
250 a32[0] &= 0xffffffff;
251 a32[1] = (sp_digit)(a[1] >> 6);
252 a32[1] |= a[2] << 20;
253 a32[1] &= 0xffffffff;
254 a32[2] = (sp_digit)(a[2] >> 12);
255 a32[2] |= a[3] << 14;
256 a32[2] &= 0xffffffff;
257 a32[3] = (sp_digit)(a[3] >> 18);
258 a32[3] |= a[4] << 8;
259 a32[3] &= 0xffffffff;
260 a32[4] = (sp_digit)(a[4] >> 24);
261 a32[4] |= a[5] << 2;
262 a32[4] |= a[6] << 28;
263 a32[4] &= 0xffffffff;
264 a32[5] = (sp_digit)(a[6] >> 4);
265 a32[5] |= a[7] << 22;
266 a32[5] &= 0xffffffff;
267 a32[6] = (sp_digit)(a[7] >> 10);
268 a32[6] |= a[8] << 16;
269 a32[6] &= 0xffffffff;
270 a32[7] = (sp_digit)(a[8] >> 16);
271 a32[7] |= a[9] << 10;
272 a32[7] &= 0xffffffff;
273
274 /* 1 1 0 -1 -1 -1 -1 0 */
275 t[0] = 0 + a32[0] + a32[1] - a32[3] - a32[4] - a32[5] - a32[6];
276 /* 0 1 1 0 -1 -1 -1 -1 */
277 t[1] = 0 + a32[1] + a32[2] - a32[4] - a32[5] - a32[6] - a32[7];
278 /* 0 0 1 1 0 -1 -1 -1 */
279 t[2] = 0 + a32[2] + a32[3] - a32[5] - a32[6] - a32[7];
280 /* -1 -1 0 2 2 1 0 -1 */
281 t[3] = 0 - a32[0] - a32[1] + 2 * a32[3] + 2 * a32[4] + a32[5] - a32[7];
282 /* 0 -1 -1 0 2 2 1 0 */
283 t[4] = 0 - a32[1] - a32[2] + 2 * a32[4] + 2 * a32[5] + a32[6];
284 /* 0 0 -1 -1 0 2 2 1 */
285 t[5] = 0 - a32[2] - a32[3] + 2 * a32[5] + 2 * a32[6] + a32[7];
286 /* -1 -1 0 0 0 1 3 2 */
287 t[6] = 0 - a32[0] - a32[1] + a32[5] + 3 * a32[6] + 2 * a32[7];
288 /* 1 0 -1 -1 -1 -1 0 3 */
289 t[7] = 0 + a32[0] - a32[2] - a32[3] - a32[4] - a32[5] + 3 * a32[7];
290
291 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
292 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
293 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
294 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
295 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
296 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
297 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
298 o = t[7] >> 32; t[7] &= 0xffffffff;
299 t[0] += o;
300 t[3] -= o;
301 t[6] -= o;
302 t[7] += o;
303 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
304 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
305 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
306 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
307 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
308 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
309 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
310
311 r[0] = (sp_digit)(t[0]) & 0x3ffffff;
312 r[1] = (sp_digit)(t[0] >> 26);
313 r[1] |= t[1] << 6;
314 r[1] &= 0x3ffffff;
315 r[2] = (sp_digit)(t[1] >> 20);
316 r[2] |= t[2] << 12;
317 r[2] &= 0x3ffffff;
318 r[3] = (sp_digit)(t[2] >> 14);
319 r[3] |= t[3] << 18;
320 r[3] &= 0x3ffffff;
321 r[4] = (sp_digit)(t[3] >> 8);
322 r[4] |= t[4] << 24;
323 r[4] &= 0x3ffffff;
324 r[5] = (sp_digit)(t[4] >> 2) & 0x3ffffff;
325 r[6] = (sp_digit)(t[4] >> 28);
326 r[6] |= t[5] << 4;
327 r[6] &= 0x3ffffff;
328 r[7] = (sp_digit)(t[5] >> 22);
329 r[7] |= t[6] << 10;
330 r[7] &= 0x3ffffff;
331 r[8] = (sp_digit)(t[6] >> 16);
332 r[8] |= t[7] << 16;
333 r[8] &= 0x3ffffff;
334 r[9] = (sp_digit)(t[7] >> 10);
335}
336
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200337/* Mul a by scalar b and add into r. (r += a * b) */
338static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200339{
340 int64_t tb = b;
341 int64_t t = 0;
342 int i;
343
344 for (i = 0; i < 10; i++) {
345 t += (tb * a[i]) + r[i];
346 r[i] = t & 0x3ffffff;
347 t >>= 26;
348 }
349 r[10] += t;
350}
351
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200352/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200353static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
354{
355 sp_256_cond_add_10(r, a, m, 0 - (a[0] & 1));
356 sp_256_norm_10(r);
357 sp_256_rshift1_10(r, r);
358}
359
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200360/* Shift the result in the high 256 bits down to the bottom. */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200361static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a)
362{
363 int i;
364 sp_digit n, s;
365
366 s = a[10];
367 n = a[9] >> 22;
368 for (i = 0; i < 9; i++) {
369 n += (s & 0x3ffffff) << 4;
370 r[i] = n & 0x3ffffff;
371 n >>= 26;
372 s = a[11 + i] + (s >> 26);
373 }
374 n += s << 4;
375 r[9] = n;
376 memset(&r[10], 0, sizeof(*r) * 10);
377}
378
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200379/* Add two Montgomery form numbers (r = a + b % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200380static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
381 const sp_digit* m)
382{
383 sp_256_add_10(r, a, b);
384 sp_256_norm_10(r);
385 sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
386 sp_256_norm_10(r);
387}
388
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200389/* Double a Montgomery form number (r = a + a % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200390static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
391{
392 sp_256_add_10(r, a, a);
393 sp_256_norm_10(r);
394 sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
395 sp_256_norm_10(r);
396}
397
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200398/* Triple a Montgomery form number (r = a + a + a % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200399static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m)
400{
401 sp_256_add_10(r, a, a);
402 sp_256_norm_10(r);
403 sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
404 sp_256_norm_10(r);
405 sp_256_add_10(r, r, a);
406 sp_256_norm_10(r);
407 sp_256_cond_sub_10(r, r, m, 0 - ((r[9] >> 22) > 0));
408 sp_256_norm_10(r);
409}
410
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200411/* Sub b from a into r. (r = a - b) */
412static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200413{
414 int i;
415 for (i = 0; i < 10; i++)
416 r[i] = a[i] - b[i];
417}
418
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200419/* Subtract two Montgomery form numbers (r = a - b % m) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200420static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
421 const sp_digit* m)
422{
423 sp_256_sub_10(r, a, b);
424 sp_256_cond_add_10(r, r, m, r[9] >> 22);
425 sp_256_norm_10(r);
426}
427
428/* Reduce the number back to 256 bits using Montgomery reduction.
429 *
430 * a A single precision number to reduce in place.
431 * m The single precision number representing the modulus.
432 * mp The digit representing the negative inverse of m mod 2^n.
433 */
434static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp)
435{
436 int i;
437 sp_digit mu;
438
439 if (mp != 1) {
440 for (i = 0; i < 9; i++) {
441 mu = (a[i] * mp) & 0x3ffffff;
442 sp_256_mul_add_10(a+i, m, mu);
443 a[i+1] += a[i] >> 26;
444 }
445 mu = (a[i] * mp) & 0x3fffffl;
446 sp_256_mul_add_10(a+i, m, mu);
447 a[i+1] += a[i] >> 26;
448 a[i] &= 0x3ffffff;
449 }
450 else {
451 for (i = 0; i < 9; i++) {
452 mu = a[i] & 0x3ffffff;
453 sp_256_mul_add_10(a+i, p256_mod, mu);
454 a[i+1] += a[i] >> 26;
455 }
456 mu = a[i] & 0x3fffffl;
457 sp_256_mul_add_10(a+i, p256_mod, mu);
458 a[i+1] += a[i] >> 26;
459 a[i] &= 0x3ffffff;
460 }
461
462 sp_256_mont_shift_10(a, a);
463 sp_256_cond_sub_10(a, a, m, 0 - ((a[9] >> 22) > 0));
464 sp_256_norm_10(a);
465}
466
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200467/* Multiply a and b into r. (r = a * b) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200468static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b)
469{
470 int i, j, k;
471 int64_t c;
472
473 c = ((int64_t)a[9]) * b[9];
474 r[19] = (sp_digit)(c >> 26);
475 c = (c & 0x3ffffff) << 26;
476 for (k = 17; k >= 0; k--) {
477 for (i = 9; i >= 0; i--) {
478 j = k - i;
479 if (j >= 10)
480 break;
481 if (j < 0)
482 continue;
483 c += ((int64_t)a[i]) * b[j];
484 }
485 r[k + 2] += c >> 52;
486 r[k + 1] = (c >> 26) & 0x3ffffff;
487 c = (c & 0x3ffffff) << 26;
488 }
489 r[0] = (sp_digit)(c >> 26);
490}
491
492/* Multiply two Montogmery form numbers mod the modulus (prime).
493 * (r = a * b mod m)
494 *
495 * r Result of multiplication.
496 * a First number to multiply in Montogmery form.
497 * b Second number to multiply in Montogmery form.
498 * m Modulus (prime).
499 * mp Montogmery mulitplier.
500 */
501static void sp_256_mont_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b,
502 const sp_digit* m, sp_digit mp)
503{
504 sp_256_mul_10(r, a, b);
505 sp_256_mont_reduce_10(r, m, mp);
506}
507
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200508/* Square a and put result in r. (r = a * a) */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200509static void sp_256_sqr_10(sp_digit* r, const sp_digit* a)
510{
511 int i, j, k;
512 int64_t c;
513
514 c = ((int64_t)a[9]) * a[9];
515 r[19] = (sp_digit)(c >> 26);
516 c = (c & 0x3ffffff) << 26;
517 for (k = 17; k >= 0; k--) {
518 for (i = 9; i >= 0; i--) {
519 j = k - i;
520 if (j >= 10 || i <= j)
521 break;
522 if (j < 0)
523 continue;
524
525 c += ((int64_t)a[i]) * a[j] * 2;
526 }
527 if (i == j)
528 c += ((int64_t)a[i]) * a[i];
529
530 r[k + 2] += c >> 52;
531 r[k + 1] = (c >> 26) & 0x3ffffff;
532 c = (c & 0x3ffffff) << 26;
533 }
534 r[0] = (sp_digit)(c >> 26);
535}
536
537/* Square the Montgomery form number. (r = a * a mod m)
538 *
539 * r Result of squaring.
540 * a Number to square in Montogmery form.
541 * m Modulus (prime).
542 * mp Montogmery mulitplier.
543 */
544static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m,
545 sp_digit mp)
546{
547 sp_256_sqr_10(r, a);
548 sp_256_mont_reduce_10(r, m, mp);
549}
550
551/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
552 * P256 curve. (r = 1 / a mod m)
553 *
554 * r Inverse result.
555 * a Number to invert.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200556 */
Denys Vlasenko93b886f2021-04-26 18:05:53 +0200557#if 0
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200558/* Mod-2 for the P256 curve. */
559static const uint32_t p256_mod_2[8] = {
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200560 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
561 0x00000000,0x00000000,0x00000001,0xffffffff,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200562};
Denys Vlasenko93b886f2021-04-26 18:05:53 +0200563//Bit pattern:
564//2 2 2 2 2 2 2 1...1
565//5 5 4 3 2 1 0 9...0 9...1
566//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
567//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
568#endif
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200569static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200570{
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200571 sp_digit t[2*10]; //can be just [10]?
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200572 int i;
573
574 memcpy(t, a, sizeof(sp_digit) * 10);
575 for (i = 254; i >= 0; i--) {
576 sp_256_mont_sqr_10(t, t, p256_mod, p256_mp_mod);
Denys Vlasenko93b886f2021-04-26 18:05:53 +0200577 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
578 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200579 sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod);
580 }
581 memcpy(r, t, sizeof(sp_digit) * 10);
582}
583
584/* Map the Montgomery form projective co-ordinate point to an affine point.
585 *
586 * r Resulting affine co-ordinate point.
587 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200588 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200589static void sp_256_map_10(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200590{
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200591 sp_digit t1[2*10];
592 sp_digit t2[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200593 int32_t n;
594
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200595 sp_256_mont_inv_10(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200596
597 sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod);
598 sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod);
599
600 /* x /= z^2 */
601 sp_256_mont_mul_10(r->x, p->x, t2, p256_mod, p256_mp_mod);
602 memset(r->x + 10, 0, sizeof(r->x) / 2);
603 sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod);
604 /* Reduce x to less than modulus */
605 n = sp_256_cmp_10(r->x, p256_mod);
606 sp_256_cond_sub_10(r->x, r->x, p256_mod, 0 - (n >= 0));
607 sp_256_norm_10(r->x);
608
609 /* y /= z^3 */
610 sp_256_mont_mul_10(r->y, p->y, t1, p256_mod, p256_mp_mod);
611 memset(r->y + 10, 0, sizeof(r->y) / 2);
612 sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod);
613 /* Reduce y to less than modulus */
614 n = sp_256_cmp_10(r->y, p256_mod);
615 sp_256_cond_sub_10(r->y, r->y, p256_mod, 0 - (n >= 0));
616 sp_256_norm_10(r->y);
617
618 memset(r->z, 0, sizeof(r->z));
619 r->z[0] = 1;
620}
621
622/* Double the Montgomery form projective point p.
623 *
624 * r Result of doubling point.
625 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200626 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200627static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200628{
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200629 sp_point tp;
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200630 sp_digit t1[2*10];
631 sp_digit t2[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200632
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200633 /* Put point to double into result */
634 if (r != p)
635 *r = *p; /* struct copy */
636
637 if (r->infinity) {
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200638 /* If infinity, don't double (work on dummy value) */
639 r = &tp;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200640 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200641 /* T1 = Z * Z */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200642 sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200643 /* Z = Y * Z */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200644 sp_256_mont_mul_10(r->z, r->y, r->z, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200645 /* Z = 2Z */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200646 sp_256_mont_dbl_10(r->z, r->z, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200647 /* T2 = X - T1 */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200648 sp_256_mont_sub_10(t2, r->x, t1, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200649 /* T1 = X + T1 */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200650 sp_256_mont_add_10(t1, r->x, t1, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200651 /* T2 = T1 * T2 */
652 sp_256_mont_mul_10(t2, t1, t2, p256_mod, p256_mp_mod);
653 /* T1 = 3T2 */
654 sp_256_mont_tpl_10(t1, t2, p256_mod);
655 /* Y = 2Y */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200656 sp_256_mont_dbl_10(r->y, r->y, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200657 /* Y = Y * Y */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200658 sp_256_mont_sqr_10(r->y, r->y, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200659 /* T2 = Y * Y */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200660 sp_256_mont_sqr_10(t2, r->y, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200661 /* T2 = T2/2 */
662 sp_256_div2_10(t2, t2, p256_mod);
663 /* Y = Y * X */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200664 sp_256_mont_mul_10(r->y, r->y, r->x, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200665 /* X = T1 * T1 */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200666 sp_256_mont_mul_10(r->x, t1, t1, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200667 /* X = X - Y */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200668 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200669 /* X = X - Y */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200670 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200671 /* Y = Y - X */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200672 sp_256_mont_sub_10(r->y, r->y, r->x, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200673 /* Y = Y * T1 */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200674 sp_256_mont_mul_10(r->y, r->y, t1, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200675 /* Y = Y - T2 */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200676 sp_256_mont_sub_10(r->y, r->y, t2, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200677}
678
679/* Add two Montgomery form projective points.
680 *
681 * r Result of addition.
682 * p Frist point to add.
683 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200684 */
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200685static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200686{
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200687 sp_digit t1[2*10];
688 sp_digit t2[2*10];
689 sp_digit t3[2*10];
690 sp_digit t4[2*10];
691 sp_digit t5[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200692
693 /* Ensure only the first point is the same as the result. */
694 if (q == r) {
695 sp_point* a = p;
696 p = q;
697 q = a;
698 }
699
700 /* Check double */
701 sp_256_sub_10(t1, p256_mod, q->y);
702 sp_256_norm_10(t1);
703 if (sp_256_cmp_equal_10(p->x, q->x)
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200704 && sp_256_cmp_equal_10(p->z, q->z)
705 && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1))
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200706 ) {
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200707 sp_256_proj_point_dbl_10(r, p);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200708 }
709 else {
Denys Vlasenko772e1872021-04-26 17:25:27 +0200710 sp_point tp;
711 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200712
Denys Vlasenko772e1872021-04-26 17:25:27 +0200713 v = r;
714 if (p->infinity | q->infinity) {
715 memset(&tp, 0, sizeof(tp));
716 v = &tp;
717 }
718
719 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200720
721 /* U1 = X1*Z2^2 */
722 sp_256_mont_sqr_10(t1, q->z, p256_mod, p256_mp_mod);
723 sp_256_mont_mul_10(t3, t1, q->z, p256_mod, p256_mp_mod);
Denys Vlasenko772e1872021-04-26 17:25:27 +0200724 sp_256_mont_mul_10(t1, t1, v->x, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200725 /* U2 = X2*Z1^2 */
Denys Vlasenko772e1872021-04-26 17:25:27 +0200726 sp_256_mont_sqr_10(t2, v->z, p256_mod, p256_mp_mod);
727 sp_256_mont_mul_10(t4, t2, v->z, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200728 sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod);
729 /* S1 = Y1*Z2^3 */
Denys Vlasenko772e1872021-04-26 17:25:27 +0200730 sp_256_mont_mul_10(t3, t3, v->y, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200731 /* S2 = Y2*Z1^3 */
732 sp_256_mont_mul_10(t4, t4, q->y, p256_mod, p256_mp_mod);
733 /* H = U2 - U1 */
734 sp_256_mont_sub_10(t2, t2, t1, p256_mod);
735 /* R = S2 - S1 */
736 sp_256_mont_sub_10(t4, t4, t3, p256_mod);
737 /* Z3 = H*Z1*Z2 */
Denys Vlasenko772e1872021-04-26 17:25:27 +0200738 sp_256_mont_mul_10(v->z, v->z, q->z, p256_mod, p256_mp_mod);
739 sp_256_mont_mul_10(v->z, v->z, t2, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200740 /* X3 = R^2 - H^3 - 2*U1*H^2 */
Denys Vlasenko772e1872021-04-26 17:25:27 +0200741 sp_256_mont_sqr_10(v->x, t4, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200742 sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod);
Denys Vlasenko772e1872021-04-26 17:25:27 +0200743 sp_256_mont_mul_10(v->y, t1, t5, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200744 sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod);
Denys Vlasenko772e1872021-04-26 17:25:27 +0200745 sp_256_mont_sub_10(v->x, v->x, t5, p256_mod);
746 sp_256_mont_dbl_10(t1, v->y, p256_mod);
747 sp_256_mont_sub_10(v->x, v->x, t1, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200748 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
Denys Vlasenko772e1872021-04-26 17:25:27 +0200749 sp_256_mont_sub_10(v->y, v->y, v->x, p256_mod);
750 sp_256_mont_mul_10(v->y, v->y, t4, p256_mod, p256_mp_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200751 sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod);
Denys Vlasenko772e1872021-04-26 17:25:27 +0200752 sp_256_mont_sub_10(v->y, v->y, t5, p256_mod);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200753 }
754}
755
756/* Multiply the point by the scalar and return the result.
757 * If map is true then convert result to affine co-ordinates.
758 *
759 * r Resulting point.
760 * g Point to multiply.
761 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200762 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200763 */
764static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/)
765{
766 enum { map = 1 }; /* we always convert result to affine coordinates */
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200767 sp_point t[3];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200768 sp_digit n;
769 int i;
770 int c, y;
771
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200772 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200773
774 /* t[0] = {0, 0, 1} * norm */
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200775 t[0].infinity = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200776 /* t[1] = {g->x, g->y, g->z} * norm */
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200777 sp_256_mod_mul_norm_10(t[1].x, g->x);
778 sp_256_mod_mul_norm_10(t[1].y, g->y);
779 sp_256_mod_mul_norm_10(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200780
781 i = 9;
782 c = 22;
783 n = k[i--] << (26 - c);
784 for (; ; c--) {
785 if (c == 0) {
786 if (i == -1)
787 break;
788
789 n = k[i--];
790 c = 26;
791 }
792
793 y = (n >> 25) & 1;
794 n <<= 1;
795
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200796 sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]);
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200797 memcpy(&t[2], &t[y], sizeof(sp_point));
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200798 sp_256_proj_point_dbl_10(&t[2], &t[2]);
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200799 memcpy(&t[y], &t[2], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200800 }
801
802 if (map)
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200803 sp_256_map_10(r, &t[0]);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200804 else
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200805 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200806
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200807 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200808}
809
810/* Multiply the base point of P256 by the scalar and return the result.
811 * If map is true then convert result to affine co-ordinates.
812 *
813 * r Resulting point.
814 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +0200815 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200816 */
817static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/)
818{
Denys Vlasenko6381f3d2021-04-26 17:41:43 +0200819 sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200820}
821
822/* Multiply the point by the scalar and serialize the X ordinate.
823 * The number is 0 padded to maximum size on output.
824 *
825 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200826 * pub2x32 Point to multiply.
827 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200828 */
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200829static void sp_ecc_secret_gen_256(sp_digit priv[10], const uint8_t *pub2x32, uint8_t* out32)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200830{
831 sp_point point[1];
832
833#if FIXED_PEER_PUBKEY
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200834 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200835#endif
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200836 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
837 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200838
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200839 sp_256_point_from_bin2x32(point, pub2x32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200840 dump_hex("point->x %s\n", point->x, sizeof(point->x));
841 dump_hex("point->y %s\n", point->y, sizeof(point->y));
842
843 sp_256_ecc_mulmod_10(point, point, priv);
844
845 sp_256_to_bin(point->x, out32);
846 dump_hex("out32: %s\n", out32, 32);
847}
848
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200849/* Generates a scalar that is in the range 1..order-1. */
850#define SIMPLIFY 1
851/* Add 1 to a. (a = a + 1) */
852#if !SIMPLIFY
853static void sp_256_add_one_10(sp_digit* a)
854{
855 a[0]++;
856 sp_256_norm_10(a);
857}
858#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200859static void sp_256_ecc_gen_k_10(sp_digit k[10])
860{
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200861#if !SIMPLIFY
862 /* The order of the curve P256 minus 2. */
863 static const sp_digit p256_order2[10] = {
864 0x063254f,0x272b0bf,0x1e84f3b,0x2b69c5e,0x3bce6fa,
865 0x3ffffff,0x3ffffff,0x00003ff,0x3ff0000,0x03fffff,
866 };
867#endif
868 uint8_t buf[32];
869
870 for (;;) {
871 tls_get_random(buf, sizeof(buf));
872#if FIXED_SECRET
873 memset(buf, 0x77, sizeof(buf));
874#endif
875 sp_256_from_bin(k, 10, buf, sizeof(buf));
876#if !SIMPLIFY
877 if (sp_256_cmp_10(k, p256_order2) < 0)
878 break;
879#else
880 /* non-loopy version (and not needing p256_order2[]):
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200881 * if most-significant word seems that k can be larger
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200882 * than p256_order2, fix it up:
883 */
884 if (k[9] >= 0x03fffff)
885 k[9] = 0x03ffffe;
886 break;
887#endif
888 }
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200889#if !SIMPLIFY
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200890 sp_256_add_one_10(k);
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200891#else
892 if (k[0] == 0)
893 k[0] = 1;
894#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200895#undef SIMPLIFY
896}
897
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200898/* Makes a random EC key pair. */
899static void sp_ecc_make_key_256(sp_digit privkey[10], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200900{
901 sp_point point[1];
902
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200903 sp_256_ecc_gen_k_10(privkey);
904 sp_256_ecc_mulmod_base_10(point, privkey);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200905 sp_256_to_bin(point->x, pubkey);
906 sp_256_to_bin(point->y, pubkey + 32);
907
908 memset(point, 0, sizeof(point)); //paranoia
909}
910
911void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200912 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200913 const uint8_t *peerkey2x32)
914{
915 sp_digit privkey[10];
916
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200917 sp_ecc_make_key_256(privkey, pubkey2x32);
918 dump_hex("pubkey: %s\n", pubkey2x32, 32);
919 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200920
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200921 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200922 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
923 dump_hex("premaster: %s\n", premaster32, 32);
924}