Skip to main content

ARM-CE / AES-NI accelerated Telegram crypto via OpenSSL EVP

Project description

โšก cipheron

ARM-CE / AES-NI accelerated Telegram crypto via OpenSSL EVP

PyPI License: MIT Python GitHub Stars

Drop-in replacement for tgcrypto, cryptg, and cryptogram: with 5โ€“6ร— faster IGE by routing AES through OpenSSL's hardware engine instead of software T-tables.


๐Ÿ’ฌ A personal note

I originally built cipheron in 2024 for my own personal use: I was frustrated that Telegram crypto on ARM devices (Termux, Android, Raspberry Pi) was bottlenecked by software AES even though the hardware was perfectly capable of going 5ร— faster. So I fixed it. ๐Ÿ˜„

If cipheron helps speed up your bots, userbots, or Telegram clients: don't forget to โญ star the repo, it means a lot!
And do check out cryptogram (PyPI): the excellent project that inspired this work. cipheron builds on the same idea and takes it further with full OpenSSL EVP dispatch.


๐Ÿง  Why cipheron exists

Every Telegram message, file chunk, and MTProto packet is encrypted with AES-256-IGE. Popular libraries like tgcrypto and cryptogram implement IGE using AES_encrypt(): a software path that bypasses ARM Crypto Extensions and AES-NI entirely.

cipheron fixes this by routing all AES operations through OpenSSL's EVP_CipherUpdate(), which dispatches to hardware automatically:

EVP_CipherUpdate()  โ†’  OpenSSL engine dispatch  โ†’  aes_v8_encrypt (ARM-CE)  โ†’  843 MB/s โœ…
AES_encrypt()       โ†’  software T-table path    โ†’  ~130 MB/s  โ† tgcrypto / cryptogram stuck here โŒ

On a device with ARM Crypto Extensions confirmed at 843 MB/s CBC, cipheron IGE lands at ~600โ€“800 MB/s: making crypto essentially free and your network the only bottleneck.


๐Ÿ“ฆ Installation

pip install cipheron

Termux / Android (ARM):

pkg install clang openssl python
pip install cipheron

Verify hardware acceleration:

python -c "import cipheron; print(cipheron.get_backend(), '|', cipheron.has_aesni())"
# C/EVP+ARM-CE | True

๐Ÿ”Œ Drop-in replacement: Pyrogram & Telethon

Method 1: sys.modules hijack โœ… recommended

Add two lines before any Pyrogram/Telethon import: zero other code changes needed:

import sys
import cipheron

# Pyrogram uses tgcrypto internally
sys.modules['tgcrypto'] = cipheron

# Telethon uses cryptg internally
sys.modules['cryptg'] = cipheron

# Now import normally โ€” cipheron handles all crypto transparently โšก
from pyrogram import Client
# or
from telethon import TelegramClient

Method 2: Direct API (same as tgcrypto)

import cipheron

# IGE-256 โ€” used for every MTProto message & file chunk
encrypted = cipheron.ige256_encrypt(data, key, iv)
decrypted = cipheron.ige256_decrypt(encrypted, key, iv)

# CTR-256 โ€” obfuscated transport layer
out, state = cipheron.ctr256_encrypt(data, key, iv, state)
out, state = cipheron.ctr256_decrypt(data, key, iv, state)

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

# PQ factorization โ€” Telegram handshake (unique to cipheron!)
p, q = cipheron.factorize_pq_pair(pq_int)

# Runtime detection
print(cipheron.has_aesni())    # True on ARM-CE / AES-NI hardware
print(cipheron.get_backend())  # "C/EVP+ARM-CE"

Method 3: Replace all frameworks at once

import sys, cipheron

for name in ('tgcrypto', 'cryptg', 'cryptogram'):
    sys.modules[name] = cipheron

# Works transparently with any Telegram framework โœ…

๐Ÿš€ Benchmark Results

ARM Device (Termux / Android): ARM Crypto Extensions active

openssl speed -evp aes-256-cbc โ†’ 843 MB/s  โ† hardware AES confirmed โœ…
843 MB/s = ARM Crypto Extensions are 100% active!

โ”€โ”€ IGE ENCRYPT (MB/s) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  Size        tgcrypto    cryptogram    cipheron      speedup
  16 KB         ~130          ~100       ~750 โ–ฒ       5.8ร— ๐Ÿ†
  256 KB        ~125           ~85       ~680 โ–ฒ       5.4ร— ๐Ÿ†
  1 MB          ~128           ~90       ~700 โ–ฒ       5.5ร— ๐Ÿ†
  8 MB          ~130           ~88       ~720 โ–ฒ       5.5ร— ๐Ÿ†

โ”€โ”€ CTR ENCRYPT (MB/s) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  16 KB         ~150          ~800       ~840 โ–ฒ       5.6ร— ๐Ÿ†
  256 KB        ~148          ~810       ~830 โ–ฒ       5.6ร— ๐Ÿ†

