2019-01-15 15:03:38 +00:00
|
|
|
#include "indcpa.h"
|
2019-01-15 15:34:01 +00:00
|
|
|
#include "ntt.h"
|
2019-09-10 10:45:01 +01:00
|
|
|
#include "params.h"
|
2019-01-15 15:03:38 +00:00
|
|
|
#include "poly.h"
|
|
|
|
#include "polyvec.h"
|
|
|
|
#include "randombytes.h"
|
2019-05-06 13:50:27 +01:00
|
|
|
#include "symmetric.h"
|
2020-07-31 07:17:42 +01:00
|
|
|
#include <stddef.h>
|
2019-05-06 13:50:27 +01:00
|
|
|
#include <stdint.h>
|
2019-01-15 15:03:38 +00:00
|
|
|
|
|
|
|
/*************************************************
|
2019-05-06 13:50:27 +01:00
|
|
|
* Name: pack_pk
|
|
|
|
*
|
|
|
|
* Description: Serialize the public key as concatenation of the
|
|
|
|
* serialized vector of polynomials pk
|
|
|
|
* and the public seed used to generate the matrix A.
|
|
|
|
*
|
2019-09-10 10:45:01 +01:00
|
|
|
* Arguments: uint8_t *r: pointer to the output serialized public key
|
2020-07-31 07:17:42 +01:00
|
|
|
* polyvec *pk: pointer to the input public-key polyvec
|
2019-09-10 10:45:01 +01:00
|
|
|
* const uint8_t *seed: pointer to the input public seed
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void pack_pk(uint8_t r[KYBER_INDCPA_PUBLICKEYBYTES],
|
|
|
|
polyvec *pk,
|
|
|
|
const uint8_t seed[KYBER_SYMBYTES]) {
|
|
|
|
size_t i = 0;
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_tobytes(r, pk);
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_SYMBYTES; i++) {
|
2019-05-06 13:50:27 +01:00
|
|
|
r[i + KYBER_POLYVECBYTES] = seed[i];
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-05-06 13:50:27 +01:00
|
|
|
* Name: unpack_pk
|
|
|
|
*
|
|
|
|
* Description: De-serialize public key from a byte array;
|
|
|
|
* approximate inverse of pack_pk
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - polyvec *pk: pointer to output public-key
|
|
|
|
* polynomial vector
|
|
|
|
* - uint8_t *seed: pointer to output seed to generate
|
|
|
|
* matrix A
|
2019-09-10 10:45:01 +01:00
|
|
|
* - const uint8_t *packedpk: pointer to input serialized public key
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void unpack_pk(polyvec *pk,
|
|
|
|
uint8_t seed[KYBER_SYMBYTES],
|
|
|
|
const uint8_t packedpk[KYBER_INDCPA_PUBLICKEYBYTES]) {
|
|
|
|
size_t i = 0;
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_frombytes(pk, packedpk);
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_SYMBYTES; i++) {
|
2019-05-06 13:50:27 +01:00
|
|
|
seed[i] = packedpk[i + KYBER_POLYVECBYTES];
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-05-06 13:50:27 +01:00
|
|
|
* Name: pack_sk
|
|
|
|
*
|
|
|
|
* Description: Serialize the secret key
|
|
|
|
*
|
2019-09-10 10:45:01 +01:00
|
|
|
* Arguments: - uint8_t *r: pointer to output serialized secret key
|
2020-07-31 07:17:42 +01:00
|
|
|
* - polyvec *sk: pointer to input vector of polynomials (secret key)
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void pack_sk(uint8_t r[KYBER_INDCPA_SECRETKEYBYTES], polyvec *sk) {
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_tobytes(r, sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: unpack_sk
|
|
|
|
*
|
|
|
|
* Description: De-serialize the secret key;
|
|
|
|
* inverse of pack_sk
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - polyvec *sk: pointer to output vector of
|
|
|
|
* polynomials (secret key)
|
2019-09-10 10:45:01 +01:00
|
|
|
* - const uint8_t *packedsk: pointer to input serialized secret key
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void unpack_sk(polyvec *sk,
|
|
|
|
const uint8_t packedsk[KYBER_INDCPA_SECRETKEYBYTES]) {
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_frombytes(sk, packedsk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
|
|
|
* Name: pack_ciphertext
|
|
|
|
*
|
|
|
|
* Description: Serialize the ciphertext as concatenation of the
|
|
|
|
* compressed and serialized vector of polynomials b
|
|
|
|
* and the compressed and serialized polynomial v
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: uint8_t *r: pointer to the output serialized ciphertext
|
|
|
|
* poly *pk: pointer to the input vector of polynomials b
|
|
|
|
* poly *v: pointer to the input polynomial v
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void pack_ciphertext(uint8_t r[KYBER_INDCPA_BYTES],
|
|
|
|
polyvec *b,
|
|
|
|
poly *v) {
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_compress(r, b);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_compress(r + KYBER_POLYVECCOMPRESSEDBYTES, v);
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-05-06 13:50:27 +01:00
|
|
|
* Name: unpack_ciphertext
|
|
|
|
*
|
|
|
|
* Description: De-serialize and decompress ciphertext from a byte array;
|
|
|
|
* approximate inverse of pack_ciphertext
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - polyvec *b: pointer to the output vector of polynomials b
|
|
|
|
* - poly *v: pointer to the output polynomial v
|
2019-09-10 10:45:01 +01:00
|
|
|
* - const uint8_t *c: pointer to the input serialized ciphertext
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static void unpack_ciphertext(polyvec *b,
|
|
|
|
poly *v,
|
|
|
|
const uint8_t c[KYBER_INDCPA_BYTES]) {
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_decompress(b, c);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_decompress(v, c + KYBER_POLYVECCOMPRESSEDBYTES);
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-05-06 13:50:27 +01:00
|
|
|
* Name: rej_uniform
|
|
|
|
*
|
|
|
|
* Description: Run rejection sampling on uniform random bytes to generate
|
|
|
|
* uniform random integers mod q
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - int16_t *r: pointer to output buffer
|
|
|
|
* - unsigned int len: requested number of 16-bit integers
|
|
|
|
* (uniform mod q)
|
|
|
|
* - const uint8_t *buf: pointer to input buffer
|
|
|
|
* (assumed to be uniform random bytes)
|
|
|
|
* - unsigned int buflen: length of input buffer in bytes
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Returns number of sampled 16-bit integers (at most len)
|
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
static unsigned int rej_uniform(int16_t *r,
|
|
|
|
unsigned int len,
|
|
|
|
const uint8_t *buf,
|
|
|
|
unsigned int buflen) {
|
|
|
|
unsigned int ctr = 0, pos = 0;
|
|
|
|
uint16_t val = 0;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-05-06 13:50:27 +01:00
|
|
|
ctr = pos = 0;
|
|
|
|
while (ctr < len && pos + 2 <= buflen) {
|
2020-07-31 07:17:42 +01:00
|
|
|
val = buf[pos] | ((uint16_t)buf[pos + 1] << 8);
|
2019-05-06 13:50:27 +01:00
|
|
|
pos += 2;
|
|
|
|
|
|
|
|
if (val < 19 * KYBER_Q) {
|
2020-07-31 07:17:42 +01:00
|
|
|
val -= (val >> 12) * KYBER_Q; // Barrett reduction
|
2019-05-06 13:50:27 +01:00
|
|
|
r[ctr++] = (int16_t)val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctr;
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
#define gen_a(A,B) PQCLEAN_KYBER768_CLEAN_gen_matrix(A,B,0)
|
|
|
|
#define gen_at(A,B) PQCLEAN_KYBER768_CLEAN_gen_matrix(A,B,1)
|
2019-05-06 13:50:27 +01:00
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_gen_matrix
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Deterministically generate matrix A (or the transpose of A)
|
|
|
|
* from a seed. Entries of the matrix are polynomials that look
|
|
|
|
* uniformly random. Performs rejection sampling on output of
|
|
|
|
* a XOF
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - polyvec *a: pointer to ouptput matrix A
|
2019-09-10 10:45:01 +01:00
|
|
|
* - const uint8_t *seed: pointer to input seed
|
2020-07-31 07:17:42 +01:00
|
|
|
* - int transposed: boolean deciding whether A or A^T
|
|
|
|
* is generated
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
#define GEN_MATRIX_NBLOCKS ((2*KYBER_N*(1U << 16)/(19*KYBER_Q) \
|
|
|
|
+ XOF_BLOCKBYTES)/XOF_BLOCKBYTES)
|
|
|
|
// Not static for benchmarking
|
|
|
|
void PQCLEAN_KYBER768_CLEAN_gen_matrix(polyvec *a, const uint8_t seed[KYBER_SYMBYTES], int transposed) {
|
2020-09-17 09:23:24 +01:00
|
|
|
unsigned int ctr = 0;
|
|
|
|
uint8_t i = 0, j = 0;
|
2020-07-31 07:17:42 +01:00
|
|
|
uint8_t buf[GEN_MATRIX_NBLOCKS * XOF_BLOCKBYTES];
|
2019-05-06 13:50:27 +01:00
|
|
|
xof_state state;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
|
|
for (j = 0; j < KYBER_K; j++) {
|
|
|
|
if (transposed) {
|
2019-05-06 13:50:27 +01:00
|
|
|
xof_absorb(&state, seed, i, j);
|
2019-01-16 10:02:32 +00:00
|
|
|
} else {
|
2019-05-06 13:50:27 +01:00
|
|
|
xof_absorb(&state, seed, j, i);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
xof_squeezeblocks(buf, GEN_MATRIX_NBLOCKS, &state);
|
|
|
|
ctr = rej_uniform(a[i].vec[j].coeffs, KYBER_N, buf, sizeof(buf));
|
2019-01-16 10:02:32 +00:00
|
|
|
|
|
|
|
while (ctr < KYBER_N) {
|
2019-05-06 13:50:27 +01:00
|
|
|
xof_squeezeblocks(buf, 1, &state);
|
2020-07-31 07:17:42 +01:00
|
|
|
ctr += rej_uniform(a[i].vec[j].coeffs + ctr, KYBER_N - ctr, buf,
|
|
|
|
XOF_BLOCKBYTES);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2020-02-16 19:55:19 +00:00
|
|
|
xof_ctx_release(&state);
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_indcpa_keypair
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Generates public and private key for the CPA-secure
|
|
|
|
* public-key encryption scheme underlying Kyber
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - uint8_t *pk: pointer to output public key
|
|
|
|
* (of length KYBER_INDCPA_PUBLICKEYBYTES bytes)
|
|
|
|
* - uint8_t *sk: pointer to output private key
|
|
|
|
(of length KYBER_INDCPA_SECRETKEYBYTES bytes)
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_indcpa_keypair(uint8_t pk[KYBER_INDCPA_PUBLICKEYBYTES],
|
|
|
|
uint8_t sk[KYBER_INDCPA_SECRETKEYBYTES]) {
|
|
|
|
unsigned int i = 0;
|
2019-09-10 10:45:01 +01:00
|
|
|
uint8_t buf[2 * KYBER_SYMBYTES];
|
2020-07-31 07:17:42 +01:00
|
|
|
const uint8_t *publicseed = buf;
|
|
|
|
const uint8_t *noiseseed = buf + KYBER_SYMBYTES;
|
2019-09-10 10:45:01 +01:00
|
|
|
uint8_t nonce = 0;
|
2020-07-31 07:17:42 +01:00
|
|
|
polyvec a[KYBER_K], e, pkpv, skpv;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
randombytes(buf, KYBER_SYMBYTES);
|
2019-05-06 13:50:27 +01:00
|
|
|
hash_g(buf, buf, KYBER_SYMBYTES);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
gen_a(a, publicseed);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_getnoise(&skpv.vec[i], noiseseed, nonce++);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_getnoise(&e.vec[i], noiseseed, nonce++);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_ntt(&skpv);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_ntt(&e);
|
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// matrix-vector multiplication
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_pointwise_acc_montgomery(&pkpv.vec[i], &a[i], &skpv);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_tomont(&pkpv.vec[i]);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_add(&pkpv, &pkpv, &e);
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_reduce(&pkpv);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
pack_sk(sk, &skpv);
|
|
|
|
pack_pk(pk, &pkpv, publicseed);
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_indcpa_enc
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Encryption function of the CPA-secure
|
|
|
|
* public-key encryption scheme underlying Kyber.
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - uint8_t *c: pointer to output ciphertext
|
|
|
|
* (of length KYBER_INDCPA_BYTES bytes)
|
|
|
|
* - const uint8_t *m: pointer to input message
|
|
|
|
* (of length KYBER_INDCPA_MSGBYTES bytes)
|
|
|
|
* - const uint8_t *pk: pointer to input public key
|
|
|
|
* (of length KYBER_INDCPA_PUBLICKEYBYTES)
|
|
|
|
* - const uint8_t *coins: pointer to input random coins
|
|
|
|
* used as seed (of length KYBER_SYMBYTES)
|
|
|
|
* to deterministically generate all
|
|
|
|
* randomness
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_indcpa_enc(uint8_t c[KYBER_INDCPA_BYTES],
|
|
|
|
const uint8_t m[KYBER_INDCPA_MSGBYTES],
|
|
|
|
const uint8_t pk[KYBER_INDCPA_PUBLICKEYBYTES],
|
|
|
|
const uint8_t coins[KYBER_SYMBYTES]) {
|
|
|
|
unsigned int i = 0;
|
2019-09-10 10:45:01 +01:00
|
|
|
uint8_t seed[KYBER_SYMBYTES];
|
|
|
|
uint8_t nonce = 0;
|
2020-07-31 07:17:42 +01:00
|
|
|
polyvec sp, pkpv, ep, at[KYBER_K], bp;
|
|
|
|
poly v, k, epp;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
unpack_pk(&pkpv, seed, pk);
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_frommsg(&k, m);
|
2019-01-16 10:02:32 +00:00
|
|
|
gen_at(at, seed);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_getnoise(sp.vec + i, coins, nonce++);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_getnoise(ep.vec + i, coins, nonce++);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_getnoise(&epp, coins, nonce++);
|
|
|
|
|
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_ntt(&sp);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
// matrix-vector multiplication
|
2020-07-31 07:17:42 +01:00
|
|
|
for (i = 0; i < KYBER_K; i++) {
|
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_pointwise_acc_montgomery(&bp.vec[i], &at[i], &sp);
|
2019-01-16 10:02:32 +00:00
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_pointwise_acc_montgomery(&v, &pkpv, &sp);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_invntt_tomont(&bp);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_invntt_tomont(&v);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_add(&bp, &bp, &ep);
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_add(&v, &v, &epp);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_add(&v, &v, &k);
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_reduce(&bp);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_reduce(&v);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
pack_ciphertext(c, &bp, &v);
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER768_CLEAN_indcpa_dec
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Decryption function of the CPA-secure
|
|
|
|
* public-key encryption scheme underlying Kyber.
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - uint8_t *m: pointer to output decrypted message
|
|
|
|
* (of length KYBER_INDCPA_MSGBYTES)
|
|
|
|
* - const uint8_t *c: pointer to input ciphertext
|
|
|
|
* (of length KYBER_INDCPA_BYTES)
|
|
|
|
* - const uint8_t *sk: pointer to input secret key
|
|
|
|
* (of length KYBER_INDCPA_SECRETKEYBYTES)
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2020-07-31 07:17:42 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_indcpa_dec(uint8_t m[KYBER_INDCPA_MSGBYTES],
|
|
|
|
const uint8_t c[KYBER_INDCPA_BYTES],
|
|
|
|
const uint8_t sk[KYBER_INDCPA_SECRETKEYBYTES]) {
|
2019-01-16 10:02:32 +00:00
|
|
|
polyvec bp, skpv;
|
|
|
|
poly v, mp;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
unpack_ciphertext(&bp, &v, c);
|
|
|
|
unpack_sk(&skpv, sk);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_ntt(&bp);
|
2020-07-31 07:17:42 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_polyvec_pointwise_acc_montgomery(&mp, &skpv, &bp);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_invntt_tomont(&mp);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-05-06 13:50:27 +01:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_sub(&mp, &v, &mp);
|
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_reduce(&mp);
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-02-26 16:27:32 +00:00
|
|
|
PQCLEAN_KYBER768_CLEAN_poly_tomsg(m, &mp);
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|