| 1 | #pragma once |
| 2 | |
| 3 | #include <map> |
| 4 | #include <mls/common.h> |
| 5 | #include <mls/crypto.h> |
| 6 | #include <mls/messages.h> |
| 7 | #include <mls/tree_math.h> |
| 8 | |
| 9 | namespace mlspp { |
| 10 | |
| 11 | struct HashRatchet |
| 12 | { |
| 13 | CipherSuite suite; |
| 14 | bytes next_secret; |
| 15 | uint32_t next_generation; |
| 16 | std::map<uint32_t, KeyAndNonce> cache; |
| 17 | |
| 18 | size_t key_size; |
| 19 | size_t nonce_size; |
| 20 | size_t secret_size; |
| 21 | |
| 22 | // These defaults are necessary for use with containers |
| 23 | HashRatchet() = default; |
| 24 | HashRatchet(const HashRatchet& other) = default; |
| 25 | HashRatchet(HashRatchet&& other) = default; |
| 26 | HashRatchet& operator=(const HashRatchet& other) = default; |
| 27 | HashRatchet& operator=(HashRatchet&& other) = default; |
| 28 | |
| 29 | HashRatchet(CipherSuite suite_in, bytes base_secret_in); |
| 30 | |
| 31 | std::tuple<uint32_t, KeyAndNonce> next(); |
| 32 | KeyAndNonce get(uint32_t generation); |
| 33 | void erase(uint32_t generation); |
| 34 | }; |
| 35 | |
| 36 | struct SecretTree |
| 37 | { |
| 38 | SecretTree() = default; |
| 39 | SecretTree(CipherSuite suite_in, |
| 40 | LeafCount group_size_in, |
| 41 | bytes encryption_secret_in); |
| 42 | |
| 43 | bool has_leaf(LeafIndex sender) { return sender < group_size; } |
| 44 | |
| 45 | bytes get(LeafIndex sender); |
| 46 | |
| 47 | private: |
| 48 | CipherSuite suite; |
| 49 | LeafCount group_size; |
| 50 | NodeIndex root; |
| 51 | std::map<NodeIndex, bytes> secrets; |
| 52 | size_t secret_size; |
| 53 | }; |
| 54 | |
| 55 | using ReuseGuard = std::array<uint8_t, 4>; |
| 56 | |
| 57 | struct GroupKeySource |
| 58 | { |
| 59 | enum struct RatchetType |
| 60 | { |
| 61 | handshake, |
| 62 | application, |
| 63 | }; |
| 64 | |
| 65 | GroupKeySource() = default; |
| 66 | GroupKeySource(CipherSuite suite_in, |
| 67 | LeafCount group_size, |
| 68 | bytes encryption_secret); |
| 69 | |
| 70 | bool has_leaf(LeafIndex sender) { return secret_tree.has_leaf(sender); } |
| 71 | |
| 72 | std::tuple<uint32_t, ReuseGuard, KeyAndNonce> next(ContentType content_type, |
| 73 | LeafIndex sender); |
| 74 | KeyAndNonce get(ContentType content_type, |
| 75 | LeafIndex sender, |
| 76 | uint32_t generation, |
| 77 | ReuseGuard reuse_guard); |
| 78 | void erase(ContentType type, LeafIndex sender, uint32_t generation); |
| 79 | |
| 80 | private: |
| 81 | CipherSuite suite; |
| 82 | SecretTree secret_tree; |
| 83 | |
| 84 | using Key = std::tuple<RatchetType, LeafIndex>; |
| 85 | std::map<Key, HashRatchet> chains; |
| 86 | |
| 87 | HashRatchet& chain(RatchetType type, LeafIndex sender); |
| 88 | HashRatchet& chain(ContentType type, LeafIndex sender); |
| 89 | |
| 90 | static const std::array<RatchetType, 2> all_ratchet_types; |
| 91 | }; |
| 92 | |
| 93 | struct KeyScheduleEpoch |
| 94 | { |
| 95 | private: |
| 96 | CipherSuite suite; |
| 97 | |
| 98 | public: |
| 99 | bytes joiner_secret; |
| 100 | bytes epoch_secret; |
| 101 | |
| 102 | bytes sender_data_secret; |
| 103 | bytes encryption_secret; |
| 104 | bytes exporter_secret; |
| 105 | bytes epoch_authenticator; |
| 106 | bytes external_secret; |
| 107 | bytes confirmation_key; |
| 108 | bytes membership_key; |
| 109 | bytes resumption_psk; |
| 110 | bytes init_secret; |
| 111 | |
| 112 | HPKEPrivateKey external_priv; |
| 113 | |
| 114 | KeyScheduleEpoch() = default; |
| 115 | |
| 116 | // Full initializer, used by invited joiner |
| 117 | static KeyScheduleEpoch joiner(CipherSuite suite_in, |
| 118 | const bytes& joiner_secret, |
| 119 | const std::vector<PSKWithSecret>& psks, |
| 120 | const bytes& context); |
| 121 | |
| 122 | // Ciphersuite-only initializer, used by external joiner |
| 123 | KeyScheduleEpoch(CipherSuite suite_in); |
| 124 | |
| 125 | // Initial epoch |
| 126 | KeyScheduleEpoch(CipherSuite suite_in, |
| 127 | const bytes& init_secret, |
| 128 | const bytes& context); |
| 129 | |
| 130 | static std::tuple<bytes, bytes> external_init( |
| 131 | CipherSuite suite, |
| 132 | const HPKEPublicKey& external_pub); |
| 133 | bytes receive_external_init(const bytes& kem_output) const; |
| 134 | |
| 135 | KeyScheduleEpoch next(const bytes& commit_secret, |
| 136 | const std::vector<PSKWithSecret>& psks, |
| 137 | const std::optional<bytes>& force_init_secret, |
| 138 | const bytes& context) const; |
| 139 | |
| 140 | GroupKeySource encryption_keys(LeafCount size) const; |
| 141 | bytes confirmation_tag(const bytes& confirmed_transcript_hash) const; |
| 142 | bytes do_export(const std::string& label, |
| 143 | const bytes& context, |
| 144 | size_t size) const; |
| 145 | PSKWithSecret resumption_psk_w_secret(ResumptionPSKUsage usage, |
| 146 | const bytes& group_id, |
| 147 | epoch_t epoch); |
| 148 | |
| 149 | static bytes make_psk_secret(CipherSuite suite, |
| 150 | const std::vector<PSKWithSecret>& psks); |
| 151 | static bytes welcome_secret(CipherSuite suite, |
| 152 | const bytes& joiner_secret, |
| 153 | const std::vector<PSKWithSecret>& psks); |
| 154 | static KeyAndNonce sender_data_keys(CipherSuite suite, |
| 155 | const bytes& sender_data_secret, |
| 156 | const bytes& ciphertext); |
| 157 | |
| 158 | // TODO(RLB) make these methods private, but accessible to test vectors |
| 159 | KeyScheduleEpoch(CipherSuite suite_in, |
| 160 | const bytes& init_secret, |
| 161 | const bytes& commit_secret, |
| 162 | const bytes& psk_secret, |
| 163 | const bytes& context); |
| 164 | KeyScheduleEpoch next_raw(const bytes& commit_secret, |
| 165 | const bytes& psk_secret, |
| 166 | const std::optional<bytes>& force_init_secret, |
| 167 | const bytes& context) const; |
| 168 | static bytes welcome_secret_raw(CipherSuite suite, |
| 169 | const bytes& joiner_secret, |
| 170 | const bytes& psk_secret); |
| 171 | |
| 172 | private: |
| 173 | KeyScheduleEpoch(CipherSuite suite_in, |
| 174 | const bytes& joiner_secret, |
| 175 | const bytes& psk_secret, |
| 176 | const bytes& context); |
| 177 | }; |
| 178 | |
| 179 | bool |
| 180 | operator==(const KeyScheduleEpoch& lhs, const KeyScheduleEpoch& rhs); |
| 181 | |
| 182 | struct TranscriptHash |
| 183 | { |
| 184 | CipherSuite suite; |
| 185 | bytes confirmed; |
| 186 | bytes interim; |
| 187 | |
| 188 | // For a new group |
| 189 | TranscriptHash(CipherSuite suite_in); |
| 190 | |
| 191 | // For joining a group |
| 192 | TranscriptHash(CipherSuite suite_in, |
| 193 | bytes confirmed_in, |
| 194 | const bytes& confirmation_tag); |
| 195 | |
| 196 | void update(const AuthenticatedContent& content_auth); |
| 197 | void update_confirmed(const AuthenticatedContent& content_auth); |
| 198 | void update_interim(const bytes& confirmation_tag); |
| 199 | void update_interim(const AuthenticatedContent& content_auth); |
| 200 | }; |
| 201 | |
| 202 | bool |
| 203 | operator==(const TranscriptHash& lhs, const TranscriptHash& rhs); |
| 204 | |
| 205 | } // namespace mlspp |