

#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/core_names.h>
#include <openssl/kdf.h>
#include "internal/deterministic_nonce.h"


static int bits2int(BIGNUM *out, int qlen_bits,
                    const unsigned char *in, size_t inlen)
{
    int blen_bits = inlen * 8;
    int shift;

    if (BN_bin2bn(in, (int)inlen, out) == NULL)
        return 0;

    shift = blen_bits - qlen_bits;
    if (shift > 0)
        return BN_rshift(out, out, shift);
    return 1;
}


static int int2octets(unsigned char *out, const BIGNUM *num, int rlen)
{
    return BN_bn2binpad(num, out, rlen) >= 0;
}


static int bits2octets(unsigned char *out, const BIGNUM *q, int qlen_bits,
                       int rlen, const unsigned char *in, size_t inlen)
{
   int ret = 0;
   BIGNUM *z = BN_new();

   if (z == NULL
           || !bits2int(z, qlen_bits, in, inlen))
       goto err;

   
   if (BN_cmp(z, q) >= 0
           && !BN_usub(z, z, q))
       goto err;

   ret = int2octets(out, z, rlen);
err:
   BN_free(z);
   return ret;
}


static EVP_KDF_CTX *kdf_setup(const char *digestname,
                              const unsigned char *entropy, size_t entropylen,
                              const unsigned char *nonce, size_t noncelen,
                              OSSL_LIB_CTX *libctx, const char *propq)
{
    EVP_KDF_CTX *ctx = NULL;
    EVP_KDF *kdf = NULL;
    OSSL_PARAM params[5], *p;

    kdf = EVP_KDF_fetch(libctx, "HMAC-DRBG-KDF", propq);
    ctx = EVP_KDF_CTX_new(kdf);
    EVP_KDF_free(kdf);
    if (ctx == NULL)
        goto err;

    p = params;
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
                                            (char *)digestname, 0);
    if (propq != NULL)
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES,
                                                (char *)propq, 0);
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY,
                                             (void *)entropy, entropylen);
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE,
                                             (void *)nonce, noncelen);
    *p = OSSL_PARAM_construct_end();

    if (EVP_KDF_CTX_set_params(ctx, params) <= 0)
        goto err;

    return ctx;
err:
    EVP_KDF_CTX_free(ctx);
    return NULL;
}


int ossl_gen_deterministic_nonce_rfc6979(BIGNUM *out, const BIGNUM *q,
                                         const BIGNUM *priv,
                                         const unsigned char *hm, size_t hmlen,
                                         const char *digestname,
                                         OSSL_LIB_CTX *libctx,
                                         const char *propq)
{
    EVP_KDF_CTX *kdfctx = NULL;
    int ret = 0, rlen = 0, qlen_bits = 0;
    unsigned char *entropyx = NULL, *nonceh = NULL, *T = NULL;
    size_t allocsz = 0;

    if (out == NULL)
        return 0;

    qlen_bits = BN_num_bits(q);
    if (qlen_bits == 0)
        return 0;

    
    rlen = (qlen_bits + 7) / 8;
    allocsz = 3 * rlen;

    
    T = (unsigned char *)OPENSSL_zalloc(allocsz);
    if (T == NULL)
        return 0;
    nonceh = T + rlen;
    entropyx = nonceh + rlen;

    if (!int2octets(entropyx, priv, rlen)
            || !bits2octets(nonceh, q, qlen_bits, rlen, hm, hmlen))
        goto end;

    kdfctx = kdf_setup(digestname, entropyx, rlen, nonceh, rlen, libctx, propq);
    if (kdfctx == NULL)
        goto end;

    do {
        if (!EVP_KDF_derive(kdfctx, T, rlen, NULL)
                || !bits2int(out, qlen_bits, T, rlen))
            goto end;
    } while (BN_is_zero(out) || BN_is_one(out) || BN_cmp(out, q) >= 0);
    ret = 1;

end:
    EVP_KDF_CTX_free(kdfctx);
    OPENSSL_clear_free(T, allocsz);
    return ret;
}
