blob: 1ab6106a71f7942c15e1906827df48471f571138 [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 Vlasenko3b411eb2021-10-05 20:00:50 +0200572 uint64_t t = 0;
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200573
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200574// for (i = 0; i < 8; i++) {
575// uint32_t t_hi;
576// uint64_t m = ((uint64_t)b * a[i]) + r[i];
577// t += m;
578// t_hi = (t < m);
579// r[i] = (sp_digit)t;
580// t = (t >> 32) | ((uint64_t)t_hi << 32);
581// }
582// r[8] += (sp_digit)t;
583
584 // Unroll, then optimize the above loop:
585 //uint32_t t_hi;
586 uint64_t m;
587
588 //m = ((uint64_t)b * a[0]) + r[0];
589 // Since b is r[0] and a[0] is ffffffff, the above optimizes to:
590 // m = r[0] * ffffffff + r[0] = (r[0] * 100000000 - r[0]) + r[0] = r[0] << 32;
591 //t += m;
592 // t = (uint64_t)r[0] << 32;
593 //t_hi = (t < m);
594 // t_hi = 0;
595 //r[0] = (sp_digit)t;
596 r[0] = 0;
597 //t = (t >> 32) | ((uint64_t)t_hi << 32);
598 // t = b;
599
600 //m = ((uint64_t)b * a[1]) + r[1];
601 // Since a[1] is ffffffff, the above optimizes to:
602 // m = b * ffffffff + r[1] = (b * 100000000 - b) + r[1] = (b << 32) - b + r[1];
603 //t += m;
604 // t = b + (b << 32) - b + r[1] = (b << 32) + r[1];
605 //t_hi = (t < m);
606 // t_hi = 0;
607 //r[1] = (sp_digit)t;
608 // r[1] = r[1];
609 //t = (t >> 32) | ((uint64_t)t_hi << 32);
610 // t = b;
611
612 //m = ((uint64_t)b * a[2]) + r[2];
613 // Since a[2] is ffffffff, the above optimizes to:
614 // m = b * ffffffff + r[2] = (b * 100000000 - b) + r[2] = (b << 32) - b + r[2];
615 //t += m;
616 // t = b + (b << 32) - b + r[2] = (b << 32) + r[2]
617 //t_hi = (t < m);
618 // t_hi = 0;
619 //r[2] = (sp_digit)t;
620 // r[2] = r[2];
621 //t = (t >> 32) | ((uint64_t)t_hi << 32);
622 // t = b;
623
624 //m = ((uint64_t)b * a[3]) + r[3];
625 // Since a[3] is 00000000, the above optimizes to:
626 // m = b * 0 + r[3] = r[3];
627 //t += m;
628 // t += r[3];
629 //t_hi = (t < m);
630 // t_hi = 0;
631 //r[3] = (sp_digit)t;
632 r[3] = r[3] + b;
633 //t = (t >> 32) | ((uint64_t)t_hi << 32);
634 t = (r[3] < b);
635
636 //m = ((uint64_t)b * a[4]) + r[4];
637 // Since a[4] is 00000000, the above optimizes to:
638 // m = b * 0 + r[4] = r[4];
639 //t += m;
640 t += r[4];
641 //t_hi = (t < m);
642 // t_hi = 0;
643 r[4] = (sp_digit)t;
644 //t = (t >> 32) | ((uint64_t)t_hi << 32);
645 t = (t >> 32);
646
647 //m = ((uint64_t)b * a[5]) + r[5];
648 // Since a[5] is 00000000, the above optimizes to:
649 // m = b * 0 + r[5] = r[5];
650 //t += m;
651 t += r[5];
652 //t_hi = (t < m);
653 // t_hi = 0;
654 r[5] = (sp_digit)t;
655 //t = (t >> 32) | ((uint64_t)t_hi << 32);
656 t = (t >> 32);
657
658 //m = ((uint64_t)b * a[6]) + r[6];
659 // Since a[6] is 00000001, the above optimizes to:
660 m = (uint64_t)b + r[6]; // 33 bits at most
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200661 t += m;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200662 //t_hi = (t < m);
663 // t_hi = 0; //32bit_value + 33bit_value can't overflow 64 bits
664 r[6] = (sp_digit)t;
665 //t = (t >> 32) | ((uint64_t)t_hi << 32);
666 t = (t >> 32);
667
668 //m = ((uint64_t)b * a[7]) + r[7];
669 // Since a[7] is ffffffff, the above optimizes to:
670 // m = b * ffffffff + r[7] = (b * 100000000 - b) + r[7]
671 m = ((uint64_t)b << 32) - b + r[7];
672 t += m;
673 //t_hi = (t < m);
674 // t_hi in fact is always 0 here
675 r[7] = (sp_digit)t;
676 //t = (t >> 32) | ((uint64_t)t_hi << 32);
677 t = (t >> 32);
678
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200679 r[8] += (sp_digit)t;
680 return (r[8] < (sp_digit)t); /* 1 if addition overflowed */
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200681}
682
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200683/* Reduce the number back to 256 bits using Montgomery reduction.
684 *
685 * a A single precision number to reduce in place.
686 * m The single precision number representing the modulus.
687 * mp The digit representing the negative inverse of m mod 2^n.
688 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200689static void sp_256_mont_reduce_8(sp_digit* a/*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200690{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200691// const sp_digit* m = p256_mod;
Denys Vlasenko389329e2021-10-05 13:39:33 +0200692 sp_digit mp = p256_mp_mod;
693
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200694 int i;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200695// sp_digit mu;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200696
697 if (mp != 1) {
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200698 sp_digit word16th = 0;
699 for (i = 0; i < 8; i++) {
700// mu = (sp_digit)(a[i] * mp);
701 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
702 int j = i + 8;
703 inc_next_word0:
704 if (++j > 15) { /* a[16] array has no more words? */
705 word16th++;
706 continue;
707 }
708 if (++a[j] == 0) /* did this overflow too? */
709 goto inc_next_word0;
710 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200711 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200712 sp_256_mont_shift_8(a, a);
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200713 if (word16th != 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200714 sp_256_sub_8_p256_mod(a, a /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200715 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200716 }
Denys Vlasenko389329e2021-10-05 13:39:33 +0200717 else { /* Same code for explicit mp == 1 (which is always the case for P256) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200718 sp_digit word16th = 0;
719 for (i = 0; i < 8; i++) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200720 /*mu = a[i];*/
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200721 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200722 int j = i + 8;
723 inc_next_word:
724 if (++j > 15) { /* a[16] array has no more words? */
725 word16th++;
726 continue;
727 }
728 if (++a[j] == 0) /* did this overflow too? */
729 goto inc_next_word;
730 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200731 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200732 sp_256_mont_shift_8(a, a);
733 if (word16th != 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200734 sp_256_sub_8_p256_mod(a, a /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200735 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200736 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200737}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200738#if 0
739//TODO: arm32 asm (also adapt for x86?)
740static void sp_256_mont_reduce_8(sp_digit* a, sp_digit* m, sp_digit mp)
741{
742 sp_digit ca = 0;
743
744 asm volatile (
745 # i = 0
746 mov r12, #0 # i = 0
747 ldr r10, [%[a], #0] # r10 = a[0]
748 ldr r14, [%[a], #4] # r14 = a[1]
7491:
750 # mu = a[i] * mp #
751 mul r8, %[mp], r10 # mu = a[i] * mp
752 # a[i+0] += m[0] * mu #
753 ldr r7, [%[m], #0] # a[i+0] += m[0] * mu
754 ldr r9, [%[a], #0] #
755 umull r6, r7, r8, r7 # r7:r6 = mu * m[0]
756 adds r10, r10, r6 # r5:r10 += r7:r6
757 adc r5, r7, #0 #
758 # a[i+1] += m[1] * mu #
759 ldr r7, [%[m], #4] # a[i+1] += m[1] * mu
760 ldr r9, [%[a], #4] #
761 umull r6, r7, r8, r7 # r7:r6 = mu * m[1]
762 adds r10, r14, r6 # r4:r10 = r7:r14 + r7:r6
763 adc r4, r7, #0 #
764 adds r10, r10, r5 # r4:r10 += r5
765 adc r4, r4, #0 #
766 # a[i+2] += m[2] * mu #
767 ldr r7, [%[m], #8] # a[i+2] += m[2] * mu
768 ldr r14, [%[a], #8] #
769 umull r6, r7, r8, r7 #
770 adds r14, r14, r6 #
771 adc r5, r7, #0 #
772 adds r14, r14, r4 #
773 adc r5, r5, #0 #
774 # a[i+3] += m[3] * mu #
775 ldr r7, [%[m], #12] # a[i+3] += m[3] * mu
776 ldr r9, [%[a], #12] #
777 umull r6, r7, r8, r7 #
778 adds r9, r9, r6 #
779 adc r4, r7, #0 #
780 adds r9, r9, r5 #
781 str r9, [%[a], #12] # a[3] = r9
782 adc r4, r4, #0 #
783 # a[i+4] += m[4] * mu #
784 ldr r7, [%[m], #16] # a[i+4] += m[4] * mu
785 ldr r9, [%[a], #16] #
786 umull r6, r7, r8, r7 #
787 adds r9, r9, r6 #
788 adc r5, r7, #0 #
789 adds r9, r9, r4 #
790 str r9, [%[a], #16] # a[4] = r9
791 adc r5, r5, #0 #
792 # a[i+5] += m[5] * mu #
793 ldr r7, [%[m], #20] # a[i+5] += m[5] * mu
794 ldr r9, [%[a], #20] #
795 umull r6, r7, r8, r7 #
796 adds r9, r9, r6 #
797 adc r4, r7, #0 #
798 adds r9, r9, r5 #
799 str r9, [%[a], #20] # a[5] = r9
800 adc r4, r4, #0 #
801 # a[i+6] += m[6] * mu #
802 ldr r7, [%[m], #24] # a[i+6] += m[6] * mu
803 ldr r9, [%[a], #24] #
804 umull r6, r7, r8, r7 #
805 adds r9, r9, r6 #
806 adc r5, r7, #0 #
807 adds r9, r9, r4 #
808 str r9, [%[a], #24] # a[6] = r9
809 adc r5, r5, #0 #
810 # a[i+7] += m[7] * mu #
811 ldr r7, [%[m], #28] # a[i+7] += m[7] * mu
812 ldr r9, [%[a], #28] #
813 umull r6, r7, r8, r7 #
814 adds r5, r5, r6 #
815 adcs r7, r7, %[ca] #
816 mov %[ca], #0 #
817 adc %[ca], %[ca], %[ca] # ca = CF
818 adds r9, r9, r5 #
819 str r9, [%[a], #28] # a[7] = r9
820 ldr r9, [%[a], #32] # r9 = a[8]
821 adcs r9, r9, r7 #
822 str r9, [%[a], #32] # a[8] = r9
823 adc %[ca], %[ca], #0 # ca += CF
824 # i += 1 # i++
825 add %[a], %[a], #4 # a++
826 add r12, r12, #4 # i += 4
827 cmp r12, #32 # if (i < 32)
828 blt 1b # goto 1
829
830 str r10, [%[a], #0] # a[0] = r10
831 str r14, [%[a], #4] # a[1] = r14
832 : [ca] "+r" (ca), [a] "+r" (a)
833 : [m] "r" (m), [mp] "r" (mp)
834 : "memory", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r14", "r12"
835 );
836
837 if (ca)
838 a -= m;
839}
840#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200841
842/* Multiply two Montogmery form numbers mod the modulus (prime).
843 * (r = a * b mod m)
844 *
845 * r Result of multiplication.
846 * a First number to multiply in Montogmery form.
847 * b Second number to multiply in Montogmery form.
848 * m Modulus (prime).
849 * mp Montogmery mulitplier.
850 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200851static void sp_256_mont_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b
Denys Vlasenko389329e2021-10-05 13:39:33 +0200852 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200853{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200854 //const sp_digit* m = p256_mod;
855 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200856 sp_256_mul_8(r, a, b);
857 sp_256_mont_reduce_8(r /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200858}
859
860/* Square the Montgomery form number. (r = a * a mod m)
861 *
862 * r Result of squaring.
863 * a Number to square in Montogmery form.
864 * m Modulus (prime).
865 * mp Montogmery mulitplier.
866 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200867static void sp_256_mont_sqr_8(sp_digit* r, const sp_digit* a
Denys Vlasenko389329e2021-10-05 13:39:33 +0200868 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200869{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200870 //const sp_digit* m = p256_mod;
871 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200872 sp_256_mont_mul_8(r, a, a /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200873}
874
875/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
876 * P256 curve. (r = 1 / a mod m)
877 *
878 * r Inverse result.
879 * a Number to invert.
880 */
881#if 0
882/* Mod-2 for the P256 curve. */
883static const uint32_t p256_mod_2[8] = {
884 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
885 0x00000000,0x00000000,0x00000001,0xffffffff,
886};
887//Bit pattern:
888//2 2 2 2 2 2 2 1...1
889//5 5 4 3 2 1 0 9...0 9...1
890//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
891//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
892#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200893static void sp_256_mont_inv_8(sp_digit* r, sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200894{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200895 sp_digit t[2*8]; //can be just [8]?
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200896 int i;
897
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200898 memcpy(t, a, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200899 for (i = 254; i >= 0; i--) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200900 sp_256_mont_sqr_8(t, t /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200901 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
902 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200903 sp_256_mont_mul_8(t, t, a /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200904 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200905 memcpy(r, t, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200906}
907
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200908/* Multiply a number by Montogmery normalizer mod modulus (prime).
909 *
910 * r The resulting Montgomery form number.
911 * a The number to convert.
912 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200913static void sp_256_mod_mul_norm_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200914{
Denys Vlasenko12040122021-04-26 20:24:34 +0200915 int64_t t[8];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200916 int32_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200917
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200918#define A(n) ((uint64_t)a[n])
Denys Vlasenko12040122021-04-26 20:24:34 +0200919 /* 1 1 0 -1 -1 -1 -1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200920 t[0] = 0 + A(0) + A(1) - A(3) - A(4) - A(5) - A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +0200921 /* 0 1 1 0 -1 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200922 t[1] = 0 + A(1) + A(2) - A(4) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200923 /* 0 0 1 1 0 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200924 t[2] = 0 + A(2) + A(3) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200925 /* -1 -1 0 2 2 1 0 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200926 t[3] = 0 - A(0) - A(1) + 2 * A(3) + 2 * A(4) + A(5) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200927 /* 0 -1 -1 0 2 2 1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200928 t[4] = 0 - A(1) - A(2) + 2 * A(4) + 2 * A(5) + A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +0200929 /* 0 0 -1 -1 0 2 2 1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200930 t[5] = 0 - A(2) - A(3) + 2 * A(5) + 2 * A(6) + A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200931 /* -1 -1 0 0 0 1 3 2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200932 t[6] = 0 - A(0) - A(1) + A(5) + 3 * A(6) + 2 * A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200933 /* 1 0 -1 -1 -1 -1 0 3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200934 t[7] = 0 + A(0) - A(2) - A(3) - A(4) - A(5) + 3 * A(7);
935#undef A
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200936
Denys Vlasenko12040122021-04-26 20:24:34 +0200937 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
938 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
939 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
940 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
941 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
942 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
943 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200944 o = t[7] >> 32; //t[7] &= 0xffffffff;
Denys Vlasenko12040122021-04-26 20:24:34 +0200945 t[0] += o;
946 t[3] -= o;
947 t[6] -= o;
948 t[7] += o;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200949 r[0] = (sp_digit)t[0];
950 t[1] += t[0] >> 32;
951 r[1] = (sp_digit)t[1];
952 t[2] += t[1] >> 32;
953 r[2] = (sp_digit)t[2];
954 t[3] += t[2] >> 32;
955 r[3] = (sp_digit)t[3];
956 t[4] += t[3] >> 32;
957 r[4] = (sp_digit)t[4];
958 t[5] += t[4] >> 32;
959 r[5] = (sp_digit)t[5];
960 t[6] += t[5] >> 32;
961 r[6] = (sp_digit)t[6];
962// t[7] += t[6] >> 32;
963// r[7] = (sp_digit)t[7];
964 r[7] = (sp_digit)t[7] + (sp_digit)(t[6] >> 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200965}
966
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200967/* Map the Montgomery form projective co-ordinate point to an affine point.
968 *
969 * r Resulting affine co-ordinate point.
970 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200971 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200972static void sp_256_map_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200973{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200974 sp_digit t1[2*8];
975 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200976
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200977 sp_256_mont_inv_8(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200978
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200979 sp_256_mont_sqr_8(t2, t1 /*, p256_mod, p256_mp_mod*/);
980 sp_256_mont_mul_8(t1, t2, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200981
Denys Vlasenko12040122021-04-26 20:24:34 +0200982 /* x /= z^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200983 sp_256_mont_mul_8(r->x, p->x, t2 /*, p256_mod, p256_mp_mod*/);
984 memset(r->x + 8, 0, sizeof(r->x) / 2);
985 sp_256_mont_reduce_8(r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +0200986 /* Reduce x to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200987 if (sp_256_cmp_8(r->x, p256_mod) >= 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200988 sp_256_sub_8_p256_mod(r->x, r->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200989 sp_256_norm_8(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200990
Denys Vlasenko12040122021-04-26 20:24:34 +0200991 /* y /= z^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200992 sp_256_mont_mul_8(r->y, p->y, t1 /*, p256_mod, p256_mp_mod*/);
993 memset(r->y + 8, 0, sizeof(r->y) / 2);
994 sp_256_mont_reduce_8(r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +0200995 /* Reduce y to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200996 if (sp_256_cmp_8(r->y, p256_mod) >= 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200997 sp_256_sub_8_p256_mod(r->y, r->y /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200998 sp_256_norm_8(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200999
Denys Vlasenko12040122021-04-26 20:24:34 +02001000 memset(r->z, 0, sizeof(r->z));
1001 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001002}
1003
1004/* Double the Montgomery form projective point p.
1005 *
1006 * r Result of doubling point.
1007 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001008 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001009static void sp_256_proj_point_dbl_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001010{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001011 sp_digit t1[2*8];
1012 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001013
Denys Vlasenko12040122021-04-26 20:24:34 +02001014 /* Put point to double into result */
1015 if (r != p)
1016 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +02001017
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001018 if (r->infinity)
Denys Vlasenkoe7305052021-10-05 13:30:48 +02001019 return;
1020
Denys Vlasenko55578f22021-10-05 19:45:56 +02001021 if (SP_DEBUG) {
1022 /* unused part of t2, may result in spurios
1023 * differences in debug output. Clear it.
1024 */
1025 memset(t2, 0, sizeof(t2));
1026 }
1027
Denys Vlasenko12040122021-04-26 20:24:34 +02001028 /* T1 = Z * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001029 sp_256_mont_sqr_8(t1, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001030 /* Z = Y * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001031 sp_256_mont_mul_8(r->z, r->y, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001032 /* Z = 2Z */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001033 sp_256_mont_dbl_8(r->z, r->z /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001034 /* T2 = X - T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001035 sp_256_mont_sub_8(t2, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001036 /* T1 = X + T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001037 sp_256_mont_add_8(t1, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001038 /* T2 = T1 * T2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001039 sp_256_mont_mul_8(t2, t1, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001040 /* T1 = 3T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001041 sp_256_mont_tpl_8(t1, t2 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001042 /* Y = 2Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001043 sp_256_mont_dbl_8(r->y, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001044 /* Y = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001045 sp_256_mont_sqr_8(r->y, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001046 /* T2 = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001047 sp_256_mont_sqr_8(t2, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001048 /* T2 = T2/2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001049 sp_256_div2_8(t2, t2, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +02001050 /* Y = Y * X */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001051 sp_256_mont_mul_8(r->y, r->y, r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001052 /* X = T1 * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001053 sp_256_mont_mul_8(r->x, t1, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001054 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001055 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001056 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001057 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001058 /* Y = Y - X */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001059 sp_256_mont_sub_8(r->y, r->y, r->x /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001060 /* Y = Y * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001061 sp_256_mont_mul_8(r->y, r->y, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001062 /* Y = Y - T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001063 sp_256_mont_sub_8(r->y, r->y, t2 /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001064 dump_512("y2 %s\n", r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001065}
1066
1067/* Add two Montgomery form projective points.
1068 *
1069 * r Result of addition.
1070 * p Frist point to add.
1071 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001072 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001073static void sp_256_proj_point_add_8(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001074{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001075 sp_digit t1[2*8];
1076 sp_digit t2[2*8];
1077 sp_digit t3[2*8];
1078 sp_digit t4[2*8];
1079 sp_digit t5[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001080
Denys Vlasenko12040122021-04-26 20:24:34 +02001081 /* Ensure only the first point is the same as the result. */
1082 if (q == r) {
1083 sp_point* a = p;
1084 p = q;
1085 q = a;
1086 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001087
Denys Vlasenko12040122021-04-26 20:24:34 +02001088 /* Check double */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001089 sp_256_sub_8(t1, p256_mod, q->y);
1090 sp_256_norm_8(t1);
1091 if (sp_256_cmp_equal_8(p->x, q->x)
1092 && sp_256_cmp_equal_8(p->z, q->z)
1093 && (sp_256_cmp_equal_8(p->y, q->y) || sp_256_cmp_equal_8(p->y, t1))
Denys Vlasenko12040122021-04-26 20:24:34 +02001094 ) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001095 sp_256_proj_point_dbl_8(r, p);
Denys Vlasenko12040122021-04-26 20:24:34 +02001096 }
1097 else {
1098 sp_point tp;
1099 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001100
Denys Vlasenko12040122021-04-26 20:24:34 +02001101 v = r;
1102 if (p->infinity | q->infinity) {
1103 memset(&tp, 0, sizeof(tp));
1104 v = &tp;
1105 }
Denys Vlasenko772e1872021-04-26 17:25:27 +02001106
Denys Vlasenko12040122021-04-26 20:24:34 +02001107 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001108
Denys Vlasenko12040122021-04-26 20:24:34 +02001109 /* U1 = X1*Z2^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001110 sp_256_mont_sqr_8(t1, q->z /*, p256_mod, p256_mp_mod*/);
1111 sp_256_mont_mul_8(t3, t1, q->z /*, p256_mod, p256_mp_mod*/);
1112 sp_256_mont_mul_8(t1, t1, v->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001113 /* U2 = X2*Z1^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001114 sp_256_mont_sqr_8(t2, v->z /*, p256_mod, p256_mp_mod*/);
1115 sp_256_mont_mul_8(t4, t2, v->z /*, p256_mod, p256_mp_mod*/);
1116 sp_256_mont_mul_8(t2, t2, q->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001117 /* S1 = Y1*Z2^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001118 sp_256_mont_mul_8(t3, t3, v->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001119 /* S2 = Y2*Z1^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001120 sp_256_mont_mul_8(t4, t4, q->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001121 /* H = U2 - U1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001122 sp_256_mont_sub_8(t2, t2, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001123 /* R = S2 - S1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001124 sp_256_mont_sub_8(t4, t4, t3 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001125 /* Z3 = H*Z1*Z2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001126 sp_256_mont_mul_8(v->z, v->z, q->z /*, p256_mod, p256_mp_mod*/);
1127 sp_256_mont_mul_8(v->z, v->z, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001128 /* X3 = R^2 - H^3 - 2*U1*H^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001129 sp_256_mont_sqr_8(v->x, t4 /*, p256_mod, p256_mp_mod*/);
1130 sp_256_mont_sqr_8(t5, t2 /*, p256_mod, p256_mp_mod*/);
1131 sp_256_mont_mul_8(v->y, t1, t5 /*, p256_mod, p256_mp_mod*/);
1132 sp_256_mont_mul_8(t5, t5, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001133 sp_256_mont_sub_8(v->x, v->x, t5 /*, p256_mod*/);
1134 sp_256_mont_dbl_8(t1, v->y /*, p256_mod*/);
1135 sp_256_mont_sub_8(v->x, v->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001136 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001137 sp_256_mont_sub_8(v->y, v->y, v->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001138 sp_256_mont_mul_8(v->y, v->y, t4 /*, p256_mod, p256_mp_mod*/);
1139 sp_256_mont_mul_8(t5, t5, t3 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001140 sp_256_mont_sub_8(v->y, v->y, t5 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001141 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001142}
1143
1144/* Multiply the point by the scalar and return the result.
1145 * If map is true then convert result to affine co-ordinates.
1146 *
1147 * r Resulting point.
1148 * g Point to multiply.
1149 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001150 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001151 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001152static 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 +02001153{
Denys Vlasenko12040122021-04-26 20:24:34 +02001154 enum { map = 1 }; /* we always convert result to affine coordinates */
1155 sp_point t[3];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001156 sp_digit n = n; /* for compiler */
Denys Vlasenko12040122021-04-26 20:24:34 +02001157 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001158
Denys Vlasenko12040122021-04-26 20:24:34 +02001159 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001160
Denys Vlasenko12040122021-04-26 20:24:34 +02001161 /* t[0] = {0, 0, 1} * norm */
1162 t[0].infinity = 1;
1163 /* t[1] = {g->x, g->y, g->z} * norm */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001164 sp_256_mod_mul_norm_8(t[1].x, g->x);
1165 sp_256_mod_mul_norm_8(t[1].y, g->y);
1166 sp_256_mod_mul_norm_8(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001167
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001168 /* For every bit, starting from most significant... */
1169 k += 7;
1170 c = 256;
1171 for (;;) {
1172 if ((c & 0x1f) == 0) {
1173 if (c == 0)
Denys Vlasenko12040122021-04-26 20:24:34 +02001174 break;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001175 n = *k--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001176 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001177
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001178 y = (n >> 31);
1179 dbg("y:%d t[%d] = t[0]+t[1]\n", y, y^1);
1180 sp_256_proj_point_add_8(&t[y^1], &t[0], &t[1]);
1181 dump_512("t[0].x %s\n", t[0].x);
1182 dump_512("t[0].y %s\n", t[0].y);
1183 dump_512("t[0].z %s\n", t[0].z);
1184 dump_512("t[1].x %s\n", t[1].x);
1185 dump_512("t[1].y %s\n", t[1].y);
1186 dump_512("t[1].z %s\n", t[1].z);
1187 dbg("t[2] = t[%d]\n", y);
Denys Vlasenko12040122021-04-26 20:24:34 +02001188 memcpy(&t[2], &t[y], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001189 dbg("t[2] *= 2\n");
1190 sp_256_proj_point_dbl_8(&t[2], &t[2]);
1191 dump_512("t[2].x %s\n", t[2].x);
1192 dump_512("t[2].y %s\n", t[2].y);
1193 dump_512("t[2].z %s\n", t[2].z);
Denys Vlasenko12040122021-04-26 20:24:34 +02001194 memcpy(&t[y], &t[2], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001195
1196 n <<= 1;
1197 c--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001198 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001199
Denys Vlasenko12040122021-04-26 20:24:34 +02001200 if (map)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001201 sp_256_map_8(r, &t[0]);
Denys Vlasenko12040122021-04-26 20:24:34 +02001202 else
1203 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001204
Denys Vlasenko12040122021-04-26 20:24:34 +02001205 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001206}
1207
1208/* Multiply the base point of P256 by the scalar and return the result.
1209 * If map is true then convert result to affine co-ordinates.
1210 *
1211 * r Resulting point.
1212 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001213 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001214 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001215static void sp_256_ecc_mulmod_base_8(sp_point* r, sp_digit* k /*, int map*/)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001216{
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001217 /* Since this function is called only once, save space:
1218 * don't have "static const sp_point p256_base = {...}",
1219 * it would have more zeros than data.
1220 */
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001221 static const uint8_t p256_base_bin[] = {
1222 /* x (big-endian) */
1223 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,
1224 /* y */
1225 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 +02001226 /* z will be set to 1, infinity flag to "false" */
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001227 };
1228 sp_point p256_base;
1229
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001230 sp_256_point_from_bin2x32(&p256_base, p256_base_bin);
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001231
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001232 sp_256_ecc_mulmod_8(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001233}
1234
1235/* Multiply the point by the scalar and serialize the X ordinate.
1236 * The number is 0 padded to maximum size on output.
1237 *
1238 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001239 * pub2x32 Point to multiply.
1240 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001241 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001242static 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 +02001243{
Denys Vlasenko12040122021-04-26 20:24:34 +02001244 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001245
1246#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +02001247 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001248#endif
Denys Vlasenko12040122021-04-26 20:24:34 +02001249 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
1250 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001251
Denys Vlasenko12040122021-04-26 20:24:34 +02001252 sp_256_point_from_bin2x32(point, pub2x32);
Denys Vlasenko81d8af12021-10-05 17:31:33 +02001253 dump_512("point->x %s\n", point->x);
1254 dump_512("point->y %s\n", point->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001255
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001256 sp_256_ecc_mulmod_8(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001257
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001258 sp_256_to_bin_8(point->x, out32);
Denys Vlasenko12040122021-04-26 20:24:34 +02001259 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001260}
1261
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001262/* Generates a random scalar in [1..order-1] range. */
1263static void sp_256_ecc_gen_k_8(sp_digit k[8])
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001264{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001265 /* Since 32-bit words are "dense", no need to use
1266 * sp_256_from_bin_8(k, buf) to convert random stream
1267 * to sp_digit array - just store random bits there directly.
1268 */
1269 tls_get_random(k, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001270#if FIXED_SECRET
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001271 memset(k, 0x77, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001272#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001273
1274// If scalar is too large, try again (pseudo-code)
1275// if (k >= 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 - 1) // order of P256
1276// goto pick_another_random;
1277// k++; // ensure non-zero
1278 /* Simpler alternative, at the cost of not choosing some valid
1279 * random values, and slightly non-uniform distribution */
1280 if (k[0] == 0)
1281 k[0] = 1;
1282 if (k[7] >= 0xffffffff)
1283 k[7] = 0xfffffffe;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001284}
1285
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001286/* Makes a random EC key pair. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001287static void sp_ecc_make_key_256(sp_digit privkey[8], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001288{
1289 sp_point point[1];
1290
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001291 sp_256_ecc_gen_k_8(privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001292 dump_256("privkey %s\n", privkey);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001293 sp_256_ecc_mulmod_base_8(point, privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001294 dump_512("point->x %s\n", point->x);
1295 dump_512("point->y %s\n", point->y);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001296 sp_256_to_bin_8(point->x, pubkey);
1297 sp_256_to_bin_8(point->y, pubkey + 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001298
1299 memset(point, 0, sizeof(point)); //paranoia
1300}
1301
1302void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001303 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001304 const uint8_t *peerkey2x32)
1305{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001306 sp_digit privkey[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001307
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001308 dump_hex("peerkey2x32: %s\n", peerkey2x32, 64);
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001309 sp_ecc_make_key_256(privkey, pubkey2x32);
1310 dump_hex("pubkey: %s\n", pubkey2x32, 32);
1311 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001312
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001313 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001314 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
1315 dump_hex("premaster: %s\n", premaster32, 32);
1316}