Skip to content

miniproto_native::mtproto

miniproto_native / mtproto


Registers encrypted MTProto message-envelope callables. Native encoding and decoding of encrypted MTProto 2.0 message envelopes.

These exports use the Python fallback’s argument and result layout. PyO3 conversion errors propagate unchanged, and protocol validation returns ValueError. Large workloads detach from Python, releasing the GIL on GIL-enabled builds.

struct DecodedEncryptedMessage {
pub auth_key_id: Vec<u8>,
pub server_salt: u64,
pub session_id: u64,
pub msg_id: i64,
pub seq_no: i32,
pub body: Vec<u8>,
pub padding: Vec<u8>,
}

Defined in rust/miniproto/src/mtproto.rs:29-44

Decoded contents of a validated encrypted MTProto packet for Rust-internal callers.

  • auth_key_id: Vec<u8>

    Eight-byte identifier derived from the packet’s authentication key.

  • server_salt: u64

    Server salt carried by the envelope.

  • session_id: u64

    Session identifier carried by the envelope.

  • msg_id: i64

    MTProto message identifier carried by the envelope.

  • seq_no: i32

    MTProto sequence number carried by the envelope.

  • body: Vec<u8>

    Application message body, excluding the envelope and random padding.

  • padding: Vec<u8>

    Validated random padding trailing the body.

  • fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
impl PartialEq for DecodedEncryptedMessage
Section titled “impl PartialEq for DecodedEncryptedMessage”
  • fn eq(&self, other: &Self) -> bool
  • type Output = T
impl StructuralPartialEq for DecodedEncryptedMessage
Section titled “impl StructuralPartialEq for DecodedEncryptedMessage”
struct EnvelopeEncodeInput<'a> {
pub auth_key: &'a [u8],
pub server_salt: u64,
pub session_id: u64,
pub msg_id: i64,
pub seq_no: i32,
pub body: &'a [u8],
pub client_to_server: bool,
pub padding: Option<&'a [u8]>,
}

Defined in rust/miniproto/src/mtproto.rs:47-64

Borrowed inputs used to build one encrypted MTProto envelope without Python interaction.

  • auth_key: &'a [u8]

    The 256-byte MTProto authentication key.

  • server_salt: u64

    Salt to serialize in the envelope.

  • session_id: u64

    Session identifier to serialize in the envelope.

  • msg_id: i64

    Message identifier to serialize in the envelope.

  • seq_no: i32

    Sequence number to serialize in the envelope.

  • body: &'a [u8]

    Four-byte-aligned application body to serialize.

  • client_to_server: bool

    Whether the packet direction is client-to-server for MTProto key derivation.

  • padding: Option<&'a [u8]>

    Optional caller-selected padding; None requests securely random valid padding.

  • type Output = T
