135 void aes_decrypter_close(AESDecrypter *dec) { |
135 void aes_decrypter_close(AESDecrypter *dec) { |
136 free(dec); |
136 free(dec); |
137 } |
137 } |
138 |
138 |
139 |
139 |
140 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) { |
140 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func, dav_seek_func seek_func) { |
141 unsigned char *iv = malloc(16); |
141 unsigned char *iv = malloc(16); |
142 if(!RAND_bytes(iv, 16)) { |
142 if(!RAND_bytes(iv, 16)) { |
143 free(iv); |
143 free(iv); |
144 return NULL; |
144 return NULL; |
145 } |
145 } |
146 |
146 |
147 AESEncrypter *enc = malloc(sizeof(AESEncrypter)); |
147 AESEncrypter *enc = malloc(sizeof(AESEncrypter)); |
148 SHA256_Init(&enc->sha256); |
148 SHA256_Init(&enc->sha256); |
149 enc->stream = stream; |
149 enc->stream = stream; |
150 enc->read = read_func; |
150 enc->read = read_func; |
|
151 enc->seek = seek_func; |
151 enc->tmp = NULL; |
152 enc->tmp = NULL; |
152 enc->tmplen = 0; |
153 enc->tmplen = 0; |
153 enc->tmpoff = 0; |
154 enc->tmpoff = 0; |
154 enc->end = 0; |
155 enc->end = 0; |
155 enc->iv = iv; |
156 enc->iv = iv; |
197 int outlen = 0; |
198 int outlen = 0; |
198 size_t ivl = enc->ivlen; |
199 size_t ivl = enc->ivlen; |
199 if(in_len != 0) { |
200 if(in_len != 0) { |
200 outlen = len + 32; |
201 outlen = len + 32; |
201 out = malloc(outlen + ivl); |
202 out = malloc(outlen + ivl); |
202 if(enc->iv) { |
203 if(ivl > 0) { |
203 memcpy(out, enc->iv, ivl); |
204 memcpy(out, enc->iv, ivl); |
204 } |
205 } |
205 EVP_EncryptUpdate(enc->ctx, out + ivl, &outlen, in, in_len); |
206 EVP_EncryptUpdate(enc->ctx, out + ivl, &outlen, in, in_len); |
206 if(in_len != len) { |
207 if(in_len != len) { |
207 int newoutlen = 16; |
208 int newoutlen = 16; |
237 free(enc->iv); |
236 free(enc->iv); |
238 } |
237 } |
239 //EVP_CIPHER_CTX_cleanup(&enc->ctx); |
238 //EVP_CIPHER_CTX_cleanup(&enc->ctx); |
240 EVP_CIPHER_CTX_free(enc->ctx); |
239 EVP_CIPHER_CTX_free(enc->ctx); |
241 free(enc); |
240 free(enc); |
|
241 } |
|
242 |
|
243 int aes_encrypter_reset(AESEncrypter *enc, curl_off_t offset, int origin) { |
|
244 if(origin != SEEK_SET || offset != 0 || !enc->seek) { |
|
245 return CURL_SEEKFUNC_CANTSEEK; |
|
246 } |
|
247 |
|
248 enc->ivlen = 16; |
|
249 if(enc->seek(enc->stream, 0, SEEK_SET) != 0) { |
|
250 return CURL_SEEKFUNC_FAIL; |
|
251 } |
|
252 return CURL_SEEKFUNC_OK; |
242 } |
253 } |
243 |
254 |
244 |
255 |
245 char* aes_encrypt(char *in, size_t len, DavKey *key) { |
256 char* aes_encrypt(char *in, size_t len, DavKey *key) { |
246 unsigned char iv[16]; |
257 unsigned char iv[16]; |
466 |
477 |
467 void aes_decrypter_close(AESDecrypter *dec) { |
478 void aes_decrypter_close(AESDecrypter *dec) { |
468 |
479 |
469 } |
480 } |
470 |
481 |
471 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) { |
482 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func, dav_seek_func seek_func) { |
472 unsigned char *iv = malloc(16); |
483 unsigned char *iv = malloc(16); |
473 if(dav_rand_bytes(iv, 16)) { |
484 if(dav_rand_bytes(iv, 16)) { |
474 return NULL; |
485 return NULL; |
475 } |
486 } |
476 |
487 |
529 size_t outlen = 0; |
541 size_t outlen = 0; |
530 size_t ivl = enc->ivlen; |
542 size_t ivl = enc->ivlen; |
531 if(in_len != 0) { |
543 if(in_len != 0) { |
532 outlen = len + 32; |
544 outlen = len + 32; |
533 out = malloc(outlen + ivl); |
545 out = malloc(outlen + ivl); |
534 if(enc->iv) { |
546 if(ivl > 0) { |
535 memcpy(out, enc->iv, ivl); |
547 memcpy(out, enc->iv, ivl); |
536 } |
548 } |
537 |
549 |
538 CCCryptorStatus status; |
550 CCCryptorStatus status; |
539 size_t avail = outlen; |
551 size_t avail = outlen; |
553 } |
565 } |
554 enc->tmp = (char*)out; |
566 enc->tmp = (char*)out; |
555 enc->tmplen = outlen + ivl; |
567 enc->tmplen = outlen + ivl; |
556 enc->tmpoff = 0; |
568 enc->tmpoff = 0; |
557 |
569 |
558 if(enc->iv) { |
570 if(enc->ivlen > 0) { |
559 free(enc->iv); |
|
560 enc->iv = NULL; |
|
561 enc->ivlen = 0; |
571 enc->ivlen = 0; |
562 } |
572 } |
563 |
573 |
564 free(in); |
574 free(in); |
565 |
575 |
566 return aes_read(buf, s, n, enc); |
576 return aes_read(buf, s, n, enc); |
|
577 } |
|
578 |
|
579 int aes_encrypter_reset(AESEncrypter *enc, curl_off_t offset, int origin) { |
|
580 if(origin != SEEK_SET || offset != 0 || !enc->seek) { |
|
581 return CURL_SEEKFUNC_CANTSEEK; |
|
582 } |
|
583 |
|
584 enc->ivlen = 16; |
|
585 if(enc->seek(enc->stream, 0, SEEK_SET) != 0) { |
|
586 return CURL_SEEKFUNC_FAIL; |
|
587 } |
|
588 return CURL_SEEKFUNC_OK; |
567 } |
589 } |
568 |
590 |
569 void aes_encrypter_close(AESEncrypter *enc) { |
591 void aes_encrypter_close(AESEncrypter *enc) { |
570 if(enc->tmp) { |
592 if(enc->tmp) { |
571 free(enc->tmp); |
593 free(enc->tmp); |