Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/hpke/hpke.c

raw
1 // Copyright (c) 2020, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/hpke.h>
5
6 #include <assert.h>
7 #include <string.h>
8
9 #include <openssl/aead.h>
10 #include <openssl/bytestring.h>
11 #include <openssl/curve25519.h>
12 #include <openssl/digest.h>
13 #include <openssl/err.h>
14 #include <openssl/evp_errors.h>
15 #include <openssl/hkdf.h>
16 #include <openssl/rand.h>
17 #include <openssl/sha.h>
18
19 #include "../internal.h"
20
21
22 // This file implements RFC 9180.
23
24 #define MAX_SEED_LEN X25519_PRIVATE_KEY_LEN
25 #define MAX_SHARED_SECRET_LEN SHA256_DIGEST_LENGTH
26
27 struct evp_hpke_kem_st {
28 uint16_t id;
29 size_t public_key_len;
30 size_t private_key_len;
31 size_t seed_len;
32 size_t enc_len;
33 int (*init_key)(EVP_HPKE_KEY *key, const uint8_t *priv_key,
34 size_t priv_key_len);
35 int (*generate_key)(EVP_HPKE_KEY *key);
36 int (*encap_with_seed)(const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret,
37 size_t *out_shared_secret_len, uint8_t *out_enc,
38 size_t *out_enc_len, size_t max_enc,
39 const uint8_t *peer_public_key,
40 size_t peer_public_key_len, const uint8_t *seed,
41 size_t seed_len);
42 int (*decap)(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
43 size_t *out_shared_secret_len, const uint8_t *enc,
44 size_t enc_len);
45 int (*auth_encap_with_seed)(const EVP_HPKE_KEY *key,
46 uint8_t *out_shared_secret,
47 size_t *out_shared_secret_len, uint8_t *out_enc,
48 size_t *out_enc_len, size_t max_enc,
49 const uint8_t *peer_public_key,
50 size_t peer_public_key_len, const uint8_t *seed,
51 size_t seed_len);
52 int (*auth_decap)(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
53 size_t *out_shared_secret_len, const uint8_t *enc,
54 size_t enc_len, const uint8_t *peer_public_key,
55 size_t peer_public_key_len);
56 };
57
58 struct evp_hpke_kdf_st {
59 uint16_t id;
60 // We only support HKDF-based KDFs.
61 const EVP_MD *(*hkdf_md_func)(void);
62 };
63
64 struct evp_hpke_aead_st {
65 uint16_t id;
66 const EVP_AEAD *(*aead_func)(void);
67 };
68
69
70 // Low-level labeled KDF functions.
71
72 static const char kHpkeVersionId[] = "HPKE-v1";
73
74 static int add_label_string(CBB *cbb, const char *label) {
75 return CBB_add_bytes(cbb, (const uint8_t *)label, strlen(label));
76 }
77
78 static int hpke_labeled_extract(const EVP_MD *hkdf_md, uint8_t *out_key,
79 size_t *out_len, const uint8_t *salt,
80 size_t salt_len, const uint8_t *suite_id,
81 size_t suite_id_len, const char *label,
82 const uint8_t *ikm, size_t ikm_len) {
83 // labeledIKM = concat("HPKE-v1", suite_id, label, IKM)
84 CBB labeled_ikm;
85 int ok = CBB_init(&labeled_ikm, 0) &&
86 add_label_string(&labeled_ikm, kHpkeVersionId) &&
87 CBB_add_bytes(&labeled_ikm, suite_id, suite_id_len) &&
88 add_label_string(&labeled_ikm, label) &&
89 CBB_add_bytes(&labeled_ikm, ikm, ikm_len) &&
90 HKDF_extract(out_key, out_len, hkdf_md, CBB_data(&labeled_ikm),
91 CBB_len(&labeled_ikm), salt, salt_len);
92 CBB_cleanup(&labeled_ikm);
93 return ok;
94 }
95
96 static int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key,
97 size_t out_len, const uint8_t *prk,
98 size_t prk_len, const uint8_t *suite_id,
99 size_t suite_id_len, const char *label,
100 const uint8_t *info, size_t info_len) {
101 // labeledInfo = concat(I2OSP(L, 2), "HPKE-v1", suite_id, label, info)
102 CBB labeled_info;
103 int ok = CBB_init(&labeled_info, 0) &&
104 CBB_add_u16(&labeled_info, out_len) &&
105 add_label_string(&labeled_info, kHpkeVersionId) &&
106 CBB_add_bytes(&labeled_info, suite_id, suite_id_len) &&
107 add_label_string(&labeled_info, label) &&
108 CBB_add_bytes(&labeled_info, info, info_len) &&
109 HKDF_expand(out_key, out_len, hkdf_md, prk, prk_len,
110 CBB_data(&labeled_info), CBB_len(&labeled_info));
111 CBB_cleanup(&labeled_info);
112 return ok;
113 }
114
115
116 // KEM implementations.
117
118 // dhkem_extract_and_expand implements the ExtractAndExpand operation in the
119 // DHKEM construction. See section 4.1 of RFC 9180.
120 static int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md,
121 uint8_t *out_key, size_t out_len,
122 const uint8_t *dh, size_t dh_len,
123 const uint8_t *kem_context,
124 size_t kem_context_len) {
125 // concat("KEM", I2OSP(kem_id, 2))
126 uint8_t suite_id[5] = {'K', 'E', 'M', kem_id >> 8, kem_id & 0xff};
127 uint8_t prk[EVP_MAX_MD_SIZE];
128 size_t prk_len;
129 return hpke_labeled_extract(hkdf_md, prk, &prk_len, NULL, 0, suite_id,
130 sizeof(suite_id), "eae_prk", dh, dh_len) &&
131 hpke_labeled_expand(hkdf_md, out_key, out_len, prk, prk_len, suite_id,
132 sizeof(suite_id), "shared_secret", kem_context,
133 kem_context_len);
134 }
135
136 static int x25519_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key,
137 size_t priv_key_len) {
138 if (priv_key_len != X25519_PRIVATE_KEY_LEN) {
139 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
140 return 0;
141 }
142
143 OPENSSL_memcpy(key->private_key, priv_key, priv_key_len);
144 X25519_public_from_private(key->public_key, priv_key);
145 return 1;
146 }
147
148 static int x25519_generate_key(EVP_HPKE_KEY *key) {
149 X25519_keypair(key->public_key, key->private_key);
150 return 1;
151 }
152
153 static int x25519_encap_with_seed(
154 const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret,
155 size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len,
156 size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len,
157 const uint8_t *seed, size_t seed_len) {
158 if (max_enc < X25519_PUBLIC_VALUE_LEN) {
159 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
160 return 0;
161 }
162 if (seed_len != X25519_PRIVATE_KEY_LEN) {
163 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
164 return 0;
165 }
166 X25519_public_from_private(out_enc, seed);
167
168 uint8_t dh[X25519_SHARED_KEY_LEN];
169 if (peer_public_key_len != X25519_PUBLIC_VALUE_LEN ||
170 !X25519(dh, seed, peer_public_key)) {
171 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
172 return 0;
173 }
174
175 uint8_t kem_context[2 * X25519_PUBLIC_VALUE_LEN];
176 OPENSSL_memcpy(kem_context, out_enc, X25519_PUBLIC_VALUE_LEN);
177 OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, peer_public_key,
178 X25519_PUBLIC_VALUE_LEN);
179 if (!dhkem_extract_and_expand(kem->id, EVP_sha256(), out_shared_secret,
180 SHA256_DIGEST_LENGTH, dh, sizeof(dh),
181 kem_context, sizeof(kem_context))) {
182 return 0;
183 }
184
185 *out_enc_len = X25519_PUBLIC_VALUE_LEN;
186 *out_shared_secret_len = SHA256_DIGEST_LENGTH;
187 return 1;
188 }
189
190 static int x25519_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
191 size_t *out_shared_secret_len, const uint8_t *enc,
192 size_t enc_len) {
193 uint8_t dh[X25519_SHARED_KEY_LEN];
194 if (enc_len != X25519_PUBLIC_VALUE_LEN ||
195 !X25519(dh, key->private_key, enc)) {
196 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
197 return 0;
198 }
199
200 uint8_t kem_context[2 * X25519_PUBLIC_VALUE_LEN];
201 OPENSSL_memcpy(kem_context, enc, X25519_PUBLIC_VALUE_LEN);
202 OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, key->public_key,
203 X25519_PUBLIC_VALUE_LEN);
204 if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
205 SHA256_DIGEST_LENGTH, dh, sizeof(dh),
206 kem_context, sizeof(kem_context))) {
207 return 0;
208 }
209
210 *out_shared_secret_len = SHA256_DIGEST_LENGTH;
211 return 1;
212 }
213
214 static int x25519_auth_encap_with_seed(
215 const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
216 size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len,
217 size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len,
218 const uint8_t *seed, size_t seed_len) {
219 if (max_enc < X25519_PUBLIC_VALUE_LEN) {
220 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
221 return 0;
222 }
223 if (seed_len != X25519_PRIVATE_KEY_LEN) {
224 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
225 return 0;
226 }
227 X25519_public_from_private(out_enc, seed);
228
229 uint8_t dh[2 * X25519_SHARED_KEY_LEN];
230 if (peer_public_key_len != X25519_PUBLIC_VALUE_LEN ||
231 !X25519(dh, seed, peer_public_key) ||
232 !X25519(dh + X25519_SHARED_KEY_LEN, key->private_key, peer_public_key)) {
233 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
234 return 0;
235 }
236
237 uint8_t kem_context[3 * X25519_PUBLIC_VALUE_LEN];
238 OPENSSL_memcpy(kem_context, out_enc, X25519_PUBLIC_VALUE_LEN);
239 OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, peer_public_key,
240 X25519_PUBLIC_VALUE_LEN);
241 OPENSSL_memcpy(kem_context + 2 * X25519_PUBLIC_VALUE_LEN, key->public_key,
242 X25519_PUBLIC_VALUE_LEN);
243 if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
244 SHA256_DIGEST_LENGTH, dh, sizeof(dh),
245 kem_context, sizeof(kem_context))) {
246 return 0;
247 }
248
249 *out_enc_len = X25519_PUBLIC_VALUE_LEN;
250 *out_shared_secret_len = SHA256_DIGEST_LENGTH;
251 return 1;
252 }
253
254 static int x25519_auth_decap(const EVP_HPKE_KEY *key,
255 uint8_t *out_shared_secret,
256 size_t *out_shared_secret_len, const uint8_t *enc,
257 size_t enc_len, const uint8_t *peer_public_key,
258 size_t peer_public_key_len) {
259 uint8_t dh[2 * X25519_SHARED_KEY_LEN];
260 if (enc_len != X25519_PUBLIC_VALUE_LEN ||
261 peer_public_key_len != X25519_PUBLIC_VALUE_LEN ||
262 !X25519(dh, key->private_key, enc) ||
263 !X25519(dh + X25519_SHARED_KEY_LEN, key->private_key, peer_public_key)) {
264 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
265 return 0;
266 }
267
268 uint8_t kem_context[3 * X25519_PUBLIC_VALUE_LEN];
269 OPENSSL_memcpy(kem_context, enc, X25519_PUBLIC_VALUE_LEN);
270 OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, key->public_key,
271 X25519_PUBLIC_VALUE_LEN);
272 OPENSSL_memcpy(kem_context + 2 * X25519_PUBLIC_VALUE_LEN, peer_public_key,
273 X25519_PUBLIC_VALUE_LEN);
274 if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
275 SHA256_DIGEST_LENGTH, dh, sizeof(dh),
276 kem_context, sizeof(kem_context))) {
277 return 0;
278 }
279
280 *out_shared_secret_len = SHA256_DIGEST_LENGTH;
281 return 1;
282 }
283
284 const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void) {
285 static const EVP_HPKE_KEM kKEM = {
286 /*id=*/EVP_HPKE_DHKEM_X25519_HKDF_SHA256,
287 /*public_key_len=*/X25519_PUBLIC_VALUE_LEN,
288 /*private_key_len=*/X25519_PRIVATE_KEY_LEN,
289 /*seed_len=*/X25519_PRIVATE_KEY_LEN,
290 /*enc_len=*/X25519_PUBLIC_VALUE_LEN,
291 x25519_init_key,
292 x25519_generate_key,
293 x25519_encap_with_seed,
294 x25519_decap,
295 x25519_auth_encap_with_seed,
296 x25519_auth_decap,
297 };
298 return &kKEM;
299 }
300
301 uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem) { return kem->id; }
302
303 size_t EVP_HPKE_KEM_public_key_len(const EVP_HPKE_KEM *kem) {
304 return kem->public_key_len;
305 }
306
307 size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem) {
308 return kem->private_key_len;
309 }
310
311 size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem) { return kem->enc_len; }
312
313 void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key) {
314 OPENSSL_memset(key, 0, sizeof(EVP_HPKE_KEY));
315 }
316
317 void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key) {
318 // Nothing to clean up for now, but we may introduce a cleanup process in the
319 // future.
320 }
321
322 EVP_HPKE_KEY *EVP_HPKE_KEY_new(void) {
323 EVP_HPKE_KEY *key = OPENSSL_malloc(sizeof(EVP_HPKE_KEY));
324 if (key == NULL) {
325 return NULL;
326 }
327 EVP_HPKE_KEY_zero(key);
328 return key;
329 }
330
331 void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key) {
332 if (key != NULL) {
333 EVP_HPKE_KEY_cleanup(key);
334 OPENSSL_free(key);
335 }
336 }
337
338 int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst, const EVP_HPKE_KEY *src) {
339 // For now, |EVP_HPKE_KEY| is trivially copyable.
340 OPENSSL_memcpy(dst, src, sizeof(EVP_HPKE_KEY));
341 return 1;
342 }
343
344 void EVP_HPKE_KEY_move(EVP_HPKE_KEY *out, EVP_HPKE_KEY *in) {
345 EVP_HPKE_KEY_cleanup(out);
346 // For now, |EVP_HPKE_KEY| is trivially movable.
347 OPENSSL_memcpy(out, in, sizeof(EVP_HPKE_KEY));
348 EVP_HPKE_KEY_zero(in);
349 }
350
351 int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem,
352 const uint8_t *priv_key, size_t priv_key_len) {
353 EVP_HPKE_KEY_zero(key);
354 key->kem = kem;
355 if (!kem->init_key(key, priv_key, priv_key_len)) {
356 key->kem = NULL;
357 return 0;
358 }
359 return 1;
360 }
361
362 int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem) {
363 EVP_HPKE_KEY_zero(key);
364 key->kem = kem;
365 if (!kem->generate_key(key)) {
366 key->kem = NULL;
367 return 0;
368 }
369 return 1;
370 }
371
372 const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key) {
373 return key->kem;
374 }
375
376 int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key, uint8_t *out,
377 size_t *out_len, size_t max_out) {
378 if (max_out < key->kem->public_key_len) {
379 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
380 return 0;
381 }
382 OPENSSL_memcpy(out, key->public_key, key->kem->public_key_len);
383 *out_len = key->kem->public_key_len;
384 return 1;
385 }
386
387 int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key, uint8_t *out,
388 size_t *out_len, size_t max_out) {
389 if (max_out < key->kem->private_key_len) {
390 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
391 return 0;
392 }
393 OPENSSL_memcpy(out, key->private_key, key->kem->private_key_len);
394 *out_len = key->kem->private_key_len;
395 return 1;
396 }
397
398
399 // Supported KDFs and AEADs.
400
401 const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void) {
402 static const EVP_HPKE_KDF kKDF = {EVP_HPKE_HKDF_SHA256, &EVP_sha256};
403 return &kKDF;
404 }
405
406 uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf) { return kdf->id; }
407
408 const EVP_MD *EVP_HPKE_KDF_hkdf_md(const EVP_HPKE_KDF *kdf) {
409 return kdf->hkdf_md_func();
410 }
411
412 const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void) {
413 static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_AES_128_GCM,
414 &EVP_aead_aes_128_gcm};
415 return &kAEAD;
416 }
417
418 const EVP_HPKE_AEAD *EVP_hpke_aes_256_gcm(void) {
419 static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_AES_256_GCM,
420 &EVP_aead_aes_256_gcm};
421 return &kAEAD;
422 }
423
424 const EVP_HPKE_AEAD *EVP_hpke_chacha20_poly1305(void) {
425 static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_CHACHA20_POLY1305,
426 &EVP_aead_chacha20_poly1305};
427 return &kAEAD;
428 }
429
430 uint16_t EVP_HPKE_AEAD_id(const EVP_HPKE_AEAD *aead) { return aead->id; }
431
432 const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead) {
433 return aead->aead_func();
434 }
435
436
437 // HPKE implementation.
438
439 // This is strlen("HPKE") + 3 * sizeof(uint16_t).
440 #define HPKE_SUITE_ID_LEN 10
441
442 // The suite_id for non-KEM pieces of HPKE is defined as concat("HPKE",
443 // I2OSP(kem_id, 2), I2OSP(kdf_id, 2), I2OSP(aead_id, 2)).
444 static int hpke_build_suite_id(const EVP_HPKE_CTX *ctx,
445 uint8_t out[HPKE_SUITE_ID_LEN]) {
446 CBB cbb;
447 CBB_init_fixed(&cbb, out, HPKE_SUITE_ID_LEN);
448 return add_label_string(&cbb, "HPKE") && //
449 CBB_add_u16(&cbb, ctx->kem->id) && //
450 CBB_add_u16(&cbb, ctx->kdf->id) && //
451 CBB_add_u16(&cbb, ctx->aead->id);
452 }
453
454 #define HPKE_MODE_BASE 0
455 #define HPKE_MODE_AUTH 2
456
457 static int hpke_key_schedule(EVP_HPKE_CTX *ctx, uint8_t mode,
458 const uint8_t *shared_secret,
459 size_t shared_secret_len, const uint8_t *info,
460 size_t info_len) {
461 uint8_t suite_id[HPKE_SUITE_ID_LEN];
462 if (!hpke_build_suite_id(ctx, suite_id)) {
463 return 0;
464 }
465
466 // psk_id_hash = LabeledExtract("", "psk_id_hash", psk_id)
467 // TODO(davidben): Precompute this value and store it with the EVP_HPKE_KDF.
468 const EVP_MD *hkdf_md = ctx->kdf->hkdf_md_func();
469 uint8_t psk_id_hash[EVP_MAX_MD_SIZE];
470 size_t psk_id_hash_len;
471 if (!hpke_labeled_extract(hkdf_md, psk_id_hash, &psk_id_hash_len, NULL, 0,
472 suite_id, sizeof(suite_id), "psk_id_hash", NULL,
473 0)) {
474 return 0;
475 }
476
477 // info_hash = LabeledExtract("", "info_hash", info)
478 uint8_t info_hash[EVP_MAX_MD_SIZE];
479 size_t info_hash_len;
480 if (!hpke_labeled_extract(hkdf_md, info_hash, &info_hash_len, NULL, 0,
481 suite_id, sizeof(suite_id), "info_hash", info,
482 info_len)) {
483 return 0;
484 }
485
486 // key_schedule_context = concat(mode, psk_id_hash, info_hash)
487 uint8_t context[sizeof(uint8_t) + 2 * EVP_MAX_MD_SIZE];
488 size_t context_len;
489 CBB context_cbb;
490 CBB_init_fixed(&context_cbb, context, sizeof(context));
491 if (!CBB_add_u8(&context_cbb, mode) ||
492 !CBB_add_bytes(&context_cbb, psk_id_hash, psk_id_hash_len) ||
493 !CBB_add_bytes(&context_cbb, info_hash, info_hash_len) ||
494 !CBB_finish(&context_cbb, NULL, &context_len)) {
495 return 0;
496 }
497
498 // secret = LabeledExtract(shared_secret, "secret", psk)
499 uint8_t secret[EVP_MAX_MD_SIZE];
500 size_t secret_len;
501 if (!hpke_labeled_extract(hkdf_md, secret, &secret_len, shared_secret,
502 shared_secret_len, suite_id, sizeof(suite_id),
503 "secret", NULL, 0)) {
504 return 0;
505 }
506
507 // key = LabeledExpand(secret, "key", key_schedule_context, Nk)
508 const EVP_AEAD *aead = EVP_HPKE_AEAD_aead(ctx->aead);
509 uint8_t key[EVP_AEAD_MAX_KEY_LENGTH];
510 const size_t kKeyLen = EVP_AEAD_key_length(aead);
511 if (!hpke_labeled_expand(hkdf_md, key, kKeyLen, secret, secret_len, suite_id,
512 sizeof(suite_id), "key", context, context_len) ||
513 !EVP_AEAD_CTX_init(&ctx->aead_ctx, aead, key, kKeyLen,
514 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL)) {
515 return 0;
516 }
517
518 // base_nonce = LabeledExpand(secret, "base_nonce", key_schedule_context, Nn)
519 if (!hpke_labeled_expand(hkdf_md, ctx->base_nonce,
520 EVP_AEAD_nonce_length(aead), secret, secret_len,
521 suite_id, sizeof(suite_id), "base_nonce", context,
522 context_len)) {
523 return 0;
524 }
525
526 // exporter_secret = LabeledExpand(secret, "exp", key_schedule_context, Nh)
527 if (!hpke_labeled_expand(hkdf_md, ctx->exporter_secret, EVP_MD_size(hkdf_md),
528 secret, secret_len, suite_id, sizeof(suite_id),
529 "exp", context, context_len)) {
530 return 0;
531 }
532
533 return 1;
534 }
535
536 void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx) {
537 OPENSSL_memset(ctx, 0, sizeof(EVP_HPKE_CTX));
538 EVP_AEAD_CTX_zero(&ctx->aead_ctx);
539 }
540
541 void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx) {
542 EVP_AEAD_CTX_cleanup(&ctx->aead_ctx);
543 }
544
545 EVP_HPKE_CTX *EVP_HPKE_CTX_new(void) {
546 EVP_HPKE_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_HPKE_CTX));
547 if (ctx == NULL) {
548 return NULL;
549 }
550 // NO-OP: struct already zeroed
551 //EVP_HPKE_CTX_zero(ctx);
552 return ctx;
553 }
554
555 void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx) {
556 if (ctx != NULL) {
557 EVP_HPKE_CTX_cleanup(ctx);
558 OPENSSL_free(ctx);
559 }
560 }
561
562 int EVP_HPKE_CTX_setup_sender(EVP_HPKE_CTX *ctx, uint8_t *out_enc,
563 size_t *out_enc_len, size_t max_enc,
564 const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf,
565 const EVP_HPKE_AEAD *aead,
566 const uint8_t *peer_public_key,
567 size_t peer_public_key_len, const uint8_t *info,
568 size_t info_len) {
569 uint8_t seed[MAX_SEED_LEN];
570 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(seed, kem->seed_len));
571 return EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
572 ctx, out_enc, out_enc_len, max_enc, kem, kdf, aead, peer_public_key,
573 peer_public_key_len, info, info_len, seed, kem->seed_len);
574 }
575
576 int EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
577 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
578 const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
579 const uint8_t *peer_public_key, size_t peer_public_key_len,
580 const uint8_t *info, size_t info_len, const uint8_t *seed,
581 size_t seed_len) {
582 EVP_HPKE_CTX_zero(ctx);
583 ctx->is_sender = 1;
584 ctx->kem = kem;
585 ctx->kdf = kdf;
586 ctx->aead = aead;
587 uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
588 size_t shared_secret_len;
589 if (!kem->encap_with_seed(kem, shared_secret, &shared_secret_len, out_enc,
590 out_enc_len, max_enc, peer_public_key,
591 peer_public_key_len, seed, seed_len) ||
592 !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len,
593 info, info_len)) {
594 EVP_HPKE_CTX_cleanup(ctx);
595 return 0;
596 }
597 return 1;
598 }
599
600 int EVP_HPKE_CTX_setup_recipient(EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key,
601 const EVP_HPKE_KDF *kdf,
602 const EVP_HPKE_AEAD *aead, const uint8_t *enc,
603 size_t enc_len, const uint8_t *info,
604 size_t info_len) {
605 EVP_HPKE_CTX_zero(ctx);
606 ctx->is_sender = 0;
607 ctx->kem = key->kem;
608 ctx->kdf = kdf;
609 ctx->aead = aead;
610 uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
611 size_t shared_secret_len;
612 if (!key->kem->decap(key, shared_secret, &shared_secret_len, enc, enc_len) ||
613 !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len,
614 info, info_len)) {
615 EVP_HPKE_CTX_cleanup(ctx);
616 return 0;
617 }
618 return 1;
619 }
620
621
622 int EVP_HPKE_CTX_setup_auth_sender(
623 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
624 const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
625 const uint8_t *peer_public_key, size_t peer_public_key_len,
626 const uint8_t *info, size_t info_len) {
627 uint8_t seed[MAX_SEED_LEN];
628 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(seed, key->kem->seed_len));
629 return EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing(
630 ctx, out_enc, out_enc_len, max_enc, key, kdf, aead, peer_public_key,
631 peer_public_key_len, info, info_len, seed, key->kem->seed_len);
632 }
633
634 int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing(
635 EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
636 const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
637 const uint8_t *peer_public_key, size_t peer_public_key_len,
638 const uint8_t *info, size_t info_len, const uint8_t *seed,
639 size_t seed_len) {
640 if (key->kem->auth_encap_with_seed == NULL) {
641 // Not all HPKE KEMs support AuthEncap.
642 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
643 return 0;
644 }
645
646 EVP_HPKE_CTX_zero(ctx);
647 ctx->is_sender = 1;
648 ctx->kem = key->kem;
649 ctx->kdf = kdf;
650 ctx->aead = aead;
651 uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
652 size_t shared_secret_len;
653 if (!key->kem->auth_encap_with_seed(
654 key, shared_secret, &shared_secret_len, out_enc, out_enc_len, max_enc,
655 peer_public_key, peer_public_key_len, seed, seed_len) ||
656 !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret, shared_secret_len,
657 info, info_len)) {
658 EVP_HPKE_CTX_cleanup(ctx);
659 return 0;
660 }
661 return 1;
662 }
663
664 int EVP_HPKE_CTX_setup_auth_recipient(
665 EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf,
666 const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len,
667 const uint8_t *info, size_t info_len, const uint8_t *peer_public_key,
668 size_t peer_public_key_len) {
669 if (key->kem->auth_decap == NULL) {
670 // Not all HPKE KEMs support AuthDecap.
671 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
672 return 0;
673 }
674
675 EVP_HPKE_CTX_zero(ctx);
676 ctx->is_sender = 0;
677 ctx->kem = key->kem;
678 ctx->kdf = kdf;
679 ctx->aead = aead;
680 uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
681 size_t shared_secret_len;
682 if (!key->kem->auth_decap(key, shared_secret, &shared_secret_len, enc,
683 enc_len, peer_public_key, peer_public_key_len) ||
684 !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret, shared_secret_len,
685 info, info_len)) {
686 EVP_HPKE_CTX_cleanup(ctx);
687 return 0;
688 }
689 return 1;
690 }
691
692 static void hpke_nonce(const EVP_HPKE_CTX *ctx, uint8_t *out_nonce,
693 size_t nonce_len) {
694 assert(nonce_len >= 8);
695
696 // Write padded big-endian bytes of |ctx->seq| to |out_nonce|.
697 OPENSSL_memset(out_nonce, 0, nonce_len);
698 uint64_t seq_copy = ctx->seq;
699 for (size_t i = 0; i < 8; i++) {
700 out_nonce[nonce_len - i - 1] = seq_copy & 0xff;
701 seq_copy >>= 8;
702 }
703
704 // XOR the encoded sequence with the |ctx->base_nonce|.
705 for (size_t i = 0; i < nonce_len; i++) {
706 out_nonce[i] ^= ctx->base_nonce[i];
707 }
708 }
709
710 int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len,
711 size_t max_out_len, const uint8_t *in, size_t in_len,
712 const uint8_t *ad, size_t ad_len) {
713 if (ctx->is_sender) {
714 OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
715 return 0;
716 }
717 if (ctx->seq == UINT64_MAX) {
718 OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
719 return 0;
720 }
721
722 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
723 const size_t nonce_len = EVP_AEAD_nonce_length(ctx->aead_ctx.aead);
724 hpke_nonce(ctx, nonce, nonce_len);
725
726 if (!EVP_AEAD_CTX_open(&ctx->aead_ctx, out, out_len, max_out_len, nonce,
727 nonce_len, in, in_len, ad, ad_len)) {
728 return 0;
729 }
730 ctx->seq++;
731 return 1;
732 }
733
734 int EVP_HPKE_CTX_seal(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len,
735 size_t max_out_len, const uint8_t *in, size_t in_len,
736 const uint8_t *ad, size_t ad_len) {
737 if (!ctx->is_sender) {
738 OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
739 return 0;
740 }
741 if (ctx->seq == UINT64_MAX) {
742 OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
743 return 0;
744 }
745
746 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
747 const size_t nonce_len = EVP_AEAD_nonce_length(ctx->aead_ctx.aead);
748 hpke_nonce(ctx, nonce, nonce_len);
749
750 if (!EVP_AEAD_CTX_seal(&ctx->aead_ctx, out, out_len, max_out_len, nonce,
751 nonce_len, in, in_len, ad, ad_len)) {
752 return 0;
753 }
754 ctx->seq++;
755 return 1;
756 }
757
758 int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out,
759 size_t secret_len, const uint8_t *context,
760 size_t context_len) {
761 uint8_t suite_id[HPKE_SUITE_ID_LEN];
762 if (!hpke_build_suite_id(ctx, suite_id)) {
763 return 0;
764 }
765 const EVP_MD *hkdf_md = ctx->kdf->hkdf_md_func();
766 if (!hpke_labeled_expand(hkdf_md, out, secret_len, ctx->exporter_secret,
767 EVP_MD_size(hkdf_md), suite_id, sizeof(suite_id),
768 "sec", context, context_len)) {
769 return 0;
770 }
771 return 1;
772 }
773
774 size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx) {
775 assert(ctx->is_sender);
776 return EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(&ctx->aead_ctx));
777 }
778
779 const EVP_HPKE_KEM *EVP_HPKE_CTX_kem(const EVP_HPKE_CTX *ctx) {
780 return ctx->kem;
781 }
782
783 const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx) {
784 return ctx->aead;
785 }
786
787 const EVP_HPKE_KDF *EVP_HPKE_CTX_kdf(const EVP_HPKE_CTX *ctx) {
788 return ctx->kdf;
789 }