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 #ifndef HASHING_H 30 #define HASHING_H 31 32 #define WS_SHA1_DIGEST_LENGTH 20 33 #define WS_SHA256_DIGEST_LENGTH 32 34 #define WS_SHA512_DIGEST_LENGTH 32 35 36 #ifdef __APPLE__ 37 #define WS_USE_CRYPTO_COMMON 38 /* macos */ 39 40 #define WS_CRYPTO_COMMON_CRYPTO 41 42 #define WS_AES_CTX CCCryptorRef 43 #define WS_SHA1_CTX CC_SHA1_CTX 44 #define WS_SHA256_CTX CC_SHA256_CTX 45 #define WS_SHA512_CTX CC_SHA512_CTX 46 47 #include <CommonCrypto/CommonCrypto.h> 48 #include <CommonCrypto/CommonDigest.h> 49 50 #elif defined(_WIN32) 51 52 #define WS_USE_CRYPTO_CNG 53 54 #include <windows.h> 55 #include <bcrypt.h> 56 57 typedef struct WinBCryptCTX { 58 BCRYPT_ALG_HANDLE hAlg; 59 BCRYPT_KEY_HANDLE hKey; 60 void *pbKeyObject; 61 unsigned char pbIV[16]; 62 63 unsigned char buf[16]; 64 ULONG buflen; 65 } WinBCryptCTX; 66 67 typedef struct WinBCryptSHACTX { 68 BCRYPT_ALG_HANDLE hAlg; 69 BCRYPT_HASH_HANDLE hHash; 70 void *pbHashObject; 71 } WinBCryptSHACTX; 72 73 #define WS_AES_CTX WinBCryptCTX 74 #define WS_SHA1_CTX WinBCryptSHACTX 75 #define WS_SHA256_CTX WinBCryptSHACTX 76 #define WS_SHA512_CTX WinBCryptSHACTX 77 78 #else 79 /* unix/linux */ 80 81 #include <openssl/evp.h> 82 #include <openssl/rand.h> 83 84 #define WS_USE_OPENSSL 85 86 #if OPENSSL_VERSION_NUMBER < 0x30000000L 87 #define WS_SHA1_CTX SHA_CTX 88 #define WS_SHA256_CTX SHA256_CTX 89 #define WS_SHA512_CTX SHA512_CTX 90 #else 91 #define WS_SHA1_CTX EVP_MD_CTX* 92 #define WS_SHA256_CTX EVP_MD_CTX* 93 #define WS_SHA512_CTX EVP_MD_CTX* 94 #endif 95 96 97 #if defined(__sun) && defined(__SunOS_5_10) 98 #include <sha2.h> 99 #define SHA_Init SHAInit 100 #define SHA_Update SHAUpdate 101 #define SHA_Final SHAFinal 102 103 #define SHA256_Init SHA256Init 104 #define SHA256_Update SHA256Update 105 #define SHA256_Final SHA256Final 106 107 #define SHA512_Init SHA512Init 108 #define SHA512_Update SHA512Update 109 #define SHA512_Final SHA512Final 110 #else 111 #include <openssl/sha.h> 112 #endif 113 114 #endif 115 116 int ws_sha1_init(WS_SHA1_CTX *ctx); 117 WS_SHA1_CTX* ws_sha1_create(void); 118 void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len); 119 void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf); 120 121 int ws_sha256_init(WS_SHA256_CTX *ctx); 122 WS_SHA256_CTX* ws_sha256_create(void); 123 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len); 124 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf); 125 126 int ws_sha512_init(WS_SHA512_CTX *ctx); 127 WS_SHA512_CTX* ws_sha512_create(void); 128 void ws_sha512_update(WS_SHA512_CTX *ctx, const char *data, size_t len); 129 void ws_sha512_final(WS_SHA512_CTX *ctx, unsigned char *buf); 130 131 132 #ifdef __cplusplus 133 } 134 #endif 135 136 #endif /* HASHING_H */ 137 138