blob: 14a7c706632a88877de22eb3723ba7269cfa12ec [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
32/* The code below is taken from parts of
33 * wolfssl-3.15.3/wolfcrypt/src/sp_c32.c
34 * and heavily modified.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020035 */
36
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020037typedef struct sp_point {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020038 sp_digit x[2 * 8];
39 sp_digit y[2 * 8];
40 sp_digit z[2 * 8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020041 int infinity;
42} sp_point;
43
44/* The modulus (prime) of the curve P256. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020045static const sp_digit p256_mod[8] = {
46 0xffffffff,0xffffffff,0xffffffff,0x00000000,
47 0x00000000,0x00000000,0x00000001,0xffffffff,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020048};
49
50#define p256_mp_mod ((sp_digit)0x000001)
51
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020052/* Normalize the values in each word to 32 bits - NOP */
53#define sp_256_norm_8(a) ((void)0)
Denys Vlasenko77145182021-10-01 13:51:39 +020054
Denys Vlasenkoe7305052021-10-05 13:30:48 +020055/* Write r as big endian to byte array.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020056 * Fixed length number of bytes written: 32
57 *
58 * r A single precision integer.
59 * a Byte array.
60 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020061static void sp_256_to_bin_8(const sp_digit* r, uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020062{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020063 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020064
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020065 sp_256_norm_8(r);
Denys Vlasenko77145182021-10-01 13:51:39 +020066
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020067 r += 8;
68 for (i = 0; i < 8; i++) {
69 r--;
70 move_to_unaligned32(a, SWAP_BE32(*r));
71 a += 4;
Denys Vlasenko12040122021-04-26 20:24:34 +020072 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020073}
74
Denys Vlasenkoe7305052021-10-05 13:30:48 +020075/* Read big endian unsigned byte array into r.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020076 *
77 * r A single precision integer.
78 * a Byte array.
79 * n Number of bytes in array to read.
80 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020081static void sp_256_from_bin_8(sp_digit* r, const uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020082{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020083 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020084
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020085 r += 8;
86 for (i = 0; i < 8; i++) {
87 sp_digit v;
88 move_from_unaligned32(v, a);
89 *--r = SWAP_BE32(v);
90 a += 4;
Denys Vlasenko12040122021-04-26 20:24:34 +020091 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020092}
93
Denys Vlasenko137864f2021-10-05 13:47:42 +020094#if SP_DEBUG
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020095static void dump_256(const char *fmt, const sp_digit* r)
Denys Vlasenko137864f2021-10-05 13:47:42 +020096{
Denys Vlasenko137864f2021-10-05 13:47:42 +020097 uint8_t b32[32];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020098 sp_256_to_bin_8(r, b32);
Denys Vlasenko137864f2021-10-05 13:47:42 +020099 dump_hex(fmt, b32, 32);
100}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200101static void dump_512(const char *fmt, const sp_digit* r)
Denys Vlasenko137864f2021-10-05 13:47:42 +0200102{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200103 uint8_t b64[64];
104 sp_256_to_bin_8(r, b64 + 32);
105 sp_256_to_bin_8(r+8, b64);
106 dump_hex(fmt, b64, 64);
Denys Vlasenko137864f2021-10-05 13:47:42 +0200107}
108#else
109# define dump_256(...) ((void)0)
110# define dump_512(...) ((void)0)
111#endif
112
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200113/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
114static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
115{
Denys Vlasenko12040122021-04-26 20:24:34 +0200116 memset(p, 0, sizeof(*p));
117 /*p->infinity = 0;*/
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200118 sp_256_from_bin_8(p->x, bin2x32);
119 sp_256_from_bin_8(p->y, bin2x32 + 32);
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200120 p->z[0] = 1; /* p->z = 1 */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200121}
122
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200123/* Compare a with b.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200124 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200125 * return -ve, 0 or +ve if a is less than, equal to or greater than b
126 * respectively.
127 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200128static signed_sp_digit sp_256_cmp_8(const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200129{
Denys Vlasenko12040122021-04-26 20:24:34 +0200130 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200131 for (i = 7; i >= 0; i--) {
132/* signed_sp_digit r = a[i] - b[i];
133 * if (r != 0)
134 * return r;
135 * does not work: think about a[i]=0, b[i]=0xffffffff
136 */
137 if (a[i] == b[i])
138 continue;
139 return (a[i] > b[i]) * 2 - 1;
Denys Vlasenko12040122021-04-26 20:24:34 +0200140 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200141 return 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200142}
143
144/* Compare two numbers to determine if they are equal.
145 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200146 * return 1 when equal and 0 otherwise.
147 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200148static int sp_256_cmp_equal_8(const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200149{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200150 return sp_256_cmp_8(a, b) == 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200151}
152
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200153/* Add b to a into r. (r = a + b). Return !0 on overflow */
154static int sp_256_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200155{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200156#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
157 sp_digit reg;
158 asm volatile (
159"\n movl (%0), %3"
160"\n addl (%1), %3"
161"\n movl %3, (%2)"
162"\n"
163"\n movl 1*4(%0), %3"
164"\n adcl 1*4(%1), %3"
165"\n movl %3, 1*4(%2)"
166"\n"
167"\n movl 2*4(%0), %3"
168"\n adcl 2*4(%1), %3"
169"\n movl %3, 2*4(%2)"
170"\n"
171"\n movl 3*4(%0), %3"
172"\n adcl 3*4(%1), %3"
173"\n movl %3, 3*4(%2)"
174"\n"
175"\n movl 4*4(%0), %3"
176"\n adcl 4*4(%1), %3"
177"\n movl %3, 4*4(%2)"
178"\n"
179"\n movl 5*4(%0), %3"
180"\n adcl 5*4(%1), %3"
181"\n movl %3, 5*4(%2)"
182"\n"
183"\n movl 6*4(%0), %3"
184"\n adcl 6*4(%1), %3"
185"\n movl %3, 6*4(%2)"
186"\n"
187"\n movl 7*4(%0), %3"
188"\n adcl 7*4(%1), %3"
189"\n movl %3, 7*4(%2)"
190"\n"
191"\n sbbl %3, %3"
192"\n"
193 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
194 : "0" (a), "1" (b), "2" (r)
195 : "memory"
196 );
197 return reg;
Denys Vlasenko911344a2021-10-06 17:17:34 +0200198#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
199 /* x86_64 has no alignment restrictions, and is little-endian,
200 * so 64-bit and 32-bit representations are identical */
201 uint64_t reg;
202 asm volatile (
203"\n movq (%0), %3"
204"\n addq (%1), %3"
205"\n movq %3, (%2)"
206"\n"
207"\n movq 1*8(%0), %3"
208"\n adcq 1*8(%1), %3"
209"\n movq %3, 1*8(%2)"
210"\n"
211"\n movq 2*8(%0), %3"
212"\n adcq 2*8(%1), %3"
213"\n movq %3, 2*8(%2)"
214"\n"
215"\n movq 3*8(%0), %3"
216"\n adcq 3*8(%1), %3"
217"\n movq %3, 3*8(%2)"
218"\n"
219"\n sbbq %3, %3"
220"\n"
221 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
222 : "0" (a), "1" (b), "2" (r)
223 : "memory"
224 );
225 return reg;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200226#else
Denys Vlasenko12040122021-04-26 20:24:34 +0200227 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200228 sp_digit carry;
229
230 carry = 0;
231 for (i = 0; i < 8; i++) {
232 sp_digit w, v;
233 w = b[i] + carry;
234 v = a[i];
235 if (w != 0) {
236 v = a[i] + w;
237 carry = (v < a[i]);
238 /* hope compiler detects above as "carry flag set" */
239 }
240 /* else: b + carry == 0, two cases:
241 * b:ffffffff, carry:1
242 * b:00000000, carry:0
243 * in either case, r[i] = a[i] and carry remains unchanged
244 */
245 r[i] = v;
246 }
247 return carry;
248#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200249}
250
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200251/* Sub b from a into r. (r = a - b). Return !0 on underflow */
252static int sp_256_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200253{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200254#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
255 sp_digit reg;
256 asm volatile (
257"\n movl (%0), %3"
258"\n subl (%1), %3"
259"\n movl %3, (%2)"
260"\n"
261"\n movl 1*4(%0), %3"
262"\n sbbl 1*4(%1), %3"
263"\n movl %3, 1*4(%2)"
264"\n"
265"\n movl 2*4(%0), %3"
266"\n sbbl 2*4(%1), %3"
267"\n movl %3, 2*4(%2)"
268"\n"
269"\n movl 3*4(%0), %3"
270"\n sbbl 3*4(%1), %3"
271"\n movl %3, 3*4(%2)"
272"\n"
273"\n movl 4*4(%0), %3"
274"\n sbbl 4*4(%1), %3"
275"\n movl %3, 4*4(%2)"
276"\n"
277"\n movl 5*4(%0), %3"
278"\n sbbl 5*4(%1), %3"
279"\n movl %3, 5*4(%2)"
280"\n"
281"\n movl 6*4(%0), %3"
282"\n sbbl 6*4(%1), %3"
283"\n movl %3, 6*4(%2)"
284"\n"
285"\n movl 7*4(%0), %3"
286"\n sbbl 7*4(%1), %3"
287"\n movl %3, 7*4(%2)"
288"\n"
289"\n sbbl %3, %3"
290"\n"
291 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
292 : "0" (a), "1" (b), "2" (r)
293 : "memory"
294 );
295 return reg;
Denys Vlasenko911344a2021-10-06 17:17:34 +0200296#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
297 /* x86_64 has no alignment restrictions, and is little-endian,
298 * so 64-bit and 32-bit representations are identical */
299 uint64_t reg;
300 asm volatile (
301"\n movq (%0), %3"
302"\n subq (%1), %3"
303"\n movq %3, (%2)"
304"\n"
305"\n movq 1*8(%0), %3"
306"\n sbbq 1*8(%1), %3"
307"\n movq %3, 1*8(%2)"
308"\n"
309"\n movq 2*8(%0), %3"
310"\n sbbq 2*8(%1), %3"
311"\n movq %3, 2*8(%2)"
312"\n"
313"\n movq 3*8(%0), %3"
314"\n sbbq 3*8(%1), %3"
315"\n movq %3, 3*8(%2)"
316"\n"
317"\n sbbq %3, %3"
318"\n"
319 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
320 : "0" (a), "1" (b), "2" (r)
321 : "memory"
322 );
323 return reg;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200324#else
Denys Vlasenko12040122021-04-26 20:24:34 +0200325 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200326 sp_digit borrow;
327
328 borrow = 0;
329 for (i = 0; i < 8; i++) {
330 sp_digit w, v;
331 w = b[i] + borrow;
332 v = a[i];
333 if (w != 0) {
334 v = a[i] - w;
335 borrow = (v > a[i]);
336 /* hope compiler detects above as "carry flag set" */
337 }
338 /* else: b + borrow == 0, two cases:
339 * b:ffffffff, borrow:1
340 * b:00000000, borrow:0
341 * in either case, r[i] = a[i] and borrow remains unchanged
342 */
343 r[i] = v;
344 }
345 return borrow;
346#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200347}
348
Denys Vlasenko567eefc2021-10-06 14:25:10 +0200349#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200350/* Sub p256_mod from a into r. (r = a - p256_mod). */
351static void sp_256_sub_8_p256_mod(sp_digit* r, const sp_digit* a)
352{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200353 sp_digit reg;
354//p256_mod[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
355 asm volatile (
356"\n movl (%0), %2"
357"\n subl $0xffffffff, %2"
358"\n movl %2, (%1)"
359"\n"
360"\n movl 1*4(%0), %2"
361"\n sbbl $0xffffffff, %2"
362"\n movl %2, 1*4(%1)"
363"\n"
364"\n movl 2*4(%0), %2"
365"\n sbbl $0xffffffff, %2"
366"\n movl %2, 2*4(%1)"
367"\n"
368"\n movl 3*4(%0), %2"
369"\n sbbl $0, %2"
370"\n movl %2, 3*4(%1)"
371"\n"
372"\n movl 4*4(%0), %2"
373"\n sbbl $0, %2"
374"\n movl %2, 4*4(%1)"
375"\n"
376"\n movl 5*4(%0), %2"
377"\n sbbl $0, %2"
378"\n movl %2, 5*4(%1)"
379"\n"
380"\n movl 6*4(%0), %2"
381"\n sbbl $1, %2"
382"\n movl %2, 6*4(%1)"
383"\n"
384"\n movl 7*4(%0), %2"
385"\n sbbl $0xffffffff, %2"
386"\n movl %2, 7*4(%1)"
387"\n"
388 : "=r" (a), "=r" (r), "=r" (reg)
389 : "0" (a), "1" (r)
390 : "memory"
391 );
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200392}
Denys Vlasenko567eefc2021-10-06 14:25:10 +0200393#else
394# define sp_256_sub_8_p256_mod(r, a) sp_256_sub_8((r), (a), p256_mod)
395#endif
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200396
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200397/* Multiply a and b into r. (r = a * b) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200398static void sp_256_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200399{
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200400#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
401 sp_digit rr[15]; /* in case r coincides with a or b */
402 int k;
403 uint32_t accl;
404 uint32_t acch;
405
406 acch = accl = 0;
407 for (k = 0; k < 15; k++) {
408 int i, j;
409 uint32_t acc_hi;
410 i = k - 7;
411 if (i < 0)
412 i = 0;
413 j = k - i;
414 acc_hi = 0;
415 do {
416////////////////////////
417// uint64_t m = ((uint64_t)a[i]) * b[j];
418// acc_hi:acch:accl += m;
419 asm volatile (
420 // a[i] is already loaded in %%eax
421"\n mull %7"
422"\n addl %%eax, %0"
423"\n adcl %%edx, %1"
424"\n adcl $0, %2"
425 : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi)
426 : "0" (accl), "1" (acch), "2" (acc_hi), "a" (a[i]), "m" (b[j])
427 : "cc", "dx"
428 );
429////////////////////////
430 j--;
431 i++;
432 } while (i != 8 && i <= k);
433 rr[k] = accl;
434 accl = acch;
435 acch = acc_hi;
436 }
437 r[15] = accl;
438 memcpy(r, rr, sizeof(rr));
Denys Vlasenko911344a2021-10-06 17:17:34 +0200439#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
440 /* x86_64 has no alignment restrictions, and is little-endian,
441 * so 64-bit and 32-bit representations are identical */
442 const uint64_t* aa = (const void*)a;
443 const uint64_t* bb = (const void*)b;
444 uint64_t rr[8];
445 int k;
446 uint64_t accl;
447 uint64_t acch;
448
449 acch = accl = 0;
450 for (k = 0; k < 7; k++) {
451 int i, j;
452 uint64_t acc_hi;
453 i = k - 3;
454 if (i < 0)
455 i = 0;
456 j = k - i;
457 acc_hi = 0;
458 do {
459////////////////////////
460// uint128_t m = ((uint128_t)a[i]) * b[j];
461// acc_hi:acch:accl += m;
462 asm volatile (
463 // aa[i] is already loaded in %%rax
464"\n mulq %7"
465"\n addq %%rax, %0"
466"\n adcq %%rdx, %1"
467"\n adcq $0, %2"
468 : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi)
469 : "0" (accl), "1" (acch), "2" (acc_hi), "a" (aa[i]), "m" (bb[j])
470 : "cc", "dx"
471 );
472////////////////////////
473 j--;
474 i++;
475 } while (i != 4 && i <= k);
476 rr[k] = accl;
477 accl = acch;
478 acch = acc_hi;
479 }
480 rr[7] = accl;
481 memcpy(r, rr, sizeof(rr));
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200482#elif 0
483 //TODO: arm assembly (untested)
484 sp_digit tmp[16];
485
486 asm volatile (
487"\n mov r5, #0"
488"\n mov r6, #0"
489"\n mov r7, #0"
490"\n mov r8, #0"
491"\n 1:"
492"\n subs r3, r5, #28"
493"\n movcc r3, #0"
494"\n sub r4, r5, r3"
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200495"\n 2:"
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200496"\n ldr r14, [%[a], r3]"
497"\n ldr r12, [%[b], r4]"
498"\n umull r9, r10, r14, r12"
499"\n adds r6, r6, r9"
500"\n adcs r7, r7, r10"
501"\n adc r8, r8, #0"
502"\n add r3, r3, #4"
503"\n sub r4, r4, #4"
504"\n cmp r3, #32"
505"\n beq 3f"
506"\n cmp r3, r5"
507"\n ble 2b"
508"\n 3:"
509"\n str r6, [%[r], r5]"
510"\n mov r6, r7"
511"\n mov r7, r8"
512"\n mov r8, #0"
513"\n add r5, r5, #4"
514"\n cmp r5, #56"
515"\n ble 1b"
516"\n str r6, [%[r], r5]"
517 : [r] "r" (tmp), [a] "r" (a), [b] "r" (b)
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200518 : "memory", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14"
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200519 );
520 memcpy(r, tmp, sizeof(tmp));
521#else
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200522 sp_digit rr[15]; /* in case r coincides with a or b */
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200523 int i, j, k;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200524 uint64_t acc;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200525
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200526 acc = 0;
527 for (k = 0; k < 15; k++) {
528 uint32_t acc_hi;
529 i = k - 7;
530 if (i < 0)
531 i = 0;
532 j = k - i;
533 acc_hi = 0;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200534 do {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200535 uint64_t m = ((uint64_t)a[i]) * b[j];
536 acc += m;
537 if (acc < m)
538 acc_hi++;
539 j--;
540 i++;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200541 } while (i != 8 && i <= k);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200542 rr[k] = acc;
543 acc = (acc >> 32) | ((uint64_t)acc_hi << 32);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200544 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200545 r[15] = acc;
546 memcpy(r, rr, sizeof(rr));
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200547#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200548}
549
Denys Vlasenko389329e2021-10-05 13:39:33 +0200550/* Shift number right one bit. Bottom bit is lost. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200551static void sp_256_rshift1_8(sp_digit* r, sp_digit* a, sp_digit carry)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200552{
553 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200554
555 carry = (!!carry << 31);
556 for (i = 7; i >= 0; i--) {
557 sp_digit c = a[i] << 31;
558 r[i] = (a[i] >> 1) | carry;
559 carry = c;
560 }
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200561}
562
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200563/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200564static void sp_256_div2_8(sp_digit* r, const sp_digit* a, const sp_digit* m)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200565{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200566 int carry = 0;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200567 if (a[0] & 1)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200568 carry = sp_256_add_8(r, a, m);
569 sp_256_norm_8(r);
570 sp_256_rshift1_8(r, r, carry);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200571}
572
573/* Add two Montgomery form numbers (r = a + b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200574static void sp_256_mont_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b
575 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200576{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200577// const sp_digit* m = p256_mod;
578
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200579 int carry = sp_256_add_8(r, a, b);
580 sp_256_norm_8(r);
581 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200582 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200583 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200584 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200585}
586
587/* Subtract two Montgomery form numbers (r = a - b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200588static void sp_256_mont_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b
589 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200590{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200591 const sp_digit* m = p256_mod;
592
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200593 int borrow;
594 borrow = sp_256_sub_8(r, a, b);
595 sp_256_norm_8(r);
596 if (borrow) {
597 sp_256_add_8(r, r, m);
598 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200599 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200600}
601
602/* Double a Montgomery form number (r = a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200603static void sp_256_mont_dbl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200604{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200605// const sp_digit* m = p256_mod;
606
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200607 int carry = sp_256_add_8(r, a, a);
608 sp_256_norm_8(r);
609 if (carry)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200610 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200611 sp_256_norm_8(r);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200612}
613
614/* Triple a Montgomery form number (r = a + a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200615static void sp_256_mont_tpl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200616{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200617// const sp_digit* m = p256_mod;
618
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200619 int carry = sp_256_add_8(r, a, a);
620 sp_256_norm_8(r);
621 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200622 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200623 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200624 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200625 carry = sp_256_add_8(r, r, a);
626 sp_256_norm_8(r);
627 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200628 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200629 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200630 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200631}
632
633/* Shift the result in the high 256 bits down to the bottom. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200634static void sp_256_mont_shift_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200635{
636 int i;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200637
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200638 for (i = 0; i < 8; i++) {
639 r[i] = a[i+8];
640 r[i+8] = 0;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200641 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200642}
643
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200644/* Mul a by scalar b and add into r. (r += a * b) */
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200645static int sp_256_mul_add_8(sp_digit* r /*, const sp_digit* a, sp_digit b*/)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200646{
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200647// const sp_digit* a = p256_mod;
648//a[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
649 sp_digit b = r[0];
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200650
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200651 uint64_t t;
652
653// t = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200654// for (i = 0; i < 8; i++) {
655// uint32_t t_hi;
656// uint64_t m = ((uint64_t)b * a[i]) + r[i];
657// t += m;
658// t_hi = (t < m);
659// r[i] = (sp_digit)t;
660// t = (t >> 32) | ((uint64_t)t_hi << 32);
661// }
662// r[8] += (sp_digit)t;
663
664 // Unroll, then optimize the above loop:
665 //uint32_t t_hi;
666 uint64_t m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200667 uint32_t t32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200668
669 //m = ((uint64_t)b * a[0]) + r[0];
670 // Since b is r[0] and a[0] is ffffffff, the above optimizes to:
671 // m = r[0] * ffffffff + r[0] = (r[0] * 100000000 - r[0]) + r[0] = r[0] << 32;
672 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200673 // t = r[0] << 32 = b << 32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200674 //t_hi = (t < m);
675 // t_hi = 0;
676 //r[0] = (sp_digit)t;
677 r[0] = 0;
678 //t = (t >> 32) | ((uint64_t)t_hi << 32);
679 // t = b;
680
681 //m = ((uint64_t)b * a[1]) + r[1];
682 // Since a[1] is ffffffff, the above optimizes to:
683 // m = b * ffffffff + r[1] = (b * 100000000 - b) + r[1] = (b << 32) - b + r[1];
684 //t += m;
685 // t = b + (b << 32) - b + r[1] = (b << 32) + r[1];
686 //t_hi = (t < m);
687 // t_hi = 0;
688 //r[1] = (sp_digit)t;
689 // r[1] = r[1];
690 //t = (t >> 32) | ((uint64_t)t_hi << 32);
691 // t = b;
692
693 //m = ((uint64_t)b * a[2]) + r[2];
694 // Since a[2] is ffffffff, the above optimizes to:
695 // m = b * ffffffff + r[2] = (b * 100000000 - b) + r[2] = (b << 32) - b + r[2];
696 //t += m;
697 // t = b + (b << 32) - b + r[2] = (b << 32) + r[2]
698 //t_hi = (t < m);
699 // t_hi = 0;
700 //r[2] = (sp_digit)t;
701 // r[2] = r[2];
702 //t = (t >> 32) | ((uint64_t)t_hi << 32);
703 // t = b;
704
705 //m = ((uint64_t)b * a[3]) + r[3];
706 // Since a[3] is 00000000, the above optimizes to:
707 // m = b * 0 + r[3] = r[3];
708 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200709 // t = b + r[3];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200710 //t_hi = (t < m);
711 // t_hi = 0;
712 //r[3] = (sp_digit)t;
713 r[3] = r[3] + b;
714 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200715 t32 = (r[3] < b); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200716
717 //m = ((uint64_t)b * a[4]) + r[4];
718 // Since a[4] is 00000000, the above optimizes to:
719 // m = b * 0 + r[4] = r[4];
720 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200721 // t = t32 + r[4];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200722 //t_hi = (t < m);
723 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200724 //r[4] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200725 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200726 if (t32 != 0) {
727 r[4]++;
728 t32 = (r[4] == 0); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200729
730 //m = ((uint64_t)b * a[5]) + r[5];
731 // Since a[5] is 00000000, the above optimizes to:
732 // m = b * 0 + r[5] = r[5];
733 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200734 // t = t32 + r[5]; (t32 is 0 or 1)
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200735 //t_hi = (t < m);
736 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200737 //r[5] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200738 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200739 if (t32 != 0) {
740 r[5]++;
741 t32 = (r[5] == 0); // 0 or 1
742 }
743 }
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200744
745 //m = ((uint64_t)b * a[6]) + r[6];
746 // Since a[6] is 00000001, the above optimizes to:
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200747 // m = (uint64_t)b + r[6]; // 33 bits at most
748 //t += m;
749 t = t32 + (uint64_t)b + r[6];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200750 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200751 // t_hi = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200752 r[6] = (sp_digit)t;
753 //t = (t >> 32) | ((uint64_t)t_hi << 32);
754 t = (t >> 32);
755
756 //m = ((uint64_t)b * a[7]) + r[7];
757 // Since a[7] is ffffffff, the above optimizes to:
758 // m = b * ffffffff + r[7] = (b * 100000000 - b) + r[7]
759 m = ((uint64_t)b << 32) - b + r[7];
760 t += m;
761 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200762 // 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 +0200763 r[7] = (sp_digit)t;
764 //t = (t >> 32) | ((uint64_t)t_hi << 32);
765 t = (t >> 32);
766
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200767 r[8] += (sp_digit)t;
768 return (r[8] < (sp_digit)t); /* 1 if addition overflowed */
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200769}
770
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200771/* Reduce the number back to 256 bits using Montgomery reduction.
772 *
773 * a A single precision number to reduce in place.
774 * m The single precision number representing the modulus.
775 * mp The digit representing the negative inverse of m mod 2^n.
776 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200777static void sp_256_mont_reduce_8(sp_digit* a/*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200778{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200779// const sp_digit* m = p256_mod;
Denys Vlasenko389329e2021-10-05 13:39:33 +0200780 sp_digit mp = p256_mp_mod;
781
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200782 int i;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200783// sp_digit mu;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200784
785 if (mp != 1) {
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200786 sp_digit word16th = 0;
787 for (i = 0; i < 8; i++) {
788// mu = (sp_digit)(a[i] * mp);
789 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
790 int j = i + 8;
791 inc_next_word0:
792 if (++j > 15) { /* a[16] array has no more words? */
793 word16th++;
794 continue;
795 }
796 if (++a[j] == 0) /* did this overflow too? */
797 goto inc_next_word0;
798 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200799 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200800 sp_256_mont_shift_8(a, a);
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200801 if (word16th != 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200802 sp_256_sub_8_p256_mod(a, a /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200803 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200804 }
Denys Vlasenko389329e2021-10-05 13:39:33 +0200805 else { /* Same code for explicit mp == 1 (which is always the case for P256) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200806 sp_digit word16th = 0;
807 for (i = 0; i < 8; i++) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200808 /*mu = a[i];*/
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200809 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200810 int j = i + 8;
811 inc_next_word:
812 if (++j > 15) { /* a[16] array has no more words? */
813 word16th++;
814 continue;
815 }
816 if (++a[j] == 0) /* did this overflow too? */
817 goto inc_next_word;
818 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200819 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200820 sp_256_mont_shift_8(a, a);
821 if (word16th != 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200822 sp_256_sub_8_p256_mod(a, a /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200823 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200824 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200825}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200826#if 0
827//TODO: arm32 asm (also adapt for x86?)
828static void sp_256_mont_reduce_8(sp_digit* a, sp_digit* m, sp_digit mp)
829{
830 sp_digit ca = 0;
831
832 asm volatile (
833 # i = 0
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200834 mov r12, #0
835 ldr r10, [%[a], #0]
836 ldr r14, [%[a], #4]
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02008371:
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200838 # mu = a[i] * mp
839 mul r8, %[mp], r10
840 # a[i+0] += m[0] * mu
841 ldr r7, [%[m], #0]
842 ldr r9, [%[a], #0]
843 umull r6, r7, r8, r7
844 adds r10, r10, r6
845 adc r5, r7, #0
846 # a[i+1] += m[1] * mu
847 ldr r7, [%[m], #4]
848 ldr r9, [%[a], #4]
849 umull r6, r7, r8, r7
850 adds r10, r14, r6
851 adc r4, r7, #0
852 adds r10, r10, r5
853 adc r4, r4, #0
854 # a[i+2] += m[2] * mu
855 ldr r7, [%[m], #8]
856 ldr r14, [%[a], #8]
857 umull r6, r7, r8, r7
858 adds r14, r14, r6
859 adc r5, r7, #0
860 adds r14, r14, r4
861 adc r5, r5, #0
862 # a[i+3] += m[3] * mu
863 ldr r7, [%[m], #12]
864 ldr r9, [%[a], #12]
865 umull r6, r7, r8, r7
866 adds r9, r9, r6
867 adc r4, r7, #0
868 adds r9, r9, r5
869 str r9, [%[a], #12]
870 adc r4, r4, #0
871 # a[i+4] += m[4] * mu
872 ldr r7, [%[m], #16]
873 ldr r9, [%[a], #16]
874 umull r6, r7, r8, r7
875 adds r9, r9, r6
876 adc r5, r7, #0
877 adds r9, r9, r4
878 str r9, [%[a], #16]
879 adc r5, r5, #0
880 # a[i+5] += m[5] * mu
881 ldr r7, [%[m], #20]
882 ldr r9, [%[a], #20]
883 umull r6, r7, r8, r7
884 adds r9, r9, r6
885 adc r4, r7, #0
886 adds r9, r9, r5
887 str r9, [%[a], #20]
888 adc r4, r4, #0
889 # a[i+6] += m[6] * mu
890 ldr r7, [%[m], #24]
891 ldr r9, [%[a], #24]
892 umull r6, r7, r8, r7
893 adds r9, r9, r6
894 adc r5, r7, #0
895 adds r9, r9, r4
896 str r9, [%[a], #24]
897 adc r5, r5, #0
898 # a[i+7] += m[7] * mu
899 ldr r7, [%[m], #28]
900 ldr r9, [%[a], #28]
901 umull r6, r7, r8, r7
902 adds r5, r5, r6
903 adcs r7, r7, %[ca]
904 mov %[ca], #0
905 adc %[ca], %[ca], %[ca]
906 adds r9, r9, r5
907 str r9, [%[a], #28]
908 ldr r9, [%[a], #32]
909 adcs r9, r9, r7
910 str r9, [%[a], #32]
911 adc %[ca], %[ca], #0
912 # i += 1
913 add %[a], %[a], #4
914 add r12, r12, #4
915 cmp r12, #32
916 blt 1b
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200917
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200918 str r10, [%[a], #0]
919 str r14, [%[a], #4]
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200920 : [ca] "+r" (ca), [a] "+r" (a)
921 : [m] "r" (m), [mp] "r" (mp)
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200922 : "memory", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14"
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200923 );
924
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200925 memcpy(a, a + 8, 32);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200926 if (ca)
927 a -= m;
928}
929#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200930
931/* Multiply two Montogmery form numbers mod the modulus (prime).
932 * (r = a * b mod m)
933 *
934 * r Result of multiplication.
935 * a First number to multiply in Montogmery form.
936 * b Second number to multiply in Montogmery form.
937 * m Modulus (prime).
938 * mp Montogmery mulitplier.
939 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200940static void sp_256_mont_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b
Denys Vlasenko389329e2021-10-05 13:39:33 +0200941 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200942{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200943 //const sp_digit* m = p256_mod;
944 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200945 sp_256_mul_8(r, a, b);
946 sp_256_mont_reduce_8(r /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200947}
948
949/* Square the Montgomery form number. (r = a * a mod m)
950 *
951 * r Result of squaring.
952 * a Number to square in Montogmery form.
953 * m Modulus (prime).
954 * mp Montogmery mulitplier.
955 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200956static void sp_256_mont_sqr_8(sp_digit* r, const sp_digit* a
Denys Vlasenko389329e2021-10-05 13:39:33 +0200957 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200958{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200959 //const sp_digit* m = p256_mod;
960 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200961 sp_256_mont_mul_8(r, a, a /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200962}
963
964/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
965 * P256 curve. (r = 1 / a mod m)
966 *
967 * r Inverse result.
968 * a Number to invert.
969 */
970#if 0
971/* Mod-2 for the P256 curve. */
972static const uint32_t p256_mod_2[8] = {
973 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
974 0x00000000,0x00000000,0x00000001,0xffffffff,
975};
976//Bit pattern:
977//2 2 2 2 2 2 2 1...1
978//5 5 4 3 2 1 0 9...0 9...1
979//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
980//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
981#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200982static void sp_256_mont_inv_8(sp_digit* r, sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200983{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200984 sp_digit t[2*8]; //can be just [8]?
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200985 int i;
986
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200987 memcpy(t, a, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200988 for (i = 254; i >= 0; i--) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200989 sp_256_mont_sqr_8(t, t /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200990 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
991 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200992 sp_256_mont_mul_8(t, t, a /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200993 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200994 memcpy(r, t, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200995}
996
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200997/* Multiply a number by Montogmery normalizer mod modulus (prime).
998 *
999 * r The resulting Montgomery form number.
1000 * a The number to convert.
1001 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001002static void sp_256_mod_mul_norm_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001003{
Denys Vlasenko12040122021-04-26 20:24:34 +02001004 int64_t t[8];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001005 int32_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001006
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001007#define A(n) ((uint64_t)a[n])
Denys Vlasenko12040122021-04-26 20:24:34 +02001008 /* 1 1 0 -1 -1 -1 -1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001009 t[0] = 0 + A(0) + A(1) - A(3) - A(4) - A(5) - A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +02001010 /* 0 1 1 0 -1 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001011 t[1] = 0 + A(1) + A(2) - A(4) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001012 /* 0 0 1 1 0 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001013 t[2] = 0 + A(2) + A(3) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001014 /* -1 -1 0 2 2 1 0 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001015 t[3] = 0 - A(0) - A(1) + 2 * A(3) + 2 * A(4) + A(5) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001016 /* 0 -1 -1 0 2 2 1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001017 t[4] = 0 - A(1) - A(2) + 2 * A(4) + 2 * A(5) + A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +02001018 /* 0 0 -1 -1 0 2 2 1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001019 t[5] = 0 - A(2) - A(3) + 2 * A(5) + 2 * A(6) + A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001020 /* -1 -1 0 0 0 1 3 2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001021 t[6] = 0 - A(0) - A(1) + A(5) + 3 * A(6) + 2 * A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001022 /* 1 0 -1 -1 -1 -1 0 3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001023 t[7] = 0 + A(0) - A(2) - A(3) - A(4) - A(5) + 3 * A(7);
1024#undef A
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001025
Denys Vlasenko12040122021-04-26 20:24:34 +02001026 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
1027 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
1028 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
1029 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
1030 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
1031 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
1032 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001033 o = t[7] >> 32; //t[7] &= 0xffffffff;
Denys Vlasenko12040122021-04-26 20:24:34 +02001034 t[0] += o;
1035 t[3] -= o;
1036 t[6] -= o;
1037 t[7] += o;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001038 r[0] = (sp_digit)t[0];
1039 t[1] += t[0] >> 32;
1040 r[1] = (sp_digit)t[1];
1041 t[2] += t[1] >> 32;
1042 r[2] = (sp_digit)t[2];
1043 t[3] += t[2] >> 32;
1044 r[3] = (sp_digit)t[3];
1045 t[4] += t[3] >> 32;
1046 r[4] = (sp_digit)t[4];
1047 t[5] += t[4] >> 32;
1048 r[5] = (sp_digit)t[5];
1049 t[6] += t[5] >> 32;
1050 r[6] = (sp_digit)t[6];
1051// t[7] += t[6] >> 32;
1052// r[7] = (sp_digit)t[7];
1053 r[7] = (sp_digit)t[7] + (sp_digit)(t[6] >> 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001054}
1055
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001056/* Map the Montgomery form projective co-ordinate point to an affine point.
1057 *
1058 * r Resulting affine co-ordinate point.
1059 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001060 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001061static void sp_256_map_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001062{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001063 sp_digit t1[2*8];
1064 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001065
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001066 sp_256_mont_inv_8(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001067
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001068 sp_256_mont_sqr_8(t2, t1 /*, p256_mod, p256_mp_mod*/);
1069 sp_256_mont_mul_8(t1, t2, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001070
Denys Vlasenko12040122021-04-26 20:24:34 +02001071 /* x /= z^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001072 sp_256_mont_mul_8(r->x, p->x, t2 /*, p256_mod, p256_mp_mod*/);
1073 memset(r->x + 8, 0, sizeof(r->x) / 2);
1074 sp_256_mont_reduce_8(r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001075 /* Reduce x to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001076 if (sp_256_cmp_8(r->x, p256_mod) >= 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001077 sp_256_sub_8_p256_mod(r->x, r->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001078 sp_256_norm_8(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001079
Denys Vlasenko12040122021-04-26 20:24:34 +02001080 /* y /= z^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001081 sp_256_mont_mul_8(r->y, p->y, t1 /*, p256_mod, p256_mp_mod*/);
1082 memset(r->y + 8, 0, sizeof(r->y) / 2);
1083 sp_256_mont_reduce_8(r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001084 /* Reduce y to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001085 if (sp_256_cmp_8(r->y, p256_mod) >= 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001086 sp_256_sub_8_p256_mod(r->y, r->y /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001087 sp_256_norm_8(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001088
Denys Vlasenko12040122021-04-26 20:24:34 +02001089 memset(r->z, 0, sizeof(r->z));
1090 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001091}
1092
1093/* Double the Montgomery form projective point p.
1094 *
1095 * r Result of doubling point.
1096 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001097 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001098static void sp_256_proj_point_dbl_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001099{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001100 sp_digit t1[2*8];
1101 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001102
Denys Vlasenko12040122021-04-26 20:24:34 +02001103 /* Put point to double into result */
1104 if (r != p)
1105 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +02001106
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001107 if (r->infinity)
Denys Vlasenkoe7305052021-10-05 13:30:48 +02001108 return;
1109
Denys Vlasenko55578f22021-10-05 19:45:56 +02001110 if (SP_DEBUG) {
1111 /* unused part of t2, may result in spurios
1112 * differences in debug output. Clear it.
1113 */
1114 memset(t2, 0, sizeof(t2));
1115 }
1116
Denys Vlasenko12040122021-04-26 20:24:34 +02001117 /* T1 = Z * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001118 sp_256_mont_sqr_8(t1, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001119 /* Z = Y * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001120 sp_256_mont_mul_8(r->z, r->y, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001121 /* Z = 2Z */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001122 sp_256_mont_dbl_8(r->z, r->z /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001123 /* T2 = X - T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001124 sp_256_mont_sub_8(t2, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001125 /* T1 = X + T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001126 sp_256_mont_add_8(t1, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001127 /* T2 = T1 * T2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001128 sp_256_mont_mul_8(t2, t1, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001129 /* T1 = 3T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001130 sp_256_mont_tpl_8(t1, t2 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001131 /* Y = 2Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001132 sp_256_mont_dbl_8(r->y, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001133 /* Y = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001134 sp_256_mont_sqr_8(r->y, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001135 /* T2 = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001136 sp_256_mont_sqr_8(t2, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001137 /* T2 = T2/2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001138 sp_256_div2_8(t2, t2, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +02001139 /* Y = Y * X */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001140 sp_256_mont_mul_8(r->y, r->y, r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001141 /* X = T1 * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001142 sp_256_mont_mul_8(r->x, t1, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001143 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001144 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001145 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001146 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001147 /* Y = Y - X */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001148 sp_256_mont_sub_8(r->y, r->y, r->x /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001149 /* Y = Y * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001150 sp_256_mont_mul_8(r->y, r->y, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001151 /* Y = Y - T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001152 sp_256_mont_sub_8(r->y, r->y, t2 /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001153 dump_512("y2 %s\n", r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001154}
1155
1156/* Add two Montgomery form projective points.
1157 *
1158 * r Result of addition.
1159 * p Frist point to add.
1160 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001161 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001162static void sp_256_proj_point_add_8(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001163{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001164 sp_digit t1[2*8];
1165 sp_digit t2[2*8];
1166 sp_digit t3[2*8];
1167 sp_digit t4[2*8];
1168 sp_digit t5[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001169
Denys Vlasenko12040122021-04-26 20:24:34 +02001170 /* Ensure only the first point is the same as the result. */
1171 if (q == r) {
1172 sp_point* a = p;
1173 p = q;
1174 q = a;
1175 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001176
Denys Vlasenko12040122021-04-26 20:24:34 +02001177 /* Check double */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001178 sp_256_sub_8(t1, p256_mod, q->y);
1179 sp_256_norm_8(t1);
1180 if (sp_256_cmp_equal_8(p->x, q->x)
1181 && sp_256_cmp_equal_8(p->z, q->z)
1182 && (sp_256_cmp_equal_8(p->y, q->y) || sp_256_cmp_equal_8(p->y, t1))
Denys Vlasenko12040122021-04-26 20:24:34 +02001183 ) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001184 sp_256_proj_point_dbl_8(r, p);
Denys Vlasenko12040122021-04-26 20:24:34 +02001185 }
1186 else {
1187 sp_point tp;
1188 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001189
Denys Vlasenko12040122021-04-26 20:24:34 +02001190 v = r;
1191 if (p->infinity | q->infinity) {
1192 memset(&tp, 0, sizeof(tp));
1193 v = &tp;
1194 }
Denys Vlasenko772e1872021-04-26 17:25:27 +02001195
Denys Vlasenko12040122021-04-26 20:24:34 +02001196 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001197
Denys Vlasenko12040122021-04-26 20:24:34 +02001198 /* U1 = X1*Z2^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001199 sp_256_mont_sqr_8(t1, q->z /*, p256_mod, p256_mp_mod*/);
1200 sp_256_mont_mul_8(t3, t1, q->z /*, p256_mod, p256_mp_mod*/);
1201 sp_256_mont_mul_8(t1, t1, v->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001202 /* U2 = X2*Z1^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001203 sp_256_mont_sqr_8(t2, v->z /*, p256_mod, p256_mp_mod*/);
1204 sp_256_mont_mul_8(t4, t2, v->z /*, p256_mod, p256_mp_mod*/);
1205 sp_256_mont_mul_8(t2, t2, q->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001206 /* S1 = Y1*Z2^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001207 sp_256_mont_mul_8(t3, t3, v->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001208 /* S2 = Y2*Z1^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001209 sp_256_mont_mul_8(t4, t4, q->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001210 /* H = U2 - U1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001211 sp_256_mont_sub_8(t2, t2, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001212 /* R = S2 - S1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001213 sp_256_mont_sub_8(t4, t4, t3 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001214 /* Z3 = H*Z1*Z2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001215 sp_256_mont_mul_8(v->z, v->z, q->z /*, p256_mod, p256_mp_mod*/);
1216 sp_256_mont_mul_8(v->z, v->z, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001217 /* X3 = R^2 - H^3 - 2*U1*H^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001218 sp_256_mont_sqr_8(v->x, t4 /*, p256_mod, p256_mp_mod*/);
1219 sp_256_mont_sqr_8(t5, t2 /*, p256_mod, p256_mp_mod*/);
1220 sp_256_mont_mul_8(v->y, t1, t5 /*, p256_mod, p256_mp_mod*/);
1221 sp_256_mont_mul_8(t5, t5, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001222 sp_256_mont_sub_8(v->x, v->x, t5 /*, p256_mod*/);
1223 sp_256_mont_dbl_8(t1, v->y /*, p256_mod*/);
1224 sp_256_mont_sub_8(v->x, v->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001225 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001226 sp_256_mont_sub_8(v->y, v->y, v->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001227 sp_256_mont_mul_8(v->y, v->y, t4 /*, p256_mod, p256_mp_mod*/);
1228 sp_256_mont_mul_8(t5, t5, t3 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001229 sp_256_mont_sub_8(v->y, v->y, t5 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001230 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001231}
1232
1233/* Multiply the point by the scalar and return the result.
1234 * If map is true then convert result to affine co-ordinates.
1235 *
1236 * r Resulting point.
1237 * g Point to multiply.
1238 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001239 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001240 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001241static 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 +02001242{
Denys Vlasenko12040122021-04-26 20:24:34 +02001243 enum { map = 1 }; /* we always convert result to affine coordinates */
1244 sp_point t[3];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001245 sp_digit n = n; /* for compiler */
Denys Vlasenko12040122021-04-26 20:24:34 +02001246 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001247
Denys Vlasenko12040122021-04-26 20:24:34 +02001248 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001249
Denys Vlasenko12040122021-04-26 20:24:34 +02001250 /* t[0] = {0, 0, 1} * norm */
1251 t[0].infinity = 1;
1252 /* t[1] = {g->x, g->y, g->z} * norm */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001253 sp_256_mod_mul_norm_8(t[1].x, g->x);
1254 sp_256_mod_mul_norm_8(t[1].y, g->y);
1255 sp_256_mod_mul_norm_8(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001256
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001257 /* For every bit, starting from most significant... */
1258 k += 7;
1259 c = 256;
1260 for (;;) {
1261 if ((c & 0x1f) == 0) {
1262 if (c == 0)
Denys Vlasenko12040122021-04-26 20:24:34 +02001263 break;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001264 n = *k--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001265 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001266
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001267 y = (n >> 31);
1268 dbg("y:%d t[%d] = t[0]+t[1]\n", y, y^1);
1269 sp_256_proj_point_add_8(&t[y^1], &t[0], &t[1]);
1270 dump_512("t[0].x %s\n", t[0].x);
1271 dump_512("t[0].y %s\n", t[0].y);
1272 dump_512("t[0].z %s\n", t[0].z);
1273 dump_512("t[1].x %s\n", t[1].x);
1274 dump_512("t[1].y %s\n", t[1].y);
1275 dump_512("t[1].z %s\n", t[1].z);
1276 dbg("t[2] = t[%d]\n", y);
Denys Vlasenko12040122021-04-26 20:24:34 +02001277 memcpy(&t[2], &t[y], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001278 dbg("t[2] *= 2\n");
1279 sp_256_proj_point_dbl_8(&t[2], &t[2]);
1280 dump_512("t[2].x %s\n", t[2].x);
1281 dump_512("t[2].y %s\n", t[2].y);
1282 dump_512("t[2].z %s\n", t[2].z);
Denys Vlasenko12040122021-04-26 20:24:34 +02001283 memcpy(&t[y], &t[2], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001284
1285 n <<= 1;
1286 c--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001287 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001288
Denys Vlasenko12040122021-04-26 20:24:34 +02001289 if (map)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001290 sp_256_map_8(r, &t[0]);
Denys Vlasenko12040122021-04-26 20:24:34 +02001291 else
1292 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001293
Denys Vlasenko12040122021-04-26 20:24:34 +02001294 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001295}
1296
1297/* Multiply the base point of P256 by the scalar and return the result.
1298 * If map is true then convert result to affine co-ordinates.
1299 *
1300 * r Resulting point.
1301 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001302 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001303 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001304static void sp_256_ecc_mulmod_base_8(sp_point* r, sp_digit* k /*, int map*/)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001305{
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001306 /* Since this function is called only once, save space:
1307 * don't have "static const sp_point p256_base = {...}",
1308 * it would have more zeros than data.
1309 */
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001310 static const uint8_t p256_base_bin[] = {
1311 /* x (big-endian) */
1312 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,
1313 /* y */
1314 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 +02001315 /* z will be set to 1, infinity flag to "false" */
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001316 };
1317 sp_point p256_base;
1318
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001319 sp_256_point_from_bin2x32(&p256_base, p256_base_bin);
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001320
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001321 sp_256_ecc_mulmod_8(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001322}
1323
1324/* Multiply the point by the scalar and serialize the X ordinate.
1325 * The number is 0 padded to maximum size on output.
1326 *
1327 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001328 * pub2x32 Point to multiply.
1329 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001330 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001331static 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 +02001332{
Denys Vlasenko12040122021-04-26 20:24:34 +02001333 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001334
1335#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +02001336 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001337#endif
Denys Vlasenko12040122021-04-26 20:24:34 +02001338 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
1339 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001340
Denys Vlasenko12040122021-04-26 20:24:34 +02001341 sp_256_point_from_bin2x32(point, pub2x32);
Denys Vlasenko81d8af12021-10-05 17:31:33 +02001342 dump_512("point->x %s\n", point->x);
1343 dump_512("point->y %s\n", point->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001344
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001345 sp_256_ecc_mulmod_8(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001346
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001347 sp_256_to_bin_8(point->x, out32);
Denys Vlasenko12040122021-04-26 20:24:34 +02001348 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001349}
1350
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001351/* Generates a random scalar in [1..order-1] range. */
1352static void sp_256_ecc_gen_k_8(sp_digit k[8])
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001353{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001354 /* Since 32-bit words are "dense", no need to use
1355 * sp_256_from_bin_8(k, buf) to convert random stream
1356 * to sp_digit array - just store random bits there directly.
1357 */
1358 tls_get_random(k, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001359#if FIXED_SECRET
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001360 memset(k, 0x77, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001361#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001362
1363// If scalar is too large, try again (pseudo-code)
1364// if (k >= 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 - 1) // order of P256
1365// goto pick_another_random;
1366// k++; // ensure non-zero
1367 /* Simpler alternative, at the cost of not choosing some valid
1368 * random values, and slightly non-uniform distribution */
1369 if (k[0] == 0)
1370 k[0] = 1;
1371 if (k[7] >= 0xffffffff)
1372 k[7] = 0xfffffffe;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001373}
1374
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001375/* Makes a random EC key pair. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001376static void sp_ecc_make_key_256(sp_digit privkey[8], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001377{
1378 sp_point point[1];
1379
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001380 sp_256_ecc_gen_k_8(privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001381 dump_256("privkey %s\n", privkey);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001382 sp_256_ecc_mulmod_base_8(point, privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001383 dump_512("point->x %s\n", point->x);
1384 dump_512("point->y %s\n", point->y);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001385 sp_256_to_bin_8(point->x, pubkey);
1386 sp_256_to_bin_8(point->y, pubkey + 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001387
1388 memset(point, 0, sizeof(point)); //paranoia
1389}
1390
1391void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001392 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001393 const uint8_t *peerkey2x32)
1394{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001395 sp_digit privkey[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001396
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001397 dump_hex("peerkey2x32: %s\n", peerkey2x32, 64);
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001398 sp_ecc_make_key_256(privkey, pubkey2x32);
1399 dump_hex("pubkey: %s\n", pubkey2x32, 32);
1400 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001401
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001402 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001403 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
1404 dump_hex("premaster: %s\n", premaster32, 32);
1405}