src/server/util/hashing.h

Wed, 28 May 2025 21:09:45 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 28 May 2025 21:09:45 +0200
changeset 587
c94800af0490
parent 586
9861e8ceaf96
child 589
70ad04769cbf
permissions
-rw-r--r--

add sha1 API

586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2025 Olaf Wintermann. All rights reserved.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #ifndef HASHING_H
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #define HASHING_H
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
32 #define WS_SHA1_DIGEST_LENGTH 20
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
33 #define WS_SHA256_DIGEST_LENGTH 32
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #ifdef __APPLE__
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 /* macos */
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #define WS_CRYPTO_COMMON_CRYPTO
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #define WS_AES_CTX CCCryptorRef
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
41 #define WS_SHA1_CTX CC_SHA1_CTX
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #define WS_SHA_CTX CC_SHA256_CTX
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include <CommonCrypto/CommonCrypto.h>
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #include <CommonCrypto/CommonDigest.h>
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #elif defined(_WIN32)
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 #define WS_CRYPTO_CNG
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 #include <windows.h>
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 #include <bcrypt.h>
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 typedef struct WinBCryptCTX {
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 BCRYPT_ALG_HANDLE hAlg;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 BCRYPT_KEY_HANDLE hKey;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 void *pbKeyObject;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 unsigned char pbIV[16];
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 unsigned char buf[16];
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 ULONG buflen;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 } WinBCryptCTX;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 typedef struct WinBCryptSHACTX {
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 BCRYPT_ALG_HANDLE hAlg;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 BCRYPT_HASH_HANDLE hHash;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 void *pbHashObject;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 } WinBCryptSHACTX;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 #define WS_AES_CTX WinBCryptCTX
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
71 #define WS_SHA1_CTX WinBCryptSHACTX
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
72 #define WS_SHA256_CTX WinBCryptSHACTX
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 #else
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 /* unix/linux */
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 #include <openssl/evp.h>
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 #include <openssl/rand.h>
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 #define WS_USE_OPENSSL
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 #if OPENSSL_VERSION_NUMBER < 0x30000000L
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
83 #define WS_SHA256_CTX SHA1_CTX
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
84 #define WS_SHA256_CTX SHA256_CTX
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 #else
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
86 #define WS_SHA1_CTX EVP_MD_CTX*
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
87 #define WS_SHA256_CTX EVP_MD_CTX*
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 #endif
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 #if defined(__sun) && defined(__SunOS_5_10)
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 #include <sha2.h>
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
93 #define SHA_Init SHAInit
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
94 #define SHA_Update SHAUpdate
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
95 #define SHA_Final SHAFinal
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
96
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 #define SHA256_Init SHA256Init
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 #define SHA256_Update SHA256Update
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 #define SHA256_Final SHA256Final
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 #else
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 #include <openssl/sha.h>
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 #endif
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 #endif
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
106 int ws_sha1_init(WS_SHA1_CTX *ctx);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
107 WS_SHA1_CTX* ws_sha1_create(void);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
108 void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
109 void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf);
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
111 int ws_sha256_init(WS_SHA256_CTX *ctx);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
112 WS_SHA256_CTX* ws_sha256_create(void);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
113 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
114 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf);
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 #ifdef __cplusplus
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 #endif
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 #endif /* HASHING_H */
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122

mercurial