cryptogram 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ankit Chaubey <ankitchaubey.dev@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ include README.md
2
+ include LICENSE
3
+ include cryptogram/_cryptogram.c
4
+ include cryptogram/_fallback.py
5
+ include cryptogram/__init__.py
6
+ recursive-include tests *.py
@@ -0,0 +1,214 @@
1
+ Metadata-Version: 2.4
2
+ Name: cryptogram
3
+ Version: 0.1.0
4
+ Summary: Ultra-fast AES-256 cryptography for Telegram MTProto — AES-NI accelerated, API-compatible with tgcrypto and cryptg
5
+ Author-email: Ankit Chaubey <ankitchaubey.dev@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/ankit-chaubey/cryptogram
8
+ Project-URL: Repository, https://github.com/ankit-chaubey/cryptogram
9
+ Project-URL: Bug Tracker, https://github.com/ankit-chaubey/cryptogram/issues
10
+ Keywords: telegram,mtproto,aes,cryptography,telethon,pyrogram,aesni
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: C
21
+ Classifier: Topic :: Security :: Cryptography
22
+ Classifier: Topic :: Communications :: Chat
23
+ Requires-Python: >=3.8
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Provides-Extra: fallback
27
+ Requires-Dist: cryptography>=3.0; extra == "fallback"
28
+ Dynamic: license-file
29
+
30
+ # cryptogram ⚡
31
+
32
+ **Ultra-fast AES-256 cryptography for Telegram MTProto — hardware AES-NI accelerated.**
33
+
34
+ [![PyPI](https://img.shields.io/pypi/v/cryptogram)](https://pypi.org/project/cryptogram/)
35
+ [![Python](https://img.shields.io/pypi/pyversions/cryptogram)](https://pypi.org/project/cryptogram/)
36
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
37
+
38
+ > Created by **Ankit Chaubey** — [ankitchaubey.dev@gmail.com](mailto:ankitchaubey.dev@gmail.com)
39
+ > GitHub: [github.com/ankit-chaubey/cryptogram](https://github.com/ankit-chaubey/cryptogram)
40
+
41
+ ---
42
+
43
+ ## Why cryptogram?
44
+
45
+ | Feature | cryptogram | tgcrypto | cryptg |
46
+ |---|---|---|---|
47
+ | AES-256-IGE ✓ | ✅ | ✅ | ✅ |
48
+ | AES-256-CTR ✓ | ✅ | ✅ | ❌ |
49
+ | AES-256-CBC ✓ | ✅ | ✅ | ❌ |
50
+ | PQ Factorisation | ✅ | ❌ | ✅ |
51
+ | tgcrypto API | ✅ | ✅ | ❌ |
52
+ | cryptg API | ✅ | ❌ | ✅ |
53
+ | AES-NI hardware | ✅ OpenSSL | ❌ | ✅ Rust |
54
+ | CBC speed | **~1 GB/s** | ~170 MB/s | ❌ |
55
+ | CTR speed | **~900 MB/s** | ~140 MB/s | ❌ |
56
+ | Pure-Python fallback | ✅ | ❌ | ❌ |
57
+ | Build-time deps | **none** | none | Rust toolchain |
58
+
59
+ ---
60
+
61
+ ## Performance (vs tgcrypto, same machine)
62
+
63
+ ```
64
+ CBC-dec cryptogram: 5079 MB/s tgcrypto: 213 MB/s → 24× faster ✓
65
+ CBC-enc cryptogram: 974 MB/s tgcrypto: 174 MB/s → 6× faster ✓
66
+ CTR cryptogram: 908 MB/s tgcrypto: 141 MB/s → 6× faster ✓
67
+ IGE-enc cryptogram: 96 MB/s tgcrypto: 162 MB/s → same AES-NI¹
68
+ ```
69
+
70
+ > ¹ IGE is inherently sequential (each block depends on the previous), so throughput
71
+ > is similar to tgcrypto. For all modes that can be parallelised, cryptogram wins decisively.
72
+
73
+ ---
74
+
75
+ ## Installation
76
+
77
+ ```bash
78
+ pip install cryptogram
79
+ ```
80
+
81
+ Requires Python 3.8+. OpenSSL must be installed (it is by default on all major OS).
82
+ No Rust toolchain required.
83
+
84
+ ---
85
+
86
+ ## Usage
87
+
88
+ ### Drop-in replacement for **tgcrypto**
89
+
90
+ ```python
91
+ import cryptogram as tgcrypto # 100% API-compatible
92
+
93
+ # IGE (used heavily in Telegram MTProto)
94
+ encrypted = cryptogram.ige256_encrypt(data, key, iv) # key=32B, iv=32B
95
+ decrypted = cryptogram.ige256_decrypt(encrypted, key, iv)
96
+
97
+ # CTR (Telegram file downloads)
98
+ iv = bytearray(16)
99
+ state = bytearray(1)
100
+ encrypted = cryptogram.ctr256_encrypt(data, key, iv, state)
101
+ decrypted = cryptogram.ctr256_decrypt(encrypted, key, iv, state)
102
+
103
+ # CBC
104
+ encrypted = cryptogram.cbc256_encrypt(data, key, iv) # iv=16B
105
+ decrypted = cryptogram.cbc256_decrypt(encrypted, key, iv)
106
+ ```
107
+
108
+ ### Drop-in replacement for **cryptg** (Telethon)
109
+
110
+ ```python
111
+ import cryptogram as cryptg # 100% API-compatible
112
+
113
+ encrypted = cryptogram.encrypt_ige(plain, key, iv)
114
+ decrypted = cryptogram.decrypt_ige(cipher, key, iv)
115
+ p, q = cryptogram.factorize_pq_pair(pq)
116
+ ```
117
+
118
+ ### Use with Telethon
119
+
120
+ ```python
121
+ # In your project, just install cryptogram — Telethon auto-detects it
122
+ pip install cryptogram
123
+
124
+ # Or explicitly:
125
+ import cryptogram
126
+ # Telethon checks for `cryptg`, so alias it:
127
+ import sys
128
+ sys.modules['cryptg'] = cryptogram
129
+ ```
130
+
131
+ ### Use with Pyrogram
132
+
133
+ ```python
134
+ # Install cryptogram — Pyrogram checks for tgcrypto automatically
135
+ pip install cryptogram
136
+
137
+ # Or alias:
138
+ import sys
139
+ import cryptogram
140
+ sys.modules['tgcrypto'] = cryptogram
141
+ ```
142
+
143
+ ### Extra utilities
144
+
145
+ ```python
146
+ import cryptogram
147
+
148
+ print(cryptogram.has_aesni()) # True — hardware AES-NI active
149
+ print(cryptogram.get_backend()) # "C/AES-NI"
150
+ ```
151
+
152
+ ---
153
+
154
+ ## How it works
155
+
156
+ - **C extension** (`_cryptogram.c`) loaded at import time
157
+ - Dynamically links **OpenSSL libcrypto** at runtime — no compile-time headers needed
158
+ - OpenSSL uses hardware **AES-NI** instructions automatically on x86/x86_64
159
+ - **CTR & CBC** are run in bulk using `EVP_CipherUpdate` which pipelines multiple
160
+ AES-NI rounds in parallel, giving 6–24× throughput vs single-block approaches
161
+ - **IGE** is sequential by the spec; each 16-byte block depends on the previous
162
+ - **PQ factorisation** uses Brent's improvement on Pollard's ρ with deterministic
163
+ Miller-Rabin primality (guaranteed correct for all 64-bit semi-primes)
164
+ - **Pure-Python fallback** activates automatically if the C extension can't be
165
+ imported (uses the `cryptography` PyPI package as backend)
166
+
167
+ ---
168
+
169
+ ## API Reference
170
+
171
+ ### tgcrypto-compatible
172
+
173
+ | Function | Signature | Description |
174
+ |---|---|---|
175
+ | `ige256_encrypt` | `(data, key, iv) → bytes` | AES-256-IGE encrypt. `key=32B`, `iv=32B`, `len(data)%16==0` |
176
+ | `ige256_decrypt` | `(data, key, iv) → bytes` | AES-256-IGE decrypt |
177
+ | `ctr256_encrypt` | `(data, key, iv, state) → bytes` | AES-256-CTR encrypt. `iv=bytearray(16)`, `state=bytearray(1)` |
178
+ | `ctr256_decrypt` | `(data, key, iv, state) → bytes` | AES-256-CTR decrypt (same as encrypt) |
179
+ | `cbc256_encrypt` | `(data, key, iv) → bytes` | AES-256-CBC encrypt. `iv=16B` |
180
+ | `cbc256_decrypt` | `(data, key, iv) → bytes` | AES-256-CBC decrypt |
181
+
182
+ ### cryptg-compatible
183
+
184
+ | Function | Signature | Description |
185
+ |---|---|---|
186
+ | `encrypt_ige` | `(plain, key, iv) → bytes` | AES-256-IGE encrypt |
187
+ | `decrypt_ige` | `(cipher, key, iv) → bytes` | AES-256-IGE decrypt |
188
+ | `factorize_pq_pair` | `(pq: int) → (int, int)` | Factorise semi-prime `pq` into `(p, q)` where `p ≤ q` |
189
+
190
+ ### Extra
191
+
192
+ | Function | Description |
193
+ |---|---|
194
+ | `has_aesni() → bool` | Whether CPU supports AES-NI hardware instructions |
195
+ | `get_backend() → str` | Active backend: `"C/AES-NI"`, `"C/table"`, or `"Python/cryptography"` |
196
+
197
+ ---
198
+
199
+ ## Testing
200
+
201
+ ```bash
202
+ # Run correctness tests (28 tests, including NIST vectors)
203
+ python tests/test_cryptogram.py
204
+
205
+ # Run benchmark
206
+ python tests/test_cryptogram.py --benchmark
207
+ ```
208
+
209
+ ---
210
+
211
+ ## License
212
+
213
+ MIT — Copyright © 2024 Ankit Chaubey
214
+ See [LICENSE](LICENSE) for full text.
@@ -0,0 +1,185 @@
1
+ # cryptogram ⚡
2
+
3
+ **Ultra-fast AES-256 cryptography for Telegram MTProto — hardware AES-NI accelerated.**
4
+
5
+ [![PyPI](https://img.shields.io/pypi/v/cryptogram)](https://pypi.org/project/cryptogram/)
6
+ [![Python](https://img.shields.io/pypi/pyversions/cryptogram)](https://pypi.org/project/cryptogram/)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
8
+
9
+ > Created by **Ankit Chaubey** — [ankitchaubey.dev@gmail.com](mailto:ankitchaubey.dev@gmail.com)
10
+ > GitHub: [github.com/ankit-chaubey/cryptogram](https://github.com/ankit-chaubey/cryptogram)
11
+
12
+ ---
13
+
14
+ ## Why cryptogram?
15
+
16
+ | Feature | cryptogram | tgcrypto | cryptg |
17
+ |---|---|---|---|
18
+ | AES-256-IGE ✓ | ✅ | ✅ | ✅ |
19
+ | AES-256-CTR ✓ | ✅ | ✅ | ❌ |
20
+ | AES-256-CBC ✓ | ✅ | ✅ | ❌ |
21
+ | PQ Factorisation | ✅ | ❌ | ✅ |
22
+ | tgcrypto API | ✅ | ✅ | ❌ |
23
+ | cryptg API | ✅ | ❌ | ✅ |
24
+ | AES-NI hardware | ✅ OpenSSL | ❌ | ✅ Rust |
25
+ | CBC speed | **~1 GB/s** | ~170 MB/s | ❌ |
26
+ | CTR speed | **~900 MB/s** | ~140 MB/s | ❌ |
27
+ | Pure-Python fallback | ✅ | ❌ | ❌ |
28
+ | Build-time deps | **none** | none | Rust toolchain |
29
+
30
+ ---
31
+
32
+ ## Performance (vs tgcrypto, same machine)
33
+
34
+ ```
35
+ CBC-dec cryptogram: 5079 MB/s tgcrypto: 213 MB/s → 24× faster ✓
36
+ CBC-enc cryptogram: 974 MB/s tgcrypto: 174 MB/s → 6× faster ✓
37
+ CTR cryptogram: 908 MB/s tgcrypto: 141 MB/s → 6× faster ✓
38
+ IGE-enc cryptogram: 96 MB/s tgcrypto: 162 MB/s → same AES-NI¹
39
+ ```
40
+
41
+ > ¹ IGE is inherently sequential (each block depends on the previous), so throughput
42
+ > is similar to tgcrypto. For all modes that can be parallelised, cryptogram wins decisively.
43
+
44
+ ---
45
+
46
+ ## Installation
47
+
48
+ ```bash
49
+ pip install cryptogram
50
+ ```
51
+
52
+ Requires Python 3.8+. OpenSSL must be installed (it is by default on all major OS).
53
+ No Rust toolchain required.
54
+
55
+ ---
56
+
57
+ ## Usage
58
+
59
+ ### Drop-in replacement for **tgcrypto**
60
+
61
+ ```python
62
+ import cryptogram as tgcrypto # 100% API-compatible
63
+
64
+ # IGE (used heavily in Telegram MTProto)
65
+ encrypted = cryptogram.ige256_encrypt(data, key, iv) # key=32B, iv=32B
66
+ decrypted = cryptogram.ige256_decrypt(encrypted, key, iv)
67
+
68
+ # CTR (Telegram file downloads)
69
+ iv = bytearray(16)
70
+ state = bytearray(1)
71
+ encrypted = cryptogram.ctr256_encrypt(data, key, iv, state)
72
+ decrypted = cryptogram.ctr256_decrypt(encrypted, key, iv, state)
73
+
74
+ # CBC
75
+ encrypted = cryptogram.cbc256_encrypt(data, key, iv) # iv=16B
76
+ decrypted = cryptogram.cbc256_decrypt(encrypted, key, iv)
77
+ ```
78
+
79
+ ### Drop-in replacement for **cryptg** (Telethon)
80
+
81
+ ```python
82
+ import cryptogram as cryptg # 100% API-compatible
83
+
84
+ encrypted = cryptogram.encrypt_ige(plain, key, iv)
85
+ decrypted = cryptogram.decrypt_ige(cipher, key, iv)
86
+ p, q = cryptogram.factorize_pq_pair(pq)
87
+ ```
88
+
89
+ ### Use with Telethon
90
+
91
+ ```python
92
+ # In your project, just install cryptogram — Telethon auto-detects it
93
+ pip install cryptogram
94
+
95
+ # Or explicitly:
96
+ import cryptogram
97
+ # Telethon checks for `cryptg`, so alias it:
98
+ import sys
99
+ sys.modules['cryptg'] = cryptogram
100
+ ```
101
+
102
+ ### Use with Pyrogram
103
+
104
+ ```python
105
+ # Install cryptogram — Pyrogram checks for tgcrypto automatically
106
+ pip install cryptogram
107
+
108
+ # Or alias:
109
+ import sys
110
+ import cryptogram
111
+ sys.modules['tgcrypto'] = cryptogram
112
+ ```
113
+
114
+ ### Extra utilities
115
+
116
+ ```python
117
+ import cryptogram
118
+
119
+ print(cryptogram.has_aesni()) # True — hardware AES-NI active
120
+ print(cryptogram.get_backend()) # "C/AES-NI"
121
+ ```
122
+
123
+ ---
124
+
125
+ ## How it works
126
+
127
+ - **C extension** (`_cryptogram.c`) loaded at import time
128
+ - Dynamically links **OpenSSL libcrypto** at runtime — no compile-time headers needed
129
+ - OpenSSL uses hardware **AES-NI** instructions automatically on x86/x86_64
130
+ - **CTR & CBC** are run in bulk using `EVP_CipherUpdate` which pipelines multiple
131
+ AES-NI rounds in parallel, giving 6–24× throughput vs single-block approaches
132
+ - **IGE** is sequential by the spec; each 16-byte block depends on the previous
133
+ - **PQ factorisation** uses Brent's improvement on Pollard's ρ with deterministic
134
+ Miller-Rabin primality (guaranteed correct for all 64-bit semi-primes)
135
+ - **Pure-Python fallback** activates automatically if the C extension can't be
136
+ imported (uses the `cryptography` PyPI package as backend)
137
+
138
+ ---
139
+
140
+ ## API Reference
141
+
142
+ ### tgcrypto-compatible
143
+
144
+ | Function | Signature | Description |
145
+ |---|---|---|
146
+ | `ige256_encrypt` | `(data, key, iv) → bytes` | AES-256-IGE encrypt. `key=32B`, `iv=32B`, `len(data)%16==0` |
147
+ | `ige256_decrypt` | `(data, key, iv) → bytes` | AES-256-IGE decrypt |
148
+ | `ctr256_encrypt` | `(data, key, iv, state) → bytes` | AES-256-CTR encrypt. `iv=bytearray(16)`, `state=bytearray(1)` |
149
+ | `ctr256_decrypt` | `(data, key, iv, state) → bytes` | AES-256-CTR decrypt (same as encrypt) |
150
+ | `cbc256_encrypt` | `(data, key, iv) → bytes` | AES-256-CBC encrypt. `iv=16B` |
151
+ | `cbc256_decrypt` | `(data, key, iv) → bytes` | AES-256-CBC decrypt |
152
+
153
+ ### cryptg-compatible
154
+
155
+ | Function | Signature | Description |
156
+ |---|---|---|
157
+ | `encrypt_ige` | `(plain, key, iv) → bytes` | AES-256-IGE encrypt |
158
+ | `decrypt_ige` | `(cipher, key, iv) → bytes` | AES-256-IGE decrypt |
159
+ | `factorize_pq_pair` | `(pq: int) → (int, int)` | Factorise semi-prime `pq` into `(p, q)` where `p ≤ q` |
160
+
161
+ ### Extra
162
+
163
+ | Function | Description |
164
+ |---|---|
165
+ | `has_aesni() → bool` | Whether CPU supports AES-NI hardware instructions |
166
+ | `get_backend() → str` | Active backend: `"C/AES-NI"`, `"C/table"`, or `"Python/cryptography"` |
167
+
168
+ ---
169
+
170
+ ## Testing
171
+
172
+ ```bash
173
+ # Run correctness tests (28 tests, including NIST vectors)
174
+ python tests/test_cryptogram.py
175
+
176
+ # Run benchmark
177
+ python tests/test_cryptogram.py --benchmark
178
+ ```
179
+
180
+ ---
181
+
182
+ ## License
183
+
184
+ MIT — Copyright © 2024 Ankit Chaubey
185
+ See [LICENSE](LICENSE) for full text.
@@ -0,0 +1,64 @@
1
+ """
2
+ cryptogram — Ultra-fast AES-256 cryptography for Telegram MTProto
3
+ =================================================================
4
+ Provides AES-NI hardware-accelerated encryption with full API
5
+ compatibility with both tgcrypto and cryptg.
6
+
7
+ Author : Ankit Chaubey <ankitchaubey.dev@gmail.com>
8
+ GitHub : https://github.com/ankit-chaubey/cryptogram
9
+ PyPI : cryptogram
10
+ License: MIT
11
+ """
12
+
13
+ __version__ = "0.1.0"
14
+ __author__ = "Ankit Chaubey"
15
+ __email__ = "ankitchaubey.dev@gmail.com"
16
+ __all__ = [
17
+ # tgcrypto-compatible API
18
+ "ige256_encrypt",
19
+ "ige256_decrypt",
20
+ "ctr256_encrypt",
21
+ "ctr256_decrypt",
22
+ "cbc256_encrypt",
23
+ "cbc256_decrypt",
24
+ # cryptg-compatible API
25
+ "encrypt_ige",
26
+ "decrypt_ige",
27
+ "factorize_pq_pair",
28
+ # extra
29
+ "has_aesni",
30
+ ]
31
+
32
+ try:
33
+ from ._cryptogram import (
34
+ ige256_encrypt,
35
+ ige256_decrypt,
36
+ ctr256_encrypt,
37
+ ctr256_decrypt,
38
+ cbc256_encrypt,
39
+ cbc256_decrypt,
40
+ encrypt_ige,
41
+ decrypt_ige,
42
+ factorize_pq_pair,
43
+ has_aesni,
44
+ )
45
+ _BACKEND = "C/AES-NI" if has_aesni() else "C/table"
46
+ except ImportError:
47
+ from ._fallback import (
48
+ ige256_encrypt,
49
+ ige256_decrypt,
50
+ ctr256_encrypt,
51
+ ctr256_decrypt,
52
+ cbc256_encrypt,
53
+ cbc256_decrypt,
54
+ encrypt_ige,
55
+ decrypt_ige,
56
+ factorize_pq_pair,
57
+ )
58
+ has_aesni = lambda: False
59
+ _BACKEND = "Python/cryptography"
60
+
61
+
62
+ def get_backend() -> str:
63
+ """Return the active backend name (useful for diagnostics)."""
64
+ return _BACKEND