blob: 6fca2aad84179b15d31e0b1af8ee321d53e0b4cb [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.
35 * Header comment is kept intact:
36 */
37
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020038typedef struct sp_point {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020039 sp_digit x[2 * 8];
40 sp_digit y[2 * 8];
41 sp_digit z[2 * 8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020042 int infinity;
43} sp_point;
44
45/* The modulus (prime) of the curve P256. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020046static const sp_digit p256_mod[8] = {
47 0xffffffff,0xffffffff,0xffffffff,0x00000000,
48 0x00000000,0x00000000,0x00000001,0xffffffff,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020049};
50
51#define p256_mp_mod ((sp_digit)0x000001)
52
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020053/* Normalize the values in each word to 32 bits - NOP */
54#define sp_256_norm_8(a) ((void)0)
Denys Vlasenko77145182021-10-01 13:51:39 +020055
Denys Vlasenkoe7305052021-10-05 13:30:48 +020056/* Write r as big endian to byte array.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020057 * Fixed length number of bytes written: 32
58 *
59 * r A single precision integer.
60 * a Byte array.
61 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020062static void sp_256_to_bin_8(const sp_digit* r, uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020063{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020064 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020065
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020066 sp_256_norm_8(r);
Denys Vlasenko77145182021-10-01 13:51:39 +020067
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020068 r += 8;
69 for (i = 0; i < 8; i++) {
70 r--;
71 move_to_unaligned32(a, SWAP_BE32(*r));
72 a += 4;
Denys Vlasenko12040122021-04-26 20:24:34 +020073 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020074}
75
Denys Vlasenkoe7305052021-10-05 13:30:48 +020076/* Read big endian unsigned byte array into r.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020077 *
78 * r A single precision integer.
79 * a Byte array.
80 * n Number of bytes in array to read.
81 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020082static void sp_256_from_bin_8(sp_digit* r, const uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020083{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020084 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020085
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020086 r += 8;
87 for (i = 0; i < 8; i++) {
88 sp_digit v;
89 move_from_unaligned32(v, a);
90 *--r = SWAP_BE32(v);
91 a += 4;
Denys Vlasenko12040122021-04-26 20:24:34 +020092 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020093}
94
Denys Vlasenko137864f2021-10-05 13:47:42 +020095#if SP_DEBUG
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020096static void dump_256(const char *fmt, const sp_digit* r)
Denys Vlasenko137864f2021-10-05 13:47:42 +020097{
Denys Vlasenko137864f2021-10-05 13:47:42 +020098 uint8_t b32[32];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020099 sp_256_to_bin_8(r, b32);
Denys Vlasenko137864f2021-10-05 13:47:42 +0200100 dump_hex(fmt, b32, 32);
101}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200102static void dump_512(const char *fmt, const sp_digit* r)
Denys Vlasenko137864f2021-10-05 13:47:42 +0200103{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200104 uint8_t b64[64];
105 sp_256_to_bin_8(r, b64 + 32);
106 sp_256_to_bin_8(r+8, b64);
107 dump_hex(fmt, b64, 64);
Denys Vlasenko137864f2021-10-05 13:47:42 +0200108}
109#else
110# define dump_256(...) ((void)0)
111# define dump_512(...) ((void)0)
112#endif
113
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200114/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
115static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
116{
Denys Vlasenko12040122021-04-26 20:24:34 +0200117 memset(p, 0, sizeof(*p));
118 /*p->infinity = 0;*/
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200119 sp_256_from_bin_8(p->x, bin2x32);
120 sp_256_from_bin_8(p->y, bin2x32 + 32);
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200121 p->z[0] = 1; /* p->z = 1 */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200122}
123
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200124/* Compare a with b.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200125 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200126 * return -ve, 0 or +ve if a is less than, equal to or greater than b
127 * respectively.
128 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200129static signed_sp_digit sp_256_cmp_8(const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200130{
Denys Vlasenko12040122021-04-26 20:24:34 +0200131 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200132 for (i = 7; i >= 0; i--) {
133/* signed_sp_digit r = a[i] - b[i];
134 * if (r != 0)
135 * return r;
136 * does not work: think about a[i]=0, b[i]=0xffffffff
137 */
138 if (a[i] == b[i])
139 continue;
140 return (a[i] > b[i]) * 2 - 1;
Denys Vlasenko12040122021-04-26 20:24:34 +0200141 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200142 return 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200143}
144
145/* Compare two numbers to determine if they are equal.
146 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200147 * return 1 when equal and 0 otherwise.
148 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200149static int sp_256_cmp_equal_8(const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200150{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200151 return sp_256_cmp_8(a, b) == 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200152}
153
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200154/* Add b to a into r. (r = a + b). Return !0 on overflow */
155static int sp_256_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200156{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200157#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
158 sp_digit reg;
159 asm volatile (
160"\n movl (%0), %3"
161"\n addl (%1), %3"
162"\n movl %3, (%2)"
163"\n"
164"\n movl 1*4(%0), %3"
165"\n adcl 1*4(%1), %3"
166"\n movl %3, 1*4(%2)"
167"\n"
168"\n movl 2*4(%0), %3"
169"\n adcl 2*4(%1), %3"
170"\n movl %3, 2*4(%2)"
171"\n"
172"\n movl 3*4(%0), %3"
173"\n adcl 3*4(%1), %3"
174"\n movl %3, 3*4(%2)"
175"\n"
176"\n movl 4*4(%0), %3"
177"\n adcl 4*4(%1), %3"
178"\n movl %3, 4*4(%2)"
179"\n"
180"\n movl 5*4(%0), %3"
181"\n adcl 5*4(%1), %3"
182"\n movl %3, 5*4(%2)"
183"\n"
184"\n movl 6*4(%0), %3"
185"\n adcl 6*4(%1), %3"
186"\n movl %3, 6*4(%2)"
187"\n"
188"\n movl 7*4(%0), %3"
189"\n adcl 7*4(%1), %3"
190"\n movl %3, 7*4(%2)"
191"\n"
192"\n sbbl %3, %3"
193"\n"
194 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
195 : "0" (a), "1" (b), "2" (r)
196 : "memory"
197 );
198 return reg;
199#else
Denys Vlasenko12040122021-04-26 20:24:34 +0200200 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200201 sp_digit carry;
202
203 carry = 0;
204 for (i = 0; i < 8; i++) {
205 sp_digit w, v;
206 w = b[i] + carry;
207 v = a[i];
208 if (w != 0) {
209 v = a[i] + w;
210 carry = (v < a[i]);
211 /* hope compiler detects above as "carry flag set" */
212 }
213 /* else: b + carry == 0, two cases:
214 * b:ffffffff, carry:1
215 * b:00000000, carry:0
216 * in either case, r[i] = a[i] and carry remains unchanged
217 */
218 r[i] = v;
219 }
220 return carry;
221#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200222}
223
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200224/* Sub b from a into r. (r = a - b). Return !0 on underflow */
225static int sp_256_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200226{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200227#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
228 sp_digit reg;
229 asm volatile (
230"\n movl (%0), %3"
231"\n subl (%1), %3"
232"\n movl %3, (%2)"
233"\n"
234"\n movl 1*4(%0), %3"
235"\n sbbl 1*4(%1), %3"
236"\n movl %3, 1*4(%2)"
237"\n"
238"\n movl 2*4(%0), %3"
239"\n sbbl 2*4(%1), %3"
240"\n movl %3, 2*4(%2)"
241"\n"
242"\n movl 3*4(%0), %3"
243"\n sbbl 3*4(%1), %3"
244"\n movl %3, 3*4(%2)"
245"\n"
246"\n movl 4*4(%0), %3"
247"\n sbbl 4*4(%1), %3"
248"\n movl %3, 4*4(%2)"
249"\n"
250"\n movl 5*4(%0), %3"
251"\n sbbl 5*4(%1), %3"
252"\n movl %3, 5*4(%2)"
253"\n"
254"\n movl 6*4(%0), %3"
255"\n sbbl 6*4(%1), %3"
256"\n movl %3, 6*4(%2)"
257"\n"
258"\n movl 7*4(%0), %3"
259"\n sbbl 7*4(%1), %3"
260"\n movl %3, 7*4(%2)"
261"\n"
262"\n sbbl %3, %3"
263"\n"
264 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
265 : "0" (a), "1" (b), "2" (r)
266 : "memory"
267 );
268 return reg;
269#else
Denys Vlasenko12040122021-04-26 20:24:34 +0200270 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200271 sp_digit borrow;
272
273 borrow = 0;
274 for (i = 0; i < 8; i++) {
275 sp_digit w, v;
276 w = b[i] + borrow;
277 v = a[i];
278 if (w != 0) {
279 v = a[i] - w;
280 borrow = (v > a[i]);
281 /* hope compiler detects above as "carry flag set" */
282 }
283 /* else: b + borrow == 0, two cases:
284 * b:ffffffff, borrow:1
285 * b:00000000, borrow:0
286 * in either case, r[i] = a[i] and borrow remains unchanged
287 */
288 r[i] = v;
289 }
290 return borrow;
291#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200292}
293
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200294/* Sub p256_mod from a into r. (r = a - p256_mod). */
295static void sp_256_sub_8_p256_mod(sp_digit* r, const sp_digit* a)
296{
297#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
298 sp_digit reg;
299//p256_mod[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
300 asm volatile (
301"\n movl (%0), %2"
302"\n subl $0xffffffff, %2"
303"\n movl %2, (%1)"
304"\n"
305"\n movl 1*4(%0), %2"
306"\n sbbl $0xffffffff, %2"
307"\n movl %2, 1*4(%1)"
308"\n"
309"\n movl 2*4(%0), %2"
310"\n sbbl $0xffffffff, %2"
311"\n movl %2, 2*4(%1)"
312"\n"
313"\n movl 3*4(%0), %2"
314"\n sbbl $0, %2"
315"\n movl %2, 3*4(%1)"
316"\n"
317"\n movl 4*4(%0), %2"
318"\n sbbl $0, %2"
319"\n movl %2, 4*4(%1)"
320"\n"
321"\n movl 5*4(%0), %2"
322"\n sbbl $0, %2"
323"\n movl %2, 5*4(%1)"
324"\n"
325"\n movl 6*4(%0), %2"
326"\n sbbl $1, %2"
327"\n movl %2, 6*4(%1)"
328"\n"
329"\n movl 7*4(%0), %2"
330"\n sbbl $0xffffffff, %2"
331"\n movl %2, 7*4(%1)"
332"\n"
333 : "=r" (a), "=r" (r), "=r" (reg)
334 : "0" (a), "1" (r)
335 : "memory"
336 );
337#else
338 const sp_digit* b = p256_mod;
339 int i;
340 sp_digit borrow;
341
342 borrow = 0;
343 for (i = 0; i < 8; i++) {
344 sp_digit w, v;
345 w = b[i] + borrow;
346 v = a[i];
347 if (w != 0) {
348 v = a[i] - w;
349 borrow = (v > a[i]);
350 /* hope compiler detects above as "carry flag set" */
351 }
352 /* else: b + borrow == 0, two cases:
353 * b:ffffffff, borrow:1
354 * b:00000000, borrow:0
355 * in either case, r[i] = a[i] and borrow remains unchanged
356 */
357 r[i] = v;
358 }
359#endif
360}
361
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200362/* Multiply a and b into r. (r = a * b) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200363static void sp_256_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200364{
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200365#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
366 sp_digit rr[15]; /* in case r coincides with a or b */
367 int k;
368 uint32_t accl;
369 uint32_t acch;
370
371 acch = accl = 0;
372 for (k = 0; k < 15; k++) {
373 int i, j;
374 uint32_t acc_hi;
375 i = k - 7;
376 if (i < 0)
377 i = 0;
378 j = k - i;
379 acc_hi = 0;
380 do {
381////////////////////////
382// uint64_t m = ((uint64_t)a[i]) * b[j];
383// acc_hi:acch:accl += m;
384 asm volatile (
385 // a[i] is already loaded in %%eax
386"\n mull %7"
387"\n addl %%eax, %0"
388"\n adcl %%edx, %1"
389"\n adcl $0, %2"
390 : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi)
391 : "0" (accl), "1" (acch), "2" (acc_hi), "a" (a[i]), "m" (b[j])
392 : "cc", "dx"
393 );
394////////////////////////
395 j--;
396 i++;
397 } while (i != 8 && i <= k);
398 rr[k] = accl;
399 accl = acch;
400 acch = acc_hi;
401 }
402 r[15] = accl;
403 memcpy(r, rr, sizeof(rr));
404#elif 0
405 //TODO: arm assembly (untested)
406 sp_digit tmp[16];
407
408 asm volatile (
409"\n mov r5, #0"
410"\n mov r6, #0"
411"\n mov r7, #0"
412"\n mov r8, #0"
413"\n 1:"
414"\n subs r3, r5, #28"
415"\n movcc r3, #0"
416"\n sub r4, r5, r3"
417"\n 2:"
418"\n ldr r14, [%[a], r3]"
419"\n ldr r12, [%[b], r4]"
420"\n umull r9, r10, r14, r12"
421"\n adds r6, r6, r9"
422"\n adcs r7, r7, r10"
423"\n adc r8, r8, #0"
424"\n add r3, r3, #4"
425"\n sub r4, r4, #4"
426"\n cmp r3, #32"
427"\n beq 3f"
428"\n cmp r3, r5"
429"\n ble 2b"
430"\n 3:"
431"\n str r6, [%[r], r5]"
432"\n mov r6, r7"
433"\n mov r7, r8"
434"\n mov r8, #0"
435"\n add r5, r5, #4"
436"\n cmp r5, #56"
437"\n ble 1b"
438"\n str r6, [%[r], r5]"
439 : [r] "r" (tmp), [a] "r" (a), [b] "r" (b)
440 : "memory", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r14", "r12"
441 );
442 memcpy(r, tmp, sizeof(tmp));
443#else
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200444 sp_digit rr[15]; /* in case r coincides with a or b */
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200445 int i, j, k;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200446 uint64_t acc;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200447
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200448 acc = 0;
449 for (k = 0; k < 15; k++) {
450 uint32_t acc_hi;
451 i = k - 7;
452 if (i < 0)
453 i = 0;
454 j = k - i;
455 acc_hi = 0;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200456 do {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200457 uint64_t m = ((uint64_t)a[i]) * b[j];
458 acc += m;
459 if (acc < m)
460 acc_hi++;
461 j--;
462 i++;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200463 } while (i != 8 && i <= k);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200464 rr[k] = acc;
465 acc = (acc >> 32) | ((uint64_t)acc_hi << 32);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200466 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200467 r[15] = acc;
468 memcpy(r, rr, sizeof(rr));
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200469#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200470}
471
Denys Vlasenko389329e2021-10-05 13:39:33 +0200472/* Shift number right one bit. Bottom bit is lost. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200473static void sp_256_rshift1_8(sp_digit* r, sp_digit* a, sp_digit carry)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200474{
475 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200476
477 carry = (!!carry << 31);
478 for (i = 7; i >= 0; i--) {
479 sp_digit c = a[i] << 31;
480 r[i] = (a[i] >> 1) | carry;
481 carry = c;
482 }
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200483}
484
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200485/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200486static void sp_256_div2_8(sp_digit* r, const sp_digit* a, const sp_digit* m)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200487{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200488 int carry = 0;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200489 if (a[0] & 1)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200490 carry = sp_256_add_8(r, a, m);
491 sp_256_norm_8(r);
492 sp_256_rshift1_8(r, r, carry);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200493}
494
495/* Add two Montgomery form numbers (r = a + b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200496static void sp_256_mont_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b
497 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200498{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200499// const sp_digit* m = p256_mod;
500
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200501 int carry = sp_256_add_8(r, a, b);
502 sp_256_norm_8(r);
503 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200504 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200505 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200506 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200507}
508
509/* Subtract two Montgomery form numbers (r = a - b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200510static void sp_256_mont_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b
511 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200512{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200513 const sp_digit* m = p256_mod;
514
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200515 int borrow;
516 borrow = sp_256_sub_8(r, a, b);
517 sp_256_norm_8(r);
518 if (borrow) {
519 sp_256_add_8(r, r, m);
520 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200521 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200522}
523
524/* Double a Montgomery form number (r = a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200525static void sp_256_mont_dbl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200526{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200527// const sp_digit* m = p256_mod;
528
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200529 int carry = sp_256_add_8(r, a, a);
530 sp_256_norm_8(r);
531 if (carry)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200532 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200533 sp_256_norm_8(r);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200534}
535
536/* Triple a Montgomery form number (r = a + a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200537static void sp_256_mont_tpl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200538{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200539// const sp_digit* m = p256_mod;
540
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200541 int carry = sp_256_add_8(r, a, a);
542 sp_256_norm_8(r);
543 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200544 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200545 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200546 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200547 carry = sp_256_add_8(r, r, a);
548 sp_256_norm_8(r);
549 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200550 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200551 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200552 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200553}
554
555/* Shift the result in the high 256 bits down to the bottom. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200556static void sp_256_mont_shift_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200557{
558 int i;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200559
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200560 for (i = 0; i < 8; i++) {
561 r[i] = a[i+8];
562 r[i+8] = 0;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200563 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200564}
565
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200566/* Mul a by scalar b and add into r. (r += a * b) */
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200567static int sp_256_mul_add_8(sp_digit* r /*, const sp_digit* a, sp_digit b*/)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200568{
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200569// const sp_digit* a = p256_mod;
570//a[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
571 sp_digit b = r[0];
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200572
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200573 uint64_t t;
574
575// t = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200576// for (i = 0; i < 8; i++) {
577// uint32_t t_hi;
578// uint64_t m = ((uint64_t)b * a[i]) + r[i];
579// t += m;
580// t_hi = (t < m);
581// r[i] = (sp_digit)t;
582// t = (t >> 32) | ((uint64_t)t_hi << 32);
583// }
584// r[8] += (sp_digit)t;
585
586 // Unroll, then optimize the above loop:
587 //uint32_t t_hi;
588 uint64_t m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200589 uint32_t t32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200590
591 //m = ((uint64_t)b * a[0]) + r[0];
592 // Since b is r[0] and a[0] is ffffffff, the above optimizes to:
593 // m = r[0] * ffffffff + r[0] = (r[0] * 100000000 - r[0]) + r[0] = r[0] << 32;
594 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200595 // t = r[0] << 32 = b << 32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200596 //t_hi = (t < m);
597 // t_hi = 0;
598 //r[0] = (sp_digit)t;
599 r[0] = 0;
600 //t = (t >> 32) | ((uint64_t)t_hi << 32);
601 // t = b;
602
603 //m = ((uint64_t)b * a[1]) + r[1];
604 // Since a[1] is ffffffff, the above optimizes to:
605 // m = b * ffffffff + r[1] = (b * 100000000 - b) + r[1] = (b << 32) - b + r[1];
606 //t += m;
607 // t = b + (b << 32) - b + r[1] = (b << 32) + r[1];
608 //t_hi = (t < m);
609 // t_hi = 0;
610 //r[1] = (sp_digit)t;
611 // r[1] = r[1];
612 //t = (t >> 32) | ((uint64_t)t_hi << 32);
613 // t = b;
614
615 //m = ((uint64_t)b * a[2]) + r[2];
616 // Since a[2] is ffffffff, the above optimizes to:
617 // m = b * ffffffff + r[2] = (b * 100000000 - b) + r[2] = (b << 32) - b + r[2];
618 //t += m;
619 // t = b + (b << 32) - b + r[2] = (b << 32) + r[2]
620 //t_hi = (t < m);
621 // t_hi = 0;
622 //r[2] = (sp_digit)t;
623 // r[2] = r[2];
624 //t = (t >> 32) | ((uint64_t)t_hi << 32);
625 // t = b;
626
627 //m = ((uint64_t)b * a[3]) + r[3];
628 // Since a[3] is 00000000, the above optimizes to:
629 // m = b * 0 + r[3] = r[3];
630 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200631 // t = b + r[3];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200632 //t_hi = (t < m);
633 // t_hi = 0;
634 //r[3] = (sp_digit)t;
635 r[3] = r[3] + b;
636 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200637 t32 = (r[3] < b); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200638
639 //m = ((uint64_t)b * a[4]) + r[4];
640 // Since a[4] is 00000000, the above optimizes to:
641 // m = b * 0 + r[4] = r[4];
642 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200643 // t = t32 + r[4];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200644 //t_hi = (t < m);
645 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200646 //r[4] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200647 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200648 if (t32 != 0) {
649 r[4]++;
650 t32 = (r[4] == 0); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200651
652 //m = ((uint64_t)b * a[5]) + r[5];
653 // Since a[5] is 00000000, the above optimizes to:
654 // m = b * 0 + r[5] = r[5];
655 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200656 // t = t32 + r[5]; (t32 is 0 or 1)
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200657 //t_hi = (t < m);
658 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200659 //r[5] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200660 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200661 if (t32 != 0) {
662 r[5]++;
663 t32 = (r[5] == 0); // 0 or 1
664 }
665 }
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200666
667 //m = ((uint64_t)b * a[6]) + r[6];
668 // Since a[6] is 00000001, the above optimizes to:
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200669 // m = (uint64_t)b + r[6]; // 33 bits at most
670 //t += m;
671 t = t32 + (uint64_t)b + r[6];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200672 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200673 // t_hi = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200674 r[6] = (sp_digit)t;
675 //t = (t >> 32) | ((uint64_t)t_hi << 32);
676 t = (t >> 32);
677
678 //m = ((uint64_t)b * a[7]) + r[7];
679 // Since a[7] is ffffffff, the above optimizes to:
680 // m = b * ffffffff + r[7] = (b * 100000000 - b) + r[7]
681 m = ((uint64_t)b << 32) - b + r[7];
682 t += m;
683 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200684 // 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 +0200685 r[7] = (sp_digit)t;
686 //t = (t >> 32) | ((uint64_t)t_hi << 32);
687 t = (t >> 32);
688
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200689 r[8] += (sp_digit)t;
690 return (r[8] < (sp_digit)t); /* 1 if addition overflowed */
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200691}
692
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200693/* Reduce the number back to 256 bits using Montgomery reduction.
694 *
695 * a A single precision number to reduce in place.
696 * m The single precision number representing the modulus.
697 * mp The digit representing the negative inverse of m mod 2^n.
698 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200699static void sp_256_mont_reduce_8(sp_digit* a/*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200700{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200701// const sp_digit* m = p256_mod;
Denys Vlasenko389329e2021-10-05 13:39:33 +0200702 sp_digit mp = p256_mp_mod;
703
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200704 int i;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200705// sp_digit mu;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200706
707 if (mp != 1) {
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200708 sp_digit word16th = 0;
709 for (i = 0; i < 8; i++) {
710// mu = (sp_digit)(a[i] * mp);
711 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
712 int j = i + 8;
713 inc_next_word0:
714 if (++j > 15) { /* a[16] array has no more words? */
715 word16th++;
716 continue;
717 }
718 if (++a[j] == 0) /* did this overflow too? */
719 goto inc_next_word0;
720 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200721 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200722 sp_256_mont_shift_8(a, a);
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200723 if (word16th != 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200724 sp_256_sub_8_p256_mod(a, a /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200725 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200726 }
Denys Vlasenko389329e2021-10-05 13:39:33 +0200727 else { /* Same code for explicit mp == 1 (which is always the case for P256) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200728 sp_digit word16th = 0;
729 for (i = 0; i < 8; i++) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200730 /*mu = a[i];*/
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200731 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200732 int j = i + 8;
733 inc_next_word:
734 if (++j > 15) { /* a[16] array has no more words? */
735 word16th++;
736 continue;
737 }
738 if (++a[j] == 0) /* did this overflow too? */
739 goto inc_next_word;
740 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200741 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200742 sp_256_mont_shift_8(a, a);
743 if (word16th != 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200744 sp_256_sub_8_p256_mod(a, a /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200745 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200746 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200747}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200748#if 0
749//TODO: arm32 asm (also adapt for x86?)
750static void sp_256_mont_reduce_8(sp_digit* a, sp_digit* m, sp_digit mp)
751{
752 sp_digit ca = 0;
753
754 asm volatile (
755 # i = 0
756 mov r12, #0 # i = 0
757 ldr r10, [%[a], #0] # r10 = a[0]
758 ldr r14, [%[a], #4] # r14 = a[1]
7591:
760 # mu = a[i] * mp #
761 mul r8, %[mp], r10 # mu = a[i] * mp
762 # a[i+0] += m[0] * mu #
763 ldr r7, [%[m], #0] # a[i+0] += m[0] * mu
764 ldr r9, [%[a], #0] #
765 umull r6, r7, r8, r7 # r7:r6 = mu * m[0]
766 adds r10, r10, r6 # r5:r10 += r7:r6
767 adc r5, r7, #0 #
768 # a[i+1] += m[1] * mu #
769 ldr r7, [%[m], #4] # a[i+1] += m[1] * mu
770 ldr r9, [%[a], #4] #
771 umull r6, r7, r8, r7 # r7:r6 = mu * m[1]
772 adds r10, r14, r6 # r4:r10 = r7:r14 + r7:r6
773 adc r4, r7, #0 #
774 adds r10, r10, r5 # r4:r10 += r5
775 adc r4, r4, #0 #
776 # a[i+2] += m[2] * mu #
777 ldr r7, [%[m], #8] # a[i+2] += m[2] * mu
778 ldr r14, [%[a], #8] #
779 umull r6, r7, r8, r7 #
780 adds r14, r14, r6 #
781 adc r5, r7, #0 #
782 adds r14, r14, r4 #
783 adc r5, r5, #0 #
784 # a[i+3] += m[3] * mu #
785 ldr r7, [%[m], #12] # a[i+3] += m[3] * mu
786 ldr r9, [%[a], #12] #
787 umull r6, r7, r8, r7 #
788 adds r9, r9, r6 #
789 adc r4, r7, #0 #
790 adds r9, r9, r5 #
791 str r9, [%[a], #12] # a[3] = r9
792 adc r4, r4, #0 #
793 # a[i+4] += m[4] * mu #
794 ldr r7, [%[m], #16] # a[i+4] += m[4] * mu
795 ldr r9, [%[a], #16] #
796 umull r6, r7, r8, r7 #
797 adds r9, r9, r6 #
798 adc r5, r7, #0 #
799 adds r9, r9, r4 #
800 str r9, [%[a], #16] # a[4] = r9
801 adc r5, r5, #0 #
802 # a[i+5] += m[5] * mu #
803 ldr r7, [%[m], #20] # a[i+5] += m[5] * mu
804 ldr r9, [%[a], #20] #
805 umull r6, r7, r8, r7 #
806 adds r9, r9, r6 #
807 adc r4, r7, #0 #
808 adds r9, r9, r5 #
809 str r9, [%[a], #20] # a[5] = r9
810 adc r4, r4, #0 #
811 # a[i+6] += m[6] * mu #
812 ldr r7, [%[m], #24] # a[i+6] += m[6] * mu
813 ldr r9, [%[a], #24] #
814 umull r6, r7, r8, r7 #
815 adds r9, r9, r6 #
816 adc r5, r7, #0 #
817 adds r9, r9, r4 #
818 str r9, [%[a], #24] # a[6] = r9
819 adc r5, r5, #0 #
820 # a[i+7] += m[7] * mu #
821 ldr r7, [%[m], #28] # a[i+7] += m[7] * mu
822 ldr r9, [%[a], #28] #
823 umull r6, r7, r8, r7 #
824 adds r5, r5, r6 #
825 adcs r7, r7, %[ca] #
826 mov %[ca], #0 #
827 adc %[ca], %[ca], %[ca] # ca = CF
828 adds r9, r9, r5 #
829 str r9, [%[a], #28] # a[7] = r9
830 ldr r9, [%[a], #32] # r9 = a[8]
831 adcs r9, r9, r7 #
832 str r9, [%[a], #32] # a[8] = r9
833 adc %[ca], %[ca], #0 # ca += CF
834 # i += 1 # i++
835 add %[a], %[a], #4 # a++
836 add r12, r12, #4 # i += 4
837 cmp r12, #32 # if (i < 32)
838 blt 1b # goto 1
839
840 str r10, [%[a], #0] # a[0] = r10
841 str r14, [%[a], #4] # a[1] = r14
842 : [ca] "+r" (ca), [a] "+r" (a)
843 : [m] "r" (m), [mp] "r" (mp)
844 : "memory", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r14", "r12"
845 );
846
847 if (ca)
848 a -= m;
849}
850#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200851
852/* Multiply two Montogmery form numbers mod the modulus (prime).
853 * (r = a * b mod m)
854 *
855 * r Result of multiplication.
856 * a First number to multiply in Montogmery form.
857 * b Second number to multiply in Montogmery form.
858 * m Modulus (prime).
859 * mp Montogmery mulitplier.
860 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200861static void sp_256_mont_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b
Denys Vlasenko389329e2021-10-05 13:39:33 +0200862 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200863{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200864 //const sp_digit* m = p256_mod;
865 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200866 sp_256_mul_8(r, a, b);
867 sp_256_mont_reduce_8(r /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200868}
869
870/* Square the Montgomery form number. (r = a * a mod m)
871 *
872 * r Result of squaring.
873 * a Number to square in Montogmery form.
874 * m Modulus (prime).
875 * mp Montogmery mulitplier.
876 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200877static void sp_256_mont_sqr_8(sp_digit* r, const sp_digit* a
Denys Vlasenko389329e2021-10-05 13:39:33 +0200878 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200879{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200880 //const sp_digit* m = p256_mod;
881 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200882 sp_256_mont_mul_8(r, a, a /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200883}
884
885/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
886 * P256 curve. (r = 1 / a mod m)
887 *
888 * r Inverse result.
889 * a Number to invert.
890 */
891#if 0
892/* Mod-2 for the P256 curve. */
893static const uint32_t p256_mod_2[8] = {
894 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
895 0x00000000,0x00000000,0x00000001,0xffffffff,
896};
897//Bit pattern:
898//2 2 2 2 2 2 2 1...1
899//5 5 4 3 2 1 0 9...0 9...1
900//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
901//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
902#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200903static void sp_256_mont_inv_8(sp_digit* r, sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200904{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200905 sp_digit t[2*8]; //can be just [8]?
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200906 int i;
907
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200908 memcpy(t, a, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200909 for (i = 254; i >= 0; i--) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200910 sp_256_mont_sqr_8(t, t /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200911 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
912 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200913 sp_256_mont_mul_8(t, t, a /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200914 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200915 memcpy(r, t, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200916}
917
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200918/* Multiply a number by Montogmery normalizer mod modulus (prime).
919 *
920 * r The resulting Montgomery form number.
921 * a The number to convert.
922 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200923static void sp_256_mod_mul_norm_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200924{
Denys Vlasenko12040122021-04-26 20:24:34 +0200925 int64_t t[8];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200926 int32_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200927
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200928#define A(n) ((uint64_t)a[n])
Denys Vlasenko12040122021-04-26 20:24:34 +0200929 /* 1 1 0 -1 -1 -1 -1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200930 t[0] = 0 + A(0) + A(1) - A(3) - A(4) - A(5) - A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +0200931 /* 0 1 1 0 -1 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200932 t[1] = 0 + A(1) + A(2) - A(4) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200933 /* 0 0 1 1 0 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200934 t[2] = 0 + A(2) + A(3) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200935 /* -1 -1 0 2 2 1 0 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200936 t[3] = 0 - A(0) - A(1) + 2 * A(3) + 2 * A(4) + A(5) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200937 /* 0 -1 -1 0 2 2 1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200938 t[4] = 0 - A(1) - A(2) + 2 * A(4) + 2 * A(5) + A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +0200939 /* 0 0 -1 -1 0 2 2 1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200940 t[5] = 0 - A(2) - A(3) + 2 * A(5) + 2 * A(6) + A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200941 /* -1 -1 0 0 0 1 3 2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200942 t[6] = 0 - A(0) - A(1) + A(5) + 3 * A(6) + 2 * A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200943 /* 1 0 -1 -1 -1 -1 0 3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200944 t[7] = 0 + A(0) - A(2) - A(3) - A(4) - A(5) + 3 * A(7);
945#undef A
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200946
Denys Vlasenko12040122021-04-26 20:24:34 +0200947 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
948 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
949 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
950 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
951 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
952 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
953 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200954 o = t[7] >> 32; //t[7] &= 0xffffffff;
Denys Vlasenko12040122021-04-26 20:24:34 +0200955 t[0] += o;
956 t[3] -= o;
957 t[6] -= o;
958 t[7] += o;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200959 r[0] = (sp_digit)t[0];
960 t[1] += t[0] >> 32;
961 r[1] = (sp_digit)t[1];
962 t[2] += t[1] >> 32;
963 r[2] = (sp_digit)t[2];
964 t[3] += t[2] >> 32;
965 r[3] = (sp_digit)t[3];
966 t[4] += t[3] >> 32;
967 r[4] = (sp_digit)t[4];
968 t[5] += t[4] >> 32;
969 r[5] = (sp_digit)t[5];
970 t[6] += t[5] >> 32;
971 r[6] = (sp_digit)t[6];
972// t[7] += t[6] >> 32;
973// r[7] = (sp_digit)t[7];
974 r[7] = (sp_digit)t[7] + (sp_digit)(t[6] >> 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200975}
976
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200977/* Map the Montgomery form projective co-ordinate point to an affine point.
978 *
979 * r Resulting affine co-ordinate point.
980 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200981 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200982static void sp_256_map_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200983{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200984 sp_digit t1[2*8];
985 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200986
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200987 sp_256_mont_inv_8(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200988
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200989 sp_256_mont_sqr_8(t2, t1 /*, p256_mod, p256_mp_mod*/);
990 sp_256_mont_mul_8(t1, t2, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200991
Denys Vlasenko12040122021-04-26 20:24:34 +0200992 /* x /= z^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200993 sp_256_mont_mul_8(r->x, p->x, t2 /*, p256_mod, p256_mp_mod*/);
994 memset(r->x + 8, 0, sizeof(r->x) / 2);
995 sp_256_mont_reduce_8(r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +0200996 /* Reduce x to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200997 if (sp_256_cmp_8(r->x, p256_mod) >= 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200998 sp_256_sub_8_p256_mod(r->x, r->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200999 sp_256_norm_8(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001000
Denys Vlasenko12040122021-04-26 20:24:34 +02001001 /* y /= z^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001002 sp_256_mont_mul_8(r->y, p->y, t1 /*, p256_mod, p256_mp_mod*/);
1003 memset(r->y + 8, 0, sizeof(r->y) / 2);
1004 sp_256_mont_reduce_8(r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001005 /* Reduce y to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001006 if (sp_256_cmp_8(r->y, p256_mod) >= 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001007 sp_256_sub_8_p256_mod(r->y, r->y /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001008 sp_256_norm_8(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001009
Denys Vlasenko12040122021-04-26 20:24:34 +02001010 memset(r->z, 0, sizeof(r->z));
1011 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001012}
1013
1014/* Double the Montgomery form projective point p.
1015 *
1016 * r Result of doubling point.
1017 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001018 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001019static void sp_256_proj_point_dbl_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001020{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001021 sp_digit t1[2*8];
1022 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001023
Denys Vlasenko12040122021-04-26 20:24:34 +02001024 /* Put point to double into result */
1025 if (r != p)
1026 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +02001027
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001028 if (r->infinity)
Denys Vlasenkoe7305052021-10-05 13:30:48 +02001029 return;
1030
Denys Vlasenko55578f22021-10-05 19:45:56 +02001031 if (SP_DEBUG) {
1032 /* unused part of t2, may result in spurios
1033 * differences in debug output. Clear it.
1034 */
1035 memset(t2, 0, sizeof(t2));
1036 }
1037
Denys Vlasenko12040122021-04-26 20:24:34 +02001038 /* T1 = Z * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001039 sp_256_mont_sqr_8(t1, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001040 /* Z = Y * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001041 sp_256_mont_mul_8(r->z, r->y, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001042 /* Z = 2Z */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001043 sp_256_mont_dbl_8(r->z, r->z /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001044 /* T2 = X - T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001045 sp_256_mont_sub_8(t2, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001046 /* T1 = X + T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001047 sp_256_mont_add_8(t1, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001048 /* T2 = T1 * T2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001049 sp_256_mont_mul_8(t2, t1, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001050 /* T1 = 3T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001051 sp_256_mont_tpl_8(t1, t2 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001052 /* Y = 2Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001053 sp_256_mont_dbl_8(r->y, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001054 /* Y = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001055 sp_256_mont_sqr_8(r->y, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001056 /* T2 = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001057 sp_256_mont_sqr_8(t2, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001058 /* T2 = T2/2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001059 sp_256_div2_8(t2, t2, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +02001060 /* Y = Y * X */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001061 sp_256_mont_mul_8(r->y, r->y, r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001062 /* X = T1 * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001063 sp_256_mont_mul_8(r->x, t1, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001064 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001065 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001066 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001067 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001068 /* Y = Y - X */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001069 sp_256_mont_sub_8(r->y, r->y, r->x /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001070 /* Y = Y * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001071 sp_256_mont_mul_8(r->y, r->y, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001072 /* Y = Y - T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001073 sp_256_mont_sub_8(r->y, r->y, t2 /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001074 dump_512("y2 %s\n", r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001075}
1076
1077/* Add two Montgomery form projective points.
1078 *
1079 * r Result of addition.
1080 * p Frist point to add.
1081 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001082 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001083static void sp_256_proj_point_add_8(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001084{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001085 sp_digit t1[2*8];
1086 sp_digit t2[2*8];
1087 sp_digit t3[2*8];
1088 sp_digit t4[2*8];
1089 sp_digit t5[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001090
Denys Vlasenko12040122021-04-26 20:24:34 +02001091 /* Ensure only the first point is the same as the result. */
1092 if (q == r) {
1093 sp_point* a = p;
1094 p = q;
1095 q = a;
1096 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001097
Denys Vlasenko12040122021-04-26 20:24:34 +02001098 /* Check double */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001099 sp_256_sub_8(t1, p256_mod, q->y);
1100 sp_256_norm_8(t1);
1101 if (sp_256_cmp_equal_8(p->x, q->x)
1102 && sp_256_cmp_equal_8(p->z, q->z)
1103 && (sp_256_cmp_equal_8(p->y, q->y) || sp_256_cmp_equal_8(p->y, t1))
Denys Vlasenko12040122021-04-26 20:24:34 +02001104 ) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001105 sp_256_proj_point_dbl_8(r, p);
Denys Vlasenko12040122021-04-26 20:24:34 +02001106 }
1107 else {
1108 sp_point tp;
1109 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001110
Denys Vlasenko12040122021-04-26 20:24:34 +02001111 v = r;
1112 if (p->infinity | q->infinity) {
1113 memset(&tp, 0, sizeof(tp));
1114 v = &tp;
1115 }
Denys Vlasenko772e1872021-04-26 17:25:27 +02001116
Denys Vlasenko12040122021-04-26 20:24:34 +02001117 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001118
Denys Vlasenko12040122021-04-26 20:24:34 +02001119 /* U1 = X1*Z2^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001120 sp_256_mont_sqr_8(t1, q->z /*, p256_mod, p256_mp_mod*/);
1121 sp_256_mont_mul_8(t3, t1, q->z /*, p256_mod, p256_mp_mod*/);
1122 sp_256_mont_mul_8(t1, t1, v->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001123 /* U2 = X2*Z1^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001124 sp_256_mont_sqr_8(t2, v->z /*, p256_mod, p256_mp_mod*/);
1125 sp_256_mont_mul_8(t4, t2, v->z /*, p256_mod, p256_mp_mod*/);
1126 sp_256_mont_mul_8(t2, t2, q->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001127 /* S1 = Y1*Z2^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001128 sp_256_mont_mul_8(t3, t3, v->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001129 /* S2 = Y2*Z1^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001130 sp_256_mont_mul_8(t4, t4, q->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001131 /* H = U2 - U1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001132 sp_256_mont_sub_8(t2, t2, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001133 /* R = S2 - S1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001134 sp_256_mont_sub_8(t4, t4, t3 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001135 /* Z3 = H*Z1*Z2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001136 sp_256_mont_mul_8(v->z, v->z, q->z /*, p256_mod, p256_mp_mod*/);
1137 sp_256_mont_mul_8(v->z, v->z, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001138 /* X3 = R^2 - H^3 - 2*U1*H^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001139 sp_256_mont_sqr_8(v->x, t4 /*, p256_mod, p256_mp_mod*/);
1140 sp_256_mont_sqr_8(t5, t2 /*, p256_mod, p256_mp_mod*/);
1141 sp_256_mont_mul_8(v->y, t1, t5 /*, p256_mod, p256_mp_mod*/);
1142 sp_256_mont_mul_8(t5, t5, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001143 sp_256_mont_sub_8(v->x, v->x, t5 /*, p256_mod*/);
1144 sp_256_mont_dbl_8(t1, v->y /*, p256_mod*/);
1145 sp_256_mont_sub_8(v->x, v->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001146 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001147 sp_256_mont_sub_8(v->y, v->y, v->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001148 sp_256_mont_mul_8(v->y, v->y, t4 /*, p256_mod, p256_mp_mod*/);
1149 sp_256_mont_mul_8(t5, t5, t3 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001150 sp_256_mont_sub_8(v->y, v->y, t5 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001151 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001152}
1153
1154/* Multiply the point by the scalar and return the result.
1155 * If map is true then convert result to affine co-ordinates.
1156 *
1157 * r Resulting point.
1158 * g Point to multiply.
1159 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001160 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001161 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001162static 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 +02001163{
Denys Vlasenko12040122021-04-26 20:24:34 +02001164 enum { map = 1 }; /* we always convert result to affine coordinates */
1165 sp_point t[3];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001166 sp_digit n = n; /* for compiler */
Denys Vlasenko12040122021-04-26 20:24:34 +02001167 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001168
Denys Vlasenko12040122021-04-26 20:24:34 +02001169 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001170
Denys Vlasenko12040122021-04-26 20:24:34 +02001171 /* t[0] = {0, 0, 1} * norm */
1172 t[0].infinity = 1;
1173 /* t[1] = {g->x, g->y, g->z} * norm */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001174 sp_256_mod_mul_norm_8(t[1].x, g->x);
1175 sp_256_mod_mul_norm_8(t[1].y, g->y);
1176 sp_256_mod_mul_norm_8(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001177
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001178 /* For every bit, starting from most significant... */
1179 k += 7;
1180 c = 256;
1181 for (;;) {
1182 if ((c & 0x1f) == 0) {
1183 if (c == 0)
Denys Vlasenko12040122021-04-26 20:24:34 +02001184 break;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001185 n = *k--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001186 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001187
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001188 y = (n >> 31);
1189 dbg("y:%d t[%d] = t[0]+t[1]\n", y, y^1);
1190 sp_256_proj_point_add_8(&t[y^1], &t[0], &t[1]);
1191 dump_512("t[0].x %s\n", t[0].x);
1192 dump_512("t[0].y %s\n", t[0].y);
1193 dump_512("t[0].z %s\n", t[0].z);
1194 dump_512("t[1].x %s\n", t[1].x);
1195 dump_512("t[1].y %s\n", t[1].y);
1196 dump_512("t[1].z %s\n", t[1].z);
1197 dbg("t[2] = t[%d]\n", y);
Denys Vlasenko12040122021-04-26 20:24:34 +02001198 memcpy(&t[2], &t[y], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001199 dbg("t[2] *= 2\n");
1200 sp_256_proj_point_dbl_8(&t[2], &t[2]);
1201 dump_512("t[2].x %s\n", t[2].x);
1202 dump_512("t[2].y %s\n", t[2].y);
1203 dump_512("t[2].z %s\n", t[2].z);
Denys Vlasenko12040122021-04-26 20:24:34 +02001204 memcpy(&t[y], &t[2], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001205
1206 n <<= 1;
1207 c--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001208 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001209
Denys Vlasenko12040122021-04-26 20:24:34 +02001210 if (map)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001211 sp_256_map_8(r, &t[0]);
Denys Vlasenko12040122021-04-26 20:24:34 +02001212 else
1213 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001214
Denys Vlasenko12040122021-04-26 20:24:34 +02001215 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001216}
1217
1218/* Multiply the base point of P256 by the scalar and return the result.
1219 * If map is true then convert result to affine co-ordinates.
1220 *
1221 * r Resulting point.
1222 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001223 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001224 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001225static void sp_256_ecc_mulmod_base_8(sp_point* r, sp_digit* k /*, int map*/)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001226{
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001227 /* Since this function is called only once, save space:
1228 * don't have "static const sp_point p256_base = {...}",
1229 * it would have more zeros than data.
1230 */
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001231 static const uint8_t p256_base_bin[] = {
1232 /* x (big-endian) */
1233 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,
1234 /* y */
1235 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 +02001236 /* z will be set to 1, infinity flag to "false" */
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001237 };
1238 sp_point p256_base;
1239
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001240 sp_256_point_from_bin2x32(&p256_base, p256_base_bin);
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001241
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001242 sp_256_ecc_mulmod_8(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001243}
1244
1245/* Multiply the point by the scalar and serialize the X ordinate.
1246 * The number is 0 padded to maximum size on output.
1247 *
1248 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001249 * pub2x32 Point to multiply.
1250 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001251 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001252static 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 +02001253{
Denys Vlasenko12040122021-04-26 20:24:34 +02001254 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001255
1256#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +02001257 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001258#endif
Denys Vlasenko12040122021-04-26 20:24:34 +02001259 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
1260 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001261
Denys Vlasenko12040122021-04-26 20:24:34 +02001262 sp_256_point_from_bin2x32(point, pub2x32);
Denys Vlasenko81d8af12021-10-05 17:31:33 +02001263 dump_512("point->x %s\n", point->x);
1264 dump_512("point->y %s\n", point->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001265
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001266 sp_256_ecc_mulmod_8(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001267
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001268 sp_256_to_bin_8(point->x, out32);
Denys Vlasenko12040122021-04-26 20:24:34 +02001269 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001270}
1271
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001272/* Generates a random scalar in [1..order-1] range. */
1273static void sp_256_ecc_gen_k_8(sp_digit k[8])
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001274{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001275 /* Since 32-bit words are "dense", no need to use
1276 * sp_256_from_bin_8(k, buf) to convert random stream
1277 * to sp_digit array - just store random bits there directly.
1278 */
1279 tls_get_random(k, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001280#if FIXED_SECRET
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001281 memset(k, 0x77, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001282#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001283
1284// If scalar is too large, try again (pseudo-code)
1285// if (k >= 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 - 1) // order of P256
1286// goto pick_another_random;
1287// k++; // ensure non-zero
1288 /* Simpler alternative, at the cost of not choosing some valid
1289 * random values, and slightly non-uniform distribution */
1290 if (k[0] == 0)
1291 k[0] = 1;
1292 if (k[7] >= 0xffffffff)
1293 k[7] = 0xfffffffe;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001294}
1295
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001296/* Makes a random EC key pair. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001297static void sp_ecc_make_key_256(sp_digit privkey[8], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001298{
1299 sp_point point[1];
1300
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001301 sp_256_ecc_gen_k_8(privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001302 dump_256("privkey %s\n", privkey);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001303 sp_256_ecc_mulmod_base_8(point, privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001304 dump_512("point->x %s\n", point->x);
1305 dump_512("point->y %s\n", point->y);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001306 sp_256_to_bin_8(point->x, pubkey);
1307 sp_256_to_bin_8(point->y, pubkey + 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001308
1309 memset(point, 0, sizeof(point)); //paranoia
1310}
1311
1312void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001313 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001314 const uint8_t *peerkey2x32)
1315{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001316 sp_digit privkey[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001317
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001318 dump_hex("peerkey2x32: %s\n", peerkey2x32, 64);
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001319 sp_ecc_make_key_256(privkey, pubkey2x32);
1320 dump_hex("pubkey: %s\n", pubkey2x32, 32);
1321 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001322
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001323 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001324 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
1325 dump_hex("premaster: %s\n", premaster32, 32);
1326}