opentele2-core 2.0.0

Telegram client identity core: Telegram Desktop tdata, Telethon/mtcute/grammers sessions, official API profiles, devices, fingerprint checks and MTProto crypto
Documentation
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
//! Tests for `tdata::key_data` against the four vector folders in
//! `tests/vectors` (described by `tdata.json`) and the `argon2id_wrap` case of
//! `crypto.json`, plus synthetic malformed trailers.
//!
//! Argon2id derivations are slow in debug builds; only three tests run one:
//! the two passcode folders and the wrong-passcode case. Key bytes are never
//! printed: comparisons of key material use `assert!` without the values.

use std::fs;
use std::path::{Path, PathBuf};

use opentele2_core::crypto::AuthKey;
use opentele2_core::qt::QWriter;
use opentele2_core::tdata::key_data::{
    self, AccountsInfo, KDF_ARGON2ID, KDF_PBKDF2, KDF_SCRYPT, KEY_DATA_FORMAT_VERSION,
    KEY_DATA_MAGIC, KeyData, KeyDataTrailer, PasscodeKdf, create_passcode_key,
    decrypt_accounts_info, default_passcode_kdf, open_local_key, unwrap_local_key, wrap_local_key,
};
use opentele2_core::{Error, Result, tdf};
use serde_json::Value;

/// Telegram Desktop version stored in every vector key file.
const VECTOR_APP_VERSION: u32 = 7_003_001;

fn vectors_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/vectors")
}

fn manifest(file: &str) -> Value {
    let text = fs::read_to_string(vectors_dir().join(file)).expect("read vector manifest");
    serde_json::from_str(&text).expect("parse vector manifest")
}

fn code_of<T>(result: Result<T>) -> &'static str {
    match result {
        Ok(_) => panic!("expected an error, got Ok"),
        Err(error) => error.code(),
    }
}

/// Reads and parses the key file of a vector folder.
fn load(folder: &str) -> (tdf::TdfFile, KeyData) {
    let entry = &manifest("tdata.json")["folders"][folder];
    let key_file = entry["key_file"].as_str().expect("key_file");
    let file = tdf::read(vectors_dir().join(folder), &key_data::file_name(key_file))
        .expect("read key file");
    let data = KeyData::parse(&file.payload).expect("parse key data");
    (file, data)
}

/// Round-trips, opens, and decodes one vector folder.
fn check_folder(folder: &str, passcode_lock: bool) {
    let entry = manifest("tdata.json")["folders"][folder].clone();
    let (file, data) = load(folder);
    assert_eq!(file.version, VECTOR_APP_VERSION, "{folder} version");
    assert!(
        data.encode().expect("encode key data") == file.payload,
        "{folder} key data does not re-encode to identical bytes"
    );

    let trailer = data.trailer.as_ref().expect("7.3 trailer");
    if passcode_lock {
        assert!(data.key_encrypted.is_none(), "{folder} open wrap not null");
        assert_eq!(trailer.committed, 1, "{folder} committed");
        assert_eq!(trailer.wraps.len(), 1, "{folder} wrap count");
        assert_eq!(
            trailer.wraps[0].kdf,
            default_passcode_kdf(),
            "{folder} wrap kdf"
        );
    } else {
        assert!(data.key_encrypted.is_some(), "{folder} open wrap missing");
        assert_eq!(trailer.committed, 0, "{folder} committed");
        assert!(trailer.wraps.is_empty(), "{folder} has wraps");
    }

    let passcode = entry["passcode"].as_str().expect("passcode").as_bytes();
    let opened = open_local_key(&data, passcode).expect("open local key");
    let info = decrypt_accounts_info(&data, &opened.local_key, passcode).expect("accounts info");
    let indices: Vec<i32> = entry["accounts"]
        .as_array()
        .expect("accounts")
        .iter()
        .map(|a| i32::try_from(a["index"].as_i64().expect("index")).expect("i32 index"))
        .collect();
    let main = entry["main_account_index"].as_i64().expect("main index");
    assert_eq!(info.indices, indices, "{folder} account indices");
    // The account whose index is the active one is main, else the first
    // loaded account (the vectors store an active index of -1).
    let effective_main = if info.indices.contains(&info.active_index) {
        info.active_index
    } else {
        info.indices[0]
    };
    assert_eq!(
        i64::from(effective_main),
        main,
        "{folder} main account index"
    );

    // The recovered keys re-wrap and unwrap consistently.
    let rewrapped = wrap_local_key(&opened.local_key, &opened.passcode_key, &[0; 12])
        .expect("re-wrap local key");
    let unwrapped = unwrap_local_key(&rewrapped, &opened.passcode_key).expect("unwrap");
    assert!(
        unwrapped.as_bytes() == opened.local_key.as_bytes(),
        "{folder} re-wrapped local key differs"
    );
}

