# HG changeset patch # User Olaf Wintermann # Date 1748459385 -7200 # Node ID c94800af0490f58f2c8b237ce9d964b34c99650d # Parent 9861e8ceaf96a20a1d56195e30a886ccd77bd857 add sha1 API diff -r 9861e8ceaf96 -r c94800af0490 src/server/util/hashing.c --- a/src/server/util/hashing.c Wed May 28 20:54:43 2025 +0200 +++ b/src/server/util/hashing.c Wed May 28 21:09:45 2025 +0200 @@ -30,39 +30,93 @@ #include -WS_SHA_CTX* ws_sha256_create(void) { - WS_SHA_CTX *ctx = malloc(sizeof(WS_SHA_CTX)); - ws_sha256_init(ctx); +WS_SHA1_CTX* ws_sha256_create(void) { + WS_SHA1_CTX *ctx = malloc(sizeof(WS_SHA1_CTX)); + if(!ctx) { + return NULL; + } + if(ws_sha1_init(ctx)) { + free(ctx); + return NULL; + } + return ctx; +} + +WS_SHA256_CTX* ws_sha1_create(void) { + WS_SHA256_CTX *ctx = malloc(sizeof(WS_SHA256_CTX)); + if(!ctx) { + return NULL; + } + if(ws_sha256_init(ctx)) { + free(ctx); + return NULL; + } return ctx; } #if OPENSSL_VERSION_NUMBER < 0x30000000L -void ws_sha256_init(WS_SHA_CTX *ctx) { +void ws_sha1_init(WS_SHA1_CTX *ctx) { SHA256_Init(ctx); + return 0; } -void ws_sha256_update(WS_SHA_CTX *ctx, const void *data, size_t length) { +void ws_sha1_update(WS_SHA1_CTX *ctx, const void *data, size_t length) { SHA256_Update(ctx, data, length); } -void ws_sha256_final(char *md, WS_SHA_CTX *ctx) { - SHA256_Final(md, ctx); +void ws_sha1_final(char *md, WS_SHA1_CTX *ctx) { + SHA_Final(md, ctx); +} + +void ws_sha256_init(WS_SHA1_CTX *ctx) { + SHA_Init(ctx); + return 0; +} + +void ws_sha256_update(WS_SHA1_CTX *ctx, const void *data, size_t length) { + SHA_Update(ctx, data, length); +} + +void ws_sha256_final(char *md, WS_SHA1_CTX *ctx) { + SHA_Final(md, ctx); } #else -void ws_sha256_init(WS_SHA_CTX *ctx) { +int ws_sha1_init(WS_SHA256_CTX *ctx) { EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); - EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); + if(!mdctx) { + return 1; + } + EVP_DigestInit_ex(mdctx, EVP_sha1(), NULL); *ctx = mdctx; + return 0; } -void ws_sha256_update(WS_SHA_CTX *ctx, const char *data, size_t length) { +void ws_sha1_update(WS_SHA256_CTX *ctx, const char *data, size_t length) { EVP_DigestUpdate(*ctx, data, length); } -void ws_sha256_final(WS_SHA_CTX *ctx, unsigned char *md) { +void ws_sha1_final(WS_SHA256_CTX *ctx, unsigned char *md) { + EVP_DigestFinal(*ctx, md, NULL); +} + +int ws_sha256_init(WS_SHA256_CTX *ctx) { + EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); + if(!mdctx) { + return 1; + } + EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); + *ctx = mdctx; + return 0; +} + +void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t length) { + EVP_DigestUpdate(*ctx, data, length); +} + +void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *md) { EVP_DigestFinal(*ctx, md, NULL); } diff -r 9861e8ceaf96 -r c94800af0490 src/server/util/hashing.h --- a/src/server/util/hashing.h Wed May 28 20:54:43 2025 +0200 +++ b/src/server/util/hashing.h Wed May 28 21:09:45 2025 +0200 @@ -29,6 +29,8 @@ #ifndef HASHING_H #define HASHING_H +#define WS_SHA1_DIGEST_LENGTH 20 +#define WS_SHA256_DIGEST_LENGTH 32 #ifdef __APPLE__ /* macos */ @@ -36,8 +38,8 @@ #define WS_CRYPTO_COMMON_CRYPTO #define WS_AES_CTX CCCryptorRef +#define WS_SHA1_CTX CC_SHA1_CTX #define WS_SHA_CTX CC_SHA256_CTX -#define WS_SHA256_DIGEST_LENGTH 32 #include #include @@ -66,8 +68,8 @@ } WinBCryptSHACTX; #define WS_AES_CTX WinBCryptCTX -#define WS_SHA_CTX WinBCryptSHACTX -#define WS_SHA256_DIGEST_LENGTH 32 +#define WS_SHA1_CTX WinBCryptSHACTX +#define WS_SHA256_CTX WinBCryptSHACTX #else /* unix/linux */ @@ -77,18 +79,21 @@ #define WS_USE_OPENSSL -#define WS_AES_CTX EVP_CIPHER_CTX* - #if OPENSSL_VERSION_NUMBER < 0x30000000L -#define WS_SHA_CTX SHA256_CTX +#define WS_SHA256_CTX SHA1_CTX +#define WS_SHA256_CTX SHA256_CTX #else -#define WS_SHA_CTX EVP_MD_CTX* +#define WS_SHA1_CTX EVP_MD_CTX* +#define WS_SHA256_CTX EVP_MD_CTX* #endif -#define WS_SHA256_DIGEST_LENGTH 32 #if defined(__sun) && defined(__SunOS_5_10) #include +#define SHA_Init SHAInit +#define SHA_Update SHAUpdate +#define SHA_Final SHAFinal + #define SHA256_Init SHA256Init #define SHA256_Update SHA256Update #define SHA256_Final SHA256Final @@ -98,11 +103,15 @@ #endif +int ws_sha1_init(WS_SHA1_CTX *ctx); +WS_SHA1_CTX* ws_sha1_create(void); +void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len); +void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf); -void ws_sha256_init(WS_SHA_CTX *ctx); -WS_SHA_CTX* ws_sha256_create(void); -void ws_sha256_update(WS_SHA_CTX *ctx, const char *data, size_t len); -void ws_sha256_final(WS_SHA_CTX *ctx, unsigned char *buf); +int ws_sha256_init(WS_SHA256_CTX *ctx); +WS_SHA256_CTX* ws_sha256_create(void); +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); #ifdef __cplusplus