diff -r 7bb47ddc1b5e -r ff477f1f7765 libidav/crypto.c --- a/libidav/crypto.c Sun Oct 07 09:14:03 2018 +0200 +++ b/libidav/crypto.c Thu Oct 11 19:29:45 2018 +0200 @@ -137,7 +137,7 @@ } -AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) { +AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func, dav_seek_func seek_func) { unsigned char *iv = malloc(16); if(!RAND_bytes(iv, 16)) { free(iv); @@ -148,6 +148,7 @@ SHA256_Init(&enc->sha256); enc->stream = stream; enc->read = read_func; + enc->seek = seek_func; enc->tmp = NULL; enc->tmplen = 0; enc->tmpoff = 0; @@ -199,7 +200,7 @@ if(in_len != 0) { outlen = len + 32; out = malloc(outlen + ivl); - if(enc->iv) { + if(ivl > 0) { memcpy(out, enc->iv, ivl); } EVP_EncryptUpdate(enc->ctx, out + ivl, &outlen, in, in_len); @@ -218,9 +219,7 @@ enc->tmplen = outlen + ivl; enc->tmpoff = 0; - if(enc->iv) { - free(enc->iv); - enc->iv = NULL; + if(enc->ivlen > 0) { enc->ivlen = 0; } @@ -241,6 +240,18 @@ free(enc); } +int aes_encrypter_reset(AESEncrypter *enc, curl_off_t offset, int origin) { + if(origin != SEEK_SET || offset != 0 || !enc->seek) { + return CURL_SEEKFUNC_CANTSEEK; + } + + enc->ivlen = 16; + if(enc->seek(enc->stream, 0, SEEK_SET) != 0) { + return CURL_SEEKFUNC_FAIL; + } + return CURL_SEEKFUNC_OK; +} + char* aes_encrypt(char *in, size_t len, DavKey *key) { unsigned char iv[16]; @@ -468,7 +479,7 @@ } -AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) { +AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func, dav_seek_func seek_func) { unsigned char *iv = malloc(16); if(dav_rand_bytes(iv, 16)) { return NULL; @@ -490,6 +501,7 @@ CC_SHA256_Init(&enc->sha256); enc->stream = stream; enc->read = read_func; + enc->seek = seek_func; enc->tmp = NULL; enc->tmplen = 0; enc->tmpoff = 0; @@ -531,7 +543,7 @@ if(in_len != 0) { outlen = len + 32; out = malloc(outlen + ivl); - if(enc->iv) { + if(ivl > 0) { memcpy(out, enc->iv, ivl); } @@ -555,9 +567,7 @@ enc->tmplen = outlen + ivl; enc->tmpoff = 0; - if(enc->iv) { - free(enc->iv); - enc->iv = NULL; + if(enc->ivlen > 0) { enc->ivlen = 0; } @@ -566,6 +576,18 @@ return aes_read(buf, s, n, enc); } +int aes_encrypter_reset(AESEncrypter *enc, curl_off_t offset, int origin) { + if(origin != SEEK_SET || offset != 0 || !enc->seek) { + return CURL_SEEKFUNC_CANTSEEK; + } + + enc->ivlen = 16; + if(enc->seek(enc->stream, 0, SEEK_SET) != 0) { + return CURL_SEEKFUNC_FAIL; + } + return CURL_SEEKFUNC_OK; +} + void aes_encrypter_close(AESEncrypter *enc) { if(enc->tmp) { free(enc->tmp);