#include <openssl/hpke.h>
#include <assert.h>
#include <string.h>
#include <openssl/aead.h>
#include <openssl/bytestring.h>
#include <openssl/curve25519.h>
#include <openssl/digest.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/evp_errors.h>
#include <openssl/hkdf.h>
#include <openssl/mem.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include "../fipsmodule/ec/internal.h"
#include "../internal.h"
#define MAX_SEED_LEN …
#define MAX_SHARED_SECRET_LEN …
struct evp_hpke_kem_st { … };
struct evp_hpke_kdf_st { … };
struct evp_hpke_aead_st { … };
static const char kHpkeVersionId[] = …;
static int add_label_string(CBB *cbb, const char *label) { … }
static int hpke_labeled_extract(const EVP_MD *hkdf_md, uint8_t *out_key,
size_t *out_len, const uint8_t *salt,
size_t salt_len, const uint8_t *suite_id,
size_t suite_id_len, const char *label,
const uint8_t *ikm, size_t ikm_len) { … }
static int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key,
size_t out_len, const uint8_t *prk,
size_t prk_len, const uint8_t *suite_id,
size_t suite_id_len, const char *label,
const uint8_t *info, size_t info_len) { … }
static int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md,
uint8_t *out_key, size_t out_len,
const uint8_t *dh, size_t dh_len,
const uint8_t *kem_context,
size_t kem_context_len) { … }
static int x25519_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key,
size_t priv_key_len) { … }
static int x25519_generate_key(EVP_HPKE_KEY *key) { … }
static int x25519_encap_with_seed(
const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret,
size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len,
size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len,
const uint8_t *seed, size_t seed_len) { … }
static int x25519_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
size_t *out_shared_secret_len, const uint8_t *enc,
size_t enc_len) { … }
static int x25519_auth_encap_with_seed(
const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len,
size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len,
const uint8_t *seed, size_t seed_len) { … }
static int x25519_auth_decap(const EVP_HPKE_KEY *key,
uint8_t *out_shared_secret,
size_t *out_shared_secret_len, const uint8_t *enc,
size_t enc_len, const uint8_t *peer_public_key,
size_t peer_public_key_len) { … }
const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void) { … }
#define P256_PRIVATE_KEY_LEN …
#define P256_PUBLIC_KEY_LEN …
#define P256_PUBLIC_VALUE_LEN …
#define P256_SEED_LEN …
#define P256_SHARED_KEY_LEN …
static int p256_public_from_private(uint8_t out_pub[P256_PUBLIC_VALUE_LEN],
const uint8_t priv[P256_PRIVATE_KEY_LEN]) { … }
static int p256_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key,
size_t priv_key_len) { … }
static int p256_private_key_from_seed(uint8_t out_priv[P256_PRIVATE_KEY_LEN],
const uint8_t seed[P256_SEED_LEN]) { … }
static int p256_generate_key(EVP_HPKE_KEY *key) { … }
static int p256(uint8_t out_dh[P256_SHARED_KEY_LEN],
const uint8_t my_private[P256_PRIVATE_KEY_LEN],
const uint8_t their_public[P256_PUBLIC_VALUE_LEN]) { … }
static int p256_encap_with_seed(const EVP_HPKE_KEM *kem,
uint8_t *out_shared_secret,
size_t *out_shared_secret_len, uint8_t *out_enc,
size_t *out_enc_len, size_t max_enc,
const uint8_t *peer_public_key,
size_t peer_public_key_len, const uint8_t *seed,
size_t seed_len) { … }
static int p256_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
size_t *out_shared_secret_len, const uint8_t *enc,
size_t enc_len) { … }
static int p256_auth_encap_with_seed(
const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len,
size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len,
const uint8_t *seed, size_t seed_len) { … }
static int p256_auth_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
size_t *out_shared_secret_len, const uint8_t *enc,
size_t enc_len, const uint8_t *peer_public_key,
size_t peer_public_key_len) { … }
const EVP_HPKE_KEM *EVP_hpke_p256_hkdf_sha256(void) { … }
uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem) { … }
size_t EVP_HPKE_KEM_public_key_len(const EVP_HPKE_KEM *kem) { … }
size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem) { … }
size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem) { … }
void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key) { … }
void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key) { … }
EVP_HPKE_KEY *EVP_HPKE_KEY_new(void) { … }
void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key) { … }
int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst, const EVP_HPKE_KEY *src) { … }
void EVP_HPKE_KEY_move(EVP_HPKE_KEY *out, EVP_HPKE_KEY *in) { … }
int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem,
const uint8_t *priv_key, size_t priv_key_len) { … }
int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem) { … }
const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key) { … }
int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key, uint8_t *out,
size_t *out_len, size_t max_out) { … }
int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key, uint8_t *out,
size_t *out_len, size_t max_out) { … }
const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void) { … }
uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf) { … }
const EVP_MD *EVP_HPKE_KDF_hkdf_md(const EVP_HPKE_KDF *kdf) { … }
const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void) { … }
const EVP_HPKE_AEAD *EVP_hpke_aes_256_gcm(void) { … }
const EVP_HPKE_AEAD *EVP_hpke_chacha20_poly1305(void) { … }
uint16_t EVP_HPKE_AEAD_id(const EVP_HPKE_AEAD *aead) { … }
const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead) { … }
#define HPKE_SUITE_ID_LEN …
static int hpke_build_suite_id(const EVP_HPKE_CTX *ctx,
uint8_t out[HPKE_SUITE_ID_LEN]) { … }
#define HPKE_MODE_BASE …
#define HPKE_MODE_AUTH …
static int hpke_key_schedule(EVP_HPKE_CTX *ctx, uint8_t mode,
const uint8_t *shared_secret,
size_t shared_secret_len, const uint8_t *info,
size_t info_len) { … }
void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx) { … }
void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx) { … }
EVP_HPKE_CTX *EVP_HPKE_CTX_new(void) { … }
void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx) { … }
int EVP_HPKE_CTX_setup_sender(EVP_HPKE_CTX *ctx, uint8_t *out_enc,
size_t *out_enc_len, size_t max_enc,
const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf,
const EVP_HPKE_AEAD *aead,
const uint8_t *peer_public_key,
size_t peer_public_key_len, const uint8_t *info,
size_t info_len) { … }
int EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
const uint8_t *peer_public_key, size_t peer_public_key_len,
const uint8_t *info, size_t info_len, const uint8_t *seed,
size_t seed_len) { … }
int EVP_HPKE_CTX_setup_recipient(EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key,
const EVP_HPKE_KDF *kdf,
const EVP_HPKE_AEAD *aead, const uint8_t *enc,
size_t enc_len, const uint8_t *info,
size_t info_len) { … }
int EVP_HPKE_CTX_setup_auth_sender(
EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
const uint8_t *peer_public_key, size_t peer_public_key_len,
const uint8_t *info, size_t info_len) { … }
int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing(
EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
const uint8_t *peer_public_key, size_t peer_public_key_len,
const uint8_t *info, size_t info_len, const uint8_t *seed,
size_t seed_len) { … }
int EVP_HPKE_CTX_setup_auth_recipient(
EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf,
const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len,
const uint8_t *info, size_t info_len, const uint8_t *peer_public_key,
size_t peer_public_key_len) { … }
static void hpke_nonce(const EVP_HPKE_CTX *ctx, uint8_t *out_nonce,
size_t nonce_len) { … }
int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len,
size_t max_out_len, const uint8_t *in, size_t in_len,
const uint8_t *ad, size_t ad_len) { … }
int EVP_HPKE_CTX_seal(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len,
size_t max_out_len, const uint8_t *in, size_t in_len,
const uint8_t *ad, size_t ad_len) { … }
int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out,
size_t secret_len, const uint8_t *context,
size_t context_len) { … }
size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx) { … }
const EVP_HPKE_KEM *EVP_HPKE_CTX_kem(const EVP_HPKE_CTX *ctx) { … }
const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx) { … }
const EVP_HPKE_KDF *EVP_HPKE_CTX_kdf(const EVP_HPKE_CTX *ctx) { … }