



#include "internal/deprecated.h"

#include <string.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/kdf.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <openssl/rand.h>
#include <openssl/proverr.h>
#include "prov/provider_ctx.h"
#include "prov/implementations.h"
#include "prov/securitycheck.h"
#include "prov/providercommon.h"
#include "prov/ecx.h"
#include "crypto/ecx.h"
#include <openssl/hpke.h>
#include "internal/hpke_util.h"
#include "eckem.h"

#define MAX_ECX_KEYLEN X448_KEYLEN


#define KEMID_X25519_HKDF_SHA256 0x20
#define KEMID_X448_HKDF_SHA512   0x21


static const char LABEL_KEM[] = "\x4b\x45\x4d";

typedef struct {
    ECX_KEY *recipient_key;
    ECX_KEY *sender_authkey;
    OSSL_LIB_CTX *libctx;
    char *propq;
    unsigned int mode;
    unsigned int op;
    unsigned char *ikm;
    size_t ikmlen;
    const char *kdfname;
    const OSSL_HPKE_KEM_INFO *info;
} PROV_ECX_CTX;

static OSSL_FUNC_kem_newctx_fn ecxkem_newctx;
static OSSL_FUNC_kem_encapsulate_init_fn ecxkem_encapsulate_init;
static OSSL_FUNC_kem_encapsulate_fn ecxkem_encapsulate;
static OSSL_FUNC_kem_decapsulate_init_fn ecxkem_decapsulate_init;
static OSSL_FUNC_kem_decapsulate_fn ecxkem_decapsulate;
static OSSL_FUNC_kem_freectx_fn ecxkem_freectx;
static OSSL_FUNC_kem_set_ctx_params_fn ecxkem_set_ctx_params;
static OSSL_FUNC_kem_auth_encapsulate_init_fn ecxkem_auth_encapsulate_init;
static OSSL_FUNC_kem_auth_decapsulate_init_fn ecxkem_auth_decapsulate_init;


static const OSSL_HPKE_KEM_INFO *get_kem_info(ECX_KEY *ecx)
{
    const char *name = NULL;

    if (ecx->type == ECX_KEY_TYPE_X25519)
        name = SN_X25519;
    else
        name = SN_X448;
    return ossl_HPKE_KEM_INFO_find_curve(name);
}


static int recipient_key_set(PROV_ECX_CTX *ctx, ECX_KEY *ecx)
{
    ossl_ecx_key_free(ctx->recipient_key);
    ctx->recipient_key = NULL;
    if (ecx != NULL) {
        ctx->info = get_kem_info(ecx);
        if (ctx->info == NULL)
            return -2;
        ctx->kdfname = "HKDF";
        if (!ossl_ecx_key_up_ref(ecx))
            return 0;
        ctx->recipient_key = ecx;
    }
    return 1;
}


static int sender_authkey_set(PROV_ECX_CTX *ctx, ECX_KEY *ecx)
{
    ossl_ecx_key_free(ctx->sender_authkey);
    ctx->sender_authkey = NULL;

    if (ecx != NULL) {
        if (!ossl_ecx_key_up_ref(ecx))
            return 0;
        ctx->sender_authkey = ecx;
    }
    return 1;
}


static ECX_KEY *ecxkey_pubfromdata(PROV_ECX_CTX *ctx,
                                   const unsigned char *pubbuf, size_t pubbuflen)
{
    ECX_KEY *ecx = NULL;
    OSSL_PARAM params[2], *p = params;

    *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
                                             (char *)pubbuf, pubbuflen);
    *p = OSSL_PARAM_construct_end();

    ecx = ossl_ecx_key_new(ctx->libctx, ctx->recipient_key->type, 1, ctx->propq);
    if (ecx == NULL)
        return NULL;
    if (ossl_ecx_key_fromdata(ecx, params, 0) <= 0) {
        ossl_ecx_key_free(ecx);
        ecx = NULL;
    }
    return ecx;
}

static unsigned char *ecx_pubkey(ECX_KEY *ecx)
{
    if (ecx == NULL || !ecx->haspubkey) {
        ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
        return 0;
    }
    return ecx->pubkey;
}

