Coverage Report

Created: 2025-11-03 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/hpke/hpke.cc
Line
Count
Source
1
// Copyright 2020 The BoringSSL Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/hpke.h>
16
17
#include <assert.h>
18
#include <string.h>
19
20
#include <openssl/aead.h>
21
#include <openssl/bytestring.h>
22
#include <openssl/curve25519.h>
23
#include <openssl/digest.h>
24
#include <openssl/ec.h>
25
#include <openssl/err.h>
26
#include <openssl/evp_errors.h>
27
#include <openssl/hkdf.h>
28
#include <openssl/mem.h>
29
#include <openssl/mlkem.h>
30
#include <openssl/rand.h>
31
#include <openssl/sha2.h>
32
#include <openssl/xwing.h>
33
34
#include "../fipsmodule/bcm_interface.h"
35
#include "../fipsmodule/ec/internal.h"
36
#include "../internal.h"
37
38
39
// This file implements RFC 9180.
40
41
#define MAX_SEED_LEN XWING_SEED_LEN
42
#define MAX_SHARED_SECRET_LEN SHA256_DIGEST_LENGTH
43
44
struct evp_hpke_kem_st {
45
  uint16_t id;
46
  size_t public_key_len;
47
  size_t private_key_len;
48
  size_t seed_len;
49
  size_t enc_len;
50
  int (*init_key)(EVP_HPKE_KEY *key, const uint8_t *priv_key,
51
                  size_t priv_key_len);
52
  int (*generate_key)(EVP_HPKE_KEY *key);
53
  int (*encap_with_seed)(const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret,
54
                         size_t *out_shared_secret_len, uint8_t *out_enc,
55
                         size_t *out_enc_len, size_t max_enc,
56
                         const uint8_t *peer_public_key,
57
                         size_t peer_public_key_len, const uint8_t *seed,
58
                         size_t seed_len);
59
  int (*decap)(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
60
               size_t *out_shared_secret_len, const uint8_t *enc,
61
               size_t enc_len);
62
  int (*auth_encap_with_seed)(const EVP_HPKE_KEY *key,
63
                              uint8_t *out_shared_secret,
64
                              size_t *out_shared_secret_len, uint8_t *out_enc,
65
                              size_t *out_enc_len, size_t max_enc,
66
                              const uint8_t *peer_public_key,
67
                              size_t peer_public_key_len, const uint8_t *seed,
68
                              size_t seed_len);
69
  int (*auth_decap)(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
70
                    size_t *out_shared_secret_len, const uint8_t *enc,
71
                    size_t enc_len, const uint8_t *peer_public_key,
72
                    size_t peer_public_key_len);
73
};
74
75
struct evp_hpke_kdf_st {
76
  uint16_t id;
77
  // We only support HKDF-based KDFs.
78
  const EVP_MD *(*hkdf_md_func)(void);
79
};
80
81
struct evp_hpke_aead_st {
82
  uint16_t id;
83
  const EVP_AEAD *(*aead_func)(void);
84
};
85
86
87
// Low-level labeled KDF functions.
88
89
static const char kHpkeVersionId[] = "HPKE-v1";
90
91
5.21k
static int add_label_string(CBB *cbb, const char *label) {
92
5.21k
  return CBB_add_bytes(cbb, (const uint8_t *)label, strlen(label));
93
5.21k
}
94
95
static int hpke_labeled_extract(const EVP_MD *hkdf_md, uint8_t *out_key,
96
                                size_t *out_len, const uint8_t *salt,
97
                                size_t salt_len, const uint8_t *suite_id,
98
                                size_t suite_id_len, const char *label,
99
1.22k
                                const uint8_t *ikm, size_t ikm_len) {
100
  // labeledIKM = concat("HPKE-v1", suite_id, label, IKM)
101
1.22k
  CBB labeled_ikm;
102
1.22k
  int ok = CBB_init(&labeled_ikm, 0) &&
103
1.22k
           add_label_string(&labeled_ikm, kHpkeVersionId) &&
104
1.22k
           CBB_add_bytes(&labeled_ikm, suite_id, suite_id_len) &&
105
1.22k
           add_label_string(&labeled_ikm, label) &&
106
1.22k
           CBB_add_bytes(&labeled_ikm, ikm, ikm_len) &&
107
1.22k
           HKDF_extract(out_key, out_len, hkdf_md, CBB_data(&labeled_ikm),
108
1.22k
                        CBB_len(&labeled_ikm), salt, salt_len);
109
1.22k
  CBB_cleanup(&labeled_ikm);
110
1.22k
  return ok;
111
1.22k
}
112
113
static int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key,
114
                               size_t out_len, const uint8_t *prk,
115
                               size_t prk_len, const uint8_t *suite_id,
116
                               size_t suite_id_len, const char *label,
117
1.22k
                               const uint8_t *info, size_t info_len) {
118
  // labeledInfo = concat(I2OSP(L, 2), "HPKE-v1", suite_id, label, info)
119
1.22k
  CBB labeled_info;
120
1.22k
  int ok = CBB_init(&labeled_info, 0) &&  //
121
1.22k
           CBB_add_u16(&labeled_info, out_len) &&
122
1.22k
           add_label_string(&labeled_info, kHpkeVersionId) &&
123
1.22k
           CBB_add_bytes(&labeled_info, suite_id, suite_id_len) &&
124
1.22k
           add_label_string(&labeled_info, label) &&
125
1.22k
           CBB_add_bytes(&labeled_info, info, info_len) &&
126
1.22k
           HKDF_expand(out_key, out_len, hkdf_md, prk, prk_len,
127
1.22k
                       CBB_data(&labeled_info), CBB_len(&labeled_info));
128
1.22k
  CBB_cleanup(&labeled_info);
129
1.22k
  return ok;
130
1.22k
}
131
132
133
// KEM implementations.
134
135
// dhkem_extract_and_expand implements the ExtractAndExpand operation in the
136
// DHKEM construction. See section 4.1 of RFC 9180.
137
static int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md,
138
                                    uint8_t *out_key, size_t out_len,
139
                                    const uint8_t *dh, size_t dh_len,
140
                                    const uint8_t *kem_context,
141
307
                                    size_t kem_context_len) {
142
  // concat("KEM", I2OSP(kem_id, 2))
143
307
  uint8_t suite_id[5] = {'K', 'E', 'M', static_cast<uint8_t>(kem_id >> 8),
144
307
                         static_cast<uint8_t>(kem_id & 0xff)};
145
307
  uint8_t prk[EVP_MAX_MD_SIZE];
146
307
  size_t prk_len;
147
307
  return hpke_labeled_extract(hkdf_md, prk, &prk_len, nullptr, 0, suite_id,
148
307
                              sizeof(suite_id), "eae_prk", dh, dh_len) &&
149
307
         hpke_labeled_expand(hkdf_md, out_key, out_len, prk, prk_len, suite_id,
150
307
                             sizeof(suite_id), "shared_secret", kem_context,
151
307
                             kem_context_len);
152
307
}
153
154
static int x25519_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key,
155
12.4k
                           size_t priv_key_len) {
156
12.4k
  if (priv_key_len != X25519_PRIVATE_KEY_LEN) {
157
609
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
158
609
    return 0;
159
609
  }
160
161
11.8k
  OPENSSL_memcpy(key->private_key, priv_key, priv_key_len);
162
11.8k
  X25519_public_from_private(key->public_key, priv_key);
163
11.8k
  return 1;
164
12.4k
}
165
166
0
static int x25519_generate_key(EVP_HPKE_KEY *key) {
167
0
  X25519_keypair(key->public_key, key->private_key);
168
0
  return 1;
169
0
}
170
171
static int x25519_encap_with_seed(
172
    const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret,
173
    size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len,
174
    size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len,
175
0
    const uint8_t *seed, size_t seed_len) {
176
0
  if (max_enc < X25519_PUBLIC_VALUE_LEN) {
177
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
178
0
    return 0;
179
0
  }
180
0
  if (seed_len != X25519_PRIVATE_KEY_LEN) {
181
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
182
0
    return 0;
183
0
  }
184
0
  X25519_public_from_private(out_enc, seed);
185
186
0
  uint8_t dh[X25519_SHARED_KEY_LEN];
187
0
  if (peer_public_key_len != X25519_PUBLIC_VALUE_LEN ||
188
0
      !X25519(dh, seed, peer_public_key)) {
189
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
190
0
    return 0;
191
0
  }
192
193
0
  uint8_t kem_context[2 * X25519_PUBLIC_VALUE_LEN];
194
0
  OPENSSL_memcpy(kem_context, out_enc, X25519_PUBLIC_VALUE_LEN);
195
0
  OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, peer_public_key,
196
0
                 X25519_PUBLIC_VALUE_LEN);
