pqc/crypto_kem/saber/clean/kem.c

97 lines
2.8 KiB
C
Raw Normal View History

2019-06-18 10:00:33 +01:00
#include "SABER_indcpa.h"
#include "SABER_params.h"
#include "fips202.h"
#include "randombytes.h"
#include "verify.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int PQCLEAN_SABER_CLEAN_crypto_kem_keypair(unsigned char *pk, unsigned char *sk) {
int i;
2019-06-20 10:03:20 +01:00
// sk[0:SABER_INDCPA_SECRETKEYBYTES-1] <-- sk
PQCLEAN_SABER_CLEAN_indcpa_kem_keypair(pk, sk);
// sk[SABER_INDCPA_SECRETKEYBYTES:SABER_INDCPA_SECRETKEYBYTES+SABER_INDCPA_SECRETKEYBYTES-1] <-- pk
2019-06-18 10:00:33 +01:00
for (i = 0; i < SABER_INDCPA_PUBLICKEYBYTES; i++) {
2019-06-20 10:03:20 +01:00
sk[i + SABER_INDCPA_SECRETKEYBYTES] = pk[i];
2019-06-18 10:00:33 +01:00
}
2019-06-20 10:03:20 +01:00
// Then hash(pk) is appended.
sha3_256(sk + SABER_SECRETKEYBYTES - 64, pk, SABER_INDCPA_PUBLICKEYBYTES);
2019-06-18 10:00:33 +01:00
2019-06-20 10:03:20 +01:00
// Remaining part of sk contains a pseudo-random number.
2019-06-18 10:00:33 +01:00
// This is output when check in crypto_kem_dec() fails.
2019-06-20 10:03:20 +01:00
randombytes(sk + SABER_SECRETKEYBYTES - SABER_KEYBYTES, SABER_KEYBYTES );
2019-06-18 10:00:33 +01:00
return (0);
}
int PQCLEAN_SABER_CLEAN_crypto_kem_enc(unsigned char *ct, unsigned char *ss, const unsigned char *pk) {
2019-06-20 10:03:20 +01:00
// Will contain key, coins
unsigned char kr[64];
2019-06-18 10:00:33 +01:00
unsigned char buf[64];
randombytes(buf, 32);
2019-06-20 10:03:20 +01:00
// BUF[0:31] <-- random message (will be used as the key for client) Note: hash doesnot release system RNG output
sha3_256(buf, buf, 32);
// BUF[32:63] <-- Hash(public key); Multitarget countermeasure for coins + contributory KEM
sha3_256(buf + 32, pk, SABER_INDCPA_PUBLICKEYBYTES);
2019-06-18 10:00:33 +01:00
2019-06-20 10:03:20 +01:00
// kr[0:63] <-- Hash(buf[0:63]);
sha3_512(kr, buf, 64);
2019-06-18 10:00:33 +01:00
// K^ <-- kr[0:31]
// noiseseed (r) <-- kr[32:63];
2019-06-20 10:03:20 +01:00
// buf[0:31] contains message; kr[32:63] contains randomness r;
PQCLEAN_SABER_CLEAN_indcpa_kem_enc(buf, kr + 32, pk, ct);
2019-06-18 10:00:33 +01:00
sha3_256(kr + 32, ct, SABER_BYTES_CCA_DEC);
2019-06-20 10:03:20 +01:00
// hash concatenation of pre-k and h(c) to k
sha3_256(ss, kr, 64);
2019-06-18 10:00:33 +01:00
return (0);
}
int PQCLEAN_SABER_CLEAN_crypto_kem_dec(unsigned char *ss, const unsigned char *ct, const unsigned char *sk) {
2019-06-18 10:41:54 +01:00
int i;
unsigned char fail;
2019-06-18 10:00:33 +01:00
unsigned char cmp[SABER_BYTES_CCA_DEC];
unsigned char buf[64];
2019-06-20 10:03:20 +01:00
// Will contain key, coins
unsigned char kr[64];
2019-06-18 10:00:33 +01:00
const unsigned char *pk = sk + SABER_INDCPA_SECRETKEYBYTES;
2019-06-20 10:03:20 +01:00
// buf[0:31] <-- message
PQCLEAN_SABER_CLEAN_indcpa_kem_dec(sk, ct, buf);
2019-06-18 10:00:33 +01:00
// Multitarget countermeasure for coins + contributory KEM
2019-06-20 10:03:20 +01:00
// Save hash by storing h(pk) in sk
for (i = 0; i < 32; i++) {
2019-06-18 10:00:33 +01:00
buf[32 + i] = sk[SABER_SECRETKEYBYTES - 64 + i];
}
sha3_512(kr, buf, 64);
PQCLEAN_SABER_CLEAN_indcpa_kem_enc(buf, kr + 32, pk, cmp);
fail = PQCLEAN_SABER_CLEAN_verify(ct, cmp, SABER_BYTES_CCA_DEC);
2019-06-20 10:03:20 +01:00
// overwrite coins in kr with h(c)
sha3_256(kr + 32, ct, SABER_BYTES_CCA_DEC);
2019-06-18 10:00:33 +01:00
PQCLEAN_SABER_CLEAN_cmov(kr, sk + SABER_SECRETKEYBYTES - SABER_KEYBYTES, SABER_KEYBYTES, fail);
2019-06-20 10:03:20 +01:00
// hash concatenation of pre-k and h(c) to k
sha3_256(ss, kr, 64);
2019-06-18 10:00:33 +01:00
return (0);
}