1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-24 00:11:27 +00:00
pqcrypto/crypto_kem/ledakemlt32/leaktime/kem.c

93 lines
3.6 KiB
C
Raw Normal View History

2019-06-10 19:42:31 +01:00
#include "api.h"
#include "niederreiter.h"
#include "randombytes.h"
#include "rng.h"
#include <string.h>
static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) {
size_t i;
for (i = 0; i < N0 - 1; i++) {
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B,
pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT);
}
}
static void unpack_pk(publicKeyNiederreiter_t *pk, const uint8_t *pk_bytes) {
size_t i;
for (i = 0; i < N0 - 1; i++) {
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_frombytes(pk->Mtr + i * NUM_DIGITS_GF2X_ELEMENT,
pk_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B);
}
}
static void pack_ct(uint8_t *sk_bytes, DIGIT *ct) {
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(sk_bytes, ct);
}
static void unpack_ct(DIGIT *ct, const uint8_t *ct_bytes) {
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_frombytes(ct, ct_bytes);
}
static void pack_error(uint8_t *error_bytes, DIGIT *error_digits) {
size_t i;
for (i = 0; i < N0; i++) {
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(error_bytes + i * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B,
error_digits + i * NUM_DIGITS_GF2X_ELEMENT);
}
}
2019-06-10 19:42:31 +01:00
/* Generates a keypair - pk is the public key and sk is the secret key. */
2019-06-16 16:01:29 +01:00
int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) {
2019-06-10 19:42:31 +01:00
AES_XOF_struct niederreiter_keys_expander;
publicKeyNiederreiter_t pk_nie;
2019-06-10 19:42:31 +01:00
randombytes(((privateKeyNiederreiter_t *)sk)->prng_seed, TRNG_BYTE_LENGTH);
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(&niederreiter_keys_expander, ((privateKeyNiederreiter_t *)sk)->prng_seed);
PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_keygen(&pk_nie, (privateKeyNiederreiter_t *) sk, &niederreiter_keys_expander);
2019-06-10 19:42:31 +01:00
pack_pk(pk, &pk_nie);
2019-06-10 19:42:31 +01:00
return 0;
2019-06-10 19:42:31 +01:00
}
/* Encrypt - pk is the public key, ct is a key encapsulation message
(ciphertext), ss is the shared secret.*/
2019-06-16 16:01:29 +01:00
int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) {
2019-06-10 19:42:31 +01:00
AES_XOF_struct niederreiter_encap_key_expander;
unsigned char encapsulated_key_seed[TRNG_BYTE_LENGTH];
DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT];
uint8_t error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B];
DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT];
publicKeyNiederreiter_t pk_nie;
2019-06-10 19:42:31 +01:00
randombytes(encapsulated_key_seed, TRNG_BYTE_LENGTH);
unpack_pk(&pk_nie, pk);
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(&niederreiter_encap_key_expander, encapsulated_key_seed);
PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_circulant_blocks_sequence(error_vector, &niederreiter_encap_key_expander);
2019-06-10 19:42:31 +01:00
pack_error(error_bytes, error_vector);
HASH_FUNCTION(ss, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B));
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_encrypt(syndrome, &pk_nie, error_vector);
pack_ct(ct, syndrome);
2019-06-10 19:42:31 +01:00
return 0;
}
/* Decrypt - ct is a key encapsulation message (ciphertext), sk is the private
key, ss is the shared secret */
2019-06-16 16:01:29 +01:00
int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) {
2019-06-10 19:42:31 +01:00
DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT];
uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B];
DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT];
2019-06-10 19:42:31 +01:00
unpack_ct(syndrome, ct);
2019-06-16 16:01:29 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_decrypt(decoded_error_vector, (privateKeyNiederreiter_t *)sk, syndrome);
2019-06-10 19:42:31 +01:00
pack_error(decoded_error_bytes, decoded_error_vector);
HASH_FUNCTION(ss, decoded_error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B));
return 0;
}