-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the obfuscated MTProto transport
- Loading branch information
Showing
10 changed files
with
317 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2020 - developers of the `grammers` project. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use aes::cipher::{generic_array::GenericArray, KeyIvInit, StreamCipher, StreamCipherSeek}; | ||
|
||
/// This implements the AES-256-CTR cipher used by Telegram to encrypt data | ||
/// when using the obfuscated transport. | ||
/// | ||
/// You're not supposed to use this directly, You're probably looking for the | ||
/// actual implementation in `grammers-mtproto`. | ||
pub struct ObfuscatedCipher { | ||
rx: ctr::Ctr128BE<aes::Aes256>, | ||
tx: ctr::Ctr128BE<aes::Aes256>, | ||
} | ||
|
||
impl ObfuscatedCipher { | ||
pub fn new(init: &[u8; 64]) -> Self { | ||
let init_rev = init.iter().copied().rev().collect::<Vec<_>>(); | ||
Self { | ||
rx: ctr::Ctr128BE::<aes::Aes256>::new( | ||
GenericArray::from_slice(&init_rev[8..40]), | ||
GenericArray::from_slice(&init_rev[40..56]), | ||
), | ||
tx: ctr::Ctr128BE::<aes::Aes256>::new( | ||
GenericArray::from_slice(&init[8..40]), | ||
GenericArray::from_slice(&init[40..56]), | ||
), | ||
} | ||
} | ||
|
||
pub fn encrypt(&mut self, buffer: &mut [u8]) { | ||
self.tx.apply_keystream(buffer); | ||
} | ||
|
||
pub fn decrypt(&mut self, buffer: &mut [u8]) { | ||
self.rx.apply_keystream(buffer); | ||
} | ||
|
||
/// Undo the decryption of the last `decrypt` call. | ||
/// The buffer must be a slice to the end from the same buffer that | ||
/// was last passed to `decrypt`. | ||
pub fn undo_decrypt(&mut self, buffer: &mut [u8]) { | ||
let pos = self | ||
.rx | ||
.current_pos::<u128>() | ||
.wrapping_sub(buffer.len() as u128); | ||
self.rx.seek(pos); | ||
self.rx.apply_keystream(buffer); | ||
self.rx.seek(pos); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_revert() { | ||
let mut cipher = ObfuscatedCipher::new(&[0x69; 64]); | ||
let orignial = [0x42; 128]; | ||
let mut buffer = orignial.clone(); | ||
|
||
cipher.decrypt(&mut buffer); | ||
cipher.undo_decrypt(&mut buffer); | ||
assert_eq!(&buffer, &orignial); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.