This repository was archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaes.cxx
166 lines (129 loc) · 3.54 KB
/
aes.cxx
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
#include <Python.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
static bool _inited = false;
static void _init()
{
if (!_inited)
{
_inited = true;
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
}
}
int _handle_error()
{
ERR_print_errors_fp(stderr);
return -1;
}
int AES_encrypt(unsigned char* data, int size, unsigned char* key, unsigned char* iv, unsigned char* ciphertext)
{
EVP_CIPHER_CTX *ctx;
int ciphertext_len, len;
if (!(ctx = EVP_CIPHER_CTX_new()))
goto error;
if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1)
goto error;
if (EVP_EncryptUpdate(ctx, ciphertext, &len, data, size) != 1)
goto error;
ciphertext_len = len;
if (EVP_EncryptFinal_ex(ctx, &ciphertext[len], &len) != 1)
goto error;
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len + len;
error:
if (ctx) EVP_CIPHER_CTX_free(ctx);
return _handle_error();
}
int AES_decrypt(unsigned char* data, int size, unsigned char* key, unsigned char* iv, unsigned char* plaintext)
{
EVP_CIPHER_CTX *ctx;
int plaintext_len, len;
if (!(ctx = EVP_CIPHER_CTX_new()))
goto error;
if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1)
goto error;
if (EVP_DecryptUpdate(ctx, plaintext, &len, data, size) != 1)
goto error;
plaintext_len = len;
if (EVP_DecryptFinal_ex(ctx, plaintext + len, &len) != 1)
goto error;
EVP_CIPHER_CTX_free(ctx);
return plaintext_len + len;
error:
if (ctx) EVP_CIPHER_CTX_free(ctx);
return _handle_error();
}
static PyObject* py_aes_encrypt(PyObject* self, PyObject* args)
{
unsigned char* data;
unsigned char* key;
unsigned char* iv;
int size, keysize, ivsize;
if (!PyArg_ParseTuple(args, "s#s#s#", &data, &size, &key, &keysize, &iv, &ivsize))
{
return NULL;
}
if (ivsize != 16 || keysize != 16)
{
PyErr_Format(PyExc_ValueError, "iv and key must 16 bytes");
return NULL;
}
unsigned char* ciphertext = new unsigned char[size + 16];
int ciphertext_len = AES_encrypt(data, size, key, iv, ciphertext);
if (ciphertext_len == -1)
{
delete[] ciphertext;
Py_INCREF(Py_None);
return Py_None;
}
PyObject* v = Py_BuildValue("s#", ciphertext, ciphertext_len);
delete[] ciphertext;
return v;
};
static PyObject* py_aes_decrypt(PyObject* self, PyObject* args)
{
unsigned char* data;
unsigned char* key;
unsigned char* iv;
int size, keysize, ivsize;
if (!PyArg_ParseTuple(args, "s#s#s#", &data, &size, &key, &keysize, &iv, &ivsize))
{
return NULL;
}
if (ivsize != 16 || keysize != 16)
{
PyErr_Format(PyExc_ValueError, "iv and key must 16 bytes");
return NULL;
}
unsigned char* plaintext = new unsigned char[size + 16];
int plaintext_len = AES_decrypt(data, size, key, iv, plaintext);
if (plaintext_len == -1)
{
delete[] plaintext;
Py_INCREF(Py_None);
return Py_None;
}
PyObject* v = Py_BuildValue("s#", plaintext, plaintext_len);
delete[] plaintext;
return v;
};
static PyMethodDef Methods[] = {
{"encrypt", py_aes_encrypt, METH_VARARGS},
{"decrypt", py_aes_decrypt, METH_VARARGS},
{NULL, NULL, 0}
};
#ifdef BUILD_PYD
extern "C"
#ifdef _WIN32
__declspec(dllexport)
#endif
#endif
void initaes(void)
{
_init();
Py_InitModule("aes", Methods);
}