libidav/crypto.c

branch
v1.2
changeset 481
ff477f1f7765
parent 465
70ed56e56122
child 484
9435cb1ddf76
equal deleted inserted replaced
480:7bb47ddc1b5e 481:ff477f1f7765
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;
216 } 217 }
217 enc->tmp = (char*)out; 218 enc->tmp = (char*)out;
218 enc->tmplen = outlen + ivl; 219 enc->tmplen = outlen + ivl;
219 enc->tmpoff = 0; 220 enc->tmpoff = 0;
220 221
221 if(enc->iv) { 222 if(enc->ivlen > 0) {
222 free(enc->iv);
223 enc->iv = NULL;
224 enc->ivlen = 0; 223 enc->ivlen = 0;
225 } 224 }
226 225
227 free(in); 226 free(in);
228 227
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
488 AESEncrypter *enc = malloc(sizeof(AESEncrypter)); 499 AESEncrypter *enc = malloc(sizeof(AESEncrypter));
489 enc->ctx = cryptor; 500 enc->ctx = cryptor;
490 CC_SHA256_Init(&enc->sha256); 501 CC_SHA256_Init(&enc->sha256);
491 enc->stream = stream; 502 enc->stream = stream;
492 enc->read = read_func; 503 enc->read = read_func;
504 enc->seek = seek_func;
493 enc->tmp = NULL; 505 enc->tmp = NULL;
494 enc->tmplen = 0; 506 enc->tmplen = 0;
495 enc->tmpoff = 0; 507 enc->tmpoff = 0;
496 enc->end = 0; 508 enc->end = 0;
497 enc->iv = iv; 509 enc->iv = iv;
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);

mercurial