34 #include <libidav/utils.h> |
34 #include <libidav/utils.h> |
35 #include "crypto.h" |
35 #include "crypto.h" |
36 |
36 |
37 AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) { |
37 AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) { |
38 AESDecrypter *dec = malloc(sizeof(AESDecrypter)); |
38 AESDecrypter *dec = malloc(sizeof(AESDecrypter)); |
|
39 SHA256_Init(&dec->sha256); |
39 dec->stream = stream; |
40 dec->stream = stream; |
40 dec->write = write_func; |
41 dec->write = write_func; |
41 dec->key = key; |
42 dec->key = key; |
42 dec->init = 0; |
43 dec->init = 0; |
43 dec->ivpos = 0; |
44 dec->ivpos = 0; |
87 } |
88 } |
88 |
89 |
89 int outlen = len + 16; |
90 int outlen = len + 16; |
90 unsigned char *out = malloc(outlen); |
91 unsigned char *out = malloc(outlen); |
91 EVP_DecryptUpdate(&dec->ctx, out, &len, buf, len); |
92 EVP_DecryptUpdate(&dec->ctx, out, &len, buf, len); |
92 dec->write(out, 1, len, dec->stream); |
93 ssize_t wlen = dec->write(out, 1, len, dec->stream); |
|
94 SHA256_Update(&dec->sha256, out, wlen); |
93 free(out); |
95 free(out); |
94 return (s*n) / s; |
96 return (s*n) / s; |
95 } |
97 } |
96 |
98 |
97 void aes_decrypter_close(AESDecrypter *dec) { |
99 void aes_decrypter_close(AESDecrypter *dec) { |
111 free(iv); |
113 free(iv); |
112 return NULL; |
114 return NULL; |
113 } |
115 } |
114 |
116 |
115 AESEncrypter *enc = malloc(sizeof(AESEncrypter)); |
117 AESEncrypter *enc = malloc(sizeof(AESEncrypter)); |
|
118 SHA256_Init(&enc->sha256); |
116 enc->stream = stream; |
119 enc->stream = stream; |
117 enc->read = read_func; |
120 enc->read = read_func; |
118 enc->tmp = NULL; |
121 enc->tmp = NULL; |
119 enc->tmplen = 0; |
122 enc->tmplen = 0; |
120 enc->tmpoff = 0; |
123 enc->tmpoff = 0; |
121 enc->end = 0; |
124 enc->end = 0; |
122 //enc->iv = iv; |
|
123 enc->iv = iv; |
125 enc->iv = iv; |
124 enc->ivlen = 16; |
126 enc->ivlen = 16; |
125 |
127 |
126 EVP_CIPHER_CTX_init(&enc->ctx); |
128 EVP_CIPHER_CTX_init(&enc->ctx); |
127 if(key->type == DAV_KEY_AES128) { |
129 if(key->type == DAV_KEY_AES128) { |
156 } |
158 } |
157 |
159 |
158 void *in = malloc(len); |
160 void *in = malloc(len); |
159 size_t in_len = enc->read(in, 1, len, enc->stream); |
161 size_t in_len = enc->read(in, 1, len, enc->stream); |
160 |
162 |
|
163 SHA256_Update(&enc->sha256, in, in_len); |
|
164 |
161 unsigned char *out = NULL; |
165 unsigned char *out = NULL; |
162 int outlen = 0; |
166 int outlen = 0; |
163 size_t ivl = enc->ivlen; |
167 size_t ivl = enc->ivlen; |
164 if(in_len != 0) { |
168 if(in_len != 0) { |
165 outlen = len + 16; |
169 outlen = len + 16; |
171 free(in); |
175 free(in); |
172 } else { |
176 } else { |
173 out = malloc(16); |
177 out = malloc(16); |
174 EVP_EncryptFinal_ex(&enc->ctx, out, &outlen); |
178 EVP_EncryptFinal_ex(&enc->ctx, out, &outlen); |
175 enc->end = 1; |
179 enc->end = 1; |
|
180 free(in); |
176 } |
181 } |
177 enc->tmp = (char*)out; |
182 enc->tmp = (char*)out; |
178 enc->tmplen = outlen + ivl; |
183 enc->tmplen = outlen + ivl; |
179 enc->tmpoff = 0; |
184 enc->tmpoff = 0; |
180 |
185 |
181 if(enc->iv) { |
186 if(enc->iv) { |
|
187 free(enc->iv); |
182 enc->iv = NULL; |
188 enc->iv = NULL; |
183 enc->ivlen = 0; |
189 enc->ivlen = 0; |
184 } |
190 } |
185 |
191 |
186 return aes_read(buf, s, n, enc); |
192 return aes_read(buf, s, n, enc); |
188 |
194 |
189 void aes_encrypter_close(AESEncrypter *enc) { |
195 void aes_encrypter_close(AESEncrypter *enc) { |
190 if(enc->tmp) { |
196 if(enc->tmp) { |
191 free(enc->tmp); |
197 free(enc->tmp); |
192 } |
198 } |
|
199 if(enc->iv) { |
|
200 free(enc->iv); |
|
201 } |
193 EVP_CIPHER_CTX_cleanup(&enc->ctx); |
202 EVP_CIPHER_CTX_cleanup(&enc->ctx); |
194 free(enc); |
203 free(enc); |
195 } |
204 } |
196 |
205 |
197 |
206 |
198 char* aes_encrypt(char *in, DavKey *key) { |
207 char* aes_encrypt(char *in, size_t len, DavKey *key) { |
199 unsigned char iv[16]; |
208 unsigned char iv[16]; |
200 if(!RAND_bytes(iv, 16)) { |
209 if(!RAND_bytes(iv, 16)) { |
201 return NULL; |
210 return NULL; |
202 } |
211 } |
203 |
212 |
216 EVP_aes_256_cbc(), |
225 EVP_aes_256_cbc(), |
217 NULL, |
226 NULL, |
218 (unsigned char*)key->data, |
227 (unsigned char*)key->data, |
219 iv); |
228 iv); |
220 } else { |
229 } else { |
221 return NULL; |
230 EVP_CIPHER_CTX_cleanup(&ctx); |
222 } |
231 return NULL; |
223 |
232 } |
224 int len = strlen(in); |
233 |
|
234 //int len = strlen(in); |
225 int buflen = len + 64; |
235 int buflen = len + 64; |
226 unsigned char *buf = calloc(1, buflen); |
236 unsigned char *buf = calloc(1, buflen); |
227 memcpy(buf, iv, 16); |
237 memcpy(buf, iv, 16); |
228 |
238 |
229 int l = buflen - 16; |
239 int l = buflen - 16; |
231 |
241 |
232 int f = 0; |
242 int f = 0; |
233 EVP_EncryptFinal_ex(&ctx, buf + 16 + l, &f); |
243 EVP_EncryptFinal_ex(&ctx, buf + 16 + l, &f); |
234 char *out = util_base64encode((char*)buf, 16 + l + f); |
244 char *out = util_base64encode((char*)buf, 16 + l + f); |
235 free(buf); |
245 free(buf); |
|
246 EVP_CIPHER_CTX_cleanup(&ctx); |
|
247 |
236 return out; |
248 return out; |
237 } |
249 } |
238 |
250 |
239 char* aes_decrypt(char *in, DavKey *key) { |
251 char* aes_decrypt(char *in, size_t *length, DavKey *key) { |
240 int len; |
252 int len; |
241 unsigned char *buf = (unsigned char*)util_base64decode_len(in, &len); |
253 unsigned char *buf = (unsigned char*)util_base64decode_len(in, &len); |
242 |
254 |
243 EVP_CIPHER_CTX ctx; |
255 EVP_CIPHER_CTX ctx; |
244 EVP_CIPHER_CTX_init(&ctx); |
256 EVP_CIPHER_CTX_init(&ctx); |
268 |
281 |
269 EVP_DecryptUpdate(&ctx, out, &outlen, in_buf, inlen); |
282 EVP_DecryptUpdate(&ctx, out, &outlen, in_buf, inlen); |
270 EVP_DecryptFinal_ex(&ctx, out + outlen, &f); |
283 EVP_DecryptFinal_ex(&ctx, out + outlen, &f); |
271 out[outlen + f] = '\0'; |
284 out[outlen + f] = '\0'; |
272 free(buf); |
285 free(buf); |
|
286 EVP_CIPHER_CTX_cleanup(&ctx); |
|
287 |
|
288 *length = outlen + f; |
273 return (char*)out; |
289 return (char*)out; |
274 } |
290 } |
|
291 |
|
292 |
|
293 void dav_get_hash(SHA256_CTX *sha256, char *buf) { |
|
294 #ifdef __sun |
|
295 SHA256Final(buf, sha256); |
|
296 #else |
|
297 SHA256_Final(buf, sha256); |
|
298 #endif |
|
299 } |