blob: eaf5947f36ab47fa091427bd3a308853fedb513a [file] [log] [blame]
Daniel McArdle8b601c82020-07-16 14:10:52 -04001/* Copyright (c) 2020, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#ifndef OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H
16#define OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H
17
18#include <openssl/aead.h>
19#include <openssl/base.h>
20#include <openssl/curve25519.h>
Dan McArdle1920c6f2020-03-11 17:29:40 -040021#include <openssl/digest.h>
Daniel McArdle8b601c82020-07-16 14:10:52 -040022
23#if defined(__cplusplus)
24extern "C" {
25#endif
26
27
28// Hybrid Public Key Encryption.
29//
30// Hybrid Public Key Encryption (HPKE) enables a sender to encrypt messages to a
David Benjamine4d65562021-05-03 16:01:32 -040031// receiver with a public key.
Daniel McArdle8b601c82020-07-16 14:10:52 -040032//
David Benjaminc76da9d2022-02-25 10:53:31 -050033// See RFC 9180.
Daniel McArdle8b601c82020-07-16 14:10:52 -040034
David Benjaminf39c81d2021-05-03 18:39:46 -040035
36// Parameters.
37//
David Benjamin070a6c32021-05-05 15:39:27 -040038// An HPKE context is parameterized by KEM, KDF, and AEAD algorithms,
39// represented by |EVP_HPKE_KEM|, |EVP_HPKE_KDF|, and |EVP_HPKE_AEAD| types,
40// respectively.
David Benjaminf39c81d2021-05-03 18:39:46 -040041
42// The following constants are KEM identifiers.
Daniel McArdle00e434d2021-02-18 11:47:18 -050043#define EVP_HPKE_DHKEM_X25519_HKDF_SHA256 0x0020
44
David Benjamin1d58cd12021-05-04 15:24:24 -040045// The following functions are KEM algorithms which may be used with HPKE. Note
46// that, while some HPKE KEMs use KDFs internally, this is separate from the
47// |EVP_HPKE_KDF| selection.
48OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void);
49
50// EVP_HPKE_KEM_id returns the HPKE KEM identifier for |kem|, which
51// will be one of the |EVP_HPKE_KEM_*| constants.
52OPENSSL_EXPORT uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem);
53
David Benjamin46a1c7e2022-09-29 17:02:53 -040054// EVP_HPKE_MAX_PUBLIC_KEY_LENGTH is the maximum length of an encoded public key
55// for all KEMs currently supported by this library.
56#define EVP_HPKE_MAX_PUBLIC_KEY_LENGTH 32
57
58// EVP_HPKE_KEM_public_key_len returns the length of a public key for |kem|.
59// This value will be at most |EVP_HPKE_MAX_PUBLIC_KEY_LENGTH|.
60OPENSSL_EXPORT size_t EVP_HPKE_KEM_public_key_len(const EVP_HPKE_KEM *kem);
61
62// EVP_HPKE_MAX_PRIVATE_KEY_LENGTH is the maximum length of an encoded private
63// key for all KEMs currently supported by this library.
64#define EVP_HPKE_MAX_PRIVATE_KEY_LENGTH 32
65
66// EVP_HPKE_KEM_private_key_len returns the length of a private key for |kem|.
67// This value will be at most |EVP_HPKE_MAX_PRIVATE_KEY_LENGTH|.
68OPENSSL_EXPORT size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem);
69
David Benjamin779f7d02022-08-26 16:02:49 -040070// EVP_HPKE_MAX_ENC_LENGTH is the maximum length of "enc", the encapsulated
David Benjamin46a1c7e2022-09-29 17:02:53 -040071// shared secret, for all KEMs currently supported by this library.
David Benjamin779f7d02022-08-26 16:02:49 -040072#define EVP_HPKE_MAX_ENC_LENGTH 32
73
74// EVP_HPKE_KEM_enc_len returns the length of the "enc", the encapsulated shared
75// secret, for |kem|. This value will be at most |EVP_HPKE_MAX_ENC_LENGTH|.
76OPENSSL_EXPORT size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem);
77
David Benjaminf39c81d2021-05-03 18:39:46 -040078// The following constants are KDF identifiers.
Daniel McArdle8b601c82020-07-16 14:10:52 -040079#define EVP_HPKE_HKDF_SHA256 0x0001
Daniel McArdle8b601c82020-07-16 14:10:52 -040080
David Benjaminf39c81d2021-05-03 18:39:46 -040081// The following functions are KDF algorithms which may be used with HPKE.
82OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void);
83
84// EVP_HPKE_KDF_id returns the HPKE KDF identifier for |kdf|.
85OPENSSL_EXPORT uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf);
86
David Benjaminee477d42022-08-26 16:24:49 -040087// EVP_HPKE_KDF_hkdf_md returns the HKDF hash function corresponding to |kdf|,
88// or NULL if |kdf| is not an HKDF-based KDF. All currently supported KDFs are
89// HKDF-based.
90OPENSSL_EXPORT const EVP_MD *EVP_HPKE_KDF_hkdf_md(const EVP_HPKE_KDF *kdf);
91
David Benjaminf39c81d2021-05-03 18:39:46 -040092// The following constants are AEAD identifiers.
93#define EVP_HPKE_AES_128_GCM 0x0001
94#define EVP_HPKE_AES_256_GCM 0x0002
95#define EVP_HPKE_CHACHA20_POLY1305 0x0003
96
97// The following functions are AEAD algorithms which may be used with HPKE.
98OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void);
99OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_aes_256_gcm(void);
100OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_chacha20_poly1305(void);
101
102// EVP_HPKE_AEAD_id returns the HPKE AEAD identifier for |aead|.
103OPENSSL_EXPORT uint16_t EVP_HPKE_AEAD_id(const EVP_HPKE_AEAD *aead);
Daniel McArdle8b601c82020-07-16 14:10:52 -0400104
David Benjamin83a49932021-05-20 15:57:09 -0400105// EVP_HPKE_AEAD_aead returns the |EVP_AEAD| corresponding to |aead|.
106OPENSSL_EXPORT const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead);
107
Daniel McArdle8b601c82020-07-16 14:10:52 -0400108
David Benjamin1d58cd12021-05-04 15:24:24 -0400109// Recipient keys.
110//
111// An HPKE recipient maintains a long-term KEM key. This library represents keys
112// with the |EVP_HPKE_KEY| type.
113
David Benjamin1d58cd12021-05-04 15:24:24 -0400114// EVP_HPKE_KEY_zero sets an uninitialized |EVP_HPKE_KEY| to the zero state. The
David Benjaminc890ae52021-06-06 13:32:29 -0400115// caller should then use |EVP_HPKE_KEY_init|, |EVP_HPKE_KEY_copy|, or
116// |EVP_HPKE_KEY_generate| to finish initializing |key|.
David Benjamin1d58cd12021-05-04 15:24:24 -0400117//
118// It is safe, but not necessary to call |EVP_HPKE_KEY_cleanup| in this state.
119// This may be used for more uniform cleanup of |EVP_HPKE_KEY|.
120OPENSSL_EXPORT void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key);
121
122// EVP_HPKE_KEY_cleanup releases memory referenced by |key|.
123OPENSSL_EXPORT void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key);
124
David Benjamin897a2ca2021-07-16 12:05:40 -0400125// EVP_HPKE_KEY_new returns a newly-allocated |EVP_HPKE_KEY|, or NULL on error.
126// The caller must call |EVP_HPKE_KEY_free| on the result to release it.
127//
128// This is a convenience function for callers that need a heap-allocated
129// |EVP_HPKE_KEY|.
130OPENSSL_EXPORT EVP_HPKE_KEY *EVP_HPKE_KEY_new(void);
131
132// EVP_HPKE_KEY_free releases memory associated with |key|, which must have been
133// created with |EVP_HPKE_KEY_new|.
134OPENSSL_EXPORT void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key);
135
David Benjaminc890ae52021-06-06 13:32:29 -0400136// EVP_HPKE_KEY_copy sets |dst| to a copy of |src|. It returns one on success
137// and zero on error. On success, the caller must call |EVP_HPKE_KEY_cleanup| to
138// release |dst|. On failure, calling |EVP_HPKE_KEY_cleanup| is safe, but not
139// necessary.
140OPENSSL_EXPORT int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst,
141 const EVP_HPKE_KEY *src);
142
David Benjamin1d58cd12021-05-04 15:24:24 -0400143// EVP_HPKE_KEY_init decodes |priv_key| as a private key for |kem| and
144// initializes |key| with the result. It returns one on success and zero if
145// |priv_key| was invalid. On success, the caller must call
146// |EVP_HPKE_KEY_cleanup| to release the key. On failure, calling
147// |EVP_HPKE_KEY_cleanup| is safe, but not necessary.
148OPENSSL_EXPORT int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem,
149 const uint8_t *priv_key,
150 size_t priv_key_len);
151
David Benjaminc890ae52021-06-06 13:32:29 -0400152// EVP_HPKE_KEY_generate sets |key| to a newly-generated key using |kem|.
153OPENSSL_EXPORT int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key,
154 const EVP_HPKE_KEM *kem);
155
156// EVP_HPKE_KEY_kem returns the HPKE KEM used by |key|.
157OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key);
158
David Benjamin1d58cd12021-05-04 15:24:24 -0400159// EVP_HPKE_KEY_public_key writes |key|'s public key to |out| and sets
160// |*out_len| to the number of bytes written. On success, it returns one and
161// writes at most |max_out| bytes. If |max_out| is too small, it returns zero.
162// Setting |max_out| to |EVP_HPKE_MAX_PUBLIC_KEY_LENGTH| will ensure the public
David Benjamin46a1c7e2022-09-29 17:02:53 -0400163// key fits. An exact size can also be determined by
164// |EVP_HPKE_KEM_public_key_len|.
David Benjamin1d58cd12021-05-04 15:24:24 -0400165OPENSSL_EXPORT int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key,
166 uint8_t *out, size_t *out_len,
167 size_t max_out);
168
David Benjaminc890ae52021-06-06 13:32:29 -0400169// EVP_HPKE_KEY_private_key writes |key|'s private key to |out| and sets
170// |*out_len| to the number of bytes written. On success, it returns one and
171// writes at most |max_out| bytes. If |max_out| is too small, it returns zero.
172// Setting |max_out| to |EVP_HPKE_MAX_PRIVATE_KEY_LENGTH| will ensure the
David Benjamin46a1c7e2022-09-29 17:02:53 -0400173// private key fits. An exact size can also be determined by
174// |EVP_HPKE_KEM_private_key_len|.
David Benjaminc890ae52021-06-06 13:32:29 -0400175OPENSSL_EXPORT int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key,
176 uint8_t *out, size_t *out_len,
177 size_t max_out);
178
David Benjamin1d58cd12021-05-04 15:24:24 -0400179
Daniel McArdle8b601c82020-07-16 14:10:52 -0400180// Encryption contexts.
David Benjamin070a6c32021-05-05 15:39:27 -0400181//
182// An HPKE encryption context is represented by the |EVP_HPKE_CTX| type.
Daniel McArdle8b601c82020-07-16 14:10:52 -0400183
David Benjamin1d58cd12021-05-04 15:24:24 -0400184// EVP_HPKE_CTX_zero sets an uninitialized |EVP_HPKE_CTX| to the zero state. The
185// caller should then use one of the |EVP_HPKE_CTX_setup_*| functions to finish
186// setting up |ctx|.
Daniel McArdle8b601c82020-07-16 14:10:52 -0400187//
188// It is safe, but not necessary to call |EVP_HPKE_CTX_cleanup| in this state.
David Benjamin1d58cd12021-05-04 15:24:24 -0400189// This may be used for more uniform cleanup of |EVP_HPKE_CTX|.
190OPENSSL_EXPORT void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx);
Daniel McArdle8b601c82020-07-16 14:10:52 -0400191
192// EVP_HPKE_CTX_cleanup releases memory referenced by |ctx|. |ctx| must have
David Benjamin1d58cd12021-05-04 15:24:24 -0400193// been initialized with |EVP_HPKE_CTX_zero| or one of the
194// |EVP_HPKE_CTX_setup_*| functions.
Daniel McArdle8b601c82020-07-16 14:10:52 -0400195OPENSSL_EXPORT void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx);
196
David Benjamin897a2ca2021-07-16 12:05:40 -0400197// EVP_HPKE_CTX_new returns a newly-allocated |EVP_HPKE_CTX|, or NULL on error.
198// The caller must call |EVP_HPKE_CTX_free| on the result to release it.
199//
200// This is a convenience function for callers that need a heap-allocated
201// |EVP_HPKE_CTX|.
202OPENSSL_EXPORT EVP_HPKE_CTX *EVP_HPKE_CTX_new(void);
203
204// EVP_HPKE_CTX_free releases memory associated with |ctx|, which must have been
205// created with |EVP_HPKE_CTX_new|.
206OPENSSL_EXPORT void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx);
207
David Benjamin1d58cd12021-05-04 15:24:24 -0400208// EVP_HPKE_CTX_setup_sender implements the SetupBaseS HPKE operation. It
David Benjamin1eb77692021-05-07 14:32:39 -0400209// encapsulates a shared secret for |peer_public_key| and sets up |ctx| as a
David Benjamin1d58cd12021-05-04 15:24:24 -0400210// sender context. It writes the encapsulated shared secret to |out_enc| and
211// sets |*out_enc_len| to the number of bytes written. It writes at most
212// |max_enc| bytes and fails if the buffer is too small. Setting |max_enc| to at
David Benjamin779f7d02022-08-26 16:02:49 -0400213// least |EVP_HPKE_MAX_ENC_LENGTH| will ensure the buffer is large enough. An
214// exact size may also be determined by |EVP_PKEY_KEM_enc_len|.
Daniel McArdle8b601c82020-07-16 14:10:52 -0400215//
David Benjamin1d58cd12021-05-04 15:24:24 -0400216// This function returns one on success and zero on error. Note that
217// |peer_public_key| may be invalid, in which case this function will return an
218// error.
219//
220// On success, callers may call |EVP_HPKE_CTX_seal| to encrypt messages for the
221// recipient. Callers must then call |EVP_HPKE_CTX_cleanup| when done. On
222// failure, calling |EVP_HPKE_CTX_cleanup| is safe, but not required.
223OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender(
David Benjamin1eb77692021-05-07 14:32:39 -0400224 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
David Benjamin1d58cd12021-05-04 15:24:24 -0400225 const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
226 const uint8_t *peer_public_key, size_t peer_public_key_len,
David Benjaminf39c81d2021-05-03 18:39:46 -0400227 const uint8_t *info, size_t info_len);
Daniel McArdle8b601c82020-07-16 14:10:52 -0400228
David Benjamin1d58cd12021-05-04 15:24:24 -0400229// EVP_HPKE_CTX_setup_sender_with_seed_for_testing behaves like
230// |EVP_HPKE_CTX_setup_sender|, but takes a seed to behave deterministically.
231// The seed's format depends on |kem|. For X25519, it is the sender's
232// ephemeral private key.
233OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
David Benjamin1eb77692021-05-07 14:32:39 -0400234 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
David Benjamin1d58cd12021-05-04 15:24:24 -0400235 const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
236 const uint8_t *peer_public_key, size_t peer_public_key_len,
David Benjaminf39c81d2021-05-03 18:39:46 -0400237 const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len);
Daniel McArdle8b601c82020-07-16 14:10:52 -0400238
David Benjamin1d58cd12021-05-04 15:24:24 -0400239// EVP_HPKE_CTX_setup_recipient implements the SetupBaseR HPKE operation. It
David Benjamin1eb77692021-05-07 14:32:39 -0400240// decapsulates the shared secret in |enc| with |key| and sets up |ctx| as a
David Benjamin1d58cd12021-05-04 15:24:24 -0400241// recipient context. It returns one on success and zero on failure. Note that
242// |enc| may be invalid, in which case this function will return an error.
Dan McArdled9ee55a2021-03-15 16:55:36 -0400243//
David Benjamin1d58cd12021-05-04 15:24:24 -0400244// On success, callers may call |EVP_HPKE_CTX_open| to decrypt messages from the
245// sender. Callers must then call |EVP_HPKE_CTX_cleanup| when done. On failure,
246// calling |EVP_HPKE_CTX_cleanup| is safe, but not required.
247OPENSSL_EXPORT int EVP_HPKE_CTX_setup_recipient(
David Benjamin1eb77692021-05-07 14:32:39 -0400248 EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf,
David Benjamin1d58cd12021-05-04 15:24:24 -0400249 const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len,
David Benjaminf39c81d2021-05-03 18:39:46 -0400250 const uint8_t *info, size_t info_len);
Daniel McArdle8b601c82020-07-16 14:10:52 -0400251
David Benjamin4c8bcf02023-04-28 17:59:18 -0400252// EVP_HPKE_CTX_setup_auth_sender implements the SetupAuthS HPKE operation. It
253// behaves like |EVP_HPKE_CTX_setup_sender| but authenticates the resulting
254// context with |key|.
255OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_sender(
256 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
257 const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
258 const uint8_t *peer_public_key, size_t peer_public_key_len,
259 const uint8_t *info, size_t info_len);
260
261// EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing behaves like
262// |EVP_HPKE_CTX_setup_auth_sender|, but takes a seed to behave
263// deterministically. The seed's format depends on |kem|. For X25519, it is the
264// sender's ephemeral private key.
265OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing(
266 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
267 const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
268 const uint8_t *peer_public_key, size_t peer_public_key_len,
269 const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len);
270
271// EVP_HPKE_CTX_setup_auth_recipient implements the SetupAuthR HPKE operation.
272// It behaves like |EVP_HPKE_CTX_setup_recipient| but checks the resulting
273// context was authenticated with |peer_public_key|.
274OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_recipient(
275 EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf,
276 const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len,
277 const uint8_t *info, size_t info_len, const uint8_t *peer_public_key,
278 size_t peer_public_key_len);
279
Daniel McArdle8b601c82020-07-16 14:10:52 -0400280
281// Using an HPKE context.
David Benjamin1d58cd12021-05-04 15:24:24 -0400282//
283// Once set up, callers may encrypt or decrypt with an |EVP_HPKE_CTX| using the
284// following functions.
Daniel McArdle8b601c82020-07-16 14:10:52 -0400285
David Benjamin1eb77692021-05-07 14:32:39 -0400286// EVP_HPKE_CTX_open uses the HPKE context |ctx| to authenticate |in_len| bytes
Daniel McArdle8b601c82020-07-16 14:10:52 -0400287// from |in| and |ad_len| bytes from |ad| and to decrypt at most |in_len| bytes
288// into |out|. It returns one on success, and zero otherwise.
289//
David Benjamin1eb77692021-05-07 14:32:39 -0400290// This operation will fail if the |ctx| context is not set up as a receiver.
Daniel McArdle8b601c82020-07-16 14:10:52 -0400291//
292// Note that HPKE encryption is stateful and ordered. The sender's first call to
293// |EVP_HPKE_CTX_seal| must correspond to the recipient's first call to
294// |EVP_HPKE_CTX_open|, etc.
295//
296// At most |in_len| bytes are written to |out|. In order to ensure success,
297// |max_out_len| should be at least |in_len|. On successful return, |*out_len|
298// is set to the actual number of bytes written.
David Benjamin1eb77692021-05-07 14:32:39 -0400299OPENSSL_EXPORT int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out,
Daniel McArdle8b601c82020-07-16 14:10:52 -0400300 size_t *out_len, size_t max_out_len,
301 const uint8_t *in, size_t in_len,
302 const uint8_t *ad, size_t ad_len);
303
David Benjamin1eb77692021-05-07 14:32:39 -0400304// EVP_HPKE_CTX_seal uses the HPKE context |ctx| to encrypt and authenticate
Daniel McArdle8b601c82020-07-16 14:10:52 -0400305// |in_len| bytes of ciphertext |in| and authenticate |ad_len| bytes from |ad|,
306// writing the result to |out|. It returns one on success and zero otherwise.
307//
David Benjamin1eb77692021-05-07 14:32:39 -0400308// This operation will fail if the |ctx| context is not set up as a sender.
Daniel McArdle8b601c82020-07-16 14:10:52 -0400309//
310// Note that HPKE encryption is stateful and ordered. The sender's first call to
311// |EVP_HPKE_CTX_seal| must correspond to the recipient's first call to
312// |EVP_HPKE_CTX_open|, etc.
313//
314// At most, |max_out_len| encrypted bytes are written to |out|. On successful
315// return, |*out_len| is set to the actual number of bytes written.
316//
317// To ensure success, |max_out_len| should be |in_len| plus the result of
318// |EVP_HPKE_CTX_max_overhead| or |EVP_HPKE_MAX_OVERHEAD|.
David Benjamin1eb77692021-05-07 14:32:39 -0400319OPENSSL_EXPORT int EVP_HPKE_CTX_seal(EVP_HPKE_CTX *ctx, uint8_t *out,
Daniel McArdle8b601c82020-07-16 14:10:52 -0400320 size_t *out_len, size_t max_out_len,
321 const uint8_t *in, size_t in_len,
322 const uint8_t *ad, size_t ad_len);
323
David Benjamin1eb77692021-05-07 14:32:39 -0400324// EVP_HPKE_CTX_export uses the HPKE context |ctx| to export a secret of
Daniel McArdle8b601c82020-07-16 14:10:52 -0400325// |secret_len| bytes into |out|. This function uses |context_len| bytes from
326// |context| as a context string for the secret. This is necessary to separate
327// different uses of exported secrets and bind relevant caller-specific context
328// into the output. It returns one on success and zero otherwise.
David Benjamin1eb77692021-05-07 14:32:39 -0400329OPENSSL_EXPORT int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out,
Daniel McArdle8b601c82020-07-16 14:10:52 -0400330 size_t secret_len,
331 const uint8_t *context,
332 size_t context_len);
333
David Benjaminf39c81d2021-05-03 18:39:46 -0400334// EVP_HPKE_MAX_OVERHEAD contains the largest value that
335// |EVP_HPKE_CTX_max_overhead| would ever return for any context.
336#define EVP_HPKE_MAX_OVERHEAD EVP_AEAD_MAX_OVERHEAD
337
Daniel McArdle8b601c82020-07-16 14:10:52 -0400338// EVP_HPKE_CTX_max_overhead returns the maximum number of additional bytes
David Benjamin1eb77692021-05-07 14:32:39 -0400339// added by sealing data with |EVP_HPKE_CTX_seal|. The |ctx| context must be set
340// up as a sender.
341OPENSSL_EXPORT size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx);
Daniel McArdle8b601c82020-07-16 14:10:52 -0400342
David Benjamin779f7d02022-08-26 16:02:49 -0400343// EVP_HPKE_CTX_kem returns |ctx|'s configured KEM, or NULL if the context has
344// not been set up.
345OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_HPKE_CTX_kem(const EVP_HPKE_CTX *ctx);
346
David Benjamin1eb77692021-05-07 14:32:39 -0400347// EVP_HPKE_CTX_aead returns |ctx|'s configured AEAD, or NULL if the context has
348// not been set up.
349OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx);
Daniel McArdle00e434d2021-02-18 11:47:18 -0500350
David Benjamin1eb77692021-05-07 14:32:39 -0400351// EVP_HPKE_CTX_kdf returns |ctx|'s configured KDF, or NULL if the context has
352// not been set up.
353OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_HPKE_CTX_kdf(const EVP_HPKE_CTX *ctx);
Daniel McArdle00e434d2021-02-18 11:47:18 -0500354
Dan McArdle1920c6f2020-03-11 17:29:40 -0400355
David Benjaminf39c81d2021-05-03 18:39:46 -0400356// Private structures.
357//
358// The following structures are exported so their types are stack-allocatable,
359// but accessing or modifying their fields is forbidden.
360
361struct evp_hpke_ctx_st {
David Benjamin779f7d02022-08-26 16:02:49 -0400362 const EVP_HPKE_KEM *kem;
David Benjaminf39c81d2021-05-03 18:39:46 -0400363 const EVP_HPKE_AEAD *aead;
364 const EVP_HPKE_KDF *kdf;
365 EVP_AEAD_CTX aead_ctx;
366 uint8_t base_nonce[EVP_AEAD_MAX_NONCE_LENGTH];
367 uint8_t exporter_secret[EVP_MAX_MD_SIZE];
368 uint64_t seq;
369 int is_sender;
370};
Dan McArdle1920c6f2020-03-11 17:29:40 -0400371
David Benjamin1d58cd12021-05-04 15:24:24 -0400372struct evp_hpke_key_st {
373 const EVP_HPKE_KEM *kem;
374 uint8_t private_key[X25519_PRIVATE_KEY_LEN];
375 uint8_t public_key[X25519_PUBLIC_VALUE_LEN];
376};
377
Daniel McArdle8b601c82020-07-16 14:10:52 -0400378
379#if defined(__cplusplus)
380} // extern C
381#endif
382
383#if !defined(BORINGSSL_NO_CXX)
384extern "C++" {
385
386BSSL_NAMESPACE_BEGIN
387
388using ScopedEVP_HPKE_CTX =
David Benjamin1d58cd12021-05-04 15:24:24 -0400389 internal::StackAllocated<EVP_HPKE_CTX, void, EVP_HPKE_CTX_zero,
Daniel McArdle8b601c82020-07-16 14:10:52 -0400390 EVP_HPKE_CTX_cleanup>;
David Benjamin1d58cd12021-05-04 15:24:24 -0400391using ScopedEVP_HPKE_KEY =
392 internal::StackAllocated<EVP_HPKE_KEY, void, EVP_HPKE_KEY_zero,
393 EVP_HPKE_KEY_cleanup>;
Daniel McArdle8b601c82020-07-16 14:10:52 -0400394
David Benjamin897a2ca2021-07-16 12:05:40 -0400395BORINGSSL_MAKE_DELETER(EVP_HPKE_CTX, EVP_HPKE_CTX_free)
396BORINGSSL_MAKE_DELETER(EVP_HPKE_KEY, EVP_HPKE_KEY_free)
397
Daniel McArdle8b601c82020-07-16 14:10:52 -0400398BSSL_NAMESPACE_END
399
400} // extern C++
401#endif
402
403#endif // OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H