#include <string.h>
#include <openssl/crypto.h>
#include "slh_dsa_local.h"
#include "slh_dsa_key.h"
#define SLH_WOTS_LOGW 4
#define SLH_WOTS_W 16
#define SLH_WOTS_LEN1(n) (2 * (n))
#define SLH_WOTS_LEN2 3
#define SLH_WOTS_CHECKSUM_LEN ((SLH_WOTS_LEN2 + SLH_WOTS_LOGW + 7) / 8)
#define SLH_WOTS_LEN_MAX SLH_WOTS_LEN(SLH_MAX_N)
#define NIBBLE_MASK 15
#define NIBBLE_SHIFT 4
static ossl_inline void slh_bytes_to_nibbles(const uint8_t *in, size_t in_len,
uint8_t *out)
{
size_t consumed = 0;
for (consumed = 0; consumed < in_len; consumed++) {
*out++ = (*in >> NIBBLE_SHIFT);
*out++ = (*in++ & NIBBLE_MASK);
}
}
static ossl_inline void compute_checksum_nibbles(const uint8_t *in, size_t in_len,
uint8_t *out)
{
size_t i;
uint16_t csum = 0;
for (i = 0; i < in_len; ++i)
csum += in[i];
csum = (uint16_t)(NIBBLE_MASK * in_len) - csum;
out[0] = (csum >> (2 * NIBBLE_SHIFT)) & NIBBLE_MASK;
out[1] = (csum >> NIBBLE_SHIFT) & NIBBLE_MASK;
out[2] = csum & NIBBLE_MASK;
}
static int slh_wots_chain(SLH_DSA_HASH_CTX *ctx, const uint8_t *in,
uint8_t start_index, uint8_t steps,
const uint8_t *pk_seed, uint8_t *adrs, WPACKET *wpkt)
{
const SLH_DSA_KEY *key = ctx->key;
SLH_HASH_FUNC_DECLARE(key, hashf);
SLH_ADRS_FUNC_DECLARE(key, adrsf);
SLH_HASH_FN_DECLARE(hashf, F);
SLH_ADRS_FN_DECLARE(adrsf, set_hash_address);
size_t j = start_index, end_index;
size_t n = key->params->n;
uint8_t *tmp;
size_t tmp_len = n;
if (steps == 0)
return WPACKET_memcpy(wpkt, in, n);
if (!WPACKET_allocate_bytes(wpkt, tmp_len, &tmp))
return 0;
set_hash_address(adrs, j++);
if (!F(ctx, pk_seed, adrs, in, n, tmp, tmp_len))
return 0;
end_index = start_index + steps;
for (; j < end_index; ++j) {
set_hash_address(adrs, j);
if (!F(ctx, pk_seed, adrs, tmp, n, tmp, tmp_len))
return 0;
}
return 1;
}
int ossl_slh_wots_pk_gen(SLH_DSA_HASH_CTX *ctx,
const uint8_t *sk_seed, const uint8_t *pk_seed,
uint8_t *adrs, uint8_t *pk_out, size_t pk_out_len)
{
int ret = 0;
const SLH_DSA_KEY *key = ctx->key;
size_t n = key->params->n;
size_t i, len = SLH_WOTS_LEN(n);
uint8_t sk[SLH_MAX_N];
uint8_t tmp[SLH_WOTS_LEN_MAX * SLH_MAX_N];
WPACKET pkt, *tmp_wpkt = &pkt;
size_t tmp_len = 0;
SLH_HASH_FUNC_DECLARE(key, hashf);
SLH_ADRS_FUNC_DECLARE(key, adrsf);
SLH_HASH_FN_DECLARE(hashf, PRF);
SLH_ADRS_FN_DECLARE(adrsf, set_chain_address);
SLH_ADRS_DECLARE(sk_adrs);
SLH_ADRS_DECLARE(wots_pk_adrs);
if (!WPACKET_init_static_len(tmp_wpkt, tmp, sizeof(tmp), 0))
return 0;
adrsf->copy(sk_adrs, adrs);
adrsf->set_type_and_clear(sk_adrs, SLH_ADRS_TYPE_WOTS_PRF);
adrsf->copy_keypair_address(sk_adrs, adrs);
for (i = 0; i < len; ++i) {
set_chain_address(sk_adrs, i);
if (!PRF(ctx, pk_seed, sk_seed, sk_adrs, sk, sizeof(sk)))
goto end;
set_chain_address(adrs, i);
if (!slh_wots_chain(ctx, sk, 0, NIBBLE_MASK, pk_seed, adrs, tmp_wpkt))
goto end;
}
if (!WPACKET_get_total_written(tmp_wpkt, &tmp_len))
goto end;
adrsf->copy(wots_pk_adrs, adrs);
adrsf->set_type_and_clear(wots_pk_adrs, SLH_ADRS_TYPE_WOTS_PK);
adrsf->copy_keypair_address(wots_pk_adrs, adrs);
ret = hashf->T(ctx, pk_seed, wots_pk_adrs, tmp, tmp_len, pk_out, pk_out_len);
end:
WPACKET_finish(tmp_wpkt);
OPENSSL_cleanse(tmp, sizeof(tmp));
OPENSSL_cleanse(sk, n);
return ret;
}
int ossl_slh_wots_sign(SLH_DSA_HASH_CTX *ctx, const uint8_t *msg,
const uint8_t *sk_seed, const uint8_t *pk_seed,
uint8_t *adrs, WPACKET *sig_wpkt)
{
int ret = 0;
const SLH_DSA_KEY *key = ctx->key;
uint8_t msg_and_csum_nibbles[SLH_WOTS_LEN_MAX];
uint8_t sk[SLH_MAX_N];
size_t i;
size_t n = key->params->n;
size_t len1 = SLH_WOTS_LEN1(n);
size_t len = len1 + SLH_WOTS_LEN2;
SLH_ADRS_DECLARE(sk_adrs);
SLH_HASH_FUNC_DECLARE(key, hashf);
SLH_ADRS_FUNC_DECLARE(key, adrsf);
SLH_HASH_FN_DECLARE(hashf, PRF);
SLH_ADRS_FN_DECLARE(adrsf, set_chain_address);
slh_bytes_to_nibbles(msg, n, msg_and_csum_nibbles);
compute_checksum_nibbles(msg_and_csum_nibbles, len1, msg_and_csum_nibbles + len1);
adrsf->copy(sk_adrs, adrs);
adrsf->set_type_and_clear(sk_adrs, SLH_ADRS_TYPE_WOTS_PRF);
adrsf->copy_keypair_address(sk_adrs, adrs);
for (i = 0; i < len; ++i) {
set_chain_address(sk_adrs, i);
if (!PRF(ctx, pk_seed, sk_seed, sk_adrs, sk, sizeof(sk)))
goto err;
set_chain_address(adrs, i);
if (!slh_wots_chain(ctx, sk, 0, msg_and_csum_nibbles[i],
pk_seed, adrs, sig_wpkt))
goto err;
}
ret = 1;
err:
return ret;
}
int ossl_slh_wots_pk_from_sig(SLH_DSA_HASH_CTX *ctx,
PACKET *sig_rpkt, const uint8_t *msg,
const uint8_t *pk_seed, uint8_t *adrs,
uint8_t *pk_out, size_t pk_out_len)
{
int ret = 0;
const SLH_DSA_KEY *key = ctx->key;
uint8_t msg_and_csum_nibbles[SLH_WOTS_LEN_MAX];
size_t i;
size_t n = key->params->n;
size_t len1 = SLH_WOTS_LEN1(n);
size_t len = len1 + SLH_WOTS_LEN2;
const uint8_t *sig_i;
uint8_t tmp[SLH_WOTS_LEN_MAX * SLH_MAX_N];
WPACKET pkt, *tmp_pkt = &pkt;
size_t tmp_len = 0;
SLH_HASH_FUNC_DECLARE(key, hashf);
SLH_ADRS_FUNC_DECLARE(key, adrsf);
SLH_ADRS_FN_DECLARE(adrsf, set_chain_address);
SLH_ADRS_DECLARE(wots_pk_adrs);
if (!WPACKET_init_static_len(tmp_pkt, tmp, sizeof(tmp), 0))
return 0;
slh_bytes_to_nibbles(msg, n, msg_and_csum_nibbles);
compute_checksum_nibbles(msg_and_csum_nibbles, len1, msg_and_csum_nibbles + len1);
for (i = 0; i < len; ++i) {
set_chain_address(adrs, i);
if (!PACKET_get_bytes(sig_rpkt, &sig_i, n)
|| !slh_wots_chain(ctx, sig_i, msg_and_csum_nibbles[i],
NIBBLE_MASK - msg_and_csum_nibbles[i],
pk_seed, adrs, tmp_pkt))
goto err;
}
adrsf->copy(wots_pk_adrs, adrs);
adrsf->set_type_and_clear(wots_pk_adrs, SLH_ADRS_TYPE_WOTS_PK);
adrsf->copy_keypair_address(wots_pk_adrs, adrs);
if (!WPACKET_get_total_written(tmp_pkt, &tmp_len))
goto err;
ret = hashf->T(ctx, pk_seed, wots_pk_adrs, tmp, tmp_len,
pk_out, pk_out_len);
err:
if (!WPACKET_finish(tmp_pkt))
ret = 0;
return ret;
}