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