197
0
  if (!dhkem_extract_and_expand(kem->id, EVP_sha256(), out_shared_secret,
198
0
                                SHA256_DIGEST_LENGTH, dh, sizeof(dh),
199
0
                                kem_context, sizeof(kem_context))) {
200
0
    return 0;
201
0
  }
202
203
0
  *out_enc_len = X25519_PUBLIC_VALUE_LEN;
204
0
  *out_shared_secret_len = SHA256_DIGEST_LENGTH;
205
0
  return 1;
206
0
}
207
208
static int x25519_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
209
                        size_t *out_shared_secret_len, const uint8_t *enc,
210
317
                        size_t enc_len) {
211
317
  uint8_t dh[X25519_SHARED_KEY_LEN];
212
317
  if (enc_len != X25519_PUBLIC_VALUE_LEN ||
213
310
      !X25519(dh, key->private_key, enc)) {
214
10
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
215
10
    return 0;
216
10
  }
217
218
307
  uint8_t kem_context[2 * X25519_PUBLIC_VALUE_LEN];
219
307
  OPENSSL_memcpy(kem_context, enc, X25519_PUBLIC_VALUE_LEN);
220
307
  OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, key->public_key,
221
307
                 X25519_PUBLIC_VALUE_LEN);
222
307
  if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
223
307
                                SHA256_DIGEST_LENGTH, dh, sizeof(dh),
224
307
                                kem_context, sizeof(kem_context))) {
225
0
    return 0;
226
0
  }
227
228
307
  *out_shared_secret_len = SHA256_DIGEST_LENGTH;
229
307
  return 1;
230
307
}
231
232
static int x25519_auth_encap_with_seed(
233
    const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
234
    size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len,
235
    size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len,
236
0
    const uint8_t *seed, size_t seed_len) {
237
0
  if (max_enc < X25519_PUBLIC_VALUE_LEN) {
238
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
239
0
    return 0;
240
0
  }
241
0
  if (seed_len != X25519_PRIVATE_KEY_LEN) {
242
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
243
0
    return 0;
244
0
  }
245
0
  X25519_public_from_private(out_enc, seed);
246
247
0
  uint8_t dh[2 * X25519_SHARED_KEY_LEN];
248
0
  if (peer_public_key_len != X25519_PUBLIC_VALUE_LEN ||
249
0
      !X25519(dh, seed, peer_public_key) ||
250
0
      !X25519(dh + X25519_SHARED_KEY_LEN, key->private_key, peer_public_key)) {
251
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
252
0
    return 0;
253
0
  }
254
255
0
  uint8_t kem_context[3 * X25519_PUBLIC_VALUE_LEN];
256
0
  OPENSSL_memcpy(kem_context, out_enc, X25519_PUBLIC_VALUE_LEN);
257
0
  OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, peer_public_key,
258
0
                 X25519_PUBLIC_VALUE_LEN);
259
0
  OPENSSL_memcpy(kem_context + 2 * X25519_PUBLIC_VALUE_LEN, key->public_key,
260
0
                 X25519_PUBLIC_VALUE_LEN);
261
0
  if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
262
0
                                SHA256_DIGEST_LENGTH, dh, sizeof(dh),
263
0
                                kem_context, sizeof(kem_context))) {
264
0
    return 0;
265
0
  }
266
267
0
  *out_enc_len = X25519_PUBLIC_VALUE_LEN;
268
0
  *out_shared_secret_len = SHA256_DIGEST_LENGTH;
269
0
  return 1;
270
0
}
271
272
static int x25519_auth_decap(const EVP_HPKE_KEY *key,
273
                             uint8_t *out_shared_secret,
274
                             size_t *out_shared_secret_len, const uint8_t *enc,
275
                             size_t enc_len, const uint8_t *peer_public_key,
276
0
                             size_t peer_public_key_len) {
277
0
  uint8_t dh[2 * X25519_SHARED_KEY_LEN];
278
0
  if (enc_len != X25519_PUBLIC_VALUE_LEN ||
279
0
      peer_public_key_len != X25519_PUBLIC_VALUE_LEN ||
280
0
      !X25519(dh, key->private_key, enc) ||
281
0
      !X25519(dh + X25519_SHARED_KEY_LEN, key->private_key, peer_public_key)) {
282
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
283
0
    return 0;
284
0
  }
285
286
0
  uint8_t kem_context[3 * X25519_PUBLIC_VALUE_LEN];
287
0
  OPENSSL_memcpy(kem_context, enc, X25519_PUBLIC_VALUE_LEN);
288
0
  OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, key->public_key,
289
0
                 X25519_PUBLIC_VALUE_LEN);
290
0
  OPENSSL_memcpy(kem_context + 2 * X25519_PUBLIC_VALUE_LEN, peer_public_key,
291
0
                 X25519_PUBLIC_VALUE_LEN);
292
0
  if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
293
0
                                SHA256_DIGEST_LENGTH, dh, sizeof(dh),
294
0
                                kem_context, sizeof(kem_context))) {
295
0
    return 0;
296
0
  }
297
298
0
  *out_shared_secret_len = SHA256_DIGEST_LENGTH;
299
0
  return 1;
300
0
}
301
302
12.4k
const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void) {
303
12.4k
  static const EVP_HPKE_KEM kKEM = {
304
12.4k
      /*id=*/EVP_HPKE_DHKEM_X25519_HKDF_SHA256,
305
12.4k
      /*public_key_len=*/X25519_PUBLIC_VALUE_LEN,
306
12.4k
      /*private_key_len=*/X25519_PRIVATE_KEY_LEN,
307
12.4k
      /*seed_len=*/X25519_PRIVATE_KEY_LEN,
308
12.4k
      /*enc_len=*/X25519_PUBLIC_VALUE_LEN,
309
12.4k
      x25519_init_key,
310
12.4k
      x25519_generate_key,
311
12.4k
      x25519_encap_with_seed,
312
12.4k
      x25519_decap,
313
12.4k
      x25519_auth_encap_with_seed,
314
12.4k
      x25519_auth_decap,
315
12.4k
  };
316
12.4k
  return &kKEM;
317
12.4k
}
318
319
0
#define P256_PRIVATE_KEY_LEN 32
320
0
#define P256_PUBLIC_KEY_LEN 65
321
0
#define P256_PUBLIC_VALUE_LEN 65
322
0
#define P256_SEED_LEN 32
323
0
#define P256_SHARED_KEY_LEN 32
324
325
static int p256_public_from_private(uint8_t out_pub[P256_PUBLIC_VALUE_LEN],
326
0
                                    const uint8_t priv[P256_PRIVATE_KEY_LEN]) {
327
0
  const EC_GROUP *const group = EC_group_p256();
328
0
  const uint8_t kAllZeros[P256_PRIVATE_KEY_LEN] = {0};
329
0
  EC_SCALAR private_scalar;
330
0
  EC_JACOBIAN public_point;
331
0
  EC_AFFINE public_point_affine;
332
333
0
  if (CRYPTO_memcmp(kAllZeros, priv, sizeof(kAllZeros)) == 0) {
334
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
335
0
    return 0;
336
0
  }
337
338
0
  if (!ec_scalar_from_bytes(group, &private_scalar, priv,
339
0
                            P256_PRIVATE_KEY_LEN) ||
340
0
      !ec_point_mul_scalar_base(group, &public_point, &private_scalar) ||
341
0
      !ec_jacobian_to_affine(group, &public_point_affine, &public_point)) {
342
0
    return 0;
343
0
  }
344
345
0
  size_t out_len_x, out_len_y;
346
0
  out_pub[0] = POINT_CONVERSION_UNCOMPRESSED;
347
0
  ec_felem_to_bytes(group, &out_pub[1], &out_len_x, &public_point_affine.X);
348
0
  ec_felem_to_bytes(group, &out_pub[33], &out_len_y, &public_point_affine.Y);
349
0
  return 1;
350
0
}
351
352
static int p256_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key,
353
0
                         size_t priv_key_len) {
354
0
  if (priv_key_len != P256_PRIVATE_KEY_LEN) {
355
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
356
0
    return 0;
357
0
  }
358
359
0
  if (!p256_public_from_private(key->public_key, priv_key)) {
360
0
    return 0;
361
0
  }
362
363
0
  OPENSSL_memcpy(key->private_key, priv_key, priv_key_len);
364
0
  return 1;
365
0
}
366
367
static int p256_private_key_from_seed(uint8_t out_priv[P256_PRIVATE_KEY_LEN],
368
0
                                      const uint8_t seed[P256_SEED_LEN]) {
369
  // https://www.rfc-editor.org/rfc/rfc9180.html#name-derivekeypair
370
0
  const uint8_t suite_id[5] = {'K', 'E', 'M',
371
0
                               EVP_HPKE_DHKEM_P256_HKDF_SHA256 >> 8,
372
0
                               EVP_HPKE_DHKEM_P256_HKDF_SHA256 & 0xff};
373
374
0
  uint8_t dkp_prk[32];
375
0
  size_t dkp_prk_len;
376
0
  if (!hpke_labeled_extract(EVP_sha256(), dkp_prk, &dkp_prk_len, nullptr, 0,
377
0
                            suite_id, sizeof(suite_id), "dkp_prk", seed,
378
0
                            P256_SEED_LEN)) {
379
0
    return 0;
380
0
  }
381
0
  assert(dkp_prk_len == sizeof(dkp_prk));
382
383
0
  const EC_GROUP *const group = EC_group_p256();
384
0
  EC_SCALAR private_scalar;
385
386
0
  for (unsigned counter = 0; counter < 256; counter++) {
387
0
    const uint8_t counter_byte = counter & 0xff;
388
0
    if (!hpke_labeled_expand(EVP_sha256(), out_priv, P256_PRIVATE_KEY_LEN,
389
0
                             dkp_prk, sizeof(dkp_prk), suite_id,
390
0
                             sizeof(suite_id), "candidate", &counter_byte,
391
0
                             sizeof(counter_byte))) {
392
0
      return 0;
393
0
    }
394
395
    // This checks that the scalar is less than the order.
396
0
    if (ec_scalar_from_bytes(group, &private_scalar, out_priv,
397
0
                             P256_PRIVATE_KEY_LEN)) {
398
0
      return 1;
399
0
    }
400
0
  }
401
402
  // This happens with probability of 2^-(32*256).
403
0
  OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR);
