| 1 | #include <stdbool.h> |
| 2 | #include <stdint.h> |
| 3 | |
| 4 | #include <sc.h> |
| 5 | #include <sodium.h> |
| 6 | |
| 7 | // ge_scalarmult_base, sc_reduce, sc_muladd all store private data on the stack without overwriting it before |
| 8 | // returning. The sodium_stackzero function solves that by allocating a configurable amount of space on the |
| 9 | // stack and overwriting it in the hope to catch all leftovers of stack variables allocated by previously |
| 10 | // called functions. ge_scalarmult_base allocates around 470 bytes of stack, sc_reduce around 330 and |
| 11 | // sc_muladd around 660 bytes which makes it the biggest stack consumer. Together with things like parameters |
| 12 | // and the return addresses lying on the stack, at least 700 bytes have to be cleared. To be sure (and to have |
| 13 | // a power of two!), STACKZERO_SIZE is set to 1024. |
| 14 | #define STACKZERO_SIZE 1024 |
| 15 | |
| 16 | // Byte-representation of the scalar value of 0 on the Ed25519 curve. Needed by `sc_neg`. |
| 17 | static const uint8_t ZERO[32] = { |
| 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 19 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 22 | }; |
| 23 | |
| 24 | // Byte-representation of the scalar value of -1 on the Ed25519 curve. Needed by `sc_neg`. |
| 25 | static const uint8_t MINUS_ONE[32] = { |
| 26 | 0xec, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, |
| 27 | 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, |
| 28 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 29 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * Clamp a scalar. This operation is idempotent. |
| 34 | * |
| 35 | * @param scalar_out (out): The clamped output scalar. |
| 36 | * @param scalar_in (in): The input scalar. |
| 37 | */ |
| 38 | static void sc_clamp(uint8_t* scalar_out, const uint8_t* scalar_in) { |
| 39 | // Mandated by RFC 7748: https://tools.ietf.org/html/rfc7748#section-5 |
| 40 | // Referred to as "clamping": https://moderncrypto.org/mail-archive/curves/2017/000858.html |
| 41 | for (uint8_t i = 0; i < 32; i++) |
| 42 | scalar_out[i] = scalar_in[i]; |
| 43 | |
| 44 | scalar_out[ 0] = scalar_in[ 0] & 248; |
| 45 | scalar_out[31] = scalar_in[31] & 127 | 64; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Constant-time implementation of a conditional move without conditional branching. |
| 50 | * |
| 51 | * @param scalar_inout (inout): The first input scalar as well as the output. |
| 52 | * @param scalar_in (in): The second input scalar. |
| 53 | * @param move: Whether to move the second scalar into the first or not. |
| 54 | * |
| 55 | * @warning The value of `move` is not cleared from the stack before returning. Thus, if the value of `move` |
| 56 | * contains private data, make sure to clean the stack after the call returns. |
| 57 | */ |
| 58 | static void sc_cmov(uint8_t* scalar_inout, const uint8_t* scalar_in, const bool move) { |
| 59 | // The goal is a bitmask with either all ones or all zeros, based on the boolean value of move and in |
| 60 | // constant time without conditional branches. |
| 61 | // The code first negates `move` twice logically to get either a one or a zero. |
| 62 | // Then it subtracts one from `move`, which either results in 0x00 (if `move` is true) or in 0xff due to |
| 63 | // an 8 bit underflow (if move is false). |
| 64 | uint8_t mask = ((uint8_t) !!move) - 1; |
| 65 | |
| 66 | // First, XOR both possible results |
| 67 | for (uint8_t i = 0; i < 32; i++) |
| 68 | scalar_inout[i] ^= scalar_in[i]; |
| 69 | |
| 70 | // Second, and the condition mask to the mixture, which results in either of: |
| 71 | // - out = (out ^ in) & 0x00 = 0x00, if `move` is true |
| 72 | // - out = (out ^ in) & 0xff = out ^ in, if `move` is false |
| 73 | for (uint8_t i = 0; i < 32; i++) |
| 74 | scalar_inout[i] &= mask; |
| 75 | |
| 76 | // Third, XOR out with in again, which results in either of: |
| 77 | // - out ^ in = (0 ) ^ in = in, if `move` is true |
| 78 | // - out ^ in = (out ^ in) ^ in = out, if `move` is false |
| 79 | for (uint8_t i = 0; i < 32; i++) |
| 80 | scalar_inout[i] ^= scalar_in[i]; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Negate a scalar on the Ed25519 curve. |
| 85 | * |
| 86 | * @param scalar_out (out): The negated scalar. |
| 87 | * @param scalar_in (in): The scalar to negate. |
| 88 | */ |
| 89 | static void sc_neg(uint8_t* scalar_out, const uint8_t* scalar_in) { |
| 90 | // ref10 only provides sc_muladd to calculate (ab+c) mod l. |
| 91 | // This can be used to negate a scalar by choosing a = -1 mod l, b = scalar, c = 0. |
| 92 | // The "difficult" part is calculating -1 mod l, where l = 2^252 + 27742317777372353535851937790883648493. |
| 93 | // Python to the rescue! |
| 94 | // Running (-1 % l).to_bytes(32, "little") yields the byte sequence stored in MINUS_ONE. |
| 95 | sc_muladd(scalar_out, MINUS_ONE, scalar_in, ZERO); |
| 96 | |
| 97 | sodium_stackzero(STACKZERO_SIZE); |
| 98 | } |