openmls 0.8.1

A Rust implementation of the Messaging Layer Security (MLS) protocol, as defined in RFC 9420.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! # Known Answer Tests for the key schedule
//!
//! See <https://github.com/mlswg/mls-implementations/blob/master/test-vectors.md>
//! for more description on the test vectors.
//!
//! If values are not present, they are encoded as empty strings.

use log::info;
use openmls_traits::{random::OpenMlsRand, types::HpkeKeyPair, OpenMlsProvider};
use serde::{self, Deserialize, Serialize};
use tls_codec::Serialize as TlsSerializeTrait;

#[cfg(test)]
use crate::test_utils::write;
use crate::{
    ciphersuite::*,
    extensions::Extensions,
    group::*,
    schedule::{errors::KsTestVectorError, CommitSecret, *},
    test_utils::*,
};

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
struct Exporter {
    label: String,
    context: String,
    length: u32,
    secret: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
struct Epoch {
    // Chosen by the generator
    tree_hash: String,
    commit_secret: String,
    psk_secret: String,
    confirmed_transcript_hash: String,

    // Computed values
    group_context: String,
    joiner_secret: String,
    welcome_secret: String,
    init_secret: String,
    sender_data_secret: String,
    encryption_secret: String,
    exporter_secret: String,
    epoch_authenticator: String,
    external_secret: String,
    confirmation_key: String,
    membership_key: String,
    resumption_psk: String,