404
0
  return 0;
405
0
}
406
407
0
static int p256_generate_key(EVP_HPKE_KEY *key) {
408
0
  uint8_t seed[P256_SEED_LEN];
409
0
  RAND_bytes(seed, sizeof(seed));
410
0
  if (!p256_private_key_from_seed(key->private_key, seed) ||
411
0
      !p256_public_from_private(key->public_key, key->private_key)) {
412
0
    return 0;
413
0
  }
414
0
  return 1;
415
0
}
416
417
static int p256(uint8_t out_dh[P256_SHARED_KEY_LEN],
418
                const uint8_t my_private[P256_PRIVATE_KEY_LEN],
419
0
                const uint8_t their_public[P256_PUBLIC_VALUE_LEN]) {
420
0
  const EC_GROUP *const group = EC_group_p256();
421
0
  EC_SCALAR private_scalar;
422
0
  EC_FELEM x, y;
423
0
  EC_JACOBIAN shared_point, their_point;
424
0
  EC_AFFINE their_point_affine, shared_point_affine;
425
426
0
  if (their_public[0] != POINT_CONVERSION_UNCOMPRESSED ||
427
0
      !ec_felem_from_bytes(group, &x, &their_public[1], 32) ||
428
0
      !ec_felem_from_bytes(group, &y, &their_public[33], 32) ||
429
0
      !ec_point_set_affine_coordinates(group, &their_point_affine, &x, &y) ||
430
0
      !ec_scalar_from_bytes(group, &private_scalar, my_private,
431
0
                            P256_PRIVATE_KEY_LEN)) {
432
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR);
433
0
    return 0;
434
0
  }
435
436
0
  ec_affine_to_jacobian(group, &their_point, &their_point_affine);
437
0
  if (!ec_point_mul_scalar(group, &shared_point, &their_point,
438
0
                           &private_scalar) ||
439
0
      !ec_jacobian_to_affine(group, &shared_point_affine, &shared_point)) {
440
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR);
441
0
    return 0;
442
0
  }
443
444
0
  size_t out_len;
445
0
  ec_felem_to_bytes(group, out_dh, &out_len, &shared_point_affine.X);
446
0
  assert(out_len == P256_SHARED_KEY_LEN);
447
0
  return 1;
448
0
}
449
450
static int p256_encap_with_seed(const EVP_HPKE_KEM *kem,
451
                                uint8_t *out_shared_secret,
452
                                size_t *out_shared_secret_len, uint8_t *out_enc,
453
                                size_t *out_enc_len, size_t max_enc,
454
                                const uint8_t *peer_public_key,
455
                                size_t peer_public_key_len, const uint8_t *seed,
456
0
                                size_t seed_len) {
457
0
  if (max_enc < P256_PUBLIC_VALUE_LEN) {
458
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
459
0
    return 0;
460
0
  }
461
0
  if (seed_len != P256_SEED_LEN) {
462
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
463
0
    return 0;
464
0
  }
465
0
  uint8_t private_key[P256_PRIVATE_KEY_LEN];
466
0
  if (!p256_private_key_from_seed(private_key, seed)) {
467
0
    return 0;
468
0
  }
469
0
  p256_public_from_private(out_enc, private_key);
470
471
0
  uint8_t dh[P256_SHARED_KEY_LEN];
472
0
  if (peer_public_key_len != P256_PUBLIC_VALUE_LEN ||
473
0
      !p256(dh, private_key, peer_public_key)) {
474
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
475
0
    return 0;
476
0
  }
477
478
0
  uint8_t kem_context[2 * P256_PUBLIC_VALUE_LEN];
479
0
  OPENSSL_memcpy(kem_context, out_enc, P256_PUBLIC_VALUE_LEN);
480
0
  OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, peer_public_key,
481
0
                 P256_PUBLIC_VALUE_LEN);
482
0
  if (!dhkem_extract_and_expand(kem->id, EVP_sha256(), out_shared_secret,
483
0
                                SHA256_DIGEST_LENGTH, dh, sizeof(dh),
484
0
                                kem_context, sizeof(kem_context))) {
485
0
    return 0;
486
0
  }
487
488
0
  *out_enc_len = P256_PUBLIC_VALUE_LEN;
489
0
  *out_shared_secret_len = SHA256_DIGEST_LENGTH;
490
0
  return 1;
491
0
}
492
493
static int p256_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
494
                      size_t *out_shared_secret_len, const uint8_t *enc,
495
0
                      size_t enc_len) {
496
0
  uint8_t dh[P256_SHARED_KEY_LEN];
497
0
  if (enc_len != P256_PUBLIC_VALUE_LEN ||  //
498
0
      !p256(dh, key->private_key, enc)) {
499
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
500
0
    return 0;
501
0
  }
502
503
0
  uint8_t kem_context[2 * P256_PUBLIC_VALUE_LEN];
504
0
  OPENSSL_memcpy(kem_context, enc, P256_PUBLIC_VALUE_LEN);
505
0
  OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, key->public_key,
506
0
                 P256_PUBLIC_VALUE_LEN);
507
0
  if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
508
0
                                SHA256_DIGEST_LENGTH, dh, sizeof(dh),
509
0
                                kem_context, sizeof(kem_context))) {
510
0
    return 0;
511
0
  }
512
513
0
  *out_shared_secret_len = SHA256_DIGEST_LENGTH;
514
0
  return 1;
515
0
}
516
517
static int p256_auth_encap_with_seed(
518
    const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
519
    size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len,
520
    size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len,
521
0
    const uint8_t *seed, size_t seed_len) {
522
0
  if (max_enc < P256_PUBLIC_VALUE_LEN) {
523
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
524
0
    return 0;
525
0
  }
526
0
  if (seed_len != P256_SEED_LEN) {
527
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
528
0
    return 0;
529
0
  }
530
0
  uint8_t private_key[P256_PRIVATE_KEY_LEN];
531
0
  if (!p256_private_key_from_seed(private_key, seed)) {
532
0
    return 0;
533
0
  }
534
0
  p256_public_from_private(out_enc, private_key);
535
536
0
  uint8_t dh[2 * P256_SHARED_KEY_LEN];
537
0
  if (peer_public_key_len != P256_PUBLIC_VALUE_LEN ||
538
0
      !p256(dh, private_key, peer_public_key) ||
539
0
      !p256(dh + P256_SHARED_KEY_LEN, key->private_key, peer_public_key)) {
540
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
541
0
    return 0;
542
0
  }
543
544
0
  uint8_t kem_context[3 * P256_PUBLIC_VALUE_LEN];
545
0
  OPENSSL_memcpy(kem_context, out_enc, P256_PUBLIC_VALUE_LEN);
546
0
  OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, peer_public_key,
547
0
                 P256_PUBLIC_VALUE_LEN);
548
0
  OPENSSL_memcpy(kem_context + 2 * P256_PUBLIC_VALUE_LEN, key->public_key,
549
0
                 P256_PUBLIC_VALUE_LEN);
550
0
  if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
551
0
                                SHA256_DIGEST_LENGTH, dh, sizeof(dh),
