| 64 return !RAND_bytes(buf, len); |
64 return !RAND_bytes(buf, len); |
| 65 } |
65 } |
| 66 |
66 |
| 67 AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) { |
67 AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) { |
| 68 AESDecrypter *dec = calloc(1, sizeof(AESDecrypter)); |
68 AESDecrypter *dec = calloc(1, sizeof(AESDecrypter)); |
| 69 SHA256_Init(&dec->sha256); |
69 dav_sha256_init(&dec->sha256); |
| 70 dec->stream = stream; |
70 dec->stream = stream; |
| 71 dec->write = write_func; |
71 dec->write = write_func; |
| 72 dec->key = key; |
72 dec->key = key; |
| 73 dec->init = 0; |
73 dec->init = 0; |
| 74 dec->ivpos = 0; |
74 dec->ivpos = 0; |
| 120 |
120 |
| 121 int outlen = len + 16; |
121 int outlen = len + 16; |
| 122 unsigned char *out = malloc(outlen); |
122 unsigned char *out = malloc(outlen); |
| 123 EVP_DecryptUpdate(dec->ctx, out, &outlen, buf, len); |
123 EVP_DecryptUpdate(dec->ctx, out, &outlen, buf, len); |
| 124 ssize_t wlen = dec->write(out, 1, outlen, dec->stream); |
124 ssize_t wlen = dec->write(out, 1, outlen, dec->stream); |
| 125 SHA256_Update(&dec->sha256, out, wlen); |
125 dav_sha256_update(&dec->sha256, out, wlen); |
| 126 free(out); |
126 free(out); |
| 127 return (s*n) / s; |
127 return (s*n) / s; |
| 128 } |
128 } |
| 129 |
129 |
| 130 void aes_decrypter_shutdown(AESDecrypter *dec) { |
130 void aes_decrypter_shutdown(AESDecrypter *dec) { |
| 131 if(dec->init) { |
131 if(dec->init) { |
| 132 void *out = malloc(128); |
132 void *out = malloc(128); |
| 133 int len = 0; |
133 int len = 0; |
| 134 EVP_DecryptFinal_ex(dec->ctx, out, &len); |
134 EVP_DecryptFinal_ex(dec->ctx, out, &len); |
| 135 dec->write(out, 1, len, dec->stream); |
135 dec->write(out, 1, len, dec->stream); |
| 136 SHA256_Update(&dec->sha256, out, len); |
136 dav_sha256_update(&dec->sha256, out, len); |
| 137 free(out); |
137 free(out); |
| 138 //EVP_CIPHER_CTX_cleanup(&dec->ctx); |
138 //EVP_CIPHER_CTX_cleanup(&dec->ctx); |
| 139 EVP_CIPHER_CTX_free(dec->ctx); |
139 EVP_CIPHER_CTX_free(dec->ctx); |
| 140 } |
140 } |
| 141 } |
141 } |
| 198 } |
198 } |
| 199 |
199 |
| 200 void *in = malloc(len); |
200 void *in = malloc(len); |
| 201 size_t in_len = enc->read(in, 1, len, enc->stream); |
201 size_t in_len = enc->read(in, 1, len, enc->stream); |
| 202 |
202 |
| 203 SHA256_Update(&enc->sha256, in, in_len); |
203 dav_sha256_update(&enc->sha256, in, in_len); |
| 204 |
204 |
| 205 unsigned char *out = NULL; |
205 unsigned char *out = NULL; |
| 206 int outlen = 0; |
206 int outlen = 0; |
| 207 size_t ivl = enc->ivlen; |
207 size_t ivl = enc->ivlen; |
| 208 if(in_len != 0) { |
208 if(in_len != 0) { |
| 355 return (char*)out; |
355 return (char*)out; |
| 356 } |
356 } |
| 357 |
357 |
| 358 |
358 |
| 359 void dav_get_hash(DAV_SHA_CTX *sha256, unsigned char *buf){ |
359 void dav_get_hash(DAV_SHA_CTX *sha256, unsigned char *buf){ |
| 360 SHA256_Final((unsigned char*)buf, sha256); |
360 dav_sha256_final(sha256, (unsigned char*)buf); |
| 361 } |
361 } |
| 362 |
362 |
| 363 char* dav_create_hash(const char *data, size_t len) { |
363 char* dav_create_hash(const char *data, size_t len) { |
| 364 unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; |
364 unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; |
| 365 DAV_SHA_CTX ctx; |
365 DAV_SHA_CTX ctx; |
| 366 SHA256_Init(&ctx); |
366 dav_sha256_init(&ctx); |
| 367 SHA256_Update(&ctx, data, len); |
367 dav_sha256_update(&ctx, data, len); |
| 368 SHA256_Final(hash, &ctx); |
368 dav_sha256_final(&ctx, hash); |
| 369 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); |
369 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); |
| 370 } |
370 } |
| 371 |
371 |
| 372 DAV_SHA_CTX* dav_hash_init(void) { |
372 DAV_SHA_CTX* dav_sha256_create(void) { |
| 373 DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX)); |
373 DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX)); |
| |
374 dav_sha256_init(ctx); |
| |
375 return ctx; |
| |
376 } |
| |
377 |
| |
378 #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| |
379 |
| |
380 void dav_sha256_init(DAV_SHA_CTX *ctx) { |
| 374 SHA256_Init(ctx); |
381 SHA256_Init(ctx); |
| 375 return ctx; |
382 } |
| 376 } |
383 |
| 377 |
384 void dav_sha256_update(DAV_SHA_CTX *ctx, const void *data, size_t length) { |
| 378 void dav_hash_update(DAV_SHA_CTX *ctx, const char *data, size_t len) { |
385 SHA256_Update(ctx, data, length); |
| 379 SHA256_Update(ctx, data, len); |
386 } |
| 380 } |
387 |
| 381 |
388 void dav_sha256_final(char *md, DAV_SHA_CTX *ctx) { |
| 382 void dav_hash_final(DAV_SHA_CTX *ctx, unsigned char *buf) { |
389 SHA256_Final(md, ctx); |
| 383 SHA256_Final(buf, ctx); |
390 } |
| 384 free(ctx); |
391 |
| 385 } |
392 #else |
| |
393 |
| |
394 void dav_sha256_init(DAV_SHA_CTX *ctx) { |
| |
395 EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); |
| |
396 EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); |
| |
397 *ctx = mdctx; |
| |
398 } |
| |
399 |
| |
400 void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t length) { |
| |
401 EVP_DigestUpdate(*ctx, data, length); |
| |
402 } |
| |
403 |
| |
404 void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *md) { |
| |
405 EVP_DigestFinal(*ctx, md, NULL); |
| |
406 } |
| |
407 |
| |
408 #endif |
| 386 |
409 |
| 387 #if OPENSSL_VERSION_NUMBER < 0x10100000L |
410 #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 388 static int crypto_pw2key_error = 0; |
411 static int crypto_pw2key_error = 0; |
| 389 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) { |
412 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) { |
| 390 if(!crypto_pw2key_error) { |
413 if(!crypto_pw2key_error) { |
| 817 unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; |
840 unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; |
| 818 CC_SHA256((const unsigned char*)data, len, hash); |
841 CC_SHA256((const unsigned char*)data, len, hash); |
| 819 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); |
842 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); |
| 820 } |
843 } |
| 821 |
844 |
| 822 DAV_SHA_CTX* dav_hash_init(void) { |
845 DAV_SHA_CTX* dav_sha256_create(void) { |
| 823 DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX)); |
846 DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX)); |
| 824 CC_SHA256_Init(ctx); |
847 CC_SHA256_Init(ctx); |
| 825 return ctx; |
848 return ctx; |
| 826 } |
849 } |
| 827 |
850 |
| 828 void dav_hash_update(DAV_SHA_CTX *ctx, const char *data, size_t len) { |
851 void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t len) { |
| 829 CC_SHA256_Update(ctx, data, len); |
852 CC_SHA256_Update(ctx, data, len); |
| 830 } |
853 } |
| 831 |
854 |
| 832 void dav_hash_final(DAV_SHA_CTX *ctx, unsigned char *buf) { |
855 void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf) { |
| 833 CC_SHA256_Final(buf, ctx); |
856 CC_SHA256_Final(buf, ctx); |
| 834 free(ctx); |
857 free(ctx); |
| 835 } |
858 } |
| 836 |
859 |
| 837 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) { |
860 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) { |
| 1394 } |
1417 } |
| 1395 |
1418 |
| 1396 |
1419 |
| 1397 char* dav_create_hash(const char *data, size_t len) { |
1420 char* dav_create_hash(const char *data, size_t len) { |
| 1398 unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; |
1421 unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; |
| 1399 DAV_SHA_CTX *ctx = dav_hash_init(); |
1422 DAV_SHA_CTX *ctx = dav_sha256_create(); |
| 1400 if(ctx) { |
1423 if(ctx) { |
| 1401 dav_hash_update(ctx, data, len); |
1424 dav_sha256_update(ctx, data, len); |
| 1402 dav_hash_final(ctx, hash); |
1425 dav_sha256_final(ctx, hash); |
| 1403 } |
1426 } |
| 1404 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); |
1427 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); |
| 1405 } |
1428 } |
| 1406 |
1429 |
| 1407 DAV_SHA_CTX* dav_hash_init(void) { |
1430 DAV_SHA_CTX* dav_sha256_create(void) { |
| 1408 DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX)); |
1431 DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX)); |
| 1409 if(!ctx) { |
1432 if(!ctx) { |
| 1410 return NULL; |
1433 return NULL; |
| 1411 } |
1434 } |
| 1412 if(cng_hash_init(ctx)) { |
1435 if(cng_hash_init(ctx)) { |
| 1414 return NULL; |
1437 return NULL; |
| 1415 } |
1438 } |
| 1416 return ctx; |
1439 return ctx; |
| 1417 } |
1440 } |
| 1418 |
1441 |
| 1419 void dav_hash_update(DAV_SHA_CTX *ctx, const char *data, size_t len) { |
1442 void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t len) { |
| 1420 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0); |
1443 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0); |
| 1421 } |
1444 } |
| 1422 |
1445 |
| 1423 void dav_hash_final(DAV_SHA_CTX *ctx, unsigned char *buf) { |
1446 void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf) { |
| 1424 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0); |
1447 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0); |
| 1425 |
1448 |
| 1426 // cleanup |
1449 // cleanup |
| 1427 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject); |
1450 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject); |
| 1428 free(ctx); |
1451 free(ctx); |