pqc/crypto_kem/ledakemlt32/leaktime/kem.c

157 lines
6.2 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 "utils.h"
2019-06-10 19:42:31 +01:00
#include <string.h>
2019-08-23 11:57:17 +01:00
#define pack_ct(sk_bytes, ct) PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_tobytes(sk_bytes, ct);
#define unpack_ct(ct, ct_bytes) PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_frombytes(ct, ct_bytes)
static void pack_pk(uint8_t *pk_bytes, publicKeyNiederreiter_t *pk) {
2019-08-23 10:30:02 +01:00
for (size_t 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) {
2019-08-23 10:30:02 +01:00
for (size_t 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_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-08-23 11:57:17 +01:00
}
2019-08-21 13:28:31 +01:00
/* IND-CCA2 Keygen */
int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_keypair(uint8_t *pk, uint8_t *sk) {
2019-08-23 10:30:02 +01:00
publicKeyNiederreiter_t niederreiter_pk;
PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_keygen(&niederreiter_pk, (privateKeyNiederreiter_t *) sk);
2019-06-10 19:42:31 +01:00
2019-08-23 10:30:02 +01:00
pack_pk(pk, &niederreiter_pk);
2019-06-10 19:42:31 +01:00
return 0;
2019-06-10 19:42:31 +01:00
}
2019-08-21 13:28:31 +01:00
/* IND-CCA2 Encapsulation */
int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk) {
2019-08-23 10:30:02 +01:00
publicKeyNiederreiter_t niederreiter_pk;
DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT];
2019-08-21 13:28:31 +01:00
AES_XOF_struct hashedAndTruncatedSeed_expander;
POSITION_T errorPos[NUM_ERRORS_T];
2019-06-10 19:42:31 +01:00
DIGIT error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT];
2019-08-23 11:57:17 +01:00
uint8_t error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B];
2019-08-21 13:28:31 +01:00
uint8_t seed[TRNG_BYTE_LENGTH];
uint8_t ss_input[2 * TRNG_BYTE_LENGTH] = {0};
uint8_t hashedSeed[HASH_BYTE_LENGTH];
uint8_t hashedAndTruncatedSeed[TRNG_BYTE_LENGTH] = {0};
uint8_t hashedErrorVector[HASH_BYTE_LENGTH];
uint8_t hashedAndTruncatedErrorVector[TRNG_BYTE_LENGTH] = {0};
uint8_t maskedSeed[TRNG_BYTE_LENGTH];
2019-08-23 10:30:02 +01:00
unpack_pk(&niederreiter_pk, pk);
2019-08-21 13:28:31 +01:00
randombytes(seed, TRNG_BYTE_LENGTH);
memcpy(ss_input, seed, TRNG_BYTE_LENGTH);
2019-08-23 10:30:02 +01:00
2019-08-21 13:28:31 +01:00
HASH_FUNCTION(ss, ss_input, 2 * TRNG_BYTE_LENGTH);
HASH_FUNCTION(hashedSeed, seed, TRNG_BYTE_LENGTH);
memcpy(hashedAndTruncatedSeed, hashedSeed, TRNG_BYTE_LENGTH);
2019-06-10 19:42:31 +01:00
2019-08-21 13:28:31 +01:00
memset(&hashedAndTruncatedSeed_expander, 0x00, sizeof(AES_XOF_struct));
PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(&hashedAndTruncatedSeed_expander, hashedAndTruncatedSeed);
PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_error_pos(errorPos, &hashedAndTruncatedSeed_expander);
PQCLEAN_LEDAKEMLT32_LEAKTIME_expand_error(error_vector, errorPos);
2019-08-23 11:57:17 +01:00
pack_error(error_bytes, error_vector);
HASH_FUNCTION(hashedErrorVector, error_bytes, (N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B));
2019-08-21 13:28:31 +01:00
memcpy(hashedAndTruncatedErrorVector, hashedErrorVector, TRNG_BYTE_LENGTH);
2019-08-23 10:30:02 +01:00
for (size_t i = 0; i < TRNG_BYTE_LENGTH; ++i) {
2019-08-21 13:28:31 +01:00
maskedSeed[i] = seed[i] ^ hashedAndTruncatedErrorVector[i];
}
2019-06-10 19:42:31 +01:00
2019-08-23 10:30:02 +01:00
PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_encrypt(syndrome,
(const publicKeyNiederreiter_t *) &niederreiter_pk, error_vector);
2019-08-21 13:28:31 +01:00
2019-08-23 10:30:02 +01:00
pack_ct(ct, syndrome);
2019-08-21 13:28:31 +01:00
memcpy(ct + (NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B), maskedSeed, TRNG_BYTE_LENGTH);
2019-08-23 10:30:02 +01:00
2019-06-10 19:42:31 +01:00
return 0;
}
2019-08-23 10:30:02 +01:00
/* IND-CCA2 Decapsulation */
2019-08-21 13:28:31 +01:00
int PQCLEAN_LEDAKEMLT32_LEAKTIME_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk) {
2019-08-23 10:30:02 +01:00
DIGIT syndrome[NUM_DIGITS_GF2X_ELEMENT];
2019-08-21 13:28:31 +01:00
AES_XOF_struct hashedAndTruncatedSeed_expander;
POSITION_T reconstructed_errorPos[NUM_ERRORS_T];
DIGIT reconstructed_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT];
2019-06-10 19:42:31 +01:00
DIGIT decoded_error_vector[N0 * NUM_DIGITS_GF2X_ELEMENT];
2019-08-23 11:57:17 +01:00
uint8_t decoded_error_bytes[N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B];
2019-08-21 13:28:31 +01:00
uint8_t hashedErrorVector[HASH_BYTE_LENGTH];
uint8_t hashedAndTruncatedErrorVector[TRNG_BYTE_LENGTH] = {0};
uint8_t decoded_seed[TRNG_BYTE_LENGTH];
uint8_t hashed_decoded_seed[HASH_BYTE_LENGTH];
uint8_t hashedAndTruncated_decoded_seed[TRNG_BYTE_LENGTH] = {0};
uint8_t ss_input[2 * TRNG_BYTE_LENGTH], tail[TRNG_BYTE_LENGTH] = {0};
2019-08-23 10:30:02 +01:00
unpack_ct(syndrome, ct);
2019-08-21 13:28:31 +01:00
int decode_ok = PQCLEAN_LEDAKEMLT32_LEAKTIME_niederreiter_decrypt(decoded_error_vector,
2019-08-23 10:30:02 +01:00
(const privateKeyNiederreiter_t *)sk, syndrome);
2019-08-21 13:28:31 +01:00
2019-08-23 11:57:17 +01:00
pack_error(decoded_error_bytes, decoded_error_vector);
HASH_FUNCTION(hashedErrorVector, decoded_error_bytes, N0 * NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B);
2019-08-21 13:28:31 +01:00
memcpy(hashedAndTruncatedErrorVector, hashedErrorVector, TRNG_BYTE_LENGTH);
for (int i = 0; i < TRNG_BYTE_LENGTH; ++i) {
decoded_seed[i] = ct[(NUM_DIGITS_GF2X_ELEMENT * DIGIT_SIZE_B) + i] ^
hashedAndTruncatedErrorVector[i];
}
HASH_FUNCTION(hashed_decoded_seed, decoded_seed, TRNG_BYTE_LENGTH);
memcpy(hashedAndTruncated_decoded_seed, hashed_decoded_seed, TRNG_BYTE_LENGTH);
memset(&hashedAndTruncatedSeed_expander, 0x00, sizeof(AES_XOF_struct));
PQCLEAN_LEDAKEMLT32_LEAKTIME_seedexpander_from_trng(&hashedAndTruncatedSeed_expander,
hashed_decoded_seed);
PQCLEAN_LEDAKEMLT32_LEAKTIME_rand_error_pos(reconstructed_errorPos, &hashedAndTruncatedSeed_expander);
PQCLEAN_LEDAKEMLT32_LEAKTIME_expand_error(reconstructed_error_vector, reconstructed_errorPos);
2019-08-23 10:30:02 +01:00
int equal = PQCLEAN_LEDAKEMLT32_LEAKTIME_gf2x_verify(decoded_error_vector,
reconstructed_error_vector, N0 * NUM_DIGITS_GF2X_ELEMENT);
// equal == 0, if the reconstructed error vector match !!!
2019-08-21 13:28:31 +01:00
int decryptOk = (decode_ok == 1 && equal == 0);
2019-08-21 13:28:31 +01:00
memcpy(ss_input, decoded_seed, TRNG_BYTE_LENGTH);
memcpy(ss_input + sizeof(decoded_seed), tail, TRNG_BYTE_LENGTH);
2019-08-21 13:28:31 +01:00
// Overwrite on failure
PQCLEAN_LEDAKEMLT32_LEAKTIME_cmov(ss_input + sizeof(decoded_seed),
((const privateKeyNiederreiter_t *) sk)->decryption_failure_secret,
TRNG_BYTE_LENGTH,
!decryptOk);
2019-06-10 19:42:31 +01:00
2019-08-21 13:28:31 +01:00
HASH_FUNCTION(ss, ss_input, 2 * TRNG_BYTE_LENGTH);
2019-06-10 19:42:31 +01:00
return 0;
}