static void *ecxkem_newctx(void *provctx)
{
    PROV_ECX_CTX *ctx =  OPENSSL_zalloc(sizeof(PROV_ECX_CTX));

    if (ctx == NULL)
        return NULL;
    ctx->libctx = PROV_LIBCTX_OF(provctx);

    return ctx;
}

static void ecxkem_freectx(void *vectx)
{
    PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vectx;

    OPENSSL_clear_free(ctx->ikm, ctx->ikmlen);
    recipient_key_set(ctx, NULL);
    sender_authkey_set(ctx, NULL);
    OPENSSL_free(ctx);
}

static int ecx_match_params(const ECX_KEY *key1, const ECX_KEY *key2)
{
    return (key1->type == key2->type && key1->keylen == key2->keylen);
}

static int ecx_key_check(const ECX_KEY *ecx, int requires_privatekey)
{
    if (ecx->privkey == NULL)
        return (requires_privatekey == 0);
    return 1;
}

static int ecxkem_init(void *vecxctx, int operation, void *vecx, void *vauth,
                       ossl_unused const OSSL_PARAM params[])
{
    int rv;
    PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vecxctx;
    ECX_KEY *ecx = vecx;
    ECX_KEY *auth = vauth;

    if (!ossl_prov_is_running())
        return 0;

    if (!ecx_key_check(ecx, operation == EVP_PKEY_OP_DECAPSULATE))
        return 0;
    rv = recipient_key_set(ctx, ecx);
    if (rv <= 0)
        return rv;

    if (auth != NULL) {
        if (!ecx_match_params(auth, ctx->recipient_key)
                || !ecx_key_check(auth, operation == EVP_PKEY_OP_ENCAPSULATE)
                || !sender_authkey_set(ctx, auth))
            return 0;
    }

    ctx->op = operation;
    return ecxkem_set_ctx_params(vecxctx, params);
}

static int ecxkem_encapsulate_init(void *vecxctx, void *vecx,
                                   const OSSL_PARAM params[])
{
    return ecxkem_init(vecxctx, EVP_PKEY_OP_ENCAPSULATE, vecx, NULL, params);
}

static int ecxkem_decapsulate_init(void *vecxctx, void *vecx,
                                   const OSSL_PARAM params[])
{
    return ecxkem_init(vecxctx, EVP_PKEY_OP_DECAPSULATE, vecx, NULL, params);
}

static int ecxkem_auth_encapsulate_init(void *vctx, void *vecx, void *vauthpriv,
                                        const OSSL_PARAM params[])
{
    return ecxkem_init(vctx, EVP_PKEY_OP_ENCAPSULATE, vecx, vauthpriv, params);
}

static int ecxkem_auth_decapsulate_init(void *vctx, void *vecx, void *vauthpub,
                                        const OSSL_PARAM params[])
{
    return ecxkem_init(vctx, EVP_PKEY_OP_DECAPSULATE, vecx, vauthpub, params);
}

static int ecxkem_set_ctx_params(void *vctx, const OSSL_PARAM params[])
{
    PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vctx;
    const OSSL_PARAM *p;
    int mode;

    if (ctx == NULL)
        return 0;
    if (params == NULL)
        return 1;

    p = OSSL_PARAM_locate_const(params, OSSL_KEM_PARAM_IKME);
    if (p != NULL) {
        void *tmp = NULL;
        size_t tmplen = 0;

        if (p->data != NULL && p->data_size != 0) {
            if (!OSSL_PARAM_get_octet_string(p, &tmp, 0, &tmplen))
                return 0;
        }
        OPENSSL_clear_free(ctx->ikm, ctx->ikmlen);
        ctx->ikm = tmp;
        ctx->ikmlen = tmplen;
    }
    p = OSSL_PARAM_locate_const(params, OSSL_KEM_PARAM_OPERATION);
    if (p != NULL) {
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
            return 0;
        mode = ossl_eckem_modename2id(p->data);
        if (mode == KEM_MODE_UNDEFINED)
            return 0;
        ctx->mode = mode;
    }
    return 1;
}

