| 1 | #include <mls/key_schedule.h> |
| 2 | |
| 3 | namespace mlspp { |
| 4 | |
| 5 | /// |
| 6 | /// Key Derivation Functions |
| 7 | /// |
| 8 | |
| 9 | struct TreeContext |
| 10 | { |
| 11 | NodeIndex node; |
| 12 | uint32_t generation = 0; |
| 13 | |
| 14 | TLS_SERIALIZABLE(node, generation) |
| 15 | }; |
| 16 | |
| 17 | /// |
| 18 | /// HashRatchet |
| 19 | /// |
| 20 | |
| 21 | HashRatchet::HashRatchet(CipherSuite suite_in, bytes base_secret_in) |
| 22 | : suite(suite_in) |
| 23 | , next_secret(std::move(base_secret_in)) |
| 24 | , next_generation(0) |
| 25 | , key_size(suite.hpke().aead.key_size) |
| 26 | , nonce_size(suite.hpke().aead.nonce_size) |
| 27 | , secret_size(suite.secret_size()) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | std::tuple<uint32_t, KeyAndNonce> |
| 32 | HashRatchet::next() |
| 33 | { |
| 34 | auto generation = next_generation; |
| 35 | auto key = suite.derive_tree_secret(next_secret, "key", generation, key_size); |
| 36 | auto nonce = |
| 37 | suite.derive_tree_secret(next_secret, "nonce", generation, nonce_size); |
| 38 | auto secret = |
| 39 | suite.derive_tree_secret(next_secret, "secret", generation, secret_size); |
| 40 | |
| 41 | next_generation += 1; |
| 42 | next_secret = secret; |
| 43 | |
| 44 | cache[generation] = { key, nonce }; |
| 45 | return { generation, cache.at(generation) }; |
| 46 | } |
| 47 | |
| 48 | // Note: This construction deliberately does not preserve the forward-secrecy |
| 49 | // invariant, in that keys/nonces are not deleted after they are used. |
| 50 | // Otherwise, it would not be possible for a node to send to itself. Keys can |
| 51 | // be deleted once they are not needed by calling HashRatchet::erase(). |
| 52 | KeyAndNonce |
| 53 | HashRatchet::get(uint32_t generation) |
| 54 | { |
| 55 | if (cache.count(generation) > 0) { |
| 56 | auto out = cache.at(generation); |
| 57 | return out; |
| 58 | } |
| 59 | |
| 60 | if (next_generation > generation) { |
| 61 | throw ProtocolError("Request for expired key"); |
| 62 | } |
| 63 | |
| 64 | while (next_generation <= generation) { |
| 65 | next(); |
| 66 | } |
| 67 | |
| 68 | return cache.at(generation); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | HashRatchet::erase(uint32_t generation) |
| 73 | { |
| 74 | if (cache.count(generation) == 0) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | cache.erase(generation); |
| 79 | } |
| 80 | |
| 81 | /// |
| 82 | /// SecretTree |
| 83 | /// |
| 84 | |
| 85 | SecretTree::SecretTree(CipherSuite suite_in, |
| 86 | LeafCount group_size_in, |
| 87 | bytes encryption_secret_in) |
| 88 | : suite(suite_in) |
| 89 | , group_size(LeafCount::full(group_size_in)) |
| 90 | , root(NodeIndex::root(group_size)) |
| 91 | , secret_size(suite_in.secret_size()) |
| 92 | { |
| 93 | secrets.emplace(root, std::move(encryption_secret_in)); |
| 94 | } |
| 95 | |
| 96 | bytes |
| 97 | SecretTree::get(LeafIndex sender) |
| 98 | { |
| 99 | static const auto context_left = from_ascii("left"); |
| 100 | static const auto context_right = from_ascii("right"); |
| 101 | auto node = NodeIndex(sender); |
| 102 | |
| 103 | // Find an ancestor that is populated |
| 104 | auto dirpath = node.dirpath(group_size); |
| 105 | dirpath.insert(dirpath.begin(), node); |
| 106 | dirpath.push_back(root); |
| 107 | uint32_t curr = 0; |
| 108 | for (; curr < dirpath.size(); ++curr) { |
| 109 | auto i = dirpath.at(curr); |
| 110 | if (secrets.count(i) > 0) { |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (curr > dirpath.size()) { |
| 116 | throw InvalidParameterError("No secret found to derive base key"); |
| 117 | } |
| 118 | |
| 119 | // Derive down |
| 120 | for (; curr > 0; --curr) { |
| 121 | auto curr_node = dirpath.at(curr); |
| 122 | auto left = curr_node.left(); |
| 123 | auto right = curr_node.right(); |
| 124 | |
| 125 | auto& secret = secrets.at(curr_node); |
| 126 | |
| 127 | const auto left_secret = |
| 128 | suite.expand_with_label(secret, "tree", context_left, secret_size); |
| 129 | const auto right_secret = |
| 130 | suite.expand_with_label(secret, "tree", context_right, secret_size); |
| 131 | |
| 132 | secrets.insert_or_assign(left, left_secret); |
| 133 | secrets.insert_or_assign(right, right_secret); |
| 134 | } |
| 135 | |
| 136 | // Copy the leaf |
| 137 | auto out = secrets.at(node); |
| 138 | |
| 139 | // Zeroize along the direct path |
| 140 | for (auto i : dirpath) { |
| 141 | secrets.erase(i); |
| 142 | } |
| 143 | |
| 144 | return out; |
| 145 | } |
| 146 | |
| 147 | /// |
| 148 | /// ReuseGuard |
| 149 | /// |
| 150 | |
| 151 | static ReuseGuard |
| 152 | new_reuse_guard() |
| 153 | { |
| 154 | auto random = random_bytes(4); |
| 155 | auto guard = ReuseGuard(); |
| 156 | std::copy(random.begin(), random.end(), guard.begin()); |
| 157 | return guard; |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | apply_reuse_guard(const ReuseGuard& guard, bytes& nonce) |
| 162 | { |
| 163 | for (size_t i = 0; i < guard.size(); i++) { |
| 164 | nonce.at(i) ^= guard.at(i); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// |
| 169 | /// GroupKeySource |
| 170 | /// |
| 171 | |
| 172 | GroupKeySource::GroupKeySource(CipherSuite suite_in, |
| 173 | LeafCount group_size, |
| 174 | bytes encryption_secret) |
| 175 | : suite(suite_in) |
| 176 | , secret_tree(suite, group_size, std::move(encryption_secret)) |
| 177 | { |
| 178 | } |
| 179 | |
| 180 | HashRatchet& |
| 181 | GroupKeySource::chain(ContentType type, LeafIndex sender) |
| 182 | { |
| 183 | switch (type) { |
| 184 | case ContentType::proposal: |
| 185 | case ContentType::commit: |
| 186 | return chain(RatchetType::handshake, sender); |
| 187 | |
| 188 | case ContentType::application: |
| 189 | return chain(RatchetType::application, sender); |
| 190 | |
| 191 | default: |
| 192 | throw InvalidParameterError("Invalid content type"); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | HashRatchet& |
| 197 | GroupKeySource::chain(RatchetType type, LeafIndex sender) |
| 198 | { |
| 199 | auto key = Key{ type, sender }; |
| 200 | if (chains.count(key) > 0) { |
| 201 | return chains[key]; |
| 202 | } |
| 203 | |
| 204 | auto secret_size = suite.secret_size(); |
| 205 | auto leaf_secret = secret_tree.get(sender); |
| 206 | |
| 207 | auto handshake_secret = |
| 208 | suite.expand_with_label(leaf_secret, "handshake", {}, secret_size); |
| 209 | auto application_secret = |
| 210 | suite.expand_with_label(leaf_secret, "application", {}, secret_size); |
| 211 | |
| 212 | chains.emplace(Key{ RatchetType::handshake, sender }, |
| 213 | HashRatchet{ suite, handshake_secret }); |
| 214 | chains.emplace(Key{ RatchetType::application, sender }, |
| 215 | HashRatchet{ suite, application_secret }); |
| 216 | |
| 217 | return chains[key]; |
| 218 | } |
| 219 | |
| 220 | std::tuple<uint32_t, ReuseGuard, KeyAndNonce> |
| 221 | GroupKeySource::next(ContentType type, LeafIndex sender) |
| 222 | { |
| 223 | auto [generation, keys] = chain(type, sender).next(); |
| 224 | |
| 225 | auto reuse_guard = new_reuse_guard(); |
| 226 | apply_reuse_guard(reuse_guard, keys.nonce); |
| 227 | |
| 228 | return { generation, reuse_guard, keys }; |
| 229 | } |
| 230 | |
| 231 | KeyAndNonce |
| 232 | GroupKeySource::get(ContentType type, |
| 233 | LeafIndex sender, |
| 234 | uint32_t generation, |
| 235 | ReuseGuard reuse_guard) |
| 236 | { |
| 237 | auto keys = chain(type, sender).get(generation); |
| 238 | apply_reuse_guard(reuse_guard, keys.nonce); |
| 239 | return keys; |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | GroupKeySource::erase(ContentType type, LeafIndex sender, uint32_t generation) |
| 244 | { |
| 245 | return chain(type, sender).erase(generation); |
| 246 | } |
| 247 | |
| 248 | // struct { |
| 249 | // opaque group_id<0..255>; |
| 250 | // uint64 epoch; |
| 251 | // ContentType content_type; |
| 252 | // opaque authenticated_data<0..2^32-1>; |
| 253 | // } ContentAAD; |
| 254 | struct ContentAAD |
| 255 | { |
| 256 | const bytes& group_id; |
| 257 | const epoch_t epoch; |
| 258 | const ContentType content_type; |
| 259 | const bytes& authenticated_data; |
| 260 | |
| 261 | TLS_SERIALIZABLE(group_id, epoch, content_type, authenticated_data) |
| 262 | }; |
| 263 | |
| 264 | /// |
| 265 | /// KeyScheduleEpoch |
| 266 | /// |
| 267 | |
| 268 | struct PSKLabel |
| 269 | { |
| 270 | const PreSharedKeyID& id; |
| 271 | uint16_t index; |
| 272 | uint16_t count; |
| 273 | |
| 274 | TLS_SERIALIZABLE(id, index, count); |
| 275 | }; |
| 276 | |
| 277 | static bytes |
| 278 | make_joiner_secret(CipherSuite suite, |
| 279 | const bytes& context, |
| 280 | const bytes& init_secret, |
| 281 | const bytes& commit_secret) |
| 282 | { |
| 283 | auto pre_joiner_secret = suite.hpke().kdf.extract(init_secret, commit_secret); |
| 284 | return suite.expand_with_label( |
| 285 | pre_joiner_secret, "joiner", context, suite.secret_size()); |
| 286 | } |
| 287 | |
| 288 | static bytes |
| 289 | make_epoch_secret(CipherSuite suite, |
| 290 | const bytes& joiner_secret, |
| 291 | const bytes& psk_secret, |
| 292 | const bytes& context) |
| 293 | { |
| 294 | auto member_secret = suite.hpke().kdf.extract(joiner_secret, psk_secret); |
| 295 | return suite.expand_with_label( |
| 296 | member_secret, "epoch", context, suite.secret_size()); |
| 297 | } |
| 298 | |
| 299 | KeyScheduleEpoch |
| 300 | KeyScheduleEpoch::joiner(CipherSuite suite_in, |
| 301 | const bytes& joiner_secret, |
| 302 | const std::vector<PSKWithSecret>& psks, |
| 303 | const bytes& context) |
| 304 | { |
| 305 | return { suite_in, joiner_secret, make_psk_secret(suite_in, psks), context }; |
| 306 | } |
| 307 | |
| 308 | KeyScheduleEpoch::KeyScheduleEpoch(CipherSuite suite_in, |
| 309 | const bytes& joiner_secret, |
| 310 | const bytes& psk_secret, |
| 311 | const bytes& context) |
| 312 | : suite(suite_in) |
| 313 | , joiner_secret(joiner_secret) |
| 314 | , epoch_secret( |
| 315 | make_epoch_secret(suite_in, joiner_secret, psk_secret, context)) |
| 316 | , sender_data_secret(suite.derive_secret(epoch_secret, "sender data")) |
| 317 | , encryption_secret(suite.derive_secret(epoch_secret, "encryption")) |
| 318 | , exporter_secret(suite.derive_secret(epoch_secret, "exporter")) |
| 319 | , epoch_authenticator(suite.derive_secret(epoch_secret, "authentication")) |
| 320 | , external_secret(suite.derive_secret(epoch_secret, "external")) |
| 321 | , confirmation_key(suite.derive_secret(epoch_secret, "confirm")) |
| 322 | , membership_key(suite.derive_secret(epoch_secret, "membership")) |
| 323 | , resumption_psk(suite.derive_secret(epoch_secret, "resumption")) |
| 324 | , init_secret(suite.derive_secret(epoch_secret, "init")) |
| 325 | , external_priv(HPKEPrivateKey::derive(suite, external_secret)) |
| 326 | { |
| 327 | } |
| 328 | |
| 329 | KeyScheduleEpoch::KeyScheduleEpoch(CipherSuite suite_in) |
| 330 | : suite(suite_in) |
| 331 | { |
| 332 | } |
| 333 | |
| 334 | KeyScheduleEpoch::KeyScheduleEpoch(CipherSuite suite_in, |
| 335 | const bytes& init_secret, |
| 336 | const bytes& context) |
| 337 | : KeyScheduleEpoch( |
| 338 | suite_in, |
| 339 | make_joiner_secret(suite_in, context, init_secret, suite_in.zero()), |
| 340 | { /* no PSKs */ }, |
| 341 | context) |
| 342 | { |
| 343 | } |
| 344 | |
| 345 | KeyScheduleEpoch::KeyScheduleEpoch(CipherSuite suite_in, |
| 346 | const bytes& init_secret, |
| 347 | const bytes& commit_secret, |
| 348 | const bytes& psk_secret, |
| 349 | const bytes& context) |
| 350 | : KeyScheduleEpoch( |
| 351 | suite_in, |
| 352 | make_joiner_secret(suite_in, context, init_secret, commit_secret), |
| 353 | psk_secret, |
| 354 | context) |
| 355 | { |
| 356 | } |
| 357 | |
| 358 | std::tuple<bytes, bytes> |
| 359 | KeyScheduleEpoch::external_init(CipherSuite suite, |
| 360 | const HPKEPublicKey& external_pub) |
| 361 | { |
| 362 | auto size = suite.secret_size(); |
| 363 | return external_pub.do_export( |
| 364 | suite, {}, "MLS 1.0 external init secret", size); |
| 365 | } |
| 366 | |
| 367 | bytes |
| 368 | KeyScheduleEpoch::receive_external_init(const bytes& kem_output) const |
| 369 | { |
| 370 | auto size = suite.secret_size(); |
| 371 | return external_priv.do_export( |
| 372 | suite, {}, kem_output, "MLS 1.0 external init secret", size); |
| 373 | } |
| 374 | |
| 375 | KeyScheduleEpoch |
| 376 | KeyScheduleEpoch::next(const bytes& commit_secret, |
| 377 | const std::vector<PSKWithSecret>& psks, |
| 378 | const std::optional<bytes>& force_init_secret, |
| 379 | const bytes& context) const |
| 380 | { |
| 381 | return next_raw( |
| 382 | commit_secret, make_psk_secret(suite, psks), force_init_secret, context); |
| 383 | } |
| 384 | |
| 385 | KeyScheduleEpoch |
| 386 | KeyScheduleEpoch::next_raw(const bytes& commit_secret, |
| 387 | const bytes& psk_secret, |
| 388 | const std::optional<bytes>& force_init_secret, |
| 389 | const bytes& context) const |
| 390 | { |
| 391 | auto actual_init_secret = init_secret; |
| 392 | if (force_init_secret) { |
| 393 | actual_init_secret = opt::get(force_init_secret); |
| 394 | } |
| 395 | |
| 396 | return { suite, actual_init_secret, commit_secret, psk_secret, context }; |
| 397 | } |
| 398 | |
| 399 | GroupKeySource |
| 400 | KeyScheduleEpoch::encryption_keys(LeafCount size) const |
| 401 | { |
| 402 | return { suite, size, encryption_secret }; |
| 403 | } |
| 404 | |
| 405 | bytes |
| 406 | KeyScheduleEpoch::confirmation_tag(const bytes& confirmed_transcript_hash) const |
| 407 | { |
| 408 | return suite.digest().hmac(confirmation_key, confirmed_transcript_hash); |
| 409 | } |
| 410 | |
| 411 | bytes |
| 412 | KeyScheduleEpoch::do_export(const std::string& label, |
| 413 | const bytes& context, |
| 414 | size_t size) const |
| 415 | { |
| 416 | auto secret = suite.derive_secret(exporter_secret, label); |
| 417 | auto context_hash = suite.digest().hash(context); |
| 418 | return suite.expand_with_label(secret, "exported", context_hash, size); |
| 419 | } |
| 420 | |
| 421 | PSKWithSecret |
| 422 | KeyScheduleEpoch::resumption_psk_w_secret(ResumptionPSKUsage usage, |
| 423 | const bytes& group_id, |
| 424 | epoch_t epoch) |
| 425 | { |
| 426 | auto nonce = random_bytes(suite.secret_size()); |
| 427 | auto psk = ResumptionPSK{ usage, group_id, epoch }; |
| 428 | return { { psk, nonce }, resumption_psk }; |
| 429 | } |
| 430 | |
| 431 | bytes |
| 432 | KeyScheduleEpoch::make_psk_secret(CipherSuite suite, |
| 433 | const std::vector<PSKWithSecret>& psks) |
| 434 | { |
| 435 | auto psk_secret = suite.zero(); |
| 436 | auto count = uint16_t(psks.size()); |
| 437 | auto index = uint16_t(0); |
| 438 | for (const auto& psk : psks) { |
| 439 | auto psk_extracted = suite.hpke().kdf.extract(suite.zero(), psk.secret); |
| 440 | auto psk_label = tls::marshal(PSKLabel{ psk.id, index, count }); |
| 441 | auto psk_input = suite.expand_with_label( |
| 442 | psk_extracted, "derived psk", psk_label, suite.secret_size()); |
| 443 | psk_secret = suite.hpke().kdf.extract(psk_input, psk_secret); |
| 444 | index += 1; |
| 445 | } |
| 446 | return psk_secret; |
| 447 | } |
| 448 | |
| 449 | bytes |
| 450 | KeyScheduleEpoch::welcome_secret(CipherSuite suite, |
| 451 | const bytes& joiner_secret, |
| 452 | const std::vector<PSKWithSecret>& psks) |
| 453 | { |
| 454 | auto psk_secret = make_psk_secret(suite, psks); |
| 455 | return welcome_secret_raw(suite, joiner_secret, psk_secret); |
| 456 | } |
| 457 | |
| 458 | bytes |
| 459 | KeyScheduleEpoch::welcome_secret_raw(CipherSuite suite, |
| 460 | const bytes& joiner_secret, |
| 461 | const bytes& psk_secret) |
| 462 | { |
| 463 | auto extract = suite.hpke().kdf.extract(joiner_secret, psk_secret); |
| 464 | return suite.derive_secret(extract, "welcome"); |
| 465 | } |
| 466 | |
| 467 | KeyAndNonce |
| 468 | KeyScheduleEpoch::sender_data_keys(CipherSuite suite, |
| 469 | const bytes& sender_data_secret, |
| 470 | const bytes& ciphertext) |
| 471 | { |
| 472 | auto sample_size = suite.secret_size(); |
| 473 | auto sample = bytes(sample_size); |
| 474 | if (ciphertext.size() <= sample_size) { |
| 475 | sample = ciphertext; |
| 476 | } else { |
| 477 | sample = ciphertext.slice(0, sample_size); |
| 478 | } |
| 479 | |
| 480 | auto key_size = suite.hpke().aead.key_size; |
| 481 | auto nonce_size = suite.hpke().aead.nonce_size; |
| 482 | return { |
| 483 | suite.expand_with_label(sender_data_secret, "key", sample, key_size), |
| 484 | suite.expand_with_label(sender_data_secret, "nonce", sample, nonce_size), |
| 485 | }; |
| 486 | } |
| 487 | |
| 488 | bool |
| 489 | operator==(const KeyScheduleEpoch& lhs, const KeyScheduleEpoch& rhs) |
| 490 | { |
| 491 | auto epoch_secret = (lhs.epoch_secret == rhs.epoch_secret); |
| 492 | auto sender_data_secret = (lhs.sender_data_secret == rhs.sender_data_secret); |
| 493 | auto encryption_secret = (lhs.encryption_secret == rhs.encryption_secret); |
| 494 | auto exporter_secret = (lhs.exporter_secret == rhs.exporter_secret); |
| 495 | auto confirmation_key = (lhs.confirmation_key == rhs.confirmation_key); |
| 496 | auto init_secret = (lhs.init_secret == rhs.init_secret); |
| 497 | auto external_priv = (lhs.external_priv == rhs.external_priv); |
| 498 | |
| 499 | return epoch_secret && sender_data_secret && encryption_secret && |
| 500 | exporter_secret && confirmation_key && init_secret && external_priv; |
| 501 | } |
| 502 | |
| 503 | // struct { |
| 504 | // WireFormat wire_format; |
| 505 | // GroupContent content; // with content.content_type == commit |
| 506 | // opaque signature<V>; |
| 507 | // } ConfirmedTranscriptHashInput; |
| 508 | struct ConfirmedTranscriptHashInput |
| 509 | { |
| 510 | WireFormat wire_format; |
| 511 | const GroupContent& content; |
| 512 | const bytes& signature; |
| 513 | |
| 514 | TLS_SERIALIZABLE(wire_format, content, signature) |
| 515 | }; |
| 516 | |
| 517 | // struct { |
| 518 | // MAC confirmation_tag; |
| 519 | // } InterimTranscriptHashInput; |
| 520 | struct InterimTranscriptHashInput |
| 521 | { |
| 522 | bytes confirmation_tag; |
| 523 | |
| 524 | TLS_SERIALIZABLE(confirmation_tag) |
| 525 | }; |
| 526 | |
| 527 | TranscriptHash::TranscriptHash(CipherSuite suite_in) |
| 528 | : suite(suite_in) |
| 529 | { |
| 530 | } |
| 531 | |
| 532 | TranscriptHash::TranscriptHash(CipherSuite suite_in, |
| 533 | bytes confirmed_in, |
| 534 | const bytes& confirmation_tag) |
| 535 | : suite(suite_in) |
| 536 | , confirmed(std::move(confirmed_in)) |
| 537 | { |
| 538 | update_interim(confirmation_tag); |
| 539 | } |
| 540 | |
| 541 | void |
| 542 | TranscriptHash::update(const AuthenticatedContent& content_auth) |
| 543 | { |
| 544 | update_confirmed(content_auth); |
| 545 | update_interim(content_auth); |
| 546 | } |
| 547 | |
| 548 | void |
| 549 | TranscriptHash::update_confirmed(const AuthenticatedContent& content_auth) |
| 550 | { |
| 551 | const auto transcript = |
| 552 | interim + content_auth.confirmed_transcript_hash_input(); |
| 553 | confirmed = suite.digest().hash(transcript); |
| 554 | } |
| 555 | |
| 556 | void |
| 557 | TranscriptHash::update_interim(const bytes& confirmation_tag) |
| 558 | { |
| 559 | const auto transcript = confirmed + tls::marshal(confirmation_tag); |
| 560 | interim = suite.digest().hash(transcript); |
| 561 | } |
| 562 | |
| 563 | void |
| 564 | TranscriptHash::update_interim(const AuthenticatedContent& content_auth) |
| 565 | { |
| 566 | const auto transcript = |
| 567 | confirmed + content_auth.interim_transcript_hash_input(); |
| 568 | interim = suite.digest().hash(transcript); |
| 569 | } |
| 570 | |
| 571 | bool |
| 572 | operator==(const TranscriptHash& lhs, const TranscriptHash& rhs) |
| 573 | { |
| 574 | auto confirmed = (lhs.confirmed == rhs.confirmed); |
| 575 | auto interim = (lhs.interim == rhs.interim); |
| 576 | return confirmed && interim; |
| 577 | } |
| 578 | |
| 579 | } // namespace mlspp |