+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | // Monocypher version __git__ |
| 2 | // |
| 3 | // This file is dual-licensed. Choose whichever licence you want from |
| 4 | // the two licences listed below. |
| 5 | // |
| 6 | // The first licence is a regular 2-clause BSD licence. The second licence |
| 7 | // is the CC-0 from Creative Commons. It is intended to release Monocypher |
| 8 | // to the public domain. The BSD licence serves as a fallback option. |
| 9 | // |
| 10 | // SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0 |
| 11 | // |
| 12 | // ------------------------------------------------------------------------ |
| 13 | // |
| 14 | // Copyright (c) 2017-2020, Loup Vaillant |
| 15 | // All rights reserved. |
| 16 | // |
| 17 | // |
| 18 | // Redistribution and use in source and binary forms, with or without |
| 19 | // modification, are permitted provided that the following conditions are |
| 20 | // met: |
| 21 | // |
| 22 | // 1. Redistributions of source code must retain the above copyright |
| 23 | // notice, this list of conditions and the following disclaimer. |
| 24 | // |
| 25 | // 2. Redistributions in binary form must reproduce the above copyright |
| 26 | // notice, this list of conditions and the following disclaimer in the |
| 27 | // documentation and/or other materials provided with the |
| 28 | // distribution. |
| 29 | // |
| 30 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 31 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 32 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 33 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 34 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 35 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 36 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 37 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 38 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 39 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 40 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 41 | // |
| 42 | // ------------------------------------------------------------------------ |
| 43 | // |
| 44 | // Written in 2017-2020 by Loup Vaillant |
| 45 | // |
| 46 | // To the extent possible under law, the author(s) have dedicated all copyright |
| 47 | // and related neighboring rights to this software to the public domain |
| 48 | // worldwide. This software is distributed without any warranty. |
| 49 | // |
| 50 | // You should have received a copy of the CC0 Public Domain Dedication along |
| 51 | // with this software. If not, see |
| 52 | // <https://creativecommons.org/publicdomain/zero/1.0/> |
| 53 | |
| 54 | #include "monocypher.h" |
| 55 | |
| 56 | #ifdef MONOCYPHER_CPP_NAMESPACE |
| 57 | namespace MONOCYPHER_CPP_NAMESPACE { |
| 58 | #endif |
| 59 | |
| 60 | ///////////////// |
| 61 | /// Utilities /// |
| 62 | ///////////////// |
| 63 | #define FOR_T(type, i, start, end) for (type i = (start); i < (end); i++) |
| 64 | #define FOR(i, start, end) FOR_T(size_t, i, start, end) |
| 65 | #define COPY(dst, src, size) FOR(_i_, 0, size) (dst)[_i_] = (src)[_i_] |
| 66 | #define ZERO(buf, size) FOR(_i_, 0, size) (buf)[_i_] = 0 |
| 67 | #define WIPE_CTX(ctx) crypto_wipe(ctx , sizeof(*(ctx))) |
| 68 | #define WIPE_BUFFER(buffer) crypto_wipe(buffer, sizeof(buffer)) |
| 69 | #define MIN(a, b) ((a) <= (b) ? (a) : (b)) |
| 70 | #define MAX(a, b) ((a) >= (b) ? (a) : (b)) |
| 71 | |
| 72 | typedef int8_t i8; |
| 73 | typedef uint8_t u8; |
| 74 | typedef int16_t i16; |
| 75 | typedef uint32_t u32; |
| 76 | typedef int32_t i32; |
| 77 | typedef int64_t i64; |
| 78 | typedef uint64_t u64; |
| 79 | |
| 80 | static const u8 zero[128] = {0}; |
| 81 | |
| 82 | // Return the smallest positive integer y such that |
| 83 | // (x + y) % pow_2 == 0 |
| 84 | // Basically, y is the "gap" missing to align x. |
| 85 | // Only works when pow_2 is a power of 2. |
| 86 | // Note: we use ~x+1 instead of -x to avoid compiler warnings |
| 87 | static size_t gap(size_t x, size_t pow_2) |
| 88 | { |
| 89 | return (~x + 1) & (pow_2 - 1); |
| 90 | } |
| 91 | |
| 92 | static u32 load24_le(const u8 s[3]) |
| 93 | { |
| 94 | return |
| 95 | ((u32)s[0] << 0) | |
| 96 | ((u32)s[1] << 8) | |
| 97 | ((u32)s[2] << 16); |
| 98 | } |
| 99 | |
| 100 | static u32 load32_le(const u8 s[4]) |
| 101 | { |
| 102 | return |
| 103 | ((u32)s[0] << 0) | |
| 104 | ((u32)s[1] << 8) | |
| 105 | ((u32)s[2] << 16) | |
| 106 | ((u32)s[3] << 24); |
| 107 | } |
| 108 | |
| 109 | static u64 load64_le(const u8 s[8]) |
| 110 | { |
| 111 | return load32_le(s) | ((u64)load32_le(s+4) << 32); |
| 112 | } |
| 113 | |
| 114 | static void store32_le(u8 out[4], u32 in) |
| 115 | { |
| 116 | out[0] = (u8)(in ); |
| 117 | out[1] = (u8)(in >> 8); |
| 118 | out[2] = (u8)(in >> 16); |
| 119 | out[3] = (u8)(in >> 24); |
| 120 | } |
| 121 | |
| 122 | static void store64_le(u8 out[8], u64 in) |
| 123 | { |
| 124 | store32_le(out , (u32)(in )); |
| 125 | store32_le(out + 4, (u32)(in >> 32)); |
| 126 | } |
| 127 | |
| 128 | static void load32_le_buf (u32 *dst, const u8 *src, size_t size) { |
| 129 | FOR(i, 0, size) { dst[i] = load32_le(src + i*4); } |
| 130 | } |
| 131 | static void load64_le_buf (u64 *dst, const u8 *src, size_t size) { |
| 132 | FOR(i, 0, size) { dst[i] = load64_le(src + i*8); } |
| 133 | } |
| 134 | static void store32_le_buf(u8 *dst, const u32 *src, size_t size) { |
| 135 | FOR(i, 0, size) { store32_le(dst + i*4, src[i]); } |
| 136 | } |
| 137 | static void store64_le_buf(u8 *dst, const u64 *src, size_t size) { |
| 138 | FOR(i, 0, size) { store64_le(dst + i*8, src[i]); } |
| 139 | } |
| 140 | |
| 141 | static u64 rotr64(u64 x, u64 n) { return (x >> n) ^ (x << (64 - n)); } |
| 142 | static u32 rotl32(u32 x, u32 n) { return (x << n) ^ (x >> (32 - n)); } |
| 143 | |
| 144 | static int neq0(u64 diff) |
| 145 | { |
| 146 | // Constant time comparison to zero |
| 147 | // return diff != 0 ? -1 : 0 |
| 148 | u32 fold = (u32)(diff >> 32) | (u32)diff; |
| 149 | fold |= 0u - fold; |
| 150 | return -(int)(fold >> 31); |
| 151 | } |
| 152 | |
| 153 | static u64 x16(const u8 a[16], const u8 b[16]) |
| 154 | { |
| 155 | return (load64_le(a + 0) ^ load64_le(b + 0)) |
| 156 | | (load64_le(a + 8) ^ load64_le(b + 8)); |
| 157 | } |
| 158 | static u64 x32(const u8 a[32],const u8 b[32]){return x16(a,b)| x16(a+16, b+16);} |
| 159 | static u64 x64(const u8 a[64],const u8 b[64]){return x32(a,b)| x32(a+32, b+32);} |
| 160 | int crypto_verify16(const u8 a[16], const u8 b[16]){ return neq0(x16(a, b)); } |
| 161 | int crypto_verify32(const u8 a[32], const u8 b[32]){ return neq0(x32(a, b)); } |
| 162 | int crypto_verify64(const u8 a[64], const u8 b[64]){ return neq0(x64(a, b)); } |
| 163 | |
| 164 | void crypto_wipe(void *secret, size_t size) |
| 165 | { |
| 166 | volatile u8 *v_secret = (u8*)secret; |
| 167 | ZERO(v_secret, size); |
| 168 | } |
| 169 | |
| 170 | ///////////////// |
| 171 | /// Chacha 20 /// |
| 172 | ///////////////// |
| 173 | #define QUARTERROUND(a, b, c, d) \ |
| 174 | a += b; d = rotl32(d ^ a, 16); \ |
| 175 | c += d; b = rotl32(b ^ c, 12); \ |
| 176 | a += b; d = rotl32(d ^ a, 8); \ |
| 177 | c += d; b = rotl32(b ^ c, 7) |
| 178 | |
| 179 | static void chacha20_rounds(u32 out[16], const u32 in[16]) |
| 180 | { |
| 181 | // The temporary variables make Chacha20 10% faster. |
| 182 | u32 t0 = in[ 0]; u32 t1 = in[ 1]; u32 t2 = in[ 2]; u32 t3 = in[ 3]; |
| 183 | u32 t4 = in[ 4]; u32 t5 = in[ 5]; u32 t6 = in[ 6]; u32 t7 = in[ 7]; |
| 184 | u32 t8 = in[ 8]; u32 t9 = in[ 9]; u32 t10 = in[10]; u32 t11 = in[11]; |
| 185 | u32 t12 = in[12]; u32 t13 = in[13]; u32 t14 = in[14]; u32 t15 = in[15]; |
| 186 | |
| 187 | FOR (i, 0, 10) { // 20 rounds, 2 rounds per loop. |
| 188 | QUARTERROUND(t0, t4, t8 , t12); // column 0 |
| 189 | QUARTERROUND(t1, t5, t9 , t13); // column 1 |
| 190 | QUARTERROUND(t2, t6, t10, t14); // column 2 |
| 191 | QUARTERROUND(t3, t7, t11, t15); // column 3 |
| 192 | QUARTERROUND(t0, t5, t10, t15); // diagonal 0 |
| 193 | QUARTERROUND(t1, t6, t11, t12); // diagonal 1 |
| 194 | QUARTERROUND(t2, t7, t8 , t13); // diagonal 2 |
| 195 | QUARTERROUND(t3, t4, t9 , t14); // diagonal 3 |
| 196 | } |
| 197 | out[ 0] = t0; out[ 1] = t1; out[ 2] = t2; out[ 3] = t3; |
| 198 | out[ 4] = t4; out[ 5] = t5; out[ 6] = t6; out[ 7] = t7; |
| 199 | out[ 8] = t8; out[ 9] = t9; out[10] = t10; out[11] = t11; |
| 200 | out[12] = t12; out[13] = t13; out[14] = t14; out[15] = t15; |
| 201 | } |
| 202 | |
| 203 | static const u8 *chacha20_constant = (const u8*)"expand 32-byte k"; // 16 bytes |
| 204 | |
| 205 | void crypto_chacha20_h(u8 out[32], const u8 key[32], const u8 in [16]) |
| 206 | { |
| 207 | u32 block[16]; |
| 208 | load32_le_buf(block , chacha20_constant, 4); |
| 209 | load32_le_buf(block + 4, key , 8); |
| 210 | load32_le_buf(block + 12, in , 4); |
| 211 | |
| 212 | chacha20_rounds(block, block); |
| 213 | |
| 214 | // Prevent reversal of the rounds by revealing only half of the buffer. |
| 215 | store32_le_buf(out , block , 4); // constant |
| 216 | store32_le_buf(out+16, block+12, 4); // counter and nonce |
| 217 | WIPE_BUFFER(block); |
| 218 | } |
| 219 | |
| 220 | u64 crypto_chacha20_djb(u8 *cipher_text, const u8 *plain_text, |
| 221 | size_t text_size, const u8 key[32], const u8 nonce[8], |
| 222 | u64 ctr) |
| 223 | { |
| 224 | u32 input[16]; |
| 225 | load32_le_buf(input , chacha20_constant, 4); |
| 226 | load32_le_buf(input + 4, key , 8); |
| 227 | load32_le_buf(input + 14, nonce , 2); |
| 228 | input[12] = (u32) ctr; |
| 229 | input[13] = (u32)(ctr >> 32); |
| 230 | |
| 231 | // Whole blocks |
| 232 | u32 pool[16]; |
| 233 | size_t nb_blocks = text_size >> 6; |
| 234 | FOR (i, 0, nb_blocks) { |
| 235 | chacha20_rounds(pool, input); |
| 236 | if (plain_text != NULL) { |
| 237 | FOR (j, 0, 16) { |
| 238 | u32 p = pool[j] + input[j]; |
| 239 | store32_le(cipher_text, p ^ load32_le(plain_text)); |
| 240 | cipher_text += 4; |
| 241 | plain_text += 4; |
| 242 | } |
| 243 | } else { |
| 244 | FOR (j, 0, 16) { |
| 245 | u32 p = pool[j] + input[j]; |
| 246 | store32_le(cipher_text, p); |
| 247 | cipher_text += 4; |
| 248 | } |
| 249 | } |
| 250 | input[12]++; |
| 251 | if (input[12] == 0) { |
| 252 | input[13]++; |
| 253 | } |
| 254 | } |
| 255 | text_size &= 63; |
| 256 | |
| 257 | // Last (incomplete) block |
| 258 | if (text_size > 0) { |
| 259 | if (plain_text == NULL) { |
| 260 | plain_text = zero; |
| 261 | } |
| 262 | chacha20_rounds(pool, input); |
| 263 | u8 tmp[64]; |
| 264 | FOR (i, 0, 16) { |
| 265 | store32_le(tmp + i*4, pool[i] + input[i]); |
| 266 | } |
| 267 | FOR (i, 0, text_size) { |
| 268 | cipher_text[i] = tmp[i] ^ plain_text[i]; |
| 269 | } |
| 270 | WIPE_BUFFER(tmp); |
| 271 | } |
| 272 | ctr = input[12] + ((u64)input[13] << 32) + (text_size > 0); |
| 273 | |
| 274 | WIPE_BUFFER(pool); |
| 275 | WIPE_BUFFER(input); |
| 276 | return ctr; |
| 277 | } |
| 278 | |
| 279 | u32 crypto_chacha20_ietf(u8 *cipher_text, const u8 *plain_text, |
| 280 | size_t text_size, |
| 281 | const u8 key[32], const u8 nonce[12], u32 ctr) |
| 282 | { |
| 283 | u64 big_ctr = ctr + ((u64)load32_le(nonce) << 32); |
| 284 | return (u32)crypto_chacha20_djb(cipher_text, plain_text, text_size, |
| 285 | key, nonce + 4, big_ctr); |
| 286 | } |
| 287 | |
| 288 | u64 crypto_chacha20_x(u8 *cipher_text, const u8 *plain_text, |
| 289 | size_t text_size, |
| 290 | const u8 key[32], const u8 nonce[24], u64 ctr) |
| 291 | { |
| 292 | u8 sub_key[32]; |
| 293 | crypto_chacha20_h(sub_key, key, nonce); |
| 294 | ctr = crypto_chacha20_djb(cipher_text, plain_text, text_size, |
| 295 | sub_key, nonce + 16, ctr); |
| 296 | WIPE_BUFFER(sub_key); |
| 297 | return ctr; |
| 298 | } |
| 299 | |
| 300 | ///////////////// |
| 301 | /// Poly 1305 /// |
| 302 | ///////////////// |
| 303 | |
| 304 | // h = (h + c) * r |
| 305 | // Preconditions: |
| 306 | // ctx->h <= 4_ffffffff_ffffffff_ffffffff_ffffffff |
| 307 | // ctx->r <= 0ffffffc_0ffffffc_0ffffffc_0fffffff |
| 308 | // end <= 1 |
| 309 | // Postcondition: |
| 310 | // ctx->h <= 4_ffffffff_ffffffff_ffffffff_ffffffff |
| 311 | static void poly_blocks(crypto_poly1305_ctx *ctx, const u8 *in, |
| 312 | size_t nb_blocks, unsigned end) |
| 313 | { |
| 314 | // Local all the things! |
| 315 | const u32 r0 = ctx->r[0]; |
| 316 | const u32 r1 = ctx->r[1]; |
| 317 | const u32 r2 = ctx->r[2]; |
| 318 | const u32 r3 = ctx->r[3]; |
| 319 | const u32 rr0 = (r0 >> 2) * 5; // lose 2 bits... |
| 320 | const u32 rr1 = (r1 >> 2) + r1; // rr1 == (r1 >> 2) * 5 |
| 321 | const u32 rr2 = (r2 >> 2) + r2; // rr1 == (r2 >> 2) * 5 |
| 322 | const u32 rr3 = (r3 >> 2) + r3; // rr1 == (r3 >> 2) * 5 |
| 323 | const u32 rr4 = r0 & 3; // ...recover 2 bits |
| 324 | u32 h0 = ctx->h[0]; |
| 325 | u32 h1 = ctx->h[1]; |
| 326 | u32 h2 = ctx->h[2]; |
| 327 | u32 h3 = ctx->h[3]; |
| 328 | u32 h4 = ctx->h[4]; |
| 329 | |
| 330 | FOR (i, 0, nb_blocks) { |
| 331 | // h + c, without carry propagation |
| 332 | const u64 s0 = (u64)h0 + load32_le(in); in += 4; |
| 333 | const u64 s1 = (u64)h1 + load32_le(in); in += 4; |
| 334 | const u64 s2 = (u64)h2 + load32_le(in); in += 4; |
| 335 | const u64 s3 = (u64)h3 + load32_le(in); in += 4; |
| 336 | const u32 s4 = h4 + end; |
| 337 | |
| 338 | // (h + c) * r, without carry propagation |
| 339 | const u64 x0 = s0*r0+ s1*rr3+ s2*rr2+ s3*rr1+ s4*rr0; |
| 340 | const u64 x1 = s0*r1+ s1*r0 + s2*rr3+ s3*rr2+ s4*rr1; |
| 341 | const u64 x2 = s0*r2+ s1*r1 + s2*r0 + s3*rr3+ s4*rr2; |
| 342 | const u64 x3 = s0*r3+ s1*r2 + s2*r1 + s3*r0 + s4*rr3; |
| 343 | const u32 x4 = s4*rr4; |
| 344 | |
| 345 | // partial reduction modulo 2^130 - 5 |
| 346 | const u32 u5 = (u32)(x3 >> 32) + x4; // u5 <= 7ffffff5 |
| 347 | const u64 u0 = (u32)(u5 >> 2) * 5 + (x0 & 0xffffffff); |
| 348 | const u64 u1 = (u32)(u0 >> 32) + (x1 & 0xffffffff) + (x0 >> 32); |
| 349 | const u64 u2 = (u32)(u1 >> 32) + (x2 & 0xffffffff) + (x1 >> 32); |
| 350 | const u64 u3 = (u32)(u2 >> 32) + (x3 & 0xffffffff) + (x2 >> 32); |
| 351 | const u32 u4 = (u32)(u3 >> 32) + (u5 & 3); // u4 <= 4 |
| 352 | |
| 353 | // Update the hash |
| 354 | h0 = u0 & 0xffffffff; |
| 355 | h1 = u1 & 0xffffffff; |
| 356 | h2 = u2 & 0xffffffff; |
| 357 | h3 = u3 & 0xffffffff; |
| 358 | h4 = u4; |
| 359 | } |
| 360 | ctx->h[0] = h0; |
| 361 | ctx->h[1] = h1; |
| 362 | ctx->h[2] = h2; |
| 363 | ctx->h[3] = h3; |
| 364 | ctx->h[4] = h4; |
| 365 | } |
| 366 | |
| 367 | void crypto_poly1305_init(crypto_poly1305_ctx *ctx, const u8 key[32]) |
| 368 | { |
| 369 | ZERO(ctx->h, 5); // Initial hash is zero |
| 370 | ctx->c_idx = 0; |
| 371 | // Load r and pad (r has some of its bits cleared) |
| 372 | load32_le_buf(ctx->r , key , 4); |
| 373 | load32_le_buf(ctx->pad, key+16, 4); |
| 374 | FOR (i, 0, 1) { ctx->r[i] &= 0x0fffffff; } |
| 375 | FOR (i, 1, 4) { ctx->r[i] &= 0x0ffffffc; } |
| 376 | } |
| 377 | |
| 378 | void crypto_poly1305_update(crypto_poly1305_ctx *ctx, |
| 379 | const u8 *message, size_t message_size) |
| 380 | { |
| 381 | // Avoid undefined NULL pointer increments with empty messages |
| 382 | if (message_size == 0) { |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | // Align ourselves with block boundaries |
| 387 | size_t aligned = MIN(gap(ctx->c_idx, 16), message_size); |
| 388 | FOR (i, 0, aligned) { |
| 389 | ctx->c[ctx->c_idx] = *message; |
| 390 | ctx->c_idx++; |
| 391 | message++; |
| 392 | message_size--; |
| 393 | } |
| 394 | |
| 395 | // If block is complete, process it |
| 396 | if (ctx->c_idx == 16) { |
| 397 | poly_blocks(ctx, ctx->c, 1, 1); |
| 398 | ctx->c_idx = 0; |
| 399 | } |
| 400 | |
| 401 | // Process the message block by block |
| 402 | size_t nb_blocks = message_size >> 4; |
| 403 | poly_blocks(ctx, message, nb_blocks, 1); |
| 404 | message += nb_blocks << 4; |
| 405 | message_size &= 15; |
| 406 | |
| 407 | // Remaining bytes (we never complete a block here) |
| 408 | FOR (i, 0, message_size) { |
| 409 | ctx->c[ctx->c_idx] = message[i]; |
| 410 | ctx->c_idx++; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | void crypto_poly1305_final(crypto_poly1305_ctx *ctx, u8 mac[16]) |
| 415 | { |
| 416 | // Process the last block (if any) |
| 417 | // We move the final 1 according to remaining input length |
| 418 | // (this will add less than 2^130 to the last input block) |
| 419 | if (ctx->c_idx != 0) { |
| 420 | ZERO(ctx->c + ctx->c_idx, 16 - ctx->c_idx); |
| 421 | ctx->c[ctx->c_idx] = 1; |
| 422 | poly_blocks(ctx, ctx->c, 1, 0); |
| 423 | } |
| 424 | |
| 425 | // Check if we should subtract 2^130-5 by performing the |
| 426 | // corresponding carry propagation. |
| 427 | u64 c = 5; |
| 428 | FOR (i, 0, 4) { |
| 429 | c += ctx->h[i]; |
| 430 | c >>= 32; |
| 431 | } |
| 432 | c += ctx->h[4]; |
| 433 | c = (c >> 2) * 5; // Shift the carry back to the beginning |
| 434 | // c now indicates how many times we should subtract 2^130-5 (0 or 1) |
| 435 | FOR (i, 0, 4) { |
| 436 | c += (u64)ctx->h[i] + ctx->pad[i]; |
| 437 | store32_le(mac + i*4, (u32)c); |
| 438 | c = c >> 32; |
| 439 | } |
| 440 | WIPE_CTX(ctx); |
| 441 | } |
| 442 | |
| 443 | void crypto_poly1305(u8 mac[16], const u8 *message, |
| 444 | size_t message_size, const u8 key[32]) |
| 445 | { |
| 446 | crypto_poly1305_ctx ctx; |
| 447 | crypto_poly1305_init (&ctx, key); |
| 448 | crypto_poly1305_update(&ctx, message, message_size); |
| 449 | crypto_poly1305_final (&ctx, mac); |
| 450 | } |
| 451 | |
| 452 | //////////////// |
| 453 | /// BLAKE2 b /// |
| 454 | //////////////// |
| 455 | static const u64 iv[8] = { |
| 456 | 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, |
| 457 | 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, |
| 458 | 0x510e527fade682d1, 0x9b05688c2b3e6c1f, |
| 459 | 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179, |
| 460 | }; |
| 461 | |
| 462 | static void blake2b_compress(crypto_blake2b_ctx *ctx, int is_last_block) |
| 463 | { |
| 464 | static const u8 sigma[12][16] = { |
| 465 | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, |
| 466 | { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, |
| 467 | { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, |
| 468 | { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, |
| 469 | { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, |
| 470 | { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, |
| 471 | { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, |
| 472 | { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, |
| 473 | { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, |
| 474 | { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 }, |
| 475 | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, |
| 476 | { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, |
| 477 | }; |
| 478 | |
| 479 | // Increment input offset |
| 480 | u64 *x = ctx->input_offset; |
| 481 | size_t y = ctx->input_idx; |
| 482 | x[0] += y; |
| 483 | if (x[0] < y) { |
| 484 | x[1]++; |
| 485 | } |
| 486 | |
| 487 | // Init work vector |
| 488 | u64 v0 = ctx->hash[0]; u64 v8 = iv[0]; |
| 489 | u64 v1 = ctx->hash[1]; u64 v9 = iv[1]; |
| 490 | u64 v2 = ctx->hash[2]; u64 v10 = iv[2]; |
| 491 | u64 v3 = ctx->hash[3]; u64 v11 = iv[3]; |
| 492 | u64 v4 = ctx->hash[4]; u64 v12 = iv[4] ^ ctx->input_offset[0]; |
| 493 | u64 v5 = ctx->hash[5]; u64 v13 = iv[5] ^ ctx->input_offset[1]; |
| 494 | u64 v6 = ctx->hash[6]; u64 v14 = iv[6] ^ (u64)~(is_last_block - 1); |
| 495 | u64 v7 = ctx->hash[7]; u64 v15 = iv[7]; |
| 496 | |
| 497 | // Mangle work vector |
| 498 | u64 *input = ctx->input; |
| 499 | #define BLAKE2_G(a, b, c, d, x, y) \ |
| 500 | a += b + x; d = rotr64(d ^ a, 32); \ |
| 501 | c += d; b = rotr64(b ^ c, 24); \ |
| 502 | a += b + y; d = rotr64(d ^ a, 16); \ |
| 503 | c += d; b = rotr64(b ^ c, 63) |
| 504 | #define BLAKE2_ROUND(i) \ |
| 505 | BLAKE2_G(v0, v4, v8 , v12, input[sigma[i][ 0]], input[sigma[i][ 1]]); \ |
| 506 | BLAKE2_G(v1, v5, v9 , v13, input[sigma[i][ 2]], input[sigma[i][ 3]]); \ |
| 507 | BLAKE2_G(v2, v6, v10, v14, input[sigma[i][ 4]], input[sigma[i][ 5]]); \ |
| 508 | BLAKE2_G(v3, v7, v11, v15, input[sigma[i][ 6]], input[sigma[i][ 7]]); \ |
| 509 | BLAKE2_G(v0, v5, v10, v15, input[sigma[i][ 8]], input[sigma[i][ 9]]); \ |
| 510 | BLAKE2_G(v1, v6, v11, v12, input[sigma[i][10]], input[sigma[i][11]]); \ |
| 511 | BLAKE2_G(v2, v7, v8 , v13, input[sigma[i][12]], input[sigma[i][13]]); \ |
| 512 | BLAKE2_G(v3, v4, v9 , v14, input[sigma[i][14]], input[sigma[i][15]]) |
| 513 | |
| 514 | #ifdef BLAKE2_NO_UNROLLING |
| 515 | FOR (i, 0, 12) { |
| 516 | BLAKE2_ROUND(i); |
| 517 | } |
| 518 | #else |
| 519 | BLAKE2_ROUND(0); BLAKE2_ROUND(1); BLAKE2_ROUND(2); BLAKE2_ROUND(3); |
| 520 | BLAKE2_ROUND(4); BLAKE2_ROUND(5); BLAKE2_ROUND(6); BLAKE2_ROUND(7); |
| 521 | BLAKE2_ROUND(8); BLAKE2_ROUND(9); BLAKE2_ROUND(10); BLAKE2_ROUND(11); |
| 522 | #endif |
| 523 | |
| 524 | // Update hash |
| 525 | ctx->hash[0] ^= v0 ^ v8; ctx->hash[1] ^= v1 ^ v9; |
| 526 | ctx->hash[2] ^= v2 ^ v10; ctx->hash[3] ^= v3 ^ v11; |
| 527 | ctx->hash[4] ^= v4 ^ v12; ctx->hash[5] ^= v5 ^ v13; |
| 528 | ctx->hash[6] ^= v6 ^ v14; ctx->hash[7] ^= v7 ^ v15; |
| 529 | } |
| 530 | |
| 531 | void crypto_blake2b_keyed_init(crypto_blake2b_ctx *ctx, size_t hash_size, |
| 532 | const u8 *key, size_t key_size) |
| 533 | { |
| 534 | // Initial hash |
| 535 | COPY(ctx->hash, iv, 8); |
| 536 | ctx->hash[0] ^= 0x01010000 ^ (key_size << 8) ^ hash_size; |
| 537 | |
| 538 | ctx->input_offset[0] = 0; // beginning of the input, no offset |
| 539 | ctx->input_offset[1] = 0; // beginning of the input, no offset |
| 540 | ctx->hash_size = hash_size; |
| 541 | ctx->input_idx = 0; |
| 542 | ZERO(ctx->input, 16); |
| 543 | |
| 544 | // If there is a key, the first block is that key (padded with zeroes) |
| 545 | if (key_size > 0) { |
| 546 | u8 key_block[128] = {0}; |
| 547 | COPY(key_block, key, key_size); |
| 548 | // Same as calling crypto_blake2b_update(ctx, key_block , 128) |
| 549 | load64_le_buf(ctx->input, key_block, 16); |
| 550 | ctx->input_idx = 128; |
| 551 | WIPE_BUFFER(key_block); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | void crypto_blake2b_init(crypto_blake2b_ctx *ctx, size_t hash_size) |
| 556 | { |
| 557 | crypto_blake2b_keyed_init(ctx, hash_size, 0, 0); |
| 558 | } |
| 559 | |
| 560 | void crypto_blake2b_update(crypto_blake2b_ctx *ctx, |
| 561 | const u8 *message, size_t message_size) |
| 562 | { |
| 563 | // Avoid undefined NULL pointer increments with empty messages |
| 564 | if (message_size == 0) { |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | // Align with word boundaries |
| 569 | if ((ctx->input_idx & 7) != 0) { |
| 570 | size_t nb_bytes = MIN(gap(ctx->input_idx, 8), message_size); |
| 571 | size_t word = ctx->input_idx >> 3; |
| 572 | size_t byte = ctx->input_idx & 7; |
| 573 | FOR (i, 0, nb_bytes) { |
| 574 | ctx->input[word] |= (u64)message[i] << ((byte + i) << 3); |
| 575 | } |
| 576 | ctx->input_idx += nb_bytes; |
| 577 | message += nb_bytes; |
| 578 | message_size -= nb_bytes; |
| 579 | } |
| 580 | |
| 581 | // Align with block boundaries (faster than byte by byte) |
| 582 | if ((ctx->input_idx & 127) != 0) { |
| 583 | size_t nb_words = MIN(gap(ctx->input_idx, 128), message_size) >> 3; |
| 584 | load64_le_buf(ctx->input + (ctx->input_idx >> 3), message, nb_words); |
| 585 | ctx->input_idx += nb_words << 3; |
| 586 | message += nb_words << 3; |
| 587 | message_size -= nb_words << 3; |
| 588 | } |
| 589 | |
| 590 | // Process block by block |
| 591 | size_t nb_blocks = message_size >> 7; |
| 592 | FOR (i, 0, nb_blocks) { |
| 593 | if (ctx->input_idx == 128) { |
| 594 | blake2b_compress(ctx, 0); |
| 595 | } |
| 596 | load64_le_buf(ctx->input, message, 16); |
| 597 | message += 128; |
| 598 | ctx->input_idx = 128; |
| 599 | } |
| 600 | message_size &= 127; |
| 601 | |
| 602 | if (message_size != 0) { |
| 603 | // Compress block & flush input buffer as needed |
| 604 | if (ctx->input_idx == 128) { |
| 605 | blake2b_compress(ctx, 0); |
| 606 | ctx->input_idx = 0; |
| 607 | } |
| 608 | if (ctx->input_idx == 0) { |
| 609 | ZERO(ctx->input, 16); |
| 610 | } |
| 611 | // Fill remaining words (faster than byte by byte) |
| 612 | size_t nb_words = message_size >> 3; |
| 613 | load64_le_buf(ctx->input, message, nb_words); |
| 614 | ctx->input_idx += nb_words << 3; |
| 615 | message += nb_words << 3; |
| 616 | message_size -= nb_words << 3; |
| 617 | |
| 618 | // Fill remaining bytes |
| 619 | FOR (i, 0, message_size) { |
| 620 | size_t word = ctx->input_idx >> 3; |
| 621 | size_t byte = ctx->input_idx & 7; |
| 622 | ctx->input[word] |= (u64)message[i] << (byte << 3); |
| 623 | ctx->input_idx++; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | void crypto_blake2b_final(crypto_blake2b_ctx *ctx, u8 *hash) |
| 629 | { |
| 630 | blake2b_compress(ctx, 1); // compress the last block |
| 631 | size_t hash_size = MIN(ctx->hash_size, 64); |
| 632 | size_t nb_words = hash_size >> 3; |
| 633 | store64_le_buf(hash, ctx->hash, nb_words); |
| 634 | FOR (i, nb_words << 3, hash_size) { |
| 635 | hash[i] = (ctx->hash[i >> 3] >> (8 * (i & 7))) & 0xff; |
| 636 | } |
| 637 | WIPE_CTX(ctx); |
| 638 | } |
| 639 | |
| 640 | void crypto_blake2b_keyed(u8 *hash, size_t hash_size, |
| 641 | const u8 *key, size_t key_size, |
| 642 | const u8 *message, size_t message_size) |
| 643 | { |
| 644 | crypto_blake2b_ctx ctx; |
| 645 | crypto_blake2b_keyed_init(&ctx, hash_size, key, key_size); |
| 646 | crypto_blake2b_update (&ctx, message, message_size); |
| 647 | crypto_blake2b_final (&ctx, hash); |
| 648 | } |
| 649 | |
| 650 | void crypto_blake2b(u8 *hash, size_t hash_size, const u8 *msg, size_t msg_size) |
| 651 | { |
| 652 | crypto_blake2b_keyed(hash, hash_size, 0, 0, msg, msg_size); |
| 653 | } |
| 654 | |
| 655 | ////////////// |
| 656 | /// Argon2 /// |
| 657 | ////////////// |
| 658 | // references to R, Z, Q etc. come from the spec |
| 659 | |
| 660 | // Argon2 operates on 1024 byte blocks. |
| 661 | typedef struct { u64 a[128]; } blk; |
| 662 | |
| 663 | // Update a BLAKE2 hash with a 32 bit word, little endian. |
| 664 | static void blake_update_32(crypto_blake2b_ctx *ctx, u32 input) |
| 665 | { |
| 666 | u8 buf[4]; |
| 667 | store32_le(buf, input); |
| 668 | crypto_blake2b_update(ctx, buf, 4); |
| 669 | WIPE_BUFFER(buf); |
| 670 | } |
| 671 | |
| 672 | static void blake_update_32_buf(crypto_blake2b_ctx *ctx, |
| 673 | const u8 *buf, u32 size) |
| 674 | { |
| 675 | blake_update_32(ctx, size); |
| 676 | crypto_blake2b_update(ctx, buf, size); |
| 677 | } |
| 678 | |
| 679 | |
| 680 | static void copy_block(blk *o,const blk*in){FOR(i, 0, 128) o->a[i] = in->a[i];} |
| 681 | static void xor_block(blk *o,const blk*in){FOR(i, 0, 128) o->a[i] ^= in->a[i];} |
| 682 | |
| 683 | // Hash with a virtually unlimited digest size. |
| 684 | // Doesn't extract more entropy than the base hash function. |
| 685 | // Mainly used for filling a whole kilobyte block with pseudo-random bytes. |
| 686 | // (One could use a stream cipher with a seed hash as the key, but |
| 687 | // this would introduce another dependency —and point of failure.) |
| 688 | static void extended_hash(u8 *digest, u32 digest_size, |
| 689 | const u8 *input , u32 input_size) |
| 690 | { |
| 691 | crypto_blake2b_ctx ctx; |
| 692 | crypto_blake2b_init (&ctx, MIN(digest_size, 64)); |
| 693 | blake_update_32 (&ctx, digest_size); |
| 694 | crypto_blake2b_update(&ctx, input, input_size); |
| 695 | crypto_blake2b_final (&ctx, digest); |
| 696 | |
| 697 | if (digest_size > 64) { |
| 698 | // The conversion to u64 avoids integer overflow on |
| 699 | // ludicrously big hash sizes. |
| 700 | u32 r = (u32)(((u64)digest_size + 31) >> 5) - 2; |
| 701 | u32 i = 1; |
| 702 | u32 in = 0; |
| 703 | u32 out = 32; |
| 704 | while (i < r) { |
| 705 | // Input and output overlap. This is intentional |
| 706 | crypto_blake2b(digest + out, 64, digest + in, 64); |
| 707 | i += 1; |
| 708 | in += 32; |
| 709 | out += 32; |
| 710 | } |
| 711 | crypto_blake2b(digest + out, digest_size - (32 * r), digest + in , 64); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | #define LSB(x) ((u64)(u32)x) |
| 716 | #define G(a, b, c, d) \ |
| 717 | a += b + ((LSB(a) * LSB(b)) << 1); d ^= a; d = rotr64(d, 32); \ |
| 718 | c += d + ((LSB(c) * LSB(d)) << 1); b ^= c; b = rotr64(b, 24); \ |
| 719 | a += b + ((LSB(a) * LSB(b)) << 1); d ^= a; d = rotr64(d, 16); \ |
| 720 | c += d + ((LSB(c) * LSB(d)) << 1); b ^= c; b = rotr64(b, 63) |
| 721 | #define ROUND(v0, v1, v2, v3, v4, v5, v6, v7, \ |
| 722 | v8, v9, v10, v11, v12, v13, v14, v15) \ |
| 723 | G(v0, v4, v8, v12); G(v1, v5, v9, v13); \ |
| 724 | G(v2, v6, v10, v14); G(v3, v7, v11, v15); \ |
| 725 | G(v0, v5, v10, v15); G(v1, v6, v11, v12); \ |
| 726 | G(v2, v7, v8, v13); G(v3, v4, v9, v14) |
| 727 | |
| 728 | // Core of the compression function G. Computes Z from R in place. |
| 729 | static void g_rounds(blk *b) |
| 730 | { |
| 731 | // Row rounds |
| 732 | for (int i = 0; i < 128; i += 16) { |
| 733 | ROUND(b->a[i ], b->a[i+ 1], b->a[i+ 2], b->a[i+ 3], |
| 734 | b->a[i+ 4], b->a[i+ 5], b->a[i+ 6], b->a[i+ 7], |
| 735 | b->a[i+ 8], b->a[i+ 9], b->a[i+10], b->a[i+11], |
| 736 | b->a[i+12], b->a[i+13], b->a[i+14], b->a[i+15]); |
| 737 | } |
| 738 | // Column rounds |
| 739 | for (int i = 0; i < 16; i += 2) { |
| 740 | ROUND(b->a[i ], b->a[i+ 1], b->a[i+ 16], b->a[i+ 17], |
| 741 | b->a[i+32], b->a[i+33], b->a[i+ 48], b->a[i+ 49], |
| 742 | b->a[i+64], b->a[i+65], b->a[i+ 80], b->a[i+ 81], |
| 743 | b->a[i+96], b->a[i+97], b->a[i+112], b->a[i+113]); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | const crypto_argon2_extras crypto_argon2_no_extras = { 0, 0, 0, 0 }; |
| 748 | |
| 749 | void crypto_argon2_init(crypto_argon2_ctx *ctx, u32 hash_size, void *work_area, |
| 750 | crypto_argon2_config config, |
| 751 | crypto_argon2_inputs inputs, |
| 752 | crypto_argon2_extras extras) |
| 753 | { |
| 754 | const u32 segment_size = config.nb_blocks / config.nb_lanes / 4; |
| 755 | const u32 lane_size = segment_size * 4; |
| 756 | |
| 757 | // Work area seen as blocks (must be suitably aligned) |
| 758 | blk *blocks = (blk*)work_area; |
| 759 | { |
| 760 | u8 initial_hash[72]; // 64 bytes plus 2 words for future hashes |
| 761 | crypto_blake2b_ctx ctx; |
| 762 | crypto_blake2b_init (&ctx, 64); |
| 763 | blake_update_32 (&ctx, config.nb_lanes ); // p: number of "threads" |
| 764 | blake_update_32 (&ctx, hash_size); |
| 765 | blake_update_32 (&ctx, config.nb_blocks); |
| 766 | blake_update_32 (&ctx, config.nb_passes); |
| 767 | blake_update_32 (&ctx, 0x13); // v: version number |
| 768 | blake_update_32 (&ctx, config.algorithm); // y: Argon2i, Argon2d... |
| 769 | blake_update_32_buf (&ctx, inputs.pass, inputs.pass_size); |
| 770 | blake_update_32_buf (&ctx, inputs.salt, inputs.salt_size); |
| 771 | blake_update_32_buf (&ctx, extras.key, extras.key_size); |
| 772 | blake_update_32_buf (&ctx, extras.ad, extras.ad_size); |
| 773 | crypto_blake2b_final(&ctx, initial_hash); // Fill 64 first bytes only |
| 774 | |
| 775 | // Fill the first 2 blocks of each lane. |
| 776 | // Could be done in parallel, but it would complicate the API. |
| 777 | u8 hash_area[1024]; |
| 778 | FOR_T(u32, l, 0, config.nb_lanes) { |
| 779 | FOR_T(u32, i, 0, 2) { |
| 780 | store32_le(initial_hash + 64, i); // first additional word |
| 781 | store32_le(initial_hash + 68, l); // second additional word |
| 782 | extended_hash(hash_area, 1024, initial_hash, 72); |
| 783 | load64_le_buf(blocks[l * lane_size + i].a, hash_area, 128); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | WIPE_BUFFER(initial_hash); |
| 788 | WIPE_BUFFER(hash_area); |
| 789 | } |
| 790 | |
| 791 | // Prepare context for further iterations |
| 792 | ctx->config = config; |
| 793 | ctx->work_area = work_area; |
| 794 | ctx->hash_size = hash_size; |
| 795 | } |
| 796 | |
| 797 | void crypto_argon2_segment(const crypto_argon2_ctx *ctx, |
| 798 | u32 pass, u32 slice, u32 lane) |
| 799 | { |
| 800 | const u32 segment_size = ctx->config.nb_blocks / ctx->config.nb_lanes / 4; |
| 801 | const u32 lane_size = segment_size * 4; |
| 802 | const u32 nb_blocks = lane_size * ctx->config.nb_lanes; // rounding down |
| 803 | const u32 pass_offset = pass == 0 && slice == 0 ? 2 : 0; |
| 804 | const u32 slice_offset = slice * segment_size; |
| 805 | const int argon2_i = ctx->config.algorithm == CRYPTO_ARGON2_I; |
| 806 | const int argon2_id = ctx->config.algorithm == CRYPTO_ARGON2_ID; |
| 807 | const int constant_time = argon2_i || (argon2_id && slice < 2 && pass == 0); |
| 808 | blk *blocks = (blk*)ctx->work_area; |
| 809 | blk tmp; |
| 810 | |
| 811 | blk index_block; |
| 812 | u32 index_ctr = 1; |
| 813 | FOR_T (u32, block, pass_offset, segment_size) { |
| 814 | // Current and previous blocks |
| 815 | u32 lane_offset = lane * lane_size; |
| 816 | blk *segment_start = blocks + lane_offset + slice_offset; |
| 817 | blk *current = segment_start + block; |
| 818 | blk *previous = |
| 819 | block == 0 && slice_offset == 0 |
| 820 | ? segment_start + lane_size - 1 |
| 821 | : segment_start + block - 1; |
| 822 | |
| 823 | u64 index_seed; |
| 824 | if (constant_time) { |
| 825 | if (block == pass_offset || (block % 128) == 0) { |
| 826 | // Fill or refresh deterministic indices block |
| 827 | |
| 828 | // Seed the beginning of the block... |
| 829 | ZERO(index_block.a, 128); |
| 830 | index_block.a[0] = pass; |
| 831 | index_block.a[1] = lane; |
| 832 | index_block.a[2] = slice; |
| 833 | index_block.a[3] = nb_blocks; |
| 834 | index_block.a[4] = ctx->config.nb_passes; |
| 835 | index_block.a[5] = ctx->config.algorithm; |
| 836 | index_block.a[6] = index_ctr; |
| 837 | index_ctr++; |
| 838 | |
| 839 | // ... then shuffle it |
| 840 | copy_block(&tmp, &index_block); |
| 841 | g_rounds (&index_block); |
| 842 | xor_block (&index_block, &tmp); |
| 843 | copy_block(&tmp, &index_block); |
| 844 | g_rounds (&index_block); |
| 845 | xor_block (&index_block, &tmp); |
| 846 | } |
| 847 | index_seed = index_block.a[block % 128]; |
| 848 | } else { |
| 849 | index_seed = previous->a[0]; |
| 850 | } |
| 851 | |
| 852 | // Establish the reference set. *Approximately* comprises: |
| 853 | // - The last 3 slices (if they exist yet) |
| 854 | // - The already constructed blocks in the current segment |
| 855 | u32 next_slice = ((slice + 1) % 4) * segment_size; |
| 856 | u32 window_start = pass == 0 ? 0 : next_slice; |
| 857 | u32 nb_segments = pass == 0 ? slice : 3; |
| 858 | u32 ref_lane = |
| 859 | pass == 0 && slice == 0 |
| 860 | ? lane |
| 861 | : (u32)(index_seed >> 32) % ctx->config.nb_lanes; |
| 862 | u32 window_size = |
| 863 | nb_segments * segment_size + |
| 864 | (ref_lane == lane ? block-1 : |
| 865 | block == 0 ? (u32)-1 : 0); |
| 866 | |
| 867 | // Find reference block |
| 868 | u64 j1 = index_seed & 0xffffffff; // block selector |
| 869 | u64 x = (j1 * j1) >> 32; |
| 870 | u64 y = (window_size * x) >> 32; |
| 871 | u64 z = (window_size - 1) - y; |
| 872 | u32 ref = (u32)((window_start + z) % lane_size); |
| 873 | u32 index = ref_lane * lane_size + ref; |
| 874 | blk *reference = blocks + index; |
| 875 | |
| 876 | // Shuffle previous & reference block into the current one |
| 877 | copy_block(&tmp, previous); |
| 878 | xor_block (&tmp, reference); |
| 879 | if (pass == 0) { copy_block(current, &tmp); } |
| 880 | else { xor_block (current, &tmp); } |
| 881 | g_rounds (&tmp); |
| 882 | xor_block (current, &tmp); |
| 883 | } |
| 884 | |
| 885 | // Wipe temporary block |
| 886 | volatile u64* p = tmp.a; |
| 887 | ZERO(p, 128); |
| 888 | } |
| 889 | |
| 890 | void crypto_argon2_final(const crypto_argon2_ctx *ctx, u8 *hash) |
| 891 | { |
| 892 | const u32 segment_size = ctx->config.nb_blocks / ctx->config.nb_lanes / 4; |
| 893 | const u32 lane_size = segment_size * 4; |
| 894 | blk *blocks = (blk*)ctx->work_area; |
| 895 | |
| 896 | // XOR last blocks of each lane |
| 897 | blk *last_block = blocks + lane_size - 1; |
| 898 | FOR_T (u32, lane, 1, ctx->config.nb_lanes) { |
| 899 | blk *next_block = last_block + lane_size; |
| 900 | xor_block(next_block, last_block); |
| 901 | last_block = next_block; |
| 902 | } |
| 903 | |
| 904 | // Serialize last block (copy is necessary to allow aliasing inputs) |
| 905 | u8 final_block[1024]; |
| 906 | store64_le_buf(final_block, last_block->a, 128); |
| 907 | |
| 908 | // Wipe work area |
| 909 | volatile u64*p = (u64*)ctx->work_area; |
| 910 | ZERO(p, 128 * ctx->config.nb_blocks); |
| 911 | |
| 912 | // Hash the very last block with H' into the output hash |
| 913 | extended_hash(hash, ctx->hash_size, final_block, 1024); |
| 914 | WIPE_BUFFER(final_block); |
| 915 | } |
| 916 | |
| 917 | void crypto_argon2(u8 *hash, u32 hash_size, void *work_area, |
| 918 | crypto_argon2_config config, |
| 919 | crypto_argon2_inputs inputs, |
| 920 | crypto_argon2_extras extras) |
| 921 | { |
| 922 | crypto_argon2_ctx ctx; |
| 923 | crypto_argon2_init(&ctx, hash_size, work_area, config, inputs, extras); |
| 924 | |
| 925 | FOR_T (u32, pass, 0, config.nb_passes) { |
| 926 | FOR_T (u32, slice, 0, 4) { |
| 927 | // May be done in parallel, one thread per lane. |
| 928 | FOR_T(u32, lane, 0, config.nb_lanes) { |
| 929 | crypto_argon2_segment(&ctx, pass, slice, lane); |
| 930 | } |
| 931 | // Sync point: wait for all lanes to complete. |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | crypto_argon2_final(&ctx, hash); |
| 936 | } |
| 937 | |
| 938 | //////////////////////////////////// |
| 939 | /// Arithmetic modulo 2^255 - 19 /// |
| 940 | //////////////////////////////////// |
| 941 | // Originally taken from SUPERCOP's ref10 implementation. |
| 942 | // A bit bigger than TweetNaCl, over 4 times faster. |
| 943 | |
| 944 | // field element |
| 945 | typedef i32 fe[10]; |
| 946 | |
| 947 | // field constants |
| 948 | // |
| 949 | // fe_one : 1 |
| 950 | // sqrtm1 : sqrt(-1) |
| 951 | // d : -121665 / 121666 |
| 952 | // D2 : 2 * -121665 / 121666 |
| 953 | // lop_x, lop_y: low order point in Edwards coordinates |
| 954 | // ufactor : -sqrt(-1) * 2 |
| 955 | // A2 : 486662^2 (A squared) |
| 956 | static const fe fe_one = {1}; |
| 957 | static const fe sqrtm1 = { |
| 958 | -32595792, -7943725, 9377950, 3500415, 12389472, |
| 959 | -272473, -25146209, -2005654, 326686, 11406482, |
| 960 | }; |
| 961 | static const fe d = { |
| 962 | -10913610, 13857413, -15372611, 6949391, 114729, |
| 963 | -8787816, -6275908, -3247719, -18696448, -12055116, |
| 964 | }; |
| 965 | static const fe D2 = { |
| 966 | -21827239, -5839606, -30745221, 13898782, 229458, |
| 967 | 15978800, -12551817, -6495438, 29715968, 9444199, |
| 968 | }; |
| 969 | static const fe lop_x = { |
| 970 | 21352778, 5345713, 4660180, -8347857, 24143090, |
| 971 | 14568123, 30185756, -12247770, -33528939, 8345319, |
| 972 | }; |
| 973 | static const fe lop_y = { |
| 974 | -6952922, -1265500, 6862341, -7057498, -4037696, |
| 975 | -5447722, 31680899, -15325402, -19365852, 1569102, |
| 976 | }; |
| 977 | static const fe ufactor = { |
| 978 | -1917299, 15887451, -18755900, -7000830, -24778944, |
| 979 | 544946, -16816446, 4011309, -653372, 10741468, |
| 980 | }; |
| 981 | static const fe A2 = { |
| 982 | 12721188, 3529, 0, 0, 0, 0, 0, 0, 0, 0, |
| 983 | }; |
| 984 | |
| 985 | static void fe_0(fe h) { ZERO(h , 10); } |
| 986 | static void fe_1(fe h) { h[0] = 1; ZERO(h+1, 9); } |
| 987 | |
| 988 | static void fe_copy(fe h,const fe f ){FOR(i,0,10) h[i] = f[i]; } |
| 989 | static void fe_neg (fe h,const fe f ){FOR(i,0,10) h[i] = -f[i]; } |
| 990 | static void fe_add (fe h,const fe f,const fe g){FOR(i,0,10) h[i] = f[i] + g[i];} |
| 991 | static void fe_sub (fe h,const fe f,const fe g){FOR(i,0,10) h[i] = f[i] - g[i];} |
| 992 | |
| 993 | // Some compilers, when inlining fe_cswap() or fe_ccopy(), may introduce |
| 994 | // a timing leak. It happens when it notices `b` has only 2 possible |
| 995 | // values, and either replace the arithmetic by a secret dependent |
| 996 | // branch, or (as has been observed), swap pointers instead of values, |
| 997 | // which introduces a secret dependent index. |
| 998 | // |
| 999 | // We apply two mitigations here: |
| 1000 | // - Add `volatile` in the mask declaration. |
| 1001 | // - Unroll the copy loop (costs couple hundred bytes of binary code). |
| 1002 | // |
| 1003 | // As of June 2026, those mitigation work when applied separately or |
| 1004 | // together. Applying them both is currently overkill, but may help |
| 1005 | // delay the day compilers grow clever enough to defeat it. (The true |
| 1006 | // fix is in the semantics of the language itself: C currently has no |
| 1007 | // way to specify constant time code). |
| 1008 | // |
| 1009 | // Note (Loup): as of June 2026, the problem has yet to surface in |
| 1010 | // fe_cswap(), but since this is almost the same code as fe_ccopy() I |
| 1011 | // believe it is more prudent to apply the precaution there too. |
| 1012 | static void fe_cswap(fe f, fe g, int b) |
| 1013 | { |
| 1014 | volatile i32 mask = -b; // -1 = 0xffffffff |
| 1015 | i32 x0 = (f[0] ^ g[0]) & mask; f[0] = f[0] ^ x0; g[0] = g[0] ^ x0; |
| 1016 | i32 x1 = (f[1] ^ g[1]) & mask; f[1] = f[1] ^ x1; g[1] = g[1] ^ x1; |
| 1017 | i32 x2 = (f[2] ^ g[2]) & mask; f[2] = f[2] ^ x2; g[2] = g[2] ^ x2; |
| 1018 | i32 x3 = (f[3] ^ g[3]) & mask; f[3] = f[3] ^ x3; g[3] = g[3] ^ x3; |
| 1019 | i32 x4 = (f[4] ^ g[4]) & mask; f[4] = f[4] ^ x4; g[4] = g[4] ^ x4; |
| 1020 | i32 x5 = (f[5] ^ g[5]) & mask; f[5] = f[5] ^ x5; g[5] = g[5] ^ x5; |
| 1021 | i32 x6 = (f[6] ^ g[6]) & mask; f[6] = f[6] ^ x6; g[6] = g[6] ^ x6; |
| 1022 | i32 x7 = (f[7] ^ g[7]) & mask; f[7] = f[7] ^ x7; g[7] = g[7] ^ x7; |
| 1023 | i32 x8 = (f[8] ^ g[8]) & mask; f[8] = f[8] ^ x8; g[8] = g[8] ^ x8; |
| 1024 | i32 x9 = (f[9] ^ g[9]) & mask; f[9] = f[9] ^ x9; g[9] = g[9] ^ x9; |
| 1025 | } |
| 1026 | |
| 1027 | static void fe_ccopy(fe f, const fe g, int b) |
| 1028 | { |
| 1029 | volatile i32 mask = -b; // -1 = 0xffffffff |
| 1030 | i32 x0 = (f[0] ^ g[0]) & mask; f[0] = f[0] ^ x0; |
| 1031 | i32 x1 = (f[1] ^ g[1]) & mask; f[1] = f[1] ^ x1; |
| 1032 | i32 x2 = (f[2] ^ g[2]) & mask; f[2] = f[2] ^ x2; |
| 1033 | i32 x3 = (f[3] ^ g[3]) & mask; f[3] = f[3] ^ x3; |
| 1034 | i32 x4 = (f[4] ^ g[4]) & mask; f[4] = f[4] ^ x4; |
| 1035 | i32 x5 = (f[5] ^ g[5]) & mask; f[5] = f[5] ^ x5; |
| 1036 | i32 x6 = (f[6] ^ g[6]) & mask; f[6] = f[6] ^ x6; |
| 1037 | i32 x7 = (f[7] ^ g[7]) & mask; f[7] = f[7] ^ x7; |
| 1038 | i32 x8 = (f[8] ^ g[8]) & mask; f[8] = f[8] ^ x8; |
| 1039 | i32 x9 = (f[9] ^ g[9]) & mask; f[9] = f[9] ^ x9; |
| 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | // Signed carry propagation |
| 1044 | // ------------------------ |
| 1045 | // |
| 1046 | // Let t be a number. It can be uniquely decomposed thus: |
| 1047 | // |
| 1048 | // t = h*2^26 + l |
| 1049 | // such that -2^25 <= l < 2^25 |
| 1050 | // |
| 1051 | // Let c = (t + 2^25) / 2^26 (rounded down) |
| 1052 | // c = (h*2^26 + l + 2^25) / 2^26 (rounded down) |
| 1053 | // c = h + (l + 2^25) / 2^26 (rounded down) |
| 1054 | // c = h (exactly) |
| 1055 | // Because 0 <= l + 2^25 < 2^26 |
| 1056 | // |
| 1057 | // Let u = t - c*2^26 |
| 1058 | // u = h*2^26 + l - h*2^26 |
| 1059 | // u = l |
| 1060 | // Therefore, -2^25 <= u < 2^25 |
| 1061 | // |
| 1062 | // Additionally, if |t| < x, then |h| < x/2^26 (rounded down) |
| 1063 | // |
| 1064 | // Notations: |
| 1065 | // - In C, 1<<25 means 2^25. |
| 1066 | // - In C, x>>25 means floor(x / (2^25)). |
| 1067 | // - All of the above applies with 25 & 24 as well as 26 & 25. |
| 1068 | // |
| 1069 | // |
| 1070 | // Note on negative right shifts |
| 1071 | // ----------------------------- |
| 1072 | // |
| 1073 | // In C, x >> n, where x is a negative integer, is implementation |
| 1074 | // defined. In practice, all platforms do arithmetic shift, which is |
| 1075 | // equivalent to division by 2^26, rounded down. Some compilers, like |
| 1076 | // GCC, even guarantee it. |
| 1077 | // |
| 1078 | // If we ever stumble upon a platform that does not propagate the sign |
| 1079 | // bit (we won't), visible failures will show at the slightest test, and |
| 1080 | // the signed shifts can be replaced by the following: |
| 1081 | // |
| 1082 | // typedef struct { i64 x:39; } s25; |
| 1083 | // typedef struct { i64 x:38; } s26; |
| 1084 | // i64 shift25(i64 x) { s25 s; s.x = ((u64)x)>>25; return s.x; } |
| 1085 | // i64 shift26(i64 x) { s26 s; s.x = ((u64)x)>>26; return s.x; } |
| 1086 | // |
| 1087 | // Current compilers cannot optimise this, causing a 30% drop in |
| 1088 | // performance. Fairly expensive for something that never happens. |
| 1089 | // |
| 1090 | // |
| 1091 | // Precondition |
| 1092 | // ------------ |
| 1093 | // |
| 1094 | // |t0| < 2^63 |
| 1095 | // |t1|..|t9| < 2^62 |
| 1096 | // |
| 1097 | // Algorithm |
| 1098 | // --------- |
| 1099 | // c = t0 + 2^25 / 2^26 -- |c| <= 2^36 |
| 1100 | // t0 -= c * 2^26 -- |t0| <= 2^25 |
| 1101 | // t1 += c -- |t1| <= 2^63 |
| 1102 | // |
| 1103 | // c = t4 + 2^25 / 2^26 -- |c| <= 2^36 |
| 1104 | // t4 -= c * 2^26 -- |t4| <= 2^25 |
| 1105 | // t5 += c -- |t5| <= 2^63 |
| 1106 | // |
| 1107 | // c = t1 + 2^24 / 2^25 -- |c| <= 2^38 |
| 1108 | // t1 -= c * 2^25 -- |t1| <= 2^24 |
| 1109 | // t2 += c -- |t2| <= 2^63 |
| 1110 | // |
| 1111 | // c = t5 + 2^24 / 2^25 -- |c| <= 2^38 |
| 1112 | // t5 -= c * 2^25 -- |t5| <= 2^24 |
| 1113 | // t6 += c -- |t6| <= 2^63 |
| 1114 | // |
| 1115 | // c = t2 + 2^25 / 2^26 -- |c| <= 2^37 |
| 1116 | // t2 -= c * 2^26 -- |t2| <= 2^25 < 1.1 * 2^25 (final t2) |
| 1117 | // t3 += c -- |t3| <= 2^63 |
| 1118 | // |
| 1119 | // c = t6 + 2^25 / 2^26 -- |c| <= 2^37 |
| 1120 | // t6 -= c * 2^26 -- |t6| <= 2^25 < 1.1 * 2^25 (final t6) |
| 1121 | // t7 += c -- |t7| <= 2^63 |
| 1122 | // |
| 1123 | // c = t3 + 2^24 / 2^25 -- |c| <= 2^38 |
| 1124 | // t3 -= c * 2^25 -- |t3| <= 2^24 < 1.1 * 2^24 (final t3) |
| 1125 | // t4 += c -- |t4| <= 2^25 + 2^38 < 2^39 |
| 1126 | // |
| 1127 | // c = t7 + 2^24 / 2^25 -- |c| <= 2^38 |
| 1128 | // t7 -= c * 2^25 -- |t7| <= 2^24 < 1.1 * 2^24 (final t7) |
| 1129 | // t8 += c -- |t8| <= 2^63 |
| 1130 | // |
| 1131 | // c = t4 + 2^25 / 2^26 -- |c| <= 2^13 |
| 1132 | // t4 -= c * 2^26 -- |t4| <= 2^25 < 1.1 * 2^25 (final t4) |
| 1133 | // t5 += c -- |t5| <= 2^24 + 2^13 < 1.1 * 2^24 (final t5) |
| 1134 | // |
| 1135 | // c = t8 + 2^25 / 2^26 -- |c| <= 2^37 |
| 1136 | // t8 -= c * 2^26 -- |t8| <= 2^25 < 1.1 * 2^25 (final t8) |
| 1137 | // t9 += c -- |t9| <= 2^63 |
| 1138 | // |
| 1139 | // c = t9 + 2^24 / 2^25 -- |c| <= 2^38 |
| 1140 | // t9 -= c * 2^25 -- |t9| <= 2^24 < 1.1 * 2^24 (final t9) |
| 1141 | // t0 += c * 19 -- |t0| <= 2^25 + 2^38*19 < 2^44 |
| 1142 | // |
| 1143 | // c = t0 + 2^25 / 2^26 -- |c| <= 2^18 |
| 1144 | // t0 -= c * 2^26 -- |t0| <= 2^25 < 1.1 * 2^25 (final t0) |
| 1145 | // t1 += c -- |t1| <= 2^24 + 2^18 < 1.1 * 2^24 (final t1) |
| 1146 | // |
| 1147 | // Postcondition |
| 1148 | // ------------- |
| 1149 | // |t0|, |t2|, |t4|, |t6|, |t8| < 1.1 * 2^25 |
| 1150 | // |t1|, |t3|, |t5|, |t7|, |t9| < 1.1 * 2^24 |
| 1151 | #define FE_CARRY \ |
| 1152 | i64 c; \ |
| 1153 | c = (t0 + ((i64)1<<25)) >> 26; t0 -= c * ((i64)1 << 26); t1 += c; \ |
| 1154 | c = (t4 + ((i64)1<<25)) >> 26; t4 -= c * ((i64)1 << 26); t5 += c; \ |
| 1155 | c = (t1 + ((i64)1<<24)) >> 25; t1 -= c * ((i64)1 << 25); t2 += c; \ |
| 1156 | c = (t5 + ((i64)1<<24)) >> 25; t5 -= c * ((i64)1 << 25); t6 += c; \ |
| 1157 | c = (t2 + ((i64)1<<25)) >> 26; t2 -= c * ((i64)1 << 26); t3 += c; \ |
| 1158 | c = (t6 + ((i64)1<<25)) >> 26; t6 -= c * ((i64)1 << 26); t7 += c; \ |
| 1159 | c = (t3 + ((i64)1<<24)) >> 25; t3 -= c * ((i64)1 << 25); t4 += c; \ |
| 1160 | c = (t7 + ((i64)1<<24)) >> 25; t7 -= c * ((i64)1 << 25); t8 += c; \ |
| 1161 | c = (t4 + ((i64)1<<25)) >> 26; t4 -= c * ((i64)1 << 26); t5 += c; \ |
| 1162 | c = (t8 + ((i64)1<<25)) >> 26; t8 -= c * ((i64)1 << 26); t9 += c; \ |
| 1163 | c = (t9 + ((i64)1<<24)) >> 25; t9 -= c * ((i64)1 << 25); t0 += c * 19; \ |
| 1164 | c = (t0 + ((i64)1<<25)) >> 26; t0 -= c * ((i64)1 << 26); t1 += c; \ |
| 1165 | h[0]=(i32)t0; h[1]=(i32)t1; h[2]=(i32)t2; h[3]=(i32)t3; h[4]=(i32)t4; \ |
| 1166 | h[5]=(i32)t5; h[6]=(i32)t6; h[7]=(i32)t7; h[8]=(i32)t8; h[9]=(i32)t9 |
| 1167 | |
| 1168 | // Decode a field element from a byte buffer. |
| 1169 | // `nb_mask` specifies how many bits we ignore. |
| 1170 | // Traditionally we ignore 1. It's useful for EdDSA, |
| 1171 | // which uses that bit to denote the sign of x. |
| 1172 | // Elligator however uses positive representatives, |
| 1173 | // which means ignoring 2 bits instead. |
| 1174 | static void fe_frombytes_mask(fe h, const u8 s[32], unsigned nb_mask) |
| 1175 | { |
| 1176 | u32 mask = 0xffffff >> nb_mask; |
| 1177 | i64 t0 = load32_le(s); // t0 < 2^32 |
| 1178 | i64 t1 = load24_le(s + 4) << 6; // t1 < 2^30 |
| 1179 | i64 t2 = load24_le(s + 7) << 5; // t2 < 2^29 |
| 1180 | i64 t3 = load24_le(s + 10) << 3; // t3 < 2^27 |
| 1181 | i64 t4 = load24_le(s + 13) << 2; // t4 < 2^26 |
| 1182 | i64 t5 = load32_le(s + 16); // t5 < 2^32 |
| 1183 | i64 t6 = load24_le(s + 20) << 7; // t6 < 2^31 |
| 1184 | i64 t7 = load24_le(s + 23) << 5; // t7 < 2^29 |
| 1185 | i64 t8 = load24_le(s + 26) << 4; // t8 < 2^28 |
| 1186 | i64 t9 = (load24_le(s + 29) & mask) << 2; // t9 < 2^25 |
| 1187 | FE_CARRY; // Carry precondition OK |
| 1188 | } |
| 1189 | |
| 1190 | static void fe_frombytes(fe h, const u8 s[32]) |
| 1191 | { |
| 1192 | fe_frombytes_mask(h, s, 1); |
| 1193 | } |
| 1194 | |
| 1195 | |
| 1196 | // Precondition |
| 1197 | // |h[0]|, |h[2]|, |h[4]|, |h[6]|, |h[8]| < 1.1 * 2^25 |
| 1198 | // |h[1]|, |h[3]|, |h[5]|, |h[7]|, |h[9]| < 1.1 * 2^24 |
| 1199 | // |
| 1200 | // Therefore, |h| < 2^255-19 |
| 1201 | // There are two possibilities: |
| 1202 | // |
| 1203 | // - If h is positive, all we need to do is reduce its individual |
| 1204 | // limbs down to their tight positive range. |
| 1205 | // - If h is negative, we also need to add 2^255-19 to it. |
| 1206 | // Or just remove 19 and chop off any excess bit. |
| 1207 | static void fe_tobytes(u8 s[32], const fe h) |
| 1208 | { |
| 1209 | i32 t[10]; |
| 1210 | COPY(t, h, 10); |
| 1211 | i32 q = (19 * t[9] + (((i32) 1) << 24)) >> 25; |
| 1212 | // |t9| < 1.1 * 2^24 |
| 1213 | // -1.1 * 2^24 < t9 < 1.1 * 2^24 |
| 1214 | // -21 * 2^24 < 19 * t9 < 21 * 2^24 |
| 1215 | // -2^29 < 19 * t9 + 2^24 < 2^29 |
| 1216 | // -2^29 / 2^25 < (19 * t9 + 2^24) / 2^25 < 2^29 / 2^25 |
| 1217 | // -16 < (19 * t9 + 2^24) / 2^25 < 16 |
| 1218 | FOR (i, 0, 5) { |
| 1219 | q += t[2*i ]; q >>= 26; // q = 0 or -1 |
| 1220 | q += t[2*i+1]; q >>= 25; // q = 0 or -1 |
| 1221 | } |
| 1222 | // q = 0 iff h >= 0 |
| 1223 | // q = -1 iff h < 0 |
| 1224 | // Adding q * 19 to h reduces h to its proper range. |
| 1225 | q *= 19; // Shift carry back to the beginning |
| 1226 | FOR (i, 0, 5) { |
| 1227 | t[i*2 ] += q; q = t[i*2 ] >> 26; t[i*2 ] -= q * ((i32)1 << 26); |
| 1228 | t[i*2+1] += q; q = t[i*2+1] >> 25; t[i*2+1] -= q * ((i32)1 << 25); |
| 1229 | } |
| 1230 | // h is now fully reduced, and q represents the excess bit. |
| 1231 | |
| 1232 | store32_le(s + 0, ((u32)t[0] >> 0) | ((u32)t[1] << 26)); |
| 1233 | store32_le(s + 4, ((u32)t[1] >> 6) | ((u32)t[2] << 19)); |
| 1234 | store32_le(s + 8, ((u32)t[2] >> 13) | ((u32)t[3] << 13)); |
| 1235 | store32_le(s + 12, ((u32)t[3] >> 19) | ((u32)t[4] << 6)); |
| 1236 | store32_le(s + 16, ((u32)t[5] >> 0) | ((u32)t[6] << 25)); |
| 1237 | store32_le(s + 20, ((u32)t[6] >> 7) | ((u32)t[7] << 19)); |
| 1238 | store32_le(s + 24, ((u32)t[7] >> 13) | ((u32)t[8] << 12)); |
| 1239 | store32_le(s + 28, ((u32)t[8] >> 20) | ((u32)t[9] << 6)); |
| 1240 | |
| 1241 | WIPE_BUFFER(t); |
| 1242 | } |
| 1243 | |
| 1244 | // Precondition |
| 1245 | // ------------- |
| 1246 | // |f0|, |f2|, |f4|, |f6|, |f8| < 1.65 * 2^26 |
| 1247 | // |f1|, |f3|, |f5|, |f7|, |f9| < 1.65 * 2^25 |
| 1248 | // |
| 1249 | // |g0|, |g2|, |g4|, |g6|, |g8| < 1.65 * 2^26 |
| 1250 | // |g1|, |g3|, |g5|, |g7|, |g9| < 1.65 * 2^25 |
| 1251 | static void fe_mul_small(fe h, const fe f, i32 g) |
| 1252 | { |
| 1253 | i64 t0 = f[0] * (i64) g; i64 t1 = f[1] * (i64) g; |
| 1254 | i64 t2 = f[2] * (i64) g; i64 t3 = f[3] * (i64) g; |
| 1255 | i64 t4 = f[4] * (i64) g; i64 t5 = f[5] * (i64) g; |
| 1256 | i64 t6 = f[6] * (i64) g; i64 t7 = f[7] * (i64) g; |
| 1257 | i64 t8 = f[8] * (i64) g; i64 t9 = f[9] * (i64) g; |
| 1258 | // |t0|, |t2|, |t4|, |t6|, |t8| < 1.65 * 2^26 * 2^31 < 2^58 |
| 1259 | // |t1|, |t3|, |t5|, |t7|, |t9| < 1.65 * 2^25 * 2^31 < 2^57 |
| 1260 | |
| 1261 | FE_CARRY; // Carry precondition OK |
| 1262 | } |
| 1263 | |
| 1264 | // Precondition |
| 1265 | // ------------- |
| 1266 | // |f0|, |f2|, |f4|, |f6|, |f8| < 1.65 * 2^26 |
| 1267 | // |f1|, |f3|, |f5|, |f7|, |f9| < 1.65 * 2^25 |
| 1268 | // |
| 1269 | // |g0|, |g2|, |g4|, |g6|, |g8| < 1.65 * 2^26 |
| 1270 | // |g1|, |g3|, |g5|, |g7|, |g9| < 1.65 * 2^25 |
| 1271 | static void fe_mul(fe h, const fe f, const fe g) |
| 1272 | { |
| 1273 | // Everything is unrolled and put in temporary variables. |
| 1274 | // We could roll the loop, but that would make curve25519 twice as slow. |
| 1275 | i32 f0 = f[0]; i32 f1 = f[1]; i32 f2 = f[2]; i32 f3 = f[3]; i32 f4 = f[4]; |
| 1276 | i32 f5 = f[5]; i32 f6 = f[6]; i32 f7 = f[7]; i32 f8 = f[8]; i32 f9 = f[9]; |
| 1277 | i32 g0 = g[0]; i32 g1 = g[1]; i32 g2 = g[2]; i32 g3 = g[3]; i32 g4 = g[4]; |
| 1278 | i32 g5 = g[5]; i32 g6 = g[6]; i32 g7 = g[7]; i32 g8 = g[8]; i32 g9 = g[9]; |
| 1279 | i32 F1 = f1*2; i32 F3 = f3*2; i32 F5 = f5*2; i32 F7 = f7*2; i32 F9 = f9*2; |
| 1280 | i32 G1 = g1*19; i32 G2 = g2*19; i32 G3 = g3*19; |
| 1281 | i32 G4 = g4*19; i32 G5 = g5*19; i32 G6 = g6*19; |
| 1282 | i32 G7 = g7*19; i32 G8 = g8*19; i32 G9 = g9*19; |
| 1283 | // |F1|, |F3|, |F5|, |F7|, |F9| < 1.65 * 2^26 |
| 1284 | // |G0|, |G2|, |G4|, |G6|, |G8| < 2^31 |
| 1285 | // |G1|, |G3|, |G5|, |G7|, |G9| < 2^30 |
| 1286 | |
| 1287 | i64 t0 = f0*(i64)g0 + F1*(i64)G9 + f2*(i64)G8 + F3*(i64)G7 + f4*(i64)G6 |
| 1288 | + F5*(i64)G5 + f6*(i64)G4 + F7*(i64)G3 + f8*(i64)G2 + F9*(i64)G1; |
| 1289 | i64 t1 = f0*(i64)g1 + f1*(i64)g0 + f2*(i64)G9 + f3*(i64)G8 + f4*(i64)G7 |
| 1290 | + f5*(i64)G6 + f6*(i64)G5 + f7*(i64)G4 + f8*(i64)G3 + f9*(i64)G2; |
| 1291 | i64 t2 = f0*(i64)g2 + F1*(i64)g1 + f2*(i64)g0 + F3*(i64)G9 + f4*(i64)G8 |
| 1292 | + F5*(i64)G7 + f6*(i64)G6 + F7*(i64)G5 + f8*(i64)G4 + F9*(i64)G3; |
| 1293 | i64 t3 = f0*(i64)g3 + f1*(i64)g2 + f2*(i64)g1 + f3*(i64)g0 + f4*(i64)G9 |
| 1294 | + f5*(i64)G8 + f6*(i64)G7 + f7*(i64)G6 + f8*(i64)G5 + f9*(i64)G4; |
| 1295 | i64 t4 = f0*(i64)g4 + F1*(i64)g3 + f2*(i64)g2 + F3*(i64)g1 + f4*(i64)g0 |
| 1296 | + F5*(i64)G9 + f6*(i64)G8 + F7*(i64)G7 + f8*(i64)G6 + F9*(i64)G5; |
| 1297 | i64 t5 = f0*(i64)g5 + f1*(i64)g4 + f2*(i64)g3 + f3*(i64)g2 + f4*(i64)g1 |
| 1298 | + f5*(i64)g0 + f6*(i64)G9 + f7*(i64)G8 + f8*(i64)G7 + f9*(i64)G6; |
| 1299 | i64 t6 = f0*(i64)g6 + F1*(i64)g5 + f2*(i64)g4 + F3*(i64)g3 + f4*(i64)g2 |
| 1300 | + F5*(i64)g1 + f6*(i64)g0 + F7*(i64)G9 + f8*(i64)G8 + F9*(i64)G7; |
| 1301 | i64 t7 = f0*(i64)g7 + f1*(i64)g6 + f2*(i64)g5 + f3*(i64)g4 + f4*(i64)g3 |
| 1302 | + f5*(i64)g2 + f6*(i64)g1 + f7*(i64)g0 + f8*(i64)G9 + f9*(i64)G8; |
| 1303 | i64 t8 = f0*(i64)g8 + F1*(i64)g7 + f2*(i64)g6 + F3*(i64)g5 + f4*(i64)g4 |
| 1304 | + F5*(i64)g3 + f6*(i64)g2 + F7*(i64)g1 + f8*(i64)g0 + F9*(i64)G9; |
| 1305 | i64 t9 = f0*(i64)g9 + f1*(i64)g8 + f2*(i64)g7 + f3*(i64)g6 + f4*(i64)g5 |
| 1306 | + f5*(i64)g4 + f6*(i64)g3 + f7*(i64)g2 + f8*(i64)g1 + f9*(i64)g0; |
| 1307 | // t0 < 0.67 * 2^61 |
| 1308 | // t1 < 0.41 * 2^61 |
| 1309 | // t2 < 0.52 * 2^61 |
| 1310 | // t3 < 0.32 * 2^61 |
| 1311 | // t4 < 0.38 * 2^61 |
| 1312 | // t5 < 0.22 * 2^61 |
| 1313 | // t6 < 0.23 * 2^61 |
| 1314 | // t7 < 0.13 * 2^61 |
| 1315 | // t8 < 0.09 * 2^61 |
| 1316 | // t9 < 0.03 * 2^61 |
| 1317 | |
| 1318 | FE_CARRY; // Everything below 2^62, Carry precondition OK |
| 1319 | } |
| 1320 | |
| 1321 | // Precondition |
| 1322 | // ------------- |
| 1323 | // |f0|, |f2|, |f4|, |f6|, |f8| < 1.65 * 2^26 |
| 1324 | // |f1|, |f3|, |f5|, |f7|, |f9| < 1.65 * 2^25 |
| 1325 | // |
| 1326 | // Note: we could use fe_mul() for this, but this is significantly faster |
| 1327 | static void fe_sq(fe h, const fe f) |
| 1328 | { |
| 1329 | i32 f0 = f[0]; i32 f1 = f[1]; i32 f2 = f[2]; i32 f3 = f[3]; i32 f4 = f[4]; |
| 1330 | i32 f5 = f[5]; i32 f6 = f[6]; i32 f7 = f[7]; i32 f8 = f[8]; i32 f9 = f[9]; |
| 1331 | i32 f0_2 = f0*2; i32 f1_2 = f1*2; i32 f2_2 = f2*2; i32 f3_2 = f3*2; |
| 1332 | i32 f4_2 = f4*2; i32 f5_2 = f5*2; i32 f6_2 = f6*2; i32 f7_2 = f7*2; |
| 1333 | i32 f5_38 = f5*38; i32 f6_19 = f6*19; i32 f7_38 = f7*38; |
| 1334 | i32 f8_19 = f8*19; i32 f9_38 = f9*38; |
| 1335 | // |f0_2| , |f2_2| , |f4_2| , |f6_2| , |f8_2| < 1.65 * 2^27 |
| 1336 | // |f1_2| , |f3_2| , |f5_2| , |f7_2| , |f9_2| < 1.65 * 2^26 |
| 1337 | // |f5_38|, |f6_19|, |f7_38|, |f8_19|, |f9_38| < 2^31 |
| 1338 | |
| 1339 | i64 t0 = f0 *(i64)f0 + f1_2*(i64)f9_38 + f2_2*(i64)f8_19 |
| 1340 | + f3_2*(i64)f7_38 + f4_2*(i64)f6_19 + f5 *(i64)f5_38; |
| 1341 | i64 t1 = f0_2*(i64)f1 + f2 *(i64)f9_38 + f3_2*(i64)f8_19 |
| 1342 | + f4 *(i64)f7_38 + f5_2*(i64)f6_19; |
| 1343 | i64 t2 = f0_2*(i64)f2 + f1_2*(i64)f1 + f3_2*(i64)f9_38 |
| 1344 | + f4_2*(i64)f8_19 + f5_2*(i64)f7_38 + f6 *(i64)f6_19; |
| 1345 | i64 t3 = f0_2*(i64)f3 + f1_2*(i64)f2 + f4 *(i64)f9_38 |
| 1346 | + f5_2*(i64)f8_19 + f6 *(i64)f7_38; |
| 1347 | i64 t4 = f0_2*(i64)f4 + f1_2*(i64)f3_2 + f2 *(i64)f2 |
| 1348 | + f5_2*(i64)f9_38 + f6_2*(i64)f8_19 + f7 *(i64)f7_38; |
| 1349 | i64 t5 = f0_2*(i64)f5 + f1_2*(i64)f4 + f2_2*(i64)f3 |
| 1350 | + f6 *(i64)f9_38 + f7_2*(i64)f8_19; |
| 1351 | i64 t6 = f0_2*(i64)f6 + f1_2*(i64)f5_2 + f2_2*(i64)f4 |
| 1352 | + f3_2*(i64)f3 + f7_2*(i64)f9_38 + f8 *(i64)f8_19; |
| 1353 | i64 t7 = f0_2*(i64)f7 + f1_2*(i64)f6 + f2_2*(i64)f5 |
| 1354 | + f3_2*(i64)f4 + f8 *(i64)f9_38; |
| 1355 | i64 t8 = f0_2*(i64)f8 + f1_2*(i64)f7_2 + f2_2*(i64)f6 |
| 1356 | + f3_2*(i64)f5_2 + f4 *(i64)f4 + f9 *(i64)f9_38; |
| 1357 | i64 t9 = f0_2*(i64)f9 + f1_2*(i64)f8 + f2_2*(i64)f7 |
| 1358 | + f3_2*(i64)f6 + f4 *(i64)f5_2; |
| 1359 | // t0 < 0.67 * 2^61 |
| 1360 | // t1 < 0.41 * 2^61 |
| 1361 | // t2 < 0.52 * 2^61 |
| 1362 | // t3 < 0.32 * 2^61 |
| 1363 | // t4 < 0.38 * 2^61 |
| 1364 | // t5 < 0.22 * 2^61 |
| 1365 | // t6 < 0.23 * 2^61 |
| 1366 | // t7 < 0.13 * 2^61 |
| 1367 | // t8 < 0.09 * 2^61 |
| 1368 | // t9 < 0.03 * 2^61 |
| 1369 | |
| 1370 | FE_CARRY; |
| 1371 | } |
| 1372 | |
| 1373 | // Parity check. Return 0 if even, 1 if odd |
| 1374 | static int fe_isodd(const fe f) |
| 1375 | { |
| 1376 | u8 s[32]; |
| 1377 | fe_tobytes(s, f); |
| 1378 | u8 isodd = s[0] & 1; |
| 1379 | WIPE_BUFFER(s); |
| 1380 | return isodd; |
| 1381 | } |
| 1382 | |
| 1383 | // Return 1 if equal, 0 if not equal |
| 1384 | static int fe_isequal(const fe f, const fe g) |
| 1385 | { |
| 1386 | u8 fs[32]; |
| 1387 | u8 gs[32]; |
| 1388 | fe_tobytes(fs, f); |
| 1389 | fe_tobytes(gs, g); |
| 1390 | int isdifferent = crypto_verify32(fs, gs); |
| 1391 | WIPE_BUFFER(fs); |
| 1392 | WIPE_BUFFER(gs); |
| 1393 | return 1 + isdifferent; |
| 1394 | } |
| 1395 | |
| 1396 | // Inverse square root. |
| 1397 | // Return true if x is a square, false otherwise. |
| 1398 | // After the call: |
| 1399 | // isr = sqrt(1/x) if x is a non-zero square. |
| 1400 | // isr = sqrt(sqrt(-1)/x) if x is not a square. |
| 1401 | // isr = 0 if x is zero. |
| 1402 | // We do not guarantee the sign of the square root. |
| 1403 | // |
| 1404 | // Notes: |
| 1405 | // Let quartic = x^((p-1)/4) |
| 1406 | // |
| 1407 | // x^((p-1)/2) = chi(x) |
| 1408 | // quartic^2 = chi(x) |
| 1409 | // quartic = sqrt(chi(x)) |
| 1410 | // quartic = 1 or -1 or sqrt(-1) or -sqrt(-1) |
| 1411 | // |
| 1412 | // Note that x is a square if quartic is 1 or -1 |
| 1413 | // There are 4 cases to consider: |
| 1414 | // |
| 1415 | // if quartic = 1 (x is a square) |
| 1416 | // then x^((p-1)/4) = 1 |
| 1417 | // x^((p-5)/4) * x = 1 |
| 1418 | // x^((p-5)/4) = 1/x |
| 1419 | // x^((p-5)/8) = sqrt(1/x) or -sqrt(1/x) |
| 1420 | // |
| 1421 | // if quartic = -1 (x is a square) |
| 1422 | // then x^((p-1)/4) = -1 |
| 1423 | // x^((p-5)/4) * x = -1 |
| 1424 | // x^((p-5)/4) = -1/x |
| 1425 | // x^((p-5)/8) = sqrt(-1) / sqrt(x) |
| 1426 | // x^((p-5)/8) * sqrt(-1) = sqrt(-1)^2 / sqrt(x) |
| 1427 | // x^((p-5)/8) * sqrt(-1) = -1/sqrt(x) |
| 1428 | // x^((p-5)/8) * sqrt(-1) = -sqrt(1/x) or sqrt(1/x) |
| 1429 | // |
| 1430 | // if quartic = sqrt(-1) (x is not a square) |
| 1431 | // then x^((p-1)/4) = sqrt(-1) |
| 1432 | // x^((p-5)/4) * x = sqrt(-1) |
| 1433 | // x^((p-5)/4) = sqrt(-1)/x |
| 1434 | // x^((p-5)/8) = sqrt(sqrt(-1)/x) or -sqrt(sqrt(-1)/x) |
| 1435 | // |
| 1436 | // Note that the product of two non-squares is always a square: |
| 1437 | // For any non-squares a and b, chi(a) = -1 and chi(b) = -1. |
| 1438 | // Since chi(x) = x^((p-1)/2), chi(a)*chi(b) = chi(a*b) = 1. |
| 1439 | // Therefore a*b is a square. |
| 1440 | // |
| 1441 | // Since sqrt(-1) and x are both non-squares, their product is a |
| 1442 | // square, and we can compute their square root. |
| 1443 | // |
| 1444 | // if quartic = -sqrt(-1) (x is not a square) |
| 1445 | // then x^((p-1)/4) = -sqrt(-1) |
| 1446 | // x^((p-5)/4) * x = -sqrt(-1) |
| 1447 | // x^((p-5)/4) = -sqrt(-1)/x |
| 1448 | // x^((p-5)/8) = sqrt(-sqrt(-1)/x) |
| 1449 | // x^((p-5)/8) = sqrt( sqrt(-1)/x) * sqrt(-1) |
| 1450 | // x^((p-5)/8) * sqrt(-1) = sqrt( sqrt(-1)/x) * sqrt(-1)^2 |
| 1451 | // x^((p-5)/8) * sqrt(-1) = sqrt( sqrt(-1)/x) * -1 |
| 1452 | // x^((p-5)/8) * sqrt(-1) = -sqrt(sqrt(-1)/x) or sqrt(sqrt(-1)/x) |
| 1453 | static int invsqrt(fe isr, const fe x) |
| 1454 | { |
| 1455 | fe t0, t1, t2; |
| 1456 | |
| 1457 | // t0 = x^((p-5)/8) |
| 1458 | // Can be achieved with a simple double & add ladder, |
| 1459 | // but it would be slower. |
| 1460 | fe_sq(t0, x); |
| 1461 | fe_sq(t1,t0); fe_sq(t1, t1); fe_mul(t1, x, t1); |
| 1462 | fe_mul(t0, t0, t1); |
| 1463 | fe_sq(t0, t0); fe_mul(t0, t1, t0); |
| 1464 | fe_sq(t1, t0); FOR (i, 1, 5) { fe_sq(t1, t1); } fe_mul(t0, t1, t0); |
| 1465 | fe_sq(t1, t0); FOR (i, 1, 10) { fe_sq(t1, t1); } fe_mul(t1, t1, t0); |
| 1466 | fe_sq(t2, t1); FOR (i, 1, 20) { fe_sq(t2, t2); } fe_mul(t1, t2, t1); |
| 1467 | fe_sq(t1, t1); FOR (i, 1, 10) { fe_sq(t1, t1); } fe_mul(t0, t1, t0); |
| 1468 | fe_sq(t1, t0); FOR (i, 1, 50) { fe_sq(t1, t1); } fe_mul(t1, t1, t0); |
| 1469 | fe_sq(t2, t1); FOR (i, 1, 100) { fe_sq(t2, t2); } fe_mul(t1, t2, t1); |
| 1470 | fe_sq(t1, t1); FOR (i, 1, 50) { fe_sq(t1, t1); } fe_mul(t0, t1, t0); |
| 1471 | fe_sq(t0, t0); FOR (i, 1, 2) { fe_sq(t0, t0); } fe_mul(t0, t0, x); |
| 1472 | |
| 1473 | // quartic = x^((p-1)/4) |
| 1474 | i32 *quartic = t1; |
| 1475 | fe_sq (quartic, t0); |
| 1476 | fe_mul(quartic, quartic, x); |
| 1477 | |
| 1478 | i32 *check = t2; |
| 1479 | fe_0 (check); int z0 = fe_isequal(x , check); |
| 1480 | fe_1 (check); int p1 = fe_isequal(quartic, check); |
| 1481 | fe_neg(check, check ); int m1 = fe_isequal(quartic, check); |
| 1482 | fe_neg(check, sqrtm1); int ms = fe_isequal(quartic, check); |
| 1483 | |
| 1484 | // if quartic == -1 or sqrt(-1) |
| 1485 | // then isr = x^((p-1)/4) * sqrt(-1) |
| 1486 | // else isr = x^((p-1)/4) |
| 1487 | fe_mul(isr, t0, sqrtm1); |
| 1488 | fe_ccopy(isr, t0, 1 - (m1 | ms)); |
| 1489 | |
| 1490 | WIPE_BUFFER(t0); |
| 1491 | WIPE_BUFFER(t1); |
| 1492 | WIPE_BUFFER(t2); |
| 1493 | return p1 | m1 | z0; |
| 1494 | } |
| 1495 | |
| 1496 | // Inverse in terms of inverse square root. |
| 1497 | // Requires two additional squarings to get rid of the sign. |
| 1498 | // |
| 1499 | // 1/x = x * (+invsqrt(x^2))^2 |
| 1500 | // = x * (-invsqrt(x^2))^2 |
| 1501 | // |
| 1502 | // A fully optimised exponentiation by p-1 would save 6 field |
| 1503 | // multiplications, but it would require more code. |
| 1504 | static void fe_invert(fe out, const fe x) |
| 1505 | { |
| 1506 | fe tmp; |
| 1507 | fe_sq(tmp, x); |
| 1508 | invsqrt(tmp, tmp); |
| 1509 | fe_sq(tmp, tmp); |
| 1510 | fe_mul(out, tmp, x); |
| 1511 | WIPE_BUFFER(tmp); |
| 1512 | } |
| 1513 | |
| 1514 | // Trim a scalar for scalar multiplication |
| 1515 | void crypto_eddsa_trim_scalar(u8 out[32], const u8 in[32]) |
| 1516 | { |
| 1517 | COPY(out, in, 32); |
| 1518 | out[ 0] &= 248; |
| 1519 | out[31] &= 127; |
| 1520 | out[31] |= 64; |
| 1521 | } |
| 1522 | |
| 1523 | // Get bit from scalar at position i |
| 1524 | static int scalar_bit(const u8 s[32], int i) |
| 1525 | { |
| 1526 | if (i < 0) { return 0; } // Handle -1 for sliding windows |
| 1527 | return (s[i>>3] >> (i&7)) & 1; |
| 1528 | } |
| 1529 | |
| 1530 | /////////////// |
| 1531 | /// X-25519 /// Taken from SUPERCOP's ref10 implementation. |
| 1532 | /////////////// |
| 1533 | static void scalarmult(u8 q[32], const u8 scalar[32], const u8 p[32], |
| 1534 | int nb_bits) |
| 1535 | { |
| 1536 | // Montgomery ladder |
| 1537 | // In projective coordinates, to avoid divisions: x = X / Z |
| 1538 | // We don't care about the y coordinate, it's only 1 bit of information |
| 1539 | fe x1, x2, z2, x3, z3, t0, t1; |
| 1540 | fe_frombytes(x1, p); |
| 1541 | fe_1(x2); fe_0(z2); // "zero" point |
| 1542 | fe_copy(x3, x1); fe_1(z3); // "one" point |
| 1543 | int swap = 0; |
| 1544 | for (int pos = nb_bits-1; pos >= 0; --pos) { |
| 1545 | // constant time conditional swap before ladder step |
| 1546 | int b = scalar_bit(scalar, pos); |
| 1547 | swap ^= b; // xor trick avoids swapping at the end of the loop |
| 1548 | fe_cswap(x2, x3, swap); |
| 1549 | fe_cswap(z2, z3, swap); |
| 1550 | swap = b; // anticipates one last swap after the loop |
| 1551 | |
| 1552 | // Montgomery ladder step: replace (P2, P3) by (P2*2, P2+P3) |
| 1553 | // with differential addition |
| 1554 | fe_sub(t0, x3, z3); |
| 1555 | fe_sub(t1, x2, z2); |
| 1556 | fe_add(x2, x2, z2); |
| 1557 | fe_add(z2, x3, z3); |
| 1558 | fe_mul(z3, t0, x2); |
| 1559 | fe_mul(z2, z2, t1); |
| 1560 | fe_sq (t0, t1 ); |
| 1561 | fe_sq (t1, x2 ); |
| 1562 | fe_add(x3, z3, z2); |
| 1563 | fe_sub(z2, z3, z2); |
| 1564 | fe_mul(x2, t1, t0); |
| 1565 | fe_sub(t1, t1, t0); |
| 1566 | fe_sq (z2, z2 ); |
| 1567 | fe_mul_small(z3, t1, 121666); |
| 1568 | fe_sq (x3, x3 ); |
| 1569 | fe_add(t0, t0, z3); |
| 1570 | fe_mul(z3, x1, z2); |
| 1571 | fe_mul(z2, t1, t0); |
| 1572 | } |
| 1573 | // Last swap is necessary to compensate for the xor trick |
| 1574 | // Note: after this swap, P3 == P2 + P1. |
| 1575 | fe_cswap(x2, x3, swap); |
| 1576 | fe_cswap(z2, z3, swap); |
| 1577 | |
| 1578 | // Normalise the coordinates: x == X / Z |
| 1579 | fe_invert(z2, z2); |
| 1580 | fe_mul(x2, x2, z2); |
| 1581 | fe_tobytes(q, x2); |
| 1582 | |
| 1583 | WIPE_BUFFER(x1); |
| 1584 | WIPE_BUFFER(x2); WIPE_BUFFER(z2); WIPE_BUFFER(t0); |
| 1585 | WIPE_BUFFER(x3); WIPE_BUFFER(z3); WIPE_BUFFER(t1); |
| 1586 | } |
| 1587 | |
| 1588 | void crypto_x25519(u8 raw_shared_secret[32], |
| 1589 | const u8 your_secret_key [32], |
| 1590 | const u8 their_public_key [32]) |
| 1591 | { |
| 1592 | u8 e[32]; |
| 1593 | crypto_eddsa_trim_scalar(e, your_secret_key); |
| 1594 | scalarmult(raw_shared_secret, e, their_public_key, 255); |
| 1595 | WIPE_BUFFER(e); |
| 1596 | } |
| 1597 | |
| 1598 | void crypto_x25519_public_key_small(u8 public_key[32], |
| 1599 | const u8 secret_key[32]) |
| 1600 | { |
| 1601 | static const u8 base_point[32] = {9}; |
| 1602 | crypto_x25519(public_key, secret_key, base_point); |
| 1603 | } |
| 1604 | |
| 1605 | /////////////////////////// |
| 1606 | /// Arithmetic modulo L /// |
| 1607 | /////////////////////////// |
| 1608 | static const u32 L[8] = { |
| 1609 | 0x5cf5d3ed, 0x5812631a, 0xa2f79cd6, 0x14def9de, |
| 1610 | 0x00000000, 0x00000000, 0x00000000, 0x10000000, |
| 1611 | }; |
| 1612 | |
| 1613 | // p = a*b + p |
| 1614 | static void multiply(u32 p[16], const u32 a[8], const u32 b[8]) |
| 1615 | { |
| 1616 | FOR (i, 0, 8) { |
| 1617 | u64 carry = 0; |
| 1618 | FOR (j, 0, 8) { |
| 1619 | carry += p[i+j] + (u64)a[i] * b[j]; |
| 1620 | p[i+j] = (u32)carry; |
| 1621 | carry >>= 32; |
| 1622 | } |
| 1623 | p[i+8] = (u32)carry; |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | static int is_above_l(const u32 x[8]) |
| 1628 | { |
| 1629 | // We work with L directly, in a 2's complement encoding |
| 1630 | // (-L == ~L + 1) |
| 1631 | u64 carry = 1; |
| 1632 | FOR (i, 0, 8) { |
| 1633 | carry += (u64)x[i] + (~L[i] & 0xffffffff); |
| 1634 | carry >>= 32; |
| 1635 | } |
| 1636 | return (int)carry; // carry is either 0 or 1 |
| 1637 | } |
| 1638 | |
| 1639 | // Final reduction modulo L, by conditionally removing L. |
| 1640 | // if x < l , then r = x |
| 1641 | // if l <= x 2*l, then r = x-l |
| 1642 | // otherwise the result will be wrong |
| 1643 | static void remove_l(u32 r[8], const u32 x[8]) |
| 1644 | { |
| 1645 | u64 carry = (u64)is_above_l(x); |
| 1646 | u32 mask = ~(u32)carry + 1; // carry == 0 or 1 |
| 1647 | FOR (i, 0, 8) { |
| 1648 | carry += (u64)x[i] + (~L[i] & mask); |
| 1649 | r[i] = (u32)carry; |
| 1650 | carry >>= 32; |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | // Full reduction modulo L (Barrett reduction) |
| 1655 | static void mod_l(u8 reduced[32], const u32 x[16]) |
| 1656 | { |
| 1657 | static const u32 r[9] = { |
| 1658 | 0x0a2c131b,0xed9ce5a3,0x086329a7,0x2106215d, |
| 1659 | 0xffffffeb,0xffffffff,0xffffffff,0xffffffff,0xf, |
| 1660 | }; |
| 1661 | // xr = x * r |
| 1662 | u32 xr[25] = {0}; |
| 1663 | FOR (i, 0, 9) { |
| 1664 | u64 carry = 0; |
| 1665 | FOR (j, 0, 16) { |
| 1666 | carry += xr[i+j] + (u64)r[i] * x[j]; |
| 1667 | xr[i+j] = (u32)carry; |
| 1668 | carry >>= 32; |
| 1669 | } |
| 1670 | xr[i+16] = (u32)carry; |
| 1671 | } |
| 1672 | // xr = floor(xr / 2^512) * L |
| 1673 | // Since the result is guaranteed to be below 2*L, |
| 1674 | // it is enough to only compute the first 256 bits. |
| 1675 | // The division is performed by saying xr[i+16]. (16 * 32 = 512) |
| 1676 | ZERO(xr, 8); |
| 1677 | FOR (i, 0, 8) { |
| 1678 | u64 carry = 0; |
| 1679 | FOR (j, 0, 8-i) { |
| 1680 | carry += xr[i+j] + (u64)xr[i+16] * L[j]; |
| 1681 | xr[i+j] = (u32)carry; |
| 1682 | carry >>= 32; |
| 1683 | } |
| 1684 | } |
| 1685 | // xr = x - xr |
| 1686 | u64 carry = 1; |
| 1687 | FOR (i, 0, 8) { |
| 1688 | carry += (u64)x[i] + (~xr[i] & 0xffffffff); |
| 1689 | xr[i] = (u32)carry; |
| 1690 | carry >>= 32; |
| 1691 | } |
| 1692 | // Final reduction modulo L (conditional subtraction) |
| 1693 | remove_l(xr, xr); |
| 1694 | store32_le_buf(reduced, xr, 8); |
| 1695 | |
| 1696 | WIPE_BUFFER(xr); |
| 1697 | } |
| 1698 | |
| 1699 | void crypto_eddsa_reduce(u8 reduced[32], const u8 expanded[64]) |
| 1700 | { |
| 1701 | u32 x[16]; |
| 1702 | load32_le_buf(x, expanded, 16); |
| 1703 | mod_l(reduced, x); |
| 1704 | WIPE_BUFFER(x); |
| 1705 | } |
| 1706 | |
| 1707 | // r = (a * b) + c |
| 1708 | void crypto_eddsa_mul_add(u8 r[32], |
| 1709 | const u8 a[32], const u8 b[32], const u8 c[32]) |
| 1710 | { |
| 1711 | u32 A[8]; load32_le_buf(A, a, 8); |
| 1712 | u32 B[8]; load32_le_buf(B, b, 8); |
| 1713 | u32 p[16]; load32_le_buf(p, c, 8); ZERO(p + 8, 8); |
| 1714 | multiply(p, A, B); |
| 1715 | mod_l(r, p); |
| 1716 | WIPE_BUFFER(p); |
| 1717 | WIPE_BUFFER(A); |
| 1718 | WIPE_BUFFER(B); |
| 1719 | } |
| 1720 | |
| 1721 | /////////////// |
| 1722 | /// Ed25519 /// |
| 1723 | /////////////// |
| 1724 | |
| 1725 | // Point (group element, ge) in a twisted Edwards curve, |
| 1726 | // in extended projective coordinates. |
| 1727 | // ge : x = X/Z, y = Y/Z, T = XY/Z |
| 1728 | // ge_cached : Yp = X+Y, Ym = X-Y, T2 = T*D2 |
| 1729 | // ge_precomp: Z = 1 |
| 1730 | typedef struct { fe X; fe Y; fe Z; fe T; } ge; |
| 1731 | typedef struct { fe Yp; fe Ym; fe Z; fe T2; } ge_cached; |
| 1732 | typedef struct { fe Yp; fe Ym; fe T2; } ge_precomp; |
| 1733 | |
| 1734 | static void ge_zero(ge *p) |
| 1735 | { |
| 1736 | fe_0(p->X); |
| 1737 | fe_1(p->Y); |
| 1738 | fe_1(p->Z); |
| 1739 | fe_0(p->T); |
| 1740 | } |
| 1741 | |
| 1742 | static void ge_tobytes(u8 s[32], const ge *h) |
| 1743 | { |
| 1744 | fe recip, x, y; |
| 1745 | fe_invert(recip, h->Z); |
| 1746 | fe_mul(x, h->X, recip); |
| 1747 | fe_mul(y, h->Y, recip); |
| 1748 | fe_tobytes(s, y); |
| 1749 | s[31] ^= (u8)fe_isodd(x) << 7; |
| 1750 | |
| 1751 | WIPE_BUFFER(recip); |
| 1752 | WIPE_BUFFER(x); |
| 1753 | WIPE_BUFFER(y); |
| 1754 | } |
| 1755 | |
| 1756 | // h = -s, where s is a point encoded in 32 bytes |
| 1757 | // |
| 1758 | // Variable time! Inputs must not be secret! |
| 1759 | // => Use only to *check* signatures. |
| 1760 | // |
| 1761 | // From the specifications: |
| 1762 | // The encoding of s contains y and the sign of x |
| 1763 | // x = sqrt((y^2 - 1) / (d*y^2 + 1)) |
| 1764 | // In extended coordinates: |
| 1765 | // X = x, Y = y, Z = 1, T = x*y |
| 1766 | // |
| 1767 | // Note that num * den is a square iff num / den is a square |
| 1768 | // If num * den is not a square, the point was not on the curve. |
| 1769 | // From the above: |
| 1770 | // Let num = y^2 - 1 |
| 1771 | // Let den = d*y^2 + 1 |
| 1772 | // x = sqrt((y^2 - 1) / (d*y^2 + 1)) |
| 1773 | // x = sqrt(num / den) |
| 1774 | // x = sqrt(num^2 / (num * den)) |
| 1775 | // x = num * sqrt(1 / (num * den)) |
| 1776 | // |
| 1777 | // Therefore, we can just compute: |
| 1778 | // num = y^2 - 1 |
| 1779 | // den = d*y^2 + 1 |
| 1780 | // isr = invsqrt(num * den) // abort if not square |
| 1781 | // x = num * isr |
| 1782 | // Finally, negate x if its sign is not as specified. |
| 1783 | static int ge_frombytes_neg_vartime(ge *h, const u8 s[32]) |
| 1784 | { |
| 1785 | fe_frombytes(h->Y, s); |
| 1786 | fe_1(h->Z); |
| 1787 | fe_sq (h->T, h->Y); // t = y^2 |
| 1788 | fe_mul(h->X, h->T, d ); // x = d*y^2 |
| 1789 | fe_sub(h->T, h->T, h->Z); // t = y^2 - 1 |
| 1790 | fe_add(h->X, h->X, h->Z); // x = d*y^2 + 1 |
| 1791 | fe_mul(h->X, h->T, h->X); // x = (y^2 - 1) * (d*y^2 + 1) |
| 1792 | int is_square = invsqrt(h->X, h->X); |
| 1793 | if (!is_square) { |
| 1794 | return -1; // Not on the curve, abort |
| 1795 | } |
| 1796 | fe_mul(h->X, h->T, h->X); // x = sqrt((y^2 - 1) / (d*y^2 + 1)) |
| 1797 | if (fe_isodd(h->X) == (s[31] >> 7)) { |
| 1798 | fe_neg(h->X, h->X); |
| 1799 | } |
| 1800 | fe_mul(h->T, h->X, h->Y); |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
| 1804 | static void ge_cache(ge_cached *c, const ge *p) |
| 1805 | { |
| 1806 | fe_add (c->Yp, p->Y, p->X); |
| 1807 | fe_sub (c->Ym, p->Y, p->X); |
| 1808 | fe_copy(c->Z , p->Z ); |
| 1809 | fe_mul (c->T2, p->T, D2 ); |
| 1810 | } |
| 1811 | |
| 1812 | // Internal buffers are not wiped! Inputs must not be secret! |
| 1813 | // => Use only to *check* signatures. |
| 1814 | static void ge_add(ge *s, const ge *p, const ge_cached *q) |
| 1815 | { |
| 1816 | fe a, b; |
| 1817 | fe_add(a , p->Y, p->X ); |
| 1818 | fe_sub(b , p->Y, p->X ); |
| 1819 | fe_mul(a , a , q->Yp); |
| 1820 | fe_mul(b , b , q->Ym); |
| 1821 | fe_add(s->Y, a , b ); |
| 1822 | fe_sub(s->X, a , b ); |
| 1823 | |
| 1824 | fe_add(s->Z, p->Z, p->Z ); |
| 1825 | fe_mul(s->Z, s->Z, q->Z ); |
| 1826 | fe_mul(s->T, p->T, q->T2); |
| 1827 | fe_add(a , s->Z, s->T ); |
| 1828 | fe_sub(b , s->Z, s->T ); |
| 1829 | |
| 1830 | fe_mul(s->T, s->X, s->Y); |
| 1831 | fe_mul(s->X, s->X, b ); |
| 1832 | fe_mul(s->Y, s->Y, a ); |
| 1833 | fe_mul(s->Z, a , b ); |
| 1834 | } |
| 1835 | |
| 1836 | // Internal buffers are not wiped! Inputs must not be secret! |
| 1837 | // => Use only to *check* signatures. |
| 1838 | static void ge_sub(ge *s, const ge *p, const ge_cached *q) |
| 1839 | { |
| 1840 | ge_cached neg; |
| 1841 | fe_copy(neg.Ym, q->Yp); |
| 1842 | fe_copy(neg.Yp, q->Ym); |
| 1843 | fe_copy(neg.Z , q->Z ); |
| 1844 | fe_neg (neg.T2, q->T2); |
| 1845 | ge_add(s, p, &neg); |
| 1846 | } |
| 1847 | |
| 1848 | static void ge_madd(ge *s, const ge *p, const ge_precomp *q, fe a, fe b) |
| 1849 | { |
| 1850 | fe_add(a , p->Y, p->X ); |
| 1851 | fe_sub(b , p->Y, p->X ); |
| 1852 | fe_mul(a , a , q->Yp); |
| 1853 | fe_mul(b , b , q->Ym); |
| 1854 | fe_add(s->Y, a , b ); |
| 1855 | fe_sub(s->X, a , b ); |
| 1856 | |
| 1857 | fe_add(s->Z, p->Z, p->Z ); |
| 1858 | fe_mul(s->T, p->T, q->T2); |
| 1859 | fe_add(a , s->Z, s->T ); |
| 1860 | fe_sub(b , s->Z, s->T ); |
| 1861 | |
| 1862 | fe_mul(s->T, s->X, s->Y); |
| 1863 | fe_mul(s->X, s->X, b ); |
| 1864 | fe_mul(s->Y, s->Y, a ); |
| 1865 | fe_mul(s->Z, a , b ); |
| 1866 | } |
| 1867 | |
| 1868 | // Internal buffers are not wiped! Inputs must not be secret! |
| 1869 | // => Use only to *check* signatures. |
| 1870 | static void ge_msub(ge *s, const ge *p, const ge_precomp *q, fe a, fe b) |
| 1871 | { |
| 1872 | ge_precomp neg; |
| 1873 | fe_copy(neg.Ym, q->Yp); |
| 1874 | fe_copy(neg.Yp, q->Ym); |
| 1875 | fe_neg (neg.T2, q->T2); |
| 1876 | ge_madd(s, p, &neg, a, b); |
| 1877 | } |
| 1878 | |
| 1879 | static void ge_double(ge *s, const ge *p, ge *q) |
| 1880 | { |
| 1881 | fe_sq (q->X, p->X); |
| 1882 | fe_sq (q->Y, p->Y); |
| 1883 | fe_sq (q->Z, p->Z); // qZ = pZ^2 |
| 1884 | fe_mul_small(q->Z, q->Z, 2); // qZ = pZ^2 * 2 |
| 1885 | fe_add(q->T, p->X, p->Y); |
| 1886 | fe_sq (s->T, q->T); |
| 1887 | fe_add(q->T, q->Y, q->X); |
| 1888 | fe_sub(q->Y, q->Y, q->X); |
| 1889 | fe_sub(q->X, s->T, q->T); |
| 1890 | fe_sub(q->Z, q->Z, q->Y); |
| 1891 | |
| 1892 | fe_mul(s->X, q->X , q->Z); |
| 1893 | fe_mul(s->Y, q->T , q->Y); |
| 1894 | fe_mul(s->Z, q->Y , q->Z); |
| 1895 | fe_mul(s->T, q->X , q->T); |
| 1896 | } |
| 1897 | |
| 1898 | // 5-bit signed window in cached format (Niels coordinates, Z=1) |
| 1899 | static const ge_precomp b_window[8] = { |
| 1900 | {{25967493,-14356035,29566456,3660896,-12694345, |
| 1901 | 4014787,27544626,-11754271,-6079156,2047605,}, |
| 1902 | {-12545711,934262,-2722910,3049990,-727428, |
| 1903 | 9406986,12720692,5043384,19500929,-15469378,}, |
| 1904 | {-8738181,4489570,9688441,-14785194,10184609, |
| 1905 | -12363380,29287919,11864899,-24514362,-4438546,},}, |
| 1906 | {{15636291,-9688557,24204773,-7912398,616977, |
| 1907 | -16685262,27787600,-14772189,28944400,-1550024,}, |
| 1908 | {16568933,4717097,-11556148,-1102322,15682896, |
| 1909 | -11807043,16354577,-11775962,7689662,11199574,}, |
| 1910 | {30464156,-5976125,-11779434,-15670865,23220365, |
| 1911 | 15915852,7512774,10017326,-17749093,-9920357,},}, |
| 1912 | {{10861363,11473154,27284546,1981175,-30064349, |
| 1913 | 12577861,32867885,14515107,-15438304,10819380,}, |
| 1914 | {4708026,6336745,20377586,9066809,-11272109, |
| 1915 | 6594696,-25653668,12483688,-12668491,5581306,}, |
| 1916 | {19563160,16186464,-29386857,4097519,10237984, |
| 1917 | -4348115,28542350,13850243,-23678021,-15815942,},}, |
| 1918 | {{5153746,9909285,1723747,-2777874,30523605, |
| 1919 | 5516873,19480852,5230134,-23952439,-15175766,}, |
| 1920 | {-30269007,-3463509,7665486,10083793,28475525, |
| 1921 | 1649722,20654025,16520125,30598449,7715701,}, |
| 1922 | {28881845,14381568,9657904,3680757,-20181635, |
| 1923 | 7843316,-31400660,1370708,29794553,-1409300,},}, |
| 1924 | {{-22518993,-6692182,14201702,-8745502,-23510406, |
| 1925 | 8844726,18474211,-1361450,-13062696,13821877,}, |
| 1926 | {-6455177,-7839871,3374702,-4740862,-27098617, |
| 1927 | -10571707,31655028,-7212327,18853322,-14220951,}, |
| 1928 | {4566830,-12963868,-28974889,-12240689,-7602672, |
| 1929 | -2830569,-8514358,-10431137,2207753,-3209784,},}, |
| 1930 | {{-25154831,-4185821,29681144,7868801,-6854661, |
| 1931 | -9423865,-12437364,-663000,-31111463,-16132436,}, |
| 1932 | {25576264,-2703214,7349804,-11814844,16472782, |
| 1933 | 9300885,3844789,15725684,171356,6466918,}, |
| 1934 | {23103977,13316479,9739013,-16149481,817875, |
| 1935 | -15038942,8965339,-14088058,-30714912,16193877,},}, |
| 1936 | {{-33521811,3180713,-2394130,14003687,-16903474, |
| 1937 | -16270840,17238398,4729455,-18074513,9256800,}, |
| 1938 | {-25182317,-4174131,32336398,5036987,-21236817, |
| 1939 | 11360617,22616405,9761698,-19827198,630305,}, |
| 1940 | {-13720693,2639453,-24237460,-7406481,9494427, |
| 1941 | -5774029,-6554551,-15960994,-2449256,-14291300,},}, |
| 1942 | {{-3151181,-5046075,9282714,6866145,-31907062, |
| 1943 | -863023,-18940575,15033784,25105118,-7894876,}, |
| 1944 | {-24326370,15950226,-31801215,-14592823,-11662737, |
| 1945 | -5090925,1573892,-2625887,2198790,-15804619,}, |
| 1946 | {-3099351,10324967,-2241613,7453183,-5446979, |
| 1947 | -2735503,-13812022,-16236442,-32461234,-12290683,},}, |
| 1948 | }; |
| 1949 | |
| 1950 | // Incremental sliding windows (left to right) |
| 1951 | // Based on Roberto Maria Avanzi[2005] |
| 1952 | typedef struct { |
| 1953 | i16 next_index; // position of the next signed digit |
| 1954 | i8 next_digit; // next signed digit (odd number below 2^window_width) |
| 1955 | u8 next_check; // point at which we must check for a new window |
| 1956 | } slide_ctx; |
| 1957 | |
| 1958 | static void slide_init(slide_ctx *ctx, const u8 scalar[32]) |
| 1959 | { |
| 1960 | // scalar is guaranteed to be below L, either because we checked (s), |
| 1961 | // or because we reduced it modulo L (h_ram). L is under 2^253, so |
| 1962 | // so bits 253 to 255 are guaranteed to be zero. No need to test them. |
| 1963 | // |
| 1964 | // Note however that L is very close to 2^252, so bit 252 is almost |
| 1965 | // always zero. If we were to start at bit 251, the tests wouldn't |
| 1966 | // catch the off-by-one error (constructing one that does would be |
| 1967 | // prohibitively expensive). |
| 1968 | // |
| 1969 | // We should still check bit 252, though. |
| 1970 | int i = 252; |
| 1971 | while (i > 0 && scalar_bit(scalar, i) == 0) { |
| 1972 | i--; |
| 1973 | } |
| 1974 | ctx->next_check = (u8)(i + 1); |
| 1975 | ctx->next_index = -1; |
| 1976 | ctx->next_digit = -1; |
| 1977 | } |
| 1978 | |
| 1979 | static int slide_step(slide_ctx *ctx, int width, int i, const u8 scalar[32]) |
| 1980 | { |
| 1981 | if (i == ctx->next_check) { |
| 1982 | if (scalar_bit(scalar, i) == scalar_bit(scalar, i - 1)) { |
| 1983 | ctx->next_check--; |
| 1984 | } else { |
| 1985 | // Compute digit of next window |
| 1986 | int w = MIN(width, i + 1); |
| 1987 | int v = -(scalar_bit(scalar, i) << (w-1)); |
| 1988 | FOR_T (int, j, 0, w-1) { |
| 1989 | v += scalar_bit(scalar, i-(w-1)+j) << j; |
| 1990 | } |
| 1991 | v += scalar_bit(scalar, i-w); |
| 1992 | int lsb = v & (~v + 1); // smallest bit of v |
| 1993 | int s = // log2(lsb) |
| 1994 | (((lsb & 0xAA) != 0) << 0) | |
| 1995 | (((lsb & 0xCC) != 0) << 1) | |
| 1996 | (((lsb & 0xF0) != 0) << 2); |
| 1997 | ctx->next_index = (i16)(i-(w-1)+s); |
| 1998 | ctx->next_digit = (i8) (v >> s ); |
| 1999 | ctx->next_check -= (u8) w; |
| 2000 | } |
| 2001 | } |
| 2002 | return i == ctx->next_index ? ctx->next_digit: 0; |
| 2003 | } |
| 2004 | |
| 2005 | #define P_W_WIDTH 3 // Affects the size of the stack |
| 2006 | #define B_W_WIDTH 5 // Affects the size of the binary |
| 2007 | #define P_W_SIZE (1<<(P_W_WIDTH-2)) |
| 2008 | |
| 2009 | int crypto_eddsa_check_equation(const u8 signature[64], const u8 public_key[32], |
| 2010 | const u8 h[32]) |
| 2011 | { |
| 2012 | ge minus_A; // -public_key |
| 2013 | ge minus_R; // -first_half_of_signature |
| 2014 | const u8 *s = signature + 32; |
| 2015 | |
| 2016 | // Check that A and R are on the curve |
| 2017 | // Check that 0 <= S < L (prevents malleability) |
| 2018 | // *Allow* non-cannonical encoding for A and R |
| 2019 | { |
| 2020 | u32 s32[8]; |
| 2021 | load32_le_buf(s32, s, 8); |
| 2022 | if (ge_frombytes_neg_vartime(&minus_A, public_key) || |
| 2023 | ge_frombytes_neg_vartime(&minus_R, signature) || |
| 2024 | is_above_l(s32)) { |
| 2025 | return -1; |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | // Look-up table for minus_A |
| 2030 | ge_cached lutA[P_W_SIZE]; |
| 2031 | { |
| 2032 | ge minus_A2, tmp; |
| 2033 | ge_double(&minus_A2, &minus_A, &tmp); |
| 2034 | ge_cache(&lutA[0], &minus_A); |
| 2035 | FOR (i, 1, P_W_SIZE) { |
| 2036 | ge_add(&tmp, &minus_A2, &lutA[i-1]); |
| 2037 | ge_cache(&lutA[i], &tmp); |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | // sum = [s]B - [h]A |
| 2042 | // Merged double and add ladder, fused with sliding |
| 2043 | slide_ctx h_slide; slide_init(&h_slide, h); |
| 2044 | slide_ctx s_slide; slide_init(&s_slide, s); |
| 2045 | int i = MAX(h_slide.next_check, s_slide.next_check); |
| 2046 | ge *sum = &minus_A; // reuse minus_A for the sum |
| 2047 | ge_zero(sum); |
| 2048 | while (i >= 0) { |
| 2049 | ge tmp; |
| 2050 | ge_double(sum, sum, &tmp); |
| 2051 | int h_digit = slide_step(&h_slide, P_W_WIDTH, i, h); |
| 2052 | int s_digit = slide_step(&s_slide, B_W_WIDTH, i, s); |
| 2053 | if (h_digit > 0) { ge_add(sum, sum, &lutA[ h_digit / 2]); } |
| 2054 | if (h_digit < 0) { ge_sub(sum, sum, &lutA[-h_digit / 2]); } |
| 2055 | fe t1, t2; |
| 2056 | if (s_digit > 0) { ge_madd(sum, sum, b_window + s_digit/2, t1, t2); } |
| 2057 | if (s_digit < 0) { ge_msub(sum, sum, b_window + -s_digit/2, t1, t2); } |
| 2058 | i--; |
| 2059 | } |
| 2060 | |
| 2061 | // Compare [8](sum-R) and the zero point |
| 2062 | // The multiplication by 8 eliminates any low-order component |
| 2063 | // and ensures consistency with batched verification. |
| 2064 | ge_cached cached; |
| 2065 | ge_cache(&cached, &minus_R); |
| 2066 | ge_add(sum, sum, &cached); |
| 2067 | ge_double(sum, sum, &minus_R); // reuse minus_R as temporary |
| 2068 | ge_double(sum, sum, &minus_R); // reuse minus_R as temporary |
| 2069 | ge_double(sum, sum, &minus_R); // reuse minus_R as temporary |
| 2070 | // The zero point is the only point with y == 1, that is Y == Z |
| 2071 | // (Z is never zero: the Edwards addition law is complete). |
| 2072 | // Checking this directly saves a field inversion. |
| 2073 | return fe_isequal(sum->Y, sum->Z) - 1; |
| 2074 | } |
| 2075 | |
| 2076 | // 5-bit signed comb in cached format (Niels coordinates, Z=1) |
| 2077 | static const ge_precomp b_comb_low[8] = { |
| 2078 | {{-6816601,-2324159,-22559413,124364,18015490, |
| 2079 | 8373481,19993724,1979872,-18549925,9085059,}, |
| 2080 | {10306321,403248,14839893,9633706,8463310, |
| 2081 | -8354981,-14305673,14668847,26301366,2818560,}, |
| 2082 | {-22701500,-3210264,-13831292,-2927732,-16326337, |
| 2083 | -14016360,12940910,177905,12165515,-2397893,},}, |
| 2084 | {{-12282262,-7022066,9920413,-3064358,-32147467, |
| 2085 | 2927790,22392436,-14852487,2719975,16402117,}, |
| 2086 | {-7236961,-4729776,2685954,-6525055,-24242706, |
| 2087 | -15940211,-6238521,14082855,10047669,12228189,}, |
| 2088 | {-30495588,-12893761,-11161261,3539405,-11502464, |
| 2089 | 16491580,-27286798,-15030530,-7272871,-15934455,},}, |
| 2090 | {{17650926,582297,-860412,-187745,-12072900, |
| 2091 | -10683391,-20352381,15557840,-31072141,-5019061,}, |
| 2092 | {-6283632,-2259834,-4674247,-4598977,-4089240, |
| 2093 | 12435688,-31278303,1060251,6256175,10480726,}, |
| 2094 | {-13871026,2026300,-21928428,-2741605,-2406664, |
| 2095 | -8034988,7355518,15733500,-23379862,7489131,},}, |
| 2096 | {{6883359,695140,23196907,9644202,-33430614, |
| 2097 | 11354760,-20134606,6388313,-8263585,-8491918,}, |
| 2098 | {-7716174,-13605463,-13646110,14757414,-19430591, |
| 2099 | -14967316,10359532,-11059670,-21935259,12082603,}, |
| 2100 | {-11253345,-15943946,10046784,5414629,24840771, |
| 2101 | 8086951,-6694742,9868723,15842692,-16224787,},}, |
| 2102 | {{9639399,11810955,-24007778,-9320054,3912937, |
| 2103 | -9856959,996125,-8727907,-8919186,-14097242,}, |
| 2104 | {7248867,14468564,25228636,-8795035,14346339, |
| 2105 | 8224790,6388427,-7181107,6468218,-8720783,}, |
| 2106 | {15513115,15439095,7342322,-10157390,18005294, |
| 2107 | -7265713,2186239,4884640,10826567,7135781,},}, |
| 2108 | {{-14204238,5297536,-5862318,-6004934,28095835, |
| 2109 | 4236101,-14203318,1958636,-16816875,3837147,}, |
| 2110 | {-5511166,-13176782,-29588215,12339465,15325758, |
| 2111 | -15945770,-8813185,11075932,-19608050,-3776283,}, |
| 2112 | {11728032,9603156,-4637821,-5304487,-7827751, |
| 2113 | 2724948,31236191,-16760175,-7268616,14799772,},}, |
| 2114 | {{-28842672,4840636,-12047946,-9101456,-1445464, |
| 2115 | 381905,-30977094,-16523389,1290540,12798615,}, |
| 2116 | {27246947,-10320914,14792098,-14518944,5302070, |
| 2117 | -8746152,-3403974,-4149637,-27061213,10749585,}, |
| 2118 | {25572375,-6270368,-15353037,16037944,1146292, |
| 2119 | 32198,23487090,9585613,24714571,-1418265,},}, |
| 2120 | {{19844825,282124,-17583147,11004019,-32004269, |
| 2121 | -2716035,6105106,-1711007,-21010044,14338445,}, |
| 2122 | {8027505,8191102,-18504907,-12335737,25173494, |
| 2123 | -5923905,15446145,7483684,-30440441,10009108,}, |
| 2124 | {-14134701,-4174411,10246585,-14677495,33553567, |
| 2125 | -14012935,23366126,15080531,-7969992,7663473,},}, |
| 2126 | }; |
| 2127 | |
| 2128 | static const ge_precomp b_comb_high[8] = { |
| 2129 | {{33055887,-4431773,-521787,6654165,951411, |
| 2130 | -6266464,-5158124,6995613,-5397442,-6985227,}, |
| 2131 | {4014062,6967095,-11977872,3960002,8001989, |
| 2132 | 5130302,-2154812,-1899602,-31954493,-16173976,}, |
| 2133 | {16271757,-9212948,23792794,731486,-25808309, |
| 2134 | -3546396,6964344,-4767590,10976593,10050757,},}, |
| 2135 | {{2533007,-4288439,-24467768,-12387405,-13450051, |
| 2136 | 14542280,12876301,13893535,15067764,8594792,}, |
| 2137 | {20073501,-11623621,3165391,-13119866,13188608, |
| 2138 | -11540496,-10751437,-13482671,29588810,2197295,}, |
| 2139 | {-1084082,11831693,6031797,14062724,14748428, |
| 2140 | -8159962,-20721760,11742548,31368706,13161200,},}, |
| 2141 | {{2050412,-6457589,15321215,5273360,25484180, |
| 2142 | 124590,-18187548,-7097255,-6691621,-14604792,}, |
| 2143 | {9938196,2162889,-6158074,-1711248,4278932, |
| 2144 | -2598531,-22865792,-7168500,-24323168,11746309,}, |
| 2145 | {-22691768,-14268164,5965485,9383325,20443693, |
| 2146 | 5854192,28250679,-1381811,-10837134,13717818,},}, |
| 2147 | {{-8495530,16382250,9548884,-4971523,-4491811, |
| 2148 | -3902147,6182256,-12832479,26628081,10395408,}, |
| 2149 | {27329048,-15853735,7715764,8717446,-9215518, |
| 2150 | -14633480,28982250,-5668414,4227628,242148,}, |
| 2151 | {-13279943,-7986904,-7100016,8764468,-27276630, |
| 2152 | 3096719,29678419,-9141299,3906709,11265498,},}, |
| 2153 | {{11918285,15686328,-17757323,-11217300,-27548967, |
| 2154 | 4853165,-27168827,6807359,6871949,-1075745,}, |
| 2155 | {-29002610,13984323,-27111812,-2713442,28107359, |
| 2156 | -13266203,6155126,15104658,3538727,-7513788,}, |
| 2157 | {14103158,11233913,-33165269,9279850,31014152, |
| 2158 | 4335090,-1827936,4590951,13960841,12787712,},}, |
| 2159 | {{1469134,-16738009,33411928,13942824,8092558, |
| 2160 | -8778224,-11165065,1437842,22521552,-2792954,}, |
| 2161 | {31352705,-4807352,-25327300,3962447,12541566, |
| 2162 | -9399651,-27425693,7964818,-23829869,5541287,}, |
| 2163 | {-25732021,-6864887,23848984,3039395,-9147354, |
| 2164 | 6022816,-27421653,10590137,25309915,-1584678,},}, |
| 2165 | {{-22951376,5048948,31139401,-190316,-19542447, |
| 2166 | -626310,-17486305,-16511925,-18851313,-12985140,}, |
| 2167 | {-9684890,14681754,30487568,7717771,-10829709, |
| 2168 | 9630497,30290549,-10531496,-27798994,-13812825,}, |
| 2169 | {5827835,16097107,-24501327,12094619,7413972, |
| 2170 | 11447087,28057551,-1793987,-14056981,4359312,},}, |
| 2171 | {{26323183,2342588,-21887793,-1623758,-6062284, |
| 2172 | 2107090,-28724907,9036464,-19618351,-13055189,}, |
| 2173 | {-29697200,14829398,-4596333,14220089,-30022969, |
| 2174 | 2955645,12094100,-13693652,-5941445,7047569,}, |
| 2175 | {-3201977,14413268,-12058324,-16417589,-9035655, |
| 2176 | -7224648,9258160,1399236,30397584,-5684634,},}, |
| 2177 | }; |
| 2178 | |
| 2179 | static void lookup_add(ge *p, ge_precomp *tmp_c, fe tmp_a, fe tmp_b, |
| 2180 | const ge_precomp comb[8], const u8 scalar[32], int i) |
| 2181 | { |
| 2182 | u8 teeth = (u8)((scalar_bit(scalar, i) ) + |
| 2183 | (scalar_bit(scalar, i + 32) << 1) + |
| 2184 | (scalar_bit(scalar, i + 64) << 2) + |
| 2185 | (scalar_bit(scalar, i + 96) << 3)); |
| 2186 | u8 high = teeth >> 3; |
| 2187 | u8 index = (teeth ^ (high - 1)) & 7; |
| 2188 | FOR (j, 0, 8) { |
| 2189 | i32 select = 1 & (((j ^ index) - 1) >> 8); |
| 2190 | fe_ccopy(tmp_c->Yp, comb[j].Yp, select); |
| 2191 | fe_ccopy(tmp_c->Ym, comb[j].Ym, select); |
| 2192 | fe_ccopy(tmp_c->T2, comb[j].T2, select); |
| 2193 | } |
| 2194 | fe_neg(tmp_a, tmp_c->T2); |
| 2195 | fe_cswap(tmp_c->T2, tmp_a , high ^ 1); |
| 2196 | fe_cswap(tmp_c->Yp, tmp_c->Ym, high ^ 1); |
| 2197 | ge_madd(p, p, tmp_c, tmp_a, tmp_b); |
| 2198 | } |
| 2199 | |
| 2200 | // p = [scalar]B, where B is the base point |
| 2201 | static void ge_scalarmult_base(ge *p, const u8 scalar[32]) |
| 2202 | { |
| 2203 | // Twin 4-bits signed combs, from Mike Hamburg's |
| 2204 | // Fast and compact elliptic-curve cryptography (2012) |
| 2205 | // 1 / 2 modulo L |
| 2206 | static const u8 half_mod_L[32] = { |
| 2207 | 247,233,122,46,141,49,9,44,107,206,123,81,239,124,111,10, |
| 2208 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8, |
| 2209 | }; |
| 2210 | // (2^256 - 1) / 2 modulo L |
| 2211 | static const u8 half_ones[32] = { |
| 2212 | 142,74,204,70,186,24,118,107,184,231,190,57,250,173,119,99, |
| 2213 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,7, |
| 2214 | }; |
| 2215 | |
| 2216 | // All bits set form: 1 means 1, 0 means -1 |
| 2217 | u8 s_scalar[32]; |
| 2218 | crypto_eddsa_mul_add(s_scalar, scalar, half_mod_L, half_ones); |
| 2219 | |
| 2220 | // Double and add ladder |
| 2221 | fe tmp_a, tmp_b; // temporaries for addition |
| 2222 | ge_precomp tmp_c; // temporary for comb lookup |
| 2223 | ge tmp_d; // temporary for doubling |
| 2224 | fe_1(tmp_c.Yp); |
| 2225 | fe_1(tmp_c.Ym); |
| 2226 | fe_0(tmp_c.T2); |
| 2227 | |
| 2228 | // Save a double on the first iteration |
| 2229 | ge_zero(p); |
| 2230 | lookup_add(p, &tmp_c, tmp_a, tmp_b, b_comb_low , s_scalar, 31); |
| 2231 | lookup_add(p, &tmp_c, tmp_a, tmp_b, b_comb_high, s_scalar, 31+128); |
| 2232 | // Regular double & add for the rest |
| 2233 | for (int i = 30; i >= 0; i--) { |
| 2234 | ge_double(p, p, &tmp_d); |
| 2235 | lookup_add(p, &tmp_c, tmp_a, tmp_b, b_comb_low , s_scalar, i); |
| 2236 | lookup_add(p, &tmp_c, tmp_a, tmp_b, b_comb_high, s_scalar, i+128); |
| 2237 | } |
| 2238 | // Note: we could save one addition at the end if we assumed the |
| 2239 | // scalar fit in 252 bits. Which it does in practice if it is |
| 2240 | // selected at random. However, non-random, non-hashed scalars |
| 2241 | // *can* overflow 252 bits in practice. Better account for that |
| 2242 | // than leaving that kind of subtle corner case. |
| 2243 | |
| 2244 | WIPE_BUFFER(tmp_a); WIPE_CTX(&tmp_d); |
| 2245 | WIPE_BUFFER(tmp_b); WIPE_CTX(&tmp_c); |
| 2246 | WIPE_BUFFER(s_scalar); |
| 2247 | } |
| 2248 | |
| 2249 | void crypto_eddsa_scalarbase(u8 point[32], const u8 scalar[32]) |
| 2250 | { |
| 2251 | ge P; |
| 2252 | ge_scalarmult_base(&P, scalar); |
| 2253 | ge_tobytes(point, &P); |
| 2254 | WIPE_CTX(&P); |
| 2255 | } |
| 2256 | |
| 2257 | void crypto_eddsa_key_pair(u8 secret_key[64], u8 public_key[32], u8 seed[32]) |
| 2258 | { |
| 2259 | // To allow overlaps, observable writes happen in this order: |
| 2260 | // 1. seed |
| 2261 | // 2. secret_key |
| 2262 | // 3. public_key |
| 2263 | u8 a[64]; |
| 2264 | COPY(a, seed, 32); |
| 2265 | crypto_wipe(seed, 32); |
| 2266 | COPY(secret_key, a, 32); |
| 2267 | crypto_blake2b(a, 64, a, 32); |
| 2268 | crypto_eddsa_trim_scalar(a, a); |
| 2269 | crypto_eddsa_scalarbase(secret_key + 32, a); |
| 2270 | COPY(public_key, secret_key + 32, 32); |
| 2271 | WIPE_BUFFER(a); |
| 2272 | } |
| 2273 | |
| 2274 | static void hash_reduce(u8 h[32], |
| 2275 | const u8 *a, size_t a_size, |
| 2276 | const u8 *b, size_t b_size, |
| 2277 | const u8 *c, size_t c_size) |
| 2278 | { |
| 2279 | u8 hash[64]; |
| 2280 | crypto_blake2b_ctx ctx; |
| 2281 | crypto_blake2b_init (&ctx, 64); |
| 2282 | crypto_blake2b_update(&ctx, a, a_size); |
| 2283 | crypto_blake2b_update(&ctx, b, b_size); |
| 2284 | crypto_blake2b_update(&ctx, c, c_size); |
| 2285 | crypto_blake2b_final (&ctx, hash); |
| 2286 | crypto_eddsa_reduce(h, hash); |
| 2287 | } |
| 2288 | |
| 2289 | // Digital signature of a message with from a secret key. |
| 2290 | // |
| 2291 | // The secret key comprises two parts: |
| 2292 | // - The seed that generates the key (secret_key[ 0..31]) |
| 2293 | // - The public key (secret_key[32..63]) |
| 2294 | // |
| 2295 | // The seed and the public key are bundled together to make sure users |
| 2296 | // don't use mismatch seeds and public keys, which would instantly leak |
| 2297 | // the secret scalar and allow forgeries (allowing this to happen has |
| 2298 | // resulted in critical vulnerabilities in the wild). |
| 2299 | // |
| 2300 | // The seed is hashed to derive the secret scalar and a secret prefix. |
| 2301 | // The sole purpose of the prefix is to generate a secret random nonce. |
| 2302 | // The properties of that nonce must be as follows: |
| 2303 | // - Unique: we need a different one for each message. |
| 2304 | // - Secret: third parties must not be able to predict it. |
| 2305 | // - Random: any detectable bias would break all security. |
| 2306 | // |
| 2307 | // There are two ways to achieve these properties. The obvious one is |
| 2308 | // to simply generate a random number. Here that would be a parameter |
| 2309 | // (Monocypher doesn't have an RNG). It works, but then users may reuse |
| 2310 | // the nonce by accident, which _also_ leaks the secret scalar and |
| 2311 | // allows forgeries. This has happened in the wild too. |
| 2312 | // |
| 2313 | // This is no good, so instead we generate that nonce deterministically |
| 2314 | // by reducing modulo L a hash of the secret prefix and the message. |
| 2315 | // The secret prefix makes the nonce unpredictable, the message makes it |
| 2316 | // unique, and the hash/reduce removes all bias. |
| 2317 | // |
| 2318 | // The cost of that safety is hashing the message twice. If that cost |
| 2319 | // is unacceptable, there are two alternatives: |
| 2320 | // |
| 2321 | // - Signing a hash of the message instead of the message itself. This |
| 2322 | // is fine as long as the hash is collision resistant. It is not |
| 2323 | // compatible with existing "pure" signatures, but at least it's safe. |
| 2324 | // |
| 2325 | // - Using a random nonce. Please exercise **EXTREME CAUTION** if you |
| 2326 | // ever do that. It is absolutely **critical** that the nonce is |
| 2327 | // really an unbiased random number between 0 and L-1, never reused, |
| 2328 | // and wiped immediately. |
| 2329 | // |
| 2330 | // To lower the likelihood of complete catastrophe if the RNG is |
| 2331 | // either flawed or misused, you can hash the RNG output together with |
| 2332 | // the secret prefix and the beginning of the message, and use the |
| 2333 | // reduction of that hash instead of the RNG output itself. It's not |
| 2334 | // foolproof (you'd need to hash the whole message) but it helps. |
| 2335 | // |
| 2336 | // Signing a message involves the following operations: |
| 2337 | // |
| 2338 | // scalar, prefix = HASH(secret_key) |
| 2339 | // r = HASH(prefix || message) % L |
| 2340 | // R = [r]B |
| 2341 | // h = HASH(R || public_key || message) % L |
| 2342 | // S = ((h * a) + r) % L |
| 2343 | // signature = R || S |
| 2344 | void crypto_eddsa_sign(u8 signature [64], const u8 secret_key[64], |
| 2345 | const u8 *message, size_t message_size) |
| 2346 | { |
| 2347 | u8 a[64]; // secret scalar and prefix |
| 2348 | u8 r[32]; // secret deterministic "random" nonce |
| 2349 | u8 h[32]; // publicly verifiable hash of the message (not wiped) |
| 2350 | u8 R[32]; // first half of the signature (allows overlapping inputs) |
| 2351 | |
| 2352 | crypto_blake2b(a, 64, secret_key, 32); |
| 2353 | crypto_eddsa_trim_scalar(a, a); |
| 2354 | hash_reduce(r, a + 32, 32, message, message_size, 0, 0); |
| 2355 | crypto_eddsa_scalarbase(R, r); |
| 2356 | hash_reduce(h, R, 32, secret_key + 32, 32, message, message_size); |
| 2357 | COPY(signature, R, 32); |
| 2358 | crypto_eddsa_mul_add(signature + 32, h, a, r); |
| 2359 | |
| 2360 | WIPE_BUFFER(a); |
| 2361 | WIPE_BUFFER(r); |
| 2362 | } |
| 2363 | |
| 2364 | // To check the signature R, S of the message M with the public key A, |
| 2365 | // there are 3 steps: |
| 2366 | // |
| 2367 | // compute h = HASH(R || A || message) % L |
| 2368 | // check that A is on the curve. |
| 2369 | // check that R == [s]B - [h]A |
| 2370 | // |
| 2371 | // The last two steps are done in crypto_eddsa_check_equation() |
| 2372 | int crypto_eddsa_check(const u8 signature[64], const u8 public_key[32], |
| 2373 | const u8 *message, size_t message_size) |
| 2374 | { |
| 2375 | u8 h[32]; |
| 2376 | hash_reduce(h, signature, 32, public_key, 32, message, message_size); |
| 2377 | return crypto_eddsa_check_equation(signature, public_key, h); |
| 2378 | } |
| 2379 | |
| 2380 | ///////////////////////// |
| 2381 | /// EdDSA <--> X25519 /// |
| 2382 | ///////////////////////// |
| 2383 | void crypto_eddsa_to_x25519(u8 x25519[32], const u8 eddsa[32]) |
| 2384 | { |
| 2385 | // (u, v) = ((1+y)/(1-y), sqrt(-486664)*u/x) |
| 2386 | // Only converting y to u, the sign of x is ignored. |
| 2387 | fe t1, t2; |
| 2388 | fe_frombytes(t2, eddsa); |
| 2389 | fe_add(t1, fe_one, t2); |
| 2390 | fe_sub(t2, fe_one, t2); |
| 2391 | fe_invert(t2, t2); |
| 2392 | fe_mul(t1, t1, t2); |
| 2393 | fe_tobytes(x25519, t1); |
| 2394 | WIPE_BUFFER(t1); |
| 2395 | WIPE_BUFFER(t2); |
| 2396 | } |
| 2397 | |
| 2398 | void crypto_x25519_to_eddsa(u8 eddsa[32], const u8 x25519[32]) |
| 2399 | { |
| 2400 | // (x, y) = (sqrt(-486664)*u/v, (u-1)/(u+1)) |
| 2401 | // Only converting u to y, x is assumed positive. |
| 2402 | fe t1, t2; |
| 2403 | fe_frombytes(t2, x25519); |
| 2404 | fe_sub(t1, t2, fe_one); |
| 2405 | fe_add(t2, t2, fe_one); |
| 2406 | fe_invert(t2, t2); |
| 2407 | fe_mul(t1, t1, t2); |
| 2408 | fe_tobytes(eddsa, t1); |
| 2409 | WIPE_BUFFER(t1); |
| 2410 | WIPE_BUFFER(t2); |
| 2411 | } |
| 2412 | |
| 2413 | ///////////////////////////////////////////// |
| 2414 | /// Dirty ephemeral public key generation /// |
| 2415 | ///////////////////////////////////////////// |
| 2416 | |
| 2417 | // Those functions generates a public key *without* clearing the |
| 2418 | // cofactor. Sending that key over the network leaks 3 bits of the |
| 2419 | // private key. Use only to generate ephemeral keys that will be hidden |
| 2420 | // with crypto_curve_to_hidden(). |
| 2421 | // |
| 2422 | // The public key is otherwise compatible with crypto_x25519(), which |
| 2423 | // properly clears the cofactor. |
| 2424 | // |
| 2425 | // Note that the distribution of the resulting public keys is almost |
| 2426 | // uniform. Flipping the sign of the v coordinate (not provided by this |
| 2427 | // function), covers the entire key space almost perfectly, where |
| 2428 | // "almost" means a 2^-128 bias (undetectable). This uniformity is |
| 2429 | // needed to ensure the proper randomness of the resulting |
| 2430 | // representatives (once we apply crypto_curve_to_hidden()). |
| 2431 | // |
| 2432 | // Recall that Curve25519 has order C = 2^255 + e, with e < 2^128 (not |
| 2433 | // to be confused with the prime order of the main subgroup, L, which is |
| 2434 | // 8 times less than that). |
| 2435 | // |
| 2436 | // Generating all points would require us to multiply a point of order C |
| 2437 | // (the base point plus any point of order 8) by all scalars from 0 to |
| 2438 | // C-1. Clamping limits us to scalars between 2^254 and 2^255 - 1. But |
| 2439 | // by negating the resulting point at random, we also cover scalars from |
| 2440 | // -2^255 + 1 to -2^254 (which modulo C is congruent to e+1 to 2^254 + e). |
| 2441 | // |
| 2442 | // In practice: |
| 2443 | // - Scalars from 0 to e + 1 are never generated |
| 2444 | // - Scalars from 2^255 to 2^255 + e are never generated |
| 2445 | // - Scalars from 2^254 + 1 to 2^254 + e are generated twice |
| 2446 | // |
| 2447 | // Since e < 2^128, detecting this bias requires observing over 2^100 |
| 2448 | // representatives from a given source (this will never happen), *and* |
| 2449 | // recovering enough of the private key to determine that they do, or do |
| 2450 | // not, belong to the biased set (this practically requires solving |
| 2451 | // discrete logarithm, which is conjecturally intractable). |
| 2452 | // |
| 2453 | // In practice, this means the bias is impossible to detect. |
| 2454 | |
| 2455 | // s + (x*L) % 8*L |
| 2456 | // Guaranteed to fit in 256 bits iff s fits in 255 bits. |
| 2457 | // L < 2^253 |
| 2458 | // x%8 < 2^3 |
| 2459 | // L * (x%8) < 2^255 |
| 2460 | // s < 2^255 |
| 2461 | // s + L * (x%8) < 2^256 |
| 2462 | static void add_xl(u8 s[32], u8 x) |
| 2463 | { |
| 2464 | u64 mod8 = x & 7; |
| 2465 | u64 carry = 0; |
| 2466 | FOR (i , 0, 8) { |
| 2467 | carry = carry + load32_le(s + 4*i) + L[i] * mod8; |
| 2468 | store32_le(s + 4*i, (u32)carry); |
| 2469 | carry >>= 32; |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | // "Small" dirty ephemeral key. |
| 2474 | // Use if you need to shrink the size of the binary, and can afford to |
| 2475 | // slow down by a factor of two (compared to the fast version) |
| 2476 | // |
| 2477 | // This version works by decoupling the cofactor from the main factor. |
| 2478 | // |
| 2479 | // - The trimmed scalar determines the main factor |
| 2480 | // - The clamped bits of the scalar determine the cofactor. |
| 2481 | // |
| 2482 | // Cofactor and main factor are combined into a single scalar, which is |
| 2483 | // then multiplied by a point of order 8*L (unlike the base point, which |
| 2484 | // has prime order). That "dirty" base point is the addition of the |
| 2485 | // regular base point (9), and a point of order 8. |
| 2486 | void crypto_x25519_dirty_small(u8 public_key[32], const u8 secret_key[32]) |
| 2487 | { |
| 2488 | // Base point of order 8*L |
| 2489 | // Raw scalar multiplication with it does not clear the cofactor, |
| 2490 | // and the resulting public key will reveal 3 bits of the scalar. |
| 2491 | // |
| 2492 | // The low order component of this base point has been chosen |
| 2493 | // to yield the same results as crypto_x25519_dirty_fast(). |
| 2494 | static const u8 dirty_base_point[32] = { |
| 2495 | 0xd8, 0x86, 0x1a, 0xa2, 0x78, 0x7a, 0xd9, 0x26, |
| 2496 | 0x8b, 0x74, 0x74, 0xb6, 0x82, 0xe3, 0xbe, 0xc3, |
| 2497 | 0xce, 0x36, 0x9a, 0x1e, 0x5e, 0x31, 0x47, 0xa2, |
| 2498 | 0x6d, 0x37, 0x7c, 0xfd, 0x20, 0xb5, 0xdf, 0x75, |
| 2499 | }; |
| 2500 | // Separate the main factor & the cofactor of the scalar |
| 2501 | u8 scalar[32]; |
| 2502 | crypto_eddsa_trim_scalar(scalar, secret_key); |
| 2503 | |
| 2504 | // Separate the main factor and the cofactor |
| 2505 | // |
| 2506 | // The scalar is trimmed, so its cofactor is cleared. The three |
| 2507 | // least significant bits however still have a main factor. We must |
| 2508 | // remove it for X25519 compatibility. |
| 2509 | // |
| 2510 | // cofactor = lsb * L (modulo 8*L) |
| 2511 | // combined = scalar + cofactor (modulo 8*L) |
| 2512 | add_xl(scalar, secret_key[0]); |
| 2513 | scalarmult(public_key, scalar, dirty_base_point, 256); |
| 2514 | WIPE_BUFFER(scalar); |
| 2515 | } |
| 2516 | |
| 2517 | // Select low order point |
| 2518 | // We're computing the [cofactor]lop scalar multiplication, where: |
| 2519 | // |
| 2520 | // cofactor = tweak & 7. |
| 2521 | // lop = (lop_x, lop_y) |
| 2522 | // lop_x = sqrt((sqrt(d + 1) + 1) / d) |
| 2523 | // lop_y = -lop_x * sqrtm1 |
| 2524 | // |
| 2525 | // The low order point has order 8. There are 4 such points. We've |
| 2526 | // chosen the one whose both coordinates are positive (below p/2). |
| 2527 | // The 8 low order points are as follows: |
| 2528 | // |
| 2529 | // [0]lop = ( 0 , 1 ) |
| 2530 | // [1]lop = ( lop_x , lop_y) |
| 2531 | // [2]lop = ( sqrt(-1), -0 ) |
| 2532 | // [3]lop = ( lop_x , -lop_y) |
| 2533 | // [4]lop = (-0 , -1 ) |
| 2534 | // [5]lop = (-lop_x , -lop_y) |
| 2535 | // [6]lop = (-sqrt(-1), 0 ) |
| 2536 | // [7]lop = (-lop_x , lop_y) |
| 2537 | // |
| 2538 | // The x coordinate is either 0, sqrt(-1), lop_x, or their opposite. |
| 2539 | // The y coordinate is either 0, -1 , lop_y, or their opposite. |
| 2540 | // The pattern for both is the same, except for a rotation of 2 (modulo 8) |
| 2541 | // |
| 2542 | // This helper function captures the pattern, and we can use it thus: |
| 2543 | // |
| 2544 | // select_lop(x, lop_x, sqrtm1, cofactor); |
| 2545 | // select_lop(y, lop_y, fe_one, cofactor + 2); |
| 2546 | // |
| 2547 | // This is faster than an actual scalar multiplication, |
| 2548 | // and requires less code than naive constant time look up. |
| 2549 | static void select_lop(fe out, const fe x, const fe k, u8 cofactor) |
| 2550 | { |
| 2551 | fe tmp; |
| 2552 | fe_0(out); |
| 2553 | fe_ccopy(out, k , (cofactor >> 1) & 1); // bit 1 |
| 2554 | fe_ccopy(out, x , (cofactor >> 0) & 1); // bit 0 |
| 2555 | fe_neg (tmp, out); |
| 2556 | fe_ccopy(out, tmp, (cofactor >> 2) & 1); // bit 2 |
| 2557 | WIPE_BUFFER(tmp); |
| 2558 | } |
| 2559 | |
| 2560 | // "Fast" dirty ephemeral key |
| 2561 | // We use this one by default. |
| 2562 | // |
| 2563 | // This version works by performing a regular scalar multiplication, |
| 2564 | // then add a low order point. The scalar multiplication is done in |
| 2565 | // Edwards space for more speed (*2 compared to the "small" version). |
| 2566 | // The cost is a bigger binary for programs that don't also sign messages. |
| 2567 | static void x25519_dirty_fast(u8 public_key[32], const u8 secret_key[32], |
| 2568 | int add_lop) |
| 2569 | { |
| 2570 | // Compute clean scalar multiplication |
| 2571 | u8 scalar[32]; |
| 2572 | ge pk; |
| 2573 | crypto_eddsa_trim_scalar(scalar, secret_key); |
| 2574 | ge_scalarmult_base(&pk, scalar); |
| 2575 | |
| 2576 | // Compute low order point |
| 2577 | fe t1, t2; |
| 2578 | ge_precomp low_order_point; |
| 2579 | if (add_lop) { |
| 2580 | select_lop(t1, lop_x, sqrtm1, secret_key[0]); |
| 2581 | select_lop(t2, lop_y, fe_one, secret_key[0] + 2); |
| 2582 | fe_add(low_order_point.Yp, t2, t1); |
| 2583 | fe_sub(low_order_point.Ym, t2, t1); |
| 2584 | fe_mul(low_order_point.T2, t2, t1); |
| 2585 | fe_mul(low_order_point.T2, low_order_point.T2, D2); |
| 2586 | |
| 2587 | // Add low order point to the public key |
| 2588 | ge_madd(&pk, &pk, &low_order_point, t1, t2); |
| 2589 | } |
| 2590 | |
| 2591 | // Convert to Montgomery u coordinate (we ignore the sign) |
| 2592 | // u = (1+y)/(1-y) = (Z+Y)/(Z-Y) |
| 2593 | fe_add(t1, pk.Z, pk.Y); |
| 2594 | fe_sub(t2, pk.Z, pk.Y); |
| 2595 | fe_invert(t2, t2); |
| 2596 | fe_mul(t1, t1, t2); |
| 2597 | fe_tobytes(public_key, t1); |
| 2598 | |
| 2599 | WIPE_BUFFER(t1); WIPE_CTX(&pk); |
| 2600 | WIPE_BUFFER(t2); WIPE_CTX(&low_order_point); |
| 2601 | WIPE_BUFFER(scalar); |
| 2602 | } |
| 2603 | |
| 2604 | void crypto_x25519_dirty_fast(u8 public_key[32], const u8 secret_key[32]) |
| 2605 | { |
| 2606 | x25519_dirty_fast(public_key, secret_key, 1); |
| 2607 | } |
| 2608 | |
| 2609 | void crypto_x25519_public_key(u8 public_key[32], const u8 secret_key[32]) |
| 2610 | { |
| 2611 | // Scalar multiplication is about twice as fast in Edwards space. |
| 2612 | x25519_dirty_fast(public_key, secret_key, 0); |
| 2613 | } |
| 2614 | |
| 2615 | /////////////////// |
| 2616 | /// Elligator 2 /// |
| 2617 | /////////////////// |
| 2618 | static const fe A = {486662}; |
| 2619 | |
| 2620 | // Elligator direct map |
| 2621 | // |
| 2622 | // Compute the point corresponding to a representative, encoded in 32 |
| 2623 | // bytes (little Endian). Since positive representatives fits in 254 |
| 2624 | // bits, The two most significant bits are ignored. |
| 2625 | // |
| 2626 | // From the paper: |
| 2627 | // w = -A / (fe(1) + non_square * r^2) |
| 2628 | // e = chi(w^3 + A*w^2 + w) |
| 2629 | // u = e*w - (fe(1)-e)*(A//2) |
| 2630 | // v = -e * sqrt(u^3 + A*u^2 + u) |
| 2631 | // |
| 2632 | // We ignore v because we don't need it for X25519 (the Montgomery |
| 2633 | // ladder only uses u). |
| 2634 | // |
| 2635 | // Note that e is either 0, 1 or -1 |
| 2636 | // if e = 0 u = 0 and v = 0 |
| 2637 | // if e = 1 u = w |
| 2638 | // if e = -1 u = -w - A = w * non_square * r^2 |
| 2639 | // |
| 2640 | // Let r1 = non_square * r^2 |
| 2641 | // Let r2 = 1 + r1 |
| 2642 | // Note that r2 cannot be zero, -1/non_square is not a square. |
| 2643 | // We can (tediously) verify that: |
| 2644 | // w^3 + A*w^2 + w = (A^2*r1 - r2^2) * A / r2^3 |
| 2645 | // Therefore: |
| 2646 | // chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * (A / r2^3)) |
| 2647 | // chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * (A / r2^3)) * 1 |
| 2648 | // chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * (A / r2^3)) * chi(r2^6) |
| 2649 | // chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * (A / r2^3) * r2^6) |
| 2650 | // chi(w^3 + A*w^2 + w) = chi((A^2*r1 - r2^2) * A * r2^3) |
| 2651 | // Corollary: |
| 2652 | // e = 1 if (A^2*r1 - r2^2) * A * r2^3) is a non-zero square |
| 2653 | // e = -1 if (A^2*r1 - r2^2) * A * r2^3) is not a square |
| 2654 | // Note that w^3 + A*w^2 + w (and therefore e) can never be zero: |
| 2655 | // w^3 + A*w^2 + w = w * (w^2 + A*w + 1) |
| 2656 | // w^3 + A*w^2 + w = w * (w^2 + A*w + A^2/4 - A^2/4 + 1) |
| 2657 | // w^3 + A*w^2 + w = w * (w + A/2)^2 - A^2/4 + 1) |
| 2658 | // which is zero only if: |
| 2659 | // w = 0 (impossible) |
| 2660 | // (w + A/2)^2 = A^2/4 - 1 (impossible, because A^2/4-1 is not a square) |
| 2661 | // |
| 2662 | // Let isr = invsqrt((A^2*r1 - r2^2) * A * r2^3) |
| 2663 | // isr = sqrt(1 / ((A^2*r1 - r2^2) * A * r2^3)) if e = 1 |
| 2664 | // isr = sqrt(sqrt(-1) / ((A^2*r1 - r2^2) * A * r2^3)) if e = -1 |
| 2665 | // |
| 2666 | // if e = 1 |
| 2667 | // let u1 = -A * (A^2*r1 - r2^2) * A * r2^2 * isr^2 |
| 2668 | // u1 = w |
| 2669 | // u1 = u |
| 2670 | // |
| 2671 | // if e = -1 |
| 2672 | // let ufactor = -non_square * sqrt(-1) * r^2 |
| 2673 | // let vfactor = sqrt(ufactor) |
| 2674 | // let u2 = -A * (A^2*r1 - r2^2) * A * r2^2 * isr^2 * ufactor |
| 2675 | // u2 = w * -1 * -non_square * r^2 |
| 2676 | // u2 = w * non_square * r^2 |
| 2677 | // u2 = u |
| 2678 | void crypto_elligator_map(u8 curve[32], const u8 hidden[32]) |
| 2679 | { |
| 2680 | fe r, u, t1, t2, t3; |
| 2681 | fe_frombytes_mask(r, hidden, 2); // r is encoded in 254 bits. |
| 2682 | fe_sq(r, r); |
| 2683 | fe_add(t1, r, r); |
| 2684 | fe_add(u, t1, fe_one); |
| 2685 | fe_sq (t2, u); |
| 2686 | fe_mul(t3, A2, t1); |
| 2687 | fe_sub(t3, t3, t2); |
| 2688 | fe_mul(t3, t3, A); |
| 2689 | fe_mul(t1, t2, u); |
| 2690 | fe_mul(t1, t3, t1); |
| 2691 | int is_square = invsqrt(t1, t1); |
| 2692 | fe_mul(u, r, ufactor); |
| 2693 | fe_ccopy(u, fe_one, is_square); |
| 2694 | fe_sq (t1, t1); |
| 2695 | fe_mul(u, u, A); |
| 2696 | fe_mul(u, u, t3); |
| 2697 | fe_mul(u, u, t2); |
| 2698 | fe_mul(u, u, t1); |
| 2699 | fe_neg(u, u); |
| 2700 | fe_tobytes(curve, u); |
| 2701 | |
| 2702 | WIPE_BUFFER(t1); WIPE_BUFFER(r); |
| 2703 | WIPE_BUFFER(t2); WIPE_BUFFER(u); |
| 2704 | WIPE_BUFFER(t3); |
| 2705 | } |
| 2706 | |
| 2707 | // Elligator inverse map |
| 2708 | // |
| 2709 | // Compute the representative of a point, if possible. If not, do |
| 2710 | // nothing and return -1. Note that the success of the operation |
| 2711 | // depends only on the point (more precisely its u coordinate). The |
| 2712 | // tweak parameter is used only upon success. |
| 2713 | // |
| 2714 | // The tweak should be a random byte. Beyond that, its contents are an |
| 2715 | // implementation detail. Currently, the tweak comprises: |
| 2716 | // - Bit 1 : sign of the v coordinate (0 if positive, 1 if negative) |
| 2717 | // - Bit 2-5: not used |
| 2718 | // - Bits 6-7: random padding |
| 2719 | // |
| 2720 | // From the paper: |
| 2721 | // Let sq = -non_square * u * (u+A) |
| 2722 | // if sq is not a square, or u = -A, there is no mapping |
| 2723 | // Assuming there is a mapping: |
| 2724 | // if v is positive: r = sqrt(-u / (non_square * (u+A))) |
| 2725 | // if v is negative: r = sqrt(-(u+A) / (non_square * u )) |
| 2726 | // |
| 2727 | // We compute isr = invsqrt(-non_square * u * (u+A)) |
| 2728 | // if it wasn't a square, abort. |
| 2729 | // else, isr = sqrt(-1 / (non_square * u * (u+A)) |
| 2730 | // |
| 2731 | // If v is positive, we return isr * u: |
| 2732 | // isr * u = sqrt(-1 / (non_square * u * (u+A)) * u |
| 2733 | // isr * u = sqrt(-u / (non_square * (u+A)) |
| 2734 | // |
| 2735 | // If v is negative, we return isr * (u+A): |
| 2736 | // isr * (u+A) = sqrt(-1 / (non_square * u * (u+A)) * (u+A) |
| 2737 | // isr * (u+A) = sqrt(-(u+A) / (non_square * u) |
| 2738 | int crypto_elligator_rev(u8 hidden[32], const u8 public_key[32], u8 tweak) |
| 2739 | { |
| 2740 | fe t1, t2, t3; |
| 2741 | fe_frombytes(t1, public_key); // t1 = u |
| 2742 | |
| 2743 | fe_add(t2, t1, A); // t2 = u + A |
| 2744 | fe_mul(t3, t1, t2); |
| 2745 | fe_mul_small(t3, t3, -2); |
| 2746 | int is_square = invsqrt(t3, t3); // t3 = sqrt(-1 / non_square * u * (u+A)) |
| 2747 | if (is_square) { |
| 2748 | // The only variable time bit. This ultimately reveals how many |
| 2749 | // tries it took us to find a representable key. |
| 2750 | // This does not affect security as long as we try keys at random. |
| 2751 | |
| 2752 | fe_ccopy (t1, t2, tweak & 1); // multiply by u if v is positive, |
| 2753 | fe_mul (t3, t1, t3); // multiply by u+A otherwise |
| 2754 | fe_mul_small(t1, t3, 2); |
| 2755 | fe_neg (t2, t3); |
| 2756 | fe_ccopy (t3, t2, fe_isodd(t1)); |
| 2757 | fe_tobytes(hidden, t3); |
| 2758 | |
| 2759 | // Pad with two random bits |
| 2760 | hidden[31] |= tweak & 0xc0; |
| 2761 | } |
| 2762 | |
| 2763 | WIPE_BUFFER(t1); |
| 2764 | WIPE_BUFFER(t2); |
| 2765 | WIPE_BUFFER(t3); |
| 2766 | return is_square - 1; |
| 2767 | } |
| 2768 | |
| 2769 | void crypto_elligator_key_pair(u8 hidden[32], u8 secret_key[32], u8 seed[32]) |
| 2770 | { |
| 2771 | u8 pk [32]; // public key |
| 2772 | u8 buf[64]; // seed + representative |
| 2773 | COPY(buf + 32, seed, 32); |
| 2774 | do { |
| 2775 | crypto_chacha20_djb(buf, 0, 64, buf+32, zero, 0); |
| 2776 | crypto_x25519_dirty_fast(pk, buf); // or the "small" version |
| 2777 | } while(crypto_elligator_rev(buf+32, pk, buf[32])); |
| 2778 | // Note that the return value of crypto_elligator_rev() is |
| 2779 | // independent from its tweak parameter. |
| 2780 | // Therefore, buf[32] is not actually reused. Either we loop one |
| 2781 | // more time and buf[32] is used for the new seed, or we succeeded, |
| 2782 | // and buf[32] becomes the tweak parameter. |
| 2783 | |
| 2784 | crypto_wipe(seed, 32); |
| 2785 | COPY(hidden , buf + 32, 32); |
| 2786 | COPY(secret_key, buf , 32); |
| 2787 | WIPE_BUFFER(buf); |
| 2788 | WIPE_BUFFER(pk); |
| 2789 | } |
| 2790 | |
| 2791 | /////////////////////// |
| 2792 | /// Scalar division /// |
| 2793 | /////////////////////// |
| 2794 | |
| 2795 | // Montgomery reduction. |
| 2796 | // Divide x by (2^256), and reduce the result modulo L |
| 2797 | // |
| 2798 | // Precondition: |
| 2799 | // x < L * 2^256 |
| 2800 | // Constants: |
| 2801 | // r = 2^256 (makes division by r trivial) |
| 2802 | // k = (r * (1/r) - 1) // L (1/r is computed modulo L ) |
| 2803 | // Algorithm: |
| 2804 | // s = (x * k) % r |
| 2805 | // t = x + s*L (t is always a multiple of r) |
| 2806 | // u = (t/r) % L (u is always below 2*L, conditional subtraction is enough) |
| 2807 | static void redc(u32 u[8], u32 x[16]) |
| 2808 | { |
| 2809 | static const u32 k[8] = { |
| 2810 | 0x12547e1b, 0xd2b51da3, 0xfdba84ff, 0xb1a206f2, |
| 2811 | 0xffa36bea, 0x14e75438, 0x6fe91836, 0x9db6c6f2, |
| 2812 | }; |
| 2813 | |
| 2814 | // s = x * k (modulo 2^256) |
| 2815 | // This is cheaper than the full multiplication. |
| 2816 | u32 s[8] = {0}; |
| 2817 | FOR (i, 0, 8) { |
| 2818 | u64 carry = 0; |
| 2819 | FOR (j, 0, 8-i) { |
| 2820 | carry += s[i+j] + (u64)x[i] * k[j]; |
| 2821 | s[i+j] = (u32)carry; |
| 2822 | carry >>= 32; |
| 2823 | } |
| 2824 | } |
| 2825 | u32 t[16] = {0}; |
| 2826 | multiply(t, s, L); |
| 2827 | |
| 2828 | // t = t + x |
| 2829 | u64 carry = 0; |
| 2830 | FOR (i, 0, 16) { |
| 2831 | carry += (u64)t[i] + x[i]; |
| 2832 | t[i] = (u32)carry; |
| 2833 | carry >>= 32; |
| 2834 | } |
| 2835 | |
| 2836 | // u = (t / 2^256) % L |
| 2837 | // Note that t / 2^256 is always below 2*L, |
| 2838 | // So a constant time conditional subtraction is enough |
| 2839 | remove_l(u, t+8); |
| 2840 | |
| 2841 | WIPE_BUFFER(s); |
| 2842 | WIPE_BUFFER(t); |
| 2843 | } |
| 2844 | |
| 2845 | void crypto_x25519_inverse(u8 blind_salt [32], const u8 private_key[32], |
| 2846 | const u8 curve_point[32]) |
| 2847 | { |
| 2848 | static const u8 Lm2[32] = { // L - 2 |
| 2849 | 0xeb, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
| 2850 | 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
| 2851 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2852 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, |
| 2853 | }; |
| 2854 | // 1 in Montgomery form |
| 2855 | u32 m_inv [8] = { |
| 2856 | 0x8d98951d, 0xd6ec3174, 0x737dcf70, 0xc6ef5bf4, |
| 2857 | 0xfffffffe, 0xffffffff, 0xffffffff, 0x0fffffff, |
| 2858 | }; |
| 2859 | |
| 2860 | u8 scalar[32]; |
| 2861 | crypto_eddsa_trim_scalar(scalar, private_key); |
| 2862 | |
| 2863 | // Convert the scalar in Montgomery form |
| 2864 | // m_scl = scalar * 2^256 (modulo L) |
| 2865 | u32 m_scl[8]; |
| 2866 | { |
| 2867 | u32 tmp[16]; |
| 2868 | ZERO(tmp, 8); |
| 2869 | load32_le_buf(tmp+8, scalar, 8); |
| 2870 | mod_l(scalar, tmp); |
| 2871 | load32_le_buf(m_scl, scalar, 8); |
| 2872 | WIPE_BUFFER(tmp); // Wipe ASAP to save stack space |
| 2873 | } |
| 2874 | |
| 2875 | // Compute the inverse |
| 2876 | u32 product[16]; |
| 2877 | for (int i = 252; i >= 0; i--) { |
| 2878 | ZERO(product, 16); |
| 2879 | multiply(product, m_inv, m_inv); |
| 2880 | redc(m_inv, product); |
| 2881 | if (scalar_bit(Lm2, i)) { |
| 2882 | ZERO(product, 16); |
| 2883 | multiply(product, m_inv, m_scl); |
| 2884 | redc(m_inv, product); |
| 2885 | } |
| 2886 | } |
| 2887 | // Convert the inverse *out* of Montgomery form |
| 2888 | // scalar = m_inv / 2^256 (modulo L) |
| 2889 | COPY(product, m_inv, 8); |
| 2890 | ZERO(product + 8, 8); |
| 2891 | redc(m_inv, product); |
| 2892 | store32_le_buf(scalar, m_inv, 8); // the *inverse* of the scalar |
| 2893 | |
| 2894 | // Clear the cofactor of scalar: |
| 2895 | // cleared = scalar * (3*L + 1) (modulo 8*L) |
| 2896 | // cleared = scalar + scalar * 3 * L (modulo 8*L) |
| 2897 | // Note that (scalar * 3) is reduced modulo 8, so we only need the |
| 2898 | // first byte. |
| 2899 | add_xl(scalar, scalar[0] * 3); |
| 2900 | |
| 2901 | // Recall that 8*L < 2^256. However it is also very close to |
| 2902 | // 2^255. If we spanned the ladder over 255 bits, random tests |
| 2903 | // wouldn't catch the off-by-one error. |
| 2904 | scalarmult(blind_salt, scalar, curve_point, 256); |
| 2905 | |
| 2906 | WIPE_BUFFER(scalar); WIPE_BUFFER(m_scl); |
| 2907 | WIPE_BUFFER(product); WIPE_BUFFER(m_inv); |
| 2908 | } |
| 2909 | |
| 2910 | //////////////////////////////// |
| 2911 | /// Authenticated encryption /// |
| 2912 | //////////////////////////////// |
| 2913 | static void lock_auth(u8 mac[16], const u8 auth_key[32], |
| 2914 | const u8 *ad , size_t ad_size, |
| 2915 | const u8 *cipher_text, size_t text_size) |
| 2916 | { |
| 2917 | u8 sizes[16]; // Not secret, not wiped |
| 2918 | store64_le(sizes + 0, ad_size); |
| 2919 | store64_le(sizes + 8, text_size); |
| 2920 | crypto_poly1305_ctx poly_ctx; // Auto wiped... |
| 2921 | crypto_poly1305_init (&poly_ctx, auth_key); |
| 2922 | crypto_poly1305_update(&poly_ctx, ad , ad_size); |
| 2923 | crypto_poly1305_update(&poly_ctx, zero , gap(ad_size, 16)); |
| 2924 | crypto_poly1305_update(&poly_ctx, cipher_text, text_size); |
| 2925 | crypto_poly1305_update(&poly_ctx, zero , gap(text_size, 16)); |
| 2926 | crypto_poly1305_update(&poly_ctx, sizes , 16); |
| 2927 | crypto_poly1305_final (&poly_ctx, mac); // ...here |
| 2928 | } |
| 2929 | |
| 2930 | void crypto_aead_init_x(crypto_aead_ctx *ctx, |
| 2931 | u8 const key[32], const u8 nonce[24]) |
| 2932 | { |
| 2933 | crypto_chacha20_h(ctx->key, key, nonce); |
| 2934 | COPY(ctx->nonce, nonce + 16, 8); |
| 2935 | ctx->counter = 0; |
| 2936 | } |
| 2937 | |
| 2938 | void crypto_aead_init_djb(crypto_aead_ctx *ctx, |
| 2939 | const u8 key[32], const u8 nonce[8]) |
| 2940 | { |
| 2941 | COPY(ctx->key , key , 32); |
| 2942 | COPY(ctx->nonce, nonce, 8); |
| 2943 | ctx->counter = 0; |
| 2944 | } |
| 2945 | |
| 2946 | void crypto_aead_init_ietf(crypto_aead_ctx *ctx, |
| 2947 | const u8 key[32], const u8 nonce[12]) |
| 2948 | { |
| 2949 | COPY(ctx->key , key , 32); |
| 2950 | COPY(ctx->nonce, nonce + 4, 8); |
| 2951 | ctx->counter = (u64)load32_le(nonce) << 32; |
| 2952 | } |
| 2953 | |
| 2954 | void crypto_aead_write(crypto_aead_ctx *ctx, u8 *cipher_text, u8 mac[16], |
| 2955 | const u8 *ad, size_t ad_size, |
| 2956 | const u8 *plain_text, size_t text_size) |
| 2957 | { |
| 2958 | u8 auth_key[64]; // The last 32 bytes are used for rekeying. |
| 2959 | crypto_chacha20_djb(auth_key, 0, 64, ctx->key, ctx->nonce, ctx->counter); |
| 2960 | crypto_chacha20_djb(cipher_text, plain_text, text_size, |
| 2961 | ctx->key, ctx->nonce, ctx->counter + 1); |
| 2962 | lock_auth(mac, auth_key, ad, ad_size, cipher_text, text_size); |
| 2963 | COPY(ctx->key, auth_key + 32, 32); |
| 2964 | WIPE_BUFFER(auth_key); |
| 2965 | } |
| 2966 | |
| 2967 | int crypto_aead_read(crypto_aead_ctx *ctx, u8 *plain_text, const u8 mac[16], |
| 2968 | const u8 *ad, size_t ad_size, |
| 2969 | const u8 *cipher_text, size_t text_size) |
| 2970 | { |
| 2971 | u8 auth_key[64]; // The last 32 bytes are used for rekeying. |
| 2972 | u8 real_mac[16]; |
| 2973 | crypto_chacha20_djb(auth_key, 0, 64, ctx->key, ctx->nonce, ctx->counter); |
| 2974 | lock_auth(real_mac, auth_key, ad, ad_size, cipher_text, text_size); |
| 2975 | int mismatch = crypto_verify16(mac, real_mac); |
| 2976 | if (!mismatch) { |
| 2977 | crypto_chacha20_djb(plain_text, cipher_text, text_size, |
| 2978 | ctx->key, ctx->nonce, ctx->counter + 1); |
| 2979 | COPY(ctx->key, auth_key + 32, 32); |
| 2980 | } |
| 2981 | WIPE_BUFFER(auth_key); |
| 2982 | WIPE_BUFFER(real_mac); |
| 2983 | return mismatch; |
| 2984 | } |
| 2985 | |
| 2986 | void crypto_aead_lock(u8 *cipher_text, u8 mac[16], const u8 key[32], |
| 2987 | const u8 nonce[24], const u8 *ad, size_t ad_size, |
| 2988 | const u8 *plain_text, size_t text_size) |
| 2989 | { |
| 2990 | crypto_aead_ctx ctx; |
| 2991 | crypto_aead_init_x(&ctx, key, nonce); |
| 2992 | crypto_aead_write(&ctx, cipher_text, mac, ad, ad_size, |
| 2993 | plain_text, text_size); |
| 2994 | crypto_wipe(&ctx, sizeof(ctx)); |
| 2995 | } |
| 2996 | |
| 2997 | int crypto_aead_unlock(u8 *plain_text, const u8 mac[16], const u8 key[32], |
| 2998 | const u8 nonce[24], const u8 *ad, size_t ad_size, |
| 2999 | const u8 *cipher_text, size_t text_size) |
| 3000 | { |
| 3001 | crypto_aead_ctx ctx; |
| 3002 | crypto_aead_init_x(&ctx, key, nonce); |
| 3003 | int mismatch = crypto_aead_read(&ctx, plain_text, mac, ad, ad_size, |
| 3004 | cipher_text, text_size); |
| 3005 | crypto_wipe(&ctx, sizeof(ctx)); |
| 3006 | return mismatch; |
| 3007 | } |
| 3008 | |
| 3009 | #ifdef MONOCYPHER_CPP_NAMESPACE |
| 3010 | } |
| 3011 | #endif |