Skip to content

miniproto_native::crypto

miniproto_native / crypto


Registers cryptographic Python callables. Native cryptographic primitives used by MTProto and its Python fallback-compatible API.

Each #[pyfunction] is exported under its Rust name in miniproto._native. Selected wrappers detach for large workloads, while *_raw functions operate without Python objects. PyO3 conversion errors propagate unchanged, and algorithm or byte-shape validation returns ValueError. Python wrappers may select a fallback even when these native exports are available.

fn detach_if_large<T, F>(py: Python<'_>, work_bytes: usize, f: F) -> T
where
T: Ungil,
F: Ungil + FnOnce() -> T

Defined in rust/miniproto/src/crypto.rs:50-60

Runs f without the GIL when its input work estimate exceeds the native threshold.

work_bytes controls thread detachment, and f must satisfy PyO3’s Ungil requirements. Detachment releases the GIL on GIL-enabled builds. The computation remains synchronous.

  • T: The GIL-independent result type returned by f.
  • F: The one-shot GIL-independent closure performing the native operation.
  • py: The acquired GIL token that can detach f.
  • work_bytes: Conservative byte-work estimate compared with the detach threshold.
  • f: The native computation to run attached or detached.
fn register(m: &Bound<'_, pyo3::types::PyModule>) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:69-89

Registers this module’s fallback-compatible Python callables on miniproto._native.

Returns a PyO3 exception if a callable cannot be added to m.

  • m: The Python extension module receiving the crypto callables.
fn native_available() -> bool

Defined in rust/miniproto/src/crypto.rs:94-96

Implements Python native_available, which always returns true while this compiled callable is importable. Python fallback selection happens before this function can be called.

unsafe fn __pyfunction_native_available<'py>(py: ::pyo3::Python<'py>, _slf: *mut ::pyo3::ffi::PyObject) -> ::pyo3::PyResult<*mut ::pyo3::ffi::PyObject>

Defined in rust/miniproto/src/crypto.rs:93

fn sha1_digest(py: Python<'_>, data: std::borrow::Cow<'_, [u8]>) -> Vec<u8>

Defined in rust/miniproto/src/crypto.rs:107-109

Computes Python sha1_digest(data) and returns the 20-byte SHA-1 digest.

Large inputs are hashed with the GIL released.

  • py: The acquired GIL token used to detach a large hash operation.
  • data: Bytes to hash.
unsafe fn __pyfunction_sha1_digest<'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/crypto.rs:106

fn sha256_digest(py: Python<'_>, data: std::borrow::Cow<'_, [u8]>) -> Vec<u8>

Defined in rust/miniproto/src/crypto.rs:120-122

Computes Python sha256_digest(data) and returns the 32-byte SHA-256 digest.

Large inputs are hashed with the GIL released.

  • py: The acquired GIL token used to detach a large hash operation.
  • data: Bytes to hash.
unsafe fn __pyfunction_sha256_digest<'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/crypto.rs:119

fn mtproto_auth_key_id(auth_key: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:132-134

Computes Python mtproto_auth_key_id(auth_key) from a validated 256-byte key.

Returns ValueError when auth_key has the wrong length.

  • auth_key: The 256-byte MTProto authorization key.
unsafe fn __pyfunction_mtproto_auth_key_id<'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/crypto.rs:131

fn mtproto_message_key(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, plaintext_with_padding: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:148-163

Computes Python mtproto_message_key for padded plaintext and one MTProto direction.

Returns a 16-byte message key; rejects an invalid authorization key and releases the GIL for large inputs.

  • py: The acquired GIL token used to detach a large hash operation.
  • auth_key: The 256-byte MTProto authorization key.
  • plaintext_with_padding: Inner plaintext including padding, with no alignment or padding validation here
  • client_to_server: Selects the directional MTProto key offset.
unsafe fn __pyfunction_mtproto_message_key<'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/crypto.rs:147

fn mtproto_derive_aes_key_iv(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, msg_key: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<(Vec<u8>, Vec<u8>)>

Defined in rust/miniproto/src/crypto.rs:176-192

Derives the AES-256 key and IV used by Python mtproto_derive_aes_key_iv.

Returns (key, iv) after validating both inputs. Valid inputs total 272 bytes (auth_key 256 plus msg_key 16), so successful derivations run below the detach threshold.

  • py: The acquired GIL token retained by this bounded-size operation.
  • auth_key: The 256-byte MTProto authorization key.
  • msg_key: The 16-byte MTProto message key.
  • client_to_server: Selects the directional MTProto key offset.
unsafe fn __pyfunction_mtproto_derive_aes_key_iv<'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/crypto.rs:175

fn mtproto_encrypt_payload(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, plaintext_with_padding: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<(Vec<u8>, Vec<u8>, Vec<u8>)>

Defined in rust/miniproto/src/crypto.rs:206-216

Encrypts padded MTProto plaintext for Python mtproto_encrypt_payload.

Returns (auth_key_id, msg_key, ciphertext) or ValueError for invalid key or block input; large work runs without the GIL.

  • py: The acquired GIL token used to detach large encryption work.
  • auth_key: The 256-byte MTProto authorization key.
  • plaintext_with_padding: AES-block-aligned inner plaintext to encrypt.
  • client_to_server: Selects the directional MTProto key schedule.
unsafe fn __pyfunction_mtproto_encrypt_payload<'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/crypto.rs:205

fn mtproto_decrypt_payload(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, msg_key: std::borrow::Cow<'_, [u8]>, ciphertext: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:231-242

Decrypts and verifies Python mtproto_decrypt_payload ciphertext.

Returns padded plaintext or ValueError for invalid lengths, key material or message-key verification; large work runs without the GIL.

  • py: The acquired GIL token used to detach large decryption work.
  • auth_key: The 256-byte MTProto authorization key.
  • msg_key: The 16-byte message key to verify.
  • ciphertext: AES-IGE ciphertext whose length must be an AES-block multiple.
  • client_to_server: Selects the directional MTProto key schedule.
unsafe fn __pyfunction_mtproto_decrypt_payload<'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/crypto.rs:230

fn xor_bytes(left: &[u8], right: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:253-255

Returns the bytewise exclusive-or of Python xor_bytes(left, right) inputs.

Returns ValueError unless both inputs have equal length.

  • left: First equal-length byte sequence.
  • right: Second equal-length byte sequence.
unsafe fn __pyfunction_xor_bytes<'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/crypto.rs:252

fn aes_256_ige_encrypt(py: Python<'_>, plaintext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:269-279

Encrypts block-aligned bytes with Python aes_256_ige_encrypt.

Requires a 32-byte key and IV; returns ValueError for invalid lengths and releases the GIL for large plaintexts.

  • py: The acquired GIL token used to detach large encryption work.
  • plaintext: AES-block-aligned bytes to encrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 32-byte AES-IGE initialization vector.
unsafe fn __pyfunction_aes_256_ige_encrypt<'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/crypto.rs:268

fn aes_256_ige_decrypt(py: Python<'_>, ciphertext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:293-303

Decrypts block-aligned bytes with Python aes_256_ige_decrypt.

Requires a 32-byte key and IV; returns ValueError for invalid lengths and releases the GIL for large ciphertexts.

  • py: The acquired GIL token used to detach large decryption work.
  • ciphertext: AES-block-aligned bytes to decrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 32-byte AES-IGE initialization vector.
unsafe fn __pyfunction_aes_256_ige_decrypt<'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/crypto.rs:292

fn aes_256_cbc_encrypt(py: Python<'_>, plaintext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:317-327

Encrypts block-aligned bytes with Python aes_256_cbc_encrypt without padding.

Requires 32-byte key and 16-byte IV inputs; invalid lengths become ValueError and large plaintexts release the GIL.

  • py: The acquired GIL token used to detach large encryption work.
  • plaintext: AES-block-aligned bytes to encrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte CBC initialization vector.
unsafe fn __pyfunction_aes_256_cbc_encrypt<'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/crypto.rs:316

fn aes_256_cbc_decrypt(py: Python<'_>, ciphertext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:341-351

Decrypts block-aligned bytes with Python aes_256_cbc_decrypt without padding.

Requires 32-byte key and 16-byte IV inputs; invalid lengths become ValueError and large ciphertexts release the GIL.

  • py: The acquired GIL token used to detach large decryption work.
  • ciphertext: AES-block-aligned bytes to decrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte CBC initialization vector.
unsafe fn __pyfunction_aes_256_cbc_decrypt<'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/crypto.rs:340

fn aes_256_ctr_crypt(py: Python<'_>, data: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:365-375

Applies Python aes_256_ctr_crypt to data using AES-CTR keystream XOR.

The same routine encrypts and decrypts; it validates the 32-byte key and 16-byte counter IV, and releases the GIL for large data.

  • py: The acquired GIL token used to detach large counter-mode work.
  • data: Bytes to encrypt or decrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte initial counter block.
unsafe fn __pyfunction_aes_256_ctr_crypt<'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/crypto.rs:364

fn aes_256_gcm_encrypt(py: Python<'_>, plaintext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, nonce: std::borrow::Cow<'_, [u8]>, associated_data: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:390-401

Authenticated-encrypts Python aes_256_gcm_encrypt plaintext and associated data.

Returns ciphertext followed by its GCM tag or ValueError for a bad 32-byte key, 12-byte nonce or encryption failure; large work releases the GIL.

  • py: The acquired GIL token used to detach large authenticated-encryption work.
  • plaintext: Bytes to encrypt.
  • key: The 32-byte AES-256 key.
  • nonce: The 12-byte GCM nonce.
  • associated_data: Authenticated bytes that are not encrypted.
unsafe fn __pyfunction_aes_256_gcm_encrypt<'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/crypto.rs:389

fn aes_256_gcm_decrypt(py: Python<'_>, ciphertext_and_tag: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, nonce: std::borrow::Cow<'_, [u8]>, associated_data: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:416-427

Authenticated-decrypts Python aes_256_gcm_decrypt ciphertext-and-tag bytes.

Returns plaintext or ValueError for invalid key/nonce material or authentication failure; large work releases the GIL.

  • py: The acquired GIL token used to detach large authenticated-decryption work.
  • ciphertext_and_tag: GCM ciphertext followed by its authentication tag.
  • key: The 32-byte AES-256 key.
  • nonce: The 12-byte GCM nonce.
  • associated_data: Authenticated bytes that are not encrypted.
unsafe fn __pyfunction_aes_256_gcm_decrypt<'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/crypto.rs:415

fn scrypt_derive(py: Python<'_>, password: std::borrow::Cow<'_, [u8]>, salt: std::borrow::Cow<'_, [u8]>, n: u32, r: u32, p: u32, length: usize) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:445-462

Derives Python scrypt_derive bytes from password, salt and scrypt cost parameters.

n must be a power of two above one and length is limited to 1..=1024; invalid parameters return ValueError. Estimated memory is capped at 256 MiB and aggregate work at 1 GiB. The GIL is released when the 128 * n * r * p work estimate exceeds 4 KiB.

  • py: The acquired GIL token used to detach only sufficiently large scrypt work.
  • password: Password bytes accepted by scrypt.
  • salt: Salt bytes accepted by scrypt.
  • n: CPU/memory cost, required to be a power of two greater than one.
  • r: scrypt block-size cost parameter.
  • p: scrypt parallelization cost parameter.
  • length: Requested derived-key length in the inclusive range 1..=1024.
unsafe fn __pyfunction_scrypt_derive<'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/crypto.rs:444

fn pq_factorize(py: Python<'_>, pq: u64) -> PyResult<(u64, u64)>

Defined in rust/miniproto/src/crypto.rs:474-476

Factorizes Python pq_factorize(pq) into ordered nontrivial u64 factors.

Returns ValueError for non-composite values and runs the potentially expensive search with the GIL released.

  • py: The acquired GIL token used to detach the factorization search.
  • pq: Composite MTProto handshake value to split into ordered factors.
unsafe fn __pyfunction_pq_factorize<'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/crypto.rs:473

fn sha1_digest_raw(data: &[u8]) -> Vec<u8>

Defined in rust/miniproto/src/crypto.rs:483-485

Computes SHA-1 for Rust callers without Python or GIL interaction.

  • data: Bytes to hash.
fn sha256_digest_raw(data: &[u8]) -> Vec<u8>

Defined in rust/miniproto/src/crypto.rs:492-494

Computes SHA-256 for Rust callers without Python or GIL interaction.

  • data: Bytes to hash.
fn mtproto_auth_key_id_raw(auth_key: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:503-507

Derives the trailing eight SHA-1 bytes that identify a validated MTProto authorization key.

Returns ValueError if the key is not exactly 256 bytes; does not acquire the GIL.

  • auth_key: Authorization key whose SHA-1 tail supplies the identifier.
fn mtproto_message_key_raw(auth_key: &[u8], plaintext_with_padding: &[u8], client_to_server: bool) -> Vec<u8>

Defined in rust/miniproto/src/crypto.rs:519-526

Derives an MTProto 2.0 message key from a validated authorization key and padded plaintext.

Callers must validate the authorization-key length before use because this internal primitive slices its fixed protocol ranges directly.

  • auth_key: Previously validated 256-byte authorization key.
  • plaintext_with_padding: Padded inner plaintext to include in the SHA-256 derivation.
  • client_to_server: Selects the directional key offset.
fn mtproto_message_key_large_raw(auth_key: &[u8], plaintext_with_padding: &[u8], client_to_server: bool) -> [u8; 32]

Defined in rust/miniproto/src/crypto.rs:535-545

Computes the directional digest shared by the message key and quick acknowledgement.

  • auth_key: Previously validated 256-byte authorization key.
  • plaintext_with_padding: Entire unencrypted MTProto body including padding.
  • client_to_server: Selects the sender’s key offset.
fn mtproto_derive_aes_key_iv_raw(auth_key: &[u8], msg_key: &[u8], client_to_server: bool) -> (Vec<u8>, Vec<u8>)

Defined in rust/miniproto/src/crypto.rs:556-582

Derives the MTProto 2.0 AES-IGE key and IV from validated fixed-width key material.

Callers must validate auth_key and msg_key before calling; the function has no GIL use.

  • auth_key: Previously validated 256-byte authorization key.
  • msg_key: Previously validated 16-byte message key.
  • client_to_server: Selects the directional key offset.
fn mtproto_encrypt_payload_raw(auth_key: &[u8], plaintext_with_padding: &[u8], client_to_server: bool) -> PyResult<(Vec<u8>, Vec<u8>, Vec<u8>)>

Defined in rust/miniproto/src/crypto.rs:593-605

Produces the auth-key identifier, message key and AES-IGE ciphertext for padded plaintext.

Returns ValueError for malformed keys or non-block-aligned plaintext; no GIL interaction.

  • auth_key: 256-byte authorization key used for id, message key and AES derivation.
  • plaintext_with_padding: AES-block-aligned inner plaintext to encrypt.
  • client_to_server: Selects the directional MTProto key schedule.
fn mtproto_decrypt_payload_raw(auth_key: &[u8], msg_key: &[u8], ciphertext: &[u8], client_to_server: bool) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:617-634

Decrypts and authenticates an MTProto payload using validated directional key derivation.

Returns ValueError for malformed inputs or a message-key mismatch; no GIL interaction.

  • auth_key: 256-byte authorization key used for directional AES derivation.
  • msg_key: 16-byte message key expected after decryption.
  • ciphertext: AES-block-aligned encrypted payload.
  • client_to_server: Selects the directional MTProto key schedule.
fn xor_bytes_raw(left: &[u8], right: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:644-651

Computes bytewise XOR for equal-length Rust byte slices.

Returns ValueError for unequal lengths and does not use the GIL.

  • left: First equal-length byte slice.
  • right: Second equal-length byte slice.
fn aes_256_ige_encrypt_raw(plaintext: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:662-670

Encrypts a block-aligned byte slice using AES-256 IGE for Rust callers.

Returns ValueError unless the key, IV and plaintext lengths meet AES-IGE requirements.

  • plaintext: AES-block-aligned bytes to encrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 32-byte AES-IGE initialization vector.
fn aes_256_ige_encrypt_in_place(data: &mut [u8], key: &[u8], iv: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:679-701

Encrypts the padded payload in its final MTProto packet buffer.

  • data: Block-aligned payload replaced in place with ciphertext.
  • key: The 32-byte AES-256 key.
  • iv: The 32-byte IGE initialization vector.
fn aes_256_ige_decrypt_raw(ciphertext: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:712-738

Decrypts a block-aligned AES-256 IGE ciphertext for Rust callers.

Returns ValueError unless the key, IV and ciphertext lengths meet AES-IGE requirements.

  • ciphertext: AES-block-aligned bytes to decrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 32-byte AES-IGE initialization vector.
fn aes_256_cbc_encrypt_raw(plaintext: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:749-768

Encrypts a block-aligned byte slice with unpadded AES-256 CBC.

Returns ValueError unless the key, IV and plaintext lengths are valid.

  • plaintext: AES-block-aligned bytes to encrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte CBC initialization vector.
fn aes_256_cbc_decrypt_raw(ciphertext: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:779-805

Decrypts a block-aligned unpadded AES-256 CBC ciphertext.

Returns ValueError unless the key, IV and ciphertext lengths are valid.

  • ciphertext: AES-block-aligned bytes to decrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte CBC initialization vector.
fn aes_256_ctr_crypt_raw(data: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:816-838

XORs bytes with an AES-256 CTR keystream; the same operation encrypts and decrypts.

Returns ValueError unless key is 32 bytes and iv is one AES block.

  • data: Bytes to XOR with the generated CTR keystream.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte initial counter block.
fn aes_256_gcm_encrypt_raw(plaintext: &[u8], key: &[u8], nonce: &[u8], associated_data: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:850-871

Authenticated-encrypts plaintext with AES-256 GCM and returns ciphertext plus tag.

Returns ValueError for an invalid key or nonce or if the cipher rejects the operation.

  • plaintext: Bytes to encrypt.
  • key: The 32-byte AES-256 key.
  • nonce: The 12-byte GCM nonce.
  • associated_data: Authenticated bytes that are not encrypted.
fn aes_256_gcm_decrypt_raw(ciphertext_and_tag: &[u8], key: &[u8], nonce: &[u8], associated_data: &[u8]) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:883-904

Authenticated-decrypts AES-256 GCM ciphertext-and-tag data.

Returns ValueError for invalid key/nonce material or a failed authentication check.

  • ciphertext_and_tag: GCM ciphertext followed by its authentication tag.
  • key: The 32-byte AES-256 key.
  • nonce: The 12-byte GCM nonce.
  • associated_data: Authenticated bytes that are not encrypted.
fn scrypt_derive_raw(password: &[u8], salt: &[u8], n: u32, r: u32, p: u32, length: usize) -> PyResult<Vec<u8>>

Defined in rust/miniproto/src/crypto.rs:919-955

Runs scrypt with the library’s output and resource limits for Rust callers

Returns ValueError for invalid cost parameters, an unsupported output length or derivation failure. This is a synchronous, GIL-free primitive.

  • password: Password bytes accepted by scrypt.
  • salt: Salt bytes accepted by scrypt.
  • n: CPU/memory cost, required to be a power of two greater than one.
  • r: scrypt block-size cost parameter.
  • p: scrypt parallelization cost parameter.
  • length: Requested derived-key length in the inclusive range 1..=1024.
fn pq_factorize_raw(pq: u64) -> PyResult<(u64, u64)>

Defined in rust/miniproto/src/crypto.rs:964-979

Finds and orders the two nontrivial factors of an MTProto pq value.

Returns ValueError for values below four, primes or an exhausted factorization budget

  • pq: Candidate composite integer to split into its two ordered factors.
fn validate_auth_key(auth_key: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:988-993

Validates the fixed 256-byte MTProto authorization-key width.

Returns ValueError rather than allowing fixed-offset protocol code to panic.

  • auth_key: Candidate authorization key expected to contain 256 bytes.
fn validate_msg_key(msg_key: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:1002-1007

Validates the fixed 16-byte MTProto message-key width.

Returns ValueError on a malformed input slice.

  • msg_key: Candidate message key expected to contain 16 bytes.
fn validate_block_multiple(data: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:1016-1023

Validates that AES block-mode input has a whole-number count of AES blocks.

Returns ValueError when the byte length is not divisible by 16.

  • data: Candidate AES block-mode input.
fn validate_aes_key(key: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:1030-1035

Validates a 32-byte AES-256 key and returns ValueError otherwise.

  • key: Candidate AES-256 key.
fn validate_ige_iv(iv: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:1042-1047

Validates the two-block, 32-byte IV required by AES-IGE.

  • iv: Candidate AES-IGE initialization vector.
fn validate_cbc_ctr_iv(iv: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:1054-1059

Validates the one-block IV or counter used by CBC and CTR modes.

  • iv: Candidate CBC initialization vector or CTR counter block.
fn validate_gcm_nonce(nonce: &[u8]) -> PyResult<()>

Defined in rust/miniproto/src/crypto.rs:1066-1071

Validates the 12-byte nonce mandated by this AES-GCM interface.

  • nonce: Candidate AES-GCM nonce.
fn direction_offset(client_to_server: bool) -> usize

Defined in rust/miniproto/src/crypto.rs:1078-1080

Returns the MTProto key-schedule offset for the requested packet direction.

  • client_to_server: Whether the packet travels from client to server.
fn xor_block(left: &[u8; 16], right: &[u8; 16]) -> [u8; 16]

Defined in rust/miniproto/src/crypto.rs:1088-1094

Computes a fixed-width XOR block used by the AES block-mode loops.

  • left: First AES-sized block.
  • right: Second AES-sized block.
fn gcd(left: u64, right: u64) -> u64

Defined in rust/miniproto/src/crypto.rs:1102-1109

Computes the greatest common divisor used by Pollard-rho factorization.

  • left: First nonnegative integer.
  • right: Second nonnegative integer.
fn mul_mod(left: u64, right: u64, modulus: u64) -> u64

Defined in rust/miniproto/src/crypto.rs:1122-1124

Multiplies modulo modulus with a widened intermediate to avoid u64 overflow.

A zero modulus panics at the remainder operation. Widening the product to u128 prevents multiplication overflow for u64 inputs.

  • left: First modular multiplicand.
  • right: Second modular multiplicand.
  • modulus: Nonzero modulus used for the reduced product.
fn pow_mod(base: u64, exponent: u64, modulus: u64) -> u64

Defined in rust/miniproto/src/crypto.rs:1137-1147

Computes modular exponentiation for deterministic Miller-Rabin witnesses.

A zero modulus panics when exponent is nonzero. Products use mul_mod and its widened intermediate to avoid multiplication overflow.

  • base: Base to exponentiate modulo modulus.
  • exponent: Nonnegative exponent encoded as u64.
  • modulus: Nonzero modulus used for every reduction.
fn is_prime_u64(value: u64) -> bool

Defined in rust/miniproto/src/crypto.rs:1154-1193

Deterministically tests whether a u64 is prime using fixed Miller-Rabin witnesses.

  • value: Unsigned integer to classify as prime or composite.
fn factor_u64(value: u64, attempts: usize, iterations: usize) -> Option<u64>

Defined in rust/miniproto/src/crypto.rs:1202-1229

Returns one factor using bounded Pollard-rho iteration, the input when prime or None when exhausted

  • value: Composite unsigned integer whose nontrivial factor is requested.
  • attempts: Maximum number of polynomial constants to try
  • iterations: Maximum sequence iterations for each constant
const AES_BLOCK_SIZE: usize = 16usize;

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

AES’s fixed block size in bytes.

const MT_PROTO_AUTH_KEY_SIZE: usize = 256usize;

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

Required byte length of an MTProto authorization key.

const MT_PROTO_MSG_KEY_SIZE: usize = 16usize;

Defined in rust/miniproto/src/crypto.rs:27

Required byte length of an MTProto 2.0 message key.

const GIL_RELEASE_THRESHOLD_BYTES: usize = 4_096usize;

Defined in rust/miniproto/src/crypto.rs:29

Work-size threshold above which native wrappers detach from the Python GIL.

const SCRYPT_MAX_MEMORY_BYTES: usize = 268_435_456usize;

Defined in rust/miniproto/src/crypto.rs:31

Maximum memory estimate accepted by the public scrypt primitive.

const SCRYPT_MAX_WORK_BYTES: usize = 1_073_741_824usize;

Defined in rust/miniproto/src/crypto.rs:33

Maximum CPU-work estimate accepted by the public scrypt primitive.

const PQ_FACTOR_ATTEMPTS: usize = 16usize;

Defined in rust/miniproto/src/crypto.rs:35

Maximum Pollard-rho retries, matching the Python fallback

const PQ_FACTOR_ITERATIONS: usize = 100_000usize;

Defined in rust/miniproto/src/crypto.rs:37

Maximum Pollard-rho iterations per retry, matching the Python fallback