#include <openssl/mlkem.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/base.h>
#include <openssl/bytestring.h>
#include <openssl/mem.h>
#include <openssl/rand.h>
#include "../internal.h"
#include "../keccak/internal.h"
#include "./internal.h"
static void prf(uint8_t *out, size_t out_len, const uint8_t in[33]) { … }
static void hash_h(uint8_t out[32], const uint8_t *in, size_t len) { … }
static void hash_g(uint8_t out[64], const uint8_t *in, size_t len) { … }
static void kdf(uint8_t out[MLKEM_SHARED_SECRET_BYTES],
const uint8_t failure_secret[32], const uint8_t *ciphertext,
size_t ciphertext_len) { … }
#define DEGREE …
static const size_t kBarrettMultiplier = …;
static const unsigned kBarrettShift = …;
static const uint16_t kPrime = …;
static const int kLog2Prime = …;
static const uint16_t kHalfPrime = …;
static const uint16_t kInverseDegree = …;
#define RANK768 …
static const int kDU768 = …;
static const int kDV768 = …;
#define RANK1024 …
static const int kDU1024 = …;
static const int kDV1024 = …;
constexpr size_t encoded_vector_size(int rank) { … }
constexpr size_t encoded_public_key_size(int rank) { … }
static_assert …;
static_assert …;
constexpr size_t compressed_vector_size(int rank) { … }
constexpr size_t ciphertext_size(int rank) { … }
static_assert …;
static_assert …;
scalar;
template <int RANK>
struct vector { … };
template <int RANK>
struct matrix { … };
static const uint16_t kNTTRoots[128] = …;
static const uint16_t kInverseNTTRoots[128] = …;
static const uint16_t kModRoots[128] = …;
static uint16_t reduce_once(uint16_t x) { … }
static uint16_t reduce(uint32_t x) { … }
static void scalar_zero(scalar *out) { … }
template <int RANK>
static void vector_zero(vector<RANK> *out) { … }
static void scalar_ntt(scalar *s) { … }
template <int RANK>
static void vector_ntt(vector<RANK> *a) { … }
static void scalar_inverse_ntt(scalar *s) { … }
template <int RANK>
static void vector_inverse_ntt(vector<RANK> *a) { … }
static void scalar_add(scalar *lhs, const scalar *rhs) { … }
static void scalar_sub(scalar *lhs, const scalar *rhs) { … }
static void scalar_mult(scalar *out, const scalar *lhs, const scalar *rhs) { … }
template <int RANK>
static void vector_add(vector<RANK> *lhs, const vector<RANK> *rhs) { … }
template <int RANK>
static void matrix_mult(vector<RANK> *out, const matrix<RANK> *m,
const vector<RANK> *a) { … }
template <int RANK>
static void matrix_mult_transpose(vector<RANK> *out, const matrix<RANK> *m,
const vector<RANK> *a) { … }
template <int RANK>
static void scalar_inner_product(scalar *out, const vector<RANK> *lhs,
const vector<RANK> *rhs) { … }
static void scalar_from_keccak_vartime(scalar *out,
struct BORINGSSL_keccak_st *keccak_ctx) { … }
static void scalar_centered_binomial_distribution_eta_2_with_prf(
scalar *out, const uint8_t input[33]) { … }
template <int RANK>
static void vector_generate_secret_eta_2(vector<RANK> *out, uint8_t *counter,
const uint8_t seed[32]) { … }
template <int RANK>
static void matrix_expand(matrix<RANK> *out, const uint8_t rho[32]) { … }
static const uint8_t kMasks[8] = …;
static void scalar_encode(uint8_t *out, const scalar *s, int bits) { … }
static void scalar_encode_1(uint8_t out[32], const scalar *s) { … }
template <int RANK>
static void vector_encode(uint8_t *out, const vector<RANK> *a, int bits) { … }
static int scalar_decode(scalar *out, const uint8_t *in, int bits) { … }
static void scalar_decode_1(scalar *out, const uint8_t in[32]) { … }
template <int RANK>
static int vector_decode(vector<RANK> *out, const uint8_t *in, int bits) { … }
static uint16_t compress(uint16_t x, int bits) { … }
static uint16_t decompress(uint16_t x, int bits) { … }
static void scalar_compress(scalar *s, int bits) { … }
static void scalar_decompress(scalar *s, int bits) { … }
template <int RANK>
static void vector_compress(vector<RANK> *a, int bits) { … }
template <int RANK>
static void vector_decompress(vector<RANK> *a, int bits) { … }
template <int RANK>
struct public_key { … };
static struct public_key<RANK768> *public_key_768_from_external(
const struct MLKEM768_public_key *external) { … }
static struct public_key<RANK1024> *
public_key_1024_from_external(const struct MLKEM1024_public_key *external) { … }
template <int RANK>
struct private_key { … };
static struct private_key<RANK768> *private_key_768_from_external(
const struct MLKEM768_private_key *external) { … }
static struct private_key<RANK1024> *
private_key_1024_from_external(const struct MLKEM1024_private_key *external) { … }
void MLKEM768_generate_key(uint8_t out_encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES],
uint8_t optional_out_seed[MLKEM_SEED_BYTES],
struct MLKEM768_private_key *out_private_key) { … }
int MLKEM768_private_key_from_seed(struct MLKEM768_private_key *out_private_key,
const uint8_t *seed, size_t seed_len) { … }
void MLKEM1024_generate_key(
uint8_t out_encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES],
uint8_t optional_out_seed[MLKEM_SEED_BYTES],
struct MLKEM1024_private_key *out_private_key) { … }
int MLKEM1024_private_key_from_seed(
struct MLKEM1024_private_key *out_private_key, const uint8_t *seed,
size_t seed_len) { … }
template <int RANK>
static int mlkem_marshal_public_key(CBB *out,
const struct public_key<RANK> *pub) { … }
template <int RANK>
void mlkem_generate_key_external_seed(uint8_t *out_encoded_public_key,
private_key<RANK> *priv,
const uint8_t seed[MLKEM_SEED_BYTES]) { … }
void MLKEM768_generate_key_external_seed(
uint8_t out_encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES],
struct MLKEM768_private_key *out_private_key,
const uint8_t seed[MLKEM_SEED_BYTES]) { … }
void MLKEM1024_generate_key_external_seed(
uint8_t out_encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES],
struct MLKEM1024_private_key *out_private_key,
const uint8_t seed[MLKEM_SEED_BYTES]) { … }
void MLKEM768_public_from_private(
struct MLKEM768_public_key *out_public_key,
const struct MLKEM768_private_key *private_key) { … }
void MLKEM1024_public_from_private(
struct MLKEM1024_public_key *out_public_key,
const struct MLKEM1024_private_key *private_key) { … }
template <int RANK>
static void encrypt_cpa(uint8_t *out, const struct public_key<RANK> *pub,
const uint8_t message[32],
const uint8_t randomness[32]) { … }
void MLKEM768_encap(uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES],
uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
const struct MLKEM768_public_key *public_key) { … }
void MLKEM1024_encap(uint8_t out_ciphertext[MLKEM1024_CIPHERTEXT_BYTES],
uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
const struct MLKEM1024_public_key *public_key) { … }
template <int RANK>
static void mlkem_encap_external_entropy(
uint8_t *out_ciphertext,
uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
const struct public_key<RANK> *pub,
const uint8_t entropy[MLKEM_ENCAP_ENTROPY]) { … }
void MLKEM768_encap_external_entropy(
uint8_t out_ciphertext[MLKEM768_CIPHERTEXT_BYTES],
uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
const struct MLKEM768_public_key *public_key,
const uint8_t entropy[MLKEM_ENCAP_ENTROPY]) { … }
void MLKEM1024_encap_external_entropy(
uint8_t out_ciphertext[MLKEM1024_CIPHERTEXT_BYTES],
uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
const struct MLKEM1024_public_key *public_key,
const uint8_t entropy[MLKEM_ENCAP_ENTROPY]) { … }
template <int RANK>
static void decrypt_cpa(uint8_t out[32], const struct private_key<RANK> *priv,
const uint8_t ciphertext[MLKEM768_CIPHERTEXT_BYTES]) { … }
template <int RANK>
static void mlkem_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
const uint8_t *ciphertext,
const struct private_key<RANK> *priv) { … }
int MLKEM768_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
const uint8_t *ciphertext, size_t ciphertext_len,
const struct MLKEM768_private_key *private_key) { … }
int MLKEM1024_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES],
const uint8_t *ciphertext, size_t ciphertext_len,
const struct MLKEM1024_private_key *private_key) { … }
int MLKEM768_marshal_public_key(CBB *out,
const struct MLKEM768_public_key *public_key) { … }
int MLKEM1024_marshal_public_key(
CBB *out, const struct MLKEM1024_public_key *public_key) { … }
template <int RANK>
static int mlkem_parse_public_key_no_hash(struct public_key<RANK> *pub,
CBS *in) { … }
template <int RANK>
static int mlkem_parse_public_key(struct public_key<RANK> *pub, CBS *in) { … }
int MLKEM768_parse_public_key(struct MLKEM768_public_key *public_key, CBS *in) { … }
int MLKEM1024_parse_public_key(struct MLKEM1024_public_key *public_key,
CBS *in) { … }
template <int RANK>
static int mlkem_marshal_private_key(CBB *out,
const struct private_key<RANK> *priv) { … }
int MLKEM768_marshal_private_key(
CBB *out, const struct MLKEM768_private_key *private_key) { … }
int MLKEM1024_marshal_private_key(
CBB *out, const struct MLKEM1024_private_key *private_key) { … }
template <int RANK>
static int mlkem_parse_private_key(struct private_key<RANK> *priv, CBS *in) { … }
int MLKEM768_parse_private_key(struct MLKEM768_private_key *out_private_key,
CBS *in) { … }
int MLKEM1024_parse_private_key(struct MLKEM1024_private_key *out_private_key,
CBS *in) { … }