miniproto_native::mtproto
Provenance
Section titled “Provenance”- Rust visibility:
crate - Source:
rust/miniproto/src/mtproto.rs - Python exposure: Not evidenced by static PyO3 attributes.
Documented items
Section titled “Documented items”miniproto_native::mtproto::mtproto_decode_messageminiproto_native::mtproto::mtproto_encode_messageminiproto_native::mtproto::mtproto_encode_message_with_ackminiproto_native::mtproto::register
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.
Structs
Section titled “Structs”DecodedEncryptedMessage
Section titled “DecodedEncryptedMessage”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.
Fields
Section titled “Fields”-
auth_key_id:Vec<u8>Eight-byte identifier derived from the packet’s authentication key.
-
server_salt:u64Server salt carried by the envelope.
-
session_id:u64Session identifier carried by the envelope.
-
msg_id:i64MTProto message identifier carried by the envelope.
-
seq_no:i32MTProto 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.
Trait Implementations
Section titled “Trait Implementations”impl Debug for DecodedEncryptedMessage
Section titled “impl Debug for DecodedEncryptedMessage”fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
impl Eq for DecodedEncryptedMessage
Section titled “impl Eq for DecodedEncryptedMessage”impl PartialEq for DecodedEncryptedMessage
Section titled “impl PartialEq for DecodedEncryptedMessage”fn eq(&self, other: &Self) -> bool
impl Same for DecodedEncryptedMessage
Section titled “impl Same for DecodedEncryptedMessage”type Output = T
impl StructuralPartialEq for DecodedEncryptedMessage
Section titled “impl StructuralPartialEq for DecodedEncryptedMessage”impl Ungil for DecodedEncryptedMessage
Section titled “impl Ungil for DecodedEncryptedMessage”EnvelopeEncodeInput<'a>
Section titled “EnvelopeEncodeInput<'a>”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.
Fields
Section titled “Fields”-
auth_key:&'a [u8]The 256-byte MTProto authentication key.
-
server_salt:u64Salt to serialize in the envelope.
-
session_id:u64Session identifier to serialize in the envelope.
-
msg_id:i64Message identifier to serialize in the envelope.
-
seq_no:i32Sequence number to serialize in the envelope.
-
body:&'a [u8]Four-byte-aligned application body to serialize.
-
client_to_server:boolWhether the packet direction is client-to-server for MTProto key derivation.
-
padding:Option<&'a [u8]>Optional caller-selected padding;
Nonerequests securely random valid padding.
Trait Implementations
Section titled “Trait Implementations”impl Same for EnvelopeEncodeInput<'a>
Section titled “impl Same for EnvelopeEncodeInput<'a>”type Output = T
impl Ungil for EnvelopeEncodeInput<'a>
Section titled “impl Ungil for EnvelopeEncodeInput<'a>”Functions
Section titled “Functions”register
Section titled “register”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.
Arguments
Section titled “Arguments”m: The Python extension module to receive the envelope callables.
mtproto_encode_message
Section titled “mtproto_encode_message”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.
Arguments
Section titled “Arguments”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;Nonerequests operating-system random padding.
__pyfunction_mtproto_encode_message
Section titled “__pyfunction_mtproto_encode_message”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
mtproto_encode_message_with_ack
Section titled “mtproto_encode_message_with_ack”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.
Arguments
Section titled “Arguments”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
mtproto_decode_message
Section titled “mtproto_decode_message”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.
Arguments
Section titled “Arguments”py: The acquired GIL token used only to detach large native work.auth_key: The 256-byte authorization key used to verify and decryptpacket.packet: Full encrypted MTProto packet including auth-key id and message key.client_to_server: Selects the directional MTProto key schedule used for verification.
__pyfunction_mtproto_decode_message
Section titled “__pyfunction_mtproto_decode_message”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
encode_with_ack
Section titled “encode_with_ack”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.
Arguments
Section titled “Arguments”input: Borrowed envelope fields and key material for this packet.
mtproto_decode_message_raw
Section titled “mtproto_decode_message_raw”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.
Arguments
Section titled “Arguments”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.
random_padding
Section titled “random_padding”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.
Arguments
Section titled “Arguments”plaintext_len: Inner-envelope byte length before random padding; it must be representable after adding the selected padding length.
padding_len
Section titled “padding_len”fn padding_len(plaintext_len: usize) -> usizeDefined in rust/miniproto/src/mtproto.rs:370-373
Computes the minimum valid padding length for an inner plaintext length.
Arguments
Section titled “Arguments”plaintext_len: Inner-envelope byte length before padding; callers must ensureplaintext_len + MIN_PADDING_LENcannot overflowusize, because this arithmetic helper deliberately has no fallible return path.
validate_padding
Section titled “validate_padding”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.
Arguments
Section titled “Arguments”plaintext_len: Inner-envelope byte length before padding.padding: Candidate trailing random padding to validate.
Type Aliases
Section titled “Type Aliases”PyDecodedEnvelope
Section titled “PyDecodedEnvelope”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.
Constants
Section titled “Constants”ENCRYPTED_PACKET_HEADER_LEN
Section titled “ENCRYPTED_PACKET_HEADER_LEN”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.
ENVELOPE_HEADER_LEN
Section titled “ENVELOPE_HEADER_LEN”const ENVELOPE_HEADER_LEN: usize = 32usize;Defined in rust/miniproto/src/mtproto.rs:21
Bytes in an unencrypted inner envelope before its application body.
MIN_PADDING_LEN
Section titled “MIN_PADDING_LEN”const MIN_PADDING_LEN: usize = 12usize;Defined in rust/miniproto/src/mtproto.rs:23
Smallest MTProto 2.0 random-padding length, in bytes.
MAX_PADDING_LEN
Section titled “MAX_PADDING_LEN”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.