Skip to main content

ferogram_crypto/
rsa.rs

1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3//
4// ferogram: async Telegram MTProto client in Rust
5// https://github.com/ankit-chaubey/ferogram
6//
7//
8// If you use or modify this code, keep this notice at the top of your file
9// and include the LICENSE-MIT or LICENSE-APACHE file from this repository:
10// https://github.com/ankit-chaubey/ferogram
11
12//! RSA padding used by Telegram's auth key exchange.
13
14use crate::{aes, sha256};
15use num_bigint::BigUint;
16
17/// An RSA public key (n, e).
18pub struct Key {
19    n: BigUint,
20    e: BigUint,
21}
22
23impl Key {
24    /// Parse decimal `n` and `e` strings.
25    pub fn new(n: &str, e: &str) -> Option<Self> {
26        Some(Self {
27            n: BigUint::parse_bytes(n.as_bytes(), 10)?,
28            e: BigUint::parse_bytes(e.as_bytes(), 10)?,
29        })
30    }
31}
32
33fn increment(data: &mut [u8]) {
34    let mut i = data.len() - 1;
35    loop {
36        let (n, overflow) = data[i].overflowing_add(1);
37        data[i] = n;
38        if overflow {
39            i = i.checked_sub(1).unwrap_or(data.len() - 1);
40        } else {
41            break;
42        }
43    }
44}
45
46/// RSA-encrypt `data` using the MTProto RSA-PAD scheme.
47///
48/// `random_bytes` must be exactly 224 bytes of secure random data.
49/// `data` must be ≤ 144 bytes.
50pub fn encrypt_hashed(data: &[u8], key: &Key, random_bytes: &[u8; 224]) -> Vec<u8> {
51    assert!(data.len() <= 144, "data too large for RSA-PAD");
52
53    // data_with_padding: 192 bytes
54    let mut data_with_padding = Vec::with_capacity(192);
55    data_with_padding.extend_from_slice(data);
56    data_with_padding.extend_from_slice(&random_bytes[..192 - data.len()]);
57
58    // data_pad_reversed
59    let data_pad_reversed: Vec<u8> = data_with_padding.iter().copied().rev().collect();
60
61    let mut temp_key: [u8; 32] = random_bytes[192..].try_into().unwrap();
62
63    let key_aes_encrypted = loop {
64        // data_with_hash = data_pad_reversed + SHA256(temp_key + data_with_padding)
65        let mut data_with_hash = Vec::with_capacity(224);
66        data_with_hash.extend_from_slice(&data_pad_reversed);
67        data_with_hash.extend_from_slice(&sha256!(&temp_key, &data_with_padding));
68
69        aes::ige_encrypt(&mut data_with_hash, &temp_key, &[0u8; 32]);
70
71        // temp_key_xor = temp_key XOR SHA256(aes_encrypted)
72        let hash = sha256!(&data_with_hash);
73        let mut xored = temp_key;
74        for (a, b) in xored.iter_mut().zip(hash.iter()) {
75            *a ^= b;
76        }
77
78        let mut candidate = Vec::with_capacity(256);
79        candidate.extend_from_slice(&xored);
80        candidate.extend_from_slice(&data_with_hash);
81
82        if BigUint::from_bytes_be(&candidate) < key.n {
83            break candidate;
84        }
85        increment(&mut temp_key);
86    };
87
88    let payload = BigUint::from_bytes_be(&key_aes_encrypted);
89    let encrypted = payload.modpow(&key.e, &key.n);
90    let mut block = encrypted.to_bytes_be();
91    while block.len() < 256 {
92        block.insert(0, 0);
93    }
94    block
95}