Skip to content

ctr256_encrypt/decrypt reject bytearray for iv/state — breaks pyrogram and forks #4

Description

@illvart

What's wrong

ctr256_encrypt and ctr256_decrypt only accept bytes for iv/state. pyrogram (and kurigram, pyrofork, etc) always pass bytearray, so calling this library from pyrogram-based projects crashes:

import tgcrypto, os

key = os.urandom(32)
iv = bytearray(os.urandom(16))
state = bytearray(1)

tgcrypto.ctr256_encrypt(b"data", key, iv, state)
TypeError: 'bytearray' object is not an instance of 'bytes'

Why it matters

pyrogram uses bytearray for iv/state on purpose — it mutates them in place to carry the CTR counter across chunks (needed for its obfuscated TCP transport and CDN file downloads). This means the crash happens right at connection time, so a pyrogram/kurigram bot can't connect to Telegram at all with this library installed as the crypto backend — not just an edge case.

What would fix it

  • Accept bytearray for iv/state (matching how TgCrypto's own docs show it being used), and
  • Make sure the counter state can actually be carried forward between calls — right now the function only returns ciphertext, with no way to continue a stream across multiple calls.

Happy to test a fix if useful.

Environment

  • TgCryptoRust 1.3.0
  • Python 3.12.3 and Python 3.14.7 (both reproduced, clean uv venvs)
  • Reproduced against pyrogram (official, latest) and kurigram 2.2.24

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions