libidav/crypto.c

changeset 478
baa63fef5c5c
parent 470
6bf798ad3aec
child 488
29b979ca8750
equal deleted inserted replaced
477:9a406db6729b 478:baa63fef5c5c
139 void aes_decrypter_close(AESDecrypter *dec) { 139 void aes_decrypter_close(AESDecrypter *dec) {
140 free(dec); 140 free(dec);
141 } 141 }
142 142
143 143
144 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) { 144 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func, dav_seek_func seek_func) {
145 unsigned char *iv = malloc(16); 145 unsigned char *iv = malloc(16);
146 if(!RAND_bytes(iv, 16)) { 146 if(!RAND_bytes(iv, 16)) {
147 free(iv); 147 free(iv);
148 return NULL; 148 return NULL;
149 } 149 }
150 150
151 AESEncrypter *enc = malloc(sizeof(AESEncrypter)); 151 AESEncrypter *enc = malloc(sizeof(AESEncrypter));
152 SHA256_Init(&enc->sha256); 152 SHA256_Init(&enc->sha256);
153 enc->stream = stream; 153 enc->stream = stream;
154 enc->read = read_func; 154 enc->read = read_func;
155 enc->seek = seek_func;
155 enc->tmp = NULL; 156 enc->tmp = NULL;
156 enc->tmplen = 0; 157 enc->tmplen = 0;
157 enc->tmpoff = 0; 158 enc->tmpoff = 0;
158 enc->end = 0; 159 enc->end = 0;
159 enc->iv = iv; 160 enc->iv = iv;
201 int outlen = 0; 202 int outlen = 0;
202 size_t ivl = enc->ivlen; 203 size_t ivl = enc->ivlen;
203 if(in_len != 0) { 204 if(in_len != 0) {
204 outlen = len + 32; 205 outlen = len + 32;
205 out = malloc(outlen + ivl); 206 out = malloc(outlen + ivl);
206 if(enc->iv) { 207 if(ivl > 0) {
207 memcpy(out, enc->iv, ivl); 208 memcpy(out, enc->iv, ivl);
208 } 209 }
209 EVP_EncryptUpdate(enc->ctx, out + ivl, &outlen, in, in_len); 210 EVP_EncryptUpdate(enc->ctx, out + ivl, &outlen, in, in_len);
210 if(in_len != len) { 211 if(in_len != len) {
211 int newoutlen = 16; 212 int newoutlen = 16;
220 } 221 }
221 enc->tmp = (char*)out; 222 enc->tmp = (char*)out;
222 enc->tmplen = outlen + ivl; 223 enc->tmplen = outlen + ivl;
223 enc->tmpoff = 0; 224 enc->tmpoff = 0;
224 225
225 if(enc->iv) { 226 if(enc->ivlen > 0) {
226 free(enc->iv);
227 enc->iv = NULL;
228 enc->ivlen = 0; 227 enc->ivlen = 0;
229 } 228 }
230 229
231 free(in); 230 free(in);
232 231
241 free(enc->iv); 240 free(enc->iv);
242 } 241 }
243 //EVP_CIPHER_CTX_cleanup(&enc->ctx); 242 //EVP_CIPHER_CTX_cleanup(&enc->ctx);
244 EVP_CIPHER_CTX_free(enc->ctx); 243 EVP_CIPHER_CTX_free(enc->ctx);
245 free(enc); 244 free(enc);
245 }
246
247 int aes_encrypter_reset(AESEncrypter *enc, curl_off_t offset, int origin) {
248 if(origin != SEEK_SET || offset != 0 || !enc->seek) {
249 return CURL_SEEKFUNC_CANTSEEK;
250 }
251
252 enc->ivlen = 16;
253 if(enc->seek(enc->stream, 0, SEEK_SET) != 0) {
254 return CURL_SEEKFUNC_FAIL;
255 }
256 return CURL_SEEKFUNC_OK;
246 } 257 }
247 258
248 259
249 char* aes_encrypt(char *in, size_t len, DavKey *key) { 260 char* aes_encrypt(char *in, size_t len, DavKey *key) {
250 unsigned char iv[16]; 261 unsigned char iv[16];
527 538
528 void aes_decrypter_close(AESDecrypter *dec) { 539 void aes_decrypter_close(AESDecrypter *dec) {
529 540
530 } 541 }
531 542
532 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) { 543 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func, dav_seek_func seek_func) {
533 unsigned char *iv = malloc(16); 544 unsigned char *iv = malloc(16);
534 if(dav_rand_bytes(iv, 16)) { 545 if(dav_rand_bytes(iv, 16)) {
535 return NULL; 546 return NULL;
536 } 547 }
537 548
549 AESEncrypter *enc = malloc(sizeof(AESEncrypter)); 560 AESEncrypter *enc = malloc(sizeof(AESEncrypter));
550 enc->ctx = cryptor; 561 enc->ctx = cryptor;
551 CC_SHA256_Init(&enc->sha256); 562 CC_SHA256_Init(&enc->sha256);
552 enc->stream = stream; 563 enc->stream = stream;
553 enc->read = read_func; 564 enc->read = read_func;
565 enc->seek = seek_func;
554 enc->tmp = NULL; 566 enc->tmp = NULL;
555 enc->tmplen = 0; 567 enc->tmplen = 0;
556 enc->tmpoff = 0; 568 enc->tmpoff = 0;
557 enc->end = 0; 569 enc->end = 0;
558 enc->iv = iv; 570 enc->iv = iv;
590 size_t outlen = 0; 602 size_t outlen = 0;
591 size_t ivl = enc->ivlen; 603 size_t ivl = enc->ivlen;
592 if(in_len != 0) { 604 if(in_len != 0) {
593 outlen = len + 32; 605 outlen = len + 32;
594 out = malloc(outlen + ivl); 606 out = malloc(outlen + ivl);
595 if(enc->iv) { 607 if(ivl > 0) {
596 memcpy(out, enc->iv, ivl); 608 memcpy(out, enc->iv, ivl);
597 } 609 }
598 610
599 CCCryptorStatus status; 611 CCCryptorStatus status;
600 size_t avail = outlen; 612 size_t avail = outlen;
614 } 626 }
615 enc->tmp = (char*)out; 627 enc->tmp = (char*)out;
616 enc->tmplen = outlen + ivl; 628 enc->tmplen = outlen + ivl;
617 enc->tmpoff = 0; 629 enc->tmpoff = 0;
618 630
619 if(enc->iv) { 631 if(enc->ivlen > 0) {
620 free(enc->iv);
621 enc->iv = NULL;
622 enc->ivlen = 0; 632 enc->ivlen = 0;
623 } 633 }
624 634
625 free(in); 635 free(in);
626 636
627 return aes_read(buf, s, n, enc); 637 return aes_read(buf, s, n, enc);
638 }
639
640 int aes_encrypter_reset(AESEncrypter *enc, curl_off_t offset, int origin) {
641 if(origin != SEEK_SET || offset != 0 || !enc->seek) {
642 return CURL_SEEKFUNC_CANTSEEK;
643 }
644
645 enc->ivlen = 16;
646 if(enc->seek(enc->stream, 0, SEEK_SET) != 0) {
647 return CURL_SEEKFUNC_FAIL;
648 }
649 return CURL_SEEKFUNC_OK;
628 } 650 }
629 651
630 void aes_encrypter_close(AESEncrypter *enc) { 652 void aes_encrypter_close(AESEncrypter *enc) {
631 if(enc->tmp) { 653 if(enc->tmp) {
632 free(enc->tmp); 654 free(enc->tmp);
766 UCX_BUFFER_AUTOEXTEND); 788 UCX_BUFFER_AUTOEXTEND);
767 789
768 AESEncrypter *enc = aes_encrypter_new( 790 AESEncrypter *enc = aes_encrypter_new(
769 key, 791 key,
770 in, 792 in,
771 (dav_read_func)ucx_buffer_read); 793 (dav_read_func)ucx_buffer_read,
794 NULL);
772 if(!enc) { 795 if(!enc) {
773 ucx_buffer_free(encbuf); 796 ucx_buffer_free(encbuf);
774 return NULL; 797 return NULL;
775 } 798 }
776 799

mercurial