use newer openssl hashing functions to fix warnings default tip

4 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 01 Apr 2025 21:48:14 +0200 (4 days ago)
changeset 857
03982275f29b
parent 856
0f1349ffa719

use newer openssl hashing functions to fix warnings

dav/finfo.c file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
libidav/crypto.c file | annotate | diff | comparison | revisions
libidav/crypto.h file | annotate | diff | comparison | revisions
libidav/resource.c file | annotate | diff | comparison | revisions
libidav/utils.c file | annotate | diff | comparison | revisions
--- a/dav/finfo.c	Tue Apr 01 20:30:02 2025 +0200
+++ b/dav/finfo.c	Tue Apr 01 21:48:14 2025 +0200
@@ -230,7 +230,7 @@
     xattr->names = calloc(nelm, sizeof(char*));
     xattr->values = calloc(nelm, sizeof(cxmutstr));
     
-    DAV_SHA_CTX *sha256 = dav_hash_init();
+    DAV_SHA_CTX *sha256 = dav_sha256_create();
     
     size_t nattr = 0;
     for(int i=0;i<nelm;i++) {
@@ -245,8 +245,8 @@
         ssize_t valuelen = 0;
         char *value = xattr_get(path, attributes[i], &valuelen);
         if(valuelen >= 0) {
-            dav_hash_update(sha256, attributes[i], strlen(attributes[i]));
-            dav_hash_update(sha256, value, valuelen);
+            dav_sha256_update(sha256, attributes[i], strlen(attributes[i]));
+            dav_sha256_update(sha256, value, valuelen);
             // add name and value
             xattr->names[nattr] = attributes[i];
             cxmutstr v;
@@ -263,7 +263,7 @@
     xattr->nattr = nattr;
     
     unsigned char hash[DAV_SHA256_DIGEST_LENGTH];
-    dav_hash_final(sha256, hash);
+    dav_sha256_final(sha256, hash);
     xattr->hash = util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);
     
     free(attributes);
--- a/dav/sync.c	Tue Apr 01 20:30:02 2025 +0200
+++ b/dav/sync.c	Tue Apr 01 21:48:14 2025 +0200
@@ -4220,10 +4220,10 @@
     uint32_t session_flags = res->session->flags;
     res->session->flags ^= DAV_SESSION_ENCRYPT_NAME;
     
-    DAV_SHA_CTX *sha = dav_hash_init();
+    DAV_SHA_CTX *sha = dav_sha256_create();
     
     while((r = fread(buffer, 1, blocksize, in)) > 0) {
-        dav_hash_update(sha, buffer, r);
+        dav_sha256_update(sha, buffer, r);
         
         int upload_block = 0;
         char *block_hash = dav_create_hash(buffer, r);
@@ -4286,7 +4286,7 @@
     
     // set content-hash
     unsigned char content_hash[DAV_SHA256_DIGEST_LENGTH];
-    dav_hash_final(sha, content_hash);
+    dav_sha256_final(sha, content_hash);
     sync_set_content_hash(res, content_hash);
     local->hash = util_hexstr(content_hash, DAV_SHA256_DIGEST_LENGTH);
     
--- a/libidav/crypto.c	Tue Apr 01 20:30:02 2025 +0200
+++ b/libidav/crypto.c	Tue Apr 01 21:48:14 2025 +0200
@@ -66,7 +66,7 @@
 
 AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) {
     AESDecrypter *dec = calloc(1, sizeof(AESDecrypter));
-    SHA256_Init(&dec->sha256);
+    dav_sha256_init(&dec->sha256);
     dec->stream = stream;
     dec->write = write_func;
     dec->key = key;
@@ -122,7 +122,7 @@
     unsigned char *out = malloc(outlen);
     EVP_DecryptUpdate(dec->ctx, out, &outlen, buf, len);
     ssize_t wlen = dec->write(out, 1, outlen, dec->stream);
-    SHA256_Update(&dec->sha256, out, wlen);
+    dav_sha256_update(&dec->sha256, out, wlen);
     free(out);
     return (s*n) / s;
 }
@@ -133,7 +133,7 @@
         int len = 0;
         EVP_DecryptFinal_ex(dec->ctx, out, &len);
         dec->write(out, 1, len, dec->stream);
-        SHA256_Update(&dec->sha256, out, len);
+        dav_sha256_update(&dec->sha256, out, len);
         free(out);
         //EVP_CIPHER_CTX_cleanup(&dec->ctx);
         EVP_CIPHER_CTX_free(dec->ctx);
@@ -153,7 +153,7 @@
     }
     
     AESEncrypter *enc = malloc(sizeof(AESEncrypter));
