| 1 | #include <ge.h> |
| 2 | #include <sodium.h> |
| 3 | |
| 4 | #include <xeddsa.h> |
| 5 | #include "utils.c" |
| 6 | |
| 7 | /** |
| 8 | * Derive the Curve25519 public key from a Curve25519/Ed25519 private key. |
| 9 | * |
| 10 | * @param curve25519_pub (out): The Curve25519 public key. |
| 11 | * @param priv (in): The Curve25519/Ed25519 private key. |
| 12 | */ |
| 13 | void priv_to_curve25519_pub(uint8_t* curve25519_pub, const uint8_t* priv) { |
| 14 | uint8_t priv_clamped[32]; |
| 15 | |
| 16 | // Clamp the private key before using it |
| 17 | sc_clamp(priv_clamped, priv); |
| 18 | |
| 19 | // Calculate the Curve25519 public key from the private key. Note that while the function is declared to |
| 20 | // return an integer, it always returns the constant value of 0, and can thus be treated as a void. |
| 21 | crypto_scalarmult_curve25519_base(curve25519_pub, priv_clamped); |
| 22 | |
| 23 | sodium_memzero(priv_clamped, sizeof(priv_clamped)); |
| 24 | sodium_stackzero(STACKZERO_SIZE); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Derive the Ed25519 public key from a Curve25519/Ed25519 private key. |
| 29 | * |
| 30 | * @param ed25519_pub (out): The Ed25519 public key. |
| 31 | * @param priv (in): The Curve25519/Ed25519 private key. |
| 32 | */ |
| 33 | void priv_to_ed25519_pub(uint8_t* ed25519_pub, const uint8_t* priv) { |
| 34 | ge_p3 ed25519_pub_ge; |
| 35 | |
| 36 | uint8_t priv_clamped[32]; |
| 37 | |
| 38 | // Clamp the private key before using it |
| 39 | sc_clamp(priv_clamped, priv); |
| 40 | |
| 41 | // Calculate the Ed25519 public key from the private key |
| 42 | ge_scalarmult_base(&ed25519_pub_ge, priv_clamped); |
| 43 | ge_p3_tobytes(ed25519_pub, &ed25519_pub_ge); |
| 44 | |
| 45 | sodium_memzero(priv_clamped, sizeof(priv_clamped)); |
| 46 | sodium_stackzero(STACKZERO_SIZE); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Derive the Ed25519 public key from a Curve25519/Ed25519 seed. |
| 51 | * |
| 52 | * @param ed25519_pub (out): The Ed25519 public key. |
| 53 | * @param seed (in): The Curve25519/Ed25519 seed. |
| 54 | */ |
| 55 | void seed_to_ed25519_pub(uint8_t* ed25519_pub, const uint8_t* seed) { |
| 56 | uint8_t priv[32]; |
| 57 | |
| 58 | seed_to_priv(priv, seed); |
| 59 | priv_to_ed25519_pub(ed25519_pub, priv); |
| 60 | |
| 61 | sodium_memzero(priv, sizeof(priv)); |
| 62 | } |