| 1 | #pragma once |
| 2 | |
| 3 | #include <hpke/hpke.h> |
| 4 | |
| 5 | #include "group.h" |
| 6 | |
| 7 | namespace mlspp::hpke { |
| 8 | |
| 9 | struct DHKEM : public KEM |
| 10 | { |
| 11 | struct PrivateKey : public KEM::PrivateKey |
| 12 | { |
| 13 | PrivateKey(Group::PrivateKey* group_priv_in); |
| 14 | std::unique_ptr<KEM::PublicKey> public_key() const override; |
| 15 | |
| 16 | std::unique_ptr<Group::PrivateKey> group_priv; |
| 17 | }; |
| 18 | |
| 19 | template<KEM::ID> |
| 20 | static const DHKEM& get(); |
| 21 | |
| 22 | ~DHKEM() override = default; |
| 23 | |
| 24 | std::unique_ptr<KEM::PrivateKey> generate_key_pair() const override; |
| 25 | std::unique_ptr<KEM::PrivateKey> derive_key_pair( |
| 26 | const bytes& ikm) const override; |
| 27 | |
| 28 | bytes serialize(const KEM::PublicKey& pk) const override; |
| 29 | std::unique_ptr<KEM::PublicKey> deserialize(const bytes& enc) const override; |
| 30 | |
| 31 | bytes serialize_private(const KEM::PrivateKey& sk) const override; |
| 32 | std::unique_ptr<KEM::PrivateKey> deserialize_private( |
| 33 | const bytes& skm) const override; |
| 34 | |
| 35 | std::pair<bytes, bytes> encap(const KEM::PublicKey& pk) const override; |
| 36 | bytes decap(const bytes& enc, const KEM::PrivateKey& sk) const override; |
| 37 | |
| 38 | std::pair<bytes, bytes> auth_encap(const KEM::PublicKey& pkR, |
| 39 | const KEM::PrivateKey& skS) const override; |
| 40 | bytes auth_decap(const bytes& enc, |
| 41 | const KEM::PublicKey& pkS, |
| 42 | const KEM::PrivateKey& skR) const override; |
| 43 | |
| 44 | private: |
| 45 | const Group& group; |
| 46 | const KDF& kdf; |
| 47 | bytes suite_id; |
| 48 | |
| 49 | bytes extract_and_expand(const bytes& dh, const bytes& kem_context) const; |
| 50 | |
| 51 | DHKEM(KEM::ID kem_id_in, const Group& group_in, const KDF& kdf_in); |
| 52 | friend DHKEM make_dhkem(KEM::ID kem_id_in, |
| 53 | const Group& group_in, |
| 54 | const KDF& kdf_in); |
| 55 | }; |
| 56 | |
| 57 | } // namespace mlspp::hpke |