-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.c
133 lines (102 loc) · 3.3 KB
/
aes.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
#include "aes.h"
using namespace std;
aes_data aes_create()
{
// generate a random 32-byte password
unsigned char password[32];
RAND_bytes(password, 32);
// we pass data via an aes_data struct
aes_data data;
int z = EVP_BytesToKey(
EVP_aes_256_cbc(), EVP_sha1(), // 256-bit cbc; sha1
NULL, // salt
password, 32, // password for generation
8, // num rounds
data.aes_key, data.aes_iv); // return buffers
return data;
}
int aes_init(EVP_CIPHER_CTX *en_ctx, EVP_CIPHER_CTX *de_ctx, const aes_data &data)
{
// en_ctx stores the encryption key/iv
EVP_CIPHER_CTX_init(en_ctx);
EVP_EncryptInit_ex(en_ctx,
EVP_aes_256_cbc(),
NULL,
data.aes_key, data.aes_iv);
// de_ctx stores the decryption key/iv
EVP_CIPHER_CTX_init(de_ctx);
EVP_DecryptInit_ex(de_ctx,
EVP_aes_256_cbc(),
NULL,
data.aes_key, data.aes_iv);
return 0;
}
unsigned char *aes_encrypt(EVP_CIPHER_CTX *en_ctx,
unsigned char *ptext, int *len)
{
int outlen = *len + AES_BLOCK_SIZE;
int finlen = 0;
unsigned char *ctext = (unsigned char *)OPENSSL_malloc(outlen);
EVP_EncryptUpdate(en_ctx, ctext, &outlen, ptext, *len);
EVP_EncryptFinal_ex(en_ctx, ctext+outlen, &finlen);
*len = outlen + finlen;
return ctext;
}
unsigned char *aes_decrypt(EVP_CIPHER_CTX *de_ctx,
unsigned char *ctext, int *len)
{
int outlen = *len;
int finlen = 0;
unsigned char *ptext = (unsigned char *)OPENSSL_malloc(outlen);
EVP_DecryptUpdate(de_ctx, ptext, &outlen, ctext, *len);
EVP_DecryptFinal_ex(de_ctx, ptext+outlen, &finlen);
*len = outlen + finlen;
return ptext;
}
unsigned char *randstr(unsigned char *str, int len, bool newseed=false)
{
if (newseed) {
timeval seed;
gettimeofday(&seed, NULL);
srand(seed.tv_usec * seed.tv_sec);
}
for (int i = 0; i < len; ++i) {
str[i] = 'a' + (rand() % 26);
}
return str;
}
int main(int argc, char **argv)
{
EVP_CIPHER_CTX en_ctx;
EVP_CIPHER_CTX de_ctx;
aes_data data = aes_create();
for (int i = 0; i < 2; ++i) {
if (aes_init(&en_ctx, &de_ctx, data)) {
printf("Couldn't initialize AES System\n");
return 1;
}
unsigned char msg[1041];
int len = 1025;
randstr(msg, len, true);
unsigned char msgorig[1041];
memcpy(msgorig, msg, len);
unsigned char *ctext = aes_encrypt(&en_ctx, msg, &len);
memcpy(msg, ctext, len);
cerr << len << endl;
unsigned char *ptext = aes_decrypt(&de_ctx, msg, &len);
cerr << len << endl;
string msgstr((char *)msgorig, len);
if (memcmp(msgorig, ptext, len)) {
printf("FAIL: enc/dec failed for \"%s\"\n", msgstr.c_str());
return 0;
}
else {
printf("OK: enc/dec ok for \"%s\"\n", msgstr.c_str());
}
EVP_CIPHER_CTX_cleanup(&en_ctx);
EVP_CIPHER_CTX_cleanup(&de_ctx);
}
//free(ctext);
//free(ptext);
return 0;
}