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