Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/hpke.h

raw
1 // Copyright (c) 2020, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #ifndef OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H
5 #define OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H
6
7 #include <openssl/aead.h>
8 #include <openssl/base.h>
9 #include <openssl/curve25519.h>
10 #include <openssl/digest.h>
11
12 #if defined(__cplusplus)
13 extern "C" {
14 #endif
15
16
17 // Hybrid Public Key Encryption.
18 //
19 // Hybrid Public Key Encryption (HPKE) enables a sender to encrypt messages to a
20 // receiver with a public key.
21 //
22 // See RFC 9180.
23
24
25 // Parameters.
26 //
27 // An HPKE context is parameterized by KEM, KDF, and AEAD algorithms,
28 // represented by |EVP_HPKE_KEM|, |EVP_HPKE_KDF|, and |EVP_HPKE_AEAD| types,
29 // respectively.
30
31 // The following constants are KEM identifiers.
32 #define EVP_HPKE_DHKEM_X25519_HKDF_SHA256 0x0020
33
34 // The following functions are KEM algorithms which may be used with HPKE. Note
35 // that, while some HPKE KEMs use KDFs internally, this is separate from the
36 // |EVP_HPKE_KDF| selection.
37 OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void);
38
39 // EVP_HPKE_KEM_id returns the HPKE KEM identifier for |kem|, which
40 // will be one of the |EVP_HPKE_KEM_*| constants.
41 OPENSSL_EXPORT uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem);
42
43 // EVP_HPKE_MAX_PUBLIC_KEY_LENGTH is the maximum length of an encoded public key
44 // for all KEMs currently supported by this library.
45 #define EVP_HPKE_MAX_PUBLIC_KEY_LENGTH 32
46
47 // EVP_HPKE_KEM_public_key_len returns the length of a public key for |kem|.
48 // This value will be at most |EVP_HPKE_MAX_PUBLIC_KEY_LENGTH|.
49 OPENSSL_EXPORT size_t EVP_HPKE_KEM_public_key_len(const EVP_HPKE_KEM *kem);
50
51 // EVP_HPKE_MAX_PRIVATE_KEY_LENGTH is the maximum length of an encoded private
52 // key for all KEMs currently supported by this library.
53 #define EVP_HPKE_MAX_PRIVATE_KEY_LENGTH 32
54
55 // EVP_HPKE_KEM_private_key_len returns the length of a private key for |kem|.
56 // This value will be at most |EVP_HPKE_MAX_PRIVATE_KEY_LENGTH|.
57 OPENSSL_EXPORT size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem);
58
59 // EVP_HPKE_MAX_ENC_LENGTH is the maximum length of "enc", the encapsulated
60 // shared secret, for all KEMs currently supported by this library.
61 #define EVP_HPKE_MAX_ENC_LENGTH 32
62
63 // EVP_HPKE_KEM_enc_len returns the length of the "enc", the encapsulated shared
64 // secret, for |kem|. This value will be at most |EVP_HPKE_MAX_ENC_LENGTH|.
65 OPENSSL_EXPORT size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem);
66
67 // The following constants are KDF identifiers.
68 #define EVP_HPKE_HKDF_SHA256 0x0001
69
70 // The following functions are KDF algorithms which may be used with HPKE.
71 OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void);
72
73 // EVP_HPKE_KDF_id returns the HPKE KDF identifier for |kdf|.
74 OPENSSL_EXPORT uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf);
75
76 // EVP_HPKE_KDF_hkdf_md returns the HKDF hash function corresponding to |kdf|,
77 // or NULL if |kdf| is not an HKDF-based KDF. All currently supported KDFs are
78 // HKDF-based.
79 OPENSSL_EXPORT const EVP_MD *EVP_HPKE_KDF_hkdf_md(const EVP_HPKE_KDF *kdf);
80
81 // The following constants are AEAD identifiers.
82 #define EVP_HPKE_AES_128_GCM 0x0001
83 #define EVP_HPKE_AES_256_GCM 0x0002
84 #define EVP_HPKE_CHACHA20_POLY1305 0x0003
85
86 // The following functions are AEAD algorithms which may be used with HPKE.
87 OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void);
88 OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_aes_256_gcm(void);
89 OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_hpke_chacha20_poly1305(void);
90
91 // EVP_HPKE_AEAD_id returns the HPKE AEAD identifier for |aead|.
92 OPENSSL_EXPORT uint16_t EVP_HPKE_AEAD_id(const EVP_HPKE_AEAD *aead);
93
94 // EVP_HPKE_AEAD_aead returns the |EVP_AEAD| corresponding to |aead|.
95 OPENSSL_EXPORT const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead);
96
97
98 // Recipient keys.
99 //
100 // An HPKE recipient maintains a long-term KEM key. This library represents keys
101 // with the |EVP_HPKE_KEY| type.
102
103 // EVP_HPKE_KEY_zero sets an uninitialized |EVP_HPKE_KEY| to the zero state. The
104 // caller should then use |EVP_HPKE_KEY_init|, |EVP_HPKE_KEY_copy|, or
105 // |EVP_HPKE_KEY_generate| to finish initializing |key|.
106 //
107 // It is safe, but not necessary to call |EVP_HPKE_KEY_cleanup| in this state.
108 // This may be used for more uniform cleanup of |EVP_HPKE_KEY|.
109 OPENSSL_EXPORT void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key);
110
111 // EVP_HPKE_KEY_cleanup releases memory referenced by |key|.
112 OPENSSL_EXPORT void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key);
113
114 // EVP_HPKE_KEY_new returns a newly-allocated |EVP_HPKE_KEY|, or NULL on error.
115 // The caller must call |EVP_HPKE_KEY_free| on the result to release it.
116 //
117 // This is a convenience function for callers that need a heap-allocated
118 // |EVP_HPKE_KEY|.
119 OPENSSL_EXPORT EVP_HPKE_KEY *EVP_HPKE_KEY_new(void);
120
121 // EVP_HPKE_KEY_free releases memory associated with |key|, which must have been
122 // created with |EVP_HPKE_KEY_new|.
123 OPENSSL_EXPORT void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key);
124
125 // EVP_HPKE_KEY_copy sets |dst| to a copy of |src|. It returns one on success
126 // and zero on error. On success, the caller must call |EVP_HPKE_KEY_cleanup| to
127 // release |dst|. On failure, calling |EVP_HPKE_KEY_cleanup| is safe, but not
128 // necessary.
129 OPENSSL_EXPORT int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst,
130 const EVP_HPKE_KEY *src);
131
132 // EVP_HPKE_KEY_move sets |out|, which must be initialized or in the zero state,
133 // to the key in |in|. |in| is mutated and left in the zero state.
134 OPENSSL_EXPORT void EVP_HPKE_KEY_move(EVP_HPKE_KEY *out, EVP_HPKE_KEY *in);
135
136 // EVP_HPKE_KEY_init decodes |priv_key| as a private key for |kem| and
137 // initializes |key| with the result. It returns one on success and zero if
138 // |priv_key| was invalid. On success, the caller must call
139 // |EVP_HPKE_KEY_cleanup| to release the key. On failure, calling
140 // |EVP_HPKE_KEY_cleanup| is safe, but not necessary.
141 OPENSSL_EXPORT int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem,
142 const uint8_t *priv_key,
143 size_t priv_key_len);
144
145 // EVP_HPKE_KEY_generate sets |key| to a newly-generated key using |kem|.
146 OPENSSL_EXPORT int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key,
147 const EVP_HPKE_KEM *kem);
148
149 // EVP_HPKE_KEY_kem returns the HPKE KEM used by |key|.
150 OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key);
151
152 // EVP_HPKE_KEY_public_key writes |key|'s public key to |out| and sets
153 // |*out_len| to the number of bytes written. On success, it returns one and
154 // writes at most |max_out| bytes. If |max_out| is too small, it returns zero.
155 // Setting |max_out| to |EVP_HPKE_MAX_PUBLIC_KEY_LENGTH| will ensure the public
156 // key fits. An exact size can also be determined by
157 // |EVP_HPKE_KEM_public_key_len|.
158 OPENSSL_EXPORT int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key,
159 uint8_t *out, size_t *out_len,
160 size_t max_out);
161
162 // EVP_HPKE_KEY_private_key writes |key|'s private key to |out| and sets
163 // |*out_len| to the number of bytes written. On success, it returns one and
164 // writes at most |max_out| bytes. If |max_out| is too small, it returns zero.
165 // Setting |max_out| to |EVP_HPKE_MAX_PRIVATE_KEY_LENGTH| will ensure the
166 // private key fits. An exact size can also be determined by
167 // |EVP_HPKE_KEM_private_key_len|.
168 OPENSSL_EXPORT int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key,
169 uint8_t *out, size_t *out_len,
170 size_t max_out);
171
172
173 // Encryption contexts.
174 //
175 // An HPKE encryption context is represented by the |EVP_HPKE_CTX| type.
176
177 // EVP_HPKE_CTX_zero sets an uninitialized |EVP_HPKE_CTX| to the zero state. The
178 // caller should then use one of the |EVP_HPKE_CTX_setup_*| functions to finish
179 // setting up |ctx|.
180 //
181 // It is safe, but not necessary to call |EVP_HPKE_CTX_cleanup| in this state.
182 // This may be used for more uniform cleanup of |EVP_HPKE_CTX|.
183 OPENSSL_EXPORT void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx);
184
185 // EVP_HPKE_CTX_cleanup releases memory referenced by |ctx|. |ctx| must have
186 // been initialized with |EVP_HPKE_CTX_zero| or one of the
187 // |EVP_HPKE_CTX_setup_*| functions.
188 OPENSSL_EXPORT void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx);
189
190 // EVP_HPKE_CTX_new returns a newly-allocated |EVP_HPKE_CTX|, or NULL on error.
191 // The caller must call |EVP_HPKE_CTX_free| on the result to release it.
192 //
193 // This is a convenience function for callers that need a heap-allocated
194 // |EVP_HPKE_CTX|.
195 OPENSSL_EXPORT EVP_HPKE_CTX *EVP_HPKE_CTX_new(void);
196
197 // EVP_HPKE_CTX_free releases memory associated with |ctx|, which must have been
198 // created with |EVP_HPKE_CTX_new|.
199 OPENSSL_EXPORT void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx);
200
201 // EVP_HPKE_CTX_setup_sender implements the SetupBaseS HPKE operation. It
202 // encapsulates a shared secret for |peer_public_key| and sets up |ctx| as a
203 // sender context. It writes the encapsulated shared secret to |out_enc| and
204 // sets |*out_enc_len| to the number of bytes written. It writes at most
205 // |max_enc| bytes and fails if the buffer is too small. Setting |max_enc| to at
206 // least |EVP_HPKE_MAX_ENC_LENGTH| will ensure the buffer is large enough. An
207 // exact size may also be determined by |EVP_PKEY_KEM_enc_len|.
208 //
209 // This function returns one on success and zero on error. Note that
210 // |peer_public_key| may be invalid, in which case this function will return an
211 // error.
212 //
213 // On success, callers may call |EVP_HPKE_CTX_seal| to encrypt messages for the
214 // recipient. Callers must then call |EVP_HPKE_CTX_cleanup| when done. On
215 // failure, calling |EVP_HPKE_CTX_cleanup| is safe, but not required.
216 OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender(
217 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
218 const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
219 const uint8_t *peer_public_key, size_t peer_public_key_len,
220 const uint8_t *info, size_t info_len);
221
222 // EVP_HPKE_CTX_setup_sender_with_seed_for_testing behaves like
223 // |EVP_HPKE_CTX_setup_sender|, but takes a seed to behave deterministically.
224 // The seed's format depends on |kem|. For X25519, it is the sender's
225 // ephemeral private key.
226 OPENSSL_EXPORT int EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
227 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
228 const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
229 const uint8_t *peer_public_key, size_t peer_public_key_len,
230 const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len);
231
232 // EVP_HPKE_CTX_setup_recipient implements the SetupBaseR HPKE operation. It
233 // decapsulates the shared secret in |enc| with |key| and sets up |ctx| as a
234 // recipient context. It returns one on success and zero on failure. Note that
235 // |enc| may be invalid, in which case this function will return an error.
236 //
237 // On success, callers may call |EVP_HPKE_CTX_open| to decrypt messages from the
238 // sender. Callers must then call |EVP_HPKE_CTX_cleanup| when done. On failure,
239 // calling |EVP_HPKE_CTX_cleanup| is safe, but not required.
240 OPENSSL_EXPORT int EVP_HPKE_CTX_setup_recipient(
241 EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf,
242 const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len,
243 const uint8_t *info, size_t info_len);
244
245 // EVP_HPKE_CTX_setup_auth_sender implements the SetupAuthS HPKE operation. It
246 // behaves like |EVP_HPKE_CTX_setup_sender| but authenticates the resulting
247 // context with |key|.
248 OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_sender(
249 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
250 const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
251 const uint8_t *peer_public_key, size_t peer_public_key_len,
252 const uint8_t *info, size_t info_len);
253
254 // EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing behaves like
255 // |EVP_HPKE_CTX_setup_auth_sender|, but takes a seed to behave
256 // deterministically. The seed's format depends on |kem|. For X25519, it is the
257 // sender's ephemeral private key.
258 OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing(
259 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
260 const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
261 const uint8_t *peer_public_key, size_t peer_public_key_len,
262 const uint8_t *info, size_t info_len, const uint8_t *seed, size_t seed_len);
263
264 // EVP_HPKE_CTX_setup_auth_recipient implements the SetupAuthR HPKE operation.
265 // It behaves like |EVP_HPKE_CTX_setup_recipient| but checks the resulting
266 // context was authenticated with |peer_public_key|.
267 OPENSSL_EXPORT int EVP_HPKE_CTX_setup_auth_recipient(
268 EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf,
269 const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len,
270 const uint8_t *info, size_t info_len, const uint8_t *peer_public_key,
271 size_t peer_public_key_len);
272
273
274 // Using an HPKE context.
275 //
276 // Once set up, callers may encrypt or decrypt with an |EVP_HPKE_CTX| using the
277 // following functions.
278
279 // EVP_HPKE_CTX_open uses the HPKE context |ctx| to authenticate |in_len| bytes
280 // from |in| and |ad_len| bytes from |ad| and to decrypt at most |in_len| bytes
281 // into |out|. It returns one on success, and zero otherwise.
282 //
283 // This operation will fail if the |ctx| context is not set up as a receiver.
284 //
285 // Note that HPKE encryption is stateful and ordered. The sender's first call to
286 // |EVP_HPKE_CTX_seal| must correspond to the recipient's first call to
287 // |EVP_HPKE_CTX_open|, etc.
288 //
289 // At most |in_len| bytes are written to |out|. In order to ensure success,
290 // |max_out_len| should be at least |in_len|. On successful return, |*out_len|
291 // is set to the actual number of bytes written.
292 OPENSSL_EXPORT int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out,
293 size_t *out_len, size_t max_out_len,
294 const uint8_t *in, size_t in_len,
295 const uint8_t *ad, size_t ad_len);
296
297 // EVP_HPKE_CTX_seal uses the HPKE context |ctx| to encrypt and authenticate
298 // |in_len| bytes of ciphertext |in| and authenticate |ad_len| bytes from |ad|,
299 // writing the result to |out|. It returns one on success and zero otherwise.
300 //
301 // This operation will fail if the |ctx| context is not set up as a sender.
302 //
303 // Note that HPKE encryption is stateful and ordered. The sender's first call to
304 // |EVP_HPKE_CTX_seal| must correspond to the recipient's first call to
305 // |EVP_HPKE_CTX_open|, etc.
306 //
307 // At most, |max_out_len| encrypted bytes are written to |out|. On successful
308 // return, |*out_len| is set to the actual number of bytes written.
309 //
310 // To ensure success, |max_out_len| should be |in_len| plus the result of
311 // |EVP_HPKE_CTX_max_overhead| or |EVP_HPKE_MAX_OVERHEAD|.
312 OPENSSL_EXPORT int EVP_HPKE_CTX_seal(EVP_HPKE_CTX *ctx, uint8_t *out,
313 size_t *out_len, size_t max_out_len,
314 const uint8_t *in, size_t in_len,
315 const uint8_t *ad, size_t ad_len);
316
317 // EVP_HPKE_CTX_export uses the HPKE context |ctx| to export a secret of
318 // |secret_len| bytes into |out|. This function uses |context_len| bytes from
319 // |context| as a context string for the secret. This is necessary to separate
320 // different uses of exported secrets and bind relevant caller-specific context
321 // into the output. It returns one on success and zero otherwise.
322 OPENSSL_EXPORT int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out,
323 size_t secret_len,
324 const uint8_t *context,
325 size_t context_len);
326
327 // EVP_HPKE_MAX_OVERHEAD contains the largest value that
328 // |EVP_HPKE_CTX_max_overhead| would ever return for any context.
329 #define EVP_HPKE_MAX_OVERHEAD EVP_AEAD_MAX_OVERHEAD
330
331 // EVP_HPKE_CTX_max_overhead returns the maximum number of additional bytes
332 // added by sealing data with |EVP_HPKE_CTX_seal|. The |ctx| context must be set
333 // up as a sender.
334 OPENSSL_EXPORT size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx);
335
336 // EVP_HPKE_CTX_kem returns |ctx|'s configured KEM, or NULL if the context has
337 // not been set up.
338 OPENSSL_EXPORT const EVP_HPKE_KEM *EVP_HPKE_CTX_kem(const EVP_HPKE_CTX *ctx);
339
340 // EVP_HPKE_CTX_aead returns |ctx|'s configured AEAD, or NULL if the context has
341 // not been set up.
342 OPENSSL_EXPORT const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx);
343
344 // EVP_HPKE_CTX_kdf returns |ctx|'s configured KDF, or NULL if the context has
345 // not been set up.
346 OPENSSL_EXPORT const EVP_HPKE_KDF *EVP_HPKE_CTX_kdf(const EVP_HPKE_CTX *ctx);
347
348
349 // Private structures.
350 //
351 // The following structures are exported so their types are stack-allocatable,
352 // but accessing or modifying their fields is forbidden.
353
354 struct evp_hpke_ctx_st {
355 const EVP_HPKE_KEM *kem;
356 const EVP_HPKE_AEAD *aead;
357 const EVP_HPKE_KDF *kdf;
358 EVP_AEAD_CTX aead_ctx;
359 uint8_t base_nonce[EVP_AEAD_MAX_NONCE_LENGTH];
360 uint8_t exporter_secret[EVP_MAX_MD_SIZE];
361 uint64_t seq;
362 int is_sender;
363 };
364
365 struct evp_hpke_key_st {
366 const EVP_HPKE_KEM *kem;
367 uint8_t private_key[X25519_PRIVATE_KEY_LEN];
368 uint8_t public_key[X25519_PUBLIC_VALUE_LEN];
369 };
370
371
372 #if defined(__cplusplus)
373 } // extern C
374 #endif
375
376 #if !defined(BORINGSSL_NO_CXX)
377 extern "C++" {
378
379 BSSL_NAMESPACE_BEGIN
380
381 using ScopedEVP_HPKE_CTX =
382 internal::StackAllocated<EVP_HPKE_CTX, void, EVP_HPKE_CTX_zero,
383 EVP_HPKE_CTX_cleanup>;
384 using ScopedEVP_HPKE_KEY =
385 internal::StackAllocatedMovable<EVP_HPKE_KEY, void, EVP_HPKE_KEY_zero,
386 EVP_HPKE_KEY_cleanup, EVP_HPKE_KEY_move>;
387
388 BORINGSSL_MAKE_DELETER(EVP_HPKE_CTX, EVP_HPKE_CTX_free)
389 BORINGSSL_MAKE_DELETER(EVP_HPKE_KEY, EVP_HPKE_KEY_free)
390
391 BSSL_NAMESPACE_END
392
393 } // extern C++
394 #endif
395
396 #endif // OPENSSL_HEADER_CRYPTO_HPKE_INTERNAL_H