fn register(m: &Bound<'_, pyo3::types::PyModule>) -> PyResult<()>

Defined in rust/miniproto/src/mtproto.rs:76-81

Adds the Python-visible MTProto envelope functions to miniproto._native.

Returns a Python exception when PyO3 cannot register either callable.

  • m: The Python extension module to receive the envelope callables.
fn mtproto_encode_message(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, server_salt: u64, session_id: u64, msg_id: i64, seq_no: i32, body: std::borrow::Cow<'_, [u8]>, client_to_server: bool, padding: Option<std::borrow::Cow<'_, [u8]>>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/mtproto.rs:109-132

Encodes one encrypted MTProto envelope as Python mtproto_encode_message.

Serializes the salt, session ID, message ID, sequence number, body length, body and padding, then encrypts them with auth_key. Returns the auth-key ID, message key and ciphertext in wire order. client_to_server selects the key schedule, and explicit padding replaces generated randomness. Invalid key, body or padding values raise ValueError, while integer conversion errors propagate from PyO3. Large workloads detach from Python.

  • py: The acquired GIL token used only to detach large native work.
  • auth_key: The 256-byte authorization key used to encrypt, but not serialize, the envelope.
  • server_salt: The salt serialized at the start of the inner envelope.
  • session_id: The session identifier serialized in the inner envelope.
  • msg_id: The MTProto message identifier serialized in the inner envelope.
  • seq_no: The MTProto sequence number serialized in the inner envelope.
  • body: Four-byte-aligned application bytes serialized before the padding.
  • client_to_server: Selects the directional MTProto key schedule.
  • padding: Optional explicit padding; None requests operating-system random padding.
unsafe fn __pyfunction_mtproto_encode_message<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>

Defined in rust/miniproto/src/mtproto.rs:98-107

fn mtproto_encode_message_with_ack(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, server_salt: u64, session_id: u64, msg_id: i64, seq_no: i32, body: std::borrow::Cow<'_, [u8]>, client_to_server: bool, padding: Option<std::borrow::Cow<'_, [u8]>>) -> PyResult<(Vec<u8>, u32)>

Defined in rust/miniproto/src/mtproto.rs:158-182

Encodes one envelope and returns the receipt token derived with its message key.

  • py: Python token for detaching large encryption operations.
  • auth_key: 256-byte authorization key for the envelope.
  • server_salt: Unsigned 64-bit server salt.
  • session_id: Unsigned 64-bit session identifier.
  • msg_id: Signed MTProto message identifier.
  • seq_no: Message sequence number.
  • body: Four-byte-aligned TL body.
  • client_to_server: Selects the directional MTProto key schedule.
  • padding: Explicit MTProto padding, or None to generate it.

__pyfunction_mtproto_encode_message_with_ack

Section titled “__pyfunction_mtproto_encode_message_with_ack”
unsafe fn __pyfunction_mtproto_encode_message_with_ack<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>

Defined in rust/miniproto/src/mtproto.rs:147-156

fn mtproto_decode_message(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, packet: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<(Vec<u8>, u64, u64, i64, i32, Vec<u8>, Vec<u8>)>

Defined in rust/miniproto/src/mtproto.rs:198-217

Decodes Python mtproto_decode_message packet bytes into its seven-element envelope tuple.

client_to_server selects the direction used to verify and decrypt packet. Returns (auth_key_id, server_salt, session_id, msg_id, seq_no, body, padding) or ValueError if the wire packet, keys, body length, message key or padding is invalid. Large workloads run with the GIL released.

  • py: The acquired GIL token used only to detach large native work.
  • auth_key: The 256-byte authorization key used to verify and decrypt packet.
  • packet: Full encrypted MTProto packet including auth-key id and message key.
  • client_to_server: Selects the directional MTProto key schedule used for verification.
unsafe fn __pyfunction_mtproto_decode_message<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>

Defined in rust/miniproto/src/mtproto.rs:197

fn encode_with_ack(input: EnvelopeEncodeInput<'_>) -> PyResult<(Vec<u8>, u32)>

Defined in rust/miniproto/src/mtproto.rs:238-275

Build the inner envelope in the final packet buffer and encrypt its payload in place.

  • input: Borrowed envelope fields and key material for this packet.
fn mtproto_decode_message_raw(auth_key: &[u8], packet: &[u8], client_to_server: bool) -> PyResult<DecodedEncryptedMessage>

Defined in rust/miniproto/src/mtproto.rs:286-346

Verifies, decrypts and parses an encrypted MTProto packet for Rust callers.

Returns envelope fields and padding after checking the key identifiers, message key, body length and padding bounds. Failures return ValueError. Callers validate session IDs, message IDs and sequence semantics separately.

  • auth_key: Valid 256-byte authorization key expected by the packet.
  • packet: Complete encrypted MTProto packet to authenticate and parse.
  • client_to_server: Selects the directional MTProto key schedule.
fn random_padding(plaintext_len: usize) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/mtproto.rs:356-361

Generates random MTProto padding that completes plaintext_len to an AES block boundary.

Returns ValueError if the operating-system randomness source fails.

  • plaintext_len: Inner-envelope byte length before random padding; it must be representable after adding the selected padding length.
fn padding_len(plaintext_len: usize) -> usize

Defined in rust/miniproto/src/mtproto.rs:370-373

Computes the minimum valid padding length for an inner plaintext length.

  • plaintext_len: Inner-envelope byte length before padding; callers must ensure plaintext_len + MIN_PADDING_LEN cannot overflow usize, because this arithmetic helper deliberately has no fallible return path.
fn validate_padding(plaintext_len: usize, padding: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/mtproto.rs:385-397

Validates MTProto 2.0 padding bounds and the resulting AES block alignment.

Returns ValueError instead of panicking when the padding length is invalid. Callers must ensure plaintext_len + padding.len() is representable; that sum is evaluated directly before the modulus check.

  • plaintext_len: Inner-envelope byte length before padding.
  • padding: Candidate trailing random padding to validate.
type PyDecodedEnvelope = (Vec<u8>, u64, u64, i64, i32, Vec<u8>, Vec<u8>);

Defined in rust/miniproto/src/mtproto.rs:67

Python tuple returned by mtproto_decode_message in envelope field order.

const ENCRYPTED_PACKET_HEADER_LEN: usize = 24usize;

Defined in rust/miniproto/src/mtproto.rs:19

Bytes preceding ciphertext in an encrypted MTProto packet: auth-key id plus message key.

const ENVELOPE_HEADER_LEN: usize = 32usize;

Defined in rust/miniproto/src/mtproto.rs:21

Bytes in an unencrypted inner envelope before its application body.

const MIN_PADDING_LEN: usize = 12usize;

Defined in rust/miniproto/src/mtproto.rs:23

Smallest MTProto 2.0 random-padding length, in bytes.

const MAX_PADDING_LEN: usize = 1_024usize;

Defined in rust/miniproto/src/mtproto.rs:25

Largest accepted MTProto 2.0 random-padding length, in bytes.