Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1 | /* |
| 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 12 | #define ALLOW_ASM 1 |
| 13 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 14 | #if SP_DEBUG |
| 15 | # define dbg(...) fprintf(stderr, __VA_ARGS__) |
| 16 | static 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 29 | typedef uint32_t sp_digit; |
| 30 | typedef int32_t signed_sp_digit; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 31 | |
| 32 | /* The code below is taken from parts of |
| 33 | * wolfssl-3.15.3/wolfcrypt/src/sp_c32.c |
| 34 | * and heavily modified. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 35 | */ |
| 36 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 37 | typedef struct sp_point { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 38 | sp_digit x[2 * 8]; |
| 39 | sp_digit y[2 * 8]; |
| 40 | sp_digit z[2 * 8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 41 | int infinity; |
| 42 | } sp_point; |
| 43 | |
| 44 | /* The modulus (prime) of the curve P256. */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 45 | static const sp_digit p256_mod[8] = { |
| 46 | 0xffffffff,0xffffffff,0xffffffff,0x00000000, |
| 47 | 0x00000000,0x00000000,0x00000001,0xffffffff, |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | #define p256_mp_mod ((sp_digit)0x000001) |
| 51 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 52 | /* Normalize the values in each word to 32 bits - NOP */ |
| 53 | #define sp_256_norm_8(a) ((void)0) |
Denys Vlasenko | 7714518 | 2021-10-01 13:51:39 +0200 | [diff] [blame] | 54 | |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 55 | /* Write r as big endian to byte array. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 56 | * Fixed length number of bytes written: 32 |
| 57 | * |
| 58 | * r A single precision integer. |
| 59 | * a Byte array. |
| 60 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 61 | static void sp_256_to_bin_8(const sp_digit* r, uint8_t* a) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 62 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 63 | int i; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 64 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 65 | sp_256_norm_8(r); |
Denys Vlasenko | 7714518 | 2021-10-01 13:51:39 +0200 | [diff] [blame] | 66 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 67 | r += 8; |
| 68 | for (i = 0; i < 8; i++) { |
| 69 | r--; |
| 70 | move_to_unaligned32(a, SWAP_BE32(*r)); |
| 71 | a += 4; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 72 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 75 | /* Read big endian unsigned byte array into r. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 76 | * |
| 77 | * r A single precision integer. |
| 78 | * a Byte array. |
| 79 | * n Number of bytes in array to read. |
| 80 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 81 | static void sp_256_from_bin_8(sp_digit* r, const uint8_t* a) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 82 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 83 | int i; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 84 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 85 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 91 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 94 | #if SP_DEBUG |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 95 | static void dump_256(const char *fmt, const sp_digit* r) |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 96 | { |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 97 | uint8_t b32[32]; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 98 | sp_256_to_bin_8(r, b32); |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 99 | dump_hex(fmt, b32, 32); |
| 100 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 101 | static void dump_512(const char *fmt, const sp_digit* r) |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 102 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 103 | 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 Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 107 | } |
| 108 | #else |
| 109 | # define dump_256(...) ((void)0) |
| 110 | # define dump_512(...) ((void)0) |
| 111 | #endif |
| 112 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 113 | /* Convert a point of big-endian 32-byte x,y pair to type sp_point. */ |
| 114 | static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32) |
| 115 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 116 | memset(p, 0, sizeof(*p)); |
| 117 | /*p->infinity = 0;*/ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 118 | sp_256_from_bin_8(p->x, bin2x32); |
| 119 | sp_256_from_bin_8(p->y, bin2x32 + 32); |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 120 | p->z[0] = 1; /* p->z = 1 */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Denys Vlasenko | b3b1713 | 2021-04-26 16:53:53 +0200 | [diff] [blame] | 123 | /* Compare a with b. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 124 | * |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 125 | * return -ve, 0 or +ve if a is less than, equal to or greater than b |
| 126 | * respectively. |
| 127 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 128 | static signed_sp_digit sp_256_cmp_8(const sp_digit* a, const sp_digit* b) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 129 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 130 | int i; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 131 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 140 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 141 | return 0; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* Compare two numbers to determine if they are equal. |
| 145 | * |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 146 | * return 1 when equal and 0 otherwise. |
| 147 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 148 | static int sp_256_cmp_equal_8(const sp_digit* a, const sp_digit* b) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 149 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 150 | return sp_256_cmp_8(a, b) == 0; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 153 | /* Add b to a into r. (r = a + b). Return !0 on overflow */ |
| 154 | static int sp_256_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 155 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 156 | #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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 199 | int i; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 200 | 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 223 | /* Sub b from a into r. (r = a - b). Return !0 on underflow */ |
| 224 | static int sp_256_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 225 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 226 | #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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 269 | int i; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 270 | 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Denys Vlasenko | 567eefc | 2021-10-06 14:25:10 +0200 | [diff] [blame] | 293 | #if ALLOW_ASM && defined(__GNUC__) && defined(__i386__) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 294 | /* Sub p256_mod from a into r. (r = a - p256_mod). */ |
| 295 | static void sp_256_sub_8_p256_mod(sp_digit* r, const sp_digit* a) |
| 296 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 297 | 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 Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 336 | } |
Denys Vlasenko | 567eefc | 2021-10-06 14:25:10 +0200 | [diff] [blame] | 337 | #else |
| 338 | # define sp_256_sub_8_p256_mod(r, a) sp_256_sub_8((r), (a), p256_mod) |
| 339 | #endif |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 340 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 341 | /* Multiply a and b into r. (r = a * b) */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 342 | static void sp_256_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 343 | { |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 344 | #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 Vlasenko | 22fd8fd | 2021-10-06 16:10:49 +0200 | [diff] [blame^] | 396 | "\n 2:" |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 397 | "\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 Vlasenko | 22fd8fd | 2021-10-06 16:10:49 +0200 | [diff] [blame^] | 419 | : "memory", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14" |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 420 | ); |
| 421 | memcpy(r, tmp, sizeof(tmp)); |
| 422 | #else |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 423 | sp_digit rr[15]; /* in case r coincides with a or b */ |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 424 | int i, j, k; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 425 | uint64_t acc; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 426 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 427 | 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 Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 435 | do { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 436 | uint64_t m = ((uint64_t)a[i]) * b[j]; |
| 437 | acc += m; |
| 438 | if (acc < m) |
| 439 | acc_hi++; |
| 440 | j--; |
| 441 | i++; |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 442 | } while (i != 8 && i <= k); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 443 | rr[k] = acc; |
| 444 | acc = (acc >> 32) | ((uint64_t)acc_hi << 32); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 445 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 446 | r[15] = acc; |
| 447 | memcpy(r, rr, sizeof(rr)); |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 448 | #endif |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 451 | /* Shift number right one bit. Bottom bit is lost. */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 452 | static void sp_256_rshift1_8(sp_digit* r, sp_digit* a, sp_digit carry) |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 453 | { |
| 454 | int i; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 455 | |
| 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 Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 462 | } |
| 463 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 464 | /* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 465 | static void sp_256_div2_8(sp_digit* r, const sp_digit* a, const sp_digit* m) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 466 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 467 | int carry = 0; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 468 | if (a[0] & 1) |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 469 | carry = sp_256_add_8(r, a, m); |
| 470 | sp_256_norm_8(r); |
| 471 | sp_256_rshift1_8(r, r, carry); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* Add two Montgomery form numbers (r = a + b % m) */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 475 | static void sp_256_mont_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b |
| 476 | /*, const sp_digit* m*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 477 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 478 | // const sp_digit* m = p256_mod; |
| 479 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 480 | int carry = sp_256_add_8(r, a, b); |
| 481 | sp_256_norm_8(r); |
| 482 | if (carry) { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 483 | sp_256_sub_8_p256_mod(r, r /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 484 | sp_256_norm_8(r); |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 485 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* Subtract two Montgomery form numbers (r = a - b % m) */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 489 | static void sp_256_mont_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b |
| 490 | /*, const sp_digit* m*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 491 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 492 | const sp_digit* m = p256_mod; |
| 493 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 494 | 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 Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 500 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* Double a Montgomery form number (r = a + a % m) */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 504 | static void sp_256_mont_dbl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 505 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 506 | // const sp_digit* m = p256_mod; |
| 507 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 508 | int carry = sp_256_add_8(r, a, a); |
| 509 | sp_256_norm_8(r); |
| 510 | if (carry) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 511 | sp_256_sub_8_p256_mod(r, r /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 512 | sp_256_norm_8(r); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* Triple a Montgomery form number (r = a + a + a % m) */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 516 | static void sp_256_mont_tpl_8(sp_digit* r, const sp_digit* a /*, const sp_digit* m*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 517 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 518 | // const sp_digit* m = p256_mod; |
| 519 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 520 | int carry = sp_256_add_8(r, a, a); |
| 521 | sp_256_norm_8(r); |
| 522 | if (carry) { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 523 | sp_256_sub_8_p256_mod(r, r /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 524 | sp_256_norm_8(r); |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 525 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 526 | carry = sp_256_add_8(r, r, a); |
| 527 | sp_256_norm_8(r); |
| 528 | if (carry) { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 529 | sp_256_sub_8_p256_mod(r, r /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 530 | sp_256_norm_8(r); |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 531 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /* Shift the result in the high 256 bits down to the bottom. */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 535 | static void sp_256_mont_shift_8(sp_digit* r, const sp_digit* a) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 536 | { |
| 537 | int i; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 538 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 539 | for (i = 0; i < 8; i++) { |
| 540 | r[i] = a[i+8]; |
| 541 | r[i+8] = 0; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 542 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 543 | } |
| 544 | |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 545 | /* Mul a by scalar b and add into r. (r += a * b) */ |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 546 | static int sp_256_mul_add_8(sp_digit* r /*, const sp_digit* a, sp_digit b*/) |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 547 | { |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 548 | // 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 Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 551 | |
Denys Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 552 | uint64_t t; |
| 553 | |
| 554 | // t = 0; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 555 | // 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 Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 568 | uint32_t t32; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 569 | |
| 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 Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 574 | // t = r[0] << 32 = b << 32; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 575 | //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 Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 610 | // t = b + r[3]; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 611 | //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 Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 616 | t32 = (r[3] < b); // 0 or 1 |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 617 | |
| 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 Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 622 | // t = t32 + r[4]; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 623 | //t_hi = (t < m); |
| 624 | // t_hi = 0; |
Denys Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 625 | //r[4] = (sp_digit)t; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 626 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
Denys Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 627 | if (t32 != 0) { |
| 628 | r[4]++; |
| 629 | t32 = (r[4] == 0); // 0 or 1 |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 630 | |
| 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 Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 635 | // t = t32 + r[5]; (t32 is 0 or 1) |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 636 | //t_hi = (t < m); |
| 637 | // t_hi = 0; |
Denys Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 638 | //r[5] = (sp_digit)t; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 639 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
Denys Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 640 | if (t32 != 0) { |
| 641 | r[5]++; |
| 642 | t32 = (r[5] == 0); // 0 or 1 |
| 643 | } |
| 644 | } |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 645 | |
| 646 | //m = ((uint64_t)b * a[6]) + r[6]; |
| 647 | // Since a[6] is 00000001, the above optimizes to: |
Denys Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 648 | // m = (uint64_t)b + r[6]; // 33 bits at most |
| 649 | //t += m; |
| 650 | t = t32 + (uint64_t)b + r[6]; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 651 | //t_hi = (t < m); |
Denys Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 652 | // t_hi = 0; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 653 | 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 Vlasenko | 00f2cce | 2021-10-06 10:15:29 +0200 | [diff] [blame] | 663 | // t_hi in fact is always 0 here (256bit * 32bit can't have more than 32 bits of overflow) |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 664 | r[7] = (sp_digit)t; |
| 665 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 666 | t = (t >> 32); |
| 667 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 668 | r[8] += (sp_digit)t; |
| 669 | return (r[8] < (sp_digit)t); /* 1 if addition overflowed */ |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 670 | } |
| 671 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 672 | /* 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 678 | static void sp_256_mont_reduce_8(sp_digit* a/*, const sp_digit* m, sp_digit mp*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 679 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 680 | // const sp_digit* m = p256_mod; |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 681 | sp_digit mp = p256_mp_mod; |
| 682 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 683 | int i; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 684 | // sp_digit mu; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 685 | |
| 686 | if (mp != 1) { |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 687 | 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 Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 700 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 701 | sp_256_mont_shift_8(a, a); |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 702 | if (word16th != 0) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 703 | sp_256_sub_8_p256_mod(a, a /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 704 | sp_256_norm_8(a); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 705 | } |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 706 | else { /* Same code for explicit mp == 1 (which is always the case for P256) */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 707 | sp_digit word16th = 0; |
| 708 | for (i = 0; i < 8; i++) { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 709 | /*mu = a[i];*/ |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 710 | if (sp_256_mul_add_8(a+i /*, m, mu*/)) { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 711 | 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 Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 720 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 721 | sp_256_mont_shift_8(a, a); |
| 722 | if (word16th != 0) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 723 | sp_256_sub_8_p256_mod(a, a /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 724 | sp_256_norm_8(a); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 725 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 726 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 727 | #if 0 |
| 728 | //TODO: arm32 asm (also adapt for x86?) |
| 729 | static 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 Vlasenko | 22fd8fd | 2021-10-06 16:10:49 +0200 | [diff] [blame^] | 735 | mov r12, #0 |
| 736 | ldr r10, [%[a], #0] |
| 737 | ldr r14, [%[a], #4] |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 738 | 1: |
Denys Vlasenko | 22fd8fd | 2021-10-06 16:10:49 +0200 | [diff] [blame^] | 739 | # 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 818 | |
Denys Vlasenko | 22fd8fd | 2021-10-06 16:10:49 +0200 | [diff] [blame^] | 819 | str r10, [%[a], #0] |
| 820 | str r14, [%[a], #4] |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 821 | : [ca] "+r" (ca), [a] "+r" (a) |
| 822 | : [m] "r" (m), [mp] "r" (mp) |
Denys Vlasenko | 22fd8fd | 2021-10-06 16:10:49 +0200 | [diff] [blame^] | 823 | : "memory", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r12", "r14" |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 824 | ); |
| 825 | |
Denys Vlasenko | 22fd8fd | 2021-10-06 16:10:49 +0200 | [diff] [blame^] | 826 | memcpy(a, a + 8, 32); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 827 | if (ca) |
| 828 | a -= m; |
| 829 | } |
| 830 | #endif |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 831 | |
| 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 841 | static void sp_256_mont_mul_8(sp_digit* r, const sp_digit* a, const sp_digit* b |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 842 | /*, const sp_digit* m, sp_digit mp*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 843 | { |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 844 | //const sp_digit* m = p256_mod; |
| 845 | //sp_digit mp = p256_mp_mod; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 846 | sp_256_mul_8(r, a, b); |
| 847 | sp_256_mont_reduce_8(r /*, m, mp*/); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 848 | } |
| 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 857 | static void sp_256_mont_sqr_8(sp_digit* r, const sp_digit* a |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 858 | /*, const sp_digit* m, sp_digit mp*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 859 | { |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 860 | //const sp_digit* m = p256_mod; |
| 861 | //sp_digit mp = p256_mp_mod; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 862 | sp_256_mont_mul_8(r, a, a /*, m, mp*/); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 863 | } |
| 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. */ |
| 873 | static 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 883 | static void sp_256_mont_inv_8(sp_digit* r, sp_digit* a) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 884 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 885 | sp_digit t[2*8]; //can be just [8]? |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 886 | int i; |
| 887 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 888 | memcpy(t, a, sizeof(sp_digit) * 8); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 889 | for (i = 254; i >= 0; i--) { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 890 | sp_256_mont_sqr_8(t, t /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 891 | /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/ |
| 892 | if (i >= 224 || i == 192 || (i <= 95 && i != 1)) |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 893 | sp_256_mont_mul_8(t, t, a /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 894 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 895 | memcpy(r, t, sizeof(sp_digit) * 8); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 896 | } |
| 897 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 898 | /* 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 903 | static void sp_256_mod_mul_norm_8(sp_digit* r, const sp_digit* a) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 904 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 905 | int64_t t[8]; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 906 | int32_t o; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 907 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 908 | #define A(n) ((uint64_t)a[n]) |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 909 | /* 1 1 0 -1 -1 -1 -1 0 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 910 | t[0] = 0 + A(0) + A(1) - A(3) - A(4) - A(5) - A(6); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 911 | /* 0 1 1 0 -1 -1 -1 -1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 912 | t[1] = 0 + A(1) + A(2) - A(4) - A(5) - A(6) - A(7); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 913 | /* 0 0 1 1 0 -1 -1 -1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 914 | t[2] = 0 + A(2) + A(3) - A(5) - A(6) - A(7); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 915 | /* -1 -1 0 2 2 1 0 -1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 916 | t[3] = 0 - A(0) - A(1) + 2 * A(3) + 2 * A(4) + A(5) - A(7); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 917 | /* 0 -1 -1 0 2 2 1 0 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 918 | t[4] = 0 - A(1) - A(2) + 2 * A(4) + 2 * A(5) + A(6); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 919 | /* 0 0 -1 -1 0 2 2 1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 920 | t[5] = 0 - A(2) - A(3) + 2 * A(5) + 2 * A(6) + A(7); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 921 | /* -1 -1 0 0 0 1 3 2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 922 | t[6] = 0 - A(0) - A(1) + A(5) + 3 * A(6) + 2 * A(7); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 923 | /* 1 0 -1 -1 -1 -1 0 3 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 924 | t[7] = 0 + A(0) - A(2) - A(3) - A(4) - A(5) + 3 * A(7); |
| 925 | #undef A |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 926 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 927 | 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 Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 934 | o = t[7] >> 32; //t[7] &= 0xffffffff; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 935 | t[0] += o; |
| 936 | t[3] -= o; |
| 937 | t[6] -= o; |
| 938 | t[7] += o; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 939 | 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 957 | /* 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 961 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 962 | static void sp_256_map_8(sp_point* r, sp_point* p) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 963 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 964 | sp_digit t1[2*8]; |
| 965 | sp_digit t2[2*8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 966 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 967 | sp_256_mont_inv_8(t1, p->z); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 968 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 969 | 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 971 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 972 | /* x /= z^2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 973 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 976 | /* Reduce x to less than modulus */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 977 | if (sp_256_cmp_8(r->x, p256_mod) >= 0) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 978 | sp_256_sub_8_p256_mod(r->x, r->x /*, p256_mod*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 979 | sp_256_norm_8(r->x); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 980 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 981 | /* y /= z^3 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 982 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 985 | /* Reduce y to less than modulus */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 986 | if (sp_256_cmp_8(r->y, p256_mod) >= 0) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 987 | sp_256_sub_8_p256_mod(r->y, r->y /*, p256_mod*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 988 | sp_256_norm_8(r->y); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 989 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 990 | memset(r->z, 0, sizeof(r->z)); |
| 991 | r->z[0] = 1; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /* Double the Montgomery form projective point p. |
| 995 | * |
| 996 | * r Result of doubling point. |
| 997 | * p Point to double. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 998 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 999 | static void sp_256_proj_point_dbl_8(sp_point* r, sp_point* p) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1000 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1001 | sp_digit t1[2*8]; |
| 1002 | sp_digit t2[2*8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1003 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1004 | /* Put point to double into result */ |
| 1005 | if (r != p) |
| 1006 | *r = *p; /* struct copy */ |
Denys Vlasenko | 4d3a5c1 | 2021-04-26 15:21:38 +0200 | [diff] [blame] | 1007 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1008 | if (r->infinity) |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 1009 | return; |
| 1010 | |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 1011 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1018 | /* T1 = Z * Z */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1019 | sp_256_mont_sqr_8(t1, r->z /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1020 | /* Z = Y * Z */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1021 | sp_256_mont_mul_8(r->z, r->y, r->z /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1022 | /* Z = 2Z */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1023 | sp_256_mont_dbl_8(r->z, r->z /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1024 | /* T2 = X - T1 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1025 | sp_256_mont_sub_8(t2, r->x, t1 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1026 | /* T1 = X + T1 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1027 | sp_256_mont_add_8(t1, r->x, t1 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1028 | /* T2 = T1 * T2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1029 | sp_256_mont_mul_8(t2, t1, t2 /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1030 | /* T1 = 3T2 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1031 | sp_256_mont_tpl_8(t1, t2 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1032 | /* Y = 2Y */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1033 | sp_256_mont_dbl_8(r->y, r->y /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1034 | /* Y = Y * Y */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1035 | sp_256_mont_sqr_8(r->y, r->y /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1036 | /* T2 = Y * Y */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1037 | sp_256_mont_sqr_8(t2, r->y /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1038 | /* T2 = T2/2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1039 | sp_256_div2_8(t2, t2, p256_mod); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1040 | /* Y = Y * X */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1041 | sp_256_mont_mul_8(r->y, r->y, r->x /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1042 | /* X = T1 * T1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1043 | sp_256_mont_mul_8(r->x, t1, t1 /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1044 | /* X = X - Y */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1045 | sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1046 | /* X = X - Y */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1047 | sp_256_mont_sub_8(r->x, r->x, r->y /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1048 | /* Y = Y - X */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1049 | sp_256_mont_sub_8(r->y, r->y, r->x /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1050 | /* Y = Y * T1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1051 | sp_256_mont_mul_8(r->y, r->y, t1 /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1052 | /* Y = Y - T2 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1053 | sp_256_mont_sub_8(r->y, r->y, t2 /*, p256_mod*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1054 | dump_512("y2 %s\n", r->y); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1055 | } |
| 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1062 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1063 | static void sp_256_proj_point_add_8(sp_point* r, sp_point* p, sp_point* q) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1064 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1065 | 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1070 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1071 | /* 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1077 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1078 | /* Check double */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1079 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1084 | ) { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1085 | sp_256_proj_point_dbl_8(r, p); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1086 | } |
| 1087 | else { |
| 1088 | sp_point tp; |
| 1089 | sp_point *v; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1090 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1091 | v = r; |
| 1092 | if (p->infinity | q->infinity) { |
| 1093 | memset(&tp, 0, sizeof(tp)); |
| 1094 | v = &tp; |
| 1095 | } |
Denys Vlasenko | 772e187 | 2021-04-26 17:25:27 +0200 | [diff] [blame] | 1096 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1097 | *r = p->infinity ? *q : *p; /* struct copy */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1098 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1099 | /* U1 = X1*Z2^2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1100 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1103 | /* U2 = X2*Z1^2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1104 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1107 | /* S1 = Y1*Z2^3 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1108 | sp_256_mont_mul_8(t3, t3, v->y /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1109 | /* S2 = Y2*Z1^3 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1110 | sp_256_mont_mul_8(t4, t4, q->y /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1111 | /* H = U2 - U1 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1112 | sp_256_mont_sub_8(t2, t2, t1 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1113 | /* R = S2 - S1 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1114 | sp_256_mont_sub_8(t4, t4, t3 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1115 | /* Z3 = H*Z1*Z2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1116 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1118 | /* X3 = R^2 - H^3 - 2*U1*H^2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1119 | 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 Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1123 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1126 | /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1127 | sp_256_mont_sub_8(v->y, v->y, v->x /*, p256_mod*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1128 | 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 Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame] | 1130 | sp_256_mont_sub_8(v->y, v->y, t5 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1131 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1132 | } |
| 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 Vlasenko | 03ab2a9 | 2021-04-26 14:55:46 +0200 | [diff] [blame] | 1140 | * map Indicates whether to convert result to affine. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1141 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1142 | static void sp_256_ecc_mulmod_8(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1143 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1144 | enum { map = 1 }; /* we always convert result to affine coordinates */ |
| 1145 | sp_point t[3]; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1146 | sp_digit n = n; /* for compiler */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1147 | int c, y; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1148 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1149 | memset(t, 0, sizeof(t)); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1150 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1151 | /* t[0] = {0, 0, 1} * norm */ |
| 1152 | t[0].infinity = 1; |
| 1153 | /* t[1] = {g->x, g->y, g->z} * norm */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1154 | 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1157 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1158 | /* 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1164 | break; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1165 | n = *k--; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1166 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1167 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1168 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1178 | memcpy(&t[2], &t[y], sizeof(sp_point)); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1179 | 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 Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1184 | memcpy(&t[y], &t[2], sizeof(sp_point)); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1185 | |
| 1186 | n <<= 1; |
| 1187 | c--; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1188 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1189 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1190 | if (map) |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1191 | sp_256_map_8(r, &t[0]); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1192 | else |
| 1193 | memcpy(r, &t[0], sizeof(sp_point)); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1194 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1195 | memset(t, 0, sizeof(t)); //paranoia |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1196 | } |
| 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 Vlasenko | 03ab2a9 | 2021-04-26 14:55:46 +0200 | [diff] [blame] | 1203 | * map Indicates whether to convert result to affine. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1204 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1205 | static void sp_256_ecc_mulmod_base_8(sp_point* r, sp_digit* k /*, int map*/) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1206 | { |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 1207 | /* 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 Vlasenko | 48a18d1 | 2021-04-27 12:24:21 +0200 | [diff] [blame] | 1211 | 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 Vlasenko | 646e856 | 2021-04-27 13:09:44 +0200 | [diff] [blame] | 1216 | /* z will be set to 1, infinity flag to "false" */ |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 1217 | }; |
| 1218 | sp_point p256_base; |
| 1219 | |
Denys Vlasenko | 48a18d1 | 2021-04-27 12:24:21 +0200 | [diff] [blame] | 1220 | sp_256_point_from_bin2x32(&p256_base, p256_base_bin); |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 1221 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1222 | sp_256_ecc_mulmod_8(r, &p256_base, k /*, map*/); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1223 | } |
| 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 Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1229 | * pub2x32 Point to multiply. |
| 1230 | * out32 Buffer to hold X ordinate. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1231 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1232 | static void sp_ecc_secret_gen_256(const sp_digit priv[8], const uint8_t *pub2x32, uint8_t* out32) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1233 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1234 | sp_point point[1]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1235 | |
| 1236 | #if FIXED_PEER_PUBKEY |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1237 | memset((void*)pub2x32, 0x55, 64); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1238 | #endif |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1239 | dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */ |
| 1240 | dump_hex(" %s\n", pub2x32 + 32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1241 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1242 | sp_256_point_from_bin2x32(point, pub2x32); |
Denys Vlasenko | 81d8af1 | 2021-10-05 17:31:33 +0200 | [diff] [blame] | 1243 | dump_512("point->x %s\n", point->x); |
| 1244 | dump_512("point->y %s\n", point->y); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1245 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1246 | sp_256_ecc_mulmod_8(point, point, priv); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1247 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1248 | sp_256_to_bin_8(point->x, out32); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1249 | dump_hex("out32: %s\n", out32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1252 | /* Generates a random scalar in [1..order-1] range. */ |
| 1253 | static void sp_256_ecc_gen_k_8(sp_digit k[8]) |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1254 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1255 | /* 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1260 | #if FIXED_SECRET |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1261 | memset(k, 0x77, 8 * sizeof(k[0])); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1262 | #endif |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1263 | |
| 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 Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1276 | /* Makes a random EC key pair. */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1277 | static void sp_ecc_make_key_256(sp_digit privkey[8], uint8_t *pubkey) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1278 | { |
| 1279 | sp_point point[1]; |
| 1280 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1281 | sp_256_ecc_gen_k_8(privkey); |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 1282 | dump_256("privkey %s\n", privkey); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1283 | sp_256_ecc_mulmod_base_8(point, privkey); |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 1284 | dump_512("point->x %s\n", point->x); |
| 1285 | dump_512("point->y %s\n", point->y); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1286 | sp_256_to_bin_8(point->x, pubkey); |
| 1287 | sp_256_to_bin_8(point->y, pubkey + 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1288 | |
| 1289 | memset(point, 0, sizeof(point)); //paranoia |
| 1290 | } |
| 1291 | |
| 1292 | void FAST_FUNC curve_P256_compute_pubkey_and_premaster( |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1293 | uint8_t *pubkey2x32, uint8_t *premaster32, |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1294 | const uint8_t *peerkey2x32) |
| 1295 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1296 | sp_digit privkey[8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1297 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1298 | dump_hex("peerkey2x32: %s\n", peerkey2x32, 64); |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1299 | sp_ecc_make_key_256(privkey, pubkey2x32); |
| 1300 | dump_hex("pubkey: %s\n", pubkey2x32, 32); |
| 1301 | dump_hex(" %s\n", pubkey2x32 + 32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1302 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1303 | /* Combine our privkey and peer's public key to generate premaster */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1304 | sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32); |
| 1305 | dump_hex("premaster: %s\n", premaster32, 32); |
| 1306 | } |