552
0
                                kem_context, sizeof(kem_context))) {
553
0
    return 0;
554
0
  }
555
556
0
  *out_enc_len = P256_PUBLIC_VALUE_LEN;
557
0
  *out_shared_secret_len = SHA256_DIGEST_LENGTH;
558
0
  return 1;
559
0
}
560
561
static int p256_auth_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
562
                           size_t *out_shared_secret_len, const uint8_t *enc,
563
                           size_t enc_len, const uint8_t *peer_public_key,
564
0
                           size_t peer_public_key_len) {
565
0
  uint8_t dh[2 * P256_SHARED_KEY_LEN];
566
0
  if (enc_len != P256_PUBLIC_VALUE_LEN ||
567
0
      peer_public_key_len != P256_PUBLIC_VALUE_LEN ||
568
0
      !p256(dh, key->private_key, enc) ||
569
0
      !p256(dh + P256_SHARED_KEY_LEN, key->private_key, peer_public_key)) {
570
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
571
0
    return 0;
572
0
  }
573
574
0
  uint8_t kem_context[3 * P256_PUBLIC_VALUE_LEN];
575
0
  OPENSSL_memcpy(kem_context, enc, P256_PUBLIC_VALUE_LEN);
576
0
  OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, key->public_key,
577
0
                 P256_PUBLIC_VALUE_LEN);
578
0
  OPENSSL_memcpy(kem_context + 2 * P256_PUBLIC_VALUE_LEN, peer_public_key,
579
0
                 P256_PUBLIC_VALUE_LEN);
580
0
  if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret,
581
0
                                SHA256_DIGEST_LENGTH, dh, sizeof(dh),
582
0
                                kem_context, sizeof(kem_context))) {
583
0
    return 0;
584
0
  }
585
586
0
  *out_shared_secret_len = SHA256_DIGEST_LENGTH;
587
0
  return 1;
588
0
}
589
590
0
const EVP_HPKE_KEM *EVP_hpke_p256_hkdf_sha256(void) {
591
0
  static const EVP_HPKE_KEM kKEM = {
592
0
      /*id=*/EVP_HPKE_DHKEM_P256_HKDF_SHA256,
593
0
      /*public_key_len=*/P256_PUBLIC_KEY_LEN,
594
0
      /*private_key_len=*/P256_PRIVATE_KEY_LEN,
595
0
      /*seed_len=*/P256_SEED_LEN,
596
0
      /*enc_len=*/P256_PUBLIC_VALUE_LEN,
597
0
      p256_init_key,
598
0
      p256_generate_key,
599
0
      p256_encap_with_seed,
600
0
      p256_decap,
601
0
      p256_auth_encap_with_seed,
602
0
      p256_auth_decap,
603
0
  };
604
0
  return &kKEM;
605
0
}
606
607
0
#define XWING_PRIVATE_KEY_LEN XWING_PRIVATE_KEY_BYTES
608
0
#define XWING_PUBLIC_KEY_LEN XWING_PUBLIC_KEY_BYTES
609
0
#define XWING_PUBLIC_VALUE_LEN XWING_CIPHERTEXT_BYTES
610
0
#define XWING_SEED_LEN 64
611
0
#define XWING_SHARED_KEY_LEN XWING_SHARED_SECRET_BYTES
612
613
static int xwing_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key,
614
0
                          size_t priv_key_len) {
615
0
  CBS cbs;
616
0
  CBS_init(&cbs, priv_key, priv_key_len);
617
0
  XWING_private_key private_key;
618
0
  if (!XWING_parse_private_key(&private_key, &cbs) || CBS_len(&cbs) != 0) {
619
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
620
0
    return 0;
621
0
  }
622
623
0
  if (!XWING_public_from_private(key->public_key, &private_key)) {
624
0
    return 0;
625
0
  }
626
627
0
  if (priv_key_len > sizeof(key->private_key)) {
628
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
629
0
    return 0;
630
0
  }
631
0
  OPENSSL_memcpy(key->private_key, priv_key, priv_key_len);
632
0
  return 1;
633
0
}
634
635
0
static int xwing_generate_key(EVP_HPKE_KEY *key) {
636
0
  XWING_private_key private_key;
637
0
  if (!XWING_generate_key(key->public_key, &private_key)) {
638
0
    return 0;
639
0
  }
640
641
0
  CBB cbb;
642
0
  CBB_init_fixed(&cbb, key->private_key, XWING_PRIVATE_KEY_LEN);
643
0
  if (!XWING_marshal_private_key(&cbb, &private_key) ||
644
0
      CBB_len(&cbb) != XWING_PRIVATE_KEY_LEN) {
645
0
    return 0;
646
0
  }
647
648
0
  return 1;
649
0
}
650
651
static int xwing_encap_with_seed(const EVP_HPKE_KEM *kem,
652
                                 uint8_t *out_shared_secret,
653
                                 size_t *out_shared_secret_len,
654
                                 uint8_t *out_enc, size_t *out_enc_len,
655
                                 size_t max_enc, const uint8_t *peer_public_key,
656
                                 size_t peer_public_key_len,
657
0
                                 const uint8_t *seed, size_t seed_len) {
658
0
  if (max_enc < XWING_PUBLIC_VALUE_LEN) {
659
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
660
0
    return 0;
661
0
  }
662
0
  if (peer_public_key_len != XWING_PUBLIC_KEY_LEN ||
663
0
      seed_len != XWING_SEED_LEN) {
664
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
665
0
    return 0;
666
0
  }
667
668
0
  if (!XWING_encap_external_entropy(out_enc, out_shared_secret, peer_public_key,
669
0
                                    seed)) {
670
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR);
671
0
    return 0;
672
0
  }
673
674
0
  *out_enc_len = XWING_PUBLIC_VALUE_LEN;
675
0
  *out_shared_secret_len = XWING_SHARED_KEY_LEN;
676
0
  return 1;
677
0
}
678
679
static int xwing_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
680
                       size_t *out_shared_secret_len, const uint8_t *enc,
681
0
                       size_t enc_len) {
682
0
  if (enc_len != XWING_PUBLIC_VALUE_LEN) {
683
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
684
0
    return 0;
685
0
  }
686
687
0
  CBS cbs;
688
0
  CBS_init(&cbs, key->private_key, XWING_PRIVATE_KEY_LEN);
689
0
  XWING_private_key private_key;
690
0
  if (!XWING_parse_private_key(&private_key, &cbs)) {
691
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
692
0
    return 0;
693
0
  }
694
695
0
  if (!XWING_decap(out_shared_secret, enc, &private_key)) {
696
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR);
697
0
    return 0;
698
0
  }
699
700
0
  *out_shared_secret_len = XWING_SHARED_KEY_LEN;
701
0
  return 1;