static const OSSL_PARAM known_settable_ecxkem_ctx_params[] = {
    OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0),
    OSSL_PARAM_octet_string(OSSL_KEM_PARAM_IKME, NULL, 0),
    OSSL_PARAM_END
};

static const OSSL_PARAM *ecxkem_settable_ctx_params(ossl_unused void *vctx,
                                                   ossl_unused void *provctx)
{
    return known_settable_ecxkem_ctx_params;
}


static int dhkem_extract_and_expand(EVP_KDF_CTX *kctx,
                                    unsigned char *okm, size_t okmlen,
                                    uint16_t kemid,
                                    const unsigned char *dhkm, size_t dhkmlen,
                                    const unsigned char *kemctx,
                                    size_t kemctxlen)
{
    uint8_t suiteid[2];
    uint8_t prk[EVP_MAX_MD_SIZE];
    size_t prklen = okmlen; 
    int ret;

    if (prklen > sizeof(prk))
        return 0;

    suiteid[0] = (kemid >> 8) &0xff;
    suiteid[1] = kemid & 0xff;

    ret = ossl_hpke_labeled_extract(kctx, prk, prklen,
                                    NULL, 0, LABEL_KEM, suiteid, sizeof(suiteid),
                                    OSSL_DHKEM_LABEL_EAE_PRK, dhkm, dhkmlen)
          && ossl_hpke_labeled_expand(kctx, okm, okmlen, prk, prklen,
                                      LABEL_KEM, suiteid, sizeof(suiteid),
                                      OSSL_DHKEM_LABEL_SHARED_SECRET,
                                      kemctx, kemctxlen);
    OPENSSL_cleanse(prk, prklen);
    return ret;
}


int ossl_ecx_dhkem_derive_private(ECX_KEY *ecx, unsigned char *privout,
                                  const unsigned char *ikm, size_t ikmlen)
{
    int ret = 0;
    EVP_KDF_CTX *kdfctx = NULL;
    unsigned char prk[EVP_MAX_MD_SIZE];
    uint8_t suiteid[2];
    const OSSL_HPKE_KEM_INFO *info = get_kem_info(ecx);

    
    if (ikmlen < info->Nsk) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH,
                       "ikm length is :%zu, should be at least %zu",
                       ikmlen, info->Nsk);
        goto err;
    }

    kdfctx = ossl_kdf_ctx_create("HKDF", info->mdname, ecx->libctx, ecx->propq);
    if (kdfctx == NULL)
        return 0;

    suiteid[0] = info->kem_id / 256;
    suiteid[1] = info->kem_id % 256;

    if (!ossl_hpke_labeled_extract(kdfctx, prk, info->Nsecret,
                                   NULL, 0, LABEL_KEM, suiteid, sizeof(suiteid),
                                   OSSL_DHKEM_LABEL_DKP_PRK, ikm, ikmlen))
        goto err;

    if (!ossl_hpke_labeled_expand(kdfctx, privout, info->Nsk, prk, info->Nsecret,
                                  LABEL_KEM, suiteid, sizeof(suiteid),
                                  OSSL_DHKEM_LABEL_SK, NULL, 0))
        goto err;
    ret = 1;
err:
    OPENSSL_cleanse(prk, sizeof(prk));
    EVP_KDF_CTX_free(kdfctx);
    return ret;
}


static ECX_KEY *derivekey(PROV_ECX_CTX *ctx,
                          const unsigned char *ikm, size_t ikmlen)
{
    int ok = 0;
    ECX_KEY *key;
    unsigned char *privkey;
    unsigned char *seed = (unsigned char *)ikm;
    size_t seedlen = ikmlen;
    unsigned char tmpbuf[OSSL_HPKE_MAX_PRIVATE];
    const OSSL_HPKE_KEM_INFO *info = ctx->info;

    key = ossl_ecx_key_new(ctx->libctx, ctx->recipient_key->type, 0, ctx->propq);
    if (key == NULL)
        return NULL;
    privkey = ossl_ecx_key_allocate_privkey(key);
    if (privkey == NULL)
        goto err;

    
    if (seed == NULL || seedlen == 0) {
        if (info->Nsk > sizeof(tmpbuf))
            goto err;
        if (RAND_priv_bytes_ex(ctx->libctx, tmpbuf, info->Nsk, 0) <= 0)
            goto err;
        seed = tmpbuf;
        seedlen = info->Nsk;
    }
    if (!ossl_ecx_dhkem_derive_private(key, privkey, seed, seedlen))
        goto err;
    if (!ossl_ecx_public_from_private(key))
        goto err;
    key->haspubkey = 1;
    ok = 1;
err:
    if (!ok) {
        ossl_ecx_key_free(key);
        key = NULL;
    }
    if (seed != ikm)
        OPENSSL_cleanse(seed, seedlen);
    return key;
}