    external_pub: String,
    exporter: Exporter,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct KeyScheduleTestVector {
    pub cipher_suite: u16,
    group_id: String,
    initial_init_secret: String,
    epochs: Vec<Epoch>,
}

// Ignore clippy warning since this just used for testing
#[allow(clippy::type_complexity)]
fn generate(
    ciphersuite: Ciphersuite,
    init_secret: &InitSecret,
    group_id: &[u8],
    epoch: u64,
) -> (
    Vec<u8>,
    CommitSecret,
    PskSecret,
    JoinerSecret,
    WelcomeSecret,
    EpochSecrets,
    Vec<u8>,
    GroupContext,
    HpkeKeyPair,
) {
    let crypto = OpenMlsRustCrypto::default();
    let tree_hash = crypto
        .rand()
        .random_vec(ciphersuite.hash_length())
        .expect("An unexpected error occurred.");
    let commit_secret = CommitSecret::random(ciphersuite, crypto.rand());

    let confirmed_transcript_hash = crypto
        .rand()
        .random_vec(ciphersuite.hash_length())
        .expect("An unexpected error occurred.");

    // PSK secret can sometimes be the all zero vector
    let a: [u8; 1] = crypto.rand().random_array().unwrap();
    let psk_secret = if a[0] > 127 {
        PskSecret::from(Secret::random(ciphersuite, crypto.rand()).unwrap())
    } else {
        PskSecret::from(Secret::zero(ciphersuite))
    };

    let group_context = GroupContext::new(
        ciphersuite,
        GroupId::from_slice(group_id),
        epoch,
        tree_hash.to_vec(),
        confirmed_transcript_hash.clone(),
        Extensions::empty(),
    );

    let joiner_secret = JoinerSecret::new(
        crypto.crypto(),
        ciphersuite,
        commit_secret.clone(),
        init_secret,
        &group_context.tls_serialize_detached().unwrap(),
    )
    .expect("Could not create JoinerSecret.");
    let mut key_schedule = KeySchedule::init(
        ciphersuite,
        crypto.crypto(),
        &joiner_secret,
        psk_secret.clone(),
    )
    .expect("Could not create KeySchedule.");
    let welcome_secret = key_schedule
        .welcome(crypto.crypto(), ciphersuite)
        .expect("An unexpected error occurred.");

    let serialized_group_context = group_context
        .tls_serialize_detached()
        .expect("Could not serialize group context.");

    key_schedule
        .add_context(crypto.crypto(), &serialized_group_context)
        .expect("An unexpected error occurred.");
    let EpochSecretsResult { epoch_secrets, .. } = key_schedule
        .epoch_secrets(crypto.crypto(), ciphersuite)
        .expect("An unexpected error occurred.");

    // Calculate external HPKE key pair
    let external_key_pair = epoch_secrets
        .external_secret()
        .derive_external_keypair(crypto.crypto(), ciphersuite)
        .expect("An unexpected crypto error occurred.");

    (
        confirmed_transcript_hash,
        commit_secret,
        psk_secret,
        joiner_secret,
        welcome_secret,
        epoch_secrets,
        tree_hash,
        group_context,
        external_key_pair,
    )
}

#[cfg(any(feature = "test-utils", test))]
pub fn generate_test_vector(
    n_epochs: u64,
    ciphersuite: Ciphersuite,
    provider: &impl OpenMlsProvider,
) -> KeyScheduleTestVector {
    use tls_codec::Serialize;

    // Set up setting.
    let mut init_secret =
        InitSecret::random(ciphersuite, provider.rand()).expect("Not enough randomness.");
    let initial_init_secret = init_secret.clone();
    let group_id = provider
        .rand()
        .random_vec(16)
        .expect("An unexpected error occurred.");

    let mut epochs = Vec::new();

    // Generate info for all epochs
    for epoch in 0..n_epochs {
        println!("Generating epoch: {epoch:?}");
        let (
            confirmed_transcript_hash,
            commit_secret,
            psk_secret,
            joiner_secret,
            welcome_secret,
            epoch_secrets,
            tree_hash,
            group_context,
            external_key_pair,
        ) = generate(ciphersuite, &init_secret, &group_id, epoch);

        // exporter
        let exporter_label = "exporter label";
        let exporter_length = 32u32;
        let exporter_context = b"exporter context";
        let exported = epoch_secrets
            .exporter_secret()
            .derive_exported_secret(
                ciphersuite,
                provider.crypto(),
                exporter_label,
                exporter_context,
                exporter_length as usize,
            )
            .unwrap();

        let epoch_info = Epoch {
            tree_hash: bytes_to_hex(&tree_hash),
            commit_secret: bytes_to_hex(commit_secret.as_slice()),
            psk_secret: bytes_to_hex(psk_secret.as_slice()),
            confirmed_transcript_hash: bytes_to_hex(&confirmed_transcript_hash),
            group_context: bytes_to_hex(
                &group_context
                    .tls_serialize_detached()
                    .expect("An unexpected error occurred."),
            ),
            joiner_secret: bytes_to_hex(joiner_secret.as_slice()),
            welcome_secret: bytes_to_hex(welcome_secret.as_slice()),
            init_secret: bytes_to_hex(epoch_secrets.init_secret().as_slice()),
            sender_data_secret: bytes_to_hex(epoch_secrets.sender_data_secret().as_slice()),
            encryption_secret: bytes_to_hex(epoch_secrets.encryption_secret().as_slice()),
            exporter_secret: bytes_to_hex(epoch_secrets.exporter_secret().as_slice()),
            epoch_authenticator: bytes_to_hex(epoch_secrets.epoch_authenticator().as_slice()),
            external_secret: bytes_to_hex(epoch_secrets.external_secret().as_slice()),
            confirmation_key: bytes_to_hex(epoch_secrets.confirmation_key().as_slice()),
            membership_key: bytes_to_hex(epoch_secrets.membership_key().as_slice()),
            resumption_psk: bytes_to_hex(epoch_secrets.resumption_psk().as_slice()),
            external_pub: bytes_to_hex(&external_key_pair.public),
            exporter: Exporter {
                label: exporter_label.into(),
                context: bytes_to_hex(exporter_context),
                length: exporter_length,
                secret: bytes_to_hex(&exported),
            },
        };
        epochs.push(epoch_info);
        init_secret = epoch_secrets.init_secret().clone();
    }

    KeyScheduleTestVector {
        cipher_suite: ciphersuite as u16,
        group_id: bytes_to_hex(&group_id),
        initial_init_secret: bytes_to_hex(initial_init_secret.as_slice()),
        epochs,
    }
}

#[test]
fn write_test_vectors() {
    const NUM_EPOCHS: u64 = 2;
    let mut tests = Vec::new();
    let provider = OpenMlsRustCrypto::default();
    for &ciphersuite in provider.crypto().supported_ciphersuites().iter() {
        tests.push(generate_test_vector(NUM_EPOCHS, ciphersuite, &provider));
    }
    write("test_vectors/key-schedule-new.json", &tests);
}

#[openmls_test::openmls_test]
fn read_test_vectors_key_schedule() {
    let provider = &Provider::default();

    let _ = pretty_env_logger::try_init();

    let tests: Vec<KeyScheduleTestVector> =
        read_json!("../../../../test_vectors/key-schedule.json");

    for test_vector in tests {
        match run_test_vector(test_vector, provider) {
            Ok(_) => {}
            Err(e) => panic!("Error while checking key schedule test vector.\n{e:?}"),
        }
    }
}

#[cfg(any(feature = "test-utils", test))]
pub fn run_test_vector(
    test_vector: KeyScheduleTestVector,
    provider: &impl OpenMlsProvider,
) -> Result<(), KsTestVectorError> {
    let ciphersuite = Ciphersuite::try_from(test_vector.cipher_suite).expect("Invalid ciphersuite");
    log::trace!("  {test_vector:?}");

    if !provider
        .crypto()
        .supported_ciphersuites()
        .contains(&ciphersuite)
    {
        info!("Skipping unsupported ciphersuite `{ciphersuite:?}`.");
        return Ok(());
    }

    let group_id = hex_to_bytes(&test_vector.group_id);
    let init_secret = hex_to_bytes(&test_vector.initial_init_secret);
    log::trace!(
        "  InitSecret from tve: {:?}",
        test_vector.initial_init_secret
    );
    let mut init_secret = InitSecret::from(Secret::from_slice(&init_secret));

    for (epoch_ctr, epoch) in test_vector.epochs.iter().enumerate() {
        let tree_hash = hex_to_bytes(&epoch.tree_hash);
        let secret = hex_to_bytes(&epoch.commit_secret);
        let commit_secret = CommitSecret::from(PathSecret::from(Secret::from_slice(&secret)));
        log::trace!("    CommitSecret from tve {:?}", epoch.commit_secret);

        let confirmed_transcript_hash = hex_to_bytes(&epoch.confirmed_transcript_hash);

        let group_context = GroupContext::new(
            ciphersuite,
            GroupId::from_slice(&group_id),
            GroupEpoch::from(epoch_ctr as u64),
            tree_hash.to_vec(),
            confirmed_transcript_hash.clone(),
            Extensions::empty(),
        );

        let joiner_secret = JoinerSecret::new(
            provider.crypto(),
            ciphersuite,
            commit_secret,
            &init_secret,
            &group_context.tls_serialize_detached().unwrap(),
        )
        .expect("Could not create JoinerSecret.");
        if hex_to_bytes(&epoch.joiner_secret) != joiner_secret.as_slice() {
            if cfg!(test) {
                panic!("Joiner secret mismatch");
            }
            return Err(KsTestVectorError::JoinerSecretMismatch);
        }

        let psk_secret_inner = Secret::from_slice(&hex_to_bytes(&epoch.psk_secret));
        let psk_secret = PskSecret::from(psk_secret_inner);

        let mut key_schedule =
            KeySchedule::init(ciphersuite, provider.crypto(), &joiner_secret, psk_secret)
                .expect("Could not create KeySchedule.");
        let welcome_secret = key_schedule
            .welcome(provider.crypto(), ciphersuite)
            .expect("An unexpected error occurred.");

        if hex_to_bytes(&epoch.welcome_secret) != welcome_secret.as_slice() {
            if cfg!(test) {
                panic!("Welcome secret mismatch");
            }
            return Err(KsTestVectorError::WelcomeSecretMismatch);
        }

        let expected_group_context = hex_to_bytes(&epoch.group_context);
        let group_context_serialized = group_context
            .tls_serialize_detached()
            .expect("An unexpected error occurred.");
        if group_context_serialized != expected_group_context {
            log::error!("  Group context mismatch");
            log::debug!("    Computed: {group_context_serialized:x?}");
            log::debug!("    Expected: {expected_group_context:x?}");
            if cfg!(test) {
                panic!("Group context mismatch");
            }
            return Err(KsTestVectorError::GroupContextMismatch);
        }

        key_schedule
            .add_context(provider.crypto(), &group_context_serialized)
            .expect("An unexpected error occurred.");

        let EpochSecretsResult { epoch_secrets, .. } = key_schedule
            .epoch_secrets(provider.crypto(), ciphersuite)
            .expect("An unexpected error occurred.");

        init_secret = epoch_secrets.init_secret().clone();
        if hex_to_bytes(&epoch.init_secret) != init_secret.as_slice() {
            log_crypto!(
                debug,
                "    Epoch secret mismatch: {:x?} != {:x?}",
                hex_to_bytes(&epoch.init_secret),
                init_secret.as_slice()
            );
            if cfg!(test) {
                panic!("Init secret mismatch");
            }
            return Err(KsTestVectorError::InitSecretMismatch);
        }
        if hex_to_bytes(&epoch.sender_data_secret) != epoch_secrets.sender_data_secret().as_slice()
        {
            if cfg!(test) {
                panic!("Sender data secret mismatch");
            }
            return Err(KsTestVectorError::SenderDataSecretMismatch);
        }
        if hex_to_bytes(&epoch.encryption_secret) != epoch_secrets.encryption_secret().as_slice() {
            if cfg!(test) {
                panic!("Encryption secret mismatch");
            }
            return Err(KsTestVectorError::EncryptionSecretMismatch);
        }
        if hex_to_bytes(&epoch.exporter_secret) != epoch_secrets.exporter_secret().as_slice() {
            if cfg!(test) {
                panic!("Exporter secret mismatch");
            }
            return Err(KsTestVectorError::ExporterSecretMismatch);
        }
        if hex_to_bytes(&epoch.epoch_authenticator)
            != epoch_secrets.epoch_authenticator().as_slice()
        {
            if cfg!(test) {
                panic!("Epoch authenticator mismatch");
            }
            return Err(KsTestVectorError::EpochAuthenticatorMismatch);
        }
        if hex_to_bytes(&epoch.external_secret) != epoch_secrets.external_secret().as_slice() {
            if cfg!(test) {
                panic!("External secret mismatch");
            }
            return Err(KsTestVectorError::ExternalSecretMismatch);
        }
        if hex_to_bytes(&epoch.confirmation_key) != epoch_secrets.confirmation_key().as_slice() {
            if cfg!(test) {
                panic!("Confirmation key mismatch");
            }
            return Err(KsTestVectorError::ConfirmationKeyMismatch);
        }
        if hex_to_bytes(&epoch.membership_key) != epoch_secrets.membership_key().as_slice() {
            if cfg!(test) {
                panic!("Membership key mismatch");
            }
            return Err(KsTestVectorError::MembershipKeyMismatch);
        }
        if hex_to_bytes(&epoch.resumption_psk) != epoch_secrets.resumption_psk().as_slice() {
            if cfg!(test) {
                panic!("Resumption psk mismatch");
            }
            return Err(KsTestVectorError::ResumptionPskMismatch);
        }

        // Calculate external HPKE key pair
        let external_key_pair = epoch_secrets
            .external_secret()
            .derive_external_keypair(provider.crypto(), ciphersuite)
            .expect("an unexpected crypto error occurred");
        if hex_to_bytes(&epoch.external_pub) != external_key_pair.public {
            log::error!("  External public key mismatch");
            log::debug!(
                "    Computed: {:x?}",
                HpkePublicKey::from(external_key_pair.public)
                    .tls_serialize_detached()
                    .expect("An unexpected error occurred.")
            );
            log::debug!("    Expected: {:x?}", hex_to_bytes(&epoch.external_pub));
            if cfg!(test) {
                panic!("External pub mismatch");
            }
            return Err(KsTestVectorError::ExternalPubMismatch);
        }

        // Check exported secret
        let exported = epoch_secrets
            .exporter_secret()
            .derive_exported_secret(
                ciphersuite,
                provider.crypto(),
                &epoch.exporter.label,
                &hex_to_bytes(&epoch.exporter.context),
                epoch.exporter.length as usize,
            )
            .unwrap();
        if hex_to_bytes(&epoch.exporter.secret) != exported {
            if cfg!(test) {
                panic!("Exporter mismatch");
            }
            return Err(KsTestVectorError::ExporterMismatch);
        }
    }
    Ok(())
}