33#include <openssl/evp.h>
34#include <openssl/err.h>
39static int n_ecc_keypair(
int type,
unsigned char* priv,
unsigned char* pub,
const char* name) {
45 EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(
type, NULL);
46 EVP_PKEY* pkey = NULL;
52 if (EVP_PKEY_keygen_init(ctx) != 1 || EVP_PKEY_keygen(ctx, &pkey) != 1) {
57 if (EVP_PKEY_get_raw_private_key(pkey, priv, &plen) != 1 || plen !=
N_ECC_KEY_LEN) {
62 if (EVP_PKEY_get_raw_public_key(pkey, pub, &plen) != 1 || plen !=
N_ECC_KEY_LEN) {
71 EVP_PKEY_CTX_free(ctx);
76 return n_ecc_keypair(EVP_PKEY_X25519, priv, pub,
"n_x25519_keypair");
80 return n_ecc_keypair(EVP_PKEY_ED25519, priv, pub,
"n_ed25519_keypair");
83int n_x25519_derive(
const unsigned char* priv,
const unsigned char* peer_pub,
unsigned char* shared) {
84 if (!priv || !peer_pub || !shared) {
89 EVP_PKEY* pk = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, priv,
N_ECC_KEY_LEN);
90 EVP_PKEY* peer = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, peer_pub,
N_ECC_KEY_LEN);
91 EVP_PKEY_CTX* dctx = NULL;
97 dctx = EVP_PKEY_CTX_new(pk, NULL);
98 if (!dctx || EVP_PKEY_derive_init(dctx) != 1 || EVP_PKEY_derive_set_peer(dctx, peer) != 1) {
102 if (EVP_PKEY_derive(dctx, shared, &slen) != 1 || slen !=
N_ECC_KEY_LEN) {
110 EVP_PKEY_CTX_free(dctx);
116int n_ed25519_sign(
const unsigned char* priv,
const unsigned char* msg,
size_t len,
unsigned char* sig) {
117 if (!priv || !sig || (!msg && len > 0)) {
122 EVP_PKEY* pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, priv,
N_ECC_KEY_LEN);
123 EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
125 if (!pkey || !mdctx) {
129 if (EVP_DigestSignInit(mdctx, NULL, NULL, NULL, pkey) != 1) {
133 if (EVP_DigestSign(mdctx, sig, &siglen, msg, len) != 1 || siglen !=
N_ECC_SIG_LEN) {
141 EVP_MD_CTX_free(mdctx);
146int n_ed25519_verify(
const unsigned char* pub,
const unsigned char* msg,
size_t len,
const unsigned char* sig) {
147 if (!pub || !sig || (!msg && len > 0)) {
152 EVP_PKEY* pkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, pub,
N_ECC_KEY_LEN);
153 EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
154 if (!pkey || !mdctx) {
158 if (EVP_DigestVerifyInit(mdctx, NULL, NULL, NULL, pkey) != 1) {
164 if (EVP_DigestVerify(mdctx, sig,
N_ECC_SIG_LEN, msg, len) == 1)
176 EVP_MD_CTX_free(mdctx);
193 n_log(
LOG_ERR,
"n_x25519_keypair: requires OpenSSL 1.1.1+ (raw EVP_PKEY X25519 keys)");
200 n_log(
LOG_ERR,
"n_ed25519_keypair: requires OpenSSL 1.1.1+ (raw EVP_PKEY Ed25519 keys)");
204int n_x25519_derive(
const unsigned char* priv,
const unsigned char* peer_pub,
unsigned char* shared) {
208 n_log(
LOG_ERR,
"n_x25519_derive: requires OpenSSL 1.1.1+ (raw EVP_PKEY X25519 keys)");
212int n_ed25519_sign(
const unsigned char* priv,
const unsigned char* msg,
size_t len,
unsigned char* sig) {
217 n_log(
LOG_ERR,
"n_ed25519_sign: requires OpenSSL 1.1.1+ (raw EVP_PKEY Ed25519 keys)");
221int n_ed25519_verify(
const unsigned char* pub,
const unsigned char* msg,
size_t len,
const unsigned char* sig) {
226 n_log(
LOG_ERR,
"n_ed25519_verify: requires OpenSSL 1.1.1+ (raw EVP_PKEY Ed25519 keys)");
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
volatile sig_atomic_t done
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
int n_ed25519_keypair(unsigned char *priv, unsigned char *pub)
generate an Ed25519 keypair as N_ECC_KEY_LEN-byte raw private and public keys.
int n_ed25519_verify(const unsigned char *pub, const unsigned char *msg, size_t len, const unsigned char *sig)
Ed25519 verify the N_ECC_SIG_LEN-byte sig over len bytes of msg under the N_ECC_KEY_LEN-byte raw publ...
int n_x25519_keypair(unsigned char *priv, unsigned char *pub)
generate an X25519 keypair as N_ECC_KEY_LEN-byte raw private and public keys.
#define N_ECC_SIG_LEN
Ed25519 signature length in bytes.
#define N_ECC_KEY_LEN
raw X25519/Ed25519 key length in bytes
int n_ed25519_sign(const unsigned char *priv, const unsigned char *msg, size_t len, unsigned char *sig)
Ed25519 sign len bytes of msg with the N_ECC_KEY_LEN-byte raw private key into sig (N_ECC_SIG_LEN byt...
int n_x25519_derive(const unsigned char *priv, const unsigned char *peer_pub, unsigned char *shared)
X25519 ECDH: derive the N_ECC_KEY_LEN-byte shared secret from our raw private key and the peer's raw ...
Curve25519 primitives: X25519 key agreement and Ed25519 signatures.