Skip to main content

grammers_crypto/
obfuscated.rs

1// Copyright 2020 - developers of the `grammers` project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#[allow(deprecated)] // see https://github.com/RustCrypto/block-ciphers/issues/509
10use aes::cipher::{KeyIvInit, StreamCipher, generic_array::GenericArray};
11
12/// This implements the AES-256-CTR cipher used by Telegram to encrypt data
13/// when using the obfuscated transport.
14///
15/// It is not intended to be used directly.
16pub struct ObfuscatedCipher {
17    rx: ctr::Ctr128BE<aes::Aes256>,
18    tx: ctr::Ctr128BE<aes::Aes256>,
19}
20
21impl ObfuscatedCipher {
22    pub fn new(init: &[u8; 64]) -> Self {
23        let init_rev = init.iter().copied().rev().collect::<Vec<_>>();
24        #[allow(deprecated)] // see https://github.com/RustCrypto/block-ciphers/issues/509
25        Self {
26            rx: ctr::Ctr128BE::<aes::Aes256>::new(
27                GenericArray::from_slice(&init_rev[8..40]),
28                GenericArray::from_slice(&init_rev[40..56]),
29            ),
30            tx: ctr::Ctr128BE::<aes::Aes256>::new(
31                GenericArray::from_slice(&init[8..40]),
32                GenericArray::from_slice(&init[40..56]),
33            ),
34        }
35    }
36
37    pub fn encrypt(&mut self, buffer: &mut [u8]) {
38        self.tx.apply_keystream(buffer);
39    }
40
41    pub fn decrypt(&mut self, buffer: &mut [u8]) {
42        self.rx.apply_keystream(buffer);
43    }
44}