#include "Alg.h"
#include "kem_api.h"
#include <memory.h>
#include "rng.h"

//获取随机种子字节长度
PKC_ALG_API unsigned long long rand_get_sd_byts()
{
	return PK_SEED_LEN;
}

//根据输入的随机种子初始化随机数发生器
PKC_ALG_API int rand_init(unsigned char * s, unsigned long long s_byts)
{
	memset(Public_Key_Seed, 0, PK_SEED_LEN);
	if (s_byts != PK_SEED_LEN)
		return -1;
	memcpy(Public_Key_Seed, s, PK_SEED_LEN);
	return 0;
}

PKC_ALG_API int rand_init_get(unsigned char * s, unsigned long long *s_byts)
{
	if (*s_byts != PK_SEED_LEN)
		return -1;
	memcpy(s, Public_Key_Seed, PK_SEED_LEN);
	return 0;
}


//根据输入随机数字节长度要求，输出随机数字节序列
PKC_ALG_API int rand_byts(unsigned long long r_byts, unsigned char * r)
{
	return randombytes(r, r_byts);
}

PKC_ALG_API unsigned long long kem_get_pk_byts() {
	return KEM_PUBLICKEYBYTES;
}

PKC_ALG_API unsigned long long kem_get_sk_byts() {
	return KEM_SECRETKEYBYTES;
}

PKC_ALG_API unsigned long long kem_get_ss_byts() {
	return KEM_BYTES;
}

PKC_ALG_API unsigned long long kem_get_ct_byts() {
	return KEM_CIPHERTEXTBYTES;
}

PKC_ALG_API int kem_keygen(
	unsigned char* pk, unsigned long long* pk_byts,
	unsigned char* sk, unsigned long long* sk_byts) {
		*pk_byts = KEM_PUBLICKEYBYTES;
		*sk_byts = KEM_SECRETKEYBYTES;
		return kem_keygen(pk, sk);
}

PKC_ALG_API int kem_enc(
	unsigned char* pk, unsigned long long pk_byts,
	unsigned char* ss, unsigned long long* ss_byts,
	unsigned char* ct, unsigned long long* ct_byts) {
		*ss_byts = KEM_BYTES;
		*ct_byts = KEM_CIPHERTEXTBYTES;
		return kem_enc(pk, ss, ct);
}

PKC_ALG_API int kem_dec(
	unsigned char* sk, unsigned long long sk_byts,
	unsigned char* ct, unsigned long long ct_byts,
	unsigned char* ss, unsigned long long* ss_byts) {
		*ss_byts = KEM_BYTES;
		return kem_dec(sk, ct, ss);
}