add sha512 hashing functions

Thu, 29 May 2025 15:17:01 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 29 May 2025 15:17:01 +0200
changeset 589
70ad04769cbf
parent 588
97e7c113b1dd
child 590
33ca0f9f276f

add sha512 hashing functions

src/server/util/hashing.c file | annotate | diff | comparison | revisions
src/server/util/hashing.h file | annotate | diff | comparison | revisions
--- a/src/server/util/hashing.c	Wed May 28 21:15:49 2025 +0200
+++ b/src/server/util/hashing.c	Thu May 29 15:17:01 2025 +0200
@@ -30,7 +30,7 @@
 
 #include <stdlib.h>
 
-WS_SHA1_CTX* ws_sha256_create(void) {
+WS_SHA1_CTX* ws_sha1_create(void) {
     WS_SHA1_CTX *ctx = malloc(sizeof(WS_SHA1_CTX));
     if(!ctx) {
         return NULL;
@@ -42,7 +42,7 @@
     return ctx;
 }
 
-WS_SHA256_CTX* ws_sha1_create(void) {
+WS_SHA256_CTX* ws_sha256_create(void) {
     WS_SHA256_CTX *ctx = malloc(sizeof(WS_SHA256_CTX));
     if(!ctx) {
         return NULL;
@@ -54,6 +54,18 @@
     return ctx;
 }
 
+WS_SHA1_CTX* ws_sha512_create(void) {
+    WS_SHA512_CTX *ctx = malloc(sizeof(WS_SHA512_CTX));
+    if(!ctx) {
+        return NULL;
+    }
+    if(ws_sha512_init(ctx)) {
+        free(ctx);
+        return NULL;
+    }
+    return ctx;
+}
+
 #if OPENSSL_VERSION_NUMBER < 0x30000000L
 
 void ws_sha1_init(WS_SHA1_CTX *ctx) {
@@ -69,19 +81,32 @@
     SHA_Final(md, ctx);
 }
 
-void ws_sha256_init(WS_SHA1_CTX *ctx) {
+void ws_sha256_init(WS_SHA256_CTX *ctx) {
     SHA_Init(ctx);
     return 0;
 }
 
-void ws_sha256_update(WS_SHA1_CTX *ctx, const void *data, size_t length) {
+void ws_sha256_update(WS_SHA256_CTX *ctx, const void *data, size_t length) {
     SHA_Update(ctx, data, length);
 }
 
-void ws_sha256_final(char *md, WS_SHA1_CTX *ctx) {
+void ws_sha256_final(char *md, WS_SHA256_CTX *ctx) {
     SHA_Final(md, ctx);
 }
 
+void ws_sha512_init(WS_SHA512_CTX *ctx) {
+    SHA512_Init(ctx);
+    return 0;
+}
+
+void ws_sha256_update(WS_SHA512_CTX *ctx, const void *data, size_t length) {
+    SHA512_Update(ctx, data, length);
+}
+
+void ws_sha256_final(char *md, WS_SHA512_CTX *ctx) {
+    SHA512_Final(md, ctx);
+}
+
 #else
 
 int ws_sha1_init(WS_SHA256_CTX *ctx) {
@@ -120,4 +145,22 @@
     EVP_DigestFinal(*ctx, md, NULL);
 }
 
+int ws_sha512_init(WS_SHA512_CTX *ctx) {
+    EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
+    if(!mdctx) {
+        return 1;
+    }
+    EVP_DigestInit_ex(mdctx, EVP_sha512(), NULL);
+    *ctx = mdctx;
+    return 0;
+}
+
+void ws_sha512_update(WS_SHA256_CTX *ctx, const char *data, size_t length) {
+    EVP_DigestUpdate(*ctx, data, length);
+}
+
+void ws_sha512_final(WS_SHA256_CTX *ctx, unsigned char *md) {
+    EVP_DigestFinal(*ctx, md, NULL);
+}
+
 #endif
--- a/src/server/util/hashing.h	Wed May 28 21:15:49 2025 +0200
+++ b/src/server/util/hashing.h	Thu May 29 15:17:01 2025 +0200
@@ -31,6 +31,7 @@
 
 #define WS_SHA1_DIGEST_LENGTH 20
 #define WS_SHA256_DIGEST_LENGTH 32
+#define WS_SHA512_DIGEST_LENGTH 32
 
 #ifdef __APPLE__
 /* macos */
@@ -39,7 +40,8 @@
 
 #define WS_AES_CTX              CCCryptorRef
 #define WS_SHA1_CTX             CC_SHA1_CTX
-#define WS_SHA_CTX              CC_SHA256_CTX
+#define WS_SHA256_CTX           CC_SHA256_CTX
+#define WS_SHA512_CTX           CC_SHA512_CTX
 
 #include <CommonCrypto/CommonCrypto.h>
 #include <CommonCrypto/CommonDigest.h>
@@ -70,6 +72,7 @@
 #define WS_AES_CTX              WinBCryptCTX
 #define WS_SHA1_CTX             WinBCryptSHACTX
 #define WS_SHA256_CTX           WinBCryptSHACTX
+#define WS_SHA512_CTX           WinBCryptSHACTX
 
 #else
 /* unix/linux */
@@ -82,9 +85,11 @@
 #if OPENSSL_VERSION_NUMBER < 0x30000000L
 #define WS_SHA256_CTX           SHA1_CTX
 #define WS_SHA256_CTX           SHA256_CTX
+#define WS_SHA512_CTX           SHA512_CTX
 #else
 #define WS_SHA1_CTX             EVP_MD_CTX*
 #define WS_SHA256_CTX           EVP_MD_CTX*
+#define WS_SHA512_CTX           EVP_MD_CTX*
 #endif
 
 
@@ -97,6 +102,10 @@
 #define SHA256_Init     SHA256Init
 #define SHA256_Update   SHA256Update
 #define SHA256_Final    SHA256Final
+
+#define SHA512_Init     SHA512Init
+#define SHA512_Update   SHA512Update
+#define SHA512_Final    SHA512Final
 #else
 #include <openssl/sha.h>
 #endif
@@ -113,6 +122,11 @@
 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len);
 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf);
 
+int ws_sha512_init(WS_SHA512_CTX *ctx);
+WS_SHA512_CTX* ws_sha512_create(void);
+void ws_sha512_update(WS_SHA512_CTX *ctx, const char *data, size_t len);
+void ws_sha512_final(WS_SHA512_CTX *ctx, unsigned char *buf);
+
 
 #ifdef __cplusplus
 }

mercurial