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