| 1 | /* |
| 2 | * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "inner.h" |
| 26 | |
| 27 | /* |
| 28 | * Parameters for the field: |
| 29 | * - field modulus p = 2^255-19 |
| 30 | * - R^2 mod p (R = 2^(31k) for the smallest k such that R >= p) |
| 31 | */ |
| 32 | |
| 33 | static const uint32_t C255_P[] = { |
| 34 | 0x00000107, |
| 35 | 0x7FFFFFED, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, |
| 36 | 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x0000007F |
| 37 | }; |
| 38 | |
| 39 | #define P0I 0x286BCA1B |
| 40 | |
| 41 | static const uint32_t C255_R2[] = { |
| 42 | 0x00000107, |
| 43 | 0x00000000, 0x02D20000, 0x00000000, 0x00000000, 0x00000000, |
| 44 | 0x00000000, 0x00000000, 0x00000000, 0x00000000 |
| 45 | }; |
| 46 | |
| 47 | static const uint32_t C255_A24[] = { |
| 48 | 0x00000107, |
| 49 | 0x53000000, 0x0000468B, 0x00000000, 0x00000000, 0x00000000, |
| 50 | 0x00000000, 0x00000000, 0x00000000, 0x00000000 |
| 51 | }; |
| 52 | |
| 53 | /* obsolete |
| 54 | #include <stdio.h> |
| 55 | #include <stdlib.h> |
| 56 | static void |
| 57 | print_int_mont(const char *name, const uint32_t *x) |
| 58 | { |
| 59 | uint32_t y[10]; |
| 60 | unsigned char tmp[32]; |
| 61 | size_t u; |
| 62 | |
| 63 | printf("%s = ", name); |
| 64 | memcpy(y, x, sizeof y); |
| 65 | br_i31_from_monty(y, C255_P, P0I); |
| 66 | br_i31_encode(tmp, sizeof tmp, y); |
| 67 | for (u = 0; u < sizeof tmp; u ++) { |
| 68 | printf("%02X", tmp[u]); |
| 69 | } |
| 70 | printf("\n"); |
| 71 | } |
| 72 | */ |
| 73 | |
| 74 | static const unsigned char GEN[] = { |
| 75 | 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 76 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 77 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 78 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 79 | }; |
| 80 | |
| 81 | static const unsigned char ORDER[] = { |
| 82 | 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 83 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 84 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 85 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF |
| 86 | }; |
| 87 | |
| 88 | static const unsigned char * |
| 89 | api_generator(int curve, size_t *len) |
| 90 | { |
| 91 | (void)curve; |
| 92 | *len = 32; |
| 93 | return GEN; |
| 94 | } |
| 95 | |
| 96 | static const unsigned char * |
| 97 | api_order(int curve, size_t *len) |
| 98 | { |
| 99 | (void)curve; |
| 100 | *len = 32; |
| 101 | return ORDER; |
| 102 | } |
| 103 | |
| 104 | static size_t |
| 105 | api_xoff(int curve, size_t *len) |
| 106 | { |
| 107 | (void)curve; |
| 108 | *len = 32; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | cswap(uint32_t *a, uint32_t *b, uint32_t ctl) |
| 114 | { |
| 115 | int i; |
| 116 | |
| 117 | ctl = -ctl; |
| 118 | for (i = 0; i < 10; i ++) { |
| 119 | uint32_t aw, bw, tw; |
| 120 | |
| 121 | aw = a[i]; |
| 122 | bw = b[i]; |
| 123 | tw = ctl & (aw ^ bw); |
| 124 | a[i] = aw ^ tw; |
| 125 | b[i] = bw ^ tw; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | c255_add(uint32_t *d, const uint32_t *a, const uint32_t *b) |
| 131 | { |
| 132 | uint32_t ctl; |
| 133 | uint32_t t[10]; |
| 134 | |
| 135 | memcpy(t, a, sizeof t); |
| 136 | ctl = br_i31_add(t, b, 1); |
| 137 | ctl |= NOT(br_i31_sub(t, C255_P, 0)); |
| 138 | br_i31_sub(t, C255_P, ctl); |
| 139 | memcpy(d, t, sizeof t); |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | c255_sub(uint32_t *d, const uint32_t *a, const uint32_t *b) |
| 144 | { |
| 145 | uint32_t t[10]; |
| 146 | |
| 147 | memcpy(t, a, sizeof t); |
| 148 | br_i31_add(t, C255_P, br_i31_sub(t, b, 1)); |
| 149 | memcpy(d, t, sizeof t); |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | c255_mul(uint32_t *d, const uint32_t *a, const uint32_t *b) |
| 154 | { |
| 155 | uint32_t t[10]; |
| 156 | |
| 157 | br_i31_montymul(t, a, b, C255_P, P0I); |
| 158 | memcpy(d, t, sizeof t); |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | byteswap(unsigned char *G) |
| 163 | { |
| 164 | int i; |
| 165 | |
| 166 | for (i = 0; i < 16; i ++) { |
| 167 | unsigned char t; |
| 168 | |
| 169 | t = G[i]; |
| 170 | G[i] = G[31 - i]; |
| 171 | G[31 - i] = t; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static uint32_t |
| 176 | api_mul(unsigned char *G, size_t Glen, |
| 177 | const unsigned char *kb, size_t kblen, int curve) |
| 178 | { |
| 179 | uint32_t x1[10], x2[10], x3[10], z2[10], z3[10]; |
| 180 | uint32_t a[10], aa[10], b[10], bb[10]; |
| 181 | uint32_t c[10], d[10], e[10], da[10], cb[10]; |
| 182 | unsigned char k[32]; |
| 183 | uint32_t swap; |
| 184 | int i; |
| 185 | |
| 186 | (void)curve; |
| 187 | |
| 188 | /* |
| 189 | * Points are encoded over exactly 32 bytes. Multipliers must fit |
| 190 | * in 32 bytes as well. |
| 191 | * RFC 7748 mandates that the high bit of the last point byte must |
| 192 | * be ignored/cleared. |
| 193 | */ |
| 194 | if (Glen != 32 || kblen > 32) { |
| 195 | return 0; |
| 196 | } |
| 197 | G[31] &= 0x7F; |
| 198 | |
| 199 | /* |
| 200 | * Byteswap the point encoding, because it uses little-endian, and |
| 201 | * the generic decoding routine uses big-endian. |
| 202 | */ |
| 203 | byteswap(G); |
| 204 | |
| 205 | /* |
| 206 | * Decode the point ('u' coordinate). This should be reduced |
| 207 | * modulo p, but we prefer to avoid the dependency on |
| 208 | * br_i31_decode_reduce(). Instead, we use br_i31_decode_mod() |
| 209 | * with a synthetic modulus of value 2^255 (this must work |
| 210 | * since G was truncated to 255 bits), then use a conditional |
| 211 | * subtraction. We use br_i31_decode_mod() and not |
| 212 | * br_i31_decode(), because the ec_prime_i31 implementation uses |
| 213 | * the former but not the latter. |
| 214 | * br_i31_decode_reduce(a, G, 32, C255_P); |
| 215 | */ |
| 216 | br_i31_zero(b, 0x108); |
| 217 | b[9] = 0x0100; |
| 218 | br_i31_decode_mod(a, G, 32, b); |
| 219 | a[0] = 0x107; |
| 220 | br_i31_sub(a, C255_P, NOT(br_i31_sub(a, C255_P, 0))); |
| 221 | |
| 222 | /* |
| 223 | * Initialise variables x1, x2, z2, x3 and z3. We set all of them |
| 224 | * into Montgomery representation. |
| 225 | */ |
| 226 | br_i31_montymul(x1, a, C255_R2, C255_P, P0I); |
| 227 | memcpy(x3, x1, sizeof x1); |
| 228 | br_i31_zero(z2, C255_P[0]); |
| 229 | memcpy(x2, z2, sizeof z2); |
| 230 | x2[1] = 0x13000000; |
| 231 | memcpy(z3, x2, sizeof x2); |
| 232 | |
| 233 | memcpy(k, kb, kblen); |
| 234 | memset(k + kblen, 0, (sizeof k) - kblen); |
| 235 | k[0] &= 0xF8; |
| 236 | k[31] &= 0x7F; |
| 237 | k[31] |= 0x40; |
| 238 | |
| 239 | /* obsolete |
| 240 | print_int_mont("x1", x1); |
| 241 | */ |
| 242 | |
| 243 | swap = 0; |
| 244 | for (i = 254; i >= 0; i --) { |
| 245 | uint32_t kt; |
| 246 | |
| 247 | kt = (k[i >> 3] >> (i & 7)) & 1; |
| 248 | swap ^= kt; |
| 249 | cswap(x2, x3, swap); |
| 250 | cswap(z2, z3, swap); |
| 251 | swap = kt; |
| 252 | |
| 253 | /* obsolete |
| 254 | print_int_mont("x2", x2); |
| 255 | print_int_mont("z2", z2); |
| 256 | print_int_mont("x3", x3); |
| 257 | print_int_mont("z3", z3); |
| 258 | */ |
| 259 | |
| 260 | c255_add(a, x2, z2); |
| 261 | c255_mul(aa, a, a); |
| 262 | c255_sub(b, x2, z2); |
| 263 | c255_mul(bb, b, b); |
| 264 | c255_sub(e, aa, bb); |
| 265 | c255_add(c, x3, z3); |
| 266 | c255_sub(d, x3, z3); |
| 267 | c255_mul(da, d, a); |
| 268 | c255_mul(cb, c, b); |
| 269 | |
| 270 | /* obsolete |
| 271 | print_int_mont("a ", a); |
| 272 | print_int_mont("aa", aa); |
| 273 | print_int_mont("b ", b); |
| 274 | print_int_mont("bb", bb); |
| 275 | print_int_mont("e ", e); |
| 276 | print_int_mont("c ", c); |
| 277 | print_int_mont("d ", d); |
| 278 | print_int_mont("da", da); |
| 279 | print_int_mont("cb", cb); |
| 280 | */ |
| 281 | |
| 282 | c255_add(x3, da, cb); |
| 283 | c255_mul(x3, x3, x3); |
| 284 | c255_sub(z3, da, cb); |
| 285 | c255_mul(z3, z3, z3); |
| 286 | c255_mul(z3, z3, x1); |
| 287 | c255_mul(x2, aa, bb); |
| 288 | c255_mul(z2, C255_A24, e); |
| 289 | c255_add(z2, z2, aa); |
| 290 | c255_mul(z2, e, z2); |
| 291 | |
| 292 | /* obsolete |
| 293 | print_int_mont("x2", x2); |
| 294 | print_int_mont("z2", z2); |
| 295 | print_int_mont("x3", x3); |
| 296 | print_int_mont("z3", z3); |
| 297 | */ |
| 298 | } |
| 299 | cswap(x2, x3, swap); |
| 300 | cswap(z2, z3, swap); |
| 301 | |
| 302 | /* |
| 303 | * Inverse z2 with a modular exponentiation. This is a simple |
| 304 | * square-and-multiply algorithm; we mutualise most non-squarings |
| 305 | * since the exponent contains almost only ones. |
| 306 | */ |
| 307 | memcpy(a, z2, sizeof z2); |
| 308 | for (i = 0; i < 15; i ++) { |
| 309 | c255_mul(a, a, a); |
| 310 | c255_mul(a, a, z2); |
| 311 | } |
| 312 | memcpy(b, a, sizeof a); |
| 313 | for (i = 0; i < 14; i ++) { |
| 314 | int j; |
| 315 | |
| 316 | for (j = 0; j < 16; j ++) { |
| 317 | c255_mul(b, b, b); |
| 318 | } |
| 319 | c255_mul(b, b, a); |
| 320 | } |
| 321 | for (i = 14; i >= 0; i --) { |
| 322 | c255_mul(b, b, b); |
| 323 | if ((0xFFEB >> i) & 1) { |
| 324 | c255_mul(b, z2, b); |
| 325 | } |
| 326 | } |
| 327 | c255_mul(b, x2, b); |
| 328 | |
| 329 | /* |
| 330 | * To avoid a dependency on br_i31_from_monty(), we use |
| 331 | * a Montgomery multiplication with 1. |
| 332 | * memcpy(x2, b, sizeof b); |
| 333 | * br_i31_from_monty(x2, C255_P, P0I); |
| 334 | */ |
| 335 | br_i31_zero(a, C255_P[0]); |
| 336 | a[1] = 1; |
| 337 | br_i31_montymul(x2, a, b, C255_P, P0I); |
| 338 | |
| 339 | br_i31_encode(G, 32, x2); |
| 340 | byteswap(G); |
| 341 | return 1; |
| 342 | } |
| 343 | |
| 344 | static size_t |
| 345 | api_mulgen(unsigned char *R, |
| 346 | const unsigned char *x, size_t xlen, int curve) |
| 347 | { |
| 348 | const unsigned char *G; |
| 349 | size_t Glen; |
| 350 | |
| 351 | G = api_generator(curve, &Glen); |
| 352 | memcpy(R, G, Glen); |
| 353 | api_mul(R, Glen, x, xlen, curve); |
| 354 | return Glen; |
| 355 | } |
| 356 | |
| 357 | static uint32_t |
| 358 | api_muladd(unsigned char *A, const unsigned char *B, size_t len, |
| 359 | const unsigned char *x, size_t xlen, |
| 360 | const unsigned char *y, size_t ylen, int curve) |
| 361 | { |
| 362 | /* |
| 363 | * We don't implement this method, since it is used for ECDSA |
| 364 | * only, and there is no ECDSA over Curve25519 (which instead |
| 365 | * uses EdDSA). |
| 366 | */ |
| 367 | (void)A; |
| 368 | (void)B; |
| 369 | (void)len; |
| 370 | (void)x; |
| 371 | (void)xlen; |
| 372 | (void)y; |
| 373 | (void)ylen; |
| 374 | (void)curve; |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* see bearssl_ec.h */ |
| 379 | const br_ec_impl br_ec_c25519_i31 = { |
| 380 | (uint32_t)0x20000000, |
| 381 | &api_generator, |
| 382 | &api_order, |
| 383 | &api_xoff, |
| 384 | &api_mul, |
| 385 | &api_mulgen, |
| 386 | &api_muladd |
| 387 | }; |