static int generate_ecxdhkm(const ECX_KEY *sender, const ECX_KEY *peer,
                           unsigned char *out,  size_t maxout,
                           unsigned int secretsz)
{
    size_t len = 0;

    
    return ossl_ecx_compute_key((ECX_KEY *)peer, (ECX_KEY *)sender,
                                 sender->keylen, out, &len, maxout);
}


static int derive_secret(PROV_ECX_CTX *ctx, unsigned char *secret,
                         const ECX_KEY *privkey1, const ECX_KEY *peerkey1,
                         const ECX_KEY *privkey2, const ECX_KEY *peerkey2,
                         const unsigned char *sender_pub,
                         const unsigned char *recipient_pub)
{
    int ret = 0;
    EVP_KDF_CTX *kdfctx = NULL;
    unsigned char *sender_authpub = NULL;
    unsigned char dhkm[MAX_ECX_KEYLEN * 2];
    unsigned char kemctx[MAX_ECX_KEYLEN * 3];
    size_t kemctxlen = 0, dhkmlen = 0;
    const OSSL_HPKE_KEM_INFO *info = ctx->info;
    int auth = ctx->sender_authkey != NULL;
    size_t encodedkeylen = info->Npk;

    if (!generate_ecxdhkm(privkey1, peerkey1, dhkm, sizeof(dhkm), encodedkeylen))
        goto err;
    dhkmlen = encodedkeylen;

    
    if (auth) {
        if (!generate_ecxdhkm(privkey2, peerkey2,
                              dhkm + dhkmlen, sizeof(dhkm) - dhkmlen,
                              encodedkeylen))
            goto err;
        
        sender_authpub = ecx_pubkey(ctx->sender_authkey);
        if (sender_authpub == NULL)
            goto err;
        dhkmlen += encodedkeylen;
    }
    kemctxlen = encodedkeylen + dhkmlen;
    if (kemctxlen > sizeof(kemctx))
        goto err;

    
    memcpy(kemctx, sender_pub, encodedkeylen);
    memcpy(kemctx + encodedkeylen, recipient_pub, encodedkeylen);
    if (auth)
        memcpy(kemctx + 2 * encodedkeylen, sender_authpub, encodedkeylen);
    kdfctx = ossl_kdf_ctx_create(ctx->kdfname, info->mdname,
                                 ctx->libctx, ctx->propq);
    if (kdfctx == NULL)
        goto err;
    if (!dhkem_extract_and_expand(kdfctx, secret, info->Nsecret,
                                  info->kem_id, dhkm, dhkmlen,
                                  kemctx, kemctxlen))
        goto err;
    ret = 1;
err:
    OPENSSL_cleanse(dhkm, dhkmlen);
    EVP_KDF_CTX_free(kdfctx);
    return ret;
}