702
0
}
703
704
0
const EVP_HPKE_KEM *EVP_hpke_xwing(void) {
705
0
  static const EVP_HPKE_KEM kKEM = {
706
0
      /*id=*/EVP_HPKE_XWING,
707
0
      /*public_key_len=*/XWING_PUBLIC_KEY_LEN,
708
0
      /*private_key_len=*/XWING_PRIVATE_KEY_LEN,
709
0
      /*seed_len=*/XWING_SEED_LEN,
710
0
      /*enc_len=*/XWING_PUBLIC_VALUE_LEN,
711
0
      xwing_init_key,
712
0
      xwing_generate_key,
713
0
      xwing_encap_with_seed,
714
0
      xwing_decap,
715
      // X-Wing doesn't support authenticated encapsulation/decapsulation:
716
      // https://datatracker.ietf.org/doc/html/draft-connolly-cfrg-xwing-kem-08#name-use-in-hpke
717
0
      /* auth_encap_with_seed= */ nullptr,
718
0
      /* auth_decap= */ nullptr,
719
0
  };
720
0
  return &kKEM;
721
0
}
722
723
namespace {
724
725
template <uint16_t KEM_ID, size_t PUBLIC_KEY_BYTES, size_t CIPHERTEXT_BYTES,
726
          size_t ENCAP_ENTROPY_BYTES,
727
728
          typename PrivateKey, typename PublicKey,
729
730
          int (*PrivateKeyFromSeed)(PrivateKey *, const uint8_t *, size_t),
731
          void (*PublicFromPrivate)(PublicKey *, const PrivateKey *),
732
          int (*MarshalPublicKey)(CBB *, const PublicKey *),
733
          void (*GenerateKey)(uint8_t *, uint8_t *, PrivateKey *),
734
          int (*ParsePublicKey)(PublicKey *, CBS *),
735
          bcm_infallible (*BCMEncapExternalEntropy)(
736
              uint8_t *, uint8_t *, const PublicKey *, const uint8_t *),
737
          int (*Decap)(uint8_t *, const uint8_t *, size_t, const PrivateKey *)>
738
struct MLKEMHPKE {
739
  // These sizes are common across both ML-KEM-768 and ML-KEM-1024.
740
  static constexpr size_t PRIVATE_KEY_LEN = MLKEM_SEED_BYTES;
741
  static constexpr size_t SHARED_KEY_LEN = MLKEM_SHARED_SECRET_BYTES;
742
743
  static constexpr uint16_t ID = KEM_ID;
744
  static constexpr size_t PUBLIC_KEY_LEN = PUBLIC_KEY_BYTES;
745
  static constexpr size_t SEED_LEN = ENCAP_ENTROPY_BYTES;
746
  static constexpr size_t ENC_LEN = CIPHERTEXT_BYTES;
747
748
  static int InitKey(EVP_HPKE_KEY *key, const uint8_t *priv_key,
749
0
                     size_t priv_key_len) {
750
0
    PrivateKey expanded_private_key;
751
0
    if (!PrivateKeyFromSeed(&expanded_private_key, priv_key, priv_key_len)) {
752
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
753
0
      return 0;
754
0
    }
755
0
    PublicKey public_key;
756
0
    PublicFromPrivate(&public_key, &expanded_private_key);
757
0
    CBB cbb;
758
0
    static_assert(sizeof(key->public_key) >= PUBLIC_KEY_BYTES,
759
0
                  "EVP_HPKE_KEY public_key is too small for ML-KEM.");
760
0
    if (!CBB_init_fixed(&cbb, key->public_key, PUBLIC_KEY_BYTES) ||
761
0
        !MarshalPublicKey(&cbb, &public_key)) {
762
0
      return 0;
763
0
    }
764
765
0
    static_assert(sizeof(key->private_key) >= PRIVATE_KEY_LEN,
766
0
                  "EVP_HPKE_KEY private_key is too small for ML-KEM");
767
0
    OPENSSL_memcpy(key->private_key, priv_key, priv_key_len);
768
0
    return 1;
769
0
  }
Unexecuted instantiation: hpke.cc:(anonymous namespace)::MLKEMHPKE<(unsigned short)65, 1184ul, 1088ul, 32ul, MLKEM768_private_key, MLKEM768_public_key, &MLKEM768_private_key_from_seed, &MLKEM768_public_from_private, &MLKEM768_marshal_public_key, &MLKEM768_generate_key, &MLKEM768_parse_public_key, &BCM_mlkem768_encap_external_entropy, &MLKEM768_decap>::InitKey(evp_hpke_key_st*, unsigned char const*, unsigned long)
Unexecuted instantiation: hpke.cc:(anonymous namespace)::MLKEMHPKE<(unsigned short)66, 1568ul, 1568ul, 32ul, MLKEM1024_private_key, MLKEM1024_public_key, &MLKEM1024_private_key_from_seed, &MLKEM1024_public_from_private, &MLKEM1024_marshal_public_key, &MLKEM1024_generate_key, &MLKEM1024_parse_public_key, &BCM_mlkem1024_encap_external_entropy, &MLKEM1024_decap>::InitKey(evp_hpke_key_st*, unsigned char const*, unsigned long)
770
771
0
  static int HpkeGenerateKey(EVP_HPKE_KEY *key) {
772
0
    static_assert(sizeof(key->public_key) >= PUBLIC_KEY_BYTES,
773
0
                  "EVP_HPKE_KEY public_key is too small for ML-KEM.");
774
0
    static_assert(sizeof(key->private_key) >= PRIVATE_KEY_LEN,
775
0
                  "EVP_HPKE_KEY private_key is too small for ML-KEM");
776
0
    PrivateKey expanded_private_key;
777
0
    GenerateKey(key->public_key, key->private_key, &expanded_private_key);
778
779
0
    return 1;
780
0
  }
Unexecuted instantiation: hpke.cc:(anonymous namespace)::MLKEMHPKE<(unsigned short)65, 1184ul, 1088ul, 32ul, MLKEM768_private_key, MLKEM768_public_key, &MLKEM768_private_key_from_seed, &MLKEM768_public_from_private, &MLKEM768_marshal_public_key, &MLKEM768_generate_key, &MLKEM768_parse_public_key, &BCM_mlkem768_encap_external_entropy, &MLKEM768_decap>::HpkeGenerateKey(evp_hpke_key_st*)
Unexecuted instantiation: hpke.cc:(anonymous namespace)::MLKEMHPKE<(unsigned short)66, 1568ul, 1568ul, 32ul, MLKEM1024_private_key, MLKEM1024_public_key, &MLKEM1024_private_key_from_seed, &MLKEM1024_public_from_private, &MLKEM1024_marshal_public_key, &MLKEM1024_generate_key, &MLKEM1024_parse_public_key, &BCM_mlkem1024_encap_external_entropy, &MLKEM1024_decap>::HpkeGenerateKey(evp_hpke_key_st*)
781
782
  static int EncapWithSeed(const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret,
783
                           size_t *out_shared_secret_len, uint8_t *out_enc,
784
                           size_t *out_enc_len, size_t max_enc,
785
                           const uint8_t *peer_public_key,
786
                           size_t peer_public_key_len, const uint8_t *seed,
787
0
                           size_t seed_len) {
788
0
    if (max_enc < CIPHERTEXT_BYTES) {
789
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
790
0
      return 0;
791
0
    }
792
0
    if (peer_public_key_len != PUBLIC_KEY_BYTES ||
793
0
        seed_len != ENCAP_ENTROPY_BYTES) {
794
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
795
0
      return 0;
796
0
    }
797
798
0
    CBS cbs;
799
0
    CBS_init(&cbs, peer_public_key, peer_public_key_len);
800
0
    PublicKey public_key;
801
0
    if (!ParsePublicKey(&public_key, &cbs)) {
802
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
803
0
      return 0;
804
0
    }
805
    // The public ML-KEM interface doesn't support providing the encap entropy
806
    // so the BCM function is used here.
807
0
    BCMEncapExternalEntropy(out_enc, out_shared_secret, &public_key, seed);
808
809
0
    *out_enc_len = CIPHERTEXT_BYTES;
810
0
    *out_shared_secret_len = SHARED_KEY_LEN;
811
0
    return 1;
812
0
  }
Unexecuted instantiation: hpke.cc:(anonymous namespace)::MLKEMHPKE<(unsigned short)65, 1184ul, 1088ul, 32ul, MLKEM768_private_key, MLKEM768_public_key, &MLKEM768_private_key_from_seed, &MLKEM768_public_from_private, &MLKEM768_marshal_public_key, &MLKEM768_generate_key, &MLKEM768_parse_public_key, &BCM_mlkem768_encap_external_entropy, &MLKEM768_decap>::EncapWithSeed(evp_hpke_kem_st const*, unsigned char*, unsigned long*, unsigned char*, unsigned long*, unsigned long, unsigned char const*, unsigned long, unsigned char const*, unsigned long)
Unexecuted instantiation: hpke.cc:(anonymous namespace)::MLKEMHPKE<(unsigned short)66, 1568ul, 1568ul, 32ul, MLKEM1024_private_key, MLKEM1024_public_key, &MLKEM1024_private_key_from_seed, &MLKEM1024_public_from_private, &MLKEM1024_marshal_public_key, &MLKEM1024_generate_key, &MLKEM1024_parse_public_key, &BCM_mlkem1024_encap_external_entropy, &MLKEM1024_decap>::EncapWithSeed(evp_hpke_kem_st const*, unsigned char*, unsigned long*, unsigned char*, unsigned long*, unsigned long, unsigned char const*, unsigned long, unsigned char const*, unsigned long)
813
814
  static int HpkeDecap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret,
815
                       size_t *out_shared_secret_len, const uint8_t *enc,
816
0
                       size_t enc_len) {
817
0
    PrivateKey private_key;
818
0
    if (!PrivateKeyFromSeed(&private_key, key->private_key, PRIVATE_KEY_LEN)) {
819
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
820
0
      return 0;
821
0
    }
822
823
0
    if (!Decap(out_shared_secret, enc, enc_len, &private_key)) {
824
0
      OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR);
825
0
      return 0;
826
0
    }
827
828
0
    *out_shared_secret_len = SHARED_KEY_LEN;
829
0
    return 1;
830
0
  }
