2019-09-17 13:02:01 +01:00
|
|
|
#include "indcpa.h"
|
2020-07-31 07:17:42 +01:00
|
|
|
#include "kem.h"
|
2019-09-17 13:02:01 +01:00
|
|
|
#include "params.h"
|
|
|
|
#include "randombytes.h"
|
|
|
|
#include "symmetric.h"
|
|
|
|
#include "verify.h"
|
2020-07-31 07:17:42 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-09-17 13:02:01 +01:00
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER51290S_AVX2_crypto_kem_keypair
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Description: Generates public and private key
|
|
|
|
* for CCA-secure Kyber key encapsulation mechanism
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - unsigned char *pk: pointer to output public key
|
2020-10-27 13:48:42 +00:00
|
|
|
* (an already allocated array of KYBER_PUBLICKEYBYTES bytes)
|
2020-07-31 07:17:42 +01:00
|
|
|
* - unsigned char *sk: pointer to output private key
|
2020-10-27 13:48:42 +00:00
|
|
|
* (an already allocated array of KYBER_SECRETKEYBYTES bytes)
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Returns 0 (success)
|
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
int PQCLEAN_KYBER51290S_AVX2_crypto_kem_keypair(unsigned char pk[KYBER_PUBLICKEYBYTES],
|
|
|
|
unsigned char sk[KYBER_SECRETKEYBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
2019-09-17 13:02:01 +01:00
|
|
|
PQCLEAN_KYBER51290S_AVX2_indcpa_keypair(pk, sk);
|
|
|
|
for (i = 0; i < KYBER_INDCPA_PUBLICKEYBYTES; i++) {
|
|
|
|
sk[i + KYBER_INDCPA_SECRETKEYBYTES] = pk[i];
|
|
|
|
}
|
|
|
|
hash_h(sk + KYBER_SECRETKEYBYTES - 2 * KYBER_SYMBYTES, pk, KYBER_PUBLICKEYBYTES);
|
2020-07-31 07:17:42 +01:00
|
|
|
/* Value z for pseudo-random output on reject */
|
|
|
|
randombytes(sk + KYBER_SECRETKEYBYTES - KYBER_SYMBYTES, KYBER_SYMBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER51290S_AVX2_crypto_kem_enc
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Description: Generates cipher text and shared
|
|
|
|
* secret for given public key
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - unsigned char *ct: pointer to output cipher text
|
2020-10-27 13:48:42 +00:00
|
|
|
* (an already allocated array of KYBER_CIPHERTEXTBYTES bytes)
|
2020-07-31 07:17:42 +01:00
|
|
|
* - unsigned char *ss: pointer to output shared secret
|
2020-10-27 13:48:42 +00:00
|
|
|
* (an already allocated array of KYBER_SSBYTES bytes)
|
2020-07-31 07:17:42 +01:00
|
|
|
* - const unsigned char *pk: pointer to input public key
|
2020-10-27 13:48:42 +00:00
|
|
|
* (an already allocated array of KYBER_PUBLICKEYBYTES bytes)
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Returns 0 (success)
|
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
int PQCLEAN_KYBER51290S_AVX2_crypto_kem_enc(unsigned char ct[KYBER_CIPHERTEXTBYTES],
|
|
|
|
unsigned char ss[KYBER_SSBYTES],
|
|
|
|
const unsigned char pk[KYBER_PUBLICKEYBYTES]) {
|
|
|
|
uint8_t buf[2 * KYBER_SYMBYTES];
|
2020-07-31 07:17:42 +01:00
|
|
|
/* Will contain key, coins */
|
2020-10-27 13:48:42 +00:00
|
|
|
uint8_t kr[2 * KYBER_SYMBYTES];
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
randombytes(buf, KYBER_SYMBYTES);
|
2020-07-31 07:17:42 +01:00
|
|
|
/* Don't release system RNG output */
|
2020-10-27 13:48:42 +00:00
|
|
|
hash_h(buf, buf, KYBER_SYMBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/* Multitarget countermeasure for coins + contributory KEM */
|
2020-10-27 13:48:42 +00:00
|
|
|
hash_h(buf + KYBER_SYMBYTES, pk, KYBER_PUBLICKEYBYTES);
|
|
|
|
hash_g(kr, buf, 2 * KYBER_SYMBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/* coins are in kr+KYBER_SYMBYTES */
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER51290S_AVX2_indcpa_enc(ct, buf, pk, kr + KYBER_SYMBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/* overwrite coins in kr with H(c) */
|
2020-10-27 13:48:42 +00:00
|
|
|
hash_h(kr + KYBER_SYMBYTES, ct, KYBER_CIPHERTEXTBYTES);
|
2020-07-31 07:17:42 +01:00
|
|
|
/* hash concatenation of pre-k and H(c) to k */
|
2020-10-27 13:48:42 +00:00
|
|
|
kdf(ss, kr, 2 * KYBER_SYMBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER51290S_AVX2_crypto_kem_dec
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Description: Generates shared secret for given
|
|
|
|
* cipher text and private key
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - unsigned char *ss: pointer to output shared secret
|
2020-10-27 13:48:42 +00:00
|
|
|
* (an already allocated array of KYBER_SSBYTES bytes)
|
2020-07-31 07:17:42 +01:00
|
|
|
* - const unsigned char *ct: pointer to input cipher text
|
2020-10-27 13:48:42 +00:00
|
|
|
* (an already allocated array of KYBER_CIPHERTEXTBYTES bytes)
|
2020-07-31 07:17:42 +01:00
|
|
|
* - const unsigned char *sk: pointer to input private key
|
2020-10-27 13:48:42 +00:00
|
|
|
* (an already allocated array of KYBER_SECRETKEYBYTES bytes)
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Returns 0.
|
|
|
|
*
|
|
|
|
* On failure, ss will contain a pseudo-random value.
|
|
|
|
**************************************************/
|
2020-10-27 13:48:42 +00:00
|
|
|
int PQCLEAN_KYBER51290S_AVX2_crypto_kem_dec(unsigned char ss[KYBER_SSBYTES],
|
|
|
|
const unsigned char ct[KYBER_CIPHERTEXTBYTES],
|
|
|
|
const unsigned char sk[KYBER_SECRETKEYBYTES]) {
|
2020-10-27 00:05:07 +00:00
|
|
|
size_t i;
|
|
|
|
int fail;
|
2020-10-27 13:48:42 +00:00
|
|
|
uint8_t buf[2 * KYBER_SYMBYTES];
|
2020-07-31 07:17:42 +01:00
|
|
|
/* Will contain key, coins */
|
2020-10-27 13:48:42 +00:00
|
|
|
uint8_t kr[2 * KYBER_SYMBYTES];
|
|
|
|
ALIGNED_UINT8(KYBER_CIPHERTEXTBYTES) cmp;
|
2019-09-17 13:02:01 +01:00
|
|
|
const uint8_t *pk = sk + KYBER_INDCPA_SECRETKEYBYTES;
|
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER51290S_AVX2_indcpa_dec(buf, ct, sk);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/* Multitarget countermeasure for coins + contributory KEM */
|
|
|
|
for (i = 0; i < KYBER_SYMBYTES; i++) {
|
2020-10-27 13:48:42 +00:00
|
|
|
buf[KYBER_SYMBYTES + i] = sk[KYBER_SECRETKEYBYTES - 2 * KYBER_SYMBYTES + i];
|
2019-09-17 13:02:01 +01:00
|
|
|
}
|
2020-10-27 13:48:42 +00:00
|
|
|
hash_g(kr, buf, 2 * KYBER_SYMBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/* coins are in kr+KYBER_SYMBYTES */
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER51290S_AVX2_indcpa_enc(cmp.coeffs, buf, pk, kr + KYBER_SYMBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
fail = PQCLEAN_KYBER51290S_AVX2_verify(ct, cmp.coeffs, KYBER_CIPHERTEXTBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/* overwrite coins in kr with H(c) */
|
2020-10-27 13:48:42 +00:00
|
|
|
hash_h(kr + KYBER_SYMBYTES, ct, KYBER_CIPHERTEXTBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/* Overwrite pre-k with z on re-encryption failure */
|
2020-10-27 13:48:42 +00:00
|
|
|
PQCLEAN_KYBER51290S_AVX2_cmov(kr, sk + KYBER_SECRETKEYBYTES - KYBER_SYMBYTES, KYBER_SYMBYTES, fail);
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
/* hash concatenation of pre-k and H(c) to k */
|
2020-10-27 13:48:42 +00:00
|
|
|
kdf(ss, kr, 2 * KYBER_SYMBYTES);
|
2019-09-17 13:02:01 +01:00
|
|
|
return 0;
|
|
|
|
}
|