-    SHA256_Init(&enc->sha256);
+    dav_sha256_init(&enc->sha256);
     enc->stream = stream;
     enc->read = read_func;
     enc->seek = seek_func;
@@ -200,7 +200,7 @@
     void *in = malloc(len);
     size_t in_len = enc->read(in, 1, len, enc->stream);
     
-    SHA256_Update(&enc->sha256, in, in_len);
+    dav_sha256_update(&enc->sha256, in, in_len);
     
     unsigned char *out = NULL;
     int outlen = 0;
@@ -357,33 +357,56 @@
 
 
 void dav_get_hash(DAV_SHA_CTX *sha256, unsigned char *buf){
-    SHA256_Final((unsigned char*)buf, sha256);
+    dav_sha256_final(sha256, (unsigned char*)buf);
 }
 
 char* dav_create_hash(const char *data, size_t len) {
     unsigned char hash[DAV_SHA256_DIGEST_LENGTH];
     DAV_SHA_CTX ctx;
-    SHA256_Init(&ctx);
-    SHA256_Update(&ctx, data, len);
-    SHA256_Final(hash, &ctx);
+    dav_sha256_init(&ctx);
+    dav_sha256_update(&ctx, data, len);
+    dav_sha256_final(&ctx, hash);
     return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);
 }
 
-DAV_SHA_CTX* dav_hash_init(void) {
+DAV_SHA_CTX* dav_sha256_create(void) {
     DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX));
-    SHA256_Init(ctx);
+    dav_sha256_init(ctx);
     return ctx;
 }
 
-void dav_hash_update(DAV_SHA_CTX *ctx, const char *data, size_t len) {
-    SHA256_Update(ctx, data, len);
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+
+void dav_sha256_init(DAV_SHA_CTX *ctx) {
+    SHA256_Init(ctx);
+}
+
+void dav_sha256_update(DAV_SHA_CTX *ctx, const void *data, size_t length) {
+    SHA256_Update(ctx, data, length);
+}
+
+void dav_sha256_final(char *md, DAV_SHA_CTX *ctx) {
+    SHA256_Final(md, ctx);
 }
 
-void dav_hash_final(DAV_SHA_CTX *ctx, unsigned char *buf) {
-    SHA256_Final(buf, ctx);
-    free(ctx);
+#else
+
+void dav_sha256_init(DAV_SHA_CTX *ctx) {
+    EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
+    EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
+    *ctx = mdctx;
 }
 
+void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t length) {
+    EVP_DigestUpdate(*ctx, data, length);
+}
+
+void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *md) {
+    EVP_DigestFinal(*ctx, md, NULL);
+}
+
+#endif
+
 #if OPENSSL_VERSION_NUMBER < 0x10100000L
 static int crypto_pw2key_error = 0;
 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc) {
@@ -819,17 +842,17 @@
     return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);
 }
 
-DAV_SHA_CTX* dav_hash_init(void) {
+DAV_SHA_CTX* dav_sha256_create(void) {
     DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX));
     CC_SHA256_Init(ctx);
     return ctx;
 }
 
-void dav_hash_update(DAV_SHA_CTX *ctx, const char *data, size_t len) {
+void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t len) {
     CC_SHA256_Update(ctx, data, len);
 }
 
-void dav_hash_final(DAV_SHA_CTX *ctx, unsigned char *buf) {
+void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf) {
     CC_SHA256_Final(buf, ctx);
     free(ctx);
 }
@@ -1396,15 +1419,15 @@
 
 char* dav_create_hash(const char *data, size_t len) {
     unsigned char hash[DAV_SHA256_DIGEST_LENGTH];
-    DAV_SHA_CTX *ctx = dav_hash_init();
+    DAV_SHA_CTX *ctx = dav_sha256_create();
     if(ctx) {
-        dav_hash_update(ctx, data, len);
-        dav_hash_final(ctx, hash);
+        dav_sha256_update(ctx, data, len);
+        dav_sha256_final(ctx, hash);
     }
     return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);
 }
 