Unexecuted instantiation: hpke.cc:(anonymous namespace)::MLKEMHPKE<(unsigned short)65, 1184ul, 1088ul, 32ul, MLKEM768_private_key, MLKEM768_public_key, &MLKEM768_private_key_from_seed, &MLKEM768_public_from_private, &MLKEM768_marshal_public_key, &MLKEM768_generate_key, &MLKEM768_parse_public_key, &BCM_mlkem768_encap_external_entropy, &MLKEM768_decap>::HpkeDecap(evp_hpke_key_st const*, unsigned char*, unsigned long*, unsigned char const*, unsigned long)
Unexecuted instantiation: hpke.cc:(anonymous namespace)::MLKEMHPKE<(unsigned short)66, 1568ul, 1568ul, 32ul, MLKEM1024_private_key, MLKEM1024_public_key, &MLKEM1024_private_key_from_seed, &MLKEM1024_public_from_private, &MLKEM1024_marshal_public_key, &MLKEM1024_generate_key, &MLKEM1024_parse_public_key, &BCM_mlkem1024_encap_external_entropy, &MLKEM1024_decap>::HpkeDecap(evp_hpke_key_st const*, unsigned char*, unsigned long*, unsigned char const*, unsigned long)
831
};
832
833
using MLKEM768HPKE =
834
    MLKEMHPKE<EVP_HPKE_MLKEM768, MLKEM768_PUBLIC_KEY_BYTES,
835
              MLKEM768_CIPHERTEXT_BYTES, BCM_MLKEM_ENCAP_ENTROPY,
836
837
              MLKEM768_private_key, MLKEM768_public_key,
838
839
              MLKEM768_private_key_from_seed, MLKEM768_public_from_private,
840
              MLKEM768_marshal_public_key, MLKEM768_generate_key,
841
              MLKEM768_parse_public_key, BCM_mlkem768_encap_external_entropy,
842
              MLKEM768_decap>;
843
844
using MLKEM1024HPKE =
845
    MLKEMHPKE<EVP_HPKE_MLKEM1024, MLKEM1024_PUBLIC_KEY_BYTES,
846
              MLKEM1024_CIPHERTEXT_BYTES, BCM_MLKEM_ENCAP_ENTROPY,
847
848
              MLKEM1024_private_key, MLKEM1024_public_key,
849
850
              MLKEM1024_private_key_from_seed, MLKEM1024_public_from_private,
851
              MLKEM1024_marshal_public_key, MLKEM1024_generate_key,
852
              MLKEM1024_parse_public_key, BCM_mlkem1024_encap_external_entropy,
853
              MLKEM1024_decap>;
854
855
template <typename MLKEM>
856
static const EVP_HPKE_KEM kMLKEM = {
857
    /*id=*/MLKEM::ID,
858
    /*public_key_len=*/MLKEM::PUBLIC_KEY_LEN,
859
    /*private_key_len=*/MLKEM::PRIVATE_KEY_LEN,
860
    /*seed_len=*/MLKEM::SEED_LEN,
861
    /*enc_len=*/MLKEM::ENC_LEN,
862
    MLKEM::InitKey,
863
    MLKEM::HpkeGenerateKey,
864
    MLKEM::EncapWithSeed,
865
    MLKEM::HpkeDecap,
866
    // ML-KEM doesn't support authenticated encapsulation/decapsulation:
867
    // https://datatracker.ietf.org/doc/draft-ietf-hpke-pq/01/
868
    /*auth_encap_with_seed=*/nullptr,
869
    /*auth_decap=*/nullptr,
870
};
871
872
}  // namespace
873
874
0
const EVP_HPKE_KEM *EVP_hpke_mlkem768(void) { return &kMLKEM<MLKEM768HPKE>; }
875
0
const EVP_HPKE_KEM *EVP_hpke_mlkem1024(void) { return &kMLKEM<MLKEM1024HPKE>; }
876
877
673
uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem) { return kem->id; }
878
879
0
size_t EVP_HPKE_KEM_public_key_len(const EVP_HPKE_KEM *kem) {
880
0
  return kem->public_key_len;
881
0
}
882
883
0
size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem) {
884
0
  return kem->private_key_len;
885
0
}
886
887
0
size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem) { return kem->enc_len; }
888
889
36.6k
void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key) {
890
36.6k
  OPENSSL_memset(key, 0, sizeof(EVP_HPKE_KEY));
891
36.6k
}
892
893
24.2k
void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key) {
894
  // Nothing to clean up for now, but we may introduce a cleanup process in the
895
  // future.
896
24.2k
}
897
898
0
EVP_HPKE_KEY *EVP_HPKE_KEY_new(void) {
899
0
  EVP_HPKE_KEY *key =
900
0
      reinterpret_cast<EVP_HPKE_KEY *>(OPENSSL_malloc(sizeof(EVP_HPKE_KEY)));
901
0
  if (key == nullptr) {
902
0
    return nullptr;
903
0
  }
904
0
  EVP_HPKE_KEY_zero(key);
905
0
  return key;
906
0
}
907
908
0
void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key) {
909
0
  if (key != nullptr) {
910
0
    EVP_HPKE_KEY_cleanup(key);
911
0
    OPENSSL_free(key);
912
0
  }
913
0
}
914
915
6
int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst, const EVP_HPKE_KEY *src) {
916
  // For now, |EVP_HPKE_KEY| is trivially copyable.
917
6
  OPENSSL_memcpy(dst, src, sizeof(EVP_HPKE_KEY));
918
6
  return 1;
919
6
}
920
921
0
void EVP_HPKE_KEY_move(EVP_HPKE_KEY *out, EVP_HPKE_KEY *in) {
922
0
  EVP_HPKE_KEY_cleanup(out);
923
  // For now, |EVP_HPKE_KEY| is trivially movable.
924
  // Note that Rust may move this structure. See
925
  // bssl-crypto/src/scoped.rs:EvpHpkeKey.
926
0
  OPENSSL_memcpy(out, in, sizeof(EVP_HPKE_KEY));
927
0
  EVP_HPKE_KEY_zero(in);
928
0
}
929
930
int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem,
931
12.4k
                      const uint8_t *priv_key, size_t priv_key_len) {
932
12.4k
  EVP_HPKE_KEY_zero(key);
933
12.4k
  key->kem = kem;
934
12.4k
  if (!kem->init_key(key, priv_key, priv_key_len)) {
935
609
    key->kem = nullptr;
936
609
    return 0;
937
609
  }
938
11.8k
  return 1;
939
12.4k
}
940
941
0
int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem) {
942
0
  EVP_HPKE_KEY_zero(key);
943
0
  key->kem = kem;
944
0
  if (!kem->generate_key(key)) {
945
0
    key->kem = nullptr;
946
0
    return 0;
947
0
  }
948
0
  return 1;
949
0
}
950
951
673
const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key) {
952
673
  return key->kem;
953
673
}
954
955
int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key, uint8_t *out,
956
673
                            size_t *out_len, size_t max_out) {
957
673
  if (max_out < key->kem->public_key_len) {
958
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
959
0
    return 0;
960
0
  }
961
673
  OPENSSL_memcpy(out, key->public_key, key->kem->public_key_len);
962
673
  *out_len = key->kem->public_key_len;
963
673
  return 1;
964
673
}
965
966
int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key, uint8_t *out,
967
0
                             size_t *out_len, size_t max_out) {
968
0
  if (max_out < key->kem->private_key_len) {
969
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
970
0
    return 0;
971
0
  }
972
0
  OPENSSL_memcpy(out, key->private_key, key->kem->private_key_len);
973
0
  *out_len = key->kem->private_key_len;
974
0
  return 1;
975
0
}
976
977
978
// Supported KDFs and AEADs.
979
980
317
const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void) {
981
317
  static const EVP_HPKE_KDF kKDF = {EVP_HPKE_HKDF_SHA256, &EVP_sha256};
982
317
  return &kKDF;
983
317
}
984
985
88
uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf) { return kdf->id; }
986
987
0
const EVP_MD *EVP_HPKE_KDF_hkdf_md(const EVP_HPKE_KDF *kdf) {
988
0
  return kdf->hkdf_md_func();
989
0
}
990
991
1.77k
const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void) {
992
1.77k
  static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_AES_128_GCM,
993
1.77k
                                      &EVP_aead_aes_128_gcm};
994
1.77k
  return &kAEAD;
995
1.77k
}
996
997
732
const EVP_HPKE_AEAD *EVP_hpke_aes_256_gcm(void) {
998
732
  static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_AES_256_GCM,
999
732
                                      &EVP_aead_aes_256_gcm};
1000
732
  return &kAEAD;
1001
732
}
1002
1003
728
const EVP_HPKE_AEAD *EVP_hpke_chacha20_poly1305(void) {
1004
728
  static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_CHACHA20_POLY1305,
1005
728
                                      &EVP_aead_chacha20_poly1305};
1006
728
  return &kAEAD;