#[test]
fn plain_performance_on_round_trips_and_opens() {
    check_folder("tdata_plain/performance_on", false);
}

#[test]
fn plain_performance_off_round_trips_and_opens() {
    check_folder("tdata_plain/performance_off", false);
}

#[test]
fn passcode_performance_on_round_trips_and_opens() {
    check_folder("tdata_passcode/performance_on", true);
}

#[test]
fn passcode_performance_off_round_trips_and_opens() {
    check_folder("tdata_passcode/performance_off", true);
}

#[test]
fn wrong_passcode_is_bad_decrypt_key() {
    let (_, data) = load("tdata_passcode/performance_off");
    assert_eq!(
        code_of(open_local_key(&data, b"not-the-passcode")),
        "TDATA_BAD_DECRYPT_KEY"
    );
}

#[test]
fn passcode_without_live_wrap_is_bad_decrypt_key() {
    let (_, data) = load("tdata_plain/performance_on");
    assert!(matches!(
        open_local_key(&data, b"opentele2"),
        Err(Error::BadDecryptKey)
    ));
}

#[test]
fn empty_passcode_with_app_lock_is_bad_decrypt_key() {
    let (_, data) = load("tdata_passcode/performance_on");
    assert!(matches!(
        open_local_key(&data, b""),
        Err(Error::BadDecryptKey)
    ));
}

#[test]
fn accounts_info_failure_maps_like_start_modern() {
    let wrong = AuthKey::new([7; AuthKey::SIZE]);
    let (_, locked) = load("tdata_passcode/performance_on");
    assert!(matches!(
        decrypt_accounts_info(&locked, &wrong, b""),
        Err(Error::BadDecryptKey)
    ));
    let (_, plain) = load("tdata_plain/performance_on");
    assert!(matches!(
        decrypt_accounts_info(&plain, &wrong, b""),
        Err(Error::KeyDataInvalid { .. })
    ));
}

#[test]
fn legacy_layout_round_trips_and_opens() {
    let (_, mut data) = load("tdata_plain/performance_on");
    data.trailer = None;
    let payload = data.encode().expect("encode legacy");
    let legacy = KeyData::parse(&payload).expect("parse legacy");
    assert!(legacy.trailer.is_none());
    assert!(
        legacy.encode().expect("re-encode") == payload,
        "legacy key data does not re-encode to identical bytes"
    );
    let opened = open_local_key(&legacy, b"").expect("open legacy");
    let info = decrypt_accounts_info(&legacy, &opened.local_key, b"").expect("accounts info");
    assert_eq!(info.indices, vec![0, 1]);
}

/// One raw wrap: kind, generation, memory, time, parallelism, salt length.
type RawWrap = (u32, u32, u32, u32, u32, usize);

const ARGON2_WRAP: RawWrap = (KDF_ARGON2ID, 1, 65_536, 8, 1, 32);

fn trailer_blob(magic: u32, format: u32, committed: u32, wraps: &[RawWrap]) -> Vec<u8> {
    let mut writer = QWriter::new();
    writer.write_u32(magic);
    writer.write_u32(format);
    writer.write_u32(committed);
    writer.write_u32(u32::try_from(wraps.len()).expect("wrap count"));
    for &(kind, generation, memory, time, parallelism, salt_len) in wraps {
        for value in [kind, generation, memory, time, parallelism] {
            writer.write_u32(value);
        }
        writer
            .write_bytearray(Some(&vec![1; salt_len]))
            .expect("salt");
        writer.write_bytearray(Some(&[2; 288])).expect("key");
    }
    writer.into_bytes()
}

fn payload_with(salt_len: usize, trailer: Option<&[u8]>) -> Vec<u8> {
    let mut writer = QWriter::new();
    writer
        .write_bytearray(Some(&vec![3; salt_len]))
        .expect("salt");
    writer.write_bytearray(None).expect("open wrap");
    writer.write_bytearray(Some(&[4; 48])).expect("info");
    if let Some(blob) = trailer {
        writer.write_bytearray(Some(blob)).expect("trailer");
    }
    writer.into_bytes()
}

fn parse_trailer(blob: &[u8]) -> Result<KeyData> {
    KeyData::parse(&payload_with(32, Some(blob)))
}

