| 1 | #cxx . . $ `<file>/src/ed25519.c`/include <ge.h>cxx . . $ `<file>/ref10/crypto_sign/ge.h`/ |
| 2 | #include <sc.h>cxx . . $ `<file>/ref10/crypto_sign/sc.h`/ |
| 3 | #include <sodium.h> |
| 4 | |
| 5 | #include <xeddsa.h>cxx . . $ `<file>/include/xeddsa.h`/ |
| 6 | #include "utils.c"cxx . . $ `<file>/src/utils.c`/ |
| 7 | |
| 8 | // Needed by `ed25519_priv_sign`. |
| 9 | static const uint8_tcxx . . $ uint8_t# HASH_PADDINGcxx . . $ 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_signcxx . . $ ed25519_priv_sign(185a75904022be09).(uint8_tcxx . . $ uint8_t#* sig, |
| 26 | const uint8_tcxx . . $ uint8_t#* priv, |
| 27 | const uint8_tcxx . . $ uint8_t#* msg, |
| 28 | const uint32_tcxx . . $ uint32_t# msg_size, |
| 29 | const uint8_tcxx . . $ uint8_t#* nonce) { |
| 30 | ge_p3cxx . . $ ge_p3# ed25519_pub_ge; |
| 31 | uint8_tcxx . . $ uint8_t# A[32]; |
| 32 | |
| 33 | crypto_hash_sha512_statecxx . . $ crypto_hash_sha512_state# state; |
| 34 | |
| 35 | uint8_tcxx . . $ uint8_t# r[64]; |
| 36 | uint8_tcxx . . $ uint8_t# h[64]; |
| 37 | uint8_tcxx . . $ uint8_t# s[32]; |
| 38 | |
| 39 | ge_p3cxx . . $ ge_p3# R_ge; |
| 40 | uint8_tcxx . . $ uint8_t# R[32]; |
| 41 | |
| 42 | // Calculate the Ed25519 public key from the Ed25519 private key |
| 43 | ge_scalarmult_basecxx . . $ `ref10/crypto_sign/ge.h:73:9`!(&ed25519_pub_ge, priv); |
| 44 | ge_p3_tobytescxx . . $ `ref10/crypto_sign/ge.h:57:9`!(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_initcxx . . $ crypto_hash_sha512_init(7098ee63d6f285c3).(&state); |
| 56 | crypto_hash_sha512_updatecxx . . $ crypto_hash_sha512_update(7367fcae7857a073).(&state, HASH_PADDINGcxx . . $ HASH_PADDING., 32); |
| 57 | crypto_hash_sha512_updatecxx . . $ crypto_hash_sha512_update(7367fcae7857a073).(&state, priv, 32); |
| 58 | crypto_hash_sha512_updatecxx . . $ crypto_hash_sha512_update(7367fcae7857a073).(&state, msg, msg_size); |
| 59 | crypto_hash_sha512_updatecxx . . $ crypto_hash_sha512_update(7367fcae7857a073).(&state, nonce, 64); |
| 60 | crypto_hash_sha512_finalcxx . . $ crypto_hash_sha512_final(eff971aa86333ecd).(&state, r); |
| 61 | sc_reducecxx . . $ `ref10/crypto_sign/sc.h:11:9`!(r); |
| 62 | |
| 63 | // R = rB |
| 64 | ge_scalarmult_basecxx . . $ `ref10/crypto_sign/ge.h:73:9`!(&R_ge, r); |
| 65 | ge_p3_tobytescxx . . $ `ref10/crypto_sign/ge.h:57:9`!(R, &R_ge); |
| 66 | |
| 67 | // h = hash(R || A || M) (mod q) |
| 68 | // where: |
| 69 | // hash(X) = sha512(X) |
| 70 | crypto_hash_sha512_initcxx . . $ crypto_hash_sha512_init(7098ee63d6f285c3).(&state); |
| 71 | crypto_hash_sha512_updatecxx . . $ crypto_hash_sha512_update(7367fcae7857a073).(&state, R, 32); |
| 72 | crypto_hash_sha512_updatecxx . . $ crypto_hash_sha512_update(7367fcae7857a073).(&state, A, 32); |
| 73 | crypto_hash_sha512_updatecxx . . $ crypto_hash_sha512_update(7367fcae7857a073).(&state, msg, msg_size); |
| 74 | crypto_hash_sha512_finalcxx . . $ crypto_hash_sha512_final(eff971aa86333ecd).(&state, h); |
| 75 | sc_reducecxx . . $ `ref10/crypto_sign/sc.h:11:9`!(h); |
| 76 | |
| 77 | // s = r + ha (mod q) |
| 78 | sc_muladdcxx . . $ `ref10/crypto_sign/sc.h:12:9`!(s, h, priv, r); |
| 79 | |
| 80 | // return R || s |
| 81 | for (uint8_tcxx . . $ uint8_t# i = 0; i < 32; i++) |
| 82 | *sig++ = R[i]; |
| 83 | for (uint8_tcxx . . $ uint8_t# i = 0; i < 32; i++) |
| 84 | *sig++ = s[i]; |
| 85 | |
| 86 | sodium_memzerocxx . . $ sodium_memzero(5500c4293b1bbef0).(&state, sizeof(crypto_hash_sha512_statecxx . . $ crypto_hash_sha512_state#)); |
| 87 | sodium_memzerocxx . . $ sodium_memzero(5500c4293b1bbef0).(r, sizeof(r)); |
| 88 | sodium_stackzerocxx . . $ sodium_stackzero(2c44cd906ae4e829).(STACKZERO_SIZEcxx . . $ `src/utils.c:14:9`!); |
| 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_signcxx . . $ ed25519_seed_sign(3dcce8d6aa664ff2).(uint8_tcxx . . $ uint8_t#* sig, const uint8_tcxx . . $ uint8_t#* seed, const uint8_tcxx . . $ uint8_t#* msg, const uint32_tcxx . . $ uint32_t# msg_size) { |
| 100 | // libsodium needs seed and public key in 64 adjacent bytes |
| 101 | uint8_tcxx . . $ uint8_t# seed_and_pub[64]; |
| 102 | |
| 103 | for (uint8_tcxx . . $ uint8_t# i = 0; i < 32; i++) |
| 104 | seed_and_pub[i] = seed[i]; |
| 105 | |
| 106 | seed_to_ed25519_pubcxx . . $ seed_to_ed25519_pub(225f8250db12d4ac).(&seed_and_pub[32], seed); |
| 107 | |
| 108 | // Returns the constant value of 0, thus the return value can be ignored. |
| 109 | crypto_sign_ed25519_detachedcxx . . $ crypto_sign_ed25519_detached(258a1c600d73c5a8).(sig, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, msg, msg_size, seed_and_pub); |
| 110 | |
| 111 | sodium_memzerocxx . . $ sodium_memzero(5500c4293b1bbef0).(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_verifycxx . . $ ed25519_verify(e3561976544fbcbb).(const uint8_tcxx . . $ uint8_t#* sig, |
| 124 | const uint8_tcxx . . $ uint8_t#* ed25519_pub, |
| 125 | const uint8_tcxx . . $ uint8_t#* msg, |
| 126 | const uint32_tcxx . . $ uint32_t# msg_size) { |
| 127 | return crypto_sign_verify_detachedcxx . . $ crypto_sign_verify_detached(7ea4d1276b7c96b0).(sig, msg, msg_size, ed25519_pub); |
| 128 | } |