| 1 | #include <mls/core_types.h> |
| 2 | #include <mls/crypto.h> |
| 3 | #include <mls/messages.h> |
| 4 | |
| 5 | #include <string> |
| 6 | |
| 7 | using mlspp::hpke::AEAD; // NOLINT(misc-unused-using-decls) |
| 8 | using mlspp::hpke::Digest; // NOLINT(misc-unused-using-decls) |
| 9 | using mlspp::hpke::HPKE; // NOLINT(misc-unused-using-decls) |
| 10 | using mlspp::hpke::KDF; // NOLINT(misc-unused-using-decls) |
| 11 | using mlspp::hpke::KEM; // NOLINT(misc-unused-using-decls) |
| 12 | using mlspp::hpke::Signature; // NOLINT(misc-unused-using-decls) |
| 13 | |
| 14 | namespace mlspp { |
| 15 | |
| 16 | SignatureScheme |
| 17 | tls_signature_scheme(Signature::ID id) |
| 18 | { |
| 19 | switch (id) { |
| 20 | case Signature::ID::P256_SHA256: |
| 21 | return SignatureScheme::ecdsa_secp256r1_sha256; |
| 22 | case Signature::ID::P384_SHA384: |
| 23 | return SignatureScheme::ecdsa_secp384r1_sha384; |
| 24 | case Signature::ID::P521_SHA512: |
| 25 | return SignatureScheme::ecdsa_secp521r1_sha512; |
| 26 | case Signature::ID::Ed25519: |
| 27 | return SignatureScheme::ed25519; |
| 28 | #if !defined(WITH_BORINGSSL) |
| 29 | case Signature::ID::Ed448: |
| 30 | return SignatureScheme::ed448; |
| 31 | #endif |
| 32 | case Signature::ID::RSA_SHA256: |
| 33 | return SignatureScheme::rsa_pkcs1_sha256; |
| 34 | default: |
| 35 | throw InvalidParameterError("Unsupported algorithm"); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// |
| 40 | /// CipherSuites and details |
| 41 | /// |
| 42 | |
| 43 | CipherSuite::CipherSuite() |
| 44 | : id(ID::unknown) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | CipherSuite::CipherSuite(ID id_in) |
| 49 | : id(id_in) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | SignatureScheme |
| 54 | CipherSuite::signature_scheme() const |
| 55 | { |
| 56 | switch (id) { |
| 57 | case ID::X25519_AES128GCM_SHA256_Ed25519: |
| 58 | case ID::X25519_CHACHA20POLY1305_SHA256_Ed25519: |
| 59 | return SignatureScheme::ed25519; |
| 60 | case ID::P256_AES128GCM_SHA256_P256: |
| 61 | return SignatureScheme::ecdsa_secp256r1_sha256; |
| 62 | case ID::X448_AES256GCM_SHA512_Ed448: |
| 63 | case ID::X448_CHACHA20POLY1305_SHA512_Ed448: |
| 64 | return SignatureScheme::ed448; |
| 65 | case ID::P521_AES256GCM_SHA512_P521: |
| 66 | return SignatureScheme::ecdsa_secp521r1_sha512; |
| 67 | case ID::P384_AES256GCM_SHA384_P384: |
| 68 | return SignatureScheme::ecdsa_secp384r1_sha384; |
| 69 | default: |
| 70 | throw InvalidParameterError("Unsupported algorithm"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | const CipherSuite::Ciphers& |
| 75 | CipherSuite::get() const |
| 76 | { |
| 77 | static const auto ciphers_X25519_AES128GCM_SHA256_Ed25519 = |
| 78 | CipherSuite::Ciphers{ |
| 79 | HPKE(KEM::ID::DHKEM_X25519_SHA256, |
| 80 | KDF::ID::HKDF_SHA256, |
| 81 | AEAD::ID::AES_128_GCM), |
| 82 | Digest::get<Digest::ID::SHA256>(), |
| 83 | Signature::get<Signature::ID::Ed25519>(), |
| 84 | }; |
| 85 | |
| 86 | static const auto ciphers_P256_AES128GCM_SHA256_P256 = CipherSuite::Ciphers{ |
| 87 | HPKE( |
| 88 | KEM::ID::DHKEM_P256_SHA256, KDF::ID::HKDF_SHA256, AEAD::ID::AES_128_GCM), |
| 89 | Digest::get<Digest::ID::SHA256>(), |
| 90 | Signature::get<Signature::ID::P256_SHA256>(), |
| 91 | }; |
| 92 | |
| 93 | static const auto ciphers_X25519_CHACHA20POLY1305_SHA256_Ed25519 = |
| 94 | CipherSuite::Ciphers{ |
| 95 | HPKE(KEM::ID::DHKEM_X25519_SHA256, |
| 96 | KDF::ID::HKDF_SHA256, |
| 97 | AEAD::ID::CHACHA20_POLY1305), |
| 98 | Digest::get<Digest::ID::SHA256>(), |
| 99 | Signature::get<Signature::ID::Ed25519>(), |
| 100 | }; |
| 101 | |
| 102 | static const auto ciphers_P521_AES256GCM_SHA512_P521 = CipherSuite::Ciphers{ |
| 103 | HPKE( |
| 104 | KEM::ID::DHKEM_P521_SHA512, KDF::ID::HKDF_SHA512, AEAD::ID::AES_256_GCM), |
| 105 | Digest::get<Digest::ID::SHA512>(), |
| 106 | Signature::get<Signature::ID::P521_SHA512>(), |
| 107 | }; |
| 108 | |
| 109 | static const auto ciphers_P384_AES256GCM_SHA384_P384 = CipherSuite::Ciphers{ |
| 110 | HPKE( |
| 111 | KEM::ID::DHKEM_P384_SHA384, KDF::ID::HKDF_SHA384, AEAD::ID::AES_256_GCM), |
| 112 | Digest::get<Digest::ID::SHA384>(), |
| 113 | Signature::get<Signature::ID::P384_SHA384>(), |
| 114 | }; |
| 115 | |
| 116 | #if !defined(WITH_BORINGSSL) |
| 117 | static const auto ciphers_X448_AES256GCM_SHA512_Ed448 = CipherSuite::Ciphers{ |
| 118 | HPKE( |
| 119 | KEM::ID::DHKEM_X448_SHA512, KDF::ID::HKDF_SHA512, AEAD::ID::AES_256_GCM), |
| 120 | Digest::get<Digest::ID::SHA512>(), |
| 121 | Signature::get<Signature::ID::Ed448>(), |
| 122 | }; |
| 123 | |
| 124 | static const auto ciphers_X448_CHACHA20POLY1305_SHA512_Ed448 = |
| 125 | CipherSuite::Ciphers{ |
| 126 | HPKE(KEM::ID::DHKEM_X448_SHA512, |
| 127 | KDF::ID::HKDF_SHA512, |
| 128 | AEAD::ID::CHACHA20_POLY1305), |
| 129 | Digest::get<Digest::ID::SHA512>(), |
| 130 | Signature::get<Signature::ID::Ed448>(), |
| 131 | }; |
| 132 | #endif |
| 133 | |
| 134 | switch (id) { |
| 135 | case ID::unknown: |
| 136 | throw InvalidParameterError("Uninitialized ciphersuite"); |
| 137 | |
| 138 | case ID::X25519_AES128GCM_SHA256_Ed25519: |
| 139 | return ciphers_X25519_AES128GCM_SHA256_Ed25519; |
| 140 | |
| 141 | case ID::P256_AES128GCM_SHA256_P256: |
| 142 | return ciphers_P256_AES128GCM_SHA256_P256; |
| 143 | |
| 144 | case ID::X25519_CHACHA20POLY1305_SHA256_Ed25519: |
| 145 | return ciphers_X25519_CHACHA20POLY1305_SHA256_Ed25519; |
| 146 | |
| 147 | case ID::P521_AES256GCM_SHA512_P521: |
| 148 | return ciphers_P521_AES256GCM_SHA512_P521; |
| 149 | |
| 150 | case ID::P384_AES256GCM_SHA384_P384: |
| 151 | return ciphers_P384_AES256GCM_SHA384_P384; |
| 152 | |
| 153 | #if !defined(WITH_BORINGSSL) |
| 154 | case ID::X448_AES256GCM_SHA512_Ed448: |
| 155 | return ciphers_X448_AES256GCM_SHA512_Ed448; |
| 156 | |
| 157 | case ID::X448_CHACHA20POLY1305_SHA512_Ed448: |
| 158 | return ciphers_X448_CHACHA20POLY1305_SHA512_Ed448; |
| 159 | #endif |
| 160 | |
| 161 | default: |
| 162 | throw InvalidParameterError("Unsupported ciphersuite"); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | struct HKDFLabel |
| 167 | { |
| 168 | uint16_t length; |
| 169 | bytes label; |
| 170 | bytes context; |
| 171 | |
| 172 | TLS_SERIALIZABLE(length, label, context) |
| 173 | }; |
| 174 | |
| 175 | bytes |
| 176 | CipherSuite::expand_with_label(const bytes& secret, |
| 177 | const std::string& label, |
| 178 | const bytes& context, |
| 179 | size_t length) const |
| 180 | { |
| 181 | auto mls_label = from_ascii(std::string("MLS 1.0 ") + label); |
| 182 | auto length16 = static_cast<uint16_t>(length); |
| 183 | auto label_bytes = tls::marshal(HKDFLabel{ length16, mls_label, context }); |
| 184 | return get().hpke.kdf.expand(secret, label_bytes, length); |
| 185 | } |
| 186 | |
| 187 | bytes |
| 188 | CipherSuite::derive_secret(const bytes& secret, const std::string& label) const |
| 189 | { |
| 190 | return expand_with_label(secret, label, {}, secret_size()); |
| 191 | } |
| 192 | |
| 193 | bytes |
| 194 | CipherSuite::derive_tree_secret(const bytes& secret, |
| 195 | const std::string& label, |
| 196 | uint32_t generation, |
| 197 | size_t length) const |
| 198 | { |
| 199 | return expand_with_label(secret, label, tls::marshal(generation), length); |
| 200 | } |
| 201 | |
| 202 | #if WITH_BORINGSSL |
| 203 | const std::array<CipherSuite::ID, 5> all_supported_suites = { |
| 204 | CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519, |
| 205 | CipherSuite::ID::P256_AES128GCM_SHA256_P256, |
| 206 | CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519, |
| 207 | CipherSuite::ID::P521_AES256GCM_SHA512_P521, |
| 208 | CipherSuite::ID::P384_AES256GCM_SHA384_P384, |
| 209 | }; |
| 210 | #else |
| 211 | const std::array<CipherSuite::ID, 7> all_supported_suites = { |
| 212 | CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519, |
| 213 | CipherSuite::ID::P256_AES128GCM_SHA256_P256, |
| 214 | CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519, |
| 215 | CipherSuite::ID::P521_AES256GCM_SHA512_P521, |
| 216 | CipherSuite::ID::P384_AES256GCM_SHA384_P384, |
| 217 | CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448, |
| 218 | CipherSuite::ID::X448_AES256GCM_SHA512_Ed448, |
| 219 | }; |
| 220 | #endif |
| 221 | |
| 222 | // MakeKeyPackageRef(value) = KDF.expand( |
| 223 | // KDF.extract("", value), "MLS 1.0 KeyPackage Reference", 16) |
| 224 | template<> |
| 225 | const bytes& |
| 226 | CipherSuite::reference_label<KeyPackage>() |
| 227 | { |
| 228 | static const auto label = from_ascii("MLS 1.0 KeyPackage Reference"); |
| 229 | return label; |
| 230 | } |
| 231 | |
| 232 | // MakeProposalRef(value) = KDF.expand( |
| 233 | // KDF.extract("", value), "MLS 1.0 Proposal Reference", 16) |
| 234 | // |
| 235 | // Even though the label says "Proposal", we actually hash the entire enclosing |
| 236 | // AuthenticatedContent object. |
| 237 | template<> |
| 238 | const bytes& |
| 239 | CipherSuite::reference_label<AuthenticatedContent>() |
| 240 | { |
| 241 | static const auto label = from_ascii("MLS 1.0 Proposal Reference"); |
| 242 | return label; |
| 243 | } |
| 244 | |
| 245 | /// |
| 246 | /// HPKEPublicKey and HPKEPrivateKey |
| 247 | /// |
| 248 | |
| 249 | // This function produces a non-literal type, so it can't be constexpr. |
| 250 | // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) |
| 251 | #define MLS_1_0_PLUS(label) from_ascii("MLS 1.0 " label) |
| 252 | |
| 253 | static bytes |
| 254 | mls_1_0_plus(const std::string& label) |
| 255 | { |
| 256 | auto plus = "MLS 1.0 "s + label; |
| 257 | return from_ascii(plus); |
| 258 | } |
| 259 | |
| 260 | namespace encrypt_label { |
| 261 | const std::string update_path_node = "UpdatePathNode"; |
| 262 | const std::string welcome = "Welcome"; |
| 263 | } // namespace encrypt_label |
| 264 | |
| 265 | struct EncryptContext |
| 266 | { |
| 267 | const bytes& label; |
| 268 | const bytes& content; |
| 269 | TLS_SERIALIZABLE(label, content) |
| 270 | }; |
| 271 | |
| 272 | HPKECiphertext |
| 273 | HPKEPublicKey::encrypt(CipherSuite suite, |
| 274 | const std::string& label, |
| 275 | const bytes& context, |
| 276 | const bytes& pt) const |
| 277 | { |
| 278 | auto label_plus = mls_1_0_plus(label); |
| 279 | auto encrypt_context = tls::marshal(EncryptContext{ label_plus, context }); |
| 280 | auto pkR = suite.hpke().kem.deserialize(data); |
| 281 | auto [enc, ctx] = suite.hpke().setup_base_s(*pkR, encrypt_context); |
| 282 | auto ct = ctx.seal({}, pt); |
| 283 | return HPKECiphertext{ enc, ct }; |
| 284 | } |
| 285 | |
| 286 | std::tuple<bytes, bytes> |
| 287 | HPKEPublicKey::do_export(CipherSuite suite, |
| 288 | const bytes& info, |
| 289 | const std::string& label, |
| 290 | size_t size) const |
| 291 | { |
| 292 | auto label_data = from_ascii(label); |
| 293 | auto pkR = suite.hpke().kem.deserialize(data); |
| 294 | auto [enc, ctx] = suite.hpke().setup_base_s(*pkR, info); |
| 295 | auto exported = ctx.do_export(label_data, size); |
| 296 | return std::make_tuple(enc, exported); |
| 297 | } |
| 298 | |
| 299 | HPKEPrivateKey |
| 300 | HPKEPrivateKey::generate(CipherSuite suite) |
| 301 | { |
| 302 | auto priv = suite.hpke().kem.generate_key_pair(); |
| 303 | auto priv_data = suite.hpke().kem.serialize_private(*priv); |
| 304 | auto pub = priv->public_key(); |
| 305 | auto pub_data = suite.hpke().kem.serialize(*pub); |
| 306 | return { priv_data, pub_data }; |
| 307 | } |
| 308 | |
| 309 | HPKEPrivateKey |
| 310 | HPKEPrivateKey::parse(CipherSuite suite, const bytes& data) |
| 311 | { |
| 312 | auto priv = suite.hpke().kem.deserialize_private(data); |
| 313 | auto pub = priv->public_key(); |
| 314 | auto pub_data = suite.hpke().kem.serialize(*pub); |
| 315 | return { data, pub_data }; |
| 316 | } |
| 317 | |
| 318 | HPKEPrivateKey |
| 319 | HPKEPrivateKey::derive(CipherSuite suite, const bytes& secret) |
| 320 | { |
| 321 | auto priv = suite.hpke().kem.derive_key_pair(secret); |
| 322 | auto priv_data = suite.hpke().kem.serialize_private(*priv); |
| 323 | auto pub = priv->public_key(); |
| 324 | auto pub_data = suite.hpke().kem.serialize(*pub); |
| 325 | return { priv_data, pub_data }; |
| 326 | } |
| 327 | |
| 328 | bytes |
| 329 | HPKEPrivateKey::decrypt(CipherSuite suite, |
| 330 | const std::string& label, |
| 331 | const bytes& context, |
| 332 | const HPKECiphertext& ct) const |
| 333 | { |
| 334 | auto label_plus = mls_1_0_plus(label); |
| 335 | auto encrypt_context = tls::marshal(EncryptContext{ label_plus, context }); |
| 336 | auto skR = suite.hpke().kem.deserialize_private(data); |
| 337 | auto ctx = suite.hpke().setup_base_r(ct.kem_output, *skR, encrypt_context); |
| 338 | auto pt = ctx.open({}, ct.ciphertext); |
| 339 | if (!pt) { |
| 340 | throw InvalidParameterError("HPKE decryption failure"); |
| 341 | } |
| 342 | |
| 343 | return opt::get(pt); |
| 344 | } |
| 345 | |
| 346 | bytes |
| 347 | HPKEPrivateKey::do_export(CipherSuite suite, |
| 348 | const bytes& info, |
| 349 | const bytes& kem_output, |
| 350 | const std::string& label, |
| 351 | size_t size) const |
| 352 | { |
| 353 | auto label_data = from_ascii(label); |
| 354 | auto skR = suite.hpke().kem.deserialize_private(data); |
| 355 | auto ctx = suite.hpke().setup_base_r(kem_output, *skR, info); |
| 356 | return ctx.do_export(label_data, size); |
| 357 | } |
| 358 | |
| 359 | HPKEPrivateKey::HPKEPrivateKey(bytes priv_data, bytes pub_data) |
| 360 | : data(std::move(priv_data)) |
| 361 | , public_key{ std::move(pub_data) } |
| 362 | { |
| 363 | } |
| 364 | |
| 365 | void |
| 366 | HPKEPrivateKey::set_public_key(CipherSuite suite) |
| 367 | { |
| 368 | const auto priv = suite.hpke().kem.deserialize_private(data); |
| 369 | auto pub = priv->public_key(); |
| 370 | public_key.data = suite.hpke().kem.serialize(*pub); |
| 371 | } |
| 372 | |
| 373 | /// |
| 374 | /// SignaturePublicKey and SignaturePrivateKey |
| 375 | /// |
| 376 | namespace sign_label { |
| 377 | const std::string mls_content = "FramedContentTBS"; |
| 378 | const std::string leaf_node = "LeafNodeTBS"; |
| 379 | const std::string key_package = "KeyPackageTBS"; |
| 380 | const std::string group_info = "GroupInfoTBS"; |
| 381 | const std::string multi_credential = "MultiCredential"; |
| 382 | } // namespace sign_label |
| 383 | |
| 384 | struct SignContent |
| 385 | { |
| 386 | const bytes& label; |
| 387 | const bytes& content; |
| 388 | TLS_SERIALIZABLE(label, content) |
| 389 | }; |
| 390 | |
| 391 | bool |
| 392 | SignaturePublicKey::verify(const CipherSuite& suite, |
| 393 | const std::string& label, |
| 394 | const bytes& message, |
| 395 | const bytes& signature) const |
| 396 | { |
| 397 | auto label_plus = mls_1_0_plus(label); |
| 398 | const auto content = tls::marshal(SignContent{ label_plus, message }); |
| 399 | auto pub = suite.sig().deserialize(data); |
| 400 | return suite.sig().verify(content, signature, *pub); |
| 401 | } |
| 402 | |
| 403 | SignaturePublicKey |
| 404 | SignaturePublicKey::from_jwk(CipherSuite suite, const std::string& json_str) |
| 405 | { |
| 406 | auto pub = suite.sig().import_jwk(json_str); |
| 407 | auto pub_data = suite.sig().serialize(*pub); |
| 408 | return SignaturePublicKey{ pub_data }; |
| 409 | } |
| 410 | |
| 411 | std::string |
| 412 | SignaturePublicKey::to_jwk(CipherSuite suite) const |
| 413 | { |
| 414 | auto pub = suite.sig().deserialize(data); |
| 415 | return suite.sig().export_jwk(*pub); |
| 416 | } |
| 417 | |
| 418 | PublicJWK |
| 419 | PublicJWK::parse(const std::string& jwk_json) |
| 420 | { |
| 421 | const auto parsed = Signature::parse_jwk(jwk_json); |
| 422 | const auto scheme = tls_signature_scheme(parsed.sig.id); |
| 423 | const auto pub_data = parsed.sig.serialize(*parsed.key); |
| 424 | return { scheme, parsed.key_id, { pub_data } }; |
| 425 | } |
| 426 | |
| 427 | SignaturePrivateKey |
| 428 | SignaturePrivateKey::generate(CipherSuite suite) |
| 429 | { |
| 430 | auto priv = suite.sig().generate_key_pair(); |
| 431 | auto priv_data = suite.sig().serialize_private(*priv); |
| 432 | auto pub = priv->public_key(); |
| 433 | auto pub_data = suite.sig().serialize(*pub); |
| 434 | return { priv_data, pub_data }; |
| 435 | } |
| 436 | |
| 437 | SignaturePrivateKey |
| 438 | SignaturePrivateKey::parse(CipherSuite suite, const bytes& data) |
| 439 | { |
| 440 | auto priv = suite.sig().deserialize_private(data); |
| 441 | auto pub = priv->public_key(); |
| 442 | auto pub_data = suite.sig().serialize(*pub); |
| 443 | return { data, pub_data }; |
| 444 | } |
| 445 | |
| 446 | SignaturePrivateKey |
| 447 | SignaturePrivateKey::derive(CipherSuite suite, const bytes& secret) |
| 448 | { |
| 449 | auto priv = suite.sig().derive_key_pair(secret); |
| 450 | auto priv_data = suite.sig().serialize_private(*priv); |
| 451 | auto pub = priv->public_key(); |
| 452 | auto pub_data = suite.sig().serialize(*pub); |
| 453 | return { priv_data, pub_data }; |
| 454 | } |
| 455 | |
| 456 | bytes |
| 457 | SignaturePrivateKey::sign(const CipherSuite& suite, |
| 458 | const std::string& label, |
| 459 | const bytes& message) const |
| 460 | { |
| 461 | auto label_plus = mls_1_0_plus(label); |
| 462 | const auto content = tls::marshal(SignContent{ label_plus, message }); |
| 463 | const auto priv = suite.sig().deserialize_private(data); |
| 464 | return suite.sig().sign(content, *priv); |
| 465 | } |
| 466 | |
| 467 | SignaturePrivateKey::SignaturePrivateKey(bytes priv_data, bytes pub_data) |
| 468 | : data(std::move(priv_data)) |
| 469 | , public_key{ std::move(pub_data) } |
| 470 | { |
| 471 | } |
| 472 | |
| 473 | void |
| 474 | SignaturePrivateKey::set_public_key(CipherSuite suite) |
| 475 | { |
| 476 | const auto priv = suite.sig().deserialize_private(data); |
| 477 | auto pub = priv->public_key(); |
| 478 | public_key.data = suite.sig().serialize(*pub); |
| 479 | } |
| 480 | |
| 481 | SignaturePrivateKey |
| 482 | SignaturePrivateKey::from_jwk(CipherSuite suite, const std::string& json_str) |
| 483 | { |
| 484 | auto priv = suite.sig().import_jwk_private(json_str); |
| 485 | auto priv_data = suite.sig().serialize_private(*priv); |
| 486 | auto pub = priv->public_key(); |
| 487 | auto pub_data = suite.sig().serialize(*pub); |
| 488 | return { priv_data, pub_data }; |
| 489 | } |
| 490 | |
| 491 | std::string |
| 492 | SignaturePrivateKey::to_jwk(CipherSuite suite) const |
| 493 | { |
| 494 | const auto priv = suite.sig().deserialize_private(data); |
| 495 | return suite.sig().export_jwk_private(*priv); |
| 496 | } |
| 497 | |
| 498 | } // namespace mlspp |