2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011 Andres Mejia
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef ARCHIVE_DIGEST_PRIVATE_H_INCLUDED
28 #define ARCHIVE_DIGEST_PRIVATE_H_INCLUDED
30 #ifndef __LIBARCHIVE_BUILD
31 #error This header is only to be used internally to libarchive.
33 #ifndef __LIBARCHIVE_CONFIG_H_INCLUDED
34 #error "Should have include config.h first!"
38 * Crypto support in various Operating Systems:
41 * - MD5 and SHA1 in libc: without _ after algorithm name
42 * - SHA2 in libc: with _ after algorithm name
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
48 * DragonFly and FreeBSD:
49 * - MD5 libmd: without _ after algorithm name
50 * - SHA1, SHA256 and SHA512 in libmd: with _ after algorithm name
52 * Mac OS X (10.4 and later):
53 * - MD5, SHA1 and SHA2 in libSystem: with CC_ prefix and _ after algorithm name
56 * - MD5, SHA1 and SHA2 in libcrypto: with _ after algorithm name
59 * - MD5, SHA1 and SHA2 in archive_crypto.c using Windows crypto API
62 /* libc crypto headers */
63 #if defined(ARCHIVE_CRYPTO_MD5_LIBC)
66 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC)
69 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC)
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)
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
93 #if defined(ARCHIVE_CRYPTO_MD5_LIBMD)
96 #if defined(ARCHIVE_CRYPTO_RMD160_LIBMD)
99 #if defined(ARCHIVE_CRYPTO_SHA1_LIBMD)
102 #if defined(ARCHIVE_CRYPTO_SHA256_LIBMD)
105 #if defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
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>
118 /* mbed TLS crypto headers */
119 #if defined(ARCHIVE_CRYPTO_MD5_MBEDTLS)
120 #include <mbedtls/md5.h>
122 #if defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS)
123 #include <mbedtls/ripemd160.h>
125 #if defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS)
126 #include <mbedtls/sha1.h>
128 #if defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS)
129 #include <mbedtls/sha256.h>
131 #if defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\
132 defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS)
133 #include <mbedtls/sha512.h>
136 /* Nettle crypto headers */
137 #if defined(ARCHIVE_CRYPTO_MD5_NETTLE)
138 #include <nettle/md5.h>
140 #if defined(ARCHIVE_CRYPTO_RMD160_NETTLE)
141 #include <nettle/ripemd160.h>
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>
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"
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)
168 #include <wincrypt.h>
171 HCRYPTPROV cryptProv;
177 #if defined(ARCHIVE_CRYPTO_MD5_LIBC)
178 typedef MD5_CTX archive_md5_ctx;
179 #elif defined(ARCHIVE_CRYPTO_MD5_LIBMD)
180 typedef MD5_CTX archive_md5_ctx;
181 #elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM)
182 typedef CC_MD5_CTX archive_md5_ctx;
183 #elif defined(ARCHIVE_CRYPTO_MD5_MBEDTLS)
184 typedef mbedtls_md5_context archive_md5_ctx;
185 #elif defined(ARCHIVE_CRYPTO_MD5_NETTLE)
186 typedef struct md5_ctx archive_md5_ctx;
187 #elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL)
188 typedef EVP_MD_CTX *archive_md5_ctx;
189 #elif defined(ARCHIVE_CRYPTO_MD5_WIN)
190 typedef Digest_CTX archive_md5_ctx;
192 typedef unsigned char archive_md5_ctx;
195 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC)
196 typedef RMD160_CTX archive_rmd160_ctx;
197 #elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD)
198 typedef RIPEMD160_CTX archive_rmd160_ctx;
199 #elif defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS)
200 typedef mbedtls_ripemd160_context archive_rmd160_ctx;
201 #elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE)
202 typedef struct ripemd160_ctx archive_rmd160_ctx;
203 #elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
204 typedef EVP_MD_CTX *archive_rmd160_ctx;
206 typedef unsigned char archive_rmd160_ctx;
209 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC)
210 typedef SHA1_CTX archive_sha1_ctx;
211 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD)
212 typedef SHA1_CTX archive_sha1_ctx;
213 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM)
214 typedef CC_SHA1_CTX archive_sha1_ctx;
215 #elif defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS)
216 typedef mbedtls_sha1_context archive_sha1_ctx;
217 #elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE)
218 typedef struct sha1_ctx archive_sha1_ctx;
219 #elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL)
220 typedef EVP_MD_CTX *archive_sha1_ctx;
221 #elif defined(ARCHIVE_CRYPTO_SHA1_WIN)
222 typedef Digest_CTX archive_sha1_ctx;
224 typedef unsigned char archive_sha1_ctx;
227 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC)
228 typedef SHA256_CTX archive_sha256_ctx;
229 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2)
230 typedef SHA256_CTX archive_sha256_ctx;
231 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3)
232 typedef SHA2_CTX archive_sha256_ctx;
233 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD)
234 typedef SHA256_CTX archive_sha256_ctx;
235 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM)
236 typedef CC_SHA256_CTX archive_sha256_ctx;
237 #elif defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS)
238 typedef mbedtls_sha256_context archive_sha256_ctx;
239 #elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE)
240 typedef struct sha256_ctx archive_sha256_ctx;
241 #elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL)
242 typedef EVP_MD_CTX *archive_sha256_ctx;
243 #elif defined(ARCHIVE_CRYPTO_SHA256_WIN)
244 typedef Digest_CTX archive_sha256_ctx;
246 typedef unsigned char archive_sha256_ctx;
249 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC)
250 typedef SHA384_CTX archive_sha384_ctx;
251 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2)
252 typedef SHA384_CTX archive_sha384_ctx;
253 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3)
254 typedef SHA2_CTX archive_sha384_ctx;
255 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM)
256 typedef CC_SHA512_CTX archive_sha384_ctx;
257 #elif defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS)
258 typedef mbedtls_sha512_context archive_sha384_ctx;
259 #elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE)
260 typedef struct sha384_ctx archive_sha384_ctx;
261 #elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL)
262 typedef EVP_MD_CTX *archive_sha384_ctx;
263 #elif defined(ARCHIVE_CRYPTO_SHA384_WIN)
264 typedef Digest_CTX archive_sha384_ctx;
266 typedef unsigned char archive_sha384_ctx;
269 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC)
270 typedef SHA512_CTX archive_sha512_ctx;
271 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2)
272 typedef SHA512_CTX archive_sha512_ctx;
273 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3)
274 typedef SHA2_CTX archive_sha512_ctx;
275 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
276 typedef SHA512_CTX archive_sha512_ctx;
277 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)
278 typedef CC_SHA512_CTX archive_sha512_ctx;
279 #elif defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS)
280 typedef mbedtls_sha512_context archive_sha512_ctx;
281 #elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE)
282 typedef struct sha512_ctx archive_sha512_ctx;
283 #elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)
284 typedef EVP_MD_CTX *archive_sha512_ctx;
285 #elif defined(ARCHIVE_CRYPTO_SHA512_WIN)
286 typedef Digest_CTX archive_sha512_ctx;
288 typedef unsigned char archive_sha512_ctx;
292 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\
293 defined(ARCHIVE_CRYPTO_MD5_LIBMD) || \
294 defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\
295 defined(ARCHIVE_CRYPTO_MD5_MBEDTLS) ||\
296 defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\
297 defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\
298 defined(ARCHIVE_CRYPTO_MD5_WIN)
299 #define ARCHIVE_HAS_MD5
301 #define archive_md5_init(ctx)\
302 __archive_digest.md5init(ctx)
303 #define archive_md5_final(ctx, md)\
304 __archive_digest.md5final(ctx, md)
305 #define archive_md5_update(ctx, buf, n)\
306 __archive_digest.md5update(ctx, buf, n)
308 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\
309 defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS) ||\
310 defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\
311 defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
312 #define ARCHIVE_HAS_RMD160
314 #define archive_rmd160_init(ctx)\
315 __archive_digest.rmd160init(ctx)
316 #define archive_rmd160_final(ctx, md)\
317 __archive_digest.rmd160final(ctx, md)
318 #define archive_rmd160_update(ctx, buf, n)\
319 __archive_digest.rmd160update(ctx, buf, n)
321 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\
322 defined(ARCHIVE_CRYPTO_SHA1_LIBMD) || \
323 defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\
324 defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS) ||\
325 defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\
326 defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\
327 defined(ARCHIVE_CRYPTO_SHA1_WIN)
328 #define ARCHIVE_HAS_SHA1
330 #define archive_sha1_init(ctx)\
331 __archive_digest.sha1init(ctx)
332 #define archive_sha1_final(ctx, md)\
333 __archive_digest.sha1final(ctx, md)
334 #define archive_sha1_update(ctx, buf, n)\
335 __archive_digest.sha1update(ctx, buf, n)
337 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\
338 defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\
339 defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\
340 defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\
341 defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\
342 defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS) ||\
343 defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\
344 defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\
345 defined(ARCHIVE_CRYPTO_SHA256_WIN)
346 #define ARCHIVE_HAS_SHA256
348 #define archive_sha256_init(ctx)\
349 __archive_digest.sha256init(ctx)
350 #define archive_sha256_final(ctx, md)\
351 __archive_digest.sha256final(ctx, md)
352 #define archive_sha256_update(ctx, buf, n)\
353 __archive_digest.sha256update(ctx, buf, n)
355 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\
356 defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\
357 defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\
358 defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\
359 defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\
360 defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\
361 defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\
362 defined(ARCHIVE_CRYPTO_SHA384_WIN)
363 #define ARCHIVE_HAS_SHA384
365 #define archive_sha384_init(ctx)\
366 __archive_digest.sha384init(ctx)
367 #define archive_sha384_final(ctx, md)\
368 __archive_digest.sha384final(ctx, md)
369 #define archive_sha384_update(ctx, buf, n)\
370 __archive_digest.sha384update(ctx, buf, n)
372 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\
373 defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\
374 defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\
375 defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\
376 defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\
377 defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS) ||\
378 defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\
379 defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\
380 defined(ARCHIVE_CRYPTO_SHA512_WIN)
381 #define ARCHIVE_HAS_SHA512
383 #define archive_sha512_init(ctx)\
384 __archive_digest.sha512init(ctx)
385 #define archive_sha512_final(ctx, md)\
386 __archive_digest.sha512final(ctx, md)
387 #define archive_sha512_update(ctx, buf, n)\
388 __archive_digest.sha512update(ctx, buf, n)
390 /* Minimal interface to digest functionality for internal use in libarchive */
391 struct archive_digest
394 int (*md5init)(archive_md5_ctx *ctx);
395 int (*md5update)(archive_md5_ctx *, const void *, size_t);
396 int (*md5final)(archive_md5_ctx *, void *);
397 int (*rmd160init)(archive_rmd160_ctx *);
398 int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t);
399 int (*rmd160final)(archive_rmd160_ctx *, void *);
400 int (*sha1init)(archive_sha1_ctx *);
401 int (*sha1update)(archive_sha1_ctx *, const void *, size_t);
402 int (*sha1final)(archive_sha1_ctx *, void *);
403 int (*sha256init)(archive_sha256_ctx *);
404 int (*sha256update)(archive_sha256_ctx *, const void *, size_t);
405 int (*sha256final)(archive_sha256_ctx *, void *);
406 int (*sha384init)(archive_sha384_ctx *);
407 int (*sha384update)(archive_sha384_ctx *, const void *, size_t);
408 int (*sha384final)(archive_sha384_ctx *, void *);
409 int (*sha512init)(archive_sha512_ctx *);
410 int (*sha512update)(archive_sha512_ctx *, const void *, size_t);
411 int (*sha512final)(archive_sha512_ctx *, void *);
414 extern const struct archive_digest __archive_digest;