โ”€โ”€ CBC ENCRYPT (MB/s) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  16 KB         ~155          ~820       ~843 โ–ฒ       5.4ร— ๐Ÿ†

Correctness: IGE โœ“ PASS   CTR โœ“ PASS   CBC โœ“ PASS

Why 843 MB/s matters: cipheron routes IGE through the same hardware path as CBC. That's the entire trick: and it makes all the difference.

x86 Desktop (AES-NI): Verified benchmark

โ”€โ”€ IGE ENCRYPT (MB/s) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  Size        tgcrypto    cryptogram    cipheron      speedup
  16 KB          172         102          792 โ–ฒ       4.60ร— ๐Ÿ†
  256 KB         133          87          353 โ–ฒ       2.65ร— ๐Ÿ†
  1 MB           140          90          418 โ–ฒ       2.99ร— ๐Ÿ†
  8 MB           144          90          422 โ–ฒ       2.93ร— ๐Ÿ†

โ”€โ”€ CTR ENCRYPT (MB/s) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  16 KB          153         964 โ–ฒ        957 โ–ฒ       6.27ร— ๐Ÿ†
  256 KB         152         967 โ–ฒ        963 โ–ฒ       6.33ร— ๐Ÿ†

โ”€โ”€ CBC ENCRYPT (MB/s) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  16 KB          180         970 โ–ฒ        972 โ–ฒ       5.39ร— ๐Ÿ†

Correctness: IGE โœ“ PASS   CTR โœ“ PASS

Run your own benchmark

import os, time, cipheron, tgcrypto

KEY = os.urandom(32)
IV  = os.urandom(32)
D   = os.urandom(1024 * 1024)
N   = 100

t = time.perf_counter()
for _ in range(N): cipheron.ige256_encrypt(D, KEY, IV)
print(f'cipheron : {N / (time.perf_counter() - t) * 1024:.0f} MB/s')

t = time.perf_counter()
for _ in range(N): tgcrypto.ige256_encrypt(D, KEY, IV)
print(f'tgcrypto : {N / (time.perf_counter() - t) * 1024:.0f} MB/s')

๐Ÿ“Š Real-world impact

Use case Mode cipheron vs tgcrypto
Telegram file upload / download IGE 5โ€“6ร— faster on ARM
Every MTProto message IGE 5โ€“6ร— faster
Pyrogram obfuscated transport CTR 4โ€“6ร— faster
Telegram CDN file chunks CTR 4โ€“6ร— faster
Bot handling 1000s of msgs/sec IGE biggest real-world win
Connection handshake PQ only cipheron has it built-in
Heavy userbot (files + messages) all modes cipheron wins everything

Each Telegram file transfer splits into 512 KB IGE-encrypted chunks. At ~800 MB/s vs ~130 MB/s, crypto overhead per chunk drops from ~4ms to ~0.6ms: your CPU is essentially free and the network is your only limit.


๐Ÿ“š API Reference

Function Description
ige256_encrypt(data, key, iv) AES-256 IGE encrypt
ige256_decrypt(data, key, iv) AES-256 IGE decrypt
ctr256_encrypt(data, key, iv, state) AES-256 CTR encrypt
ctr256_decrypt(data, key, iv, state) AES-256 CTR decrypt
cbc256_encrypt(data, key, iv) AES-256 CBC encrypt
cbc256_decrypt(data, key, iv) AES-256 CBC decrypt
encrypt_ige(data, key, iv) Alias โ†’ ige256_encrypt
decrypt_ige(data, key, iv) Alias โ†’ ige256_decrypt
factorize_pq_pair(pq) RSA PQ factorization (Telegram handshake)
has_aesni() True if hardware AES is available
get_backend() Backend string e.g. "C/EVP+ARM-CE"

๐Ÿ“„ License

MIT ยฉ 2024-Present Ankit Chaubey


๐Ÿ‘จโ€๐Ÿ’ป Developed by

Ankit Chaubey
๐Ÿ“ง ankitchaubey.dev@gmail.com
๐ŸŒ github.com/ankit-chaubey/cipheron


If cipheron made your Telegram bots or scripts faster: drop a โญ star, it genuinely helps!

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

cipheron-0.2.1.tar.gz (14.6 kB view details)

Uploaded Source

File details

Details for the file cipheron-0.2.1.tar.gz.

File metadata

  • Download URL: cipheron-0.2.1.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cipheron-0.2.1.tar.gz
Algorithm Hash digest
SHA256 55ef1e841c5a6c76f79b1cb915a0fedb6425f4094dbadccbebae1babb59dcade
MD5 c1309fc44c251ac8b018645d9899731b
BLAKE2b-256 ed699a5adc4ce08cc6cd7a697d214394ab68d4b30073c1fe59ed21681d507c1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cipheron-0.2.1.tar.gz:

Publisher: publish.yml on ankit-chaubey/cipheron

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