Skip to main content

High-performance Rust-powered TgCrypto module providing secure and optimized MTProto cryptography for Pyrogram.

Project description

tgcrypto-rs

A high-performance Rust implementation of the tgcrypto Python extension module for Pyrogram.

This module provides cryptographic primitives required for Telegram's MTProto protocol, implemented in Rust for optimal performance and security.

Features

  • AES-256-IGE encryption/decryption
  • AES-256-CTR encryption/decryption
  • AES-256-CBC encryption/decryption
  • SHA-1 hashing
  • SHA-256 hashing
  • RSA encryption with Telegram server public keys
  • Pollard's rho integer factorization for MTProto handshake
  • MTProto helpers (session ID generation)

Recent Updates

  • PyO3 0.28+: Updated to the latest PyO3 for better performance and support for Python 3.13.
  • Modern Dependencies: Updated all cryptographic and utility crates to their latest stable versions.
  • Enhanced Type Hints: Improved .pyi file for better IDE support.
  • Modern CI/CD: Updated GitHub Actions to use latest tools and secure publishing.

Performance Comparison

Benchmarks were performed on 1MB data chunks (Android, AArch64).

Operation Official (C) Rust (Current) Ratio (Rust/C)
AES-IGE 88.33 MB/s 196.82 MB/s 2.23x
AES-CTR 73.09 MB/s 163.55 MB/s 2.24x
AES-CBC 89.76 MB/s 436.91 MB/s 4.87x

Note: Rust now uses OpenSSL's AES backend for these measurements on ARM.

Installation

Prerequisites

  • Rust toolchain (install via rustup)
  • Python 3.8+
  • maturin for building Python extensions
pip install maturin

Build and Install

cd pyrogram-tgcrypto
maturin develop --release

This will compile the Rust code and install the tgcrypto module into your current Python environment.

Usage

Once installed, the module can be imported directly in Python:

import tgcrypto

# AES-256-IGE
encrypted = tgcrypto.ige256_encrypt(data, key, iv)
decrypted = tgcrypto.ige256_decrypt(encrypted, key, iv)

# AES-256-CTR
encrypted = tgcrypto.ctr256_encrypt(data, key, iv, state)
decrypted = tgcrypto.ctr256_decrypt(encrypted, key, iv, state)

# AES-256-CBC
encrypted = tgcrypto.cbc256_encrypt(data, key, iv)
decrypted = tgcrypto.cbc256_decrypt(encrypted, key, iv)

# Hashing
sha1_hash = tgcrypto.sha1(data)
sha256_hash = tgcrypto.sha256(data)

# RSA encryption
encrypted = tgcrypto.rsa_encrypt(data, fingerprint)

# Factorization
factor = tgcrypto.factorize(pq)

# Session ID
session_id = tgcrypto.get_session_id(auth_key)

API Reference

ige256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Encrypt data using AES-256 in IGE mode.

  • data: Must be a multiple of 16 bytes
  • key: Must be 32 bytes
  • iv: Must be 32 bytes

ige256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Decrypt data using AES-256 in IGE mode.

ctr256_encrypt(data: bytes, key: bytes, iv: bytes, state: int) -> bytes

Encrypt data using AES-256 in CTR mode.

  • data: Any length
  • key: Must be 32 bytes
  • iv: Must be 16 bytes
  • state: Counter state offset

ctr256_decrypt(data: bytes, key: bytes, iv: bytes, state: int) -> bytes

Decrypt data using AES-256 in CTR mode.

cbc256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Encrypt data using AES-256 in CBC mode.

  • data: Must be a multiple of 16 bytes
  • key: Must be 32 bytes
  • iv: Must be 16 bytes

cbc256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Decrypt data using AES-256 in CBC mode.

sha1(data: bytes) -> bytes

Compute SHA-1 hash of data. Returns 20 bytes.

sha256(data: bytes) -> bytes

Compute SHA-256 hash of data. Returns 32 bytes.

rsa_encrypt(data: bytes, fingerprint: int) -> bytes

Encrypt data using RSA with Telegram server public key.

  • data: Data to encrypt
  • fingerprint: Telegram server key fingerprint (e.g., -4344800451088585951)

Returns 256-byte encrypted data.