static int dhkem_encap(PROV_ECX_CTX *ctx,
                       unsigned char *enc, size_t *enclen,
                       unsigned char *secret, size_t *secretlen)
{
    int ret = 0;
    ECX_KEY *sender_ephemkey = NULL;
    unsigned char *sender_ephempub, *recipient_pub;
    const OSSL_HPKE_KEM_INFO *info = ctx->info;

    if (enc == NULL) {
        if (enclen == NULL && secretlen == NULL)
            return 0;
        if (enclen != NULL)
            *enclen = info->Nenc;
        if (secretlen != NULL)
            *secretlen = info->Nsecret;
       return 1;
    }

    if (*secretlen < info->Nsecret) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*secretlen too small");
        return 0;
    }
    if (*enclen < info->Nenc) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*enclen too small");
        return 0;
    }

    
    sender_ephemkey = derivekey(ctx, ctx->ikm, ctx->ikmlen);

    sender_ephempub = ecx_pubkey(sender_ephemkey);
    recipient_pub = ecx_pubkey(ctx->recipient_key);
    if (sender_ephempub == NULL || recipient_pub == NULL)
        goto err;

    if (!derive_secret(ctx, secret,
                       sender_ephemkey, ctx->recipient_key,
                       ctx->sender_authkey, ctx->recipient_key,
                       sender_ephempub, recipient_pub))
        goto err;

    
    memcpy(enc, sender_ephempub, info->Nenc);
    *enclen = info->Nenc;
    *secretlen = info->Nsecret;
    ret = 1;
err:
    ossl_ecx_key_free(sender_ephemkey);
    return ret;
}


static int dhkem_decap(PROV_ECX_CTX *ctx,
                       unsigned char *secret, size_t *secretlen,
                       const unsigned char *enc, size_t enclen)
{
    int ret = 0;
    ECX_KEY *recipient_privkey = ctx->recipient_key;
    ECX_KEY *sender_ephempubkey = NULL;
    const OSSL_HPKE_KEM_INFO *info = ctx->info;
    unsigned char *recipient_pub;

    if (secret == NULL) {
        *secretlen = info->Nsecret;
        return 1;
    }
    if (*secretlen < info->Nsecret) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*secretlen too small");
        return 0;
    }
    if (enclen != info->Nenc) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, "Invalid enc public key");
        return 0;
    }

    
    sender_ephempubkey = ecxkey_pubfromdata(ctx, enc, enclen);
    if (sender_ephempubkey == NULL)
        goto err;

    recipient_pub = ecx_pubkey(recipient_privkey);
    if (recipient_pub == NULL)
        goto err;

    if (!derive_secret(ctx, secret,
                       ctx->recipient_key, sender_ephempubkey,
                       ctx->recipient_key, ctx->sender_authkey,
                       enc, recipient_pub))
        goto err;

    *secretlen = info->Nsecret;
    ret = 1;
err:
    ossl_ecx_key_free(sender_ephempubkey);
    return ret;
}

static int ecxkem_encapsulate(void *vctx, unsigned char *out, size_t *outlen,
                              unsigned char *secret, size_t *secretlen)
{
    PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vctx;

    switch (ctx->mode) {
        case KEM_MODE_DHKEM:
            return dhkem_encap(ctx, out, outlen, secret, secretlen);
        default:
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
            return -2;
    }
}

static int ecxkem_decapsulate(void *vctx, unsigned char *out, size_t *outlen,
                              const unsigned char *in, size_t inlen)
{
    PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vctx;

    switch (ctx->mode) {
        case KEM_MODE_DHKEM:
            return dhkem_decap(vctx, out, outlen, in, inlen);
        default:
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
            return -2;
    }
}

const OSSL_DISPATCH ossl_ecx_asym_kem_functions[] = {
    { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))ecxkem_newctx },
    { OSSL_FUNC_KEM_ENCAPSULATE_INIT,
      (void (*)(void))ecxkem_encapsulate_init },
    { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))ecxkem_encapsulate },
    { OSSL_FUNC_KEM_DECAPSULATE_INIT,
      (void (*)(void))ecxkem_decapsulate_init },
    { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))ecxkem_decapsulate },
    { OSSL_FUNC_KEM_FREECTX, (void (*)(void))ecxkem_freectx },
    { OSSL_FUNC_KEM_SET_CTX_PARAMS,
      (void (*)(void))ecxkem_set_ctx_params },
    { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,
      (void (*)(void))ecxkem_settable_ctx_params },
    { OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT,
      (void (*)(void))ecxkem_auth_encapsulate_init },
    { OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT,
      (void (*)(void))ecxkem_auth_decapsulate_init },
    OSSL_DISPATCH_END
};
