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.

Item Kind Description
DecodedEncryptedMessage struct Decoded contents of a validated encrypted MTProto packet for Rust-internal callers.
EnvelopeEncodeInput struct Borrowed inputs used to build one encrypted MTProto envelope without Python interaction.
register fn Adds the Python-visible MTProto envelope functions to miniproto._native.
mtproto_encode_message fn Encodes one encrypted MTProto envelope as Python mtproto_encode_message.
__pyfunction_mtproto_encode_message fn
mtproto_decode_message fn Decodes Python mtproto_decode_message packet bytes into its seven-element envelope tuple.
__pyfunction_mtproto_decode_message fn
mtproto_encode_message_raw fn Builds and encrypts an MTProto envelope from already borrowed Rust inputs.
mtproto_decode_message_raw fn Verifies, decrypts and parses an encrypted MTProto packet for Rust callers.
random_padding fn Generates random MTProto padding that completes plaintext_len to an AES block boundary.
padding_len fn Computes the minimum valid padding length for an inner plaintext length.
validate_padding fn Validates MTProto 2.0 padding bounds and the resulting AES block alignment.
PyDecodedEnvelope type Python tuple returned by mtproto_decode_message in envelope field order.
ENCRYPTED_PACKET_HEADER_LEN const Bytes preceding ciphertext in an encrypted MTProto packet: auth-key id plus message key.
ENVELOPE_HEADER_LEN const Bytes in an unencrypted inner envelope before its application body.
MIN_PADDING_LEN const Smallest MTProto 2.0 random-padding length, in bytes.
MAX_PADDING_LEN const Largest accepted MTProto 2.0 random-padding length, in bytes.
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:27-42

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:45-62

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:74-78

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: Vec<u8>, server_salt: u64, session_id: u64, msg_id: i64, seq_no: i32, body: Vec<u8>, client_to_server: bool, padding: Option<Vec<u8>>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/mtproto.rs:106-130

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:95-104

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

Defined in rust/miniproto/src/mtproto.rs:146-165

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:145

fn mtproto_encode_message_raw(input: EnvelopeEncodeInput<'_>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/mtproto.rs:176-209

Builds and encrypts an MTProto envelope from already borrowed Rust inputs.

Returns the complete encrypted packet or a Python ValueError for invalid protocol inputs or unavailable operating-system randomness. Callers must provide a body that is four-byte aligned; this function does not interact with the GIL.

  • input: Borrowed authorization key and envelope fields to serialize and encrypt.
fn mtproto_decode_message_raw(auth_key: &[u8], packet: &[u8], client_to_server: bool) -> PyResult<DecodedEncryptedMessage>

Defined in rust/miniproto/src/mtproto.rs:220-278

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:288-293

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:302-305

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:317-329

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:65

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:17

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:19

Bytes in an unencrypted inner envelope before its application body.

const MIN_PADDING_LEN: usize = 12usize;

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

Smallest MTProto 2.0 random-padding length, in bytes.

const MAX_PADDING_LEN: usize = 1_024usize;

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

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