blob: 4d4ecdd74123f5d21cc627ea77cb1b044ecbe863 [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 Vlasenko5e9c6172021-10-06 20:14:49 +0200349/* Sub p256_mod from r. (r = r - p256_mod). */
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200350#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200351static void sp_256_sub_8_p256_mod(sp_digit* r)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200352{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200353//p256_mod[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
354 asm volatile (
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200355"\n subl $0xffffffff, (%0)"
356"\n sbbl $0xffffffff, 1*4(%0)"
357"\n sbbl $0xffffffff, 2*4(%0)"
358"\n sbbl $0, 3*4(%0)"
359"\n sbbl $0, 4*4(%0)"
360"\n sbbl $0, 5*4(%0)"
361"\n sbbl $1, 6*4(%0)"
362"\n sbbl $0xffffffff, 7*4(%0)"
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200363"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200364 : "=r" (r)
365 : "0" (r)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200366 : "memory"
367 );
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200368}
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200369#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200370static void sp_256_sub_8_p256_mod(sp_digit* r)
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200371{
372 uint64_t reg;
373 uint64_t ooff;
374//p256_mod[3..0] = ffffffff00000001 0000000000000000 00000000ffffffff ffffffffffffffff
375 asm volatile (
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200376"\n addq $1, (%0)" // adding 1 is the same as subtracting ffffffffffffffff
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200377"\n cmc" // only carry bit needs inverting
Denys Vlasenko17e6fb02021-10-06 21:22:36 +0200378"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200379"\n sbbq %1, 1*8(%0)" // %1 holds 00000000ffffffff
Denys Vlasenko17e6fb02021-10-06 21:22:36 +0200380"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200381"\n sbbq $0, 2*8(%0)"
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200382"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200383"\n movq 3*8(%0), %2"
384"\n sbbq $0, %2" // adding 00000000ffffffff (in %1)
385"\n addq %1, %2" // is the same as subtracting ffffffff00000001
386"\n movq %2, 3*8(%0)"
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200387"\n"
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200388 : "=r" (r), "=r" (ooff), "=r" (reg)
389 : "0" (r), "1" (0x00000000ffffffff)
Denys Vlasenko87e3f2e2021-10-06 19:59:39 +0200390 : "memory"
391 );
392}
Denys Vlasenko567eefc2021-10-06 14:25:10 +0200393#else
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200394static void sp_256_sub_8_p256_mod(sp_digit* r)
395{
396 sp_256_sub_8(r, r, p256_mod);
397}
Denys Vlasenko567eefc2021-10-06 14:25:10 +0200398#endif
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200399
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200400/* Multiply a and b into r. (r = a * b) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200401static void sp_256_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200402{
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200403#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
404 sp_digit rr[15]; /* in case r coincides with a or b */
405 int k;
406 uint32_t accl;
407 uint32_t acch;
408
409 acch = accl = 0;
410 for (k = 0; k < 15; k++) {
411 int i, j;
412 uint32_t acc_hi;
413 i = k - 7;
414 if (i < 0)
415 i = 0;
416 j = k - i;
417 acc_hi = 0;
418 do {
419////////////////////////
420// uint64_t m = ((uint64_t)a[i]) * b[j];
421// acc_hi:acch:accl += m;
422 asm volatile (
423 // a[i] is already loaded in %%eax
424"\n mull %7"
425"\n addl %%eax, %0"
426"\n adcl %%edx, %1"
427"\n adcl $0, %2"
428 : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi)
429 : "0" (accl), "1" (acch), "2" (acc_hi), "a" (a[i]), "m" (b[j])
430 : "cc", "dx"
431 );
432////////////////////////
433 j--;
434 i++;
435 } while (i != 8 && i <= k);
436 rr[k] = accl;
437 accl = acch;
438 acch = acc_hi;
439 }
440 r[15] = accl;
441 memcpy(r, rr, sizeof(rr));
Denys Vlasenko911344a2021-10-06 17:17:34 +0200442#elif ALLOW_ASM && defined(__GNUC__) && defined(__x86_64__)
443 /* x86_64 has no alignment restrictions, and is little-endian,
444 * so 64-bit and 32-bit representations are identical */
445 const uint64_t* aa = (const void*)a;
446 const uint64_t* bb = (const void*)b;
447 uint64_t rr[8];
448 int k;
449 uint64_t accl;
450 uint64_t acch;
451
452 acch = accl = 0;
453 for (k = 0; k < 7; k++) {
454 int i, j;
455 uint64_t acc_hi;
456 i = k - 3;
457 if (i < 0)
458 i = 0;
459 j = k - i;
460 acc_hi = 0;
461 do {
462////////////////////////
463// uint128_t m = ((uint128_t)a[i]) * b[j];
464// acc_hi:acch:accl += m;
465 asm volatile (
466 // aa[i] is already loaded in %%rax
467"\n mulq %7"
468"\n addq %%rax, %0"
469"\n adcq %%rdx, %1"
470"\n adcq $0, %2"
471 : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi)
472 : "0" (accl), "1" (acch), "2" (acc_hi), "a" (aa[i]), "m" (bb[j])
473 : "cc", "dx"
474 );
475////////////////////////
Denys Vlasenko17e6fb02021-10-06 21:22:36 +0200476 j--;
Denys Vlasenko911344a2021-10-06 17:17:34 +0200477 i++;
478 } while (i != 4 && i <= k);
479 rr[k] = accl;
480 accl = acch;
481 acch = acc_hi;
482 }
483 rr[7] = accl;
484 memcpy(r, rr, sizeof(rr));
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200485#elif 0
486 //TODO: arm assembly (untested)
487 sp_digit tmp[16];
488
489 asm volatile (
490"\n mov r5, #0"
491"\n mov r6, #0"
492"\n mov r7, #0"
493"\n mov r8, #0"
494"\n 1:"
495"\n subs r3, r5, #28"
496"\n movcc r3, #0"
497"\n sub r4, r5, r3"
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200498"\n 2:"
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200499"\n ldr r14, [%[a], r3]"
500"\n ldr r12, [%[b], r4]"
501"\n umull r9, r10, r14, r12"
502"\n adds r6, r6, r9"
503"\n adcs r7, r7, r10"
504"\n adc r8, r8, #0"
505"\n add r3, r3, #4"
506"\n sub r4, r4, #4"
507"\n cmp r3, #32"
508"\n beq 3f"
509"\n cmp r3, r5"
510"\n ble 2b"
511"\n 3:"
512"\n str r6, [%[r], r5]"
513"\n mov r6, r7"
514"\n mov r7, r8"
515"\n mov r8, #0"
516"\n add r5, r5, #4"
517"\n cmp r5, #56"
518"\n ble 1b"
519"\n str r6, [%[r], r5]"
520 : [r] "r" (tmp), [a] "r" (a), [b] "r" (b)
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200521 : "memory", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14"
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200522 );
523 memcpy(r, tmp, sizeof(tmp));
524#else
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200525 sp_digit rr[15]; /* in case r coincides with a or b */
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200526 int i, j, k;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200527 uint64_t acc;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200528
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200529 acc = 0;
530 for (k = 0; k < 15; k++) {
531 uint32_t acc_hi;
532 i = k - 7;
533 if (i < 0)
534 i = 0;
535 j = k - i;
536 acc_hi = 0;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200537 do {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200538 uint64_t m = ((uint64_t)a[i]) * b[j];
539 acc += m;
540 if (acc < m)
541 acc_hi++;
542 j--;
543 i++;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200544 } while (i != 8 && i <= k);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200545 rr[k] = acc;
546 acc = (acc >> 32) | ((uint64_t)acc_hi << 32);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200547 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200548 r[15] = acc;
549 memcpy(r, rr, sizeof(rr));
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200550#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200551}
552
Denys Vlasenko389329e2021-10-05 13:39:33 +0200553/* Shift number right one bit. Bottom bit is lost. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200554static void sp_256_rshift1_8(sp_digit* r, sp_digit* a, sp_digit carry)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200555{
556 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200557
558 carry = (!!carry << 31);
559 for (i = 7; i >= 0; i--) {
560 sp_digit c = a[i] << 31;
561 r[i] = (a[i] >> 1) | carry;
562 carry = c;
563 }
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200564}
565
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200566/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200567static void sp_256_div2_8(sp_digit* r, const sp_digit* a, const sp_digit* m)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200568{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200569 int carry = 0;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200570 if (a[0] & 1)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200571 carry = sp_256_add_8(r, a, m);
572 sp_256_norm_8(r);
573 sp_256_rshift1_8(r, r, carry);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200574}
575
576/* Add two Montgomery form numbers (r = a + b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200577static void sp_256_mont_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b
578 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200579{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200580// const sp_digit* m = p256_mod;
581
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200582 int carry = sp_256_add_8(r, a, b);
583 sp_256_norm_8(r);
584 if (carry) {
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200585 sp_256_sub_8_p256_mod(r);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200586 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200587 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200588}
589
590/* Subtract two Montgomery form numbers (r = a - b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200591static void sp_256_mont_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b
592 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200593{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200594 const sp_digit* m = p256_mod;
595
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200596 int borrow;
597 borrow = sp_256_sub_8(r, a, b);
598 sp_256_norm_8(r);
599 if (borrow) {
600 sp_256_add_8(r, r, m);
601 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200602 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200603}
604
605/* Double a Montgomery form number (r = a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200606static void sp_256_mont_dbl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200607{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200608// const sp_digit* m = p256_mod;
609
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200610 int carry = sp_256_add_8(r, a, a);
611 sp_256_norm_8(r);
612 if (carry)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200613 sp_256_sub_8_p256_mod(r);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200614 sp_256_norm_8(r);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200615}
616
617/* Triple a Montgomery form number (r = a + a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200618static void sp_256_mont_tpl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200619{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200620// const sp_digit* m = p256_mod;
621
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200622 int carry = sp_256_add_8(r, a, a);
623 sp_256_norm_8(r);
624 if (carry) {
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200625 sp_256_sub_8_p256_mod(r);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200626 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200627 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200628 carry = sp_256_add_8(r, r, a);
629 sp_256_norm_8(r);
630 if (carry) {
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200631 sp_256_sub_8_p256_mod(r);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200632 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200633 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200634}
635
636/* Shift the result in the high 256 bits down to the bottom. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200637static void sp_256_mont_shift_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200638{
639 int i;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200640
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200641 for (i = 0; i < 8; i++) {
642 r[i] = a[i+8];
643 r[i+8] = 0;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200644 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200645}
646
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200647/* Mul a by scalar b and add into r. (r += a * b) */
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200648static int sp_256_mul_add_8(sp_digit* r /*, const sp_digit* a, sp_digit b*/)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200649{
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200650// const sp_digit* a = p256_mod;
651//a[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
652 sp_digit b = r[0];
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200653
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200654 uint64_t t;
655
656// t = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200657// for (i = 0; i < 8; i++) {
658// uint32_t t_hi;
659// uint64_t m = ((uint64_t)b * a[i]) + r[i];
660// t += m;
661// t_hi = (t < m);
662// r[i] = (sp_digit)t;
663// t = (t >> 32) | ((uint64_t)t_hi << 32);
664// }
665// r[8] += (sp_digit)t;
666
667 // Unroll, then optimize the above loop:
668 //uint32_t t_hi;
669 uint64_t m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200670 uint32_t t32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200671
672 //m = ((uint64_t)b * a[0]) + r[0];
673 // Since b is r[0] and a[0] is ffffffff, the above optimizes to:
674 // m = r[0] * ffffffff + r[0] = (r[0] * 100000000 - r[0]) + r[0] = r[0] << 32;
675 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200676 // t = r[0] << 32 = b << 32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200677 //t_hi = (t < m);
678 // t_hi = 0;
679 //r[0] = (sp_digit)t;
680 r[0] = 0;
681 //t = (t >> 32) | ((uint64_t)t_hi << 32);
682 // t = b;
683
684 //m = ((uint64_t)b * a[1]) + r[1];
685 // Since a[1] is ffffffff, the above optimizes to:
686 // m = b * ffffffff + r[1] = (b * 100000000 - b) + r[1] = (b << 32) - b + r[1];
687 //t += m;
688 // t = b + (b << 32) - b + r[1] = (b << 32) + r[1];
689 //t_hi = (t < m);
690 // t_hi = 0;
691 //r[1] = (sp_digit)t;
692 // r[1] = r[1];
693 //t = (t >> 32) | ((uint64_t)t_hi << 32);
694 // t = b;
695
696 //m = ((uint64_t)b * a[2]) + r[2];
697 // Since a[2] is ffffffff, the above optimizes to:
698 // m = b * ffffffff + r[2] = (b * 100000000 - b) + r[2] = (b << 32) - b + r[2];
699 //t += m;
700 // t = b + (b << 32) - b + r[2] = (b << 32) + r[2]
701 //t_hi = (t < m);
702 // t_hi = 0;
703 //r[2] = (sp_digit)t;
704 // r[2] = r[2];
705 //t = (t >> 32) | ((uint64_t)t_hi << 32);
706 // t = b;
707
708 //m = ((uint64_t)b * a[3]) + r[3];
709 // Since a[3] is 00000000, the above optimizes to:
710 // m = b * 0 + r[3] = r[3];
711 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200712 // t = b + r[3];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200713 //t_hi = (t < m);
714 // t_hi = 0;
715 //r[3] = (sp_digit)t;
716 r[3] = r[3] + b;
717 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200718 t32 = (r[3] < b); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200719
720 //m = ((uint64_t)b * a[4]) + r[4];
721 // Since a[4] is 00000000, the above optimizes to:
722 // m = b * 0 + r[4] = r[4];
723 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200724 // t = t32 + r[4];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200725 //t_hi = (t < m);
726 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200727 //r[4] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200728 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200729 if (t32 != 0) {
730 r[4]++;
731 t32 = (r[4] == 0); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200732
733 //m = ((uint64_t)b * a[5]) + r[5];
734 // Since a[5] is 00000000, the above optimizes to:
735 // m = b * 0 + r[5] = r[5];
736 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200737 // t = t32 + r[5]; (t32 is 0 or 1)
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200738 //t_hi = (t < m);
739 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200740 //r[5] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200741 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200742 if (t32 != 0) {
743 r[5]++;
744 t32 = (r[5] == 0); // 0 or 1
745 }
746 }
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200747
748 //m = ((uint64_t)b * a[6]) + r[6];
749 // Since a[6] is 00000001, the above optimizes to:
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200750 // m = (uint64_t)b + r[6]; // 33 bits at most
751 //t += m;
752 t = t32 + (uint64_t)b + r[6];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200753 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200754 // t_hi = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200755 r[6] = (sp_digit)t;
756 //t = (t >> 32) | ((uint64_t)t_hi << 32);
757 t = (t >> 32);
758
759 //m = ((uint64_t)b * a[7]) + r[7];
760 // Since a[7] is ffffffff, the above optimizes to:
761 // m = b * ffffffff + r[7] = (b * 100000000 - b) + r[7]
762 m = ((uint64_t)b << 32) - b + r[7];
763 t += m;
764 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200765 // 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 +0200766 r[7] = (sp_digit)t;
767 //t = (t >> 32) | ((uint64_t)t_hi << 32);
768 t = (t >> 32);
769
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200770 r[8] += (sp_digit)t;
771 return (r[8] < (sp_digit)t); /* 1 if addition overflowed */
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200772}
773
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200774/* Reduce the number back to 256 bits using Montgomery reduction.
775 *
776 * a A single precision number to reduce in place.
777 * m The single precision number representing the modulus.
778 * mp The digit representing the negative inverse of m mod 2^n.
779 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200780static void sp_256_mont_reduce_8(sp_digit* a/*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200781{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200782// const sp_digit* m = p256_mod;
Denys Vlasenko389329e2021-10-05 13:39:33 +0200783 sp_digit mp = p256_mp_mod;
784
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200785 int i;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200786// sp_digit mu;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200787
788 if (mp != 1) {
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200789 sp_digit word16th = 0;
790 for (i = 0; i < 8; i++) {
791// mu = (sp_digit)(a[i] * mp);
792 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
793 int j = i + 8;
794 inc_next_word0:
795 if (++j > 15) { /* a[16] array has no more words? */
796 word16th++;
797 continue;
798 }
799 if (++a[j] == 0) /* did this overflow too? */
800 goto inc_next_word0;
801 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200802 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200803 sp_256_mont_shift_8(a, a);
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200804 if (word16th != 0)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200805 sp_256_sub_8_p256_mod(a);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200806 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200807 }
Denys Vlasenko389329e2021-10-05 13:39:33 +0200808 else { /* Same code for explicit mp == 1 (which is always the case for P256) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200809 sp_digit word16th = 0;
810 for (i = 0; i < 8; i++) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200811 /*mu = a[i];*/
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200812 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200813 int j = i + 8;
814 inc_next_word:
815 if (++j > 15) { /* a[16] array has no more words? */
816 word16th++;
817 continue;
818 }
819 if (++a[j] == 0) /* did this overflow too? */
820 goto inc_next_word;
821 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200822 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200823 sp_256_mont_shift_8(a, a);
824 if (word16th != 0)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +0200825 sp_256_sub_8_p256_mod(a);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200826 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200827 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200828}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200829#if 0
830//TODO: arm32 asm (also adapt for x86?)
831static void sp_256_mont_reduce_8(sp_digit* a, sp_digit* m, sp_digit mp)
832{
833 sp_digit ca = 0;
834
835 asm volatile (
836 # i = 0
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200837 mov r12, #0
838 ldr r10, [%[a], #0]
839 ldr r14, [%[a], #4]
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02008401:
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200841 # mu = a[i] * mp
842 mul r8, %[mp], r10
843 # a[i+0] += m[0] * mu
844 ldr r7, [%[m], #0]
845 ldr r9, [%[a], #0]
846 umull r6, r7, r8, r7
847 adds r10, r10, r6
848 adc r5, r7, #0
849 # a[i+1] += m[1] * mu
850 ldr r7, [%[m], #4]
851 ldr r9, [%[a], #4]
852 umull r6, r7, r8, r7
853 adds r10, r14, r6
854 adc r4, r7, #0
855 adds r10, r10, r5
856 adc r4, r4, #0
857 # a[i+2] += m[2] * mu
858 ldr r7, [%[m], #8]
859 ldr r14, [%[a], #8]
860 umull r6, r7, r8, r7
861 adds r14, r14, r6
862 adc r5, r7, #0
863 adds r14, r14, r4
864 adc r5, r5, #0
865 # a[i+3] += m[3] * mu
866 ldr r7, [%[m], #12]
867 ldr r9, [%[a], #12]
868 umull r6, r7, r8, r7
869 adds r9, r9, r6
870 adc r4, r7, #0
871 adds r9, r9, r5
872 str r9, [%[a], #12]
873 adc r4, r4, #0
874 # a[i+4] += m[4] * mu
875 ldr r7, [%[m], #16]
876 ldr r9, [%[a], #16]
877 umull r6, r7, r8, r7
878 adds r9, r9, r6
879 adc r5, r7, #0
880 adds r9, r9, r4
881 str r9, [%[a], #16]
882 adc r5, r5, #0
883 # a[i+5] += m[5] * mu
884 ldr r7, [%[m], #20]
885 ldr r9, [%[a], #20]
886 umull r6, r7, r8, r7
887 adds r9, r9, r6
888 adc r4, r7, #0
889 adds r9, r9, r5
890 str r9, [%[a], #20]
891 adc r4, r4, #0
892 # a[i+6] += m[6] * mu
893 ldr r7, [%[m], #24]
894 ldr r9, [%[a], #24]
895 umull r6, r7, r8, r7
896 adds r9, r9, r6
897 adc r5, r7, #0
898 adds r9, r9, r4
899 str r9, [%[a], #24]
900 adc r5, r5, #0
901 # a[i+7] += m[7] * mu
902 ldr r7, [%[m], #28]
903 ldr r9, [%[a], #28]
904 umull r6, r7, r8, r7
905 adds r5, r5, r6
906 adcs r7, r7, %[ca]
907 mov %[ca], #0
908 adc %[ca], %[ca], %[ca]
909 adds r9, r9, r5
910 str r9, [%[a], #28]
911 ldr r9, [%[a], #32]
912 adcs r9, r9, r7
913 str r9, [%[a], #32]
914 adc %[ca], %[ca], #0
915 # i += 1
916 add %[a], %[a], #4
917 add r12, r12, #4
918 cmp r12, #32
919 blt 1b
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200920
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200921 str r10, [%[a], #0]
922 str r14, [%[a], #4]
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200923 : [ca] "+r" (ca), [a] "+r" (a)
924 : [m] "r" (m), [mp] "r" (mp)
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200925 : "memory", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14"
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200926 );
927
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200928 memcpy(a, a + 8, 32);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200929 if (ca)
930 a -= m;
931}
932#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200933
934/* Multiply two Montogmery form numbers mod the modulus (prime).
935 * (r = a * b mod m)
936 *
937 * r Result of multiplication.
938 * a First number to multiply in Montogmery form.
939 * b Second number to multiply in Montogmery form.
940 * m Modulus (prime).
941 * mp Montogmery mulitplier.
942 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200943static void sp_256_mont_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b
Denys Vlasenko389329e2021-10-05 13:39:33 +0200944 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200945{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200946 //const sp_digit* m = p256_mod;
947 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200948 sp_256_mul_8(r, a, b);
949 sp_256_mont_reduce_8(r /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200950}
951
952/* Square the Montgomery form number. (r = a * a mod m)
953 *
954 * r Result of squaring.
955 * a Number to square in Montogmery form.
956 * m Modulus (prime).
957 * mp Montogmery mulitplier.
958 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200959static void sp_256_mont_sqr_8(sp_digit* r, const sp_digit* a
Denys Vlasenko389329e2021-10-05 13:39:33 +0200960 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200961{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200962 //const sp_digit* m = p256_mod;
963 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200964 sp_256_mont_mul_8(r, a, a /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200965}
966
967/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
968 * P256 curve. (r = 1 / a mod m)
969 *
970 * r Inverse result.
971 * a Number to invert.
972 */
973#if 0
974/* Mod-2 for the P256 curve. */
975static const uint32_t p256_mod_2[8] = {
976 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
977 0x00000000,0x00000000,0x00000001,0xffffffff,
978};
979//Bit pattern:
980//2 2 2 2 2 2 2 1...1
981//5 5 4 3 2 1 0 9...0 9...1
982//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
983//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
984#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200985static void sp_256_mont_inv_8(sp_digit* r, sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200986{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200987 sp_digit t[2*8]; //can be just [8]?
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200988 int i;
989
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200990 memcpy(t, a, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200991 for (i = 254; i >= 0; i--) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200992 sp_256_mont_sqr_8(t, t /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200993 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
994 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200995 sp_256_mont_mul_8(t, t, a /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200996 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200997 memcpy(r, t, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200998}
999
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001000/* Multiply a number by Montogmery normalizer mod modulus (prime).
1001 *
1002 * r The resulting Montgomery form number.
1003 * a The number to convert.
1004 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001005static void sp_256_mod_mul_norm_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001006{
Denys Vlasenko12040122021-04-26 20:24:34 +02001007 int64_t t[8];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001008 int32_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001009
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001010#define A(n) ((uint64_t)a[n])
Denys Vlasenko12040122021-04-26 20:24:34 +02001011 /* 1 1 0 -1 -1 -1 -1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001012 t[0] = 0 + A(0) + A(1) - A(3) - A(4) - A(5) - A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +02001013 /* 0 1 1 0 -1 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001014 t[1] = 0 + A(1) + A(2) - A(4) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001015 /* 0 0 1 1 0 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001016 t[2] = 0 + A(2) + A(3) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001017 /* -1 -1 0 2 2 1 0 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001018 t[3] = 0 - A(0) - A(1) + 2 * A(3) + 2 * A(4) + A(5) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001019 /* 0 -1 -1 0 2 2 1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001020 t[4] = 0 - A(1) - A(2) + 2 * A(4) + 2 * A(5) + A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +02001021 /* 0 0 -1 -1 0 2 2 1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001022 t[5] = 0 - A(2) - A(3) + 2 * A(5) + 2 * A(6) + A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001023 /* -1 -1 0 0 0 1 3 2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001024 t[6] = 0 - A(0) - A(1) + A(5) + 3 * A(6) + 2 * A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +02001025 /* 1 0 -1 -1 -1 -1 0 3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001026 t[7] = 0 + A(0) - A(2) - A(3) - A(4) - A(5) + 3 * A(7);
1027#undef A
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001028
Denys Vlasenko12040122021-04-26 20:24:34 +02001029 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
1030 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
1031 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
1032 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
1033 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
1034 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
1035 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001036 o = t[7] >> 32; //t[7] &= 0xffffffff;
Denys Vlasenko12040122021-04-26 20:24:34 +02001037 t[0] += o;
1038 t[3] -= o;
1039 t[6] -= o;
1040 t[7] += o;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001041 r[0] = (sp_digit)t[0];
1042 t[1] += t[0] >> 32;
1043 r[1] = (sp_digit)t[1];
1044 t[2] += t[1] >> 32;
1045 r[2] = (sp_digit)t[2];
1046 t[3] += t[2] >> 32;
1047 r[3] = (sp_digit)t[3];
1048 t[4] += t[3] >> 32;
1049 r[4] = (sp_digit)t[4];
1050 t[5] += t[4] >> 32;
1051 r[5] = (sp_digit)t[5];
1052 t[6] += t[5] >> 32;
1053 r[6] = (sp_digit)t[6];
1054// t[7] += t[6] >> 32;
1055// r[7] = (sp_digit)t[7];
1056 r[7] = (sp_digit)t[7] + (sp_digit)(t[6] >> 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001057}
1058
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001059/* Map the Montgomery form projective co-ordinate point to an affine point.
1060 *
1061 * r Resulting affine co-ordinate point.
1062 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001063 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001064static void sp_256_map_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001065{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001066 sp_digit t1[2*8];
1067 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001068
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001069 sp_256_mont_inv_8(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001070
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001071 sp_256_mont_sqr_8(t2, t1 /*, p256_mod, p256_mp_mod*/);
1072 sp_256_mont_mul_8(t1, t2, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001073
Denys Vlasenko12040122021-04-26 20:24:34 +02001074 /* x /= z^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001075 sp_256_mont_mul_8(r->x, p->x, t2 /*, p256_mod, p256_mp_mod*/);
1076 memset(r->x + 8, 0, sizeof(r->x) / 2);
1077 sp_256_mont_reduce_8(r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001078 /* Reduce x to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001079 if (sp_256_cmp_8(r->x, p256_mod) >= 0)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +02001080 sp_256_sub_8_p256_mod(r->x);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001081 sp_256_norm_8(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001082
Denys Vlasenko12040122021-04-26 20:24:34 +02001083 /* y /= z^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001084 sp_256_mont_mul_8(r->y, p->y, t1 /*, p256_mod, p256_mp_mod*/);
1085 memset(r->y + 8, 0, sizeof(r->y) / 2);
1086 sp_256_mont_reduce_8(r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001087 /* Reduce y to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001088 if (sp_256_cmp_8(r->y, p256_mod) >= 0)
Denys Vlasenko5e9c6172021-10-06 20:14:49 +02001089 sp_256_sub_8_p256_mod(r->y);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001090 sp_256_norm_8(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001091
Denys Vlasenko12040122021-04-26 20:24:34 +02001092 memset(r->z, 0, sizeof(r->z));
1093 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001094}
1095
1096/* Double the Montgomery form projective point p.
1097 *
1098 * r Result of doubling point.
1099 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001100 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001101static void sp_256_proj_point_dbl_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001102{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001103 sp_digit t1[2*8];
1104 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001105
Denys Vlasenko12040122021-04-26 20:24:34 +02001106 /* Put point to double into result */
1107 if (r != p)
1108 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +02001109
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001110 if (r->infinity)
Denys Vlasenkoe7305052021-10-05 13:30:48 +02001111 return;
1112
Denys Vlasenko55578f22021-10-05 19:45:56 +02001113 if (SP_DEBUG) {
1114 /* unused part of t2, may result in spurios
1115 * differences in debug output. Clear it.
1116 */
1117 memset(t2, 0, sizeof(t2));
1118 }
1119
Denys Vlasenko12040122021-04-26 20:24:34 +02001120 /* T1 = Z * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001121 sp_256_mont_sqr_8(t1, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001122 /* Z = Y * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001123 sp_256_mont_mul_8(r->z, r->y, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001124 /* Z = 2Z */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001125 sp_256_mont_dbl_8(r->z, r->z /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001126 /* T2 = X - T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001127 sp_256_mont_sub_8(t2, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001128 /* T1 = X + T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001129 sp_256_mont_add_8(t1, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001130 /* T2 = T1 * T2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001131 sp_256_mont_mul_8(t2, t1, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001132 /* T1 = 3T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001133 sp_256_mont_tpl_8(t1, t2 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001134 /* Y = 2Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001135 sp_256_mont_dbl_8(r->y, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001136 /* Y = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001137 sp_256_mont_sqr_8(r->y, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001138 /* T2 = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001139 sp_256_mont_sqr_8(t2, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001140 /* T2 = T2/2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001141 sp_256_div2_8(t2, t2, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +02001142 /* Y = Y * X */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001143 sp_256_mont_mul_8(r->y, r->y, r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001144 /* X = T1 * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001145 sp_256_mont_mul_8(r->x, t1, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001146 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001147 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001148 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001149 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001150 /* Y = Y - X */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001151 sp_256_mont_sub_8(r->y, r->y, r->x /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001152 /* Y = Y * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001153 sp_256_mont_mul_8(r->y, r->y, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001154 /* Y = Y - T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001155 sp_256_mont_sub_8(r->y, r->y, t2 /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001156 dump_512("y2 %s\n", r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001157}
1158
1159/* Add two Montgomery form projective points.
1160 *
1161 * r Result of addition.
1162 * p Frist point to add.
1163 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001164 */
Denys Vlasenko53b2fdc2021-10-10 13:50:53 +02001165static NOINLINE void sp_256_proj_point_add_8(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001166{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001167 sp_digit t1[2*8];
1168 sp_digit t2[2*8];
1169 sp_digit t3[2*8];
1170 sp_digit t4[2*8];
1171 sp_digit t5[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001172
Denys Vlasenko12040122021-04-26 20:24:34 +02001173 /* Ensure only the first point is the same as the result. */
1174 if (q == r) {
1175 sp_point* a = p;
1176 p = q;
1177 q = a;
1178 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001179
Denys Vlasenko12040122021-04-26 20:24:34 +02001180 /* Check double */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001181 sp_256_sub_8(t1, p256_mod, q->y);
1182 sp_256_norm_8(t1);
1183 if (sp_256_cmp_equal_8(p->x, q->x)
1184 && sp_256_cmp_equal_8(p->z, q->z)
1185 && (sp_256_cmp_equal_8(p->y, q->y) || sp_256_cmp_equal_8(p->y, t1))
Denys Vlasenko12040122021-04-26 20:24:34 +02001186 ) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001187 sp_256_proj_point_dbl_8(r, p);
Denys Vlasenko12040122021-04-26 20:24:34 +02001188 }
1189 else {
1190 sp_point tp;
1191 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001192
Denys Vlasenko12040122021-04-26 20:24:34 +02001193 v = r;
1194 if (p->infinity | q->infinity) {
1195 memset(&tp, 0, sizeof(tp));
1196 v = &tp;
1197 }
Denys Vlasenko772e1872021-04-26 17:25:27 +02001198
Denys Vlasenko12040122021-04-26 20:24:34 +02001199 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001200
Denys Vlasenko12040122021-04-26 20:24:34 +02001201 /* U1 = X1*Z2^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001202 sp_256_mont_sqr_8(t1, q->z /*, p256_mod, p256_mp_mod*/);
1203 sp_256_mont_mul_8(t3, t1, q->z /*, p256_mod, p256_mp_mod*/);
1204 sp_256_mont_mul_8(t1, t1, v->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001205 /* U2 = X2*Z1^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001206 sp_256_mont_sqr_8(t2, v->z /*, p256_mod, p256_mp_mod*/);
1207 sp_256_mont_mul_8(t4, t2, v->z /*, p256_mod, p256_mp_mod*/);
1208 sp_256_mont_mul_8(t2, t2, q->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001209 /* S1 = Y1*Z2^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001210 sp_256_mont_mul_8(t3, t3, v->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001211 /* S2 = Y2*Z1^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001212 sp_256_mont_mul_8(t4, t4, q->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001213 /* H = U2 - U1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001214 sp_256_mont_sub_8(t2, t2, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001215 /* R = S2 - S1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001216 sp_256_mont_sub_8(t4, t4, t3 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001217 /* Z3 = H*Z1*Z2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001218 sp_256_mont_mul_8(v->z, v->z, q->z /*, p256_mod, p256_mp_mod*/);
1219 sp_256_mont_mul_8(v->z, v->z, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001220 /* X3 = R^2 - H^3 - 2*U1*H^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001221 sp_256_mont_sqr_8(v->x, t4 /*, p256_mod, p256_mp_mod*/);
1222 sp_256_mont_sqr_8(t5, t2 /*, p256_mod, p256_mp_mod*/);
1223 sp_256_mont_mul_8(v->y, t1, t5 /*, p256_mod, p256_mp_mod*/);
1224 sp_256_mont_mul_8(t5, t5, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001225 sp_256_mont_sub_8(v->x, v->x, t5 /*, p256_mod*/);
1226 sp_256_mont_dbl_8(t1, v->y /*, p256_mod*/);
1227 sp_256_mont_sub_8(v->x, v->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001228 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001229 sp_256_mont_sub_8(v->y, v->y, v->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001230 sp_256_mont_mul_8(v->y, v->y, t4 /*, p256_mod, p256_mp_mod*/);
1231 sp_256_mont_mul_8(t5, t5, t3 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001232 sp_256_mont_sub_8(v->y, v->y, t5 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001233 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001234}
1235
1236/* Multiply the point by the scalar and return the result.
1237 * If map is true then convert result to affine co-ordinates.
1238 *
1239 * r Resulting point.
1240 * g Point to multiply.
1241 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001242 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001243 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001244static 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 +02001245{
Denys Vlasenko12040122021-04-26 20:24:34 +02001246 enum { map = 1 }; /* we always convert result to affine coordinates */
1247 sp_point t[3];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001248 sp_digit n = n; /* for compiler */
Denys Vlasenko12040122021-04-26 20:24:34 +02001249 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001250
Denys Vlasenko12040122021-04-26 20:24:34 +02001251 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001252
Denys Vlasenko12040122021-04-26 20:24:34 +02001253 /* t[0] = {0, 0, 1} * norm */
1254 t[0].infinity = 1;
1255 /* t[1] = {g->x, g->y, g->z} * norm */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001256 sp_256_mod_mul_norm_8(t[1].x, g->x);
1257 sp_256_mod_mul_norm_8(t[1].y, g->y);
1258 sp_256_mod_mul_norm_8(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001259
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001260 /* For every bit, starting from most significant... */
1261 k += 7;
1262 c = 256;
1263 for (;;) {
1264 if ((c & 0x1f) == 0) {
1265 if (c == 0)
Denys Vlasenko12040122021-04-26 20:24:34 +02001266 break;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001267 n = *k--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001268 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001269
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001270 y = (n >> 31);
1271 dbg("y:%d t[%d] = t[0]+t[1]\n", y, y^1);
1272 sp_256_proj_point_add_8(&t[y^1], &t[0], &t[1]);
1273 dump_512("t[0].x %s\n", t[0].x);
1274 dump_512("t[0].y %s\n", t[0].y);
1275 dump_512("t[0].z %s\n", t[0].z);
1276 dump_512("t[1].x %s\n", t[1].x);
1277 dump_512("t[1].y %s\n", t[1].y);
1278 dump_512("t[1].z %s\n", t[1].z);
1279 dbg("t[2] = t[%d]\n", y);
Denys Vlasenko12040122021-04-26 20:24:34 +02001280 memcpy(&t[2], &t[y], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001281 dbg("t[2] *= 2\n");
1282 sp_256_proj_point_dbl_8(&t[2], &t[2]);
1283 dump_512("t[2].x %s\n", t[2].x);
1284 dump_512("t[2].y %s\n", t[2].y);
1285 dump_512("t[2].z %s\n", t[2].z);
Denys Vlasenko12040122021-04-26 20:24:34 +02001286 memcpy(&t[y], &t[2], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001287
1288 n <<= 1;
1289 c--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001290 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001291
Denys Vlasenko12040122021-04-26 20:24:34 +02001292 if (map)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001293 sp_256_map_8(r, &t[0]);
Denys Vlasenko12040122021-04-26 20:24:34 +02001294 else
1295 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001296
Denys Vlasenko12040122021-04-26 20:24:34 +02001297 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001298}
1299
1300/* Multiply the base point of P256 by the scalar and return the result.
1301 * If map is true then convert result to affine co-ordinates.
1302 *
1303 * r Resulting point.
1304 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001305 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001306 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001307static void sp_256_ecc_mulmod_base_8(sp_point* r, sp_digit* k /*, int map*/)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001308{
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001309 /* Since this function is called only once, save space:
1310 * don't have "static const sp_point p256_base = {...}",
1311 * it would have more zeros than data.
1312 */
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001313 static const uint8_t p256_base_bin[] = {
1314 /* x (big-endian) */
1315 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,
1316 /* y */
1317 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 +02001318 /* z will be set to 1, infinity flag to "false" */
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001319 };
1320 sp_point p256_base;
1321
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001322 sp_256_point_from_bin2x32(&p256_base, p256_base_bin);
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001323
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001324 sp_256_ecc_mulmod_8(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001325}
1326
1327/* Multiply the point by the scalar and serialize the X ordinate.
1328 * The number is 0 padded to maximum size on output.
1329 *
1330 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001331 * pub2x32 Point to multiply.
1332 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001333 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001334static 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 +02001335{
Denys Vlasenko12040122021-04-26 20:24:34 +02001336 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001337
1338#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +02001339 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001340#endif
Denys Vlasenko12040122021-04-26 20:24:34 +02001341 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
1342 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001343
Denys Vlasenko12040122021-04-26 20:24:34 +02001344 sp_256_point_from_bin2x32(point, pub2x32);
Denys Vlasenko81d8af12021-10-05 17:31:33 +02001345 dump_512("point->x %s\n", point->x);
1346 dump_512("point->y %s\n", point->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001347
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001348 sp_256_ecc_mulmod_8(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001349
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001350 sp_256_to_bin_8(point->x, out32);
Denys Vlasenko12040122021-04-26 20:24:34 +02001351 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001352}
1353
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001354/* Generates a random scalar in [1..order-1] range. */
1355static void sp_256_ecc_gen_k_8(sp_digit k[8])
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001356{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001357 /* Since 32-bit words are "dense", no need to use
1358 * sp_256_from_bin_8(k, buf) to convert random stream
1359 * to sp_digit array - just store random bits there directly.
1360 */
1361 tls_get_random(k, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001362#if FIXED_SECRET
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001363 memset(k, 0x77, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001364#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001365
1366// If scalar is too large, try again (pseudo-code)
1367// if (k >= 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 - 1) // order of P256
1368// goto pick_another_random;
1369// k++; // ensure non-zero
1370 /* Simpler alternative, at the cost of not choosing some valid
1371 * random values, and slightly non-uniform distribution */
1372 if (k[0] == 0)
1373 k[0] = 1;
1374 if (k[7] >= 0xffffffff)
1375 k[7] = 0xfffffffe;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001376}
1377
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001378/* Makes a random EC key pair. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001379static void sp_ecc_make_key_256(sp_digit privkey[8], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001380{
1381 sp_point point[1];
1382
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001383 sp_256_ecc_gen_k_8(privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001384 dump_256("privkey %s\n", privkey);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001385 sp_256_ecc_mulmod_base_8(point, privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001386 dump_512("point->x %s\n", point->x);
1387 dump_512("point->y %s\n", point->y);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001388 sp_256_to_bin_8(point->x, pubkey);
1389 sp_256_to_bin_8(point->y, pubkey + 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001390
1391 memset(point, 0, sizeof(point)); //paranoia
1392}
1393
1394void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001395 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001396 const uint8_t *peerkey2x32)
1397{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001398 sp_digit privkey[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001399
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001400 dump_hex("peerkey2x32: %s\n", peerkey2x32, 64);
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001401 sp_ecc_make_key_256(privkey, pubkey2x32);
1402 dump_hex("pubkey: %s\n", pubkey2x32, 32);
1403 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001404
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001405 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001406 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
1407 dump_hex("premaster: %s\n", premaster32, 32);
1408}