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 | |
| 12 | #if SP_DEBUG |
| 13 | # define dbg(...) fprintf(stderr, __VA_ARGS__) |
| 14 | static void dump_hex(const char *fmt, const void *vp, int len) |
| 15 | { |
| 16 | char hexbuf[32 * 1024 + 4]; |
| 17 | const uint8_t *p = vp; |
| 18 | |
| 19 | bin2hex(hexbuf, (void*)p, len)[0] = '\0'; |
| 20 | dbg(fmt, hexbuf); |
| 21 | } |
| 22 | #else |
| 23 | # define dbg(...) ((void)0) |
| 24 | # define dump_hex(...) ((void)0) |
| 25 | #endif |
| 26 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 27 | typedef int32_t sp_digit; |
| 28 | |
| 29 | /* The code below is taken from parts of |
| 30 | * wolfssl-3.15.3/wolfcrypt/src/sp_c32.c |
| 31 | * and heavily modified. |
| 32 | * Header comment is kept intact: |
| 33 | */ |
| 34 | |
| 35 | /* sp.c |
| 36 | * |
| 37 | * Copyright (C) 2006-2018 wolfSSL Inc. |
| 38 | * |
| 39 | * This file is part of wolfSSL. |
| 40 | * |
| 41 | * wolfSSL is free software; you can redistribute it and/or modify |
| 42 | * it under the terms of the GNU General Public License as published by |
| 43 | * the Free Software Foundation; either version 2 of the License, or |
| 44 | * (at your option) any later version. |
| 45 | * |
| 46 | * wolfSSL is distributed in the hope that it will be useful, |
| 47 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 48 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 49 | * GNU General Public License for more details. |
| 50 | * |
| 51 | * You should have received a copy of the GNU General Public License |
| 52 | * along with this program; if not, write to the Free Software |
| 53 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 54 | */ |
| 55 | |
| 56 | /* Implementation by Sean Parkinson. */ |
| 57 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 58 | typedef struct sp_point { |
| 59 | sp_digit x[2 * 10]; |
| 60 | sp_digit y[2 * 10]; |
| 61 | sp_digit z[2 * 10]; |
| 62 | int infinity; |
| 63 | } sp_point; |
| 64 | |
| 65 | /* The modulus (prime) of the curve P256. */ |
| 66 | static const sp_digit p256_mod[10] = { |
| 67 | 0x3ffffff,0x3ffffff,0x3ffffff,0x003ffff,0x0000000, |
| 68 | 0x0000000,0x0000000,0x0000400,0x3ff0000,0x03fffff, |
| 69 | }; |
| 70 | |
| 71 | #define p256_mp_mod ((sp_digit)0x000001) |
| 72 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 73 | /* Write r as big endian to byte aray. |
| 74 | * Fixed length number of bytes written: 32 |
| 75 | * |
| 76 | * r A single precision integer. |
| 77 | * a Byte array. |
| 78 | */ |
| 79 | static void sp_256_to_bin(sp_digit* r, uint8_t* a) |
| 80 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 81 | int i, j, s = 0, b; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 82 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 83 | for (i = 0; i < 9; i++) { |
| 84 | r[i+1] += r[i] >> 26; |
| 85 | r[i] &= 0x3ffffff; |
| 86 | } |
| 87 | j = 256 / 8 - 1; |
| 88 | a[j] = 0; |
| 89 | for (i = 0; i < 10 && j >= 0; i++) { |
| 90 | b = 0; |
| 91 | a[j--] |= r[i] << s; b += 8 - s; |
| 92 | if (j < 0) |
| 93 | break; |
| 94 | while (b < 26) { |
| 95 | a[j--] = r[i] >> b; b += 8; |
| 96 | if (j < 0) |
| 97 | break; |
| 98 | } |
| 99 | s = 8 - (b - 26); |
| 100 | if (j >= 0) |
| 101 | a[j] = 0; |
| 102 | if (s != 0) |
| 103 | j++; |
| 104 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Read big endian unsigned byte aray into r. |
| 108 | * |
| 109 | * r A single precision integer. |
| 110 | * a Byte array. |
| 111 | * n Number of bytes in array to read. |
| 112 | */ |
| 113 | static void sp_256_from_bin(sp_digit* r, int max, const uint8_t* a, int n) |
| 114 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 115 | int i, j = 0, s = 0; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 116 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 117 | r[0] = 0; |
| 118 | for (i = n-1; i >= 0; i--) { |
| 119 | r[j] |= ((sp_digit)a[i]) << s; |
| 120 | if (s >= 18) { |
| 121 | r[j] &= 0x3ffffff; |
| 122 | s = 26 - s; |
| 123 | if (j + 1 >= max) |
| 124 | break; |
| 125 | r[++j] = a[i] >> s; |
| 126 | s = 8 - s; |
| 127 | } |
| 128 | else |
| 129 | s += 8; |
| 130 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 131 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 132 | for (j++; j < max; j++) |
| 133 | r[j] = 0; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* Convert a point of big-endian 32-byte x,y pair to type sp_point. */ |
| 137 | static void sp_256_point_from_bin2x32(sp_point* p, const uint8_t *bin2x32) |
| 138 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 139 | memset(p, 0, sizeof(*p)); |
| 140 | /*p->infinity = 0;*/ |
| 141 | sp_256_from_bin(p->x, 2 * 10, bin2x32, 32); |
| 142 | sp_256_from_bin(p->y, 2 * 10, bin2x32 + 32, 32); |
| 143 | //static const uint8_t one[1] = { 1 }; |
| 144 | //sp_256_from_bin(p->z, 2 * 10, one, 1); |
| 145 | p->z[0] = 1; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Denys Vlasenko | b3b1713 | 2021-04-26 16:53:53 +0200 | [diff] [blame] | 148 | /* Compare a with b. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 149 | * |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 150 | * return -ve, 0 or +ve if a is less than, equal to or greater than b |
| 151 | * respectively. |
| 152 | */ |
| 153 | static sp_digit sp_256_cmp_10(const sp_digit* a, const sp_digit* b) |
| 154 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 155 | sp_digit r; |
| 156 | int i; |
| 157 | for (i = 9; i >= 0; i--) { |
| 158 | r = a[i] - b[i]; |
| 159 | if (r != 0) |
| 160 | break; |
| 161 | } |
| 162 | return r; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Compare two numbers to determine if they are equal. |
| 166 | * |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 167 | * return 1 when equal and 0 otherwise. |
| 168 | */ |
| 169 | static int sp_256_cmp_equal_10(const sp_digit* a, const sp_digit* b) |
| 170 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 171 | return sp_256_cmp_10(a, b) == 0; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 174 | /* Normalize the values in each word to 26 bits. */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 175 | static void sp_256_norm_10(sp_digit* a) |
| 176 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 177 | int i; |
| 178 | for (i = 0; i < 9; i++) { |
| 179 | a[i+1] += a[i] >> 26; |
| 180 | a[i] &= 0x3ffffff; |
| 181 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 184 | /* Add b to a into r. (r = a + b) */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 185 | static void sp_256_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b) |
| 186 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 187 | int i; |
| 188 | for (i = 0; i < 10; i++) |
| 189 | r[i] = a[i] + b[i]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Denys Vlasenko | 9a40be4 | 2021-04-26 21:58:04 +0200 | [diff] [blame] | 192 | /* Sub b from a into r. (r = a - b) */ |
| 193 | static void sp_256_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 194 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 195 | int i; |
| 196 | for (i = 0; i < 10; i++) |
Denys Vlasenko | 9a40be4 | 2021-04-26 21:58:04 +0200 | [diff] [blame] | 197 | r[i] = a[i] - b[i]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 200 | /* Shift number left one bit. Bottom bit is lost. */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 201 | static void sp_256_rshift1_10(sp_digit* r, sp_digit* a) |
| 202 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 203 | int i; |
| 204 | for (i = 0; i < 9; i++) |
| 205 | r[i] = ((a[i] >> 1) | (a[i + 1] << 25)) & 0x3ffffff; |
| 206 | r[9] = a[9] >> 1; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 209 | /* Mul a by scalar b and add into r. (r += a * b) */ |
| 210 | static void sp_256_mul_add_10(sp_digit* r, const sp_digit* a, sp_digit b) |
| 211 | { |
| 212 | int64_t tb = b; |
| 213 | int64_t t = 0; |
| 214 | int i; |
| 215 | |
| 216 | for (i = 0; i < 10; i++) { |
| 217 | t += (tb * a[i]) + r[i]; |
| 218 | r[i] = t & 0x3ffffff; |
| 219 | t >>= 26; |
| 220 | } |
| 221 | r[10] += t; |
| 222 | } |
| 223 | |
| 224 | /* Multiply a and b into r. (r = a * b) */ |
| 225 | static void sp_256_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b) |
| 226 | { |
| 227 | int i, j, k; |
| 228 | int64_t c; |
| 229 | |
| 230 | c = ((int64_t)a[9]) * b[9]; |
| 231 | r[19] = (sp_digit)(c >> 26); |
| 232 | c = (c & 0x3ffffff) << 26; |
| 233 | for (k = 17; k >= 0; k--) { |
| 234 | for (i = 9; i >= 0; i--) { |
| 235 | j = k - i; |
| 236 | if (j >= 10) |
| 237 | break; |
| 238 | if (j < 0) |
| 239 | continue; |
| 240 | c += ((int64_t)a[i]) * b[j]; |
| 241 | } |
| 242 | r[k + 2] += c >> 52; |
| 243 | r[k + 1] = (c >> 26) & 0x3ffffff; |
| 244 | c = (c & 0x3ffffff) << 26; |
| 245 | } |
| 246 | r[0] = (sp_digit)(c >> 26); |
| 247 | } |
| 248 | |
| 249 | /* Square a and put result in r. (r = a * a) */ |
| 250 | static void sp_256_sqr_10(sp_digit* r, const sp_digit* a) |
| 251 | { |
| 252 | int i, j, k; |
| 253 | int64_t c; |
| 254 | |
| 255 | c = ((int64_t)a[9]) * a[9]; |
| 256 | r[19] = (sp_digit)(c >> 26); |
| 257 | c = (c & 0x3ffffff) << 26; |
| 258 | for (k = 17; k >= 0; k--) { |
| 259 | for (i = 9; i >= 0; i--) { |
| 260 | j = k - i; |
| 261 | if (j >= 10 || i <= j) |
| 262 | break; |
| 263 | if (j < 0) |
| 264 | continue; |
| 265 | c += ((int64_t)a[i]) * a[j] * 2; |
| 266 | } |
| 267 | if (i == j) |
| 268 | c += ((int64_t)a[i]) * a[i]; |
| 269 | r[k + 2] += c >> 52; |
| 270 | r[k + 1] = (c >> 26) & 0x3ffffff; |
| 271 | c = (c & 0x3ffffff) << 26; |
| 272 | } |
| 273 | r[0] = (sp_digit)(c >> 26); |
| 274 | } |
| 275 | |
| 276 | /* Divide the number by 2 mod the modulus (prime). (r = a / 2 % m) */ |
| 277 | static void sp_256_div2_10(sp_digit* r, const sp_digit* a, const sp_digit* m) |
| 278 | { |
| 279 | if (a[0] & 1) |
| 280 | sp_256_add_10(r, a, m); |
| 281 | sp_256_norm_10(r); |
| 282 | sp_256_rshift1_10(r, r); |
| 283 | } |
| 284 | |
| 285 | /* Add two Montgomery form numbers (r = a + b % m) */ |
| 286 | static void sp_256_mont_add_10(sp_digit* r, const sp_digit* a, const sp_digit* b, |
| 287 | const sp_digit* m) |
| 288 | { |
| 289 | sp_256_add_10(r, a, b); |
| 290 | sp_256_norm_10(r); |
| 291 | if ((r[9] >> 22) > 0) |
| 292 | sp_256_sub_10(r, r, m); |
| 293 | sp_256_norm_10(r); |
| 294 | } |
| 295 | |
| 296 | /* Subtract two Montgomery form numbers (r = a - b % m) */ |
| 297 | static void sp_256_mont_sub_10(sp_digit* r, const sp_digit* a, const sp_digit* b, |
| 298 | const sp_digit* m) |
| 299 | { |
| 300 | sp_256_sub_10(r, a, b); |
| 301 | if (r[9] >> 22) |
| 302 | sp_256_add_10(r, r, m); |
| 303 | sp_256_norm_10(r); |
| 304 | } |
| 305 | |
| 306 | /* Double a Montgomery form number (r = a + a % m) */ |
| 307 | static void sp_256_mont_dbl_10(sp_digit* r, const sp_digit* a, const sp_digit* m) |
| 308 | { |
| 309 | sp_256_add_10(r, a, a); |
| 310 | sp_256_norm_10(r); |
| 311 | if ((r[9] >> 22) > 0) |
| 312 | sp_256_sub_10(r, r, m); |
| 313 | sp_256_norm_10(r); |
| 314 | } |
| 315 | |
| 316 | /* Triple a Montgomery form number (r = a + a + a % m) */ |
| 317 | static void sp_256_mont_tpl_10(sp_digit* r, const sp_digit* a, const sp_digit* m) |
| 318 | { |
| 319 | sp_256_add_10(r, a, a); |
| 320 | sp_256_norm_10(r); |
| 321 | if ((r[9] >> 22) > 0) |
| 322 | sp_256_sub_10(r, r, m); |
| 323 | sp_256_norm_10(r); |
| 324 | sp_256_add_10(r, r, a); |
| 325 | sp_256_norm_10(r); |
| 326 | if ((r[9] >> 22) > 0) |
| 327 | sp_256_sub_10(r, r, m); |
| 328 | sp_256_norm_10(r); |
| 329 | } |
| 330 | |
| 331 | /* Shift the result in the high 256 bits down to the bottom. */ |
| 332 | static void sp_256_mont_shift_10(sp_digit* r, const sp_digit* a) |
| 333 | { |
| 334 | int i; |
| 335 | sp_digit n, s; |
| 336 | |
| 337 | s = a[10]; |
| 338 | n = a[9] >> 22; |
| 339 | for (i = 0; i < 9; i++) { |
| 340 | n += (s & 0x3ffffff) << 4; |
| 341 | r[i] = n & 0x3ffffff; |
| 342 | n >>= 26; |
| 343 | s = a[11 + i] + (s >> 26); |
| 344 | } |
| 345 | n += s << 4; |
| 346 | r[9] = n; |
| 347 | memset(&r[10], 0, sizeof(*r) * 10); |
| 348 | } |
| 349 | |
| 350 | /* Reduce the number back to 256 bits using Montgomery reduction. |
| 351 | * |
| 352 | * a A single precision number to reduce in place. |
| 353 | * m The single precision number representing the modulus. |
| 354 | * mp The digit representing the negative inverse of m mod 2^n. |
| 355 | */ |
| 356 | static void sp_256_mont_reduce_10(sp_digit* a, const sp_digit* m, sp_digit mp) |
| 357 | { |
| 358 | int i; |
| 359 | sp_digit mu; |
| 360 | |
| 361 | if (mp != 1) { |
| 362 | for (i = 0; i < 9; i++) { |
| 363 | mu = (a[i] * mp) & 0x3ffffff; |
| 364 | sp_256_mul_add_10(a+i, m, mu); |
| 365 | a[i+1] += a[i] >> 26; |
| 366 | } |
| 367 | mu = (a[i] * mp) & 0x3fffffl; |
| 368 | sp_256_mul_add_10(a+i, m, mu); |
| 369 | a[i+1] += a[i] >> 26; |
| 370 | a[i] &= 0x3ffffff; |
| 371 | } |
| 372 | else { |
| 373 | for (i = 0; i < 9; i++) { |
| 374 | mu = a[i] & 0x3ffffff; |
| 375 | sp_256_mul_add_10(a+i, p256_mod, mu); |
| 376 | a[i+1] += a[i] >> 26; |
| 377 | } |
| 378 | mu = a[i] & 0x3fffffl; |
| 379 | sp_256_mul_add_10(a+i, p256_mod, mu); |
| 380 | a[i+1] += a[i] >> 26; |
| 381 | a[i] &= 0x3ffffff; |
| 382 | } |
| 383 | |
| 384 | sp_256_mont_shift_10(a, a); |
| 385 | if ((a[9] >> 22) > 0) |
| 386 | sp_256_sub_10(a, a, m); |
| 387 | sp_256_norm_10(a); |
| 388 | } |
| 389 | |
| 390 | /* Multiply two Montogmery form numbers mod the modulus (prime). |
| 391 | * (r = a * b mod m) |
| 392 | * |
| 393 | * r Result of multiplication. |
| 394 | * a First number to multiply in Montogmery form. |
| 395 | * b Second number to multiply in Montogmery form. |
| 396 | * m Modulus (prime). |
| 397 | * mp Montogmery mulitplier. |
| 398 | */ |
| 399 | static void sp_256_mont_mul_10(sp_digit* r, const sp_digit* a, const sp_digit* b, |
| 400 | const sp_digit* m, sp_digit mp) |
| 401 | { |
| 402 | sp_256_mul_10(r, a, b); |
| 403 | sp_256_mont_reduce_10(r, m, mp); |
| 404 | } |
| 405 | |
| 406 | /* Square the Montgomery form number. (r = a * a mod m) |
| 407 | * |
| 408 | * r Result of squaring. |
| 409 | * a Number to square in Montogmery form. |
| 410 | * m Modulus (prime). |
| 411 | * mp Montogmery mulitplier. |
| 412 | */ |
| 413 | static void sp_256_mont_sqr_10(sp_digit* r, const sp_digit* a, const sp_digit* m, |
| 414 | sp_digit mp) |
| 415 | { |
| 416 | sp_256_sqr_10(r, a); |
| 417 | sp_256_mont_reduce_10(r, m, mp); |
| 418 | } |
| 419 | |
| 420 | /* Invert the number, in Montgomery form, modulo the modulus (prime) of the |
| 421 | * P256 curve. (r = 1 / a mod m) |
| 422 | * |
| 423 | * r Inverse result. |
| 424 | * a Number to invert. |
| 425 | */ |
| 426 | #if 0 |
| 427 | /* Mod-2 for the P256 curve. */ |
| 428 | static const uint32_t p256_mod_2[8] = { |
| 429 | 0xfffffffd,0xffffffff,0xffffffff,0x00000000, |
| 430 | 0x00000000,0x00000000,0x00000001,0xffffffff, |
| 431 | }; |
| 432 | //Bit pattern: |
| 433 | //2 2 2 2 2 2 2 1...1 |
| 434 | //5 5 4 3 2 1 0 9...0 9...1 |
| 435 | //543210987654321098765432109876543210987654321098765432109876543210...09876543210...09876543210 |
| 436 | //111111111111111111111111111111110000000000000000000000000000000100...00000111111...11111111101 |
| 437 | #endif |
| 438 | static void sp_256_mont_inv_10(sp_digit* r, sp_digit* a) |
| 439 | { |
| 440 | sp_digit t[2*10]; //can be just [10]? |
| 441 | int i; |
| 442 | |
| 443 | memcpy(t, a, sizeof(sp_digit) * 10); |
| 444 | for (i = 254; i >= 0; i--) { |
| 445 | sp_256_mont_sqr_10(t, t, p256_mod, p256_mp_mod); |
| 446 | /*if (p256_mod_2[i / 32] & ((sp_digit)1 << (i % 32)))*/ |
| 447 | if (i >= 224 || i == 192 || (i <= 95 && i != 1)) |
| 448 | sp_256_mont_mul_10(t, t, a, p256_mod, p256_mp_mod); |
| 449 | } |
| 450 | memcpy(r, t, sizeof(sp_digit) * 10); |
| 451 | } |
| 452 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 453 | /* Multiply a number by Montogmery normalizer mod modulus (prime). |
| 454 | * |
| 455 | * r The resulting Montgomery form number. |
| 456 | * a The number to convert. |
| 457 | */ |
| 458 | static void sp_256_mod_mul_norm_10(sp_digit* r, const sp_digit* a) |
| 459 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 460 | int64_t t[8]; |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 461 | int64_t o; |
Denys Vlasenko | 646e856 | 2021-04-27 13:09:44 +0200 | [diff] [blame] | 462 | uint32_t a32; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 463 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 464 | /* 1 1 0 -1 -1 -1 -1 0 */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 465 | /* 0 1 1 0 -1 -1 -1 -1 */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 466 | /* 0 0 1 1 0 -1 -1 -1 */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 467 | /* -1 -1 0 2 2 1 0 -1 */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 468 | /* 0 -1 -1 0 2 2 1 0 */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 469 | /* 0 0 -1 -1 0 2 2 1 */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 470 | /* -1 -1 0 0 0 1 3 2 */ |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 471 | /* 1 0 -1 -1 -1 -1 0 3 */ |
Denys Vlasenko | 646e856 | 2021-04-27 13:09:44 +0200 | [diff] [blame] | 472 | // t[] should be calculated from "a" (converted from 26-bit to 32-bit vector a32[8]) |
| 473 | // according to the above matrix: |
| 474 | //t[0] = 0 + a32[0] + a32[1] - a32[3] - a32[4] - a32[5] - a32[6] ; |
| 475 | //t[1] = 0 + a32[1] + a32[2] - a32[4] - a32[5] - a32[6] - a32[7] ; |
| 476 | //t[2] = 0 + a32[2] + a32[3] - a32[5] - a32[6] - a32[7] ; |
| 477 | //t[3] = 0 - a32[0] - a32[1] + 2*a32[3] + 2*a32[4] + a32[5] - a32[7] ; |
| 478 | //t[4] = 0 - a32[1] - a32[2] + 2*a32[4] + 2*a32[5] + a32[6] ; |
| 479 | //t[5] = 0 - a32[2] - a32[3] + 2*a32[5] + 2*a32[6] + a32[7] ; |
| 480 | //t[6] = 0 - a32[0] - a32[1] + a32[5] + 3*a32[6] + 2*a32[7]; |
| 481 | //t[7] = 0 + a32[0] - a32[2] - a32[3] - a32[4] - a32[5] + 3*a32[7]; |
| 482 | // We can do it "piecemeal" after each a32[i] is known, no need to store entire a32[8] vector: |
| 483 | |
| 484 | #define A32 (int64_t)a32 |
| 485 | a32 = a[0] | (a[1] << 26); |
| 486 | t[0] = 0 + A32; |
| 487 | t[3] = 0 - A32; |
| 488 | t[6] = 0 - A32; |
| 489 | t[7] = 0 + A32; |
| 490 | |
| 491 | a32 = (a[1] >> 6) | (a[2] << 20); |
| 492 | t[0] += A32 ; |
| 493 | t[1] = 0 + A32; |
| 494 | t[3] -= A32 ; |
| 495 | t[4] = 0 - A32; |
| 496 | t[6] -= A32 ; |
| 497 | |
| 498 | a32 = (a[2] >> 12) | (a[3] << 14); |
| 499 | t[1] += A32 ; |
| 500 | t[2] = 0 + A32; |
| 501 | t[4] -= A32 ; |
| 502 | t[5] = 0 - A32; |
| 503 | t[7] -= A32 ; |
| 504 | |
| 505 | a32 = (a[3] >> 18) | (a[4] << 8); |
| 506 | t[0] -= A32 ; |
| 507 | t[2] += A32 ; |
| 508 | t[3] += 2*A32; |
| 509 | t[5] -= A32 ; |
| 510 | t[7] -= A32 ; |
| 511 | |
| 512 | a32 = (a[4] >> 24) | (a[5] << 2) | (a[6] << 28); |
| 513 | t[0] -= A32 ; |
| 514 | t[1] -= A32 ; |
| 515 | t[3] += 2*A32; |
| 516 | t[4] += 2*A32; |
| 517 | t[7] -= A32 ; |
| 518 | |
| 519 | a32 = (a[6] >> 4) | (a[7] << 22); |
| 520 | t[0] -= A32 ; |
| 521 | t[1] -= A32 ; |
| 522 | t[2] -= A32 ; |
| 523 | t[3] += A32 ; |
| 524 | t[4] += 2*A32; |
| 525 | t[5] += 2*A32; |
| 526 | t[6] += A32 ; |
| 527 | t[7] -= A32 ; |
| 528 | |
| 529 | a32 = (a[7] >> 10) | (a[8] << 16); |
| 530 | t[0] -= A32 ; |
| 531 | t[1] -= A32 ; |
| 532 | t[2] -= A32 ; |
| 533 | t[4] += A32 ; |
| 534 | t[5] += 2*A32; |
| 535 | t[6] += 3*A32; |
| 536 | |
| 537 | a32 = (a[8] >> 16) | (a[9] << 10); |
| 538 | t[1] -= A32 ; |
| 539 | t[2] -= A32 ; |
| 540 | t[3] -= A32 ; |
| 541 | t[5] += A32 ; |
| 542 | t[6] += 2*A32; |
| 543 | t[7] += 3*A32; |
| 544 | #undef A32 |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 545 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 546 | t[1] += t[0] >> 32; t[0] &= 0xffffffff; |
| 547 | t[2] += t[1] >> 32; t[1] &= 0xffffffff; |
| 548 | t[3] += t[2] >> 32; t[2] &= 0xffffffff; |
| 549 | t[4] += t[3] >> 32; t[3] &= 0xffffffff; |
| 550 | t[5] += t[4] >> 32; t[4] &= 0xffffffff; |
| 551 | t[6] += t[5] >> 32; t[5] &= 0xffffffff; |
| 552 | t[7] += t[6] >> 32; t[6] &= 0xffffffff; |
| 553 | o = t[7] >> 32; t[7] &= 0xffffffff; |
| 554 | t[0] += o; |
| 555 | t[3] -= o; |
| 556 | t[6] -= o; |
| 557 | t[7] += o; |
Denys Vlasenko | 840ae69 | 2021-04-27 13:31:26 +0200 | [diff] [blame] | 558 | t[1] += t[0] >> 32; //t[0] &= 0xffffffff; |
| 559 | t[2] += t[1] >> 32; //t[1] &= 0xffffffff; |
| 560 | t[3] += t[2] >> 32; //t[2] &= 0xffffffff; |
| 561 | t[4] += t[3] >> 32; //t[3] &= 0xffffffff; |
| 562 | t[5] += t[4] >> 32; //t[4] &= 0xffffffff; |
| 563 | t[6] += t[5] >> 32; //t[5] &= 0xffffffff; |
| 564 | t[7] += t[6] >> 32; //t[6] &= 0xffffffff; - (uint32_t)t[i] casts below accomplish masking |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 565 | |
Denys Vlasenko | 840ae69 | 2021-04-27 13:31:26 +0200 | [diff] [blame] | 566 | r[0] = 0x3ffffff & ((sp_digit)((uint32_t)t[0])); |
| 567 | r[1] = 0x3ffffff & ((sp_digit)((uint32_t)t[0] >> 26) | ((sp_digit)t[1] << 6)); |
| 568 | r[2] = 0x3ffffff & ((sp_digit)((uint32_t)t[1] >> 20) | ((sp_digit)t[2] << 12)); |
| 569 | r[3] = 0x3ffffff & ((sp_digit)((uint32_t)t[2] >> 14) | ((sp_digit)t[3] << 18)); |
| 570 | r[4] = 0x3ffffff & ((sp_digit)((uint32_t)t[3] >> 8) | ((sp_digit)t[4] << 24)); |
| 571 | r[5] = 0x3ffffff & ((sp_digit)((uint32_t)t[4] >> 2)); |
| 572 | r[6] = 0x3ffffff & ((sp_digit)((uint32_t)t[4] >> 28) | ((sp_digit)t[5] << 4)); |
| 573 | r[7] = 0x3ffffff & ((sp_digit)((uint32_t)t[5] >> 22) | ((sp_digit)t[6] << 10)); |
| 574 | r[8] = 0x3ffffff & ((sp_digit)((uint32_t)t[6] >> 16) | ((sp_digit)t[7] << 16)); |
| 575 | r[9] = ((sp_digit)((uint32_t)t[7] >> 10)); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 578 | /* Map the Montgomery form projective co-ordinate point to an affine point. |
| 579 | * |
| 580 | * r Resulting affine co-ordinate point. |
| 581 | * p Montgomery form projective co-ordinate point. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 582 | */ |
Denys Vlasenko | 6381f3d | 2021-04-26 17:41:43 +0200 | [diff] [blame] | 583 | static void sp_256_map_10(sp_point* r, sp_point* p) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 584 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 585 | sp_digit t1[2*10]; |
| 586 | sp_digit t2[2*10]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 587 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 588 | sp_256_mont_inv_10(t1, p->z); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 589 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 590 | sp_256_mont_sqr_10(t2, t1, p256_mod, p256_mp_mod); |
| 591 | sp_256_mont_mul_10(t1, t2, t1, p256_mod, p256_mp_mod); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 592 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 593 | /* x /= z^2 */ |
| 594 | sp_256_mont_mul_10(r->x, p->x, t2, p256_mod, p256_mp_mod); |
| 595 | memset(r->x + 10, 0, sizeof(r->x) / 2); |
| 596 | sp_256_mont_reduce_10(r->x, p256_mod, p256_mp_mod); |
| 597 | /* Reduce x to less than modulus */ |
Denys Vlasenko | 9a40be4 | 2021-04-26 21:58:04 +0200 | [diff] [blame] | 598 | if (sp_256_cmp_10(r->x, p256_mod) >= 0) |
| 599 | sp_256_sub_10(r->x, r->x, p256_mod); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 600 | sp_256_norm_10(r->x); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 601 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 602 | /* y /= z^3 */ |
| 603 | sp_256_mont_mul_10(r->y, p->y, t1, p256_mod, p256_mp_mod); |
| 604 | memset(r->y + 10, 0, sizeof(r->y) / 2); |
| 605 | sp_256_mont_reduce_10(r->y, p256_mod, p256_mp_mod); |
| 606 | /* Reduce y to less than modulus */ |
Denys Vlasenko | 9a40be4 | 2021-04-26 21:58:04 +0200 | [diff] [blame] | 607 | if (sp_256_cmp_10(r->y, p256_mod) >= 0) |
| 608 | sp_256_sub_10(r->y, r->y, p256_mod); |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 609 | sp_256_norm_10(r->y); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 610 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 611 | memset(r->z, 0, sizeof(r->z)); |
| 612 | r->z[0] = 1; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* Double the Montgomery form projective point p. |
| 616 | * |
| 617 | * r Result of doubling point. |
| 618 | * p Point to double. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 619 | */ |
Denys Vlasenko | 6381f3d | 2021-04-26 17:41:43 +0200 | [diff] [blame] | 620 | static void sp_256_proj_point_dbl_10(sp_point* r, sp_point* p) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 621 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 622 | sp_point tp; |
| 623 | sp_digit t1[2*10]; |
| 624 | sp_digit t2[2*10]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 625 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 626 | /* Put point to double into result */ |
| 627 | if (r != p) |
| 628 | *r = *p; /* struct copy */ |
Denys Vlasenko | 4d3a5c1 | 2021-04-26 15:21:38 +0200 | [diff] [blame] | 629 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 630 | if (r->infinity) { |
| 631 | /* If infinity, don't double (work on dummy value) */ |
| 632 | r = &tp; |
| 633 | } |
| 634 | /* T1 = Z * Z */ |
| 635 | sp_256_mont_sqr_10(t1, r->z, p256_mod, p256_mp_mod); |
| 636 | /* Z = Y * Z */ |
| 637 | sp_256_mont_mul_10(r->z, r->y, r->z, p256_mod, p256_mp_mod); |
| 638 | /* Z = 2Z */ |
| 639 | sp_256_mont_dbl_10(r->z, r->z, p256_mod); |
| 640 | /* T2 = X - T1 */ |
| 641 | sp_256_mont_sub_10(t2, r->x, t1, p256_mod); |
| 642 | /* T1 = X + T1 */ |
| 643 | sp_256_mont_add_10(t1, r->x, t1, p256_mod); |
| 644 | /* T2 = T1 * T2 */ |
| 645 | sp_256_mont_mul_10(t2, t1, t2, p256_mod, p256_mp_mod); |
| 646 | /* T1 = 3T2 */ |
| 647 | sp_256_mont_tpl_10(t1, t2, p256_mod); |
| 648 | /* Y = 2Y */ |
| 649 | sp_256_mont_dbl_10(r->y, r->y, p256_mod); |
| 650 | /* Y = Y * Y */ |
| 651 | sp_256_mont_sqr_10(r->y, r->y, p256_mod, p256_mp_mod); |
| 652 | /* T2 = Y * Y */ |
| 653 | sp_256_mont_sqr_10(t2, r->y, p256_mod, p256_mp_mod); |
| 654 | /* T2 = T2/2 */ |
| 655 | sp_256_div2_10(t2, t2, p256_mod); |
| 656 | /* Y = Y * X */ |
| 657 | sp_256_mont_mul_10(r->y, r->y, r->x, p256_mod, p256_mp_mod); |
| 658 | /* X = T1 * T1 */ |
| 659 | sp_256_mont_mul_10(r->x, t1, t1, p256_mod, p256_mp_mod); |
| 660 | /* X = X - Y */ |
| 661 | sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod); |
| 662 | /* X = X - Y */ |
| 663 | sp_256_mont_sub_10(r->x, r->x, r->y, p256_mod); |
| 664 | /* Y = Y - X */ |
| 665 | sp_256_mont_sub_10(r->y, r->y, r->x, p256_mod); |
| 666 | /* Y = Y * T1 */ |
| 667 | sp_256_mont_mul_10(r->y, r->y, t1, p256_mod, p256_mp_mod); |
| 668 | /* Y = Y - T2 */ |
| 669 | sp_256_mont_sub_10(r->y, r->y, t2, p256_mod); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* Add two Montgomery form projective points. |
| 673 | * |
| 674 | * r Result of addition. |
| 675 | * p Frist point to add. |
| 676 | * q Second point to add. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 677 | */ |
Denys Vlasenko | 6381f3d | 2021-04-26 17:41:43 +0200 | [diff] [blame] | 678 | static void sp_256_proj_point_add_10(sp_point* r, sp_point* p, sp_point* q) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 679 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 680 | sp_digit t1[2*10]; |
| 681 | sp_digit t2[2*10]; |
| 682 | sp_digit t3[2*10]; |
| 683 | sp_digit t4[2*10]; |
| 684 | sp_digit t5[2*10]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 685 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 686 | /* Ensure only the first point is the same as the result. */ |
| 687 | if (q == r) { |
| 688 | sp_point* a = p; |
| 689 | p = q; |
| 690 | q = a; |
| 691 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 692 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 693 | /* Check double */ |
| 694 | sp_256_sub_10(t1, p256_mod, q->y); |
| 695 | sp_256_norm_10(t1); |
| 696 | if (sp_256_cmp_equal_10(p->x, q->x) |
| 697 | && sp_256_cmp_equal_10(p->z, q->z) |
| 698 | && (sp_256_cmp_equal_10(p->y, q->y) || sp_256_cmp_equal_10(p->y, t1)) |
| 699 | ) { |
| 700 | sp_256_proj_point_dbl_10(r, p); |
| 701 | } |
| 702 | else { |
| 703 | sp_point tp; |
| 704 | sp_point *v; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 705 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 706 | v = r; |
| 707 | if (p->infinity | q->infinity) { |
| 708 | memset(&tp, 0, sizeof(tp)); |
| 709 | v = &tp; |
| 710 | } |
Denys Vlasenko | 772e187 | 2021-04-26 17:25:27 +0200 | [diff] [blame] | 711 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 712 | *r = p->infinity ? *q : *p; /* struct copy */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 713 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 714 | /* U1 = X1*Z2^2 */ |
| 715 | sp_256_mont_sqr_10(t1, q->z, p256_mod, p256_mp_mod); |
| 716 | sp_256_mont_mul_10(t3, t1, q->z, p256_mod, p256_mp_mod); |
| 717 | sp_256_mont_mul_10(t1, t1, v->x, p256_mod, p256_mp_mod); |
| 718 | /* U2 = X2*Z1^2 */ |
| 719 | sp_256_mont_sqr_10(t2, v->z, p256_mod, p256_mp_mod); |
| 720 | sp_256_mont_mul_10(t4, t2, v->z, p256_mod, p256_mp_mod); |
| 721 | sp_256_mont_mul_10(t2, t2, q->x, p256_mod, p256_mp_mod); |
| 722 | /* S1 = Y1*Z2^3 */ |
| 723 | sp_256_mont_mul_10(t3, t3, v->y, p256_mod, p256_mp_mod); |
| 724 | /* S2 = Y2*Z1^3 */ |
| 725 | sp_256_mont_mul_10(t4, t4, q->y, p256_mod, p256_mp_mod); |
| 726 | /* H = U2 - U1 */ |
| 727 | sp_256_mont_sub_10(t2, t2, t1, p256_mod); |
| 728 | /* R = S2 - S1 */ |
| 729 | sp_256_mont_sub_10(t4, t4, t3, p256_mod); |
| 730 | /* Z3 = H*Z1*Z2 */ |
| 731 | sp_256_mont_mul_10(v->z, v->z, q->z, p256_mod, p256_mp_mod); |
| 732 | sp_256_mont_mul_10(v->z, v->z, t2, p256_mod, p256_mp_mod); |
| 733 | /* X3 = R^2 - H^3 - 2*U1*H^2 */ |
| 734 | sp_256_mont_sqr_10(v->x, t4, p256_mod, p256_mp_mod); |
| 735 | sp_256_mont_sqr_10(t5, t2, p256_mod, p256_mp_mod); |
| 736 | sp_256_mont_mul_10(v->y, t1, t5, p256_mod, p256_mp_mod); |
| 737 | sp_256_mont_mul_10(t5, t5, t2, p256_mod, p256_mp_mod); |
| 738 | sp_256_mont_sub_10(v->x, v->x, t5, p256_mod); |
| 739 | sp_256_mont_dbl_10(t1, v->y, p256_mod); |
| 740 | sp_256_mont_sub_10(v->x, v->x, t1, p256_mod); |
| 741 | /* Y3 = R*(U1*H^2 - X3) - S1*H^3 */ |
| 742 | sp_256_mont_sub_10(v->y, v->y, v->x, p256_mod); |
| 743 | sp_256_mont_mul_10(v->y, v->y, t4, p256_mod, p256_mp_mod); |
| 744 | sp_256_mont_mul_10(t5, t5, t3, p256_mod, p256_mp_mod); |
| 745 | sp_256_mont_sub_10(v->y, v->y, t5, p256_mod); |
| 746 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | /* Multiply the point by the scalar and return the result. |
| 750 | * If map is true then convert result to affine co-ordinates. |
| 751 | * |
| 752 | * r Resulting point. |
| 753 | * g Point to multiply. |
| 754 | * k Scalar to multiply by. |
Denys Vlasenko | 03ab2a9 | 2021-04-26 14:55:46 +0200 | [diff] [blame] | 755 | * map Indicates whether to convert result to affine. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 756 | */ |
| 757 | static void sp_256_ecc_mulmod_10(sp_point* r, const sp_point* g, const sp_digit* k /*, int map*/) |
| 758 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 759 | enum { map = 1 }; /* we always convert result to affine coordinates */ |
| 760 | sp_point t[3]; |
| 761 | sp_digit n; |
| 762 | int i; |
| 763 | int c, y; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 764 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 765 | memset(t, 0, sizeof(t)); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 766 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 767 | /* t[0] = {0, 0, 1} * norm */ |
| 768 | t[0].infinity = 1; |
| 769 | /* t[1] = {g->x, g->y, g->z} * norm */ |
| 770 | sp_256_mod_mul_norm_10(t[1].x, g->x); |
| 771 | sp_256_mod_mul_norm_10(t[1].y, g->y); |
| 772 | sp_256_mod_mul_norm_10(t[1].z, g->z); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 773 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 774 | i = 9; |
| 775 | c = 22; |
| 776 | n = k[i--] << (26 - c); |
| 777 | for (; ; c--) { |
| 778 | if (c == 0) { |
| 779 | if (i == -1) |
| 780 | break; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 781 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 782 | n = k[i--]; |
| 783 | c = 26; |
| 784 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 785 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 786 | y = (n >> 25) & 1; |
| 787 | n <<= 1; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 788 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 789 | sp_256_proj_point_add_10(&t[y^1], &t[0], &t[1]); |
| 790 | memcpy(&t[2], &t[y], sizeof(sp_point)); |
| 791 | sp_256_proj_point_dbl_10(&t[2], &t[2]); |
| 792 | memcpy(&t[y], &t[2], sizeof(sp_point)); |
| 793 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 794 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 795 | if (map) |
| 796 | sp_256_map_10(r, &t[0]); |
| 797 | else |
| 798 | memcpy(r, &t[0], sizeof(sp_point)); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 799 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 800 | memset(t, 0, sizeof(t)); //paranoia |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* Multiply the base point of P256 by the scalar and return the result. |
| 804 | * If map is true then convert result to affine co-ordinates. |
| 805 | * |
| 806 | * r Resulting point. |
| 807 | * k Scalar to multiply by. |
Denys Vlasenko | 03ab2a9 | 2021-04-26 14:55:46 +0200 | [diff] [blame] | 808 | * map Indicates whether to convert result to affine. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 809 | */ |
| 810 | static void sp_256_ecc_mulmod_base_10(sp_point* r, sp_digit* k /*, int map*/) |
| 811 | { |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 812 | /* Since this function is called only once, save space: |
| 813 | * don't have "static const sp_point p256_base = {...}", |
| 814 | * it would have more zeros than data. |
| 815 | */ |
Denys Vlasenko | 48a18d1 | 2021-04-27 12:24:21 +0200 | [diff] [blame] | 816 | static const uint8_t p256_base_bin[] = { |
| 817 | /* x (big-endian) */ |
| 818 | 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, |
| 819 | /* y */ |
| 820 | 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] | 821 | /* z will be set to 1, infinity flag to "false" */ |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 822 | }; |
| 823 | sp_point p256_base; |
| 824 | |
Denys Vlasenko | 48a18d1 | 2021-04-27 12:24:21 +0200 | [diff] [blame] | 825 | sp_256_point_from_bin2x32(&p256_base, p256_base_bin); |
Denys Vlasenko | 39a3ef5 | 2021-04-27 01:31:51 +0200 | [diff] [blame] | 826 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 827 | sp_256_ecc_mulmod_10(r, &p256_base, k /*, map*/); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* Multiply the point by the scalar and serialize the X ordinate. |
| 831 | * The number is 0 padded to maximum size on output. |
| 832 | * |
| 833 | * priv Scalar to multiply the point by. |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 834 | * pub2x32 Point to multiply. |
| 835 | * out32 Buffer to hold X ordinate. |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 836 | */ |
Denys Vlasenko | a2bc52d | 2021-04-27 01:21:26 +0200 | [diff] [blame] | 837 | static void sp_ecc_secret_gen_256(const sp_digit priv[10], const uint8_t *pub2x32, uint8_t* out32) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 838 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 839 | sp_point point[1]; |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 840 | |
| 841 | #if FIXED_PEER_PUBKEY |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 842 | memset((void*)pub2x32, 0x55, 64); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 843 | #endif |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 844 | dump_hex("peerkey %s\n", pub2x32, 32); /* in TLS, this is peer's public key */ |
| 845 | dump_hex(" %s\n", pub2x32 + 32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 846 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 847 | sp_256_point_from_bin2x32(point, pub2x32); |
| 848 | dump_hex("point->x %s\n", point->x, sizeof(point->x)); |
| 849 | dump_hex("point->y %s\n", point->y, sizeof(point->y)); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 850 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 851 | sp_256_ecc_mulmod_10(point, point, priv); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 852 | |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 853 | sp_256_to_bin(point->x, out32); |
| 854 | dump_hex("out32: %s\n", out32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 855 | } |
| 856 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 857 | /* Generates a scalar that is in the range 1..order-1. */ |
| 858 | #define SIMPLIFY 1 |
| 859 | /* Add 1 to a. (a = a + 1) */ |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 860 | static void sp_256_add_one_10(sp_digit* a) |
| 861 | { |
Denys Vlasenko | 1204012 | 2021-04-26 20:24:34 +0200 | [diff] [blame] | 862 | a[0]++; |
| 863 | sp_256_norm_10(a); |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 864 | } |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 865 | static void sp_256_ecc_gen_k_10(sp_digit k[10]) |
| 866 | { |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 867 | #if !SIMPLIFY |
| 868 | /* The order of the curve P256 minus 2. */ |
| 869 | static const sp_digit p256_order2[10] = { |
| 870 | 0x063254f,0x272b0bf,0x1e84f3b,0x2b69c5e,0x3bce6fa, |
| 871 | 0x3ffffff,0x3ffffff,0x00003ff,0x3ff0000,0x03fffff, |
| 872 | }; |
| 873 | #endif |
| 874 | uint8_t buf[32]; |
| 875 | |
| 876 | for (;;) { |
| 877 | tls_get_random(buf, sizeof(buf)); |
| 878 | #if FIXED_SECRET |
| 879 | memset(buf, 0x77, sizeof(buf)); |
| 880 | #endif |
| 881 | sp_256_from_bin(k, 10, buf, sizeof(buf)); |
| 882 | #if !SIMPLIFY |
| 883 | if (sp_256_cmp_10(k, p256_order2) < 0) |
| 884 | break; |
| 885 | #else |
| 886 | /* non-loopy version (and not needing p256_order2[]): |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 887 | * if most-significant word seems that k can be larger |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 888 | * than p256_order2, fix it up: |
| 889 | */ |
| 890 | if (k[9] >= 0x03fffff) |
| 891 | k[9] = 0x03ffffe; |
| 892 | break; |
| 893 | #endif |
| 894 | } |
| 895 | sp_256_add_one_10(k); |
| 896 | #undef SIMPLIFY |
| 897 | } |
| 898 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 899 | /* Makes a random EC key pair. */ |
| 900 | static void sp_ecc_make_key_256(sp_digit privkey[10], uint8_t *pubkey) |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 901 | { |
| 902 | sp_point point[1]; |
| 903 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 904 | sp_256_ecc_gen_k_10(privkey); |
| 905 | sp_256_ecc_mulmod_base_10(point, privkey); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 906 | sp_256_to_bin(point->x, pubkey); |
| 907 | sp_256_to_bin(point->y, pubkey + 32); |
| 908 | |
| 909 | memset(point, 0, sizeof(point)); //paranoia |
| 910 | } |
| 911 | |
| 912 | void FAST_FUNC curve_P256_compute_pubkey_and_premaster( |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 913 | uint8_t *pubkey2x32, uint8_t *premaster32, |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 914 | const uint8_t *peerkey2x32) |
| 915 | { |
| 916 | sp_digit privkey[10]; |
| 917 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 918 | sp_ecc_make_key_256(privkey, pubkey2x32); |
| 919 | dump_hex("pubkey: %s\n", pubkey2x32, 32); |
| 920 | dump_hex(" %s\n", pubkey2x32 + 32, 32); |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 921 | |
Denys Vlasenko | 074b33b | 2021-04-26 14:33:38 +0200 | [diff] [blame] | 922 | /* Combine our privkey and peer's public key to generate premaster */ |
Denys Vlasenko | f18a1fd | 2021-04-26 13:25:56 +0200 | [diff] [blame] | 923 | sp_ecc_secret_gen_256(privkey, /*x,y:*/peerkey2x32, premaster32); |
| 924 | dump_hex("premaster: %s\n", premaster32, 32); |
| 925 | } |