ferogram-mtproto 0.6.5

MTProto 2.0 session management, message framing, DH key exchange and transport abstractions
Documentation

ferogram-mtproto

MTProto 2.0 session management, DH key exchange, and message framing for Rust.

Crates.io Telegram Channel Telegram Chat docs.rs License TL Layer

The MTProto session layer. Handles everything from raw bytes to decrypted, sequenced messages. ferogram sits on top of this; most users don't need to depend on it directly.

For installation instructions see the ferogram README.


What it handles

  • 3-step DH key exchange (req_pq_multireq_DH_paramsset_client_DH_params)
  • Encrypted sessions: AES-IGE pack/unpack, msg_key derivation, salt management
  • Message framing: salt, session_id, message_id, sequence numbers
  • msg_container and gzip_packed unwrapping
  • Error recovery: bad_msg_notification, bad_server_salt, msg_resend_req
  • Acknowledgements via MsgsAck
  • Temporary key binding for PFS (bind_temp_key module)

Core Types

EncryptedSession

Manages the live MTProto session after key exchange.

use ferogram_mtproto::EncryptedSession;

let session = EncryptedSession::new(auth_key, first_salt, time_offset);

let wire_bytes = session.pack(&my_request)?;
let wire_bytes = session.pack_serializable(&raw_obj)?;
let msg = session.unpack(&mut raw_bytes)?;

3-Step DH Handshake

use ferogram_mtproto::authentication as auth;

let (req1, state1) = auth::step1()?;
// send req1, receive res_pq

let (req2, state2) = auth::step2(state1, res_pq)?;
// send req2, receive server_DH_params

let (req3, state3) = auth::step3(state2, dh_params)?;
// send req3, receive dh_answer

let done = auth::finish(state3, dh_answer)?;
// done.auth_key    [u8; 256]
// done.first_salt  i64
// done.time_offset i32

Message

pub struct Message {
    pub msg_id:  i64,
    pub seq_no:  i32,
    pub salt:    i64,
    pub body:    Vec<u8>,  // raw TL bytes
}

bind_temp_key

Implements PFS temporary key binding.

use ferogram_mtproto::{serialize_bind_temp_auth_key, encrypt_bind_inner, gen_msg_id};

let wire = serialize_bind_temp_auth_key(
    &perm_auth_key,
    &temp_auth_key,
    nonce,
    expires_at,
    msg_id,
    seq_no,
)?;

Stack position

ferogram
└ ferogram-mtproto  <-- here
  ├ ferogram-tl-types (tl-mtproto feature)
  └ ferogram-crypto

License

This project is licensed under either the MIT License or Apache License 2.0, at your option. See LICENSE-MIT and LICENSE-APACHE for details.

Author: Ankit Chaubey (@ankit-chaubey)