#[test]
fn malformed_trailers_are_key_data_invalid() {
    let wrap = ARGON2_WRAP;
    let cases: [(&str, Vec<u8>); 7] = [
        ("bad magic", trailer_blob(0x4B44_3201, 2, 1, &[wrap])),
        ("3 wraps at format 2", {
            let wraps = [
                wrap,
                (wrap.0, 2, wrap.2, wrap.3, wrap.4, 32),
                (wrap.0, 3, wrap.2, wrap.3, wrap.4, 32),
            ];
            trailer_blob(KEY_DATA_MAGIC, 2, 1, &wraps)
        }),
        ("17 wraps at format 3", {
            let wraps: Vec<RawWrap> = (1..=17)
                .map(|g| (KDF_ARGON2ID, g, 65_536, 8, 1, 32))
                .collect();
            trailer_blob(KEY_DATA_MAGIC, 3, 1, &wraps)
        }),
        (
            "kind 0",
            trailer_blob(KEY_DATA_MAGIC, 2, 1, &[(KDF_PBKDF2, 1, 65_536, 8, 1, 32)]),
        ),
        (
            "argon2 memory below 8 * lanes",
            trailer_blob(KEY_DATA_MAGIC, 2, 1, &[(KDF_ARGON2ID, 1, 15, 8, 2, 32)]),
        ),
        (
            "short wrap salt",
            trailer_blob(KEY_DATA_MAGIC, 2, 1, &[(KDF_ARGON2ID, 1, 65_536, 8, 1, 7)]),
        ),
        (
            "duplicate generation",
            trailer_blob(KEY_DATA_MAGIC, 2, 1, &[wrap, wrap]),
        ),
    ];
    for (name, blob) in cases {
        let result = parse_trailer(&blob);
        assert!(
            matches!(result, Err(Error::KeyDataInvalid { .. })),
            "{name}: {result:?}"
        );
    }
    assert_eq!(
        code_of(parse_trailer(&trailer_blob(0, 2, 1, &[]))),
        "TDATA_KEY_DATA_INVALID"
    );
}

#[test]
fn bad_salt_and_truncation_fail() {
    let trailer = trailer_blob(KEY_DATA_MAGIC, 2, 1, &[ARGON2_WRAP]);
    assert!(matches!(
        KeyData::parse(&payload_with(31, Some(&trailer))),
        Err(Error::KeyDataInvalid { .. })
    ));
    assert!(matches!(
        parse_trailer(&trailer[..trailer.len() - 1]),
        Err(Error::QDataStreamFailed { .. })
    ));
    assert!(matches!(
        parse_trailer(&trailer[..15]),
        Err(Error::QDataStreamFailed { .. })
    ));
    let payload = payload_with(32, None);
    assert!(matches!(
        KeyData::parse(&payload[..payload.len() - 1]),
        Err(Error::QDataStreamFailed { .. })
    ));
}

#[test]
fn newer_format_reads_known_prefix_and_writes_format_2() {
    let wraps: Vec<RawWrap> = (1..=3)
        .map(|g| (KDF_SCRYPT, g, 131_072, 8, 1, 32))
        .collect();
    let mut blob = trailer_blob(KEY_DATA_MAGIC, 3, 2, &wraps);
    blob.extend_from_slice(&[9; 8]);
    let data = parse_trailer(&blob).expect("newer format parses");
    let trailer = data.trailer.as_ref().expect("trailer");
    assert_eq!(trailer.committed, 2);
    assert_eq!(trailer.wraps.len(), 3);
    assert_eq!(trailer.live_wraps().count(), 1);

    // Only the live wraps are written back.
    let live = KeyDataTrailer {
        committed: trailer.committed,
        wraps: trailer.live_wraps().cloned().collect(),
    };
    let encoded = live.encode().expect("encode trailer");
    assert_eq!(&encoded[4..8], &KEY_DATA_FORMAT_VERSION.to_be_bytes());
    assert_eq!(KeyDataTrailer::parse(&encoded).expect("reparse"), live);
}

#[test]
fn over_cap_cost_parses_and_fails_at_open_and_creation() {
    let over: RawWrap = (KDF_ARGON2ID, 1, 262_145, 8, 1, 32);
    let data =
        parse_trailer(&trailer_blob(KEY_DATA_MAGIC, 2, 1, &[over])).expect("over-cap parses");
    assert!(matches!(
        open_local_key(&data, b"opentele2"),
        Err(Error::BadDecryptKey)
    ));
    let wrap = &data.trailer.as_ref().expect("trailer").wraps[0];
    assert!(!wrap.kdf.cost_within_limits());
    assert!(matches!(
        create_passcode_key(b"opentele2", &wrap.salt, &wrap.kdf),
        Err(Error::KeyDataInvalid { .. })
    ));
}

