blob: 532047739b26f9e4b3558d69d80e010f459e5d68 [file] [log] [blame]
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001/*
2 * Copyright (C) 2021 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6#include "tls.h"
7
8#define SP_DEBUG 0
9#define FIXED_SECRET 0
10#define FIXED_PEER_PUBKEY 0
11
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020012#define ALLOW_ASM 1
13
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020014#if SP_DEBUG
15# define dbg(...) fprintf(stderr, __VA_ARGS__)
16static void dump_hex(const char *fmt, const void *vp, int len)
17{
18 char hexbuf[32 * 1024 + 4];
19 const uint8_t *p = vp;
20
21 bin2hex(hexbuf, (void*)p, len)[0] = '\0';
22 dbg(fmt, hexbuf);
23}
24#else
25# define dbg(...) ((void)0)
26# define dump_hex(...) ((void)0)
27#endif
28
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020029typedef uint32_t sp_digit;
30typedef int32_t signed_sp_digit;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020031
32/* The code below is taken from parts of
33 * wolfssl-3.15.3/wolfcrypt/src/sp_c32.c
34 * and heavily modified.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020035 */
36
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020037typedef struct sp_point {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020038 sp_digit x[2 * 8];
39 sp_digit y[2 * 8];
40 sp_digit z[2 * 8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020041 int infinity;
42} sp_point;
43
44/* The modulus (prime) of the curve P256. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020045static const sp_digit p256_mod[8] = {
46 0xffffffff,0xffffffff,0xffffffff,0x00000000,
47 0x00000000,0x00000000,0x00000001,0xffffffff,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020048};
49
50#define p256_mp_mod ((sp_digit)0x000001)
51
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020052/* Normalize the values in each word to 32 bits - NOP */
53#define sp_256_norm_8(a) ((void)0)
Denys Vlasenko77145182021-10-01 13:51:39 +020054
Denys Vlasenkoe7305052021-10-05 13:30:48 +020055/* Write r as big endian to byte array.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020056 * Fixed length number of bytes written: 32
57 *
58 * r A single precision integer.
59 * a Byte array.
60 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020061static void sp_256_to_bin_8(const sp_digit* r, uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020062{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020063 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020064
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020065 sp_256_norm_8(r);
Denys Vlasenko77145182021-10-01 13:51:39 +020066
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020067 r += 8;
68 for (i = 0; i < 8; i++) {
69 r--;
70 move_to_unaligned32(a, SWAP_BE32(*r));
71 a += 4;
Denys Vlasenko12040122021-04-26 20:24:34 +020072 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020073}
74
Denys Vlasenkoe7305052021-10-05 13:30:48 +020075/* Read big endian unsigned byte array into r.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020076 *
77 * r A single precision integer.
78 * a Byte array.
79 * n Number of bytes in array to read.
80 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020081static void sp_256_from_bin_8(sp_digit* r, const uint8_t* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020082{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020083 int i;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020084
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020085 r += 8;
86 for (i = 0; i < 8; i++) {
87 sp_digit v;
88 move_from_unaligned32(v, a);
89 *--r = SWAP_BE32(v);
90 a += 4;
Denys Vlasenko12040122021-04-26 20:24:34 +020091 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +020092}
93
Denys Vlasenko137864f2021-10-05 13:47:42 +020094#if SP_DEBUG
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020095static void dump_256(const char *fmt, const sp_digit* r)
Denys Vlasenko137864f2021-10-05 13:47:42 +020096{
Denys Vlasenko137864f2021-10-05 13:47:42 +020097 uint8_t b32[32];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +020098 sp_256_to_bin_8(r, b32);
Denys Vlasenko137864f2021-10-05 13:47:42 +020099 dump_hex(fmt, b32, 32);
100}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200101static void dump_512(const char *fmt, const sp_digit* r)
Denys Vlasenko137864f2021-10-05 13:47:42 +0200102{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200103 uint8_t b64[64];
104 sp_256_to_bin_8(r, b64 + 32);
105 sp_256_to_bin_8(r+8, b64);
106 dump_hex(fmt, b64, 64);
Denys Vlasenko137864f2021-10-05 13:47:42 +0200107}
108#else
109# define dump_256(...) ((void)0)
110# define dump_512(...) ((void)0)
111#endif
112
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200113/* Convert a point of big-endian 32-byte x,y pair to type sp_point. */
114static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32)
115{
Denys Vlasenko12040122021-04-26 20:24:34 +0200116 memset(p, 0, sizeof(*p));
117 /*p->infinity = 0;*/
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200118 sp_256_from_bin_8(p->x, bin2x32);
119 sp_256_from_bin_8(p->y, bin2x32 + 32);
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200120 p->z[0] = 1; /* p->z = 1 */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200121}
122
Denys Vlasenkob3b17132021-04-26 16:53:53 +0200123/* Compare a with b.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200124 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200125 * return -ve, 0 or +ve if a is less than, equal to or greater than b
126 * respectively.
127 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200128static signed_sp_digit sp_256_cmp_8(const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200129{
Denys Vlasenko12040122021-04-26 20:24:34 +0200130 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200131 for (i = 7; i >= 0; i--) {
132/* signed_sp_digit r = a[i] - b[i];
133 * if (r != 0)
134 * return r;
135 * does not work: think about a[i]=0, b[i]=0xffffffff
136 */
137 if (a[i] == b[i])
138 continue;
139 return (a[i] > b[i]) * 2 - 1;
Denys Vlasenko12040122021-04-26 20:24:34 +0200140 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200141 return 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200142}
143
144/* Compare two numbers to determine if they are equal.
145 *
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200146 * return 1 when equal and 0 otherwise.
147 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200148static int sp_256_cmp_equal_8(const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200149{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200150 return sp_256_cmp_8(a, b) == 0;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200151}
152
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200153/* Add b to a into r. (r = a + b). Return !0 on overflow */
154static int sp_256_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200155{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200156#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
157 sp_digit reg;
158 asm volatile (
159"\n movl (%0), %3"
160"\n addl (%1), %3"
161"\n movl %3, (%2)"
162"\n"
163"\n movl 1*4(%0), %3"
164"\n adcl 1*4(%1), %3"
165"\n movl %3, 1*4(%2)"
166"\n"
167"\n movl 2*4(%0), %3"
168"\n adcl 2*4(%1), %3"
169"\n movl %3, 2*4(%2)"
170"\n"
171"\n movl 3*4(%0), %3"
172"\n adcl 3*4(%1), %3"
173"\n movl %3, 3*4(%2)"
174"\n"
175"\n movl 4*4(%0), %3"
176"\n adcl 4*4(%1), %3"
177"\n movl %3, 4*4(%2)"
178"\n"
179"\n movl 5*4(%0), %3"
180"\n adcl 5*4(%1), %3"
181"\n movl %3, 5*4(%2)"
182"\n"
183"\n movl 6*4(%0), %3"
184"\n adcl 6*4(%1), %3"
185"\n movl %3, 6*4(%2)"
186"\n"
187"\n movl 7*4(%0), %3"
188"\n adcl 7*4(%1), %3"
189"\n movl %3, 7*4(%2)"
190"\n"
191"\n sbbl %3, %3"
192"\n"
193 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
194 : "0" (a), "1" (b), "2" (r)
195 : "memory"
196 );
197 return reg;
198#else
Denys Vlasenko12040122021-04-26 20:24:34 +0200199 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200200 sp_digit carry;
201
202 carry = 0;
203 for (i = 0; i < 8; i++) {
204 sp_digit w, v;
205 w = b[i] + carry;
206 v = a[i];
207 if (w != 0) {
208 v = a[i] + w;
209 carry = (v < a[i]);
210 /* hope compiler detects above as "carry flag set" */
211 }
212 /* else: b + carry == 0, two cases:
213 * b:ffffffff, carry:1
214 * b:00000000, carry:0
215 * in either case, r[i] = a[i] and carry remains unchanged
216 */
217 r[i] = v;
218 }
219 return carry;
220#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200221}
222
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200223/* Sub b from a into r. (r = a - b). Return !0 on underflow */
224static int sp_256_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200225{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200226#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
227 sp_digit reg;
228 asm volatile (
229"\n movl (%0), %3"
230"\n subl (%1), %3"
231"\n movl %3, (%2)"
232"\n"
233"\n movl 1*4(%0), %3"
234"\n sbbl 1*4(%1), %3"
235"\n movl %3, 1*4(%2)"
236"\n"
237"\n movl 2*4(%0), %3"
238"\n sbbl 2*4(%1), %3"
239"\n movl %3, 2*4(%2)"
240"\n"
241"\n movl 3*4(%0), %3"
242"\n sbbl 3*4(%1), %3"
243"\n movl %3, 3*4(%2)"
244"\n"
245"\n movl 4*4(%0), %3"
246"\n sbbl 4*4(%1), %3"
247"\n movl %3, 4*4(%2)"
248"\n"
249"\n movl 5*4(%0), %3"
250"\n sbbl 5*4(%1), %3"
251"\n movl %3, 5*4(%2)"
252"\n"
253"\n movl 6*4(%0), %3"
254"\n sbbl 6*4(%1), %3"
255"\n movl %3, 6*4(%2)"
256"\n"
257"\n movl 7*4(%0), %3"
258"\n sbbl 7*4(%1), %3"
259"\n movl %3, 7*4(%2)"
260"\n"
261"\n sbbl %3, %3"
262"\n"
263 : "=r" (a), "=r" (b), "=r" (r), "=r" (reg)
264 : "0" (a), "1" (b), "2" (r)
265 : "memory"
266 );
267 return reg;
268#else
Denys Vlasenko12040122021-04-26 20:24:34 +0200269 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200270 sp_digit borrow;
271
272 borrow = 0;
273 for (i = 0; i < 8; i++) {
274 sp_digit w, v;
275 w = b[i] + borrow;
276 v = a[i];
277 if (w != 0) {
278 v = a[i] - w;
279 borrow = (v > a[i]);
280 /* hope compiler detects above as "carry flag set" */
281 }
282 /* else: b + borrow == 0, two cases:
283 * b:ffffffff, borrow:1
284 * b:00000000, borrow:0
285 * in either case, r[i] = a[i] and borrow remains unchanged
286 */
287 r[i] = v;
288 }
289 return borrow;
290#endif
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200291}
292
Denys Vlasenko567eefc2021-10-06 14:25:10 +0200293#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
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{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200297 sp_digit reg;
298//p256_mod[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
299 asm volatile (
300"\n movl (%0), %2"
301"\n subl $0xffffffff, %2"
302"\n movl %2, (%1)"
303"\n"
304"\n movl 1*4(%0), %2"
305"\n sbbl $0xffffffff, %2"
306"\n movl %2, 1*4(%1)"
307"\n"
308"\n movl 2*4(%0), %2"
309"\n sbbl $0xffffffff, %2"
310"\n movl %2, 2*4(%1)"
311"\n"
312"\n movl 3*4(%0), %2"
313"\n sbbl $0, %2"
314"\n movl %2, 3*4(%1)"
315"\n"
316"\n movl 4*4(%0), %2"
317"\n sbbl $0, %2"
318"\n movl %2, 4*4(%1)"
319"\n"
320"\n movl 5*4(%0), %2"
321"\n sbbl $0, %2"
322"\n movl %2, 5*4(%1)"
323"\n"
324"\n movl 6*4(%0), %2"
325"\n sbbl $1, %2"
326"\n movl %2, 6*4(%1)"
327"\n"
328"\n movl 7*4(%0), %2"
329"\n sbbl $0xffffffff, %2"
330"\n movl %2, 7*4(%1)"
331"\n"
332 : "=r" (a), "=r" (r), "=r" (reg)
333 : "0" (a), "1" (r)
334 : "memory"
335 );
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200336}
Denys Vlasenko567eefc2021-10-06 14:25:10 +0200337#else
338# define sp_256_sub_8_p256_mod(r, a) sp_256_sub_8((r), (a), p256_mod)
339#endif
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200340
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200341/* Multiply a and b into r. (r = a * b) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200342static void sp_256_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200343{
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200344#if ALLOW_ASM && defined(__GNUC__) && defined(__i386__)
345 sp_digit rr[15]; /* in case r coincides with a or b */
346 int k;
347 uint32_t accl;
348 uint32_t acch;
349
350 acch = accl = 0;
351 for (k = 0; k < 15; k++) {
352 int i, j;
353 uint32_t acc_hi;
354 i = k - 7;
355 if (i < 0)
356 i = 0;
357 j = k - i;
358 acc_hi = 0;
359 do {
360////////////////////////
361// uint64_t m = ((uint64_t)a[i]) * b[j];
362// acc_hi:acch:accl += m;
363 asm volatile (
364 // a[i] is already loaded in %%eax
365"\n mull %7"
366"\n addl %%eax, %0"
367"\n adcl %%edx, %1"
368"\n adcl $0, %2"
369 : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi)
370 : "0" (accl), "1" (acch), "2" (acc_hi), "a" (a[i]), "m" (b[j])
371 : "cc", "dx"
372 );
373////////////////////////
374 j--;
375 i++;
376 } while (i != 8 && i <= k);
377 rr[k] = accl;
378 accl = acch;
379 acch = acc_hi;
380 }
381 r[15] = accl;
382 memcpy(r, rr, sizeof(rr));
383#elif 0
384 //TODO: arm assembly (untested)
385 sp_digit tmp[16];
386
387 asm volatile (
388"\n mov r5, #0"
389"\n mov r6, #0"
390"\n mov r7, #0"
391"\n mov r8, #0"
392"\n 1:"
393"\n subs r3, r5, #28"
394"\n movcc r3, #0"
395"\n sub r4, r5, r3"
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200396"\n 2:"
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200397"\n ldr r14, [%[a], r3]"
398"\n ldr r12, [%[b], r4]"
399"\n umull r9, r10, r14, r12"
400"\n adds r6, r6, r9"
401"\n adcs r7, r7, r10"
402"\n adc r8, r8, #0"
403"\n add r3, r3, #4"
404"\n sub r4, r4, #4"
405"\n cmp r3, #32"
406"\n beq 3f"
407"\n cmp r3, r5"
408"\n ble 2b"
409"\n 3:"
410"\n str r6, [%[r], r5]"
411"\n mov r6, r7"
412"\n mov r7, r8"
413"\n mov r8, #0"
414"\n add r5, r5, #4"
415"\n cmp r5, #56"
416"\n ble 1b"
417"\n str r6, [%[r], r5]"
418 : [r] "r" (tmp), [a] "r" (a), [b] "r" (b)
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200419 : "memory", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14"
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200420 );
421 memcpy(r, tmp, sizeof(tmp));
422#else
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200423 sp_digit rr[15]; /* in case r coincides with a or b */
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200424 int i, j, k;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200425 uint64_t acc;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200426
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200427 acc = 0;
428 for (k = 0; k < 15; k++) {
429 uint32_t acc_hi;
430 i = k - 7;
431 if (i < 0)
432 i = 0;
433 j = k - i;
434 acc_hi = 0;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200435 do {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200436 uint64_t m = ((uint64_t)a[i]) * b[j];
437 acc += m;
438 if (acc < m)
439 acc_hi++;
440 j--;
441 i++;
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200442 } while (i != 8 && i <= k);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200443 rr[k] = acc;
444 acc = (acc >> 32) | ((uint64_t)acc_hi << 32);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200445 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200446 r[15] = acc;
447 memcpy(r, rr, sizeof(rr));
Denys Vlasenkobbd723e2021-10-05 23:19:18 +0200448#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200449}
450
Denys Vlasenko389329e2021-10-05 13:39:33 +0200451/* Shift number right one bit. Bottom bit is lost. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200452static void sp_256_rshift1_8(sp_digit* r, sp_digit* a, sp_digit carry)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200453{
454 int i;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200455
456 carry = (!!carry << 31);
457 for (i = 7; i >= 0; i--) {
458 sp_digit c = a[i] << 31;
459 r[i] = (a[i] >> 1) | carry;
460 carry = c;
461 }
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200462}
463
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200464/* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200465static void sp_256_div2_8(sp_digit* r, const sp_digit* a, const sp_digit* m)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200466{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200467 int carry = 0;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200468 if (a[0] & 1)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200469 carry = sp_256_add_8(r, a, m);
470 sp_256_norm_8(r);
471 sp_256_rshift1_8(r, r, carry);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200472}
473
474/* Add two Montgomery form numbers (r = a + b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200475static void sp_256_mont_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b
476 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200477{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200478// const sp_digit* m = p256_mod;
479
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200480 int carry = sp_256_add_8(r, a, b);
481 sp_256_norm_8(r);
482 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200483 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200484 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200485 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200486}
487
488/* Subtract two Montgomery form numbers (r = a - b % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200489static void sp_256_mont_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b
490 /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200491{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200492 const sp_digit* m = p256_mod;
493
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200494 int borrow;
495 borrow = sp_256_sub_8(r, a, b);
496 sp_256_norm_8(r);
497 if (borrow) {
498 sp_256_add_8(r, r, m);
499 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200500 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200501}
502
503/* Double a Montgomery form number (r = a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200504static void sp_256_mont_dbl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200505{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200506// const sp_digit* m = p256_mod;
507
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200508 int carry = sp_256_add_8(r, a, a);
509 sp_256_norm_8(r);
510 if (carry)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200511 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200512 sp_256_norm_8(r);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200513}
514
515/* Triple a Montgomery form number (r = a + a + a % m) */
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200516static void sp_256_mont_tpl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200517{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200518// const sp_digit* m = p256_mod;
519
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200520 int carry = sp_256_add_8(r, a, a);
521 sp_256_norm_8(r);
522 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200523 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200524 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200525 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200526 carry = sp_256_add_8(r, r, a);
527 sp_256_norm_8(r);
528 if (carry) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200529 sp_256_sub_8_p256_mod(r, r /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200530 sp_256_norm_8(r);
Denys Vlasenko55578f22021-10-05 19:45:56 +0200531 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200532}
533
534/* Shift the result in the high 256 bits down to the bottom. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200535static void sp_256_mont_shift_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200536{
537 int i;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200538
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200539 for (i = 0; i < 8; i++) {
540 r[i] = a[i+8];
541 r[i+8] = 0;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200542 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200543}
544
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200545/* Mul a by scalar b and add into r. (r += a * b) */
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200546static int sp_256_mul_add_8(sp_digit* r /*, const sp_digit* a, sp_digit b*/)
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200547{
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200548// const sp_digit* a = p256_mod;
549//a[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
550 sp_digit b = r[0];
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200551
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200552 uint64_t t;
553
554// t = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200555// for (i = 0; i < 8; i++) {
556// uint32_t t_hi;
557// uint64_t m = ((uint64_t)b * a[i]) + r[i];
558// t += m;
559// t_hi = (t < m);
560// r[i] = (sp_digit)t;
561// t = (t >> 32) | ((uint64_t)t_hi << 32);
562// }
563// r[8] += (sp_digit)t;
564
565 // Unroll, then optimize the above loop:
566 //uint32_t t_hi;
567 uint64_t m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200568 uint32_t t32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200569
570 //m = ((uint64_t)b * a[0]) + r[0];
571 // Since b is r[0] and a[0] is ffffffff, the above optimizes to:
572 // m = r[0] * ffffffff + r[0] = (r[0] * 100000000 - r[0]) + r[0] = r[0] << 32;
573 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200574 // t = r[0] << 32 = b << 32;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200575 //t_hi = (t < m);
576 // t_hi = 0;
577 //r[0] = (sp_digit)t;
578 r[0] = 0;
579 //t = (t >> 32) | ((uint64_t)t_hi << 32);
580 // t = b;
581
582 //m = ((uint64_t)b * a[1]) + r[1];
583 // Since a[1] is ffffffff, the above optimizes to:
584 // m = b * ffffffff + r[1] = (b * 100000000 - b) + r[1] = (b << 32) - b + r[1];
585 //t += m;
586 // t = b + (b << 32) - b + r[1] = (b << 32) + r[1];
587 //t_hi = (t < m);
588 // t_hi = 0;
589 //r[1] = (sp_digit)t;
590 // r[1] = r[1];
591 //t = (t >> 32) | ((uint64_t)t_hi << 32);
592 // t = b;
593
594 //m = ((uint64_t)b * a[2]) + r[2];
595 // Since a[2] is ffffffff, the above optimizes to:
596 // m = b * ffffffff + r[2] = (b * 100000000 - b) + r[2] = (b << 32) - b + r[2];
597 //t += m;
598 // t = b + (b << 32) - b + r[2] = (b << 32) + r[2]
599 //t_hi = (t < m);
600 // t_hi = 0;
601 //r[2] = (sp_digit)t;
602 // r[2] = r[2];
603 //t = (t >> 32) | ((uint64_t)t_hi << 32);
604 // t = b;
605
606 //m = ((uint64_t)b * a[3]) + r[3];
607 // Since a[3] is 00000000, the above optimizes to:
608 // m = b * 0 + r[3] = r[3];
609 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200610 // t = b + r[3];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200611 //t_hi = (t < m);
612 // t_hi = 0;
613 //r[3] = (sp_digit)t;
614 r[3] = r[3] + b;
615 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200616 t32 = (r[3] < b); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200617
618 //m = ((uint64_t)b * a[4]) + r[4];
619 // Since a[4] is 00000000, the above optimizes to:
620 // m = b * 0 + r[4] = r[4];
621 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200622 // t = t32 + r[4];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200623 //t_hi = (t < m);
624 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200625 //r[4] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200626 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200627 if (t32 != 0) {
628 r[4]++;
629 t32 = (r[4] == 0); // 0 or 1
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200630
631 //m = ((uint64_t)b * a[5]) + r[5];
632 // Since a[5] is 00000000, the above optimizes to:
633 // m = b * 0 + r[5] = r[5];
634 //t += m;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200635 // t = t32 + r[5]; (t32 is 0 or 1)
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200636 //t_hi = (t < m);
637 // t_hi = 0;
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200638 //r[5] = (sp_digit)t;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200639 //t = (t >> 32) | ((uint64_t)t_hi << 32);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200640 if (t32 != 0) {
641 r[5]++;
642 t32 = (r[5] == 0); // 0 or 1
643 }
644 }
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200645
646 //m = ((uint64_t)b * a[6]) + r[6];
647 // Since a[6] is 00000001, the above optimizes to:
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200648 // m = (uint64_t)b + r[6]; // 33 bits at most
649 //t += m;
650 t = t32 + (uint64_t)b + r[6];
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200651 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200652 // t_hi = 0;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200653 r[6] = (sp_digit)t;
654 //t = (t >> 32) | ((uint64_t)t_hi << 32);
655 t = (t >> 32);
656
657 //m = ((uint64_t)b * a[7]) + r[7];
658 // Since a[7] is ffffffff, the above optimizes to:
659 // m = b * ffffffff + r[7] = (b * 100000000 - b) + r[7]
660 m = ((uint64_t)b << 32) - b + r[7];
661 t += m;
662 //t_hi = (t < m);
Denys Vlasenko00f2cce2021-10-06 10:15:29 +0200663 // 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 +0200664 r[7] = (sp_digit)t;
665 //t = (t >> 32) | ((uint64_t)t_hi << 32);
666 t = (t >> 32);
667
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200668 r[8] += (sp_digit)t;
669 return (r[8] < (sp_digit)t); /* 1 if addition overflowed */
Denys Vlasenkoe7305052021-10-05 13:30:48 +0200670}
671
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200672/* Reduce the number back to 256 bits using Montgomery reduction.
673 *
674 * a A single precision number to reduce in place.
675 * m The single precision number representing the modulus.
676 * mp The digit representing the negative inverse of m mod 2^n.
677 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200678static void sp_256_mont_reduce_8(sp_digit* a/*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200679{
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200680// const sp_digit* m = p256_mod;
Denys Vlasenko389329e2021-10-05 13:39:33 +0200681 sp_digit mp = p256_mp_mod;
682
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200683 int i;
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200684// sp_digit mu;
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200685
686 if (mp != 1) {
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200687 sp_digit word16th = 0;
688 for (i = 0; i < 8; i++) {
689// mu = (sp_digit)(a[i] * mp);
690 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
691 int j = i + 8;
692 inc_next_word0:
693 if (++j > 15) { /* a[16] array has no more words? */
694 word16th++;
695 continue;
696 }
697 if (++a[j] == 0) /* did this overflow too? */
698 goto inc_next_word0;
699 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200700 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200701 sp_256_mont_shift_8(a, a);
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200702 if (word16th != 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200703 sp_256_sub_8_p256_mod(a, a /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200704 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200705 }
Denys Vlasenko389329e2021-10-05 13:39:33 +0200706 else { /* Same code for explicit mp == 1 (which is always the case for P256) */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200707 sp_digit word16th = 0;
708 for (i = 0; i < 8; i++) {
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200709 /*mu = a[i];*/
Denys Vlasenko2430fcf2021-10-06 00:19:30 +0200710 if (sp_256_mul_add_8(a+i /*, m, mu*/)) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200711 int j = i + 8;
712 inc_next_word:
713 if (++j > 15) { /* a[16] array has no more words? */
714 word16th++;
715 continue;
716 }
717 if (++a[j] == 0) /* did this overflow too? */
718 goto inc_next_word;
719 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200720 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200721 sp_256_mont_shift_8(a, a);
722 if (word16th != 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200723 sp_256_sub_8_p256_mod(a, a /*, m*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200724 sp_256_norm_8(a);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200725 }
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200726}
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200727#if 0
728//TODO: arm32 asm (also adapt for x86?)
729static void sp_256_mont_reduce_8(sp_digit* a, sp_digit* m, sp_digit mp)
730{
731 sp_digit ca = 0;
732
733 asm volatile (
734 # i = 0
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200735 mov r12, #0
736 ldr r10, [%[a], #0]
737 ldr r14, [%[a], #4]
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02007381:
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200739 # mu = a[i] * mp
740 mul r8, %[mp], r10
741 # a[i+0] += m[0] * mu
742 ldr r7, [%[m], #0]
743 ldr r9, [%[a], #0]
744 umull r6, r7, r8, r7
745 adds r10, r10, r6
746 adc r5, r7, #0
747 # a[i+1] += m[1] * mu
748 ldr r7, [%[m], #4]
749 ldr r9, [%[a], #4]
750 umull r6, r7, r8, r7
751 adds r10, r14, r6
752 adc r4, r7, #0
753 adds r10, r10, r5
754 adc r4, r4, #0
755 # a[i+2] += m[2] * mu
756 ldr r7, [%[m], #8]
757 ldr r14, [%[a], #8]
758 umull r6, r7, r8, r7
759 adds r14, r14, r6
760 adc r5, r7, #0
761 adds r14, r14, r4
762 adc r5, r5, #0
763 # a[i+3] += m[3] * mu
764 ldr r7, [%[m], #12]
765 ldr r9, [%[a], #12]
766 umull r6, r7, r8, r7
767 adds r9, r9, r6
768 adc r4, r7, #0
769 adds r9, r9, r5
770 str r9, [%[a], #12]
771 adc r4, r4, #0
772 # a[i+4] += m[4] * mu
773 ldr r7, [%[m], #16]
774 ldr r9, [%[a], #16]
775 umull r6, r7, r8, r7
776 adds r9, r9, r6
777 adc r5, r7, #0
778 adds r9, r9, r4
779 str r9, [%[a], #16]
780 adc r5, r5, #0
781 # a[i+5] += m[5] * mu
782 ldr r7, [%[m], #20]
783 ldr r9, [%[a], #20]
784 umull r6, r7, r8, r7
785 adds r9, r9, r6
786 adc r4, r7, #0
787 adds r9, r9, r5
788 str r9, [%[a], #20]
789 adc r4, r4, #0
790 # a[i+6] += m[6] * mu
791 ldr r7, [%[m], #24]
792 ldr r9, [%[a], #24]
793 umull r6, r7, r8, r7
794 adds r9, r9, r6
795 adc r5, r7, #0
796 adds r9, r9, r4
797 str r9, [%[a], #24]
798 adc r5, r5, #0
799 # a[i+7] += m[7] * mu
800 ldr r7, [%[m], #28]
801 ldr r9, [%[a], #28]
802 umull r6, r7, r8, r7
803 adds r5, r5, r6
804 adcs r7, r7, %[ca]
805 mov %[ca], #0
806 adc %[ca], %[ca], %[ca]
807 adds r9, r9, r5
808 str r9, [%[a], #28]
809 ldr r9, [%[a], #32]
810 adcs r9, r9, r7
811 str r9, [%[a], #32]
812 adc %[ca], %[ca], #0
813 # i += 1
814 add %[a], %[a], #4
815 add r12, r12, #4
816 cmp r12, #32
817 blt 1b
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200818
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200819 str r10, [%[a], #0]
820 str r14, [%[a], #4]
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200821 : [ca] "+r" (ca), [a] "+r" (a)
822 : [m] "r" (m), [mp] "r" (mp)
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200823 : "memory", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14"
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200824 );
825
Denys Vlasenko22fd8fd2021-10-06 16:10:49 +0200826 memcpy(a, a + 8, 32);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200827 if (ca)
828 a -= m;
829}
830#endif
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200831
832/* Multiply two Montogmery form numbers mod the modulus (prime).
833 * (r = a * b mod m)
834 *
835 * r Result of multiplication.
836 * a First number to multiply in Montogmery form.
837 * b Second number to multiply in Montogmery form.
838 * m Modulus (prime).
839 * mp Montogmery mulitplier.
840 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200841static void sp_256_mont_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b
Denys Vlasenko389329e2021-10-05 13:39:33 +0200842 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200843{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200844 //const sp_digit* m = p256_mod;
845 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200846 sp_256_mul_8(r, a, b);
847 sp_256_mont_reduce_8(r /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200848}
849
850/* Square the Montgomery form number. (r = a * a mod m)
851 *
852 * r Result of squaring.
853 * a Number to square in Montogmery form.
854 * m Modulus (prime).
855 * mp Montogmery mulitplier.
856 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200857static void sp_256_mont_sqr_8(sp_digit* r, const sp_digit* a
Denys Vlasenko389329e2021-10-05 13:39:33 +0200858 /*, const sp_digit* m, sp_digit mp*/)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200859{
Denys Vlasenko389329e2021-10-05 13:39:33 +0200860 //const sp_digit* m = p256_mod;
861 //sp_digit mp = p256_mp_mod;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200862 sp_256_mont_mul_8(r, a, a /*, m, mp*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200863}
864
865/* Invert the number, in Montgomery form, modulo the modulus (prime) of the
866 * P256 curve. (r = 1 / a mod m)
867 *
868 * r Inverse result.
869 * a Number to invert.
870 */
871#if 0
872/* Mod-2 for the P256 curve. */
873static const uint32_t p256_mod_2[8] = {
874 0xfffffffd,0xffffffff,0xffffffff,0x00000000,
875 0x00000000,0x00000000,0x00000001,0xffffffff,
876};
877//Bit pattern:
878//2 2 2 2 2 2 2 1...1
879//5 5 4 3 2 1 0 9...0 9...1
880//543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210
881//111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101
882#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200883static void sp_256_mont_inv_8(sp_digit* r, sp_digit* a)
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200884{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200885 sp_digit t[2*8]; //can be just [8]?
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200886 int i;
887
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200888 memcpy(t, a, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200889 for (i = 254; i >= 0; i--) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200890 sp_256_mont_sqr_8(t, t /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200891 /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/
892 if (i >= 224 || i == 192 || (i <= 95 && i != 1))
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200893 sp_256_mont_mul_8(t, t, a /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200894 }
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200895 memcpy(r, t, sizeof(sp_digit) * 8);
Denys Vlasenkoa2bc52d2021-04-27 01:21:26 +0200896}
897
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200898/* Multiply a number by Montogmery normalizer mod modulus (prime).
899 *
900 * r The resulting Montgomery form number.
901 * a The number to convert.
902 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200903static void sp_256_mod_mul_norm_8(sp_digit* r, const sp_digit* a)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200904{
Denys Vlasenko12040122021-04-26 20:24:34 +0200905 int64_t t[8];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200906 int32_t o;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200907
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200908#define A(n) ((uint64_t)a[n])
Denys Vlasenko12040122021-04-26 20:24:34 +0200909 /* 1 1 0 -1 -1 -1 -1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200910 t[0] = 0 + A(0) + A(1) - A(3) - A(4) - A(5) - A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +0200911 /* 0 1 1 0 -1 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200912 t[1] = 0 + A(1) + A(2) - A(4) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200913 /* 0 0 1 1 0 -1 -1 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200914 t[2] = 0 + A(2) + A(3) - A(5) - A(6) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200915 /* -1 -1 0 2 2 1 0 -1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200916 t[3] = 0 - A(0) - A(1) + 2 * A(3) + 2 * A(4) + A(5) - A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200917 /* 0 -1 -1 0 2 2 1 0 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200918 t[4] = 0 - A(1) - A(2) + 2 * A(4) + 2 * A(5) + A(6);
Denys Vlasenko12040122021-04-26 20:24:34 +0200919 /* 0 0 -1 -1 0 2 2 1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200920 t[5] = 0 - A(2) - A(3) + 2 * A(5) + 2 * A(6) + A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200921 /* -1 -1 0 0 0 1 3 2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200922 t[6] = 0 - A(0) - A(1) + A(5) + 3 * A(6) + 2 * A(7);
Denys Vlasenko12040122021-04-26 20:24:34 +0200923 /* 1 0 -1 -1 -1 -1 0 3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200924 t[7] = 0 + A(0) - A(2) - A(3) - A(4) - A(5) + 3 * A(7);
925#undef A
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200926
Denys Vlasenko12040122021-04-26 20:24:34 +0200927 t[1] += t[0] >> 32; t[0] &= 0xffffffff;
928 t[2] += t[1] >> 32; t[1] &= 0xffffffff;
929 t[3] += t[2] >> 32; t[2] &= 0xffffffff;
930 t[4] += t[3] >> 32; t[3] &= 0xffffffff;
931 t[5] += t[4] >> 32; t[4] &= 0xffffffff;
932 t[6] += t[5] >> 32; t[5] &= 0xffffffff;
933 t[7] += t[6] >> 32; t[6] &= 0xffffffff;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200934 o = t[7] >> 32; //t[7] &= 0xffffffff;
Denys Vlasenko12040122021-04-26 20:24:34 +0200935 t[0] += o;
936 t[3] -= o;
937 t[6] -= o;
938 t[7] += o;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200939 r[0] = (sp_digit)t[0];
940 t[1] += t[0] >> 32;
941 r[1] = (sp_digit)t[1];
942 t[2] += t[1] >> 32;
943 r[2] = (sp_digit)t[2];
944 t[3] += t[2] >> 32;
945 r[3] = (sp_digit)t[3];
946 t[4] += t[3] >> 32;
947 r[4] = (sp_digit)t[4];
948 t[5] += t[4] >> 32;
949 r[5] = (sp_digit)t[5];
950 t[6] += t[5] >> 32;
951 r[6] = (sp_digit)t[6];
952// t[7] += t[6] >> 32;
953// r[7] = (sp_digit)t[7];
954 r[7] = (sp_digit)t[7] + (sp_digit)(t[6] >> 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200955}
956
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200957/* Map the Montgomery form projective co-ordinate point to an affine point.
958 *
959 * r Resulting affine co-ordinate point.
960 * p Montgomery form projective co-ordinate point.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200961 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200962static void sp_256_map_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200963{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200964 sp_digit t1[2*8];
965 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200966
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200967 sp_256_mont_inv_8(t1, p->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200968
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200969 sp_256_mont_sqr_8(t2, t1 /*, p256_mod, p256_mp_mod*/);
970 sp_256_mont_mul_8(t1, t2, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200971
Denys Vlasenko12040122021-04-26 20:24:34 +0200972 /* x /= z^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200973 sp_256_mont_mul_8(r->x, p->x, t2 /*, p256_mod, p256_mp_mod*/);
974 memset(r->x + 8, 0, sizeof(r->x) / 2);
975 sp_256_mont_reduce_8(r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +0200976 /* Reduce x to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200977 if (sp_256_cmp_8(r->x, p256_mod) >= 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200978 sp_256_sub_8_p256_mod(r->x, r->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200979 sp_256_norm_8(r->x);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200980
Denys Vlasenko12040122021-04-26 20:24:34 +0200981 /* y /= z^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200982 sp_256_mont_mul_8(r->y, p->y, t1 /*, p256_mod, p256_mp_mod*/);
983 memset(r->y + 8, 0, sizeof(r->y) / 2);
984 sp_256_mont_reduce_8(r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +0200985 /* Reduce y to less than modulus */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200986 if (sp_256_cmp_8(r->y, p256_mod) >= 0)
Denys Vlasenkoc7842842021-10-06 01:09:37 +0200987 sp_256_sub_8_p256_mod(r->y, r->y /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200988 sp_256_norm_8(r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200989
Denys Vlasenko12040122021-04-26 20:24:34 +0200990 memset(r->z, 0, sizeof(r->z));
991 r->z[0] = 1;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200992}
993
994/* Double the Montgomery form projective point p.
995 *
996 * r Result of doubling point.
997 * p Point to double.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +0200998 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +0200999static void sp_256_proj_point_dbl_8(sp_point* r, sp_point* p)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001000{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001001 sp_digit t1[2*8];
1002 sp_digit t2[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001003
Denys Vlasenko12040122021-04-26 20:24:34 +02001004 /* Put point to double into result */
1005 if (r != p)
1006 *r = *p; /* struct copy */
Denys Vlasenko4d3a5c12021-04-26 15:21:38 +02001007
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001008 if (r->infinity)
Denys Vlasenkoe7305052021-10-05 13:30:48 +02001009 return;
1010
Denys Vlasenko55578f22021-10-05 19:45:56 +02001011 if (SP_DEBUG) {
1012 /* unused part of t2, may result in spurios
1013 * differences in debug output. Clear it.
1014 */
1015 memset(t2, 0, sizeof(t2));
1016 }
1017
Denys Vlasenko12040122021-04-26 20:24:34 +02001018 /* T1 = Z * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001019 sp_256_mont_sqr_8(t1, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001020 /* Z = Y * Z */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001021 sp_256_mont_mul_8(r->z, r->y, r->z /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001022 /* Z = 2Z */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001023 sp_256_mont_dbl_8(r->z, r->z /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001024 /* T2 = X - T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001025 sp_256_mont_sub_8(t2, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001026 /* T1 = X + T1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001027 sp_256_mont_add_8(t1, r->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001028 /* T2 = T1 * T2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001029 sp_256_mont_mul_8(t2, t1, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001030 /* T1 = 3T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001031 sp_256_mont_tpl_8(t1, t2 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001032 /* Y = 2Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001033 sp_256_mont_dbl_8(r->y, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001034 /* Y = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001035 sp_256_mont_sqr_8(r->y, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001036 /* T2 = Y * Y */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001037 sp_256_mont_sqr_8(t2, r->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001038 /* T2 = T2/2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001039 sp_256_div2_8(t2, t2, p256_mod);
Denys Vlasenko12040122021-04-26 20:24:34 +02001040 /* Y = Y * X */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001041 sp_256_mont_mul_8(r->y, r->y, r->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001042 /* X = T1 * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001043 sp_256_mont_mul_8(r->x, t1, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001044 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001045 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001046 /* X = X - Y */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001047 sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001048 /* Y = Y - X */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001049 sp_256_mont_sub_8(r->y, r->y, r->x /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001050 /* Y = Y * T1 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001051 sp_256_mont_mul_8(r->y, r->y, t1 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001052 /* Y = Y - T2 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001053 sp_256_mont_sub_8(r->y, r->y, t2 /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001054 dump_512("y2 %s\n", r->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001055}
1056
1057/* Add two Montgomery form projective points.
1058 *
1059 * r Result of addition.
1060 * p Frist point to add.
1061 * q Second point to add.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001062 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001063static void sp_256_proj_point_add_8(sp_point* r, sp_point* p, sp_point* q)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001064{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001065 sp_digit t1[2*8];
1066 sp_digit t2[2*8];
1067 sp_digit t3[2*8];
1068 sp_digit t4[2*8];
1069 sp_digit t5[2*8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001070
Denys Vlasenko12040122021-04-26 20:24:34 +02001071 /* Ensure only the first point is the same as the result. */
1072 if (q == r) {
1073 sp_point* a = p;
1074 p = q;
1075 q = a;
1076 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001077
Denys Vlasenko12040122021-04-26 20:24:34 +02001078 /* Check double */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001079 sp_256_sub_8(t1, p256_mod, q->y);
1080 sp_256_norm_8(t1);
1081 if (sp_256_cmp_equal_8(p->x, q->x)
1082 && sp_256_cmp_equal_8(p->z, q->z)
1083 && (sp_256_cmp_equal_8(p->y, q->y) || sp_256_cmp_equal_8(p->y, t1))
Denys Vlasenko12040122021-04-26 20:24:34 +02001084 ) {
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001085 sp_256_proj_point_dbl_8(r, p);
Denys Vlasenko12040122021-04-26 20:24:34 +02001086 }
1087 else {
1088 sp_point tp;
1089 sp_point *v;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001090
Denys Vlasenko12040122021-04-26 20:24:34 +02001091 v = r;
1092 if (p->infinity | q->infinity) {
1093 memset(&tp, 0, sizeof(tp));
1094 v = &tp;
1095 }
Denys Vlasenko772e1872021-04-26 17:25:27 +02001096
Denys Vlasenko12040122021-04-26 20:24:34 +02001097 *r = p->infinity ? *q : *p; /* struct copy */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001098
Denys Vlasenko12040122021-04-26 20:24:34 +02001099 /* U1 = X1*Z2^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001100 sp_256_mont_sqr_8(t1, q->z /*, p256_mod, p256_mp_mod*/);
1101 sp_256_mont_mul_8(t3, t1, q->z /*, p256_mod, p256_mp_mod*/);
1102 sp_256_mont_mul_8(t1, t1, v->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001103 /* U2 = X2*Z1^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001104 sp_256_mont_sqr_8(t2, v->z /*, p256_mod, p256_mp_mod*/);
1105 sp_256_mont_mul_8(t4, t2, v->z /*, p256_mod, p256_mp_mod*/);
1106 sp_256_mont_mul_8(t2, t2, q->x /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001107 /* S1 = Y1*Z2^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001108 sp_256_mont_mul_8(t3, t3, v->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001109 /* S2 = Y2*Z1^3 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001110 sp_256_mont_mul_8(t4, t4, q->y /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001111 /* H = U2 - U1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001112 sp_256_mont_sub_8(t2, t2, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001113 /* R = S2 - S1 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001114 sp_256_mont_sub_8(t4, t4, t3 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001115 /* Z3 = H*Z1*Z2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001116 sp_256_mont_mul_8(v->z, v->z, q->z /*, p256_mod, p256_mp_mod*/);
1117 sp_256_mont_mul_8(v->z, v->z, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001118 /* X3 = R^2 - H^3 - 2*U1*H^2 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001119 sp_256_mont_sqr_8(v->x, t4 /*, p256_mod, p256_mp_mod*/);
1120 sp_256_mont_sqr_8(t5, t2 /*, p256_mod, p256_mp_mod*/);
1121 sp_256_mont_mul_8(v->y, t1, t5 /*, p256_mod, p256_mp_mod*/);
1122 sp_256_mont_mul_8(t5, t5, t2 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001123 sp_256_mont_sub_8(v->x, v->x, t5 /*, p256_mod*/);
1124 sp_256_mont_dbl_8(t1, v->y /*, p256_mod*/);
1125 sp_256_mont_sub_8(v->x, v->x, t1 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001126 /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001127 sp_256_mont_sub_8(v->y, v->y, v->x /*, p256_mod*/);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001128 sp_256_mont_mul_8(v->y, v->y, t4 /*, p256_mod, p256_mp_mod*/);
1129 sp_256_mont_mul_8(t5, t5, t3 /*, p256_mod, p256_mp_mod*/);
Denys Vlasenkoc7842842021-10-06 01:09:37 +02001130 sp_256_mont_sub_8(v->y, v->y, t5 /*, p256_mod*/);
Denys Vlasenko12040122021-04-26 20:24:34 +02001131 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001132}
1133
1134/* Multiply the point by the scalar and return the result.
1135 * If map is true then convert result to affine co-ordinates.
1136 *
1137 * r Resulting point.
1138 * g Point to multiply.
1139 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001140 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001141 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001142static 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 +02001143{
Denys Vlasenko12040122021-04-26 20:24:34 +02001144 enum { map = 1 }; /* we always convert result to affine coordinates */
1145 sp_point t[3];
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001146 sp_digit n = n; /* for compiler */
Denys Vlasenko12040122021-04-26 20:24:34 +02001147 int c, y;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001148
Denys Vlasenko12040122021-04-26 20:24:34 +02001149 memset(t, 0, sizeof(t));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001150
Denys Vlasenko12040122021-04-26 20:24:34 +02001151 /* t[0] = {0, 0, 1} * norm */
1152 t[0].infinity = 1;
1153 /* t[1] = {g->x, g->y, g->z} * norm */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001154 sp_256_mod_mul_norm_8(t[1].x, g->x);
1155 sp_256_mod_mul_norm_8(t[1].y, g->y);
1156 sp_256_mod_mul_norm_8(t[1].z, g->z);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001157
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001158 /* For every bit, starting from most significant... */
1159 k += 7;
1160 c = 256;
1161 for (;;) {
1162 if ((c & 0x1f) == 0) {
1163 if (c == 0)
Denys Vlasenko12040122021-04-26 20:24:34 +02001164 break;
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001165 n = *k--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001166 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001167
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001168 y = (n >> 31);
1169 dbg("y:%d t[%d] = t[0]+t[1]\n", y, y^1);
1170 sp_256_proj_point_add_8(&t[y^1], &t[0], &t[1]);
1171 dump_512("t[0].x %s\n", t[0].x);
1172 dump_512("t[0].y %s\n", t[0].y);
1173 dump_512("t[0].z %s\n", t[0].z);
1174 dump_512("t[1].x %s\n", t[1].x);
1175 dump_512("t[1].y %s\n", t[1].y);
1176 dump_512("t[1].z %s\n", t[1].z);
1177 dbg("t[2] = t[%d]\n", y);
Denys Vlasenko12040122021-04-26 20:24:34 +02001178 memcpy(&t[2], &t[y], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001179 dbg("t[2] *= 2\n");
1180 sp_256_proj_point_dbl_8(&t[2], &t[2]);
1181 dump_512("t[2].x %s\n", t[2].x);
1182 dump_512("t[2].y %s\n", t[2].y);
1183 dump_512("t[2].z %s\n", t[2].z);
Denys Vlasenko12040122021-04-26 20:24:34 +02001184 memcpy(&t[y], &t[2], sizeof(sp_point));
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001185
1186 n <<= 1;
1187 c--;
Denys Vlasenko12040122021-04-26 20:24:34 +02001188 }
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001189
Denys Vlasenko12040122021-04-26 20:24:34 +02001190 if (map)
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001191 sp_256_map_8(r, &t[0]);
Denys Vlasenko12040122021-04-26 20:24:34 +02001192 else
1193 memcpy(r, &t[0], sizeof(sp_point));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001194
Denys Vlasenko12040122021-04-26 20:24:34 +02001195 memset(t, 0, sizeof(t)); //paranoia
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001196}
1197
1198/* Multiply the base point of P256 by the scalar and return the result.
1199 * If map is true then convert result to affine co-ordinates.
1200 *
1201 * r Resulting point.
1202 * k Scalar to multiply by.
Denys Vlasenko03ab2a92021-04-26 14:55:46 +02001203 * map Indicates whether to convert result to affine.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001204 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001205static void sp_256_ecc_mulmod_base_8(sp_point* r, sp_digit* k /*, int map*/)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001206{
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001207 /* Since this function is called only once, save space:
1208 * don't have "static const sp_point p256_base = {...}",
1209 * it would have more zeros than data.
1210 */
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001211 static const uint8_t p256_base_bin[] = {
1212 /* x (big-endian) */
1213 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,
1214 /* y */
1215 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 +02001216 /* z will be set to 1, infinity flag to "false" */
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001217 };
1218 sp_point p256_base;
1219
Denys Vlasenko48a18d12021-04-27 12:24:21 +02001220 sp_256_point_from_bin2x32(&p256_base, p256_base_bin);
Denys Vlasenko39a3ef52021-04-27 01:31:51 +02001221
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001222 sp_256_ecc_mulmod_8(r, &p256_base, k /*, map*/);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001223}
1224
1225/* Multiply the point by the scalar and serialize the X ordinate.
1226 * The number is 0 padded to maximum size on output.
1227 *
1228 * priv Scalar to multiply the point by.
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001229 * pub2x32 Point to multiply.
1230 * out32 Buffer to hold X ordinate.
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001231 */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001232static 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 +02001233{
Denys Vlasenko12040122021-04-26 20:24:34 +02001234 sp_point point[1];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001235
1236#if FIXED_PEER_PUBKEY
Denys Vlasenko12040122021-04-26 20:24:34 +02001237 memset((void*)pub2x32, 0x55, 64);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001238#endif
Denys Vlasenko12040122021-04-26 20:24:34 +02001239 dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */
1240 dump_hex(" %s\n", pub2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001241
Denys Vlasenko12040122021-04-26 20:24:34 +02001242 sp_256_point_from_bin2x32(point, pub2x32);
Denys Vlasenko81d8af12021-10-05 17:31:33 +02001243 dump_512("point->x %s\n", point->x);
1244 dump_512("point->y %s\n", point->y);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001245
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001246 sp_256_ecc_mulmod_8(point, point, priv);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001247
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001248 sp_256_to_bin_8(point->x, out32);
Denys Vlasenko12040122021-04-26 20:24:34 +02001249 dump_hex("out32: %s\n", out32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001250}
1251
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001252/* Generates a random scalar in [1..order-1] range. */
1253static void sp_256_ecc_gen_k_8(sp_digit k[8])
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001254{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001255 /* Since 32-bit words are "dense", no need to use
1256 * sp_256_from_bin_8(k, buf) to convert random stream
1257 * to sp_digit array - just store random bits there directly.
1258 */
1259 tls_get_random(k, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001260#if FIXED_SECRET
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001261 memset(k, 0x77, 8 * sizeof(k[0]));
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001262#endif
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001263
1264// If scalar is too large, try again (pseudo-code)
1265// if (k >= 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 - 1) // order of P256
1266// goto pick_another_random;
1267// k++; // ensure non-zero
1268 /* Simpler alternative, at the cost of not choosing some valid
1269 * random values, and slightly non-uniform distribution */
1270 if (k[0] == 0)
1271 k[0] = 1;
1272 if (k[7] >= 0xffffffff)
1273 k[7] = 0xfffffffe;
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001274}
1275
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001276/* Makes a random EC key pair. */
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001277static void sp_ecc_make_key_256(sp_digit privkey[8], uint8_t *pubkey)
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001278{
1279 sp_point point[1];
1280
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001281 sp_256_ecc_gen_k_8(privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001282 dump_256("privkey %s\n", privkey);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001283 sp_256_ecc_mulmod_base_8(point, privkey);
Denys Vlasenko137864f2021-10-05 13:47:42 +02001284 dump_512("point->x %s\n", point->x);
1285 dump_512("point->y %s\n", point->y);
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001286 sp_256_to_bin_8(point->x, pubkey);
1287 sp_256_to_bin_8(point->y, pubkey + 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001288
1289 memset(point, 0, sizeof(point)); //paranoia
1290}
1291
1292void FAST_FUNC curve_P256_compute_pubkey_and_premaster(
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001293 uint8_t *pubkey2x32, uint8_t *premaster32,
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001294 const uint8_t *peerkey2x32)
1295{
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001296 sp_digit privkey[8];
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001297
Denys Vlasenko3b411eb2021-10-05 20:00:50 +02001298 dump_hex("peerkey2x32: %s\n", peerkey2x32, 64);
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001299 sp_ecc_make_key_256(privkey, pubkey2x32);
1300 dump_hex("pubkey: %s\n", pubkey2x32, 32);
1301 dump_hex(" %s\n", pubkey2x32 + 32, 32);
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001302
Denys Vlasenko074b33b2021-04-26 14:33:38 +02001303 /* Combine our privkey and peer's public key to generate premaster */
Denys Vlasenkof18a1fd2021-04-26 13:25:56 +02001304 sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32);
1305 dump_hex("premaster: %s\n", premaster32, 32);
1306}