Debian Codegraph @

mlspp/lib/hpke/src/hpke.cpp

raw symbols SCIP
1 #include <hpke/digest.h>
2 #include <hpke/hpke.h>
3
4 #include "aead_cipher.h"
5 #include "common.h"
6 #include "dhkem.h"
7 #include "hkdf.h"
8
9 #include <limits>
10 #include <stdexcept>
11 #include <string>
12
13 namespace mlspp::hpke {
14
15 ///
16 /// Helper functions and constants
17 ///
18
19 static const bytes&
20 label_exp()
21 {
22 static const bytes val = from_ascii("exp");
23 return val;
24 }
25
26 static const bytes&
27 label_hpke()
28 {
29 static const bytes val = from_ascii("HPKE");
30 return val;
31 }
32
33 static const bytes&
34 label_hpke_version()
35 {
36 static const bytes val = from_ascii("HPKE-v1");
37 return val;
38 }
39
40 static const bytes&
41 label_info_hash()
42 {
43 static const bytes val = from_ascii("info_hash");
44 return val;
45 }
46
47 static const bytes&
48 label_key()
49 {
50 static const bytes val = from_ascii("key");
51 return val;
52 }
53
54 static const bytes&
55 label_base_nonce()
56 {
57 static const bytes val = from_ascii("base_nonce");
58 return val;
59 }
60
61 static const bytes&
62 label_psk_id_hash()
63 {
64 static const bytes val = from_ascii("psk_id_hash");
65 return val;
66 }
67
68 static const bytes&
69 label_sec()
70 {
71 static const bytes val = from_ascii("sec");
72 return val;
73 }
74
75 static const bytes&
76 label_secret()
77 {
78 static const bytes val = from_ascii("secret");
79 return val;
80 }
81
82 ///
83 /// Factory methods for primitives
84 ///
85
86 KEM::KEM(ID id_in,
87 size_t secret_size_in,
88 size_t enc_size_in,
89 size_t pk_size_in,
90 size_t sk_size_in)
91 : id(id_in)
92 , secret_size(secret_size_in)
93 , enc_size(enc_size_in)
94 , pk_size(pk_size_in)
95 , sk_size(sk_size_in)
96 {
97 }
98
99 template<>
100 const KEM&
101 KEM::get<KEM::ID::DHKEM_P256_SHA256>()
102 {
103 return DHKEM::get<KEM::ID::DHKEM_P256_SHA256>();
104 }
105
106 template<>
107 const KEM&
108 KEM::get<KEM::ID::DHKEM_P384_SHA384>()
109 {
110 return DHKEM::get<KEM::ID::DHKEM_P384_SHA384>();
111 }
112
113 template<>
114 const KEM&
115 KEM::get<KEM::ID::DHKEM_P521_SHA512>()
116 {
117 return DHKEM::get<KEM::ID::DHKEM_P521_SHA512>();
118 }
119
120 template<>
121 const KEM&
122 KEM::get<KEM::ID::DHKEM_X25519_SHA256>()
123 {
124 return DHKEM::get<KEM::ID::DHKEM_X25519_SHA256>();
125 }
126
127 #if !defined(WITH_BORINGSSL)
128 template<>
129 const KEM&
130 KEM::get<KEM::ID::DHKEM_X448_SHA512>()
131 {
132 return DHKEM::get<KEM::ID::DHKEM_X448_SHA512>();
133 }
134 #endif
135
136 bytes
137 KEM::serialize_private(const KEM::PrivateKey& /* unused */) const
138 {
139 throw std::runtime_error("Not implemented");
140 }
141
142 std::unique_ptr<KEM::PrivateKey>
143 KEM::deserialize_private(const bytes& /* unused */) const
144 {
145 throw std::runtime_error("Not implemented");
146 }
147
148 std::pair<bytes, bytes>
149 KEM::auth_encap(const PublicKey& /* unused */,
150 const PrivateKey& /* unused */) const
151 {
152 throw std::runtime_error("Not implemented");
153 }
154
155 bytes
156 KEM::auth_decap(const bytes& /* unused */,
157 const PublicKey& /* unused */,
158 const PrivateKey& /* unused */) const
159 {
160 throw std::runtime_error("Not implemented");
161 }
162
163 template<>
164 const KDF&
165 KDF::get<KDF::ID::HKDF_SHA256>()
166 {
167 return HKDF::get<Digest::ID::SHA256>();
168 }
169
170 template<>
171 const KDF&
172 KDF::get<KDF::ID::HKDF_SHA384>()
173 {
174 return HKDF::get<Digest::ID::SHA384>();
175 }
176
177 template<>
178 const KDF&
179 KDF::get<KDF::ID::HKDF_SHA512>()
180 {
181 return HKDF::get<Digest::ID::SHA512>();
182 }
183
184 KDF::KDF(ID id_in, size_t hash_size_in)
185 : id(id_in)
186 , hash_size(hash_size_in)
187 {
188 }
189
190 bytes
191 KDF::labeled_extract(const bytes& suite_id,
192 const bytes& salt,
193 const bytes& label,
194 const bytes& ikm) const
195 {
196 auto labeled_ikm = label_hpke_version() + suite_id + label + ikm;
197 return extract(salt, labeled_ikm);
198 }
199
200 bytes
201 KDF::labeled_expand(const bytes& suite_id,
202 const bytes& prk,
203 const bytes& label,
204 const bytes& info,
205 size_t size) const
206 {
207 auto labeled_info =
208 i2osp(size, 2) + label_hpke_version() + suite_id + label + info;
209 return expand(prk, labeled_info, size);
210 }
211
212 template<>
213 const AEAD&
214 AEAD::get<AEAD::ID::AES_128_GCM>()
215 {
216 return AEADCipher::get<AEAD::ID::AES_128_GCM>();
217 }
218
219 template<>
220 const AEAD&
221 AEAD::get<AEAD::ID::AES_256_GCM>()
222 {
223 return AEADCipher::get<AEAD::ID::AES_256_GCM>();
224 }
225
226 template<>
227 const AEAD&
228 AEAD::get<AEAD::ID::CHACHA20_POLY1305>()
229 {
230 return AEADCipher::get<AEAD::ID::CHACHA20_POLY1305>();
231 }
232
233 template<>
234 const AEAD&
235 AEAD::get<AEAD::ID::export_only>()
236 {
237 static const auto export_only = ExportOnlyCipher{};
238 return export_only;
239 }
240
241 AEAD::AEAD(ID id_in, size_t key_size_in, size_t nonce_size_in)
242 : id(id_in)
243 , key_size(key_size_in)
244 , nonce_size(nonce_size_in)
245 {
246 }
247
248 ///
249 /// Encryption Contexts
250 ///
251
252 bytes
253 Context::do_export(const bytes& exporter_context, size_t size) const
254 {
255 return kdf.labeled_expand(
256 suite, exporter_secret, label_sec(), exporter_context, size);
257 }
258
259 bytes
260 Context::current_nonce() const
261 {
262 auto curr = i2osp(seq, aead.nonce_size);
263 return curr ^ nonce;
264 }
265
266 void
267 Context::increment_seq()
268 {
269 if (seq == std::numeric_limits<uint64_t>::max()) {
270 throw std::runtime_error("Sequence number overflow");
271 }
272
273 seq += 1;
274 }
275
276 Context::Context(bytes suite_in,
277 bytes key_in,
278 bytes nonce_in,
279 bytes exporter_secret_in,
280 const KDF& kdf_in,
281 const AEAD& aead_in)
282 : suite(std::move(suite_in))
283 , key(std::move(key_in))
284 , nonce(std::move(nonce_in))
285 , exporter_secret(std::move(exporter_secret_in))
286 , kdf(kdf_in)
287 , aead(aead_in)
288 , seq(0)
289 {
290 }
291
292 bool
293 operator==(const Context& lhs, const Context& rhs)
294 {
295 // TODO(RLB) Compare KDF and AEAD algorithms
296 auto suite = (lhs.suite == rhs.suite);
297 auto key = (lhs.key == rhs.key);
298 auto nonce = (lhs.nonce == rhs.nonce);
299 auto exporter_secret = (lhs.exporter_secret == rhs.exporter_secret);
300 auto seq = (lhs.seq == rhs.seq);
301 return suite && key && nonce && exporter_secret && seq;
302 }
303
304 SenderContext::SenderContext(Context&& c)
305 : Context(std::move(c))
306 {
307 }
308
309 bytes
310 SenderContext::seal(const bytes& aad, const bytes& pt)
311 {
312 auto ct = aead.seal(key, current_nonce(), aad, pt);
313 increment_seq();
314 return ct;
315 }
316
317 ReceiverContext::ReceiverContext(Context&& c)
318 : Context(std::move(c))
319 {
320 }
321
322 std::optional<bytes>
323 ReceiverContext::open(const bytes& aad, const bytes& ct)
324 {
325 auto maybe_pt = aead.open(key, current_nonce(), aad, ct);
326 increment_seq();
327 return maybe_pt;
328 }
329
330 ///
331 /// HPKE
332 ///
333
334 static const bytes default_psk = {};
335 static const bytes default_psk_id = {};
336
337 static bytes
338 suite_id(KEM::ID kem_id, KDF::ID kdf_id, AEAD::ID aead_id)
339 {
340 return label_hpke() + i2osp(static_cast<uint64_t>(kem_id), 2) +
341 i2osp(static_cast<uint64_t>(kdf_id), 2) +
342 i2osp(static_cast<uint64_t>(aead_id), 2);
343 }
344
345 static const KEM&
346 select_kem(KEM::ID id)
347 {
348 switch (id) {
349 case KEM::ID::DHKEM_P256_SHA256:
350 return KEM::get<KEM::ID::DHKEM_P256_SHA256>();
351 case KEM::ID::DHKEM_P384_SHA384:
352 return KEM::get<KEM::ID::DHKEM_P384_SHA384>();
353 case KEM::ID::DHKEM_P521_SHA512:
354 return KEM::get<KEM::ID::DHKEM_P521_SHA512>();
355 case KEM::ID::DHKEM_X25519_SHA256:
356 return KEM::get<KEM::ID::DHKEM_X25519_SHA256>();
357 #if !defined(WITH_BORINGSSL)
358 case KEM::ID::DHKEM_X448_SHA512:
359 return KEM::get<KEM::ID::DHKEM_X448_SHA512>();
360 #endif
361 default:
362 throw std::runtime_error("Unsupported algorithm");
363 }
364 }
365
366 static const KDF&
367 select_kdf(KDF::ID id)
368 {
369 switch (id) {
370 case KDF::ID::HKDF_SHA256:
371 return KDF::get<KDF::ID::HKDF_SHA256>();
372 case KDF::ID::HKDF_SHA384:
373 return KDF::get<KDF::ID::HKDF_SHA384>();
374 case KDF::ID::HKDF_SHA512:
375 return KDF::get<KDF::ID::HKDF_SHA512>();
376 default:
377 throw std::runtime_error("Unsupported algorithm");
378 }
379 }
380
381 static const AEAD&
382 select_aead(AEAD::ID id)
383 {
384 switch (id) {
385 case AEAD::ID::AES_128_GCM:
386 return AEAD::get<AEAD::ID::AES_128_GCM>();
387 case AEAD::ID::AES_256_GCM:
388 return AEAD::get<AEAD::ID::AES_256_GCM>();
389 case AEAD::ID::CHACHA20_POLY1305:
390 return AEAD::get<AEAD::ID::CHACHA20_POLY1305>();
391 case AEAD::ID::export_only:
392 return AEAD::get<AEAD::ID::export_only>();
393 default:
394 throw std::runtime_error("Unsupported algorithm");
395 }
396 }
397
398 HPKE::HPKE(KEM::ID kem_id, KDF::ID kdf_id, AEAD::ID aead_id)
399 : suite(suite_id(kem_id, kdf_id, aead_id))
400 , kem(select_kem(kem_id))
401 , kdf(select_kdf(kdf_id))
402 , aead(select_aead(aead_id))
403 {
404 }
405
406 HPKE::SenderInfo
407 HPKE::setup_base_s(const KEM::PublicKey& pkR, const bytes& info) const
408 {
409 auto [shared_secret, enc] = kem.encap(pkR);
410 auto ctx =
411 key_schedule(Mode::base, shared_secret, info, default_psk, default_psk_id);
412 return std::make_pair(enc, SenderContext(std::move(ctx)));
413 }
414
415 ReceiverContext
416 HPKE::setup_base_r(const bytes& enc,
417 const KEM::PrivateKey& skR,
418 const bytes& info) const
419 {
420 auto pkRm = kem.serialize(*skR.public_key());
421 auto shared_secret = kem.decap(enc, skR);
422 auto ctx =
423 key_schedule(Mode::base, shared_secret, info, default_psk, default_psk_id);
424 return { std::move(ctx) };
425 }
426
427 HPKE::SenderInfo
428 HPKE::setup_psk_s(const KEM::PublicKey& pkR,
429 const bytes& info,
430 const bytes& psk,
431 const bytes& psk_id) const
432 {
433 auto [shared_secret, enc] = kem.encap(pkR);
434 auto ctx = key_schedule(Mode::psk, shared_secret, info, psk, psk_id);
435 return std::make_pair(enc, SenderContext(std::move(ctx)));
436 }
437
438 ReceiverContext
439 HPKE::setup_psk_r(const bytes& enc,
440 const KEM::PrivateKey& skR,
441 const bytes& info,
442 const bytes& psk,
443 const bytes& psk_id) const
444 {
445 auto shared_secret = kem.decap(enc, skR);
446 auto ctx = key_schedule(Mode::psk, shared_secret, info, psk, psk_id);
447 return { std::move(ctx) };
448 }
449
450 HPKE::SenderInfo
451 HPKE::setup_auth_s(const KEM::PublicKey& pkR,
452 const bytes& info,
453 const KEM::PrivateKey& skS) const
454 {
455 auto [shared_secret, enc] = kem.auth_encap(pkR, skS);
456 auto ctx =
457 key_schedule(Mode::auth, shared_secret, info, default_psk, default_psk_id);
458 return std::make_pair(enc, SenderContext(std::move(ctx)));
459 }
460
461 ReceiverContext
462 HPKE::setup_auth_r(const bytes& enc,
463 const KEM::PrivateKey& skR,
464 const bytes& info,
465 const KEM::PublicKey& pkS) const
466 {
467 auto shared_secret = kem.auth_decap(enc, pkS, skR);
468 auto ctx =
469 key_schedule(Mode::auth, shared_secret, info, default_psk, default_psk_id);
470 return { std::move(ctx) };
471 }
472
473 HPKE::SenderInfo
474 HPKE::setup_auth_psk_s(const KEM::PublicKey& pkR,
475 const bytes& info,
476 const bytes& psk,
477 const bytes& psk_id,
478 const KEM::PrivateKey& skS) const
479 {
480 auto [shared_secret, enc] = kem.auth_encap(pkR, skS);
481 auto ctx = key_schedule(Mode::auth_psk, shared_secret, info, psk, psk_id);
482 return std::make_pair(enc, SenderContext(std::move(ctx)));
483 }
484
485 ReceiverContext
486 HPKE::setup_auth_psk_r(const bytes& enc,
487 const KEM::PrivateKey& skR,
488 const bytes& info,
489 const bytes& psk,
490 const bytes& psk_id,
491 const KEM::PublicKey& pkS) const
492 {
493 auto shared_secret = kem.auth_decap(enc, pkS, skR);
494 auto ctx = key_schedule(Mode::auth_psk, shared_secret, info, psk, psk_id);
495 return { std::move(ctx) };
496 }
497
498 bool
499 HPKE::verify_psk_inputs(Mode mode, const bytes& psk, const bytes& psk_id)
500 {
501 auto got_psk = (psk != default_psk);
502 auto got_psk_id = (psk_id != default_psk_id);
503 if (got_psk != got_psk_id) {
504 return false;
505 }
506
507 return (!got_psk && (mode == Mode::base || mode == Mode::auth)) ||
508 (got_psk && (mode == Mode::psk || mode == Mode::auth_psk));
509 }
510
511 Context
512 HPKE::key_schedule(Mode mode,
513 const bytes& shared_secret,
514 const bytes& info,
515 const bytes& psk,
516 const bytes& psk_id) const
517 {
518 if (!verify_psk_inputs(mode, psk, psk_id)) {
519 throw std::runtime_error("Invalid PSK inputs");
520 }
521
522 auto psk_id_hash =
523 kdf.labeled_extract(suite, {}, label_psk_id_hash(), psk_id);
524 auto info_hash = kdf.labeled_extract(suite, {}, label_info_hash(), info);
525 auto mode_bytes = bytes{ uint8_t(mode) };
526 auto key_schedule_context = mode_bytes + psk_id_hash + info_hash;
527
528 auto secret = kdf.labeled_extract(suite, shared_secret, label_secret(), psk);
529
530 auto key = kdf.labeled_expand(
531 suite, secret, label_key(), key_schedule_context, aead.key_size);
532 auto nonce = kdf.labeled_expand(
533 suite, secret, label_base_nonce(), key_schedule_context, aead.nonce_size);
534 auto exporter_secret = kdf.labeled_expand(
535 suite, secret, label_exp(), key_schedule_context, kdf.hash_size);
536
537 return { suite, key, nonce, exporter_secret, kdf, aead };
538 }
539
540 } // namespace mlspp::hpke