libidav/crypto.c

changeset 874
8f0add189b2f
parent 873
96aade0d6246
parent 872
d24de29ad1fe
equal deleted inserted replaced
873:96aade0d6246 874:8f0add189b2f
846 DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX)); 846 DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX));
847 CC_SHA256_Init(ctx); 847 CC_SHA256_Init(ctx);
848 return ctx; 848 return ctx;
849 } 849 }
850 850
851 void dav_sha256_init(DAV_SHA_CTX *ctx) {
852 CC_SHA256_Init(ctx);
853 }
854
851 void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t len) { 855 void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t len) {
852 CC_SHA256_Update(ctx, data, len); 856 CC_SHA256_Update(ctx, data, len);
853 } 857 }
854 858
855 void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf) { 859 void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf) {
856 CC_SHA256_Final(buf, ctx); 860 CC_SHA256_Final(buf, ctx);
857 free(ctx);
858 } 861 }
859 862
860 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) { 863 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) {
861 if(!password) { 864 if(!password) {
862 return NULL; 865 return NULL;
1420 char* dav_create_hash(const char *data, size_t len) { 1423 char* dav_create_hash(const char *data, size_t len) {
1421 unsigned char hash[DAV_SHA256_DIGEST_LENGTH]; 1424 unsigned char hash[DAV_SHA256_DIGEST_LENGTH];
1422 DAV_SHA_CTX *ctx = dav_sha256_create(); 1425 DAV_SHA_CTX *ctx = dav_sha256_create();
1423 if(ctx) { 1426 if(ctx) {
1424 dav_sha256_update(ctx, data, len); 1427 dav_sha256_update(ctx, data, len);
1425 dav_sha256_final(ctx, hash); 1428 dav_sha256_final_free(ctx, hash);
1426 } 1429 }
1427 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH); 1430 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);
1428 } 1431 }
1429 1432
1430 DAV_SHA_CTX* dav_sha256_create(void) { 1433 DAV_SHA_CTX* dav_sha256_create(void) {
1446 void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf) { 1449 void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf) {
1447 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0); 1450 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0);
1448 1451
1449 // cleanup 1452 // cleanup
1450 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject); 1453 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
1451 free(ctx);
1452 } 1454 }
1453 1455
1454 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) { 1456 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) {
1455 if(!password) { 1457 if(!password) {
1456 return NULL; 1458 return NULL;
1512 memcpy(key->data, keydata, keylen); 1514 memcpy(key->data, keydata, keylen);
1513 return key; 1515 return key;
1514 } 1516 }
1515 #endif 1517 #endif
1516 1518
1517 1519 void dav_sha256_final_free(DAV_SHA_CTX *ctx, unsigned char *buf) {
1520 dav_sha256_final(ctx, buf);
1521 free(ctx);
1522 }
1518 1523
1519 CxBuffer* aes_encrypt_buffer(CxBuffer *in, DavKey *key) { 1524 CxBuffer* aes_encrypt_buffer(CxBuffer *in, DavKey *key) {
1520 CxBuffer *encbuf = cxBufferCreate(NULL, in->size, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); 1525 CxBuffer *encbuf = cxBufferCreate(NULL, in->size, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
1521 if(!encbuf) { 1526 if(!encbuf) {
1522 return NULL; 1527 return NULL;

mercurial