1007
728
}
1008
1009
3.28k
uint16_t EVP_HPKE_AEAD_id(const EVP_HPKE_AEAD *aead) { return aead->id; }
1010
1011
307
const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead) {
1012
307
  return aead->aead_func();
1013
307
}
1014
1015
1016
// HPKE implementation.
1017
1018
// This is strlen("HPKE") + 3 * sizeof(uint16_t).
1019
307
#define HPKE_SUITE_ID_LEN 10
1020
1021
// The suite_id for non-KEM pieces of HPKE is defined as concat("HPKE",
1022
// I2OSP(kem_id, 2), I2OSP(kdf_id, 2), I2OSP(aead_id, 2)).
1023
static int hpke_build_suite_id(const EVP_HPKE_CTX *ctx,
1024
307
                               uint8_t out[HPKE_SUITE_ID_LEN]) {
1025
307
  CBB cbb;
1026
307
  CBB_init_fixed(&cbb, out, HPKE_SUITE_ID_LEN);
1027
307
  return add_label_string(&cbb, "HPKE") &&   //
1028
307
         CBB_add_u16(&cbb, ctx->kem->id) &&  //
1029
307
         CBB_add_u16(&cbb, ctx->kdf->id) &&  //
1030
307
         CBB_add_u16(&cbb, ctx->aead->id);
1031
307
}
1032
1033
307
#define HPKE_MODE_BASE 0
1034
0
#define HPKE_MODE_AUTH 2
1035
1036
static int hpke_key_schedule(EVP_HPKE_CTX *ctx, uint8_t mode,
1037
                             const uint8_t *shared_secret,
1038
                             size_t shared_secret_len, const uint8_t *info,
1039
307
                             size_t info_len) {
1040
307
  uint8_t suite_id[HPKE_SUITE_ID_LEN];
1041
307
  if (!hpke_build_suite_id(ctx, suite_id)) {
1042
0
    return 0;
1043
0
  }
1044
1045
  // psk_id_hash = LabeledExtract("", "psk_id_hash", psk_id)
1046
  // TODO(davidben): Precompute this value and store it with the EVP_HPKE_KDF.
1047
307
  const EVP_MD *hkdf_md = ctx->kdf->hkdf_md_func();
1048
307
  uint8_t psk_id_hash[EVP_MAX_MD_SIZE];
1049
307
  size_t psk_id_hash_len;
1050
307
  if (!hpke_labeled_extract(hkdf_md, psk_id_hash, &psk_id_hash_len, nullptr, 0,
1051
307
                            suite_id, sizeof(suite_id), "psk_id_hash", nullptr,
1052
307
                            0)) {
1053
0
    return 0;
1054
0
  }
1055
1056
  // info_hash = LabeledExtract("", "info_hash", info)
1057
307
  uint8_t info_hash[EVP_MAX_MD_SIZE];
1058
307
  size_t info_hash_len;
1059
307
  if (!hpke_labeled_extract(hkdf_md, info_hash, &info_hash_len, nullptr, 0,
1060
307
                            suite_id, sizeof(suite_id), "info_hash", info,
1061
307
                            info_len)) {
1062
0
    return 0;
1063
0
  }
1064
1065
  // key_schedule_context = concat(mode, psk_id_hash, info_hash)
1066
307
  uint8_t context[sizeof(uint8_t) + 2 * EVP_MAX_MD_SIZE];
1067
307
  size_t context_len;
1068
307
  CBB context_cbb;
1069
307
  CBB_init_fixed(&context_cbb, context, sizeof(context));
1070
307
  if (!CBB_add_u8(&context_cbb, mode) ||
1071
307
      !CBB_add_bytes(&context_cbb, psk_id_hash, psk_id_hash_len) ||
1072
307
      !CBB_add_bytes(&context_cbb, info_hash, info_hash_len) ||
1073
307
      !CBB_finish(&context_cbb, nullptr, &context_len)) {
1074
0
    return 0;
1075
0
  }
1076
1077
  // secret = LabeledExtract(shared_secret, "secret", psk)
1078
307
  uint8_t secret[EVP_MAX_MD_SIZE];
1079
307
  size_t secret_len;
1080
307
  if (!hpke_labeled_extract(hkdf_md, secret, &secret_len, shared_secret,
1081
307
                            shared_secret_len, suite_id, sizeof(suite_id),
1082
307
                            "secret", nullptr, 0)) {
1083
0
    return 0;
1084
0
  }
1085
1086
  // key = LabeledExpand(secret, "key", key_schedule_context, Nk)
1087
307
  const EVP_AEAD *aead = EVP_HPKE_AEAD_aead(ctx->aead);
1088
307
  uint8_t key[EVP_AEAD_MAX_KEY_LENGTH];
1089
307
  const size_t kKeyLen = EVP_AEAD_key_length(aead);
1090
307
  if (!hpke_labeled_expand(hkdf_md, key, kKeyLen, secret, secret_len, suite_id,
1091
307
                           sizeof(suite_id), "key", context, context_len) ||
1092
307
      !EVP_AEAD_CTX_init(&ctx->aead_ctx, aead, key, kKeyLen,
1093
307
                         EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr)) {
1094
0
    return 0;
1095
0
  }
1096
1097
  // base_nonce = LabeledExpand(secret, "base_nonce", key_schedule_context, Nn)
1098
307
  if (!hpke_labeled_expand(hkdf_md, ctx->base_nonce,
1099
307
                           EVP_AEAD_nonce_length(aead), secret, secret_len,
1100
307
                           suite_id, sizeof(suite_id), "base_nonce", context,
1101
307
                           context_len)) {
1102
0
    return 0;
1103
0
  }
1104
1105
  // exporter_secret = LabeledExpand(secret, "exp", key_schedule_context, Nh)
1106
307
  if (!hpke_labeled_expand(hkdf_md, ctx->exporter_secret, EVP_MD_size(hkdf_md),
1107
307
                           secret, secret_len, suite_id, sizeof(suite_id),
1108
307
                           "exp", context, context_len)) {
1109
0
    return 0;
1110
0
  }
1111
1112
307
  return 1;
1113
307
}
1114
1115
172k
void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx) {
1116
172k
  OPENSSL_memset(ctx, 0, sizeof(EVP_HPKE_CTX));
1117
172k
  EVP_AEAD_CTX_zero(&ctx->aead_ctx);
1118
172k
}
1119
1120
172k
void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx) {
1121
172k
  EVP_AEAD_CTX_cleanup(&ctx->aead_ctx);
1122
172k
}
1123
1124
0
EVP_HPKE_CTX *EVP_HPKE_CTX_new(void) {
1125
0
  EVP_HPKE_CTX *ctx =
1126
0
      reinterpret_cast<EVP_HPKE_CTX *>(OPENSSL_malloc(sizeof(EVP_HPKE_CTX)));
1127
0
  if (ctx == nullptr) {
1128
0
    return nullptr;
1129
0
  }
1130
0
  EVP_HPKE_CTX_zero(ctx);
1131
0
  return ctx;
1132
0
}
1133
1134
0
void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx) {
1135
0
  if (ctx != nullptr) {
1136
0
    EVP_HPKE_CTX_cleanup(ctx);
1137
0
    OPENSSL_free(ctx);
1138
0
  }
1139
0
}
1140
1141
int EVP_HPKE_CTX_setup_sender(EVP_HPKE_CTX *ctx, uint8_t *out_enc,
1142
                              size_t *out_enc_len, size_t max_enc,
1143
                              const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf,
1144
                              const EVP_HPKE_AEAD *aead,
1145
                              const uint8_t *peer_public_key,
1146
                              size_t peer_public_key_len, const uint8_t *info,
1147
0
                              size_t info_len) {
1148
0
  uint8_t seed[MAX_SEED_LEN];
1149
0
  RAND_bytes(seed, kem->seed_len);
1150
0
  return EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
1151
0
      ctx, out_enc, out_enc_len, max_enc, kem, kdf, aead, peer_public_key,
1152
0
      peer_public_key_len, info, info_len, seed, kem->seed_len);
1153
0
}
1154
1155
int EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
1156
    EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
1157
    const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
1158
    const uint8_t *peer_public_key, size_t peer_public_key_len,
1159
    const uint8_t *info, size_t info_len, const uint8_t *seed,
1160
0
    size_t seed_len) {
1161
0
  EVP_HPKE_CTX_zero(ctx);
1162
0
  ctx->is_sender = 1;
1163
0
  ctx->kem = kem;
1164
0
  ctx->kdf = kdf;
1165
0
  ctx->aead = aead;
1166
0
  uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
1167
0
  size_t shared_secret_len;
1168
0
  if (!kem->encap_with_seed(kem, shared_secret, &shared_secret_len, out_enc,
1169
0
                            out_enc_len, max_enc, peer_public_key,
1170
0
                            peer_public_key_len, seed, seed_len) ||
1171
0
      !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len,
1172
0
                         info, info_len)) {
1173
0
    EVP_HPKE_CTX_cleanup(ctx);
1174
0
    return 0;
1175
0
  }
