| 1 | #include <ge.h> |
| 2 | #include <sc.h> |
| 3 | #include <sodium.h> |
| 4 | |
| 5 | #include <xeddsa.h> |
| 6 | #include "utils.c" |
| 7 | |
| 8 | // Needed by `ed25519_priv_sign`. |
| 9 | static const uint8_t HASH_PADDING[32] = { |
| 10 | 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 11 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 12 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 13 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
| 14 | }; |
| 15 | |
| 16 | /** |
| 17 | * Sign a message using a Curve25519/Ed25519 private key. |
| 18 | * |
| 19 | * @param sig (out): An Ed25519-compatible signature of `msg`. |
| 20 | * @param priv (in): The Curve25519/Ed25519 private key to sign with. |
| 21 | * @param msg (in): The message to sign. |
| 22 | * @param msg_size (in): Size of the message to sign, in bytes. |
| 23 | * @param nonce (in): 64 bytes of secure random data. |
| 24 | */ |
| 25 | void ed25519_priv_sign(uint8_t* sig, |
| 26 | const uint8_t* priv, |
| 27 | const uint8_t* msg, |
| 28 | const uint32_t msg_size, |
| 29 | const uint8_t* nonce) { |
| 30 | ge_p3 ed25519_pub_ge; |
| 31 | uint8_t A[32]; |
| 32 | |
| 33 | crypto_hash_sha512_state state; |
| 34 | |
| 35 | uint8_t r[64]; |
| 36 | uint8_t h[64]; |
| 37 | uint8_t s[32]; |
| 38 | |
| 39 | ge_p3 R_ge; |
| 40 | uint8_t R[32]; |
| 41 | |
| 42 | // Calculate the Ed25519 public key from the Ed25519 private key |
| 43 | ge_scalarmult_base(&ed25519_pub_ge, priv); |
| 44 | ge_p3_tobytes(A, &ed25519_pub_ge); |
| 45 | |
| 46 | // r = hash_1(a || M || Z) (mod q) |
| 47 | // where: |
| 48 | // hash_i(X) = sha512(2 ^ 256 - 1 - i || X) |
| 49 | // Sadly, the ref implementation of SHA-512 only comes with a single-part API and not with a |
| 50 | // multi-part/streaming API. This makes it very inconvenient to use here. Instead, the streaming API of |
| 51 | // libsodium's SHA-512 implementation is used. libsodium also comes with useful helpers to wipe private |
| 52 | // data off the stack without being scared of the compiler optimizing it away. Note that the function |
| 53 | // signatures of the sha512 API are declared to return an integer, however the implementations always |
| 54 | // return the hardcoded value of 0 from all code paths. |
| 55 | crypto_hash_sha512_init(&state); |
| 56 | crypto_hash_sha512_update(&state, HASH_PADDING, 32); |
| 57 | crypto_hash_sha512_update(&state, priv, 32); |
| 58 | crypto_hash_sha512_update(&state, msg, msg_size); |
| 59 | crypto_hash_sha512_update(&state, nonce, 64); |
| 60 | crypto_hash_sha512_final(&state, r); |
| 61 | sc_reduce(r); |
| 62 | |
| 63 | // R = rB |
| 64 | ge_scalarmult_base(&R_ge, r); |
| 65 | ge_p3_tobytes(R, &R_ge); |
| 66 | |
| 67 | // h = hash(R || A || M) (mod q) |
| 68 | // where: |
| 69 | // hash(X) = sha512(X) |
| 70 | crypto_hash_sha512_init(&state); |
| 71 | crypto_hash_sha512_update(&state, R, 32); |
| 72 | crypto_hash_sha512_update(&state, A, 32); |
| 73 | crypto_hash_sha512_update(&state, msg, msg_size); |
| 74 | crypto_hash_sha512_final(&state, h); |
| 75 | sc_reduce(h); |
| 76 | |
| 77 | // s = r + ha (mod q) |
| 78 | sc_muladd(s, h, priv, r); |
| 79 | |
| 80 | // return R || s |
| 81 | for (uint8_t i = 0; i < 32; i++) |
| 82 | *sig++ = R[i]; |
| 83 | for (uint8_t i = 0; i < 32; i++) |
| 84 | *sig++ = s[i]; |
| 85 | |
| 86 | sodium_memzero(&state, sizeof(crypto_hash_sha512_state)); |
| 87 | sodium_memzero(r, sizeof(r)); |
| 88 | sodium_stackzero(STACKZERO_SIZE); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sign a message using a Curve25519/Ed25519 seed. |
| 93 | * |
| 94 | * @param sig (out): An Ed25519-compatible signature of `msg`. |
| 95 | * @param seed (in): The Curve25519/Ed25519 seed to sign with. |
| 96 | * @param msg (in): The message to sign. |
| 97 | * @param msg_size (in): Size of the message to sign, in bytes. |
| 98 | */ |
| 99 | void ed25519_seed_sign(uint8_t* sig, const uint8_t* seed, const uint8_t* msg, const uint32_t msg_size) { |
| 100 | // libsodium needs seed and public key in 64 adjacent bytes |
| 101 | uint8_t seed_and_pub[64]; |
| 102 | |
| 103 | for (uint8_t i = 0; i < 32; i++) |
| 104 | seed_and_pub[i] = seed[i]; |
| 105 | |
| 106 | seed_to_ed25519_pub(&seed_and_pub[32], seed); |
| 107 | |
| 108 | // Returns the constant value of 0, thus the return value can be ignored. |
| 109 | crypto_sign_ed25519_detached(sig, NULL, msg, msg_size, seed_and_pub); |
| 110 | |
| 111 | sodium_memzero(seed_and_pub, sizeof(seed_and_pub)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Verify an Ed25519 signature. |
| 116 | * |
| 117 | * @param sig (in): An Ed25519-compatible signature of `msg`. |
| 118 | * @param ed25519_pub (in): The Ed25519 public key to verify the signature with. |
| 119 | * @param msg (in): The message. |
| 120 | * @param msg_size (in): Size of the message, in bytes. |
| 121 | * @return 0 in case of success, -1 if the verification fails. |
| 122 | */ |
| 123 | int ed25519_verify(const uint8_t* sig, |
| 124 | const uint8_t* ed25519_pub, |
| 125 | const uint8_t* msg, |
| 126 | const uint32_t msg_size) { |
| 127 | return crypto_sign_verify_detached(sig, msg, msg_size, ed25519_pub); |
| 128 | } |