UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2025 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "hashing.h" 30 31 #include <stdlib.h> 32 33 WS_SHA1_CTX* ws_sha1_create(void) { 34 WS_SHA1_CTX *ctx = malloc(sizeof(WS_SHA1_CTX)); 35 if(!ctx) { 36 return NULL; 37 } 38 if(ws_sha1_init(ctx)) { 39 free(ctx); 40 return NULL; 41 } 42 return ctx; 43 } 44 45 WS_SHA256_CTX* ws_sha256_create(void) { 46 WS_SHA256_CTX *ctx = malloc(sizeof(WS_SHA256_CTX)); 47 if(!ctx) { 48 return NULL; 49 } 50 if(ws_sha256_init(ctx)) { 51 free(ctx); 52 return NULL; 53 } 54 return ctx; 55 } 56 57 WS_SHA512_CTX* ws_sha512_create(void) { 58 WS_SHA512_CTX *ctx = malloc(sizeof(WS_SHA512_CTX)); 59 if(!ctx) { 60 return NULL; 61 } 62 if(ws_sha512_init(ctx)) { 63 free(ctx); 64 return NULL; 65 } 66 return ctx; 67 } 68 69 #ifdef WS_USE_OPENSSL 70 71 #if OPENSSL_VERSION_NUMBER < 0x30000000L 72 73 int ws_sha1_init(WS_SHA1_CTX *ctx) { 74 SHA1_Init(ctx); 75 return 0; 76 } 77 78 void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len) { 79 SHA1_Update(ctx, data, len); 80 } 81 82 void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf) { 83 SHA1_Final(buf, ctx); 84 } 85 86 int ws_sha256_init(WS_SHA256_CTX *ctx) { 87 SHA256_Init(ctx); 88 return 0; 89 } 90 91 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len) { 92 SHA256_Update(ctx, data, len); 93 } 94 95 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf) { 96 SHA256_Final(buf, ctx); 97 } 98 99 int ws_sha512_init(WS_SHA512_CTX *ctx) { 100 SHA512_Init(ctx); 101 return 0; 102 } 103 104 void ws_sha512_update(WS_SHA512_CTX *ctx, const char *data, size_t len) { 105 SHA512_Update(ctx, data, len); 106 } 107 108 void ws_sha512_final(WS_SHA512_CTX *ctx, unsigned char *buf) { 109 SHA512_Final(buf, ctx); 110 } 111 112 #else 113 114 int ws_sha1_init(WS_SHA256_CTX *ctx) { 115 EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); 116 if(!mdctx) { 117 return 1; 118 } 119 EVP_DigestInit_ex(mdctx, EVP_sha1(), NULL); 120 *ctx = mdctx; 121 return 0; 122 } 123 124 void ws_sha1_update(WS_SHA256_CTX *ctx, const char *data, size_t length) { 125 EVP_DigestUpdate(*ctx, data, length); 126 } 127 128 void ws_sha1_final(WS_SHA256_CTX *ctx, unsigned char *md) { 129 EVP_DigestFinal(*ctx, md, NULL); 130 } 131 132 int ws_sha256_init(WS_SHA256_CTX *ctx) { 133 EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); 134 if(!mdctx) { 135 return 1; 136 } 137 EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); 138 *ctx = mdctx; 139 return 0; 140 } 141 142 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t length) { 143 EVP_DigestUpdate(*ctx, data, length); 144 } 145 146 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *md) { 147 EVP_DigestFinal(*ctx, md, NULL); 148 } 149 150 int ws_sha512_init(WS_SHA512_CTX *ctx) { 151 EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); 152 if(!mdctx) { 153 return 1; 154 } 155 EVP_DigestInit_ex(mdctx, EVP_sha512(), NULL); 156 *ctx = mdctx; 157 return 0; 158 } 159 160 void ws_sha512_update(WS_SHA256_CTX *ctx, const char *data, size_t length) { 161 EVP_DigestUpdate(*ctx, data, length); 162 } 163 164 void ws_sha512_final(WS_SHA256_CTX *ctx, unsigned char *md) { 165 EVP_DigestFinal(*ctx, md, NULL); 166 } 167 168 #endif // OPENSSL_VERSION_NUMBER < 0x30000000L 169 170 #endif // WS_USE_OPENSSL 171 172 173 #ifdef WS_USE_CRYPTO_COMMON 174 175 int ws_sha1_init(WS_SHA1_CTX *ctx) { 176 CC_SHA1_Init(ctx); 177 return 0; 178 } 179 180 void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len) { 181 CC_SHA1_Update(ctx, data, len); 182 } 183 184 void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf) { 185 CC_SHA1_Final(buf, ctx); 186 } 187 188 int ws_sha256_init(WS_SHA256_CTX *ctx) { 189 CC_SHA256_Init(ctx); 190 return 0; 191 } 192 193 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len) { 194 CC_SHA256_Update(ctx, data, len); 195 } 196 197 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf) { 198 CC_SHA256_Final(buf, ctx); 199 } 200 201 202 int ws_sha512_init(WS_SHA512_CTX *ctx) { 203 CC_SHA512_Init(ctx); 204 return 0; 205 } 206 207 void ws_sha512_update(WS_SHA512_CTX *ctx, const char *data, size_t len) { 208 CC_SHA512_Update(ctx, data, len); 209 } 210 211 void ws_sha512_final(WS_SHA512_CTX *ctx, unsigned char *buf) { 212 CC_SHA512_Final(buf, ctx); 213 } 214 215 #endif // WS_USE_CRYPTO_COMMON 216 217 #ifdef WS_USE_CRYPTO_CNG 218 219 static int cng_hash_init(WinBCryptSHACTX *ctx, LPCWSTR algId) { 220 if(BCryptOpenAlgorithmProvider(&ctx->hAlg, algId, NULL, 0)) { 221 return 1; 222 } 223 224 ULONG hashObjectLen; 225 ULONG result; 226 if(BCryptGetProperty(ctx->hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&hashObjectLen, sizeof(DWORD), &result, 0)) { 227 cng_cleanup(ctx->hAlg, NULL, NULL, NULL); 228 return 1; 229 } 230 231 ctx->pbHashObject = calloc(1, hashObjectLen); 232 if(!ctx->pbHashObject) { 233 cng_cleanup(ctx->hAlg, NULL, NULL, NULL); 234 return 1; 235 } 236 237 if(BCryptCreateHash(ctx->hAlg, &ctx->hHash, ctx->pbHashObject, hashObjectLen, NULL, 0, 0)) { 238 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject); 239 return 1; 240 } 241 242 return 0; 243 } 244 245 static void cng_cleanup(BCRYPT_ALG_HANDLE hAesAlg, BCRYPT_KEY_HANDLE hKey, BCRYPT_HASH_HANDLE hHash, void *pbObject) { 246 if(hAesAlg) { 247 BCryptCloseAlgorithmProvider(hAesAlg,0); 248 } 249 if(hKey) { 250 BCryptDestroyKey(hKey); 251 } 252 if(hHash) { 253 BCryptDestroyHash(hHash); 254 } 255 if(pbObject) { 256 free(pbObject); 257 } 258 } 259 260 int ws_sha1_init(WS_SHA1_CTX *ctx) { 261 return cng_hash_init(ctx, BCRYPT_SHA1_ALGORITHM); 262 } 263 264 void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len) { 265 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0); 266 } 267 268 void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf) { 269 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0); 270 271 // cleanup 272 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject); 273 } 274 275 int ws_sha256_init(WS_SHA256_CTX *ctx) { 276 return cng_hash_init(ctx, BCRYPT_SHA256_ALGORITHM); 277 } 278 279 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len) { 280 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0); 281 } 282 283 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf) { 284 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0); 285 286 // cleanup 287 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject); 288 } 289 290 int ws_sha512_init(WS_SHA512_CTX *ctx) { 291 return cng_hash_init(ctx, BCRYPT_SHA512_ALGORITHM); 292 } 293 294 void ws_sha512_update(WS_SHA512_CTX *ctx, const char *data, size_t len) { 295 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0); 296 } 297 298 void ws_sha512_final(WS_SHA512_CTX *ctx, unsigned char *buf) { 299 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0); 300 301 // cleanup 302 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject); 303 } 304 305 #endif // WS_USE_CRYPTO_CNG 306