xref: /freebsd/contrib/libarchive/libarchive/archive_digest_private.h (revision e64fe029e9d3ce476e77a478318e0c3cd201ff08)
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011 Andres Mejia
4 * 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
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 #ifndef ARCHIVE_DIGEST_PRIVATE_H_INCLUDED
28 #define ARCHIVE_DIGEST_PRIVATE_H_INCLUDED
29 
30 #ifndef __LIBARCHIVE_BUILD
31 #error This header is only to be used internally to libarchive.
32 #endif
33 #ifndef __LIBARCHIVE_CONFIG_H_INCLUDED
34 #error "Should have include config.h first!"
35 #endif
36 
37 /*
38  * Crypto support in various Operating Systems:
39  *
40  * NetBSD:
41  * - MD5 and SHA1 in libc: without _ after algorithm name
42  * - SHA2 in libc: with _ after algorithm name
43  *
44  * OpenBSD:
45  * - MD5, SHA1 and SHA2 in libc: without _ after algorithm name
46  * - OpenBSD 4.4 and earlier have SHA2 in libc with _ after algorithm name
47  *
48  * DragonFly and FreeBSD:
49  * - MD5 libmd: without _ after algorithm name
50  * - SHA1, SHA256 and SHA512 in libmd: with _ after algorithm name
51  *
52  * Mac OS X (10.4 and later):
53  * - MD5, SHA1 and SHA2 in libSystem: with CC_ prefix and _ after algorithm name
54  *
55  * OpenSSL:
56  * - MD5, SHA1 and SHA2 in libcrypto: with _ after algorithm name
57  *
58  * Windows:
59  * - MD5, SHA1 and SHA2 in archive_crypto.c using Windows crypto API
60  */
61 
62 /* libc crypto headers */
63 #if defined(ARCHIVE_CRYPTO_MD5_LIBC)
64 #include <md5.h>
65 #endif
66 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC)
67 #include <rmd160.h>
68 #endif
69 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC)
70 #include <sha1.h>
71 #endif
72 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\
73   defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\
74   defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\
75   defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\
76   defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\
77   defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\
78   defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\
79   defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\
80   defined(ARCHIVE_CRYPTO_SHA512_LIBC3)
81 #include <sha2.h>
82 #endif
83 
84 /* libmd crypto headers */
85 #if defined(ARCHIVE_CRYPTO_MD5_LIBMD) ||\
86   defined(ARCHIVE_CRYPTO_RMD160_LIBMD) ||\
87   defined(ARCHIVE_CRYPTO_SHA1_LIBMD) ||\
88   defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\
89   defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
90 #define	ARCHIVE_CRYPTO_LIBMD 1
91 #endif
92 
93 #if defined(ARCHIVE_CRYPTO_MD5_LIBMD)
94 #include <md5.h>
95 #endif
96 #if defined(ARCHIVE_CRYPTO_RMD160_LIBMD)
97 #include <ripemd.h>
98 #endif
99 #if defined(ARCHIVE_CRYPTO_SHA1_LIBMD)
100 #include <sha.h>
101 #endif
102 #if defined(ARCHIVE_CRYPTO_SHA256_LIBMD)
103 #include <sha256.h>
104 #endif
105 #if defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
106 #include <sha512.h>
107 #endif
108 
109 /* libSystem crypto headers */
110 #if defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\
111   defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\
112   defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\
113   defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\
114   defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)
115 #include <CommonCrypto/CommonDigest.h>
116 #endif
117 
118 /* mbed TLS crypto headers */
119 #if defined(ARCHIVE_CRYPTO_MD5_MBEDTLS)
120 #include <mbedtls/md5.h>
121 #endif
122 #if defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS)
123 #include <mbedtls/ripemd160.h>
124 #endif
125 #if defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS)
126 #include <mbedtls/sha1.h>
127 #endif
128 #if defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS)
129 #include <mbedtls/sha256.h>
130 #endif
131 #if defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\
132   defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS)
133 #include <mbedtls/sha512.h>
134 #endif
135 
136 /* Nettle crypto headers */
137 #if defined(ARCHIVE_CRYPTO_MD5_NETTLE)
138 #include <nettle/md5.h>
139 #endif
140 #if defined(ARCHIVE_CRYPTO_RMD160_NETTLE)
141 #include <nettle/ripemd160.h>
142 #endif
143 #if defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\
144   defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\
145   defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\
146   defined(ARCHIVE_CRYPTO_SHA512_NETTLE)
147 #include <nettle/sha.h>
148 #endif
149 
150 /* OpenSSL crypto headers */
151 #if defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\
152   defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) ||\
153   defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\
154   defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\
155   defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\
156   defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)
157 #define	ARCHIVE_CRYPTO_OPENSSL 1
158 #include "archive_openssl_evp_private.h"
159 #endif
160 
161 /* Windows crypto headers */
162 #if defined(ARCHIVE_CRYPTO_MD5_WIN)    ||\
163   defined(ARCHIVE_CRYPTO_SHA1_WIN)   ||\
164   defined(ARCHIVE_CRYPTO_SHA256_WIN) ||\
165   defined(ARCHIVE_CRYPTO_SHA384_WIN) ||\
166   defined(ARCHIVE_CRYPTO_SHA512_WIN)
167 #if defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA
168 /* don't use bcrypt when XP needs to be supported */
169 #include <bcrypt.h>
170 typedef struct {
171   int   valid;
172   BCRYPT_ALG_HANDLE  hAlg;
173   BCRYPT_HASH_HANDLE hHash;
174 } Digest_CTX;
175 #else
176 #include <windows.h>
177 #include <wincrypt.h>
178 typedef struct {
179   int   valid;
180   HCRYPTPROV  cryptProv;
181   HCRYPTHASH  hash;
182 } Digest_CTX;
183 #endif
184 #endif
185 
186 /* typedefs */
187 #if defined(ARCHIVE_CRYPTO_MD5_LIBC)
188 typedef MD5_CTX archive_md5_ctx;
189 #elif defined(ARCHIVE_CRYPTO_MD5_LIBMD)
190 typedef MD5_CTX archive_md5_ctx;
191 #elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM)
192 typedef CC_MD5_CTX archive_md5_ctx;
193 #elif defined(ARCHIVE_CRYPTO_MD5_MBEDTLS)
194 typedef mbedtls_md5_context archive_md5_ctx;
195 #elif defined(ARCHIVE_CRYPTO_MD5_NETTLE)
196 typedef struct md5_ctx archive_md5_ctx;
197 #elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL)
198 typedef EVP_MD_CTX *archive_md5_ctx;
199 #elif defined(ARCHIVE_CRYPTO_MD5_WIN)
200 typedef Digest_CTX archive_md5_ctx;
201 #else
202 typedef unsigned char archive_md5_ctx;
203 #endif
204 
205 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC)
206 typedef RMD160_CTX archive_rmd160_ctx;
207 #elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD)
208 typedef RIPEMD160_CTX archive_rmd160_ctx;
209 #elif defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS)
210 typedef mbedtls_ripemd160_context archive_rmd160_ctx;
211 #elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE)
212 typedef struct ripemd160_ctx archive_rmd160_ctx;
213 #elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
214 typedef EVP_MD_CTX *archive_rmd160_ctx;
215 #else
216 typedef unsigned char archive_rmd160_ctx;
217 #endif
218 
219 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC)
220 typedef SHA1_CTX archive_sha1_ctx;
221 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD)
222 typedef SHA1_CTX archive_sha1_ctx;
223 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM)
224 typedef CC_SHA1_CTX archive_sha1_ctx;
225 #elif defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS)
226 typedef mbedtls_sha1_context archive_sha1_ctx;
227 #elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE)
228 typedef struct sha1_ctx archive_sha1_ctx;
229 #elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL)
230 typedef EVP_MD_CTX *archive_sha1_ctx;
231 #elif defined(ARCHIVE_CRYPTO_SHA1_WIN)
232 typedef Digest_CTX archive_sha1_ctx;
233 #else
234 typedef unsigned char archive_sha1_ctx;
235 #endif
236 
237 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC)
238 typedef SHA256_CTX archive_sha256_ctx;
239 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2)
240 typedef SHA256_CTX archive_sha256_ctx;
241 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3)
242 typedef SHA2_CTX archive_sha256_ctx;
243 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD)
244 typedef SHA256_CTX archive_sha256_ctx;
245 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM)
246 typedef CC_SHA256_CTX archive_sha256_ctx;
247 #elif defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS)
248 typedef mbedtls_sha256_context archive_sha256_ctx;
249 #elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE)
250 typedef struct sha256_ctx archive_sha256_ctx;
251 #elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL)
252 typedef EVP_MD_CTX *archive_sha256_ctx;
253 #elif defined(ARCHIVE_CRYPTO_SHA256_WIN)
254 typedef Digest_CTX archive_sha256_ctx;
255 #else
256 typedef unsigned char archive_sha256_ctx;
257 #endif
258 
259 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC)
260 typedef SHA384_CTX archive_sha384_ctx;
261 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2)
262 typedef SHA384_CTX archive_sha384_ctx;
263 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3)
264 typedef SHA2_CTX archive_sha384_ctx;
265 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM)
266 typedef CC_SHA512_CTX archive_sha384_ctx;
267 #elif defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS)
268 typedef mbedtls_sha512_context archive_sha384_ctx;
269 #elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE)
270 typedef struct sha384_ctx archive_sha384_ctx;
271 #elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL)
272 typedef EVP_MD_CTX *archive_sha384_ctx;
273 #elif defined(ARCHIVE_CRYPTO_SHA384_WIN)
274 typedef Digest_CTX archive_sha384_ctx;
275 #else
276 typedef unsigned char archive_sha384_ctx;
277 #endif
278 
279 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC)
280 typedef SHA512_CTX archive_sha512_ctx;
281 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2)
282 typedef SHA512_CTX archive_sha512_ctx;
283 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3)
284 typedef SHA2_CTX archive_sha512_ctx;
285 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
286 typedef SHA512_CTX archive_sha512_ctx;
287 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)
288 typedef CC_SHA512_CTX archive_sha512_ctx;
289 #elif defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS)
290 typedef mbedtls_sha512_context archive_sha512_ctx;
291 #elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE)
292 typedef struct sha512_ctx archive_sha512_ctx;
293 #elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)
294 typedef EVP_MD_CTX *archive_sha512_ctx;
295 #elif defined(ARCHIVE_CRYPTO_SHA512_WIN)
296 typedef Digest_CTX archive_sha512_ctx;
297 #else
298 typedef unsigned char archive_sha512_ctx;
299 #endif
300 
301 /* defines */
302 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\
303   defined(ARCHIVE_CRYPTO_MD5_LIBMD) ||	\
304   defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\
305   defined(ARCHIVE_CRYPTO_MD5_MBEDTLS) ||\
306   defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\
307   defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\
308   defined(ARCHIVE_CRYPTO_MD5_WIN)
309 #define ARCHIVE_HAS_MD5
310 #endif
311 #define archive_md5_init(ctx)\
312   __archive_digest.md5init(ctx)
313 #define archive_md5_final(ctx, md)\
314   __archive_digest.md5final(ctx, md)
315 #define archive_md5_update(ctx, buf, n)\
316   __archive_digest.md5update(ctx, buf, n)
317 
318 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\
319   defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS) ||\
320   defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\
321   defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
322 #define ARCHIVE_HAS_RMD160
323 #endif
324 #define archive_rmd160_init(ctx)\
325   __archive_digest.rmd160init(ctx)
326 #define archive_rmd160_final(ctx, md)\
327   __archive_digest.rmd160final(ctx, md)
328 #define archive_rmd160_update(ctx, buf, n)\
329   __archive_digest.rmd160update(ctx, buf, n)
330 
331 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\
332   defined(ARCHIVE_CRYPTO_SHA1_LIBMD) ||	\
333   defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\
334   defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS) ||\
335   defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\
336   defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\
337   defined(ARCHIVE_CRYPTO_SHA1_WIN)
338 #define ARCHIVE_HAS_SHA1
339 #endif
340 #define archive_sha1_init(ctx)\
341   __archive_digest.sha1init(ctx)
342 #define archive_sha1_final(ctx, md)\
343   __archive_digest.sha1final(ctx, md)
344 #define archive_sha1_update(ctx, buf, n)\
345   __archive_digest.sha1update(ctx, buf, n)
346 
347 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\
348   defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\
349   defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\
350   defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\
351   defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\
352   defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS) ||\
353   defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\
354   defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\
355   defined(ARCHIVE_CRYPTO_SHA256_WIN)
356 #define ARCHIVE_HAS_SHA256
357 #endif
358 #define archive_sha256_init(ctx)\
359   __archive_digest.sha256init(ctx)
360 #define archive_sha256_final(ctx, md)\
361   __archive_digest.sha256final(ctx, md)
362 #define archive_sha256_update(ctx, buf, n)\
363   __archive_digest.sha256update(ctx, buf, n)
364 
365 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\
366   defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\
367   defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\
368   defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\
369   defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\
370   defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\
371   defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\
372   defined(ARCHIVE_CRYPTO_SHA384_WIN)
373 #define ARCHIVE_HAS_SHA384
374 #endif
375 #define archive_sha384_init(ctx)\
376   __archive_digest.sha384init(ctx)
377 #define archive_sha384_final(ctx, md)\
378   __archive_digest.sha384final(ctx, md)
379 #define archive_sha384_update(ctx, buf, n)\
380   __archive_digest.sha384update(ctx, buf, n)
381 
382 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\
383   defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\
384   defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\
385   defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\
386   defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\
387   defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS) ||\
388   defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\
389   defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\
390   defined(ARCHIVE_CRYPTO_SHA512_WIN)
391 #define ARCHIVE_HAS_SHA512
392 #endif
393 #define archive_sha512_init(ctx)\
394   __archive_digest.sha512init(ctx)
395 #define archive_sha512_final(ctx, md)\
396   __archive_digest.sha512final(ctx, md)
397 #define archive_sha512_update(ctx, buf, n)\
398   __archive_digest.sha512update(ctx, buf, n)
399 
400 /* Minimal interface to digest functionality for internal use in libarchive */
401 struct archive_digest
402 {
403   /* Message Digest */
404   int (*md5init)(archive_md5_ctx *ctx);
405   int (*md5update)(archive_md5_ctx *, const void *, size_t);
406   int (*md5final)(archive_md5_ctx *, void *);
407   int (*rmd160init)(archive_rmd160_ctx *);
408   int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t);
409   int (*rmd160final)(archive_rmd160_ctx *, void *);
410   int (*sha1init)(archive_sha1_ctx *);
411   int (*sha1update)(archive_sha1_ctx *, const void *, size_t);
412   int (*sha1final)(archive_sha1_ctx *, void *);
413   int (*sha256init)(archive_sha256_ctx *);
414   int (*sha256update)(archive_sha256_ctx *, const void *, size_t);
415   int (*sha256final)(archive_sha256_ctx *, void *);
416   int (*sha384init)(archive_sha384_ctx *);
417   int (*sha384update)(archive_sha384_ctx *, const void *, size_t);
418   int (*sha384final)(archive_sha384_ctx *, void *);
419   int (*sha512init)(archive_sha512_ctx *);
420   int (*sha512update)(archive_sha512_ctx *, const void *, size_t);
421   int (*sha512final)(archive_sha512_ctx *, void *);
422 };
423 
424 extern const struct archive_digest __archive_digest;
425 
426 #endif
427