xref: /lighttpd1.4/src/sys-crypto-md.h (revision f2e4e83a)
1 /* sys-crypto-md.h - message digest (MD) wrapper
2  *
3  * message digest (MD) algorithms are not necessarily cryptographically secure
4  * (often provided by crypto libraries, hence this file named sys-crypto-md.h)
5  *
6  * Copyright(c) 2020 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
7  * License: BSD 3-clause (same as lighttpd)
8  */
9 #ifndef LI_SYS_CRYPTO_MD_H
10 #define LI_SYS_CRYPTO_MD_H
11 #include "first.h"
12 
13 #include "sys-crypto.h" /* USE_LIB_CRYPTO */
14 #ifdef USE_LIB_CRYPTO
15 
16 #if defined(USE_NETTLE_CRYPTO)
17 
18 #include <nettle/md4.h>
19 #include <nettle/md5.h>
20 #include <nettle/sha.h>
21 
22 #define USE_LIB_CRYPTO_MD4
23 typedef struct md4_ctx MD4_CTX;
24 #define MD4_Init(ctx) \
25         nettle_md4_init(ctx)
26 #define MD4_Final(digest, ctx) \
27         nettle_md4_digest((ctx),MD4_DIGEST_SIZE,(digest))
28 static inline void
29 MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
30 {
31     nettle_md4_update(ctx, length, data);
32 }
33 
34 #define USE_LIB_CRYPTO_MD5
35 typedef struct md5_ctx MD5_CTX;
36 #define MD5_Init(ctx) \
37         nettle_md5_init(ctx)
38 #define MD5_Final(digest, ctx) \
39         nettle_md5_digest((ctx),MD5_DIGEST_SIZE,(digest))
40 static inline void
41 MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
42 {
43     nettle_md5_update(ctx, length, data);
44 }
45 
46 #define USE_LIB_CRYPTO_SHA1
47 typedef struct sha1_ctx SHA_CTX;
48 #define SHA1_Init(ctx) \
49         nettle_sha1_init(ctx)
50 #define SHA1_Final(digest, ctx) \
51         nettle_sha1_digest((ctx),SHA1_DIGEST_SIZE,(digest))
52 static inline void
53 SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
54 {
55     nettle_sha1_update(ctx, length, data);
56 }
57 
58 #define USE_LIB_CRYPTO_SHA256
59 typedef struct sha256_ctx SHA256_CTX;
60 #define SHA256_Init(ctx) \
61         nettle_sha256_init(ctx)
62 #define SHA256_Final(digest, ctx) \
63         nettle_sha256_digest((ctx),SHA256_DIGEST_SIZE,(digest))
64 static inline void
65 SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
66 {
67     nettle_sha256_update(ctx, length, data);
68 }
69 
70 #define USE_LIB_CRYPTO_SHA512_256
71 #ifndef SHA512_256_DIGEST_LENGTH
72 #define SHA512_256_DIGEST_LENGTH 32
73 #endif
74 typedef struct sha512_256_ctx SHA512_CTX;    /*(yes, SHA512_CTX)*/
75 #define SHA512_256_Init(ctx) \
76         nettle_sha512_256_init(ctx)
77 #define SHA512_256_Final(digest, ctx) \
78         nettle_sha512_256_digest((ctx),SHA256_DIGEST_SIZE,(digest))
79 static inline void
80 SHA512_256_Update(SHA512_CTX *ctx, const void *data, size_t length)
81 {
82     nettle_sha512_update(ctx, length, data); /*(yes, nettle_sha512_update())*/
83 }
84 
85 #elif defined(USE_MBEDTLS_CRYPTO)
86 
87 #ifdef MBEDTLS_MD4_C
88 #define USE_LIB_CRYPTO_MD4
89 #include <mbedtls/md4.h>
90 typedef struct mbedtls_md4_context MD4_CTX;
91 #define MD4_Init(ctx) \
92         (mbedtls_md4_init(ctx), mbedtls_md4_starts_ret(ctx))
93 #define MD4_Final(digest, ctx) \
94         (mbedtls_md4_finish_ret((ctx),(digest)), mbedtls_md4_free(ctx))
95 static inline void
96 MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
97 {
98     mbedtls_md4_update_ret(ctx, data, length);
99 }
100 #endif
101 
102 #ifdef MBEDTLS_MD5_C
103 #define USE_LIB_CRYPTO_MD5
104 #include <mbedtls/md5.h>
105 typedef struct mbedtls_md5_context MD5_CTX;
106 #define MD5_Init(ctx) \
107         (mbedtls_md5_init(ctx), mbedtls_md5_starts_ret(ctx))
108 #define MD5_Final(digest, ctx) \
109         (mbedtls_md5_finish_ret((ctx),(digest)), mbedtls_md5_free(ctx))
110 static inline void
111 MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
112 {
113     mbedtls_md5_update_ret(ctx, data, length);
114 }
115 #endif
116 
117 #ifdef MBEDTLS_SHA1_C
118 #define USE_LIB_CRYPTO_SHA1
119 #include <mbedtls/sha1.h>
120 typedef struct mbedtls_sha1_context SHA_CTX;
121 #define SHA1_Init(ctx) \
122         (mbedtls_sha1_init(ctx), mbedtls_sha1_starts_ret(ctx))
123 #define SHA1_Final(digest, ctx) \
124         (mbedtls_sha1_finish_ret((ctx),(digest)), mbedtls_sha1_free(ctx))
125 static inline void
126 SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
127 {
128     mbedtls_sha1_update_ret(ctx, data, length);
129 }
130 #endif
131 
132 #ifdef MBEDTLS_SHA256_C
133 #define USE_LIB_CRYPTO_SHA256
134 #include <mbedtls/sha256.h>
135 typedef struct mbedtls_sha256_context SHA256_CTX;
136 #define SHA256_Init(ctx) \
137         (mbedtls_sha256_init(ctx), mbedtls_sha256_starts_ret((ctx),0))
138 #define SHA256_Final(digest, ctx) \
139         (mbedtls_sha256_finish_ret((ctx),(digest)), mbedtls_sha256_free(ctx))
140 static inline void
141 SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
142 {
143     mbedtls_sha256_update_ret(ctx, data, length);
144 }
145 #endif
146 
147 #elif defined(USE_WOLFSSL_CRYPTO)
148 
149 /* WolfSSL compatibility API for OpenSSL unnecessarily bounces through an extra
150  * layer of indirection.  However, to avoid conflicting typedefs when includers
151  * also include headers from the WolfSSL compatibility API for OpenSSL, we
152  * include those headers here, as well, and use the compatibility API typedefs.
153  * (undef of OPENSSL_EXTRA and NO_OLD_WC_NAMES not sufficient, and not friendly
154  *  to do in a header when others might rely on them) */
155 
156 #ifndef NO_MD4
157 #include <wolfssl/wolfcrypt/md4.h>
158 #include <wolfssl/openssl/md4.h>
159 #undef MD4_Init
160 #undef MD4_Final
161 #undef MD4_Update
162 #define USE_LIB_CRYPTO_MD4
163 /*typedef Md4 MD4_CTX;*/
164 static inline int
165 MD4_Init(MD4_CTX *ctx)
166 {
167     return (0 == wc_InitMd4((Md4 *)ctx));
168 }
169 static inline int
170 MD4_Final(unsigned char *digest, MD4_CTX *ctx)
171 {
172     return (0 == wc_Md4Final((Md4 *)ctx, digest));
173 }
174 static inline void
175 MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
176 {
177     wc_Md4Update((Md4 *)ctx, data, length);
178 }
179 #endif
180 
181 #ifndef NO_MD5
182 #include <wolfssl/wolfcrypt/md5.h>
183 #include <wolfssl/openssl/md5.h>
184 #undef MD5_Init
185 #undef MD5_Final
186 #undef MD5_Update
187 #define USE_LIB_CRYPTO_MD5
188 /*typedef wc_Md5 MD5_CTX;*/
189 static inline int
190 MD5_Init(MD5_CTX *ctx)
191 {
192     return (0 == wc_InitMd5((wc_Md5 *)ctx));
193 }
194 static inline int
195 MD5_Final(unsigned char *digest, MD5_CTX *ctx)
196 {
197     return (0 == wc_Md5Final((wc_Md5 *)ctx, digest));
198 }
199 static inline void
200 MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
201 {
202     wc_Md5Update((wc_Md5 *)ctx, data, length);
203 }
204 #endif
205 
206 #ifndef NO_SHA
207 #include <wolfssl/wolfcrypt/sha.h>
208 #include <wolfssl/openssl/sha.h>
209 #undef SHA1_Init
210 #undef SHA1_Final
211 #undef SHA1_Update
212 #define USE_LIB_CRYPTO_SHA1
213 /*typedef wc_Sha SHA_CTX;*/
214 static inline int
215 SHA1_Init(SHA_CTX *ctx)
216 {
217     return (0 == wc_InitSha((wc_Sha *)ctx));
218 }
219 static inline int
220 SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
221 {
222     return (0 == wc_ShaFinal((wc_Sha *)ctx, digest));
223 }
224 static inline void
225 SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
226 {
227     wc_ShaUpdate((wc_Sha *)ctx, data, length);
228 }
229 #endif
230 
231 #ifndef NO_SHA256
232 #include <wolfssl/wolfcrypt/sha256.h>
233 #include <wolfssl/openssl/sha.h>
234 #undef SHA256_Init
235 #undef SHA256_Final
236 #undef SHA256_Update
237 #define USE_LIB_CRYPTO_SHA256
238 /*typedef wc_Sha256 SHA256_CTX;*/
239 static inline int
240 SHA256_Init(SHA256_CTX *ctx)
241 {
242     return (0 == wc_InitSha256((wc_Sha256 *)ctx));
243 }
244 static inline int
245 SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
246 {
247     return (0 == wc_Sha256Final((wc_Sha256 *)ctx, digest));
248 }
249 static inline void
250 SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
251 {
252     wc_Sha256Update((wc_Sha256 *)ctx, data, length);
253 }
254 #endif
255 
256 #elif defined(USE_OPENSSL_CRYPTO)
257 
258 #include <openssl/md4.h>
259 #include <openssl/md5.h>
260 #include <openssl/sha.h>
261 #ifndef OPENSSL_NO_MD4
262 #ifndef NO_MD4 /*(e.g. wolfSSL built without MD4)*/
263 #define USE_LIB_CRYPTO_MD4
264 #endif
265 #endif
266 #ifndef OPENSSL_NO_MD5
267 #define USE_LIB_CRYPTO_MD5
268 #endif
269 #define USE_LIB_CRYPTO_SHA1
270 #define USE_LIB_CRYPTO_SHA256
271 #ifdef SHA512_256_DIGEST_LENGTH
272 #define USE_LIB_CRYPTO_SHA512_256
273 #endif
274 
275 #elif defined(USE_GNUTLS_CRYPTO)
276 
277 #include <gnutls/crypto.h>
278 
279 #define USE_LIB_CRYPTO_MD5
280 typedef gnutls_hash_hd_t MD5_CTX;
281 #define MD5_Init(ctx)                                        \
282         do {                                                  \
283             if (gnutls_hash_init((ctx), GNUTLS_DIG_MD5) < 0) \
284                 SEGFAULT();                                   \
285         } while (0)
286 #define MD5_Final(digest, ctx) \
287         gnutls_hash_deinit(*(ctx),(digest))
288 static inline void
289 MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
290 {
291     gnutls_hash(*ctx, data, length);
292 }
293 
294 #define USE_LIB_CRYPTO_SHA1
295 typedef gnutls_hash_hd_t SHA_CTX;
296 #define SHA1_Init(ctx)                                        \
297         do {                                                  \
298             if (gnutls_hash_init((ctx), GNUTLS_DIG_SHA1) < 0) \
299                 SEGFAULT();                                   \
300         } while (0)
301 #define SHA1_Final(digest, ctx) \
302         gnutls_hash_deinit(*(ctx),(digest))
303 static inline void
304 SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
305 {
306     gnutls_hash(*ctx, data, length);
307 }
308 
309 #define USE_LIB_CRYPTO_SHA256
310 typedef gnutls_hash_hd_t SHA256_CTX;
311 #define SHA256_Init(ctx)                                        \
312         do {                                                    \
313             if (gnutls_hash_init((ctx), GNUTLS_DIG_SHA256) < 0) \
314                 SEGFAULT();                                     \
315         } while (0)
316 #define SHA256_Final(digest, ctx) \
317         gnutls_hash_deinit(*(ctx),(digest))
318 static inline void
319 SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
320 {
321     gnutls_hash(*ctx, data, length);
322 }
323 
324 #endif
325 
326 #endif /* USE_LIB_CRYPTO */
327 
328 
329 #ifdef USE_LIB_CRYPTO_MD5
330 #ifndef MD5_DIGEST_LENGTH
331 #define MD5_DIGEST_LENGTH 16
332 #endif
333 #include "algo_md5.h" /*(for legacy li_MD5_*() name mangling)*/
334 #else
335 #include "algo_md5.h"
336 #endif
337 
338 
339 #ifdef USE_LIB_CRYPTO_SHA1
340 #ifndef SHA_DIGEST_LENGTH
341 #define SHA_DIGEST_LENGTH 20
342 #endif
343 #else
344 #include "algo_sha1.h"
345 #endif
346 
347 
348 #endif /* LI_SYS_CRYPTO_MD_H */
349