

#include <stdio.h>
#include <string.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>




static const char *propq = NULL;

static const unsigned char peer1_privk_data[32] = {
    0x80, 0x5b, 0x30, 0x20, 0x25, 0x4a, 0x70, 0x2c,
    0xad, 0xa9, 0x8d, 0x7d, 0x47, 0xf8, 0x1b, 0x20,
    0x89, 0xd2, 0xf9, 0x14, 0xac, 0x92, 0x27, 0xf2,
    0x10, 0x7e, 0xdb, 0x21, 0xbd, 0x73, 0x73, 0x5d
};

static const unsigned char peer2_privk_data[32] = {
    0xf8, 0x84, 0x19, 0x69, 0x79, 0x13, 0x0d, 0xbd,
    0xb1, 0x76, 0xd7, 0x0e, 0x7e, 0x0f, 0xb6, 0xf4,
    0x8c, 0x4a, 0x8c, 0x5f, 0xd8, 0x15, 0x09, 0x0a,
    0x71, 0x78, 0x74, 0x92, 0x0f, 0x85, 0xc8, 0x43
};

static const unsigned char expected_result[32] = {
    0x19, 0x71, 0x26, 0x12, 0x74, 0xb5, 0xb1, 0xce,
    0x77, 0xd0, 0x79, 0x24, 0xb6, 0x0a, 0x5c, 0x72,
    0x0c, 0xa6, 0x56, 0xc0, 0x11, 0xeb, 0x43, 0x11,
    0x94, 0x3b, 0x01, 0x45, 0xca, 0x19, 0xfe, 0x09
};

typedef struct peer_data_st {
    const char *name;               
    EVP_PKEY *privk;                
    unsigned char pubk_data[32];    

    unsigned char *secret;          
    size_t secret_len;
} PEER_DATA;


static int keyexch_x25519_before(
    OSSL_LIB_CTX *libctx,
    const unsigned char *kat_privk_data,
    PEER_DATA *local_peer)
{
    int ret = 0;
    size_t pubk_data_len = 0;

    
    if (kat_privk_data != NULL)
        local_peer->privk =
            EVP_PKEY_new_raw_private_key_ex(libctx, "X25519", propq,
                                            kat_privk_data,
                                            sizeof(peer1_privk_data));
    else
        local_peer->privk = EVP_PKEY_Q_keygen(libctx, propq, "X25519");

    if (local_peer->privk == NULL) {
        fprintf(stderr, "Could not load or generate private key\n");
        goto end;
    }

    
    if (EVP_PKEY_get_octet_string_param(local_peer->privk,
                                        OSSL_PKEY_PARAM_PUB_KEY,
                                        local_peer->pubk_data,
                                        sizeof(local_peer->pubk_data),
                                        &pubk_data_len) == 0) {
        fprintf(stderr, "EVP_PKEY_get_octet_string_param() failed\n");
        goto end;
    }

    
    if (pubk_data_len != 32) {
        fprintf(stderr, "EVP_PKEY_get_octet_string_param() "
                "yielded wrong length\n");
        goto end;
    }

    ret = 1;
end:
    if (ret == 0) {
        EVP_PKEY_free(local_peer->privk);
        local_peer->privk = NULL;
    }

    return ret;
}


static int keyexch_x25519_after(
    OSSL_LIB_CTX *libctx,
    int use_kat,
    PEER_DATA *local_peer,
    const unsigned char *remote_peer_pubk_data)
{
    int ret = 0;
    EVP_PKEY *remote_peer_pubk = NULL;
    EVP_PKEY_CTX *ctx = NULL;

    local_peer->secret = NULL;

    
    remote_peer_pubk =
        EVP_PKEY_new_raw_public_key_ex(libctx, "X25519", propq,
                                       remote_peer_pubk_data, 32);
    if (remote_peer_pubk == NULL) {
        fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");
        goto end;
    }

    
    ctx = EVP_PKEY_CTX_new_from_pkey(libctx, local_peer->privk, propq);
    if (ctx == NULL) {
        fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
        goto end;
    }

    
    if (EVP_PKEY_derive_init(ctx) == 0) {
        fprintf(stderr, "EVP_PKEY_derive_init() failed\n");
        goto end;
    }

    
    if (EVP_PKEY_derive_set_peer(ctx, remote_peer_pubk) == 0) {
        fprintf(stderr, "EVP_PKEY_derive_set_peer() failed\n");
        goto end;
    }

    
    if (EVP_PKEY_derive(ctx, NULL, &local_peer->secret_len) == 0) {
        fprintf(stderr, "EVP_PKEY_derive() failed\n");
        goto end;
    }

    
    if (local_peer->secret_len != 32) { 
        fprintf(stderr, "Secret is always 32 bytes for X25519\n");
        goto end;
    }

    
    local_peer->secret = OPENSSL_malloc(local_peer->secret_len);
    if (local_peer->secret == NULL) {
        fprintf(stderr, "Could not allocate memory for secret\n");
        goto end;
    }

    
    if (EVP_PKEY_derive(ctx, local_peer->secret,
                        &local_peer->secret_len) == 0) {
        fprintf(stderr, "EVP_PKEY_derive() failed\n");
        goto end;
    }

    printf("Shared secret (%s):\n", local_peer->name);
    BIO_dump_indent_fp(stdout, local_peer->secret, local_peer->secret_len, 2);
    putchar('\n');

    ret = 1;
end:
    EVP_PKEY_CTX_free(ctx);
    EVP_PKEY_free(remote_peer_pubk);
    if (ret == 0) {
        OPENSSL_clear_free(local_peer->secret, local_peer->secret_len);
        local_peer->secret = NULL;
    }

    return ret;
}

static int keyexch_x25519(int use_kat)
{
    int ret = 0;
    OSSL_LIB_CTX *libctx = NULL;
    PEER_DATA peer1 = {"peer 1"}, peer2 = {"peer 2"};

    
    if (keyexch_x25519_before(libctx, use_kat ? peer1_privk_data : NULL,
                              &peer1) == 0)
        return 0;

    if (keyexch_x25519_before(libctx, use_kat ? peer2_privk_data : NULL,
                              &peer2) == 0)
        return 0;

    
    if (keyexch_x25519_after(libctx, use_kat, &peer1, peer2.pubk_data) == 0)
        return 0;

    if (keyexch_x25519_after(libctx, use_kat, &peer2, peer1.pubk_data) == 0)
        return 0;

    
    if (CRYPTO_memcmp(peer1.secret, peer2.secret, peer1.secret_len) != 0) {
        fprintf(stderr, "Negotiated secrets do not match\n");
        goto end;
    }

    
    if (use_kat && CRYPTO_memcmp(peer1.secret, expected_result,
                                 peer1.secret_len) != 0) {
        fprintf(stderr, "Did not get expected result\n");
        goto end;
    }

    ret = 1;
end:
    
    OPENSSL_clear_free(peer1.secret, peer1.secret_len);
    OPENSSL_clear_free(peer2.secret, peer2.secret_len);

    EVP_PKEY_free(peer1.privk);
    EVP_PKEY_free(peer2.privk);
    OSSL_LIB_CTX_free(libctx);
    return ret;
}

int main(int argc, char **argv)
{
    
    printf("Key exchange using known answer (deterministic):\n");
    if (keyexch_x25519(1) == 0)
        return EXIT_FAILURE;

    
    printf("Key exchange using random keys:\n");
    if (keyexch_x25519(0) == 0)
        return EXIT_FAILURE;

    return EXIT_SUCCESS;
}