factorize(pq: int) -> int

Find a non-trivial factor of a semiprime number using Pollard's rho algorithm. Used in MTProto key exchange.

get_session_id(auth_key: bytes) -> bytes

Generate session ID from authentication key. Returns 8 bytes.

Performance

This Rust implementation provides significant performance improvements over pure Python implementations:

  • AES operations: ~10-50x faster
  • Hashing: ~5-20x faster
  • Factorization: ~100x+ faster for large numbers

The GIL is released during heavy cryptographic operations, allowing true parallelism in multi-threaded applications.

Security

This implementation uses well-audited cryptographic crates:

  • aes - AES block cipher
  • ctr - CTR mode
  • cbc - CBC mode
  • sha1 - SHA-1 hash
  • sha2 - SHA-2 family hashes
  • num-bigint - Big integer arithmetic

No unsafe code is used for cryptographic operations.

License

LGPL-3.0-or-later (same as original tgcrypto)

Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tgcrypto_rs-2.1.14.tar.gz (66.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tgcrypto_rs-2.1.14-cp314-cp314-macosx_11_0_arm64.whl (267.1 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

tgcrypto_rs-2.1.14-cp314-cp314-macosx_10_12_x86_64.whl (290.2 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

tgcrypto_rs-2.1.14-cp312-cp312-win_amd64.whl (186.3 kB view details)

Uploaded CPython 3.12Windows x86-64

tgcrypto_rs-2.1.14-cp312-cp312-manylinux_2_38_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.38+ x86-64

tgcrypto_rs-2.1.14-cp312-cp312-manylinux_2_38_aarch64.whl (2.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.38+ ARM64

File details

Details for the file tgcrypto_rs-2.1.14.tar.gz.

File metadata

  • Download URL: tgcrypto_rs-2.1.14.tar.gz
  • Upload date:
  • Size: 66.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tgcrypto_rs-2.1.14.tar.gz
Algorithm Hash digest
SHA256 54253d03ff8d6777b3f5d41744e7a7de6b4ec45042a9b16c4d64d5cf5addd1db
MD5 d7b026bcff05efa4a948ff4d57f59844
BLAKE2b-256 82608f977be807a699a403606d0410bef4f88f6a8b765f1910e3a2a11b4794ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.14.tar.gz:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.14-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.14-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8f0336dcf7baceb6c3a92e6990683998a02a8a293c192ce0a3b24ceccf17c164
MD5 8391bc185ffb91cfee1126768b22e92c
BLAKE2b-256 3cb4b905068000b4458d319d00f0bee372c742329712e25c52ee73fbf22212d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.14-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.14-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.14-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3943cbfef21e278fdeef5a90a86a3b7ed9769d0e4e3ae21d46fe891e0a70fdba
MD5 74df261843b5dd61392e53cc6dfedb53
BLAKE2b-256 adb7adf8b8abec6b074d16151388cf3988382b3353b3d3f7e3a3ed882fde93f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.14-cp314-cp314-macosx_10_12_x86_64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.14-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.14-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 77252c914a490bd0417bb1454148af546add90c1c548130f5540a11d345fac47
MD5 282758d6f14dcc9f3a901252ebbbbddb
BLAKE2b-256 50650ac7e643df568efc93e79b61fdcbdadc4ddacacfe439de70bc1415d543f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.14-cp312-cp312-win_amd64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.14-cp312-cp312-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.14-cp312-cp312-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 3757757a31fea7184eed2556af9886584ca0ab55b5023f5a015e8336b77dc965
MD5 aea7bfac8ef74b2a574d74ad585ad169
BLAKE2b-256 da9827510507e569b6e300eb39fdc842d671847332bc217b6995073b9dcc0f95

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.14-cp312-cp312-manylinux_2_38_x86_64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.14-cp312-cp312-manylinux_2_38_aarch64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.14-cp312-cp312-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 d51b5d8b56b48ca9fb71d2658034c04bcab0e11b192f85c94ed821a5d269c520
MD5 84f5daf8317719bf4273fdf3804baf58
BLAKE2b-256 318c32108e5082e49ded53336976cb56bd5e66b5a5b55417e846770eba5f7fc9

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.14-cp312-cp312-manylinux_2_38_aarch64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page