-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypt.c
197 lines (156 loc) · 5.04 KB
/
crypt.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
//
// Created by sebikul on 23/06/18.
//
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#include <stdint.h>
#include "crypt.h"
static void handle_errors(void) {
ERR_print_errors_fp(stderr);
abort();
}
void crypt_init(void) {
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
// OPENSSL_config(NULL);
}
void crypt_free(void) {
EVP_cleanup();
ERR_free_strings();
}
static void crypt_derive_key(const EVP_CIPHER *cipher, const char *password, unsigned char *key, unsigned char *iv) {
if (EVP_BytesToKey(cipher, EVP_sha256(), NULL, (unsigned char *) password, (int) strlen(password), 1, key, iv) ==
0) {
handle_errors();
}
}
static const EVP_CIPHER *crypt_get_evp_cypher(ENC_METHOD enc_method, ENC_MODE enc_mode) {
switch (enc_method) {
case AES128:
switch (enc_mode) {
case ECB:
return EVP_aes_128_ecb();
case CFB:
return EVP_aes_128_cfb8();
case OFB:
return EVP_aes_128_ofb();
case CBC:
return EVP_aes_128_cbc();
default:
return NULL;
}
case AES192:
switch (enc_mode) {
case ECB:
return EVP_aes_192_ecb();
case CFB:
return EVP_aes_192_cfb8();
case OFB:
return EVP_aes_192_ofb();
case CBC:
return EVP_aes_192_cbc();
default:
return NULL;
}
case AES256:
switch (enc_mode) {
case ECB:
return EVP_aes_256_ecb();
case CFB:
return EVP_aes_256_cfb8();
case OFB:
return EVP_aes_256_ofb();
case CBC:
return EVP_aes_256_cbc();
default:
return NULL;
}
case DES:
switch (enc_mode) {
case ECB:
return EVP_des_ecb();
case CFB:
return EVP_des_cfb8();
case OFB:
return EVP_des_ofb();
case CBC:
return EVP_des_cbc();
default:
return NULL;
}
default:
return NULL;
}
}
unsigned char *
crypt_enc(ENC_METHOD enc_method, ENC_MODE enc_mode, uint8_t *plaintext, long plaintext_len, const char *password,
int *ciphertext_length) {
unsigned char *key;
unsigned char iv[16];
const EVP_CIPHER *evp_cipher = crypt_get_evp_cypher(enc_method, enc_mode);
if (evp_cipher == NULL) {
return NULL;
}
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
handle_errors();
}
EVP_CIPHER_CTX_set_padding(ctx, 1);
key = malloc((size_t) EVP_CIPHER_key_length(evp_cipher));
crypt_derive_key(evp_cipher, password, key, iv);
if (1 != EVP_EncryptInit_ex(ctx, evp_cipher, NULL, key, iv)) {
free(key);
handle_errors();
}
free(key);
size_t ciph_size = (size_t) (plaintext_len + EVP_CIPHER_CTX_block_size(ctx) - 1);
printf("Allocating %d for ciphertext.\n", (int) ciph_size);
unsigned char *ciphertext = malloc(ciph_size);
if (1 != EVP_EncryptUpdate(ctx, ciphertext, ciphertext_length, plaintext, (int) plaintext_len)) {
handle_errors();
}
int temp_len;
if (1 != EVP_EncryptFinal(ctx, ciphertext + *ciphertext_length, &temp_len)) {
handle_errors();
}
*ciphertext_length += temp_len;
EVP_CIPHER_CTX_free(ctx);
ciphertext = realloc(ciphertext, (size_t) *ciphertext_length);
return ciphertext;
}
unsigned char *
crypt_dec(ENC_METHOD enc_method, ENC_MODE enc_mode, uint8_t *ciphertext, long ciphertext_len, const char *password,
int *plaintext_length) {
unsigned char *key;
unsigned char iv[16];
const EVP_CIPHER *evp_cipher = crypt_get_evp_cypher(enc_method, enc_mode);
if (evp_cipher == NULL) {
return NULL;
}
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
handle_errors();
}
key = malloc((size_t) EVP_CIPHER_key_length(evp_cipher));
crypt_derive_key(evp_cipher, password, key, iv);
if (1 != EVP_DecryptInit_ex(ctx, evp_cipher, NULL, key, iv)) {
free(key);
handle_errors();
}
free(key);
unsigned char *plaintext = malloc((size_t) (ciphertext_len + EVP_CIPHER_block_size(evp_cipher)));
int plain_len;
if (1 != EVP_DecryptUpdate(ctx, plaintext, &plain_len, ciphertext, (int) ciphertext_len)) {
handle_errors();
}
int temp_len;
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + plain_len, &temp_len)) {
handle_errors();
}
*plaintext_length = plain_len + temp_len;
EVP_CIPHER_CTX_free(ctx);
plaintext = realloc(plaintext, (size_t) *plaintext_length);
return plaintext;
}