#[test]
fn create_passcode_key_rejects_unusable_inputs() {
    let kdf = default_passcode_kdf();
    let salt = [5; 32];
    assert!(matches!(
        create_passcode_key(b"", &salt, &kdf),
        Err(Error::KeyDataInvalid { .. })
    ));
    assert!(matches!(
        create_passcode_key(b"x", &salt[..7], &kdf),
        Err(Error::KeyDataInvalid { .. })
    ));
    let pbkdf2 = PasscodeKdf {
        kind: KDF_PBKDF2,
        ..kdf
    };
    assert!(matches!(
        create_passcode_key(b"x", &salt, &pbkdf2),
        Err(Error::KeyDataInvalid { .. })
    ));
}

#[test]
fn kdf_validity_and_limits() {
    let kdf = default_passcode_kdf();
    assert_eq!(
        (
            kdf.kind,
            kdf.generation,
            kdf.memory,
            kdf.time,
            kdf.parallelism
        ),
        (KDF_ARGON2ID, 1, 65_536, 8, 1)
    );
    assert!(kdf.valid() && kdf.cost_within_limits());
    let scrypt = PasscodeKdf {
        kind: KDF_SCRYPT,
        generation: 1,
        memory: 131_072,
        time: 8,
        parallelism: 1,
    };
    assert!(scrypt.valid() && scrypt.cost_within_limits());
    assert!(
        !PasscodeKdf {
            memory: 131_071,
            ..scrypt
        }
        .valid()
    );
    assert!(!PasscodeKdf { time: 0, ..scrypt }.valid());
    assert!(
        !PasscodeKdf {
            parallelism: 17,
            ..scrypt
        }
        .cost_within_limits()
    );
    assert!(
        !PasscodeKdf {
            memory: (1 << 20) * 2,
            ..scrypt
        }
        .cost_within_limits()
    );
    assert!(!PasscodeKdf { time: 65, ..kdf }.cost_within_limits());
    assert!(PasscodeKdf { kind: 9, ..kdf }.cost_within_limits());
    assert!(!PasscodeKdf { kind: 9, ..kdf }.valid());
}

#[test]
fn argon2id_wrap_vector_wraps_and_unwraps() {
    let case = &manifest("crypto.json")["argon2id_wrap"];
    let bytes = |key: &str| hex::decode(case[key].as_str().expect("hex field")).expect("hex");
    let local = AuthKey::from_slice(&bytes("local_key")).expect("local key");
    let wrap_key = AuthKey::from_slice(&bytes("wrap_key")).expect("wrap key");
    let wrapped = wrap_local_key(&local, &wrap_key, &bytes("padding")).expect("wrap");
    assert!(
        wrapped == bytes("key_encrypted"),
        "wrapped key differs from the vector"
    );
    let unwrapped = unwrap_local_key(&wrapped, &wrap_key).expect("unwrap");
    assert!(
        unwrapped.as_bytes() == local.as_bytes(),
        "unwrapped key differs"
    );

    assert!(matches!(
        unwrap_local_key(&wrapped, &local),
        Err(Error::BadDecryptKey)
    ));
    assert!(matches!(
        unwrap_local_key(&wrapped[..16], &wrap_key),
        Err(Error::BadDecryptKey)
    ));
    assert!(matches!(
        wrap_local_key(&local, &wrap_key, &[0; 11]),
        Err(Error::BadLength { .. })
    ));
}

#[test]
fn accounts_info_decoding() {
    let encode = |values: &[i32]| {
        let mut writer = QWriter::new();
        for &value in values {
            writer.write_i32(value);
        }
        writer.into_bytes()
    };
    let info = AccountsInfo::parse(&encode(&[2, 0, 3, 3])).expect("with active");
    assert_eq!((info.indices, info.active_index), (vec![0, 3], 3));
    let info = AccountsInfo::parse(&encode(&[1, 4])).expect("without active");
    assert_eq!((info.indices, info.active_index), (vec![4], 0));
    assert!(matches!(
        AccountsInfo::parse(&encode(&[0])),
        Err(Error::KeyDataInvalid { .. })
    ));
    assert!(matches!(
        AccountsInfo::parse(&encode(&[-1])),
        Err(Error::KeyDataInvalid { .. })
    ));
    assert!(matches!(
        AccountsInfo::parse(&encode(&[i32::MAX, 1])),
        Err(Error::QDataStreamFailed { .. })
    ));
}