-
Notifications
You must be signed in to change notification settings - Fork 40
/
tgcrypto.c
243 lines (195 loc) · 6.2 KB
/
tgcrypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Pyrogram - Telegram MTProto API Client Library for Python
* Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
*
* This file is part of Pyrogram.
*
* Pyrogram is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pyrogram is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "aes256.h"
#include "ige256.h"
#include "ctr256.h"
#include "cbc256.h"
#define DESCRIPTION "Fast and Portable Cryptography Extension Library for Pyrogram\n" \
"TgCrypto is part of Pyrogram, a Telegram MTProto library for Python\n" \
"You can learn more about Pyrogram here: https://pyrogram.org\n"
static PyObject *ige(PyObject *args, uint8_t encrypt) {
Py_buffer data, key, iv;
uint8_t *buf;
PyObject *out;
if (!PyArg_ParseTuple(args, "y*y*y*", &data, &key, &iv))
return NULL;
if (data.len == 0) {
PyErr_SetString(PyExc_ValueError, "Data must not be empty");
return NULL;
}
if (data.len % 16 != 0) {
PyErr_SetString(PyExc_ValueError, "Data size must match a multiple of 16 bytes");
return NULL;
}
if (key.len != 32) {
PyErr_SetString(PyExc_ValueError, "Key size must be exactly 32 bytes");
return NULL;
}
if (iv.len != 32) {
PyErr_SetString(PyExc_ValueError, "IV size must be exactly 32 bytes");
return NULL;
}
Py_BEGIN_ALLOW_THREADS
buf = ige256(data.buf, data.len, key.buf, iv.buf, encrypt);
Py_END_ALLOW_THREADS
PyBuffer_Release(&data);
PyBuffer_Release(&key);
PyBuffer_Release(&iv);
out = Py_BuildValue("y#", buf, data.len);
free(buf);
return out;
}
static PyObject *ige256_encrypt(PyObject *self, PyObject *args) {
return ige(args, 1);
}
static PyObject *ige256_decrypt(PyObject *self, PyObject *args) {
return ige(args, 0);
}
static PyObject *ctr256_encrypt(PyObject *self, PyObject *args) {
Py_buffer data, key, iv, state;
uint8_t *buf;
PyObject *out;
if (!PyArg_ParseTuple(args, "y*y*y*y*", &data, &key, &iv, &state))
return NULL;
if (data.len == 0) {
PyErr_SetString(PyExc_ValueError, "Data must not be empty");
return NULL;
}
if (key.len != 32) {
PyErr_SetString(PyExc_ValueError, "Key size must be exactly 32 bytes");
return NULL;
}
if (iv.len != 16) {
PyErr_SetString(PyExc_ValueError, "IV size must be exactly 16 bytes");
return NULL;
}
if (state.len != 1) {
PyErr_SetString(PyExc_ValueError, "State size must be exactly 1 byte");
return NULL;
}
if (*(uint8_t *) state.buf > 15) {
PyErr_SetString(PyExc_ValueError, "State value must be in the range [0, 15]");
return NULL;
}
Py_BEGIN_ALLOW_THREADS
buf = ctr256(data.buf, data.len, key.buf, iv.buf, state.buf);
Py_END_ALLOW_THREADS
PyBuffer_Release(&data);
PyBuffer_Release(&key);
PyBuffer_Release(&iv);
out = Py_BuildValue("y#", buf, data.len);
free(buf);
return out;
}
static PyObject *cbc(PyObject *args, uint8_t encrypt) {
Py_buffer data, key, iv;
uint8_t *buf;
PyObject *out;
if (!PyArg_ParseTuple(args, "y*y*y*", &data, &key, &iv))
return NULL;
if (data.len == 0) {
PyErr_SetString(PyExc_ValueError, "Data must not be empty");
return NULL;
}
if (data.len % 16 != 0) {
PyErr_SetString(PyExc_ValueError, "Data size must match a multiple of 16 bytes");
return NULL;
}
if (key.len != 32) {
PyErr_SetString(PyExc_ValueError, "Key size must be exactly 32 bytes");
return NULL;
}
if (iv.len != 16) {
PyErr_SetString(PyExc_ValueError, "IV size must be exactly 16 bytes");
return NULL;
}
Py_BEGIN_ALLOW_THREADS
buf = cbc256(data.buf, data.len, key.buf, iv.buf, encrypt);
Py_END_ALLOW_THREADS
PyBuffer_Release(&data);
PyBuffer_Release(&key);
PyBuffer_Release(&iv);
out = Py_BuildValue("y#", buf, data.len);
free(buf);
return out;
}
static PyObject *cbc256_encrypt(PyObject *self, PyObject *args) {
return cbc(args, 1);
}
static PyObject *cbc256_decrypt(PyObject *self, PyObject *args) {
return cbc(args, 0);
}
PyDoc_STRVAR(
ige256_encrypt_docs,
"ige256_encrypt(data, key, iv)\n"
"--\n\n"
"AES-256-IGE Encryption"
);
PyDoc_STRVAR(
ige256_decrypt_docs,
"ige256_decrypt(data, key, iv)\n"
"--\n\n"
"AES-256-IGE Decryption"
);
PyDoc_STRVAR(
ctr256_encrypt_docs,
"ctr256_encrypt(data, key, iv, state)\n"
"--\n\n"
"AES-256-CTR Encryption"
);
PyDoc_STRVAR(
ctr256_decrypt_docs,
"ctr256_decrypt(data, key, iv, state)\n"
"--\n\n"
"AES-256-CTR Decryption"
);
PyDoc_STRVAR(
cbc256_encrypt_docs,
"cbc256_encrypt(data, key, iv)\n"
"--\n\n"
"AES-256-CBC Encryption"
);
PyDoc_STRVAR(
cbc256_decrypt_docs,
"cbc256_decrypt(data, key, iv)\n"
"--\n\n"
"AES-256-CBC Encryption"
);
static PyMethodDef methods[] = {
{"ige256_encrypt", (PyCFunction) ige256_encrypt, METH_VARARGS, ige256_encrypt_docs},
{"ige256_decrypt", (PyCFunction) ige256_decrypt, METH_VARARGS, ige256_decrypt_docs},
{"ctr256_encrypt", (PyCFunction) ctr256_encrypt, METH_VARARGS, ctr256_encrypt_docs},
{"ctr256_decrypt", (PyCFunction) ctr256_encrypt, METH_VARARGS, ctr256_decrypt_docs},
{"cbc256_encrypt", (PyCFunction) cbc256_encrypt, METH_VARARGS, cbc256_encrypt_docs},
{"cbc256_decrypt", (PyCFunction) cbc256_decrypt, METH_VARARGS, cbc256_decrypt_docs},
{NULL}
};
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
"TgCrypto",
DESCRIPTION,
-1,
methods
};
PyMODINIT_FUNC PyInit_tgcrypto(void) {
return PyModule_Create(&module);
}