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. |
| 35 | * Header comment is kept intact: |
| 36 | */ |
| 37 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 38 | typedef struct sp_point { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 39 | sp_digit x[2 * 8]; |
| 40 | sp_digit y[2 * 8]; |
| 41 | sp_digit z[2 * 8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 42 | int infinity; |
| 43 | } sp_point; |
| 44 | |
| 45 | /* The modulus (prime) of the curve P256. */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 46 | static const sp_digit p256_mod[8] = { |
| 47 | 0xffffffff,0xffffffff,0xffffffff,0x00000000, |
| 48 | 0x00000000,0x00000000,0x00000001,0xffffffff, |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | #define p256_mp_mod ((sp_digit)0x000001) |
| 52 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 53 | /* Normalize the values in each word to 32 bits - NOP */ |
| 54 | #define sp_256_norm_8(a) ((void)0) |
Denys Vlasenko | 7714518 | 2021-10-01 13:51:39 +0200 | [diff] [blame] | 55 | |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 56 | /* Write r as big endian to byte array. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 57 | * Fixed length number of bytes written: 32 |
| 58 | * |
| 59 | * r A single precision integer. |
| 60 | * a Byte array. |
| 61 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 62 | 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] | 63 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 64 | int i; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 65 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 66 | sp_256_norm_8(r); |
Denys Vlasenko | 7714518 | 2021-10-01 13:51:39 +0200 | [diff] [blame] | 67 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 68 | r += 8; |
| 69 | for (i = 0; i < 8; i++) { |
| 70 | r--; |
| 71 | move_to_unaligned32(a, SWAP_BE32(*r)); |
| 72 | a += 4; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 73 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 76 | /* Read big endian unsigned byte array into r. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 77 | * |
| 78 | * r A single precision integer. |
| 79 | * a Byte array. |
| 80 | * n Number of bytes in array to read. |
| 81 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 82 | 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] | 83 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 84 | int i; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 85 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 86 | r += 8; |
| 87 | for (i = 0; i < 8; i++) { |
| 88 | sp_digit v; |
| 89 | move_from_unaligned32(v, a); |
| 90 | *--r = SWAP_BE32(v); |
| 91 | a += 4; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 92 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 95 | #if SP_DEBUG |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 96 | static void dump_256(const char *fmt, const sp_digit* r) |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 97 | { |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 98 | uint8_t b32[32]; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 99 | sp_256_to_bin_8(r, b32); |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 100 | dump_hex(fmt, b32, 32); |
| 101 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 102 | static void dump_512(const char *fmt, const sp_digit* r) |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 103 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 104 | uint8_t b64[64]; |
| 105 | sp_256_to_bin_8(r, b64 + 32); |
| 106 | sp_256_to_bin_8(r+8, b64); |
| 107 | dump_hex(fmt, b64, 64); |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 108 | } |
| 109 | #else |
| 110 | # define dump_256(...) ((void)0) |
| 111 | # define dump_512(...) ((void)0) |
| 112 | #endif |
| 113 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 114 | /* Convert a point of big-endian 32-byte x,y pair to type sp_point. */ |
| 115 | static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32) |
| 116 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 117 | memset(p, 0, sizeof(*p)); |
| 118 | /*p->infinity = 0;*/ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 119 | sp_256_from_bin_8(p->x, bin2x32); |
| 120 | sp_256_from_bin_8(p->y, bin2x32 + 32); |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 121 | p->z[0] = 1; /* p->z = 1 */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Denys Vlasenko | b3b1713 | 2021-04-26 16:53:53 +0200 | [diff] [blame] | 124 | /* Compare a with b. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 125 | * |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 126 | * return -ve, 0 or +ve if a is less than, equal to or greater than b |
| 127 | * respectively. |
| 128 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 129 | 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] | 130 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 131 | int i; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 132 | for (i = 7; i >= 0; i--) { |
| 133 | /* signed_sp_digit r = a[i] - b[i]; |
| 134 | * if (r != 0) |
| 135 | * return r; |
| 136 | * does not work: think about a[i]=0, b[i]=0xffffffff |
| 137 | */ |
| 138 | if (a[i] == b[i]) |
| 139 | continue; |
| 140 | return (a[i] > b[i]) * 2 - 1; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 141 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 142 | return 0; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Compare two numbers to determine if they are equal. |
| 146 | * |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 147 | * return 1 when equal and 0 otherwise. |
| 148 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 149 | 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] | 150 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 151 | return sp_256_cmp_8(a, b) == 0; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 154 | /* Add b to a into r. (r = a + b). Return !0 on overflow */ |
| 155 | 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] | 156 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 157 | #if ALLOW_ASM && defined(__GNUC__) && defined(__i386__) |
| 158 | sp_digit reg; |
| 159 | asm volatile ( |
| 160 | "\n movl (%0), %3" |
| 161 | "\n addl (%1), %3" |
| 162 | "\n movl %3, (%2)" |
| 163 | "\n" |
| 164 | "\n movl 1*4(%0), %3" |
| 165 | "\n adcl 1*4(%1), %3" |
| 166 | "\n movl %3, 1*4(%2)" |
| 167 | "\n" |
| 168 | "\n movl 2*4(%0), %3" |
| 169 | "\n adcl 2*4(%1), %3" |
| 170 | "\n movl %3, 2*4(%2)" |
| 171 | "\n" |
| 172 | "\n movl 3*4(%0), %3" |
| 173 | "\n adcl 3*4(%1), %3" |
| 174 | "\n movl %3, 3*4(%2)" |
| 175 | "\n" |
| 176 | "\n movl 4*4(%0), %3" |
| 177 | "\n adcl 4*4(%1), %3" |
| 178 | "\n movl %3, 4*4(%2)" |
| 179 | "\n" |
| 180 | "\n movl 5*4(%0), %3" |
| 181 | "\n adcl 5*4(%1), %3" |
| 182 | "\n movl %3, 5*4(%2)" |
| 183 | "\n" |
| 184 | "\n movl 6*4(%0), %3" |
| 185 | "\n adcl 6*4(%1), %3" |
| 186 | "\n movl %3, 6*4(%2)" |
| 187 | "\n" |
| 188 | "\n movl 7*4(%0), %3" |
| 189 | "\n adcl 7*4(%1), %3" |
| 190 | "\n movl %3, 7*4(%2)" |
| 191 | "\n" |
| 192 | "\n sbbl %3, %3" |
| 193 | "\n" |
| 194 | : "=r" (a), "=r" (b), "=r" (r), "=r" (reg) |
| 195 | : "0" (a), "1" (b), "2" (r) |
| 196 | : "memory" |
| 197 | ); |
| 198 | return reg; |
| 199 | #else |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 200 | int i; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 201 | sp_digit carry; |
| 202 | |
| 203 | carry = 0; |
| 204 | for (i = 0; i < 8; i++) { |
| 205 | sp_digit w, v; |
| 206 | w = b[i] + carry; |
| 207 | v = a[i]; |
| 208 | if (w != 0) { |
| 209 | v = a[i] + w; |
| 210 | carry = (v < a[i]); |
| 211 | /* hope compiler detects above as "carry flag set" */ |
| 212 | } |
| 213 | /* else: b + carry == 0, two cases: |
| 214 | * b:ffffffff, carry:1 |
| 215 | * b:00000000, carry:0 |
| 216 | * in either case, r[i] = a[i] and carry remains unchanged |
| 217 | */ |
| 218 | r[i] = v; |
| 219 | } |
| 220 | return carry; |
| 221 | #endif |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 224 | /* Sub b from a into r. (r = a - b). Return !0 on underflow */ |
| 225 | 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] | 226 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 227 | #if ALLOW_ASM && defined(__GNUC__) && defined(__i386__) |
| 228 | sp_digit reg; |
| 229 | asm volatile ( |
| 230 | "\n movl (%0), %3" |
| 231 | "\n subl (%1), %3" |
| 232 | "\n movl %3, (%2)" |
| 233 | "\n" |
| 234 | "\n movl 1*4(%0), %3" |
| 235 | "\n sbbl 1*4(%1), %3" |
| 236 | "\n movl %3, 1*4(%2)" |
| 237 | "\n" |
| 238 | "\n movl 2*4(%0), %3" |
| 239 | "\n sbbl 2*4(%1), %3" |
| 240 | "\n movl %3, 2*4(%2)" |
| 241 | "\n" |
| 242 | "\n movl 3*4(%0), %3" |
| 243 | "\n sbbl 3*4(%1), %3" |
| 244 | "\n movl %3, 3*4(%2)" |
| 245 | "\n" |
| 246 | "\n movl 4*4(%0), %3" |
| 247 | "\n sbbl 4*4(%1), %3" |
| 248 | "\n movl %3, 4*4(%2)" |
| 249 | "\n" |
| 250 | "\n movl 5*4(%0), %3" |
| 251 | "\n sbbl 5*4(%1), %3" |
| 252 | "\n movl %3, 5*4(%2)" |
| 253 | "\n" |
| 254 | "\n movl 6*4(%0), %3" |
| 255 | "\n sbbl 6*4(%1), %3" |
| 256 | "\n movl %3, 6*4(%2)" |
| 257 | "\n" |
| 258 | "\n movl 7*4(%0), %3" |
| 259 | "\n sbbl 7*4(%1), %3" |
| 260 | "\n movl %3, 7*4(%2)" |
| 261 | "\n" |
| 262 | "\n sbbl %3, %3" |
| 263 | "\n" |
| 264 | : "=r" (a), "=r" (b), "=r" (r), "=r" (reg) |
| 265 | : "0" (a), "1" (b), "2" (r) |
| 266 | : "memory" |
| 267 | ); |
| 268 | return reg; |
| 269 | #else |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 270 | int i; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 271 | sp_digit borrow; |
| 272 | |
| 273 | borrow = 0; |
| 274 | for (i = 0; i < 8; i++) { |
| 275 | sp_digit w, v; |
| 276 | w = b[i] + borrow; |
| 277 | v = a[i]; |
| 278 | if (w != 0) { |
| 279 | v = a[i] - w; |
| 280 | borrow = (v > a[i]); |
| 281 | /* hope compiler detects above as "carry flag set" */ |
| 282 | } |
| 283 | /* else: b + borrow == 0, two cases: |
| 284 | * b:ffffffff, borrow:1 |
| 285 | * b:00000000, borrow:0 |
| 286 | * in either case, r[i] = a[i] and borrow remains unchanged |
| 287 | */ |
| 288 | r[i] = v; |
| 289 | } |
| 290 | return borrow; |
| 291 | #endif |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 292 | } |
| 293 | |
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 | { |
| 297 | #if ALLOW_ASM && defined(__GNUC__) && defined(__i386__) |
| 298 | sp_digit reg; |
| 299 | //p256_mod[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff |
| 300 | asm volatile ( |
| 301 | "\n movl (%0), %2" |
| 302 | "\n subl $0xffffffff, %2" |
| 303 | "\n movl %2, (%1)" |
| 304 | "\n" |
| 305 | "\n movl 1*4(%0), %2" |
| 306 | "\n sbbl $0xffffffff, %2" |
| 307 | "\n movl %2, 1*4(%1)" |
| 308 | "\n" |
| 309 | "\n movl 2*4(%0), %2" |
| 310 | "\n sbbl $0xffffffff, %2" |
| 311 | "\n movl %2, 2*4(%1)" |
| 312 | "\n" |
| 313 | "\n movl 3*4(%0), %2" |
| 314 | "\n sbbl $0, %2" |
| 315 | "\n movl %2, 3*4(%1)" |
| 316 | "\n" |
| 317 | "\n movl 4*4(%0), %2" |
| 318 | "\n sbbl $0, %2" |
| 319 | "\n movl %2, 4*4(%1)" |
| 320 | "\n" |
| 321 | "\n movl 5*4(%0), %2" |
| 322 | "\n sbbl $0, %2" |
| 323 | "\n movl %2, 5*4(%1)" |
| 324 | "\n" |
| 325 | "\n movl 6*4(%0), %2" |
| 326 | "\n sbbl $1, %2" |
| 327 | "\n movl %2, 6*4(%1)" |
| 328 | "\n" |
| 329 | "\n movl 7*4(%0), %2" |
| 330 | "\n sbbl $0xffffffff, %2" |
| 331 | "\n movl %2, 7*4(%1)" |
| 332 | "\n" |
| 333 | : "=r" (a), "=r" (r), "=r" (reg) |
| 334 | : "0" (a), "1" (r) |
| 335 | : "memory" |
| 336 | ); |
| 337 | #else |
| 338 | const sp_digit* b = p256_mod; |
| 339 | int i; |
| 340 | sp_digit borrow; |
| 341 | |
| 342 | borrow = 0; |
| 343 | for (i = 0; i < 8; i++) { |
| 344 | sp_digit w, v; |
| 345 | w = b[i] + borrow; |
| 346 | v = a[i]; |
| 347 | if (w != 0) { |
| 348 | v = a[i] - w; |
| 349 | borrow = (v > a[i]); |
| 350 | /* hope compiler detects above as "carry flag set" */ |
| 351 | } |
| 352 | /* else: b + borrow == 0, two cases: |
| 353 | * b:ffffffff, borrow:1 |
| 354 | * b:00000000, borrow:0 |
| 355 | * in either case, r[i] = a[i] and borrow remains unchanged |
| 356 | */ |
| 357 | r[i] = v; |
| 358 | } |
| 359 | #endif |
| 360 | } |
| 361 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 362 | /* Multiply a and b into r. (r = a * b) */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 363 | 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] | 364 | { |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 365 | #if ALLOW_ASM && defined(__GNUC__) && defined(__i386__) |
| 366 | sp_digit rr[15]; /* in case r coincides with a or b */ |
| 367 | int k; |
| 368 | uint32_t accl; |
| 369 | uint32_t acch; |
| 370 | |
| 371 | acch = accl = 0; |
| 372 | for (k = 0; k < 15; k++) { |
| 373 | int i, j; |
| 374 | uint32_t acc_hi; |
| 375 | i = k - 7; |
| 376 | if (i < 0) |
| 377 | i = 0; |
| 378 | j = k - i; |
| 379 | acc_hi = 0; |
| 380 | do { |
| 381 | //////////////////////// |
| 382 | // uint64_t m = ((uint64_t)a[i]) * b[j]; |
| 383 | // acc_hi:acch:accl += m; |
| 384 | asm volatile ( |
| 385 | // a[i] is already loaded in %%eax |
| 386 | "\n mull %7" |
| 387 | "\n addl %%eax, %0" |
| 388 | "\n adcl %%edx, %1" |
| 389 | "\n adcl $0, %2" |
| 390 | : "=rm" (accl), "=rm" (acch), "=rm" (acc_hi) |
| 391 | : "0" (accl), "1" (acch), "2" (acc_hi), "a" (a[i]), "m" (b[j]) |
| 392 | : "cc", "dx" |
| 393 | ); |
| 394 | //////////////////////// |
| 395 | j--; |
| 396 | i++; |
| 397 | } while (i != 8 && i <= k); |
| 398 | rr[k] = accl; |
| 399 | accl = acch; |
| 400 | acch = acc_hi; |
| 401 | } |
| 402 | r[15] = accl; |
| 403 | memcpy(r, rr, sizeof(rr)); |
| 404 | #elif 0 |
| 405 | //TODO: arm assembly (untested) |
| 406 | sp_digit tmp[16]; |
| 407 | |
| 408 | asm volatile ( |
| 409 | "\n mov r5, #0" |
| 410 | "\n mov r6, #0" |
| 411 | "\n mov r7, #0" |
| 412 | "\n mov r8, #0" |
| 413 | "\n 1:" |
| 414 | "\n subs r3, r5, #28" |
| 415 | "\n movcc r3, #0" |
| 416 | "\n sub r4, r5, r3" |
| 417 | "\n 2:" |
| 418 | "\n ldr r14, [%[a], r3]" |
| 419 | "\n ldr r12, [%[b], r4]" |
| 420 | "\n umull r9, r10, r14, r12" |
| 421 | "\n adds r6, r6, r9" |
| 422 | "\n adcs r7, r7, r10" |
| 423 | "\n adc r8, r8, #0" |
| 424 | "\n add r3, r3, #4" |
| 425 | "\n sub r4, r4, #4" |
| 426 | "\n cmp r3, #32" |
| 427 | "\n beq 3f" |
| 428 | "\n cmp r3, r5" |
| 429 | "\n ble 2b" |
| 430 | "\n 3:" |
| 431 | "\n str r6, [%[r], r5]" |
| 432 | "\n mov r6, r7" |
| 433 | "\n mov r7, r8" |
| 434 | "\n mov r8, #0" |
| 435 | "\n add r5, r5, #4" |
| 436 | "\n cmp r5, #56" |
| 437 | "\n ble 1b" |
| 438 | "\n str r6, [%[r], r5]" |
| 439 | : [r] "r" (tmp), [a] "r" (a), [b] "r" (b) |
| 440 | : "memory", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r14", "r12" |
| 441 | ); |
| 442 | memcpy(r, tmp, sizeof(tmp)); |
| 443 | #else |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 444 | sp_digit rr[15]; /* in case r coincides with a or b */ |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 445 | int i, j, k; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 446 | uint64_t acc; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 447 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 448 | acc = 0; |
| 449 | for (k = 0; k < 15; k++) { |
| 450 | uint32_t acc_hi; |
| 451 | i = k - 7; |
| 452 | if (i < 0) |
| 453 | i = 0; |
| 454 | j = k - i; |
| 455 | acc_hi = 0; |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 456 | do { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 457 | uint64_t m = ((uint64_t)a[i]) * b[j]; |
| 458 | acc += m; |
| 459 | if (acc < m) |
| 460 | acc_hi++; |
| 461 | j--; |
| 462 | i++; |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 463 | } while (i != 8 && i <= k); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 464 | rr[k] = acc; |
| 465 | acc = (acc >> 32) | ((uint64_t)acc_hi << 32); |
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 | r[15] = acc; |
| 468 | memcpy(r, rr, sizeof(rr)); |
Denys Vlasenko | bbd723e | 2021-10-05 23:19:18 +0200 | [diff] [blame] | 469 | #endif |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 472 | /* Shift number right one bit. Bottom bit is lost. */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 473 | 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] | 474 | { |
| 475 | int i; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 476 | |
| 477 | carry = (!!carry << 31); |
| 478 | for (i = 7; i >= 0; i--) { |
| 479 | sp_digit c = a[i] << 31; |
| 480 | r[i] = (a[i] >> 1) | carry; |
| 481 | carry = c; |
| 482 | } |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 485 | /* 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] | 486 | 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] | 487 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 488 | int carry = 0; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 489 | if (a[0] & 1) |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 490 | carry = sp_256_add_8(r, a, m); |
| 491 | sp_256_norm_8(r); |
| 492 | sp_256_rshift1_8(r, r, carry); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* Add two Montgomery form numbers (r = a + b % m) */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 496 | static void sp_256_mont_add_8(sp_digit* r, const sp_digit* a, const sp_digit* b |
| 497 | /*, const sp_digit* m*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 498 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 499 | // const sp_digit* m = p256_mod; |
| 500 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 501 | int carry = sp_256_add_8(r, a, b); |
| 502 | sp_256_norm_8(r); |
| 503 | if (carry) { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 504 | sp_256_sub_8_p256_mod(r, r /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 505 | sp_256_norm_8(r); |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 506 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* Subtract two Montgomery form numbers (r = a - b % m) */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 510 | static void sp_256_mont_sub_8(sp_digit* r, const sp_digit* a, const sp_digit* b |
| 511 | /*, const sp_digit* m*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 512 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 513 | const sp_digit* m = p256_mod; |
| 514 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 515 | int borrow; |
| 516 | borrow = sp_256_sub_8(r, a, b); |
| 517 | sp_256_norm_8(r); |
| 518 | if (borrow) { |
| 519 | sp_256_add_8(r, r, m); |
| 520 | sp_256_norm_8(r); |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 521 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* Double a Montgomery form number (r = a + a % m) */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 525 | 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] | 526 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 527 | // const sp_digit* m = p256_mod; |
| 528 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 529 | int carry = sp_256_add_8(r, a, a); |
| 530 | sp_256_norm_8(r); |
| 531 | if (carry) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 532 | sp_256_sub_8_p256_mod(r, r /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 533 | sp_256_norm_8(r); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /* Triple a Montgomery form number (r = a + a + a % m) */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 537 | 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] | 538 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 539 | // const sp_digit* m = p256_mod; |
| 540 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 541 | int carry = sp_256_add_8(r, a, a); |
| 542 | sp_256_norm_8(r); |
| 543 | if (carry) { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 544 | sp_256_sub_8_p256_mod(r, r /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 545 | sp_256_norm_8(r); |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 546 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 547 | carry = sp_256_add_8(r, r, a); |
| 548 | sp_256_norm_8(r); |
| 549 | if (carry) { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 550 | sp_256_sub_8_p256_mod(r, r /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 551 | sp_256_norm_8(r); |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 552 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* Shift the result in the high 256 bits down to the bottom. */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 556 | 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] | 557 | { |
| 558 | int i; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 559 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 560 | for (i = 0; i < 8; i++) { |
| 561 | r[i] = a[i+8]; |
| 562 | r[i+8] = 0; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 563 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 566 | /* Mul a by scalar b and add into r. (r += a * b) */ |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 567 | 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] | 568 | { |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 569 | // const sp_digit* a = p256_mod; |
| 570 | //a[7..0] = ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff |
| 571 | sp_digit b = r[0]; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 572 | uint64_t t = 0; |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 573 | |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 574 | // for (i = 0; i < 8; i++) { |
| 575 | // uint32_t t_hi; |
| 576 | // uint64_t m = ((uint64_t)b * a[i]) + r[i]; |
| 577 | // t += m; |
| 578 | // t_hi = (t < m); |
| 579 | // r[i] = (sp_digit)t; |
| 580 | // t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 581 | // } |
| 582 | // r[8] += (sp_digit)t; |
| 583 | |
| 584 | // Unroll, then optimize the above loop: |
| 585 | //uint32_t t_hi; |
| 586 | uint64_t m; |
| 587 | |
| 588 | //m = ((uint64_t)b * a[0]) + r[0]; |
| 589 | // Since b is r[0] and a[0] is ffffffff, the above optimizes to: |
| 590 | // m = r[0] * ffffffff + r[0] = (r[0] * 100000000 - r[0]) + r[0] = r[0] << 32; |
| 591 | //t += m; |
| 592 | // t = (uint64_t)r[0] << 32; |
| 593 | //t_hi = (t < m); |
| 594 | // t_hi = 0; |
| 595 | //r[0] = (sp_digit)t; |
| 596 | r[0] = 0; |
| 597 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 598 | // t = b; |
| 599 | |
| 600 | //m = ((uint64_t)b * a[1]) + r[1]; |
| 601 | // Since a[1] is ffffffff, the above optimizes to: |
| 602 | // m = b * ffffffff + r[1] = (b * 100000000 - b) + r[1] = (b << 32) - b + r[1]; |
| 603 | //t += m; |
| 604 | // t = b + (b << 32) - b + r[1] = (b << 32) + r[1]; |
| 605 | //t_hi = (t < m); |
| 606 | // t_hi = 0; |
| 607 | //r[1] = (sp_digit)t; |
| 608 | // r[1] = r[1]; |
| 609 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 610 | // t = b; |
| 611 | |
| 612 | //m = ((uint64_t)b * a[2]) + r[2]; |
| 613 | // Since a[2] is ffffffff, the above optimizes to: |
| 614 | // m = b * ffffffff + r[2] = (b * 100000000 - b) + r[2] = (b << 32) - b + r[2]; |
| 615 | //t += m; |
| 616 | // t = b + (b << 32) - b + r[2] = (b << 32) + r[2] |
| 617 | //t_hi = (t < m); |
| 618 | // t_hi = 0; |
| 619 | //r[2] = (sp_digit)t; |
| 620 | // r[2] = r[2]; |
| 621 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 622 | // t = b; |
| 623 | |
| 624 | //m = ((uint64_t)b * a[3]) + r[3]; |
| 625 | // Since a[3] is 00000000, the above optimizes to: |
| 626 | // m = b * 0 + r[3] = r[3]; |
| 627 | //t += m; |
| 628 | // t += r[3]; |
| 629 | //t_hi = (t < m); |
| 630 | // t_hi = 0; |
| 631 | //r[3] = (sp_digit)t; |
| 632 | r[3] = r[3] + b; |
| 633 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 634 | t = (r[3] < b); |
| 635 | |
| 636 | //m = ((uint64_t)b * a[4]) + r[4]; |
| 637 | // Since a[4] is 00000000, the above optimizes to: |
| 638 | // m = b * 0 + r[4] = r[4]; |
| 639 | //t += m; |
| 640 | t += r[4]; |
| 641 | //t_hi = (t < m); |
| 642 | // t_hi = 0; |
| 643 | r[4] = (sp_digit)t; |
| 644 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 645 | t = (t >> 32); |
| 646 | |
| 647 | //m = ((uint64_t)b * a[5]) + r[5]; |
| 648 | // Since a[5] is 00000000, the above optimizes to: |
| 649 | // m = b * 0 + r[5] = r[5]; |
| 650 | //t += m; |
| 651 | t += r[5]; |
| 652 | //t_hi = (t < m); |
| 653 | // t_hi = 0; |
| 654 | r[5] = (sp_digit)t; |
| 655 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 656 | t = (t >> 32); |
| 657 | |
| 658 | //m = ((uint64_t)b * a[6]) + r[6]; |
| 659 | // Since a[6] is 00000001, the above optimizes to: |
| 660 | m = (uint64_t)b + r[6]; // 33 bits at most |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 661 | t += m; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 662 | //t_hi = (t < m); |
| 663 | // t_hi = 0; //32bit_value + 33bit_value can't overflow 64 bits |
| 664 | r[6] = (sp_digit)t; |
| 665 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 666 | t = (t >> 32); |
| 667 | |
| 668 | //m = ((uint64_t)b * a[7]) + r[7]; |
| 669 | // Since a[7] is ffffffff, the above optimizes to: |
| 670 | // m = b * ffffffff + r[7] = (b * 100000000 - b) + r[7] |
| 671 | m = ((uint64_t)b << 32) - b + r[7]; |
| 672 | t += m; |
| 673 | //t_hi = (t < m); |
| 674 | // t_hi in fact is always 0 here |
| 675 | r[7] = (sp_digit)t; |
| 676 | //t = (t >> 32) | ((uint64_t)t_hi << 32); |
| 677 | t = (t >> 32); |
| 678 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 679 | r[8] += (sp_digit)t; |
| 680 | return (r[8] < (sp_digit)t); /* 1 if addition overflowed */ |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 683 | /* Reduce the number back to 256 bits using Montgomery reduction. |
| 684 | * |
| 685 | * a A single precision number to reduce in place. |
| 686 | * m The single precision number representing the modulus. |
| 687 | * mp The digit representing the negative inverse of m mod 2^n. |
| 688 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 689 | 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] | 690 | { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 691 | // const sp_digit* m = p256_mod; |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 692 | sp_digit mp = p256_mp_mod; |
| 693 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 694 | int i; |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 695 | // sp_digit mu; |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 696 | |
| 697 | if (mp != 1) { |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 698 | sp_digit word16th = 0; |
| 699 | for (i = 0; i < 8; i++) { |
| 700 | // mu = (sp_digit)(a[i] * mp); |
| 701 | if (sp_256_mul_add_8(a+i /*, m, mu*/)) { |
| 702 | int j = i + 8; |
| 703 | inc_next_word0: |
| 704 | if (++j > 15) { /* a[16] array has no more words? */ |
| 705 | word16th++; |
| 706 | continue; |
| 707 | } |
| 708 | if (++a[j] == 0) /* did this overflow too? */ |
| 709 | goto inc_next_word0; |
| 710 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 711 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 712 | sp_256_mont_shift_8(a, a); |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 713 | if (word16th != 0) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 714 | sp_256_sub_8_p256_mod(a, a /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 715 | sp_256_norm_8(a); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 716 | } |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 717 | 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] | 718 | sp_digit word16th = 0; |
| 719 | for (i = 0; i < 8; i++) { |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 720 | /*mu = a[i];*/ |
Denys Vlasenko | 2430fcf | 2021-10-06 00:19:30 +0200 | [diff] [blame] | 721 | if (sp_256_mul_add_8(a+i /*, m, mu*/)) { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 722 | int j = i + 8; |
| 723 | inc_next_word: |
| 724 | if (++j > 15) { /* a[16] array has no more words? */ |
| 725 | word16th++; |
| 726 | continue; |
| 727 | } |
| 728 | if (++a[j] == 0) /* did this overflow too? */ |
| 729 | goto inc_next_word; |
| 730 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 731 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 732 | sp_256_mont_shift_8(a, a); |
| 733 | if (word16th != 0) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 734 | sp_256_sub_8_p256_mod(a, a /*, m*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 735 | sp_256_norm_8(a); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 736 | } |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 737 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 738 | #if 0 |
| 739 | //TODO: arm32 asm (also adapt for x86?) |
| 740 | static void sp_256_mont_reduce_8(sp_digit* a, sp_digit* m, sp_digit mp) |
| 741 | { |
| 742 | sp_digit ca = 0; |
| 743 | |
| 744 | asm volatile ( |
| 745 | # i = 0 |
| 746 | mov r12, #0 # i = 0 |
| 747 | ldr r10, [%[a], #0] # r10 = a[0] |
| 748 | ldr r14, [%[a], #4] # r14 = a[1] |
| 749 | 1: |
| 750 | # mu = a[i] * mp # |
| 751 | mul r8, %[mp], r10 # mu = a[i] * mp |
| 752 | # a[i+0] += m[0] * mu # |
| 753 | ldr r7, [%[m], #0] # a[i+0] += m[0] * mu |
| 754 | ldr r9, [%[a], #0] # |
| 755 | umull r6, r7, r8, r7 # r7:r6 = mu * m[0] |
| 756 | adds r10, r10, r6 # r5:r10 += r7:r6 |
| 757 | adc r5, r7, #0 # |
| 758 | # a[i+1] += m[1] * mu # |
| 759 | ldr r7, [%[m], #4] # a[i+1] += m[1] * mu |
| 760 | ldr r9, [%[a], #4] # |
| 761 | umull r6, r7, r8, r7 # r7:r6 = mu * m[1] |
| 762 | adds r10, r14, r6 # r4:r10 = r7:r14 + r7:r6 |
| 763 | adc r4, r7, #0 # |
| 764 | adds r10, r10, r5 # r4:r10 += r5 |
| 765 | adc r4, r4, #0 # |
| 766 | # a[i+2] += m[2] * mu # |
| 767 | ldr r7, [%[m], #8] # a[i+2] += m[2] * mu |
| 768 | ldr r14, [%[a], #8] # |
| 769 | umull r6, r7, r8, r7 # |
| 770 | adds r14, r14, r6 # |
| 771 | adc r5, r7, #0 # |
| 772 | adds r14, r14, r4 # |
| 773 | adc r5, r5, #0 # |
| 774 | # a[i+3] += m[3] * mu # |
| 775 | ldr r7, [%[m], #12] # a[i+3] += m[3] * mu |
| 776 | ldr r9, [%[a], #12] # |
| 777 | umull r6, r7, r8, r7 # |
| 778 | adds r9, r9, r6 # |
| 779 | adc r4, r7, #0 # |
| 780 | adds r9, r9, r5 # |
| 781 | str r9, [%[a], #12] # a[3] = r9 |
| 782 | adc r4, r4, #0 # |
| 783 | # a[i+4] += m[4] * mu # |
| 784 | ldr r7, [%[m], #16] # a[i+4] += m[4] * mu |
| 785 | ldr r9, [%[a], #16] # |
| 786 | umull r6, r7, r8, r7 # |
| 787 | adds r9, r9, r6 # |
| 788 | adc r5, r7, #0 # |
| 789 | adds r9, r9, r4 # |
| 790 | str r9, [%[a], #16] # a[4] = r9 |
| 791 | adc r5, r5, #0 # |
| 792 | # a[i+5] += m[5] * mu # |
| 793 | ldr r7, [%[m], #20] # a[i+5] += m[5] * mu |
| 794 | ldr r9, [%[a], #20] # |
| 795 | umull r6, r7, r8, r7 # |
| 796 | adds r9, r9, r6 # |
| 797 | adc r4, r7, #0 # |
| 798 | adds r9, r9, r5 # |
| 799 | str r9, [%[a], #20] # a[5] = r9 |
| 800 | adc r4, r4, #0 # |
| 801 | # a[i+6] += m[6] * mu # |
| 802 | ldr r7, [%[m], #24] # a[i+6] += m[6] * mu |
| 803 | ldr r9, [%[a], #24] # |
| 804 | umull r6, r7, r8, r7 # |
| 805 | adds r9, r9, r6 # |
| 806 | adc r5, r7, #0 # |
| 807 | adds r9, r9, r4 # |
| 808 | str r9, [%[a], #24] # a[6] = r9 |
| 809 | adc r5, r5, #0 # |
| 810 | # a[i+7] += m[7] * mu # |
| 811 | ldr r7, [%[m], #28] # a[i+7] += m[7] * mu |
| 812 | ldr r9, [%[a], #28] # |
| 813 | umull r6, r7, r8, r7 # |
| 814 | adds r5, r5, r6 # |
| 815 | adcs r7, r7, %[ca] # |
| 816 | mov %[ca], #0 # |
| 817 | adc %[ca], %[ca], %[ca] # ca = CF |
| 818 | adds r9, r9, r5 # |
| 819 | str r9, [%[a], #28] # a[7] = r9 |
| 820 | ldr r9, [%[a], #32] # r9 = a[8] |
| 821 | adcs r9, r9, r7 # |
| 822 | str r9, [%[a], #32] # a[8] = r9 |
| 823 | adc %[ca], %[ca], #0 # ca += CF |
| 824 | # i += 1 # i++ |
| 825 | add %[a], %[a], #4 # a++ |
| 826 | add r12, r12, #4 # i += 4 |
| 827 | cmp r12, #32 # if (i < 32) |
| 828 | blt 1b # goto 1 |
| 829 | |
| 830 | str r10, [%[a], #0] # a[0] = r10 |
| 831 | str r14, [%[a], #4] # a[1] = r14 |
| 832 | : [ca] "+r" (ca), [a] "+r" (a) |
| 833 | : [m] "r" (m), [mp] "r" (mp) |
| 834 | : "memory", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r14", "r12" |
| 835 | ); |
| 836 | |
| 837 | if (ca) |
| 838 | a -= m; |
| 839 | } |
| 840 | #endif |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 841 | |
| 842 | /* Multiply two Montogmery form numbers mod the modulus (prime). |
| 843 | * (r = a * b mod m) |
| 844 | * |
| 845 | * r Result of multiplication. |
| 846 | * a First number to multiply in Montogmery form. |
| 847 | * b Second number to multiply in Montogmery form. |
| 848 | * m Modulus (prime). |
| 849 | * mp Montogmery mulitplier. |
| 850 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 851 | 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] | 852 | /*, const sp_digit* m, sp_digit mp*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 853 | { |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 854 | //const sp_digit* m = p256_mod; |
| 855 | //sp_digit mp = p256_mp_mod; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 856 | sp_256_mul_8(r, a, b); |
| 857 | sp_256_mont_reduce_8(r /*, m, mp*/); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* Square the Montgomery form number. (r = a * a mod m) |
| 861 | * |
| 862 | * r Result of squaring. |
| 863 | * a Number to square in Montogmery form. |
| 864 | * m Modulus (prime). |
| 865 | * mp Montogmery mulitplier. |
| 866 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 867 | 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] | 868 | /*, const sp_digit* m, sp_digit mp*/) |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 869 | { |
Denys Vlasenko | 389329e | 2021-10-05 13:39:33 +0200 | [diff] [blame] | 870 | //const sp_digit* m = p256_mod; |
| 871 | //sp_digit mp = p256_mp_mod; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 872 | sp_256_mont_mul_8(r, a, a /*, m, mp*/); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /* Invert the number, in Montgomery form, modulo the modulus (prime) of the |
| 876 | * P256 curve. (r = 1 / a mod m) |
| 877 | * |
| 878 | * r Inverse result. |
| 879 | * a Number to invert. |
| 880 | */ |
| 881 | #if 0 |
| 882 | /* Mod-2 for the P256 curve. */ |
| 883 | static const uint32_t p256_mod_2[8] = { |
| 884 | 0xfffffffd,0xffffffff,0xffffffff,0x00000000, |
| 885 | 0x00000000,0x00000000,0x00000001,0xffffffff, |
| 886 | }; |
| 887 | //Bit pattern: |
| 888 | //2 2 2 2 2 2 2 1...1 |
| 889 | //5 5 4 3 2 1 0 9...0 9...1 |
| 890 | //543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210 |
| 891 | //111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101 |
| 892 | #endif |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 893 | 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] | 894 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 895 | sp_digit t[2*8]; //can be just [8]? |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 896 | int i; |
| 897 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 898 | memcpy(t, a, sizeof(sp_digit) * 8); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 899 | for (i = 254; i >= 0; i--) { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 900 | sp_256_mont_sqr_8(t, t /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 901 | /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/ |
| 902 | if (i >= 224 || i == 192 || (i <= 95 && i != 1)) |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 903 | 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] | 904 | } |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 905 | memcpy(r, t, sizeof(sp_digit) * 8); |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 906 | } |
| 907 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 908 | /* Multiply a number by Montogmery normalizer mod modulus (prime). |
| 909 | * |
| 910 | * r The resulting Montgomery form number. |
| 911 | * a The number to convert. |
| 912 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 913 | 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] | 914 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 915 | int64_t t[8]; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 916 | int32_t o; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 917 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 918 | #define A(n) ((uint64_t)a[n]) |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 919 | /* 1 1 0 -1 -1 -1 -1 0 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 920 | 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] | 921 | /* 0 1 1 0 -1 -1 -1 -1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 922 | 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] | 923 | /* 0 0 1 1 0 -1 -1 -1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 924 | 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] | 925 | /* -1 -1 0 2 2 1 0 -1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 926 | 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] | 927 | /* 0 -1 -1 0 2 2 1 0 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 928 | 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] | 929 | /* 0 0 -1 -1 0 2 2 1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 930 | 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] | 931 | /* -1 -1 0 0 0 1 3 2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 932 | 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] | 933 | /* 1 0 -1 -1 -1 -1 0 3 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 934 | t[7] = 0 + A(0) - A(2) - A(3) - A(4) - A(5) + 3 * A(7); |
| 935 | #undef A |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 936 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 937 | t[1] += t[0] >> 32; t[0] &= 0xffffffff; |
| 938 | t[2] += t[1] >> 32; t[1] &= 0xffffffff; |
| 939 | t[3] += t[2] >> 32; t[2] &= 0xffffffff; |
| 940 | t[4] += t[3] >> 32; t[3] &= 0xffffffff; |
| 941 | t[5] += t[4] >> 32; t[4] &= 0xffffffff; |
| 942 | t[6] += t[5] >> 32; t[5] &= 0xffffffff; |
| 943 | t[7] += t[6] >> 32; t[6] &= 0xffffffff; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 944 | o = t[7] >> 32; //t[7] &= 0xffffffff; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 945 | t[0] += o; |
| 946 | t[3] -= o; |
| 947 | t[6] -= o; |
| 948 | t[7] += o; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 949 | r[0] = (sp_digit)t[0]; |
| 950 | t[1] += t[0] >> 32; |
| 951 | r[1] = (sp_digit)t[1]; |
| 952 | t[2] += t[1] >> 32; |
| 953 | r[2] = (sp_digit)t[2]; |
| 954 | t[3] += t[2] >> 32; |
| 955 | r[3] = (sp_digit)t[3]; |
| 956 | t[4] += t[3] >> 32; |
| 957 | r[4] = (sp_digit)t[4]; |
| 958 | t[5] += t[4] >> 32; |
| 959 | r[5] = (sp_digit)t[5]; |
| 960 | t[6] += t[5] >> 32; |
| 961 | r[6] = (sp_digit)t[6]; |
| 962 | // t[7] += t[6] >> 32; |
| 963 | // r[7] = (sp_digit)t[7]; |
| 964 | r[7] = (sp_digit)t[7] + (sp_digit)(t[6] >> 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 965 | } |
| 966 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 967 | /* Map the Montgomery form projective co-ordinate point to an affine point. |
| 968 | * |
| 969 | * r Resulting affine co-ordinate point. |
| 970 | * p Montgomery form projective co-ordinate point. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 971 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 972 | static void sp_256_map_8(sp_point* r, sp_point* p) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 973 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 974 | sp_digit t1[2*8]; |
| 975 | sp_digit t2[2*8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 976 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 977 | sp_256_mont_inv_8(t1, p->z); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 978 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 979 | sp_256_mont_sqr_8(t2, t1 /*, p256_mod, p256_mp_mod*/); |
| 980 | sp_256_mont_mul_8(t1, t2, t1 /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 981 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 982 | /* x /= z^2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 983 | sp_256_mont_mul_8(r->x, p->x, t2 /*, p256_mod, p256_mp_mod*/); |
| 984 | memset(r->x + 8, 0, sizeof(r->x) / 2); |
| 985 | sp_256_mont_reduce_8(r->x /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 986 | /* Reduce x to less than modulus */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 987 | if (sp_256_cmp_8(r->x, p256_mod) >= 0) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 988 | sp_256_sub_8_p256_mod(r->x, r->x /*, p256_mod*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 989 | sp_256_norm_8(r->x); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 990 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 991 | /* y /= z^3 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 992 | sp_256_mont_mul_8(r->y, p->y, t1 /*, p256_mod, p256_mp_mod*/); |
| 993 | memset(r->y + 8, 0, sizeof(r->y) / 2); |
| 994 | sp_256_mont_reduce_8(r->y /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 995 | /* Reduce y to less than modulus */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 996 | if (sp_256_cmp_8(r->y, p256_mod) >= 0) |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 997 | sp_256_sub_8_p256_mod(r->y, r->y /*, p256_mod*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 998 | sp_256_norm_8(r->y); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 999 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1000 | memset(r->z, 0, sizeof(r->z)); |
| 1001 | r->z[0] = 1; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* Double the Montgomery form projective point p. |
| 1005 | * |
| 1006 | * r Result of doubling point. |
| 1007 | * p Point to double. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1008 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1009 | 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] | 1010 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1011 | sp_digit t1[2*8]; |
| 1012 | sp_digit t2[2*8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1013 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1014 | /* Put point to double into result */ |
| 1015 | if (r != p) |
| 1016 | *r = *p; /* struct copy */ |
Denys Vlasenko | 4d3a5c1 | 2021-04-26 15:21:38 +0200 | [diff] [blame] | 1017 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1018 | if (r->infinity) |
Denys Vlasenko | e730505 | 2021-10-05 13:30:48 +0200 | [diff] [blame] | 1019 | return; |
| 1020 | |
Denys Vlasenko | 55578f2 | 2021-10-05 19:45:56 +0200 | [diff] [blame] | 1021 | if (SP_DEBUG) { |
| 1022 | /* unused part of t2, may result in spurios |
| 1023 | * differences in debug output. Clear it. |
| 1024 | */ |
| 1025 | memset(t2, 0, sizeof(t2)); |
| 1026 | } |
| 1027 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1028 | /* T1 = Z * Z */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1029 | 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] | 1030 | /* Z = Y * Z */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1031 | 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] | 1032 | /* Z = 2Z */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1033 | sp_256_mont_dbl_8(r->z, r->z /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1034 | /* T2 = X - T1 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1035 | sp_256_mont_sub_8(t2, r->x, t1 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1036 | /* T1 = X + T1 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1037 | sp_256_mont_add_8(t1, r->x, t1 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1038 | /* T2 = T1 * T2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1039 | 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] | 1040 | /* T1 = 3T2 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1041 | sp_256_mont_tpl_8(t1, t2 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1042 | /* Y = 2Y */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1043 | sp_256_mont_dbl_8(r->y, r->y /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1044 | /* Y = Y * Y */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1045 | 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] | 1046 | /* T2 = Y * Y */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1047 | 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] | 1048 | /* T2 = T2/2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1049 | sp_256_div2_8(t2, t2, p256_mod); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1050 | /* Y = Y * X */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1051 | 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] | 1052 | /* X = T1 * T1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1053 | 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] | 1054 | /* X = X - Y */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1055 | 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] | 1056 | /* X = X - Y */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1057 | 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] | 1058 | /* Y = Y - X */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1059 | 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] | 1060 | /* Y = Y * T1 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1061 | 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] | 1062 | /* Y = Y - T2 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1063 | sp_256_mont_sub_8(r->y, r->y, t2 /*, p256_mod*/); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1064 | dump_512("y2 %s\n", r->y); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /* Add two Montgomery form projective points. |
| 1068 | * |
| 1069 | * r Result of addition. |
| 1070 | * p Frist point to add. |
| 1071 | * q Second point to add. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1072 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1073 | 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] | 1074 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1075 | sp_digit t1[2*8]; |
| 1076 | sp_digit t2[2*8]; |
| 1077 | sp_digit t3[2*8]; |
| 1078 | sp_digit t4[2*8]; |
| 1079 | sp_digit t5[2*8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1080 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1081 | /* Ensure only the first point is the same as the result. */ |
| 1082 | if (q == r) { |
| 1083 | sp_point* a = p; |
| 1084 | p = q; |
| 1085 | q = a; |
| 1086 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1087 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1088 | /* Check double */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1089 | sp_256_sub_8(t1, p256_mod, q->y); |
| 1090 | sp_256_norm_8(t1); |
| 1091 | if (sp_256_cmp_equal_8(p->x, q->x) |
| 1092 | && sp_256_cmp_equal_8(p->z, q->z) |
| 1093 | && (sp_256_cmp_equal_8(p->y, q->y) || sp_256_cmp_equal_8(p->y, t1)) |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1094 | ) { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1095 | sp_256_proj_point_dbl_8(r, p); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1096 | } |
| 1097 | else { |
| 1098 | sp_point tp; |
| 1099 | sp_point *v; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1100 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1101 | v = r; |
| 1102 | if (p->infinity | q->infinity) { |
| 1103 | memset(&tp, 0, sizeof(tp)); |
| 1104 | v = &tp; |
| 1105 | } |
Denys Vlasenko | 772e187 | 2021-04-26 17:25:27 +0200 | [diff] [blame] | 1106 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1107 | *r = p->infinity ? *q : *p; /* struct copy */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1108 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1109 | /* U1 = X1*Z2^2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1110 | sp_256_mont_sqr_8(t1, q->z /*, p256_mod, p256_mp_mod*/); |
| 1111 | sp_256_mont_mul_8(t3, t1, q->z /*, p256_mod, p256_mp_mod*/); |
| 1112 | sp_256_mont_mul_8(t1, t1, v->x /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1113 | /* U2 = X2*Z1^2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1114 | sp_256_mont_sqr_8(t2, v->z /*, p256_mod, p256_mp_mod*/); |
| 1115 | sp_256_mont_mul_8(t4, t2, v->z /*, p256_mod, p256_mp_mod*/); |
| 1116 | sp_256_mont_mul_8(t2, t2, q->x /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1117 | /* S1 = Y1*Z2^3 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1118 | 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] | 1119 | /* S2 = Y2*Z1^3 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1120 | 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] | 1121 | /* H = U2 - U1 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1122 | sp_256_mont_sub_8(t2, t2, t1 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1123 | /* R = S2 - S1 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1124 | sp_256_mont_sub_8(t4, t4, t3 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1125 | /* Z3 = H*Z1*Z2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1126 | sp_256_mont_mul_8(v->z, v->z, q->z /*, p256_mod, p256_mp_mod*/); |
| 1127 | sp_256_mont_mul_8(v->z, v->z, t2 /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1128 | /* X3 = R^2 - H^3 - 2*U1*H^2 */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1129 | sp_256_mont_sqr_8(v->x, t4 /*, p256_mod, p256_mp_mod*/); |
| 1130 | sp_256_mont_sqr_8(t5, t2 /*, p256_mod, p256_mp_mod*/); |
| 1131 | sp_256_mont_mul_8(v->y, t1, t5 /*, p256_mod, p256_mp_mod*/); |
| 1132 | sp_256_mont_mul_8(t5, t5, t2 /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1133 | sp_256_mont_sub_8(v->x, v->x, t5 /*, p256_mod*/); |
| 1134 | sp_256_mont_dbl_8(t1, v->y /*, p256_mod*/); |
| 1135 | sp_256_mont_sub_8(v->x, v->x, t1 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1136 | /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */ |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1137 | 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] | 1138 | sp_256_mont_mul_8(v->y, v->y, t4 /*, p256_mod, p256_mp_mod*/); |
| 1139 | sp_256_mont_mul_8(t5, t5, t3 /*, p256_mod, p256_mp_mod*/); |
Denys Vlasenko | c784284 | 2021-10-06 01:09:37 +0200 | [diff] [blame^] | 1140 | sp_256_mont_sub_8(v->y, v->y, t5 /*, p256_mod*/); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1141 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* Multiply the point by the scalar and return the result. |
| 1145 | * If map is true then convert result to affine co-ordinates. |
| 1146 | * |
| 1147 | * r Resulting point. |
| 1148 | * g Point to multiply. |
| 1149 | * k Scalar to multiply by. |
Denys Vlasenko | 03ab2a9 | 2021-04-26 14:55:46 +0200 | [diff] [blame] | 1150 | * map Indicates whether to convert result to affine. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1151 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1152 | 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] | 1153 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1154 | enum { map = 1 }; /* we always convert result to affine coordinates */ |
| 1155 | sp_point t[3]; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1156 | sp_digit n = n; /* for compiler */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1157 | int c, y; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1158 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1159 | memset(t, 0, sizeof(t)); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1160 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1161 | /* t[0] = {0, 0, 1} * norm */ |
| 1162 | t[0].infinity = 1; |
| 1163 | /* t[1] = {g->x, g->y, g->z} * norm */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1164 | sp_256_mod_mul_norm_8(t[1].x, g->x); |
| 1165 | sp_256_mod_mul_norm_8(t[1].y, g->y); |
| 1166 | sp_256_mod_mul_norm_8(t[1].z, g->z); |
Denys 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 | /* For every bit, starting from most significant... */ |
| 1169 | k += 7; |
| 1170 | c = 256; |
| 1171 | for (;;) { |
| 1172 | if ((c & 0x1f) == 0) { |
| 1173 | if (c == 0) |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1174 | break; |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1175 | n = *k--; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1176 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1177 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1178 | y = (n >> 31); |
| 1179 | dbg("y:%d t[%d] = t[0]+t[1]\n", y, y^1); |
| 1180 | sp_256_proj_point_add_8(&t[y^1], &t[0], &t[1]); |
| 1181 | dump_512("t[0].x %s\n", t[0].x); |
| 1182 | dump_512("t[0].y %s\n", t[0].y); |
| 1183 | dump_512("t[0].z %s\n", t[0].z); |
| 1184 | dump_512("t[1].x %s\n", t[1].x); |
| 1185 | dump_512("t[1].y %s\n", t[1].y); |
| 1186 | dump_512("t[1].z %s\n", t[1].z); |
| 1187 | dbg("t[2] = t[%d]\n", y); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1188 | memcpy(&t[2], &t[y], sizeof(sp_point)); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1189 | dbg("t[2] *= 2\n"); |
| 1190 | sp_256_proj_point_dbl_8(&t[2], &t[2]); |
| 1191 | dump_512("t[2].x %s\n", t[2].x); |
| 1192 | dump_512("t[2].y %s\n", t[2].y); |
| 1193 | dump_512("t[2].z %s\n", t[2].z); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1194 | memcpy(&t[y], &t[2], sizeof(sp_point)); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1195 | |
| 1196 | n <<= 1; |
| 1197 | c--; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1198 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1199 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1200 | if (map) |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1201 | sp_256_map_8(r, &t[0]); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1202 | else |
| 1203 | memcpy(r, &t[0], sizeof(sp_point)); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1204 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1205 | memset(t, 0, sizeof(t)); //paranoia |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /* Multiply the base point of P256 by the scalar and return the result. |
| 1209 | * If map is true then convert result to affine co-ordinates. |
| 1210 | * |
| 1211 | * r Resulting point. |
| 1212 | * k Scalar to multiply by. |
Denys Vlasenko | 03ab2a9 | 2021-04-26 14:55:46 +0200 | [diff] [blame] | 1213 | * map Indicates whether to convert result to affine. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1214 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1215 | 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] | 1216 | { |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 1217 | /* Since this function is called only once, save space: |
| 1218 | * don't have "static const sp_point p256_base = {...}", |
| 1219 | * it would have more zeros than data. |
| 1220 | */ |
Denys Vlasenko | 48a18d1 | 2021-04-27 12:24:21 +0200 | [diff] [blame] | 1221 | static const uint8_t p256_base_bin[] = { |
| 1222 | /* x (big-endian) */ |
| 1223 | 0x6b,0x17,0xd1,0xf2,0xe1,0x2c,0x42,0x47,0xf8,0xbc,0xe6,0xe5,0x63,0xa4,0x40,0xf2,0x77,0x03,0x7d,0x81,0x2d,0xeb,0x33,0xa0,0xf4,0xa1,0x39,0x45,0xd8,0x98,0xc2,0x96, |
| 1224 | /* y */ |
| 1225 | 0x4f,0xe3,0x42,0xe2,0xfe,0x1a,0x7f,0x9b,0x8e,0xe7,0xeb,0x4a,0x7c,0x0f,0x9e,0x16,0x2b,0xce,0x33,0x57,0x6b,0x31,0x5e,0xce,0xcb,0xb6,0x40,0x68,0x37,0xbf,0x51,0xf5, |
Denys Vlasenko | 646e856 | 2021-04-27 13:09:44 +0200 | [diff] [blame] | 1226 | /* z will be set to 1, infinity flag to "false" */ |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 1227 | }; |
| 1228 | sp_point p256_base; |
| 1229 | |
Denys Vlasenko | 48a18d1 | 2021-04-27 12:24:21 +0200 | [diff] [blame] | 1230 | sp_256_point_from_bin2x32(&p256_base, p256_base_bin); |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 1231 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1232 | sp_256_ecc_mulmod_8(r, &p256_base, k /*, map*/); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | /* Multiply the point by the scalar and serialize the X ordinate. |
| 1236 | * The number is 0 padded to maximum size on output. |
| 1237 | * |
| 1238 | * priv Scalar to multiply the point by. |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1239 | * pub2x32 Point to multiply. |
| 1240 | * out32 Buffer to hold X ordinate. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1241 | */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1242 | 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] | 1243 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1244 | sp_point point[1]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1245 | |
| 1246 | #if FIXED_PEER_PUBKEY |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1247 | memset((void*)pub2x32, 0x55, 64); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1248 | #endif |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1249 | dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */ |
| 1250 | dump_hex(" %s\n", pub2x32 + 32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1251 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1252 | sp_256_point_from_bin2x32(point, pub2x32); |
Denys Vlasenko | 81d8af1 | 2021-10-05 17:31:33 +0200 | [diff] [blame] | 1253 | dump_512("point->x %s\n", point->x); |
| 1254 | dump_512("point->y %s\n", point->y); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1255 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1256 | sp_256_ecc_mulmod_8(point, point, priv); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1257 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1258 | sp_256_to_bin_8(point->x, out32); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 1259 | dump_hex("out32: %s\n", out32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1262 | /* Generates a random scalar in [1..order-1] range. */ |
| 1263 | static void sp_256_ecc_gen_k_8(sp_digit k[8]) |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1264 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1265 | /* Since 32-bit words are "dense", no need to use |
| 1266 | * sp_256_from_bin_8(k, buf) to convert random stream |
| 1267 | * to sp_digit array - just store random bits there directly. |
| 1268 | */ |
| 1269 | tls_get_random(k, 8 * sizeof(k[0])); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1270 | #if FIXED_SECRET |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1271 | memset(k, 0x77, 8 * sizeof(k[0])); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1272 | #endif |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1273 | |
| 1274 | // If scalar is too large, try again (pseudo-code) |
| 1275 | // if (k >= 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 - 1) // order of P256 |
| 1276 | // goto pick_another_random; |
| 1277 | // k++; // ensure non-zero |
| 1278 | /* Simpler alternative, at the cost of not choosing some valid |
| 1279 | * random values, and slightly non-uniform distribution */ |
| 1280 | if (k[0] == 0) |
| 1281 | k[0] = 1; |
| 1282 | if (k[7] >= 0xffffffff) |
| 1283 | k[7] = 0xfffffffe; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1284 | } |
| 1285 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1286 | /* Makes a random EC key pair. */ |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1287 | 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] | 1288 | { |
| 1289 | sp_point point[1]; |
| 1290 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1291 | sp_256_ecc_gen_k_8(privkey); |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 1292 | dump_256("privkey %s\n", privkey); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1293 | sp_256_ecc_mulmod_base_8(point, privkey); |
Denys Vlasenko | 137864f | 2021-10-05 13:47:42 +0200 | [diff] [blame] | 1294 | dump_512("point->x %s\n", point->x); |
| 1295 | dump_512("point->y %s\n", point->y); |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1296 | sp_256_to_bin_8(point->x, pubkey); |
| 1297 | sp_256_to_bin_8(point->y, pubkey + 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1298 | |
| 1299 | memset(point, 0, sizeof(point)); //paranoia |
| 1300 | } |
| 1301 | |
| 1302 | void FAST_FUNC curve_P256_compute_pubkey_and_premaster( |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1303 | uint8_t *pubkey2x32, uint8_t *premaster32, |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1304 | const uint8_t *peerkey2x32) |
| 1305 | { |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1306 | sp_digit privkey[8]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1307 | |
Denys Vlasenko | 3b411eb | 2021-10-05 20:00:50 +0200 | [diff] [blame] | 1308 | dump_hex("peerkey2x32: %s\n", peerkey2x32, 64); |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1309 | sp_ecc_make_key_256(privkey, pubkey2x32); |
| 1310 | dump_hex("pubkey: %s\n", pubkey2x32, 32); |
| 1311 | dump_hex(" %s\n", pubkey2x32 + 32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1312 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 1313 | /* Combine our privkey and peer's public key to generate premaster */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 1314 | sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32); |
| 1315 | dump_hex("premaster: %s\n", premaster32, 32); |
| 1316 | } |