blob: cb166e413217e425670aac483fc5d0ba89693927 [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
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020012#define ALLOW_ASM 1
13
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020014#if SP_DEBUG
15# define dbg(...) fprintf(stderr, __VA_ARGS__)
16static void dump_hex(const char *fmt, const void *vp, int len)
17{
18 char hexbuf[32 * 1024 + 4];
19 const uint8_t *p = vp;
20
21 bin2hex(hexbuf, (void*)p, len)[0] = '\0';
22 dbg(fmt, hexbuf);
23}
24#else
25# define dbg(...) ((void)0)
26# define dump_hex(...) ((void)0)
27#endif
28
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020029typedef uint32_t sp_digit;
30typedef int32_t signed_sp_digit;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020031
Denys Vlasenko4bc9da12021-11-27 11:28:11 +010032/* 64-bit optimizations:
33 * if BB_UNALIGNED_MEMACCESS_OK && ULONG_MAX > 0xffffffff,
34 * then loads and stores can be done in 64-bit chunks.
35 *
36 * A narrower case is when arch is also little-endian (such as x86_64),
37 * then "LSW first", uint32[8] and uint64[4] representations are equivalent,
38 * and arithmetic can be done in 64 bits too.
39 */
40#if defined(__GNUC__) && defined(__x86_64__)
41# define UNALIGNED_LE_64BIT 1
42#else
43# define UNALIGNED_LE_64BIT 0
44#endif
45
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020046/* The code below is taken from parts of
47 * wolfssl-3.15.3/wolfcrypt/src/sp_c32.c
48 * and heavily modified.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020049 */
50
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020051typedef struct sp_point {
Denys Vlasenko1b93c7c2021-11-28 02:56:02 +010052 sp_digit x[8]
53#if ULONG_MAX > 0xffffffff
54 /* Make sp_point[] arrays to not be 64-bit misaligned */
55 ALIGNED(8)
56#endif
57 ;
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +010058 sp_digit y[8];
59 sp_digit z[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020060 int infinity;
61} sp_point;
62
63/* The modulus (prime) of the curve P256. */
Denys Vlasenko1b93c7c2021-11-28 02:56:02 +010064static const sp_digit p256_mod[8] ALIGNED(8) = {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020065 0xffffffff,0xffffffff,0xffffffff,0x00000000,
66 0x00000000,0x00000000,0x00000001,0xffffffff,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020067};
68
69#define p256_mp_mod ((sp_digit)0x000001)
70
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020071/* Normalize the values in each word to 32 bits - NOP */
72#define sp_256_norm_8(a) ((void)0)
Denys Vlasenko77145182021-10-01 13:51:39 +020073
Denys Vlasenkoe7305052021-10-05 13:30:48 +020074/* Write r as big endian to byte array.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020075 * Fixed length number of bytes written: 32
76 *
77 * r A single precision integer.
78 * a Byte array.
79 */
Denys Vlasenko4bc9da12021-11-27 11:28:11 +010080#if BB_UNALIGNED_MEMACCESS_OK && ULONG_MAX > 0xffffffff
81static void sp_256_to_bin_8(const sp_digit* rr, uint8_t* a)
82{
83 int i;
84 const uint64_t* r = (void*)rr;
85
86 sp_256_norm_8(rr);
87
88 r += 4;
89 for (i = 0; i < 4; i++) {
90 r--;
91 move_to_unaligned64(a, SWAP_BE64(*r));
92 a += 8;
93 }
94}
95#else
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020096static void sp_256_to_bin_8(const sp_digit* r, uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020097{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020098 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020099
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200100 sp_256_norm_8(r);
Denys Vlasenko77145182021-10-01 13:51:39 +0200101
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200102 r += 8;
103 for (i = 0; i < 8; i++) {
104 r--;
105 move_to_unaligned32(a, SWAP_BE32(*r));
106 a += 4;
Denys Vlasenko12040122021-04-26 20:24:34 +0200107 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200108}
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100109#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200110
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200111/* Read big endian unsigned byte array into r.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200112 *
113 * r A single precision integer.
114 * a Byte array.
115 * n Number of bytes in array to read.
116 */
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100117#if BB_UNALIGNED_MEMACCESS_OK && ULONG_MAX > 0xffffffff
118static void sp_256_from_bin_8(sp_digit* rr, const uint8_t* a)
119{
120 int i;
121 uint64_t* r = (void*)rr;
122
123 r += 4;
124 for (i = 0; i < 4; i++) {
125 uint64_t v;
126 move_from_unaligned64(v, a);
127 *--r = SWAP_BE64(v);
128 a += 8;
129 }
130}
131#else
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200132static void sp_256_from_bin_8(sp_digit* r, const uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200133{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200134 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200135
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200136 r += 8;
137 for (i = 0; i < 8; i++) {
138 sp_digit v;
139 move_from_unaligned32(v, a);
140 *--r = SWAP_BE32(v);
141 a += 4;
Denys Vlasenko12040122021-04-26 20:24:34 +0200142 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200143}
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100144#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200145
Denys Vlasenko137864f2021-10-05 13:47:42 +0200146#if SP_DEBUG
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200147static void dump_256(const char *fmt, const sp_digit* r)
Denys Vlasenko137864f2021-10-05 13:47:42 +0200148{
Denys Vlasenko137864f2021-10-05 13:47:42 +0200149 uint8_t b32[32];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200150 sp_256_to_bin_8(r, b32);
Denys Vlasenko137864f2021-10-05 13:47:42 +0200151 dump_hex(fmt, b32, 32);
152}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200153static void dump_512(const char *fmt, const sp_digit* r)
Denys Vlasenko137864f2021-10-05 13:47:42 +0200154{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200155 uint8_t b64[64];
156 sp_256_to_bin_8(r, b64 + 32);
157 sp_256_to_bin_8(r+8, b64);
158 dump_hex(fmt, b64, 64);
Denys Vlasenko137864f2021-10-05 13:47:42 +0200159}
160#else
161# define dump_256(...) ((void)0)
162# define dump_512(...) ((void)0)
163#endif
164
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200165/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
166static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
167{
Denys Vlasenko12040122021-04-26 20:24:34 +0200168 memset(p, 0, sizeof(*p));
169 /*p->infinity = 0;*/
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200170 sp_256_from_bin_8(p->x, bin2x32);
171 sp_256_from_bin_8(p->y, bin2x32 + 32);
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200172 p->z[0] = 1; /* p->z = 1 */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200173}
174
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200175/* Compare a with b.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200176 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200177 * return -ve, 0 or +ve if a is less than, equal to or greater than b
178 * respectively.
179 */
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100180#if UNALIGNED_LE_64BIT
181static signed_sp_digit sp_256_cmp_8(const sp_digit* aa, const sp_digit* bb)
182{
183 const uint64_t* a = (void*)aa;
184 const uint64_t* b = (void*)bb;
185 int i;
186 for (i = 3; i >= 0; i--) {
187 if (a[i] == b[i])
188 continue;
189 return (a[i] > b[i]) * 2 - 1;
190 }
191 return 0;
192}
193#else
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200194static signed_sp_digit sp_256_cmp_8(const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200195{
Denys Vlasenko12040122021-04-26 20:24:34 +0200196 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200197 for (i = 7; i >= 0; i--) {
198/* signed_sp_digit r = a[i] - b[i];
199 * if (r != 0)
200 * return r;
201 * does not work: think about a[i]=0, b[i]=0xffffffff
202 */
203 if (a[i] == b[i])
204 continue;
205 return (a[i] > b[i]) * 2 - 1;
Denys Vlasenko12040122021-04-26 20:24:34 +0200206 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200207 return 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200208}
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100209#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200210
211/* Compare two numbers to determine if they are equal.
212 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200213 * return 1 when equal and 0 otherwise.
214 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200215static int sp_256_cmp_equal_8(const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200216{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200217 return sp_256_cmp_8(a, b) == 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200218}
219
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200220/* Add b to a into r. (r = a + b). Return !0 on overflow */
221static int sp_256_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200222{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200223#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
224 sp_digit reg;
225 asm volatile (
226"\n movl (%0), %3"
227"\n addl (%1), %3"
228"\n movl %3, (%2)"
229"\n"
230"\n movl 1*4(%0), %3"
231"\n adcl 1*4(%1), %3"
232"\n movl %3, 1*4(%2)"
233"\n"
234"\n movl 2*4(%0), %3"
235"\n adcl 2*4(%1), %3"
236"\n movl %3, 2*4(%2)"
237"\n"
238"\n movl 3*4(%0), %3"
239"\n adcl 3*4(%1), %3"
240"\n movl %3, 3*4(%2)"
241"\n"
242"\n movl 4*4(%0), %3"
243"\n adcl 4*4(%1), %3"
244"\n movl %3, 4*4(%2)"
245"\n"
246"\n movl 5*4(%0), %3"
247"\n adcl 5*4(%1), %3"
248"\n movl %3, 5*4(%2)"
249"\n"
250"\n movl 6*4(%0), %3"
251"\n adcl 6*4(%1), %3"
252"\n movl %3, 6*4(%2)"
253"\n"
254"\n movl 7*4(%0), %3"
255"\n adcl 7*4(%1), %3"
256"\n movl %3, 7*4(%2)"
257"\n"
258"\n sbbl %3, %3"
259"\n"
260 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
261 : "0" (a), "1" (b), "2" (r)
262 : "memory"
263 );
264 return reg;
Denys Vlasenko911344a2021-10-06 17:17:34 +0200265#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
Denys Vlasenko911344a2021-10-06 17:17:34 +0200266 uint64_t reg;
267 asm volatile (
268"\n movq (%0), %3"
269"\n addq (%1), %3"
270"\n movq %3, (%2)"
271"\n"
272"\n movq 1*8(%0), %3"
273"\n adcq 1*8(%1), %3"
274"\n movq %3, 1*8(%2)"
275"\n"
276"\n movq 2*8(%0), %3"
277"\n adcq 2*8(%1), %3"
278"\n movq %3, 2*8(%2)"
279"\n"
280"\n movq 3*8(%0), %3"
281"\n adcq 3*8(%1), %3"
282"\n movq %3, 3*8(%2)"
283"\n"
284"\n sbbq %3, %3"
285"\n"
286 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
287 : "0" (a), "1" (b), "2" (r)
288 : "memory"
289 );
290 return reg;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200291#else
Denys Vlasenko12040122021-04-26 20:24:34 +0200292 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200293 sp_digit carry;
294
295 carry = 0;
296 for (i = 0; i < 8; i++) {
297 sp_digit w, v;
298 w = b[i] + carry;
299 v = a[i];
300 if (w != 0) {
301 v = a[i] + w;
302 carry = (v < a[i]);
303 /* hope compiler detects above as "carry flag set" */
304 }
305 /* else: b + carry == 0, two cases:
306 * b:ffffffff, carry:1
307 * b:00000000, carry:0
308 * in either case, r[i] = a[i] and carry remains unchanged
309 */
310 r[i] = v;
311 }
312 return carry;
313#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200314}
315
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200316/* Sub b from a into r. (r = a - b). Return !0 on underflow */
317static int sp_256_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200318{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200319#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
320 sp_digit reg;
321 asm volatile (
322"\n movl (%0), %3"
323"\n subl (%1), %3"
324"\n movl %3, (%2)"
325"\n"
326"\n movl 1*4(%0), %3"
327"\n sbbl 1*4(%1), %3"
328"\n movl %3, 1*4(%2)"
329"\n"
330"\n movl 2*4(%0), %3"
331"\n sbbl 2*4(%1), %3"
332"\n movl %3, 2*4(%2)"
333"\n"
334"\n movl 3*4(%0), %3"
335"\n sbbl 3*4(%1), %3"
336"\n movl %3, 3*4(%2)"
337"\n"
338"\n movl 4*4(%0), %3"
339"\n sbbl 4*4(%1), %3"
340"\n movl %3, 4*4(%2)"
341"\n"
342"\n movl 5*4(%0), %3"
343"\n sbbl 5*4(%1), %3"
344"\n movl %3, 5*4(%2)"
345"\n"
346"\n movl 6*4(%0), %3"
347"\n sbbl 6*4(%1), %3"
348"\n movl %3, 6*4(%2)"
349"\n"
350"\n movl 7*4(%0), %3"
351"\n sbbl 7*4(%1), %3"
352"\n movl %3, 7*4(%2)"
353"\n"
354"\n sbbl %3, %3"
355"\n"
356 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
357 : "0" (a), "1" (b), "2" (r)
358 : "memory"
359 );
360 return reg;
Denys Vlasenko911344a2021-10-06 17:17:34 +0200361#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
Denys Vlasenko911344a2021-10-06 17:17:34 +0200362 uint64_t reg;
363 asm volatile (
364"\n movq (%0), %3"
365"\n subq (%1), %3"
366"\n movq %3, (%2)"
367"\n"
368"\n movq 1*8(%0), %3"
369"\n sbbq 1*8(%1), %3"
370"\n movq %3, 1*8(%2)"
371"\n"
372"\n movq 2*8(%0), %3"
373"\n sbbq 2*8(%1), %3"
374"\n movq %3, 2*8(%2)"
375"\n"
376"\n movq 3*8(%0), %3"
377"\n sbbq 3*8(%1), %3"
378"\n movq %3, 3*8(%2)"
379"\n"
380"\n sbbq %3, %3"
381"\n"
382 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
383 : "0" (a), "1" (b), "2" (r)
384 : "memory"
385 );
386 return reg;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200387#else
Denys Vlasenko12040122021-04-26 20:24:34 +0200388 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200389 sp_digit borrow;
390
391 borrow = 0;
392 for (i = 0; i < 8; i++) {
393 sp_digit w, v;
394 w = b[i] + borrow;
395 v = a[i];
396 if (w != 0) {
397 v = a[i] - w;
398 borrow = (v > a[i]);
399 /* hope compiler detects above as "carry flag set" */
400 }
401 /* else: b + borrow == 0, two cases:
402 * b:ffffffff, borrow:1
403 * b:00000000, borrow:0
404 * in either case, r[i] = a[i] and borrow remains unchanged
405 */
406 r[i] = v;
407 }
408 return borrow;
409#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200410}
411
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200412/* Sub p256_mod from r. (r = r - p256_mod). */
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200413#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200414static void sp_256_sub_8_p256_mod(sp_digit* r)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200415{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200416//p256_mod[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
417 asm volatile (
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200418"\n subl $0xffffffff, (%0)"
419"\n sbbl $0xffffffff, 1*4(%0)"
420"\n sbbl $0xffffffff, 2*4(%0)"
421"\n sbbl $0, 3*4(%0)"
422"\n sbbl $0, 4*4(%0)"
423"\n sbbl $0, 5*4(%0)"
424"\n sbbl $1, 6*4(%0)"
425"\n sbbl $0xffffffff, 7*4(%0)"
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200426"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200427 : "=r" (r)
428 : "0" (r)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200429 : "memory"
430 );
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200431}
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200432#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200433static void sp_256_sub_8_p256_mod(sp_digit* r)
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200434{
435 uint64_t reg;
436 uint64_t ooff;
437//p256_mod[3..0] = ffffffff00000001 0000000000000000 00000000ffffffff ffffffffffffffff
438 asm volatile (
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200439"\n addq $1, (%0)" // adding 1 is the same as subtracting ffffffffffffffff
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200440"\n cmc" // only carry bit needs inverting
Denys Vlasenko17e6fb02021-10-06 21:22:36 +0200441"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200442"\n sbbq %1, 1*8(%0)" // %1 holds 00000000ffffffff
Denys Vlasenko17e6fb02021-10-06 21:22:36 +0200443"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200444"\n sbbq $0, 2*8(%0)"
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200445"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200446"\n movq 3*8(%0), %2"
447"\n sbbq $0, %2" // adding 00000000ffffffff (in %1)
448"\n addq %1, %2" // is the same as subtracting ffffffff00000001
449"\n movq %2, 3*8(%0)"
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200450"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200451 : "=r" (r), "=r" (ooff), "=r" (reg)
452 : "0" (r), "1" (0x00000000ffffffff)
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200453 : "memory"
454 );
455}
Denys Vlasenko567eefc2021-10-06 14:25:10 +0200456#else
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200457static void sp_256_sub_8_p256_mod(sp_digit* r)
458{
459 sp_256_sub_8(r, r, p256_mod);
460}
Denys Vlasenko567eefc2021-10-06 14:25:10 +0200461#endif
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200462
Denys Vlasenko4415f7b2021-11-27 15:47:26 +0100463/* Multiply a and b into r. (r = a * b)
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +0100464 * r should be [16] array (512 bits), and must not coincide with a or b.
Denys Vlasenko4415f7b2021-11-27 15:47:26 +0100465 */
466static void sp_256to512_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200467{
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200468#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200469 int k;
470 uint32_t accl;
471 uint32_t acch;
472
473 acch = accl = 0;
474 for (k = 0; k < 15; k++) {
475 int i, j;
476 uint32_t acc_hi;
477 i = k - 7;
478 if (i < 0)
479 i = 0;
480 j = k - i;
481 acc_hi = 0;
482 do {
483////////////////////////
484// uint64_t m = ((uint64_t)a[i]) * b[j];
485// acc_hi:acch:accl += m;
486 asm volatile (
487 // a[i] is already loaded in %%eax
488"\n mull %7"
489"\n addl %%eax, %0"
490"\n adcl %%edx, %1"
491"\n adcl $0, %2"
492 : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi)
493 : "0" (accl), "1" (acch), "2" (acc_hi), "a" (a[i]), "m" (b[j])
494 : "cc", "dx"
495 );
496////////////////////////
497 j--;
498 i++;
499 } while (i != 8 && i <= k);
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +0100500 r[k] = accl;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200501 accl = acch;
502 acch = acc_hi;
503 }
504 r[15] = accl;
Denys Vlasenko911344a2021-10-06 17:17:34 +0200505#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
Denys Vlasenko911344a2021-10-06 17:17:34 +0200506 const uint64_t* aa = (const void*)a;
507 const uint64_t* bb = (const void*)b;
Denys Vlasenko0b13ab62021-11-27 19:36:23 +0100508 uint64_t* rr = (void*)r;
Denys Vlasenko911344a2021-10-06 17:17:34 +0200509 int k;
510 uint64_t accl;
511 uint64_t acch;
512
513 acch = accl = 0;
514 for (k = 0; k < 7; k++) {
515 int i, j;
516 uint64_t acc_hi;
517 i = k - 3;
518 if (i < 0)
519 i = 0;
520 j = k - i;
521 acc_hi = 0;
522 do {
523////////////////////////
524// uint128_t m = ((uint128_t)a[i]) * b[j];
525// acc_hi:acch:accl += m;
526 asm volatile (
527 // aa[i] is already loaded in %%rax
528"\n mulq %7"
529"\n addq %%rax, %0"
530"\n adcq %%rdx, %1"
531"\n adcq $0, %2"
532 : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi)
533 : "0" (accl), "1" (acch), "2" (acc_hi), "a" (aa[i]), "m" (bb[j])
534 : "cc", "dx"
535 );
536////////////////////////
Denys Vlasenko17e6fb02021-10-06 21:22:36 +0200537 j--;
Denys Vlasenko911344a2021-10-06 17:17:34 +0200538 i++;
539 } while (i != 4 && i <= k);
540 rr[k] = accl;
541 accl = acch;
542 acch = acc_hi;
543 }
544 rr[7] = accl;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200545#elif 0
546 //TODO: arm assembly (untested)
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200547 asm volatile (
548"\n mov r5, #0"
549"\n mov r6, #0"
550"\n mov r7, #0"
551"\n mov r8, #0"
552"\n 1:"
553"\n subs r3, r5, #28"
554"\n movcc r3, #0"
555"\n sub r4, r5, r3"
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200556"\n 2:"
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200557"\n ldr r14, [%[a], r3]"
558"\n ldr r12, [%[b], r4]"
559"\n umull r9, r10, r14, r12"
560"\n adds r6, r6, r9"
561"\n adcs r7, r7, r10"
562"\n adc r8, r8, #0"
563"\n add r3, r3, #4"
564"\n sub r4, r4, #4"
565"\n cmp r3, #32"
566"\n beq 3f"
567"\n cmp r3, r5"
568"\n ble 2b"
569"\n 3:"
570"\n str r6, [%[r], r5]"
571"\n mov r6, r7"
572"\n mov r7, r8"
573"\n mov r8, #0"
574"\n add r5, r5, #4"
575"\n cmp r5, #56"
576"\n ble 1b"
577"\n str r6, [%[r], r5]"
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +0100578 : [r] "r" (r), [a] "r" (a), [b] "r" (b)
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200579 : "memory", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14"
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200580 );
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200581#else
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200582 int i, j, k;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200583 uint64_t acc;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200584
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200585 acc = 0;
586 for (k = 0; k < 15; k++) {
587 uint32_t acc_hi;
588 i = k - 7;
589 if (i < 0)
590 i = 0;
591 j = k - i;
592 acc_hi = 0;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200593 do {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200594 uint64_t m = ((uint64_t)a[i]) * b[j];
595 acc += m;
596 if (acc < m)
597 acc_hi++;
598 j--;
599 i++;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200600 } while (i != 8 && i <= k);
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +0100601 r[k] = acc;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200602 acc = (acc >> 32) | ((uint64_t)acc_hi << 32);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200603 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200604 r[15] = acc;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200605#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200606}
607
Denys Vlasenko389329e2021-10-05 13:39:33 +0200608/* Shift number right one bit. Bottom bit is lost. */
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100609#if UNALIGNED_LE_64BIT
610static void sp_256_rshift1_8(sp_digit* rr, uint64_t carry)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200611{
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100612 uint64_t *r = (void*)rr;
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200613 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200614
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100615 carry = (((uint64_t)!!carry) << 63);
616 for (i = 3; i >= 0; i--) {
617 uint64_t c = r[i] << 63;
618 r[i] = (r[i] >> 1) | carry;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200619 carry = c;
620 }
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200621}
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100622#else
623static void sp_256_rshift1_8(sp_digit* r, sp_digit carry)
624{
625 int i;
626
627 carry = (((sp_digit)!!carry) << 31);
628 for (i = 7; i >= 0; i--) {
629 sp_digit c = r[i] << 31;
630 r[i] = (r[i] >> 1) | carry;
631 carry = c;
632 }
633}
634#endif
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200635
Denys Vlasenkodcfd8d32021-11-27 16:07:42 +0100636/* Divide the number by 2 mod the modulus (prime). (r = (r / 2) % m) */
637static void sp_256_div2_8(sp_digit* r /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200638{
Denys Vlasenkodcfd8d32021-11-27 16:07:42 +0100639 const sp_digit* m = p256_mod;
640
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200641 int carry = 0;
Denys Vlasenkodcfd8d32021-11-27 16:07:42 +0100642 if (r[0] & 1)
643 carry = sp_256_add_8(r, r, m);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200644 sp_256_norm_8(r);
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100645 sp_256_rshift1_8(r, carry);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200646}
647
648/* Add two Montgomery form numbers (r = a + b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200649static void sp_256_mont_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b
650 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200651{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200652// const sp_digit* m = p256_mod;
653
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200654 int carry = sp_256_add_8(r, a, b);
655 sp_256_norm_8(r);
656 if (carry) {
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200657 sp_256_sub_8_p256_mod(r);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200658 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200659 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200660}
661
662/* Subtract two Montgomery form numbers (r = a - b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200663static void sp_256_mont_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b
664 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200665{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200666 const sp_digit* m = p256_mod;
667
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200668 int borrow;
669 borrow = sp_256_sub_8(r, a, b);
670 sp_256_norm_8(r);
671 if (borrow) {
672 sp_256_add_8(r, r, m);
673 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200674 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200675}
676
677/* Double a Montgomery form number (r = a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200678static void sp_256_mont_dbl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200679{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200680// const sp_digit* m = p256_mod;
681
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200682 int carry = sp_256_add_8(r, a, a);
683 sp_256_norm_8(r);
684 if (carry)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200685 sp_256_sub_8_p256_mod(r);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200686 sp_256_norm_8(r);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200687}
688
689/* Triple a Montgomery form number (r = a + a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200690static void sp_256_mont_tpl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200691{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200692// const sp_digit* m = p256_mod;
693
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200694 int carry = sp_256_add_8(r, a, a);
695 sp_256_norm_8(r);
696 if (carry) {
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200697 sp_256_sub_8_p256_mod(r);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200698 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200699 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200700 carry = sp_256_add_8(r, r, a);
701 sp_256_norm_8(r);
702 if (carry) {
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200703 sp_256_sub_8_p256_mod(r);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200704 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200705 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200706}
707
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100708/* Shift the result in the high 256 bits down to the bottom. */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +0100709static void sp_512to256_mont_shift_8(sp_digit* r, sp_digit* a)
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100710{
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +0100711 memcpy(r, a + 8, sizeof(*r) * 8);
Denys Vlasenko4bc9da12021-11-27 11:28:11 +0100712}
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200713
Denys Vlasenko8514b412021-11-28 21:40:23 +0100714#if UNALIGNED_LE_64BIT
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100715/* 64-bit little-endian optimized version.
716 * See generic 32-bit version below for explanation.
717 * The benefit of this version is: even though r[3] calculation is atrocious,
718 * we call sp_256_mul_add_4() four times, not 8.
Denys Vlasenko8514b412021-11-28 21:40:23 +0100719 * Measured run time improvement of curve_P256_compute_pubkey_and_premaster()
720 * call on x86-64: from ~1500us to ~900us. Code size +32 bytes.
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100721 */
722static int sp_256_mul_add_4(uint64_t *r /*, const uint64_t* a, uint64_t b*/)
723{
724 uint64_t b = r[0];
725
726# if 0
727 const uint64_t* a = (const void*)p256_mod;
728//a[3..0] = ffffffff00000001 0000000000000000 00000000ffffffff ffffffffffffffff
729 uint128_t t;
730 int i;
731 t = 0;
732 for (i = 0; i < 4; i++) {
733 uint32_t t_hi;
734 uint128_t m = ((uint128_t)b * a[i]) + r[i];
735 t += m;
736 t_hi = (t < m);
737 r[i] = (uint64_t)t;
738 t = (t >> 64) | ((uint128_t)t_hi << 64);
739 }
740 r[4] += (uint64_t)t;
741 return (r[4] < (uint64_t)t); /* 1 if addition overflowed */
742# else
743 // Unroll, then optimize the above loop:
744 //uint32_t t_hi;
745 //uint128_t m;
746 uint64_t t64, t64u;
747
748 //m = ((uint128_t)b * a[0]) + r[0];
749 // Since b is r[0] and a[0] is ffffffffffffffff, the above optimizes to:
750 // m = r[0] * ffffffffffffffff + r[0] = (r[0] << 64 - r[0]) + r[0] = r[0] << 64;
751 //t += m;
752 // t = r[0] << 64 = b << 64;
753 //t_hi = (t < m);
754 // t_hi = 0;
755 //r[0] = (uint64_t)t;
756// r[0] = 0;
757//the store can be eliminated since caller won't look at lower 256 bits of the result
758 //t = (t >> 64) | ((uint128_t)t_hi << 64);
759 // t = b;
760
761 //m = ((uint128_t)b * a[1]) + r[1];
762 // Since a[1] is 00000000ffffffff, the above optimizes to:
763 // m = b * ffffffff + r[1] = (b * 100000000 - b) + r[1] = (b << 32) - b + r[1];
764 //t += m;
765 // t = b + (b << 32) - b + r[1] = (b << 32) + r[1];
766 //t_hi = (t < m);
767 // t_hi = 0;
768 //r[1] = (uint64_t)t;
769 r[1] += (b << 32);
770 //t = (t >> 64) | ((uint128_t)t_hi << 64);
771 t64 = (r[1] < (b << 32));
772 t64 += (b >> 32);
773
774 //m = ((uint128_t)b * a[2]) + r[2];
775 // Since a[2] is 0000000000000000, the above optimizes to:
776 // m = b * 0 + r[2] = r[2];
777 //t += m;
778 // t = t64 + r[2];
779 //t_hi = (t < m);
780 // t_hi = 0;
781 //r[2] = (uint64_t)t;
782 r[2] += t64;
783 //t = (t >> 64) | ((uint128_t)t_hi << 64);
784 t64 = (r[2] < t64);
785
786 //m = ((uint128_t)b * a[3]) + r[3];
787 // Since a[3] is ffffffff00000001, the above optimizes to:
788 // m = b * ffffffff00000001 + r[3];
789 // m = b + b*ffffffff00000000 + r[3]
790 // m = b + (b*ffffffff << 32) + r[3]
791 // m = b + (((b<<32) - b) << 32) + r[3]
792 //t += m;
793 // t = t64 + (uint128_t)b + ((((uint128_t)b << 32) - b) << 32) + r[3];
794 t64 += b;
795 t64u = (t64 < b);
796 t64 += r[3];
797 t64u += (t64 < r[3]);
Denys Vlasenko8514b412021-11-28 21:40:23 +0100798 { // add ((((uint128_t)b << 32) - b) << 32):
799 uint64_t lo, hi;
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100800 //lo = (((b << 32) - b) << 32
801 //hi = (((uint128_t)b << 32) - b) >> 32
802 //but without uint128_t:
Denys Vlasenko8514b412021-11-28 21:40:23 +0100803 hi = (b << 32) - b; /* make lower 32 bits of "hi", part 1 */
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100804 b = (b >> 32) - (/*borrowed above?*/(b << 32) < b); /* upper 32 bits of "hi" are in b */
805 lo = hi << 32; /* (use "hi" value to calculate "lo",... */
806 t64 += lo; /* ...consume... */
807 t64u += (t64 < lo); /* ..."lo") */
Denys Vlasenko8514b412021-11-28 21:40:23 +0100808 hi >>= 32; /* make lower 32 bits of "hi", part 2 */
809 hi |= (b << 32); /* combine lower and upper 32 bits */
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100810 t64u += hi; /* consume "hi" */
811 }
812 //t_hi = (t < m);
813 // t_hi = 0;
814 //r[3] = (uint64_t)t;
815 r[3] = t64;
816 //t = (t >> 64) | ((uint128_t)t_hi << 64);
817 // t = t64u;
818
819 r[4] += t64u;
820 return (r[4] < t64u); /* 1 if addition overflowed */
821# endif
822}
823
824static void sp_512to256_mont_reduce_8(sp_digit* r, sp_digit* aa/*, const sp_digit* m, sp_digit mp*/)
825{
826// const sp_digit* m = p256_mod;
827 int i;
828 uint64_t *a = (void*)aa;
829
830 sp_digit carry = 0;
831 for (i = 0; i < 4; i++) {
832// mu = a[i];
833 if (sp_256_mul_add_4(a+i /*, m, mu*/)) {
834 int j = i + 4;
835 inc_next_word:
836 if (++j > 7) { /* a[8] array has no more words? */
837 carry++;
838 continue;
839 }
840 if (++a[j] == 0) /* did this overflow too? */
841 goto inc_next_word;
842 }
843 }
844 sp_512to256_mont_shift_8(r, aa);
845 if (carry != 0)
846 sp_256_sub_8_p256_mod(r);
847 sp_256_norm_8(r);
848}
849
850#else /* Generic 32-bit version */
851
Denys Vlasenko4415f7b2021-11-27 15:47:26 +0100852/* Mul a by scalar b and add into r. (r += a * b)
853 * a = p256_mod
854 * b = r[0]
855 */
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200856static int sp_256_mul_add_8(sp_digit* r /*, const sp_digit* a, sp_digit b*/)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200857{
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200858 sp_digit b = r[0];
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200859 uint64_t t;
860
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100861# if 0
862 const sp_digit* a = p256_mod;
863//a[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
864 int i;
865 t = 0;
866 for (i = 0; i < 8; i++) {
867 uint32_t t_hi;
868 uint64_t m = ((uint64_t)b * a[i]) + r[i];
869 t += m;
870 t_hi = (t < m);
871 r[i] = (sp_digit)t;
872 t = (t >> 32) | ((uint64_t)t_hi << 32);
873 }
874 r[8] += (sp_digit)t;
875 return (r[8] < (sp_digit)t); /* 1 if addition overflowed */
876# else
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200877 // Unroll, then optimize the above loop:
878 //uint32_t t_hi;
879 uint64_t m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200880 uint32_t t32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200881
882 //m = ((uint64_t)b * a[0]) + r[0];
883 // Since b is r[0] and a[0] is ffffffff, the above optimizes to:
884 // m = r[0] * ffffffff + r[0] = (r[0] * 100000000 - r[0]) + r[0] = r[0] << 32;
885 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200886 // t = r[0] << 32 = b << 32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200887 //t_hi = (t < m);
888 // t_hi = 0;
889 //r[0] = (sp_digit)t;
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100890// r[0] = 0;
891//the store can be eliminated since caller won't look at lower 256 bits of the result
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200892 //t = (t >> 32) | ((uint64_t)t_hi << 32);
893 // t = b;
894
895 //m = ((uint64_t)b * a[1]) + r[1];
896 // Since a[1] is ffffffff, the above optimizes to:
897 // m = b * ffffffff + r[1] = (b * 100000000 - b) + r[1] = (b << 32) - b + r[1];
898 //t += m;
899 // t = b + (b << 32) - b + r[1] = (b << 32) + r[1];
900 //t_hi = (t < m);
901 // t_hi = 0;
902 //r[1] = (sp_digit)t;
903 // r[1] = r[1];
904 //t = (t >> 32) | ((uint64_t)t_hi << 32);
905 // t = b;
906
907 //m = ((uint64_t)b * a[2]) + r[2];
908 // Since a[2] is ffffffff, the above optimizes to:
909 // m = b * ffffffff + r[2] = (b * 100000000 - b) + r[2] = (b << 32) - b + r[2];
910 //t += m;
911 // t = b + (b << 32) - b + r[2] = (b << 32) + r[2]
912 //t_hi = (t < m);
913 // t_hi = 0;
914 //r[2] = (sp_digit)t;
915 // r[2] = r[2];
916 //t = (t >> 32) | ((uint64_t)t_hi << 32);
917 // t = b;
918
919 //m = ((uint64_t)b * a[3]) + r[3];
920 // Since a[3] is 00000000, the above optimizes to:
921 // m = b * 0 + r[3] = r[3];
922 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200923 // t = b + r[3];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200924 //t_hi = (t < m);
925 // t_hi = 0;
926 //r[3] = (sp_digit)t;
927 r[3] = r[3] + b;
928 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200929 t32 = (r[3] < b); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200930
931 //m = ((uint64_t)b * a[4]) + r[4];
932 // Since a[4] is 00000000, the above optimizes to:
933 // m = b * 0 + r[4] = r[4];
934 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200935 // t = t32 + r[4];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200936 //t_hi = (t < m);
937 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200938 //r[4] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200939 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200940 if (t32 != 0) {
941 r[4]++;
942 t32 = (r[4] == 0); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200943
944 //m = ((uint64_t)b * a[5]) + r[5];
945 // Since a[5] is 00000000, the above optimizes to:
946 // m = b * 0 + r[5] = r[5];
947 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200948 // t = t32 + r[5]; (t32 is 0 or 1)
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200949 //t_hi = (t < m);
950 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200951 //r[5] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200952 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200953 if (t32 != 0) {
954 r[5]++;
955 t32 = (r[5] == 0); // 0 or 1
956 }
957 }
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200958
959 //m = ((uint64_t)b * a[6]) + r[6];
960 // Since a[6] is 00000001, the above optimizes to:
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200961 // m = (uint64_t)b + r[6]; // 33 bits at most
962 //t += m;
963 t = t32 + (uint64_t)b + r[6];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200964 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200965 // t_hi = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200966 r[6] = (sp_digit)t;
967 //t = (t >> 32) | ((uint64_t)t_hi << 32);
968 t = (t >> 32);
969
970 //m = ((uint64_t)b * a[7]) + r[7];
971 // Since a[7] is ffffffff, the above optimizes to:
972 // m = b * ffffffff + r[7] = (b * 100000000 - b) + r[7]
973 m = ((uint64_t)b << 32) - b + r[7];
974 t += m;
975 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200976 // t_hi in fact is always 0 here (256bit * 32bit can't have more than 32 bits of overflow)
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200977 r[7] = (sp_digit)t;
978 //t = (t >> 32) | ((uint64_t)t_hi << 32);
979 t = (t >> 32);
980
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200981 r[8] += (sp_digit)t;
982 return (r[8] < (sp_digit)t); /* 1 if addition overflowed */
Denys Vlasenko90b0d332021-11-28 15:38:51 +0100983# endif
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200984}
985
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200986/* Reduce the number back to 256 bits using Montgomery reduction.
Denys Vlasenko9c671fe2021-11-27 18:42:27 +0100987 * Note: the result is NOT guaranteed to be less than p256_mod!
988 * (it is only guaranteed to fit into 256 bits).
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200989 *
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +0100990 * r Result.
991 * a Double-wide number to reduce. Clobbered.
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200992 * m The single precision number representing the modulus.
993 * mp The digit representing the negative inverse of m mod 2^n.
Denys Vlasenko83262622021-11-28 12:55:20 +0100994 *
995 * Montgomery reduction on multiprecision integers:
996 * Montgomery reduction requires products modulo R.
997 * When R is a power of B [in our case R=2^128, B=2^32], there is a variant
998 * of Montgomery reduction which requires products only of machine word sized
999 * integers. T is stored as an little-endian word array a[0..n]. The algorithm
1000 * reduces it one word at a time. First an appropriate multiple of modulus
1001 * is added to make T divisible by B. [In our case, it is p256_mp_mod * a[0].]
1002 * Then a multiple of modulus is added to make T divisible by B^2.
1003 * [In our case, it is (p256_mp_mod * a[1]) << 32.]
1004 * And so on. Eventually T is divisible by R, and after division by R
Denys Vlasenko90b0d332021-11-28 15:38:51 +01001005 * the algorithm is in the same place as the usual Montgomery reduction.
Denys Vlasenko83262622021-11-28 12:55:20 +01001006 *
1007 * TODO: Can conditionally use 64-bit (if bit-little-endian arch) logic?
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001008 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001009static void sp_512to256_mont_reduce_8(sp_digit* r, sp_digit* a/*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001010{
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001011// const sp_digit* m = p256_mod;
Denys Vlasenko389329e2021-10-05 13:39:33 +02001012 sp_digit mp = p256_mp_mod;
1013
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001014 int i;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +02001015// sp_digit mu;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001016
1017 if (mp != 1) {
Denys Vlasenko2430fcf2021-10-06 00:19:30 +02001018 sp_digit word16th = 0;
1019 for (i = 0; i < 8; i++) {
1020// mu = (sp_digit)(a[i] * mp);
1021 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
1022 int j = i + 8;
1023 inc_next_word0:
1024 if (++j > 15) { /* a[16] array has no more words? */
1025 word16th++;
1026 continue;
1027 }
1028 if (++a[j] == 0) /* did this overflow too? */
1029 goto inc_next_word0;
1030 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001031 }
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001032 sp_512to256_mont_shift_8(r, a);
Denys Vlasenko2430fcf2021-10-06 00:19:30 +02001033 if (word16th != 0)
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001034 sp_256_sub_8_p256_mod(r);
1035 sp_256_norm_8(r);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001036 }
Denys Vlasenko389329e2021-10-05 13:39:33 +02001037 else { /* Same code for explicit mp == 1 (which is always the case for P256) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001038 sp_digit word16th = 0;
1039 for (i = 0; i < 8; i++) {
Denys Vlasenko4415f7b2021-11-27 15:47:26 +01001040// mu = a[i];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +02001041 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001042 int j = i + 8;
1043 inc_next_word:
1044 if (++j > 15) { /* a[16] array has no more words? */
1045 word16th++;
1046 continue;
1047 }
1048 if (++a[j] == 0) /* did this overflow too? */
1049 goto inc_next_word;
1050 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001051 }
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001052 sp_512to256_mont_shift_8(r, a);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001053 if (word16th != 0)
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001054 sp_256_sub_8_p256_mod(r);
1055 sp_256_norm_8(r);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001056 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001057}
Denys Vlasenko90b0d332021-11-28 15:38:51 +01001058#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001059
1060/* Multiply two Montogmery form numbers mod the modulus (prime).
1061 * (r = a * b mod m)
1062 *
1063 * r Result of multiplication.
1064 * a First number to multiply in Montogmery form.
1065 * b Second number to multiply in Montogmery form.
1066 * m Modulus (prime).
Denys Vlasenko1b93c7c2021-11-28 02:56:02 +01001067 * mp Montogmery multiplier.
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001068 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001069static void sp_256_mont_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b
Denys Vlasenko389329e2021-10-05 13:39:33 +02001070 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001071{
Denys Vlasenko389329e2021-10-05 13:39:33 +02001072 //const sp_digit* m = p256_mod;
1073 //sp_digit mp = p256_mp_mod;
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001074 sp_digit t[2 * 8];
1075 sp_256to512_mul_8(t, a, b);
1076 sp_512to256_mont_reduce_8(r, t /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001077}
1078
1079/* Square the Montgomery form number. (r = a * a mod m)
1080 *
1081 * r Result of squaring.
1082 * a Number to square in Montogmery form.
1083 * m Modulus (prime).
Denys Vlasenko1b93c7c2021-11-28 02:56:02 +01001084 * mp Montogmery multiplier.
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001085 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001086static void sp_256_mont_sqr_8(sp_digit* r, const sp_digit* a
Denys Vlasenko389329e2021-10-05 13:39:33 +02001087 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001088{
Denys Vlasenko389329e2021-10-05 13:39:33 +02001089 //const sp_digit* m = p256_mod;
1090 //sp_digit mp = p256_mp_mod;
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001091 sp_256_mont_mul_8(r, a, a /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001092}
1093
1094/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
1095 * P256 curve. (r = 1 / a mod m)
1096 *
Denys Vlasenkocfb61572021-11-28 11:10:00 +01001097 * r Inverse result. Must not coincide with a.
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001098 * a Number to invert.
1099 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001100static void sp_256_mont_inv_8(sp_digit* r, sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001101{
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001102 int i;
1103
Denys Vlasenkocfb61572021-11-28 11:10:00 +01001104 memcpy(r, a, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001105 for (i = 254; i >= 0; i--) {
Denys Vlasenkocfb61572021-11-28 11:10:00 +01001106 sp_256_mont_sqr_8(r, r /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko83262622021-11-28 12:55:20 +01001107/* p256_mod - 2:
1108 * ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff - 2
1109 * Bit pattern:
1110 * 2 2 2 2 2 2 2 1...1
1111 * 5 5 4 3 2 1 0 9...0 9...1
1112 * 543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
1113 * 111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
1114 */
1115 /*if (p256_mod_minus_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001116 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
Denys Vlasenkocfb61572021-11-28 11:10:00 +01001117 sp_256_mont_mul_8(r, r, a /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001118 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +02001119}
1120
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001121/* Multiply a number by Montogmery normalizer mod modulus (prime).
1122 *
1123 * r The resulting Montgomery form number.
1124 * a The number to convert.
1125 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001126static void sp_256_mod_mul_norm_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001127{
Denys Vlasenko12040122021-04-26 20:24:34 +02001128 int64_t t[8];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001129 int32_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001130
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001131#define A(n) ((uint64_t)a[n])
Denys Vlasenko12040122021-04-26 20:24:34 +02001132 /* 1 1 0 -1 -1 -1 -1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001133 t[0] = 0 + A(0) + A(1) - A(3) - A(4) - A(5) - A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +02001134 /* 0 1 1 0 -1 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001135 t[1] = 0 + A(1) + A(2) - A(4) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001136 /* 0 0 1 1 0 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001137 t[2] = 0 + A(2) + A(3) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001138 /* -1 -1 0 2 2 1 0 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001139 t[3] = 0 - A(0) - A(1) + 2 * A(3) + 2 * A(4) + A(5) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001140 /* 0 -1 -1 0 2 2 1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001141 t[4] = 0 - A(1) - A(2) + 2 * A(4) + 2 * A(5) + A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +02001142 /* 0 0 -1 -1 0 2 2 1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001143 t[5] = 0 - A(2) - A(3) + 2 * A(5) + 2 * A(6) + A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001144 /* -1 -1 0 0 0 1 3 2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001145 t[6] = 0 - A(0) - A(1) + A(5) + 3 * A(6) + 2 * A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001146 /* 1 0 -1 -1 -1 -1 0 3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001147 t[7] = 0 + A(0) - A(2) - A(3) - A(4) - A(5) + 3 * A(7);
1148#undef A
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001149
Denys Vlasenko12040122021-04-26 20:24:34 +02001150 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
1151 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
1152 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
1153 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
1154 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
1155 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
1156 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001157 o = t[7] >> 32; //t[7] &= 0xffffffff;
Denys Vlasenko12040122021-04-26 20:24:34 +02001158 t[0] += o;
1159 t[3] -= o;
1160 t[6] -= o;
1161 t[7] += o;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001162 r[0] = (sp_digit)t[0];
1163 t[1] += t[0] >> 32;
1164 r[1] = (sp_digit)t[1];
1165 t[2] += t[1] >> 32;
1166 r[2] = (sp_digit)t[2];
1167 t[3] += t[2] >> 32;
1168 r[3] = (sp_digit)t[3];
1169 t[4] += t[3] >> 32;
1170 r[4] = (sp_digit)t[4];
1171 t[5] += t[4] >> 32;
1172 r[5] = (sp_digit)t[5];
1173 t[6] += t[5] >> 32;
1174 r[6] = (sp_digit)t[6];
1175// t[7] += t[6] >> 32;
1176// r[7] = (sp_digit)t[7];
1177 r[7] = (sp_digit)t[7] + (sp_digit)(t[6] >> 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001178}
1179
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001180/* Map the Montgomery form projective co-ordinate point to an affine point.
1181 *
1182 * r Resulting affine co-ordinate point.
1183 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001184 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001185static void sp_256_map_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001186{
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001187 sp_digit t1[8];
1188 sp_digit t2[8];
1189 sp_digit rr[2 * 8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001190
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001191 sp_256_mont_inv_8(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001192
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001193 sp_256_mont_sqr_8(t2, t1 /*, p256_mod, p256_mp_mod*/);
1194 sp_256_mont_mul_8(t1, t2, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001195
Denys Vlasenko12040122021-04-26 20:24:34 +02001196 /* x /= z^2 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001197 sp_256_mont_mul_8(rr, p->x, t2 /*, p256_mod, p256_mp_mod*/);
1198 memset(rr + 8, 0, sizeof(rr) / 2);
1199 sp_512to256_mont_reduce_8(r->x, rr /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001200 /* Reduce x to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001201 if (sp_256_cmp_8(r->x, p256_mod) >= 0)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +02001202 sp_256_sub_8_p256_mod(r->x);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001203 sp_256_norm_8(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001204
Denys Vlasenko12040122021-04-26 20:24:34 +02001205 /* y /= z^3 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001206 sp_256_mont_mul_8(rr, p->y, t1 /*, p256_mod, p256_mp_mod*/);
1207 memset(rr + 8, 0, sizeof(rr) / 2);
1208 sp_512to256_mont_reduce_8(r->y, rr /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001209 /* Reduce y to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001210 if (sp_256_cmp_8(r->y, p256_mod) >= 0)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +02001211 sp_256_sub_8_p256_mod(r->y);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001212 sp_256_norm_8(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001213
Denys Vlasenko12040122021-04-26 20:24:34 +02001214 memset(r->z, 0, sizeof(r->z));
1215 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001216}
1217
1218/* Double the Montgomery form projective point p.
1219 *
1220 * r Result of doubling point.
1221 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001222 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001223static void sp_256_proj_point_dbl_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001224{
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001225 sp_digit t1[8];
1226 sp_digit t2[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001227
Denys Vlasenko12040122021-04-26 20:24:34 +02001228 /* Put point to double into result */
1229 if (r != p)
1230 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +02001231
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001232 if (r->infinity)
Denys Vlasenkoe7305052021-10-05 13:30:48 +02001233 return;
1234
Denys Vlasenko12040122021-04-26 20:24:34 +02001235 /* T1 = Z * Z */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001236 sp_256_mont_sqr_8(t1, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001237 /* Z = Y * Z */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001238 sp_256_mont_mul_8(r->z, r->y, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001239 /* Z = 2Z */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001240 sp_256_mont_dbl_8(r->z, r->z /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001241 /* T2 = X - T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001242 sp_256_mont_sub_8(t2, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001243 /* T1 = X + T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001244 sp_256_mont_add_8(t1, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001245 /* T2 = T1 * T2 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001246 sp_256_mont_mul_8(t2, t1, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001247 /* T1 = 3T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001248 sp_256_mont_tpl_8(t1, t2 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001249 /* Y = 2Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001250 sp_256_mont_dbl_8(r->y, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001251 /* Y = Y * Y */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001252 sp_256_mont_sqr_8(r->y, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001253 /* T2 = Y * Y */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001254 sp_256_mont_sqr_8(t2, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001255 /* T2 = T2/2 */
Denys Vlasenkodcfd8d32021-11-27 16:07:42 +01001256 sp_256_div2_8(t2 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001257 /* Y = Y * X */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001258 sp_256_mont_mul_8(r->y, r->y, r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001259 /* X = T1 * T1 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001260 sp_256_mont_mul_8(r->x, t1, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001261 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001262 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001263 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001264 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001265 /* Y = Y - X */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001266 sp_256_mont_sub_8(r->y, r->y, r->x /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001267 /* Y = Y * T1 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001268 sp_256_mont_mul_8(r->y, r->y, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001269 /* Y = Y - T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001270 sp_256_mont_sub_8(r->y, r->y, t2 /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001271 dump_512("y2 %s\n", r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001272}
1273
1274/* Add two Montgomery form projective points.
1275 *
1276 * r Result of addition.
1277 * p Frist point to add.
1278 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001279 */
Denys Vlasenko53b2fdc2021-10-10 13:50:53 +02001280static NOINLINE void sp_256_proj_point_add_8(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001281{
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001282 sp_digit t1[8];
1283 sp_digit t2[8];
1284 sp_digit t3[8];
1285 sp_digit t4[8];
1286 sp_digit t5[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001287
Denys Vlasenko12040122021-04-26 20:24:34 +02001288 /* Ensure only the first point is the same as the result. */
1289 if (q == r) {
1290 sp_point* a = p;
1291 p = q;
1292 q = a;
1293 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001294
Denys Vlasenko12040122021-04-26 20:24:34 +02001295 /* Check double */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001296 sp_256_sub_8(t1, p256_mod, q->y);
1297 sp_256_norm_8(t1);
1298 if (sp_256_cmp_equal_8(p->x, q->x)
1299 && sp_256_cmp_equal_8(p->z, q->z)
1300 && (sp_256_cmp_equal_8(p->y, q->y) || sp_256_cmp_equal_8(p->y, t1))
Denys Vlasenko12040122021-04-26 20:24:34 +02001301 ) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001302 sp_256_proj_point_dbl_8(r, p);
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001303 return;
Denys Vlasenko12040122021-04-26 20:24:34 +02001304 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001305
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001306 if (p->infinity || q->infinity) {
Denys Vlasenko12040122021-04-26 20:24:34 +02001307 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001308 return;
Denys Vlasenko12040122021-04-26 20:24:34 +02001309 }
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001310
1311 /* U1 = X1*Z2^2 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001312 sp_256_mont_sqr_8(t1, q->z /*, p256_mod, p256_mp_mod*/);
1313 sp_256_mont_mul_8(t3, t1, q->z /*, p256_mod, p256_mp_mod*/);
1314 sp_256_mont_mul_8(t1, t1, r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001315 /* U2 = X2*Z1^2 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001316 sp_256_mont_sqr_8(t2, r->z /*, p256_mod, p256_mp_mod*/);
1317 sp_256_mont_mul_8(t4, t2, r->z /*, p256_mod, p256_mp_mod*/);
1318 sp_256_mont_mul_8(t2, t2, q->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001319 /* S1 = Y1*Z2^3 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001320 sp_256_mont_mul_8(t3, t3, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001321 /* S2 = Y2*Z1^3 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001322 sp_256_mont_mul_8(t4, t4, q->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001323 /* H = U2 - U1 */
1324 sp_256_mont_sub_8(t2, t2, t1 /*, p256_mod*/);
1325 /* R = S2 - S1 */
1326 sp_256_mont_sub_8(t4, t4, t3 /*, p256_mod*/);
1327 /* Z3 = H*Z1*Z2 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001328 sp_256_mont_mul_8(r->z, r->z, q->z /*, p256_mod, p256_mp_mod*/);
1329 sp_256_mont_mul_8(r->z, r->z, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001330 /* X3 = R^2 - H^3 - 2*U1*H^2 */
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001331 sp_256_mont_sqr_8(r->x, t4 /*, p256_mod, p256_mp_mod*/);
1332 sp_256_mont_sqr_8(t5, t2 /*, p256_mod, p256_mp_mod*/);
1333 sp_256_mont_mul_8(r->y, t1, t5 /*, p256_mod, p256_mp_mod*/);
1334 sp_256_mont_mul_8(t5, t5, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001335 sp_256_mont_sub_8(r->x, r->x, t5 /*, p256_mod*/);
1336 sp_256_mont_dbl_8(t1, r->y /*, p256_mod*/);
1337 sp_256_mont_sub_8(r->x, r->x, t1 /*, p256_mod*/);
1338 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
1339 sp_256_mont_sub_8(r->y, r->y, r->x /*, p256_mod*/);
Denys Vlasenkof92ae1d2021-11-27 19:15:43 +01001340 sp_256_mont_mul_8(r->y, r->y, t4 /*, p256_mod, p256_mp_mod*/);
1341 sp_256_mont_mul_8(t5, t5, t3 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkobbda85c2021-11-27 15:06:57 +01001342 sp_256_mont_sub_8(r->y, r->y, t5 /*, p256_mod*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001343}
1344
1345/* Multiply the point by the scalar and return the result.
1346 * If map is true then convert result to affine co-ordinates.
1347 *
1348 * r Resulting point.
1349 * g Point to multiply.
1350 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001351 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001352 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001353static void sp_256_ecc_mulmod_8(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001354{
Denys Vlasenko12040122021-04-26 20:24:34 +02001355 enum { map = 1 }; /* we always convert result to affine coordinates */
1356 sp_point t[3];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001357 sp_digit n = n; /* for compiler */
Denys Vlasenko12040122021-04-26 20:24:34 +02001358 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001359
Denys Vlasenko12040122021-04-26 20:24:34 +02001360 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001361
Denys Vlasenko12040122021-04-26 20:24:34 +02001362 /* t[0] = {0, 0, 1} * norm */
1363 t[0].infinity = 1;
1364 /* t[1] = {g->x, g->y, g->z} * norm */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001365 sp_256_mod_mul_norm_8(t[1].x, g->x);
1366 sp_256_mod_mul_norm_8(t[1].y, g->y);
1367 sp_256_mod_mul_norm_8(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001368
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001369 /* For every bit, starting from most significant... */
1370 k += 7;
1371 c = 256;
1372 for (;;) {
1373 if ((c & 0x1f) == 0) {
1374 if (c == 0)
Denys Vlasenko12040122021-04-26 20:24:34 +02001375 break;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001376 n = *k--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001377 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001378
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001379 y = (n >> 31);
1380 dbg("y:%d t[%d] = t[0]+t[1]\n", y, y^1);
1381 sp_256_proj_point_add_8(&t[y^1], &t[0], &t[1]);
1382 dump_512("t[0].x %s\n", t[0].x);
1383 dump_512("t[0].y %s\n", t[0].y);
1384 dump_512("t[0].z %s\n", t[0].z);
1385 dump_512("t[1].x %s\n", t[1].x);
1386 dump_512("t[1].y %s\n", t[1].y);
1387 dump_512("t[1].z %s\n", t[1].z);
1388 dbg("t[2] = t[%d]\n", y);
Denys Vlasenko26c85222021-11-27 15:00:14 +01001389 t[2] = t[y]; /* struct copy */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001390 dbg("t[2] *= 2\n");
1391 sp_256_proj_point_dbl_8(&t[2], &t[2]);
1392 dump_512("t[2].x %s\n", t[2].x);
1393 dump_512("t[2].y %s\n", t[2].y);
1394 dump_512("t[2].z %s\n", t[2].z);
Denys Vlasenko26c85222021-11-27 15:00:14 +01001395 t[y] = t[2]; /* struct copy */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001396
1397 n <<= 1;
1398 c--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001399 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001400
Denys Vlasenko12040122021-04-26 20:24:34 +02001401 if (map)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001402 sp_256_map_8(r, &t[0]);
Denys Vlasenko12040122021-04-26 20:24:34 +02001403 else
Denys Vlasenko9c671fe2021-11-27 18:42:27 +01001404 *r = t[0]; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001405
Denys Vlasenko12040122021-04-26 20:24:34 +02001406 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001407}
1408
1409/* Multiply the base point of P256 by the scalar and return the result.
1410 * If map is true then convert result to affine co-ordinates.
1411 *
1412 * r Resulting point.
1413 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001414 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001415 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001416static void sp_256_ecc_mulmod_base_8(sp_point* r, sp_digit* k /*, int map*/)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001417{
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001418 /* Since this function is called only once, save space:
1419 * don't have "static const sp_point p256_base = {...}",
1420 * it would have more zeros than data.
1421 */
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001422 static const uint8_t p256_base_bin[] = {
1423 /* x (big-endian) */
1424 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,
1425 /* y */
1426 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 +02001427 /* z will be set to 1, infinity flag to "false" */
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001428 };
1429 sp_point p256_base;
1430
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001431 sp_256_point_from_bin2x32(&p256_base, p256_base_bin);
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001432
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001433 sp_256_ecc_mulmod_8(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001434}
1435
1436/* Multiply the point by the scalar and serialize the X ordinate.
1437 * The number is 0 padded to maximum size on output.
1438 *
1439 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001440 * pub2x32 Point to multiply.
1441 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001442 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001443static void sp_ecc_secret_gen_256(const sp_digit priv[8], const uint8_t *pub2x32, uint8_t* out32)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001444{
Denys Vlasenko12040122021-04-26 20:24:34 +02001445 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001446
1447#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +02001448 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001449#endif
Denys Vlasenko12040122021-04-26 20:24:34 +02001450 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
1451 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001452
Denys Vlasenko12040122021-04-26 20:24:34 +02001453 sp_256_point_from_bin2x32(point, pub2x32);
Denys Vlasenko81d8af12021-10-05 17:31:33 +02001454 dump_512("point->x %s\n", point->x);
1455 dump_512("point->y %s\n", point->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001456
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001457 sp_256_ecc_mulmod_8(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001458
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001459 sp_256_to_bin_8(point->x, out32);
Denys Vlasenko12040122021-04-26 20:24:34 +02001460 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001461}
1462
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001463/* Generates a random scalar in [1..order-1] range. */
1464static void sp_256_ecc_gen_k_8(sp_digit k[8])
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001465{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001466 /* Since 32-bit words are "dense", no need to use
1467 * sp_256_from_bin_8(k, buf) to convert random stream
1468 * to sp_digit array - just store random bits there directly.
1469 */
1470 tls_get_random(k, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001471#if FIXED_SECRET
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001472 memset(k, 0x77, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001473#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001474
1475// If scalar is too large, try again (pseudo-code)
1476// if (k >= 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 - 1) // order of P256
1477// goto pick_another_random;
1478// k++; // ensure non-zero
1479 /* Simpler alternative, at the cost of not choosing some valid
1480 * random values, and slightly non-uniform distribution */
1481 if (k[0] == 0)
1482 k[0] = 1;
1483 if (k[7] >= 0xffffffff)
1484 k[7] = 0xfffffffe;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001485}
1486
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001487/* Makes a random EC key pair. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001488static void sp_ecc_make_key_256(sp_digit privkey[8], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001489{
1490 sp_point point[1];
1491
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001492 sp_256_ecc_gen_k_8(privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001493 dump_256("privkey %s\n", privkey);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001494 sp_256_ecc_mulmod_base_8(point, privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001495 dump_512("point->x %s\n", point->x);
1496 dump_512("point->y %s\n", point->y);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001497 sp_256_to_bin_8(point->x, pubkey);
1498 sp_256_to_bin_8(point->y, pubkey + 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001499
1500 memset(point, 0, sizeof(point)); //paranoia
1501}
1502
1503void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001504 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001505 const uint8_t *peerkey2x32)
1506{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001507 sp_digit privkey[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001508
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001509 dump_hex("peerkey2x32: %s\n", peerkey2x32, 64);
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001510 sp_ecc_make_key_256(privkey, pubkey2x32);
1511 dump_hex("pubkey: %s\n", pubkey2x32, 32);
1512 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001513
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001514 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001515 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
1516 dump_hex("premaster: %s\n", premaster32, 32);
1517}