# HG changeset patch # User Olaf Wintermann # Date 1748524621 -7200 # Node ID 70ad04769cbf645a7a0cfd50d18b82f3eed6e11e # Parent 97e7c113b1dde91e57b886d2454b1e52c79f4cf5 add sha512 hashing functions diff -r 97e7c113b1dd -r 70ad04769cbf src/server/util/hashing.c --- 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 -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 diff -r 97e7c113b1dd -r 70ad04769cbf src/server/util/hashing.h --- 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 #include @@ -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 #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 }