blob: 8527e78648e621e08a036167d0b3a45b20f8f9d9 [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
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,
Denys Vlasenko12040122021-04-26 20:24:34 +0200210 const sp_digit* b, const sp_digit m)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200211{
Denys Vlasenko12040122021-04-26 20:24:34 +0200212 int i;
213 for (i = 0; i < 10; i++)
214 r[i] = a[i] + (b[i] & m);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200215}
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,
Denys Vlasenko12040122021-04-26 20:24:34 +0200221 const sp_digit* b, const sp_digit m)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200222{
Denys Vlasenko12040122021-04-26 20:24:34 +0200223 int i;
224 for (i = 0; i < 10; i++)
225 r[i] = a[i] - (b[i] & m);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200226}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200231 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;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200235}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200244 int64_t t[8];
245 int64_t a32[8];
246 int64_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200247
Denys Vlasenko12040122021-04-26 20:24:34 +0200248 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;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200273
Denys Vlasenko12040122021-04-26 20:24:34 +0200274 /* 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];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200290
Denys Vlasenko12040122021-04-26 20:24:34 +0200291 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;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200310
Denys Vlasenko12040122021-04-26 20:24:34 +0200311 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200335}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200340 int64_t tb = b;
341 int64_t t = 0;
342 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200343
Denys Vlasenko12040122021-04-26 20:24:34 +0200344 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;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200350}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200355 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200358}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200363 int i;
364 sp_digit n, s;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200365
Denys Vlasenko12040122021-04-26 20:24:34 +0200366 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200377}
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,
Denys Vlasenko12040122021-04-26 20:24:34 +0200381 const sp_digit* m)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200382{
Denys Vlasenko12040122021-04-26 20:24:34 +0200383 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200387}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200392 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200396}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200401 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200409}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200414 int i;
415 for (i = 0; i < 10; i++)
416 r[i] = a[i] - b[i];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200417}
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,
Denys Vlasenko12040122021-04-26 20:24:34 +0200421 const sp_digit* m)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200422{
Denys Vlasenko12040122021-04-26 20:24:34 +0200423 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200426}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200436 int i;
437 sp_digit mu;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200438
Denys Vlasenko12040122021-04-26 20:24:34 +0200439 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 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200461
Denys Vlasenko12040122021-04-26 20:24:34 +0200462 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200465}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200470 int i, j, k;
471 int64_t c;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200472
Denys Vlasenko12040122021-04-26 20:24:34 +0200473 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200490}
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,
Denys Vlasenko12040122021-04-26 20:24:34 +0200502 const sp_digit* m, sp_digit mp)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200503{
Denys Vlasenko12040122021-04-26 20:24:34 +0200504 sp_256_mul_10(r, a, b);
505 sp_256_mont_reduce_10(r, m, mp);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200506}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200511 int i, j, k;
512 int64_t c;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200513
Denys Vlasenko12040122021-04-26 20:24:34 +0200514 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;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200524
Denys Vlasenko12040122021-04-26 20:24:34 +0200525 c += ((int64_t)a[i]) * a[j] * 2;
526 }
527 if (i == j)
528 c += ((int64_t)a[i]) * a[i];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200529
Denys Vlasenko12040122021-04-26 20:24:34 +0200530 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200535}
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,
Denys Vlasenko12040122021-04-26 20:24:34 +0200545 sp_digit mp)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200546{
Denys Vlasenko12040122021-04-26 20:24:34 +0200547 sp_256_sqr_10(r, a);
548 sp_256_mont_reduce_10(r, m, mp);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200549}
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 Vlasenko12040122021-04-26 20:24:34 +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 Vlasenko12040122021-04-26 20:24:34 +0200571 sp_digit t[2*10]; //can be just [10]?
572 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200573
Denys Vlasenko12040122021-04-26 20:24:34 +0200574 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);
577 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
578 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
579 sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod);
580 }
581 memcpy(r, t, sizeof(sp_digit) * 10);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200582}
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 Vlasenko12040122021-04-26 20:24:34 +0200591 sp_digit t1[2*10];
592 sp_digit t2[2*10];
593 int32_t n;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200594
Denys Vlasenko12040122021-04-26 20:24:34 +0200595 sp_256_mont_inv_10(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200596
Denys Vlasenko12040122021-04-26 20:24:34 +0200597 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200599
Denys Vlasenko12040122021-04-26 20:24:34 +0200600 /* 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200608
Denys Vlasenko12040122021-04-26 20:24:34 +0200609 /* 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);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200617
Denys Vlasenko12040122021-04-26 20:24:34 +0200618 memset(r->z, 0, sizeof(r->z));
619 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200620}
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 Vlasenko12040122021-04-26 20:24:34 +0200629 sp_point tp;
630 sp_digit t1[2*10];
631 sp_digit t2[2*10];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200632
Denys Vlasenko12040122021-04-26 20:24:34 +0200633 /* Put point to double into result */
634 if (r != p)
635 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +0200636
Denys Vlasenko12040122021-04-26 20:24:34 +0200637 if (r->infinity) {
638 /* If infinity, don't double (work on dummy value) */
639 r = &tp;
640 }
641 /* T1 = Z * Z */
642 sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod);
643 /* Z = Y * Z */
644 sp_256_mont_mul_10(r->z, r->y, r->z, p256_mod, p256_mp_mod);
645 /* Z = 2Z */
646 sp_256_mont_dbl_10(r->z, r->z, p256_mod);
647 /* T2 = X - T1 */
648 sp_256_mont_sub_10(t2, r->x, t1, p256_mod);
649 /* T1 = X + T1 */
650 sp_256_mont_add_10(t1, r->x, t1, p256_mod);
651 /* 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 */
656 sp_256_mont_dbl_10(r->y, r->y, p256_mod);
657 /* Y = Y * Y */
658 sp_256_mont_sqr_10(r->y, r->y, p256_mod, p256_mp_mod);
659 /* T2 = Y * Y */
660 sp_256_mont_sqr_10(t2, r->y, p256_mod, p256_mp_mod);
661 /* T2 = T2/2 */
662 sp_256_div2_10(t2, t2, p256_mod);
663 /* Y = Y * X */
664 sp_256_mont_mul_10(r->y, r->y, r->x, p256_mod, p256_mp_mod);
665 /* X = T1 * T1 */
666 sp_256_mont_mul_10(r->x, t1, t1, p256_mod, p256_mp_mod);
667 /* X = X - Y */
668 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
669 /* X = X - Y */
670 sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod);
671 /* Y = Y - X */
672 sp_256_mont_sub_10(r->y, r->y, r->x, p256_mod);
673 /* Y = Y * T1 */
674 sp_256_mont_mul_10(r->y, r->y, t1, p256_mod, p256_mp_mod);
675 /* Y = Y - T2 */
676 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 Vlasenko12040122021-04-26 20:24:34 +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
Denys Vlasenko12040122021-04-26 20:24:34 +0200693 /* 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 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200699
Denys Vlasenko12040122021-04-26 20:24:34 +0200700 /* 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)
704 && 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))
706 ) {
707 sp_256_proj_point_dbl_10(r, p);
708 }
709 else {
710 sp_point tp;
711 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200712
Denys Vlasenko12040122021-04-26 20:24:34 +0200713 v = r;
714 if (p->infinity | q->infinity) {
715 memset(&tp, 0, sizeof(tp));
716 v = &tp;
717 }
Denys Vlasenko772e1872021-04-26 17:25:27 +0200718
Denys Vlasenko12040122021-04-26 20:24:34 +0200719 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200720
Denys Vlasenko12040122021-04-26 20:24:34 +0200721 /* 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);
724 sp_256_mont_mul_10(t1, t1, v->x, p256_mod, p256_mp_mod);
725 /* U2 = X2*Z1^2 */
726 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);
728 sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod);
729 /* S1 = Y1*Z2^3 */
730 sp_256_mont_mul_10(t3, t3, v->y, p256_mod, p256_mp_mod);
731 /* 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 */
738 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);
740 /* X3 = R^2 - H^3 - 2*U1*H^2 */
741 sp_256_mont_sqr_10(v->x, t4, p256_mod, p256_mp_mod);
742 sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod);
743 sp_256_mont_mul_10(v->y, t1, t5, p256_mod, p256_mp_mod);
744 sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod);
745 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);
748 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
749 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);
751 sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod);
752 sp_256_mont_sub_10(v->y, v->y, t5, p256_mod);
753 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200754}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200766 enum { map = 1 }; /* we always convert result to affine coordinates */
767 sp_point t[3];
768 sp_digit n;
769 int i;
770 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200771
Denys Vlasenko12040122021-04-26 20:24:34 +0200772 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200773
Denys Vlasenko12040122021-04-26 20:24:34 +0200774 /* t[0] = {0, 0, 1} * norm */
775 t[0].infinity = 1;
776 /* t[1] = {g->x, g->y, g->z} * norm */
777 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
Denys Vlasenko12040122021-04-26 20:24:34 +0200781 i = 9;
782 c = 22;
783 n = k[i--] << (26 - c);
784 for (; ; c--) {
785 if (c == 0) {
786 if (i == -1)
787 break;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200788
Denys Vlasenko12040122021-04-26 20:24:34 +0200789 n = k[i--];
790 c = 26;
791 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200792
Denys Vlasenko12040122021-04-26 20:24:34 +0200793 y = (n >> 25) & 1;
794 n <<= 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200795
Denys Vlasenko12040122021-04-26 20:24:34 +0200796 sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]);
797 memcpy(&t[2], &t[y], sizeof(sp_point));
798 sp_256_proj_point_dbl_10(&t[2], &t[2]);
799 memcpy(&t[y], &t[2], sizeof(sp_point));
800 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200801
Denys Vlasenko12040122021-04-26 20:24:34 +0200802 if (map)
803 sp_256_map_10(r, &t[0]);
804 else
805 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200806
Denys Vlasenko12040122021-04-26 20:24:34 +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 Vlasenko12040122021-04-26 20:24:34 +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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200831 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200832
833#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +0200834 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200835#endif
Denys Vlasenko12040122021-04-26 20:24:34 +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 Vlasenko12040122021-04-26 20:24:34 +0200839 sp_256_point_from_bin2x32(point, pub2x32);
840 dump_hex("point->x %s\n", point->x, sizeof(point->x));
841 dump_hex("point->y %s\n", point->y, sizeof(point->y));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200842
Denys Vlasenko12040122021-04-26 20:24:34 +0200843 sp_256_ecc_mulmod_10(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200844
Denys Vlasenko12040122021-04-26 20:24:34 +0200845 sp_256_to_bin(point->x, out32);
846 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200847}
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{
Denys Vlasenko12040122021-04-26 20:24:34 +0200855 a[0]++;
856 sp_256_norm_10(a);
Denys Vlasenko074b33b2021-04-26 14:33:38 +0200857}
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}