-DAV_SHA_CTX* dav_hash_init(void) {
+DAV_SHA_CTX* dav_sha256_create(void) {
     DAV_SHA_CTX *ctx = malloc(sizeof(DAV_SHA_CTX));
     if(!ctx) {
         return NULL;
@@ -1416,11 +1439,11 @@
     return ctx;
 }
 
-void dav_hash_update(DAV_SHA_CTX *ctx, const char *data, size_t len) {
+void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t len) {
     BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0);
 }
 
-void dav_hash_final(DAV_SHA_CTX *ctx, unsigned char *buf) {
+void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf) {
     BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0);
     
     // cleanup
--- a/libidav/crypto.h	Tue Apr 01 20:30:02 2025 +0200
+++ b/libidav/crypto.h	Tue Apr 01 21:48:14 2025 +0200
@@ -74,14 +74,20 @@
 #else
 /* unix/linux */
 
+#include <openssl/evp.h>
+#include <openssl/rand.h>
+
 #define DAV_USE_OPENSSL
 
 #define DAV_AES_CTX              EVP_CIPHER_CTX*
+
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
 #define DAV_SHA_CTX              SHA256_CTX
+#else
+#define DAV_SHA_CTX              EVP_MD_CTX*
+#endif
 #define DAV_SHA256_DIGEST_LENGTH 32
 
-#include <openssl/evp.h>
-#include <openssl/rand.h>
 
 #if defined(__sun) && defined(__SunOS_5_10)
 #include <sha2.h>
@@ -149,9 +155,10 @@
 
 char* dav_create_hash(const char *data, size_t len);
 
-DAV_SHA_CTX* dav_hash_init(void);
-void dav_hash_update(DAV_SHA_CTX *ctx, const char *data, size_t len);
-void dav_hash_final(DAV_SHA_CTX *ctx, unsigned char *buf);
+void dav_sha256_init(DAV_SHA_CTX *ctx);
+DAV_SHA_CTX* dav_sha256_create(void);
+void dav_sha256_update(DAV_SHA_CTX *ctx, const char *data, size_t len);
+void dav_sha256_final(DAV_SHA_CTX *ctx, unsigned char *buf);
 
 DavKey* dav_pw2key(const char *password, const unsigned char *salt, int saltlen, int pwfunc, int enc);
 
--- a/libidav/resource.c	Tue Apr 01 20:30:02 2025 +0200
+++ b/libidav/resource.c	Tue Apr 01 21:48:14 2025 +0200
@@ -823,11 +823,11 @@
 static size_t dav_read_h(void *buf, size_t size, size_t nelm, void *stream) {
     HashStream *s = stream;
     if(!s->sha) {
-        s->sha = dav_hash_init();
+        s->sha = dav_sha256_create();
     }
      
     size_t r = s->read(buf, size, nelm, s->stream);
-    dav_hash_update(s->sha, buf, r);
+    dav_sha256_update(s->sha, buf, r);
     return r;
 }
 
@@ -835,7 +835,7 @@
     HashStream *s = stream;
     if(offset == 0 && whence == SEEK_SET) {
         unsigned char buf[DAV_SHA256_DIGEST_LENGTH];
-        dav_hash_final(s->sha, buf);
+        dav_sha256_final(s->sha, buf);
         s->sha = NULL;
     } else {
         s->error = 1;
@@ -938,7 +938,7 @@
                     data->length);
             
             if(hstr.sha) {
-                dav_hash_final(hstr.sha, (unsigned char*)data->hash);
+                dav_sha256_final(hstr.sha, (unsigned char*)data->hash);
                 char *hash = util_hexstr((unsigned char*)data->hash, 32);
                 dav_set_string_property_ns(res, DAV_NS, "content-hash", hash);
                 free(hash);
--- a/libidav/utils.c	Tue Apr 01 20:30:02 2025 +0200
+++ b/libidav/utils.c	Tue Apr 01 21:48:14 2025 +0200
@@ -1290,16 +1290,16 @@
         return NULL;
     }
     
-    DAV_SHA_CTX *sha = dav_hash_init();
+    DAV_SHA_CTX *sha = dav_sha256_create();
     char *buf = malloc(16384);
     
     size_t r;
     while((r = fread(buf, 1, 16384, in)) > 0) {
-        dav_hash_update(sha, buf, r);
+        dav_sha256_update(sha, buf, r);
     }
     
     unsigned char hash[DAV_SHA256_DIGEST_LENGTH];
-    dav_hash_final(sha, hash);
+    dav_sha256_final(sha, hash);
     free(buf);
     fclose(in);
     

mercurial