| |
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 |
| |
33 #ifdef __APPLE__ |
| |
34 /* macos */ |
| |
35 |
| |
36 #define WS_CRYPTO_COMMON_CRYPTO |
| |
37 |
| |
38 #define WS_AES_CTX CCCryptorRef |
| |
39 #define WS_SHA_CTX CC_SHA256_CTX |
| |
40 #define WS_SHA256_DIGEST_LENGTH 32 |
| |
41 |
| |
42 #include <CommonCrypto/CommonCrypto.h> |
| |
43 #include <CommonCrypto/CommonDigest.h> |
| |
44 |
| |
45 #elif defined(_WIN32) |
| |
46 |
| |
47 #define WS_CRYPTO_CNG |
| |
48 |
| |
49 #include <windows.h> |
| |
50 #include <bcrypt.h> |
| |
51 |
| |
52 typedef struct WinBCryptCTX { |
| |
53 BCRYPT_ALG_HANDLE hAlg; |
| |
54 BCRYPT_KEY_HANDLE hKey; |
| |
55 void *pbKeyObject; |
| |
56 unsigned char pbIV[16]; |
| |
57 |
| |
58 unsigned char buf[16]; |
| |
59 ULONG buflen; |
| |
60 } WinBCryptCTX; |
| |
61 |
| |
62 typedef struct WinBCryptSHACTX { |
| |
63 BCRYPT_ALG_HANDLE hAlg; |
| |
64 BCRYPT_HASH_HANDLE hHash; |
| |
65 void *pbHashObject; |
| |
66 } WinBCryptSHACTX; |
| |
67 |
| |
68 #define WS_AES_CTX WinBCryptCTX |
| |
69 #define WS_SHA_CTX WinBCryptSHACTX |
| |
70 #define WS_SHA256_DIGEST_LENGTH 32 |
| |
71 |
| |
72 #else |
| |
73 /* unix/linux */ |
| |
74 |
| |
75 #include <openssl/evp.h> |
| |
76 #include <openssl/rand.h> |
| |
77 |
| |
78 #define WS_USE_OPENSSL |
| |
79 |
| |
80 #define WS_AES_CTX EVP_CIPHER_CTX* |
| |
81 |
| |
82 #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| |
83 #define WS_SHA_CTX SHA256_CTX |
| |
84 #else |
| |
85 #define WS_SHA_CTX EVP_MD_CTX* |
| |
86 #endif |
| |
87 #define WS_SHA256_DIGEST_LENGTH 32 |
| |
88 |
| |
89 |
| |
90 #if defined(__sun) && defined(__SunOS_5_10) |
| |
91 #include <sha2.h> |
| |
92 #define SHA256_Init SHA256Init |
| |
93 #define SHA256_Update SHA256Update |
| |
94 #define SHA256_Final SHA256Final |
| |
95 #else |
| |
96 #include <openssl/sha.h> |
| |
97 #endif |
| |
98 |
| |
99 #endif |
| |
100 |
| |
101 |
| |
102 void ws_sha256_init(WS_SHA_CTX *ctx); |
| |
103 WS_SHA_CTX* ws_sha256_create(void); |
| |
104 void ws_sha256_update(WS_SHA_CTX *ctx, const char *data, size_t len); |
| |
105 void ws_sha256_final(WS_SHA_CTX *ctx, unsigned char *buf); |
| |
106 |
| |
107 |
| |
108 #ifdef __cplusplus |
| |
109 } |
| |
110 #endif |
| |
111 |
| |
112 #endif /* HASHING_H */ |
| |
113 |