1176
0
  return 1;
1177
0
}
1178
1179
int EVP_HPKE_CTX_setup_recipient(EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key,
1180
                                 const EVP_HPKE_KDF *kdf,
1181
                                 const EVP_HPKE_AEAD *aead, const uint8_t *enc,
1182
                                 size_t enc_len, const uint8_t *info,
1183
317
                                 size_t info_len) {
1184
317
  EVP_HPKE_CTX_zero(ctx);
1185
317
  ctx->is_sender = 0;
1186
317
  ctx->kem = key->kem;
1187
317
  ctx->kdf = kdf;
1188
317
  ctx->aead = aead;
1189
317
  uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
1190
317
  size_t shared_secret_len;
1191
317
  if (!key->kem->decap(key, shared_secret, &shared_secret_len, enc, enc_len) ||
1192
307
      !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len,
1193
307
                         info, info_len)) {
1194
10
    EVP_HPKE_CTX_cleanup(ctx);
1195
10
    return 0;
1196
10
  }
1197
307
  return 1;
1198
317
}
1199
1200
1201
int EVP_HPKE_CTX_setup_auth_sender(
1202
    EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
1203
    const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
1204
    const uint8_t *peer_public_key, size_t peer_public_key_len,
1205
0
    const uint8_t *info, size_t info_len) {
1206
0
  uint8_t seed[MAX_SEED_LEN];
1207
0
  RAND_bytes(seed, key->kem->seed_len);
1208
0
  return EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing(
1209
0
      ctx, out_enc, out_enc_len, max_enc, key, kdf, aead, peer_public_key,
1210
0
      peer_public_key_len, info, info_len, seed, key->kem->seed_len);
1211
0
}
1212
1213
int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing(
1214
    EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc,
1215
    const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead,
1216
    const uint8_t *peer_public_key, size_t peer_public_key_len,
1217
    const uint8_t *info, size_t info_len, const uint8_t *seed,
1218
0
    size_t seed_len) {
1219
0
  if (key->kem->auth_encap_with_seed == nullptr) {
1220
    // Not all HPKE KEMs support AuthEncap.
1221
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1222
0
    return 0;
1223
0
  }
1224
1225
0
  EVP_HPKE_CTX_zero(ctx);
1226
0
  ctx->is_sender = 1;
1227
0
  ctx->kem = key->kem;
1228
0
  ctx->kdf = kdf;
1229
0
  ctx->aead = aead;
1230
0
  uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
1231
0
  size_t shared_secret_len;
1232
0
  if (!key->kem->auth_encap_with_seed(
1233
0
          key, shared_secret, &shared_secret_len, out_enc, out_enc_len, max_enc,
1234
0
          peer_public_key, peer_public_key_len, seed, seed_len) ||
1235
0
      !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret, shared_secret_len,
1236
0
                         info, info_len)) {
1237
0
    EVP_HPKE_CTX_cleanup(ctx);
1238
0
    return 0;
1239
0
  }
1240
0
  return 1;
1241
0
}
1242
1243
int EVP_HPKE_CTX_setup_auth_recipient(
1244
    EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf,
1245
    const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len,
1246
    const uint8_t *info, size_t info_len, const uint8_t *peer_public_key,
1247
0
    size_t peer_public_key_len) {
1248
0
  if (key->kem->auth_decap == nullptr) {
1249
    // Not all HPKE KEMs support AuthDecap.
1250
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
1251
0
    return 0;
1252
0
  }
1253
1254
0
  EVP_HPKE_CTX_zero(ctx);
1255
0
  ctx->is_sender = 0;
1256
0
  ctx->kem = key->kem;
1257
0
  ctx->kdf = kdf;
1258
0
  ctx->aead = aead;
1259
0
  uint8_t shared_secret[MAX_SHARED_SECRET_LEN];
1260
0
  size_t shared_secret_len;
1261
0
  if (!key->kem->auth_decap(key, shared_secret, &shared_secret_len, enc,
1262
0
                            enc_len, peer_public_key, peer_public_key_len) ||
1263
0
      !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret, shared_secret_len,
1264
0
                         info, info_len)) {
1265
0
    EVP_HPKE_CTX_cleanup(ctx);
1266
0
    return 0;
1267
0
  }
1268
0
  return 1;
1269
0
}
1270
1271
static void hpke_nonce(const EVP_HPKE_CTX *ctx, uint8_t *out_nonce,
1272
41
                       size_t nonce_len) {
1273
41
  assert(nonce_len >= 8);
1274
1275
  // Write padded big-endian bytes of |ctx->seq| to |out_nonce|.
1276
41
  OPENSSL_memset(out_nonce, 0, nonce_len);
1277
41
  uint64_t seq_copy = ctx->seq;
1278
369
  for (size_t i = 0; i < 8; i++) {
1279
328
    out_nonce[nonce_len - i - 1] = seq_copy & 0xff;
1280
328
    seq_copy >>= 8;
1281
328
  }
1282
1283
  // XOR the encoded sequence with the |ctx->base_nonce|.
1284
533
  for (size_t i = 0; i < nonce_len; i++) {
1285
492
    out_nonce[i] ^= ctx->base_nonce[i];
1286
492
  }
1287
41
}
1288
1289
int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len,
1290
                      size_t max_out_len, const uint8_t *in, size_t in_len,
1291
41
                      const uint8_t *ad, size_t ad_len) {
1292
41
  if (ctx->is_sender) {
1293
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1294
0
    return 0;
1295
0
  }
1296
41
  if (ctx->seq == UINT64_MAX) {
1297
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
1298
0
    return 0;
1299
0
  }
1300
1301
41
  uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
1302
41
  const size_t nonce_len = EVP_AEAD_nonce_length(ctx->aead_ctx.aead);
1303
41
  hpke_nonce(ctx, nonce, nonce_len);
1304
1305
41
  if (!EVP_AEAD_CTX_open(&ctx->aead_ctx, out, out_len, max_out_len, nonce,
1306
41
                         nonce_len, in, in_len, ad, ad_len)) {
1307
41
    return 0;
1308
41
  }
1309
0
  ctx->seq++;
1310
0
  return 1;
1311
41
}
1312
1313
int EVP_HPKE_CTX_seal(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len,
1314
                      size_t max_out_len, const uint8_t *in, size_t in_len,
1315
0
                      const uint8_t *ad, size_t ad_len) {
1316
0
  if (!ctx->is_sender) {
1317
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1318
0
    return 0;
1319
0
  }
1320
0
  if (ctx->seq == UINT64_MAX) {
1321
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW);
1322
0
    return 0;
1323
0
  }
1324
1325
0
  uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
1326
0
  const size_t nonce_len = EVP_AEAD_nonce_length(ctx->aead_ctx.aead);
1327
0
  hpke_nonce(ctx, nonce, nonce_len);
1328
1329
0
  if (!EVP_AEAD_CTX_seal(&ctx->aead_ctx, out, out_len, max_out_len, nonce,
1330
0
                         nonce_len, in, in_len, ad, ad_len)) {
1331
0
    return 0;
1332
0
  }
1333
0
  ctx->seq++;
1334
0
  return 1;
1335
0
}
1336
1337
int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out,
1338
                        size_t secret_len, const uint8_t *context,
1339
0
                        size_t context_len) {
1340
0
  uint8_t suite_id[HPKE_SUITE_ID_LEN];
1341
0
  if (!hpke_build_suite_id(ctx, suite_id)) {
1342
0
    return 0;
1343
0
  }
1344
0
  const EVP_MD *hkdf_md = ctx->kdf->hkdf_md_func();
1345
0
  if (!hpke_labeled_expand(hkdf_md, out, secret_len, ctx->exporter_secret,
1346
0
                           EVP_MD_size(hkdf_md), suite_id, sizeof(suite_id),
1347
0
                           "sec", context, context_len)) {
1348
0
    return 0;
1349
0
  }
1350
0
  return 1;
1351
0
}
1352
1353
0
size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx) {
1354
0
  assert(ctx->is_sender);
1355
0
  return EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(&ctx->aead_ctx));
1356
0
}
1357
1358
0
const EVP_HPKE_KEM *EVP_HPKE_CTX_kem(const EVP_HPKE_CTX *ctx) {
1359
0
  return ctx->kem;
1360
0
}
1361
1362
58
const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx) {
1363
58
  return ctx->aead;
1364
58
}
1365
1366
88
const EVP_HPKE_KDF *EVP_HPKE_CTX_kdf(const EVP_HPKE_CTX *ctx) {
1367
88
  return ctx->kdf;
1368
88
}