1/*-
2 * Copyright (c) 2009 Joerg Sonnenberger
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD: head/lib/libarchive/archive_hash.h 201171 2009-12-29 06:39:07Z kientzle $
26 */
27
28#ifndef __LIBARCHIVE_BUILD
29#error This header is only to be used internally to libarchive.
30#endif
31
32#if defined(__APPLE__)
33#include <CommonCrypto/CommonDigest.h>
34#endif
35
36/*
37 * Hash function support in various Operating Systems:
38 *
39 * NetBSD:
40 * - MD5 and SHA1 in libc: without _ after algorithm name
41 * - SHA2 in libc: with _ after algorithm name
42 *
43 * OpenBSD:
44 * - MD5, SHA1 and SHA2 in libc: without _ after algorithm name
45 * - OpenBSD 4.4 and earlier have SHA2 in libc with _ after algorithm name
46 *
47 * DragonFly and FreeBSD (XXX not used yet):
48 * - MD5 and SHA1 in libmd: without _ after algorithm name
49 * - SHA256: with _ after algorithm name
50 *
51 * OpenSSL:
52 * - MD5, SHA1 and SHA2 in libcrypto: with _ after algorithm name
53 */
54
55#if defined(HAVE_MD5_H) && defined(HAVE_MD5INIT)
56#  include <md5.h>
57#  define ARCHIVE_HAS_MD5
58typedef MD5_CTX archive_md5_ctx;
59#  define archive_md5_init(ctx)			MD5Init(ctx)
60#  define archive_md5_final(ctx, buf)		MD5Final(buf, ctx)
61#  define archive_md5_update(ctx, buf, n)	MD5Update(ctx, buf, n)
62#elif defined(HAVE_OPENSSL_MD5_H)
63#  include <openssl/md5.h>
64#  define ARCHIVE_HAS_MD5
65typedef MD5_CTX archive_md5_ctx;
66#  define archive_md5_init(ctx)			MD5_Init(ctx)
67#  define archive_md5_final(ctx, buf)		MD5_Final(buf, ctx)
68#  define archive_md5_update(ctx, buf, n)	MD5_Update(ctx, buf, n)
69#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(CALG_MD5)
70#  define ARCHIVE_HAS_MD5
71typedef MD5_CTX archive_md5_ctx;
72#  define archive_md5_init(ctx)			MD5_Init(ctx)
73#  define archive_md5_final(ctx, buf)		MD5_Final(buf, ctx)
74#  define archive_md5_update(ctx, buf, n)	MD5_Update(ctx, buf, n)
75#elif defined(__APPLE__)
76#  define ARCHIVE_HAS_MD5
77typedef CC_MD5_CTX archive_md5_ctx;
78#  define archive_md5_init(ctx)			CC_MD5_Init(ctx)
79#  define archive_md5_final(ctx, buf)		CC_MD5_Final(buf, ctx)
80#  define archive_md5_update(ctx, buf, n)	CC_MD5_Update(ctx, buf, n)
81#endif
82
83#if defined(HAVE_RMD160_H) && defined(HAVE_RMD160INIT)
84#  include <rmd160.h>
85#  define ARCHIVE_HAS_RMD160
86typedef RMD160_CTX archive_rmd160_ctx;
87#  define archive_rmd160_init(ctx)		RMD160Init(ctx)
88#  define archive_rmd160_final(ctx, buf)	RMD160Final(buf, ctx)
89#  define archive_rmd160_update(ctx, buf, n)	RMD160Update(ctx, buf, n)
90#elif defined(HAVE_OPENSSL_RIPEMD_H)
91#  include <openssl/ripemd.h>
92#  define ARCHIVE_HAS_RMD160
93typedef RIPEMD160_CTX archive_rmd160_ctx;
94#  define archive_rmd160_init(ctx)		RIPEMD160_Init(ctx)
95#  define archive_rmd160_final(ctx, buf)	RIPEMD160_Final(buf, ctx)
96#  define archive_rmd160_update(ctx, buf, n)	RIPEMD160_Update(ctx, buf, n)
97#elif defined(__APPLE__)
98// XXX: 4528609
99#endif
100
101#if defined(HAVE_SHA1_H) && defined(HAVE_SHA1INIT)
102#  include <sha1.h>
103#  define ARCHIVE_HAS_SHA1
104typedef SHA1_CTX archive_sha1_ctx;
105#  define archive_sha1_init(ctx)		SHA1Init(ctx)
106#  define archive_sha1_final(ctx, buf)		SHA1Final(buf, ctx)
107#  define archive_sha1_update(ctx, buf, n)	SHA1Update(ctx, buf, n)
108#elif defined(HAVE_OPENSSL_SHA_H)
109#  include <openssl/sha.h>
110#  define ARCHIVE_HAS_SHA1
111typedef SHA_CTX archive_sha1_ctx;
112#  define archive_sha1_init(ctx)		SHA1_Init(ctx)
113#  define archive_sha1_final(ctx, buf)		SHA1_Final(buf, ctx)
114#  define archive_sha1_update(ctx, buf, n)	SHA1_Update(ctx, buf, n)
115#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(CALG_SHA1)
116#  define ARCHIVE_HAS_SHA1
117typedef SHA1_CTX archive_sha1_ctx;
118#  define archive_sha1_init(ctx)		SHA1_Init(ctx)
119#  define archive_sha1_final(ctx, buf)		SHA1_Final(buf, ctx)
120#  define archive_sha1_update(ctx, buf, n)	SHA1_Update(ctx, buf, n)
121#elif defined(__APPLE__)
122#  define ARCHIVE_HAS_SHA1
123typedef CC_SHA1_CTX archive_sha1_ctx;
124#  define archive_sha1_init(ctx)		CC_SHA1_Init(ctx)
125#  define archive_sha1_final(ctx, buf)		CC_SHA1_Final(buf, ctx)
126#  define archive_sha1_update(ctx, buf, n)	CC_SHA1_Update(ctx, buf, n)
127#endif
128
129#if defined(HAVE_SHA2_H) && defined(HAVE_SHA256_INIT)
130#  include <sha2.h>
131#  define ARCHIVE_HAS_SHA256
132typedef SHA256_CTX archive_sha256_ctx;
133#  define archive_sha256_init(ctx)		SHA256_Init(ctx)
134#  define archive_sha256_final(ctx, buf)	SHA256_Final(buf, ctx)
135#  define archive_sha256_update(ctx, buf, n)	SHA256_Update(ctx, buf, n)
136#elif defined(HAVE_SHA2_H) && defined(HAVE_SHA256INIT)
137#  include <sha2.h>
138#  define ARCHIVE_HAS_SHA256
139typedef SHA256_CTX archive_sha256_ctx;
140#  define archive_sha256_init(ctx)		SHA256Init(ctx)
141#  define archive_sha256_final(ctx, buf)	SHA256Final(buf, ctx)
142#  define archive_sha256_update(ctx, buf, n)	SHA256Update(ctx, buf, n)
143#elif defined(HAVE_OPENSSL_SHA_H) && defined(HAVE_OPENSSL_SHA256_INIT)
144#  include <openssl/sha.h>
145#  define ARCHIVE_HAS_SHA256
146typedef SHA256_CTX archive_sha256_ctx;
147#  define archive_sha256_init(ctx)		SHA256_Init(ctx)
148#  define archive_sha256_final(ctx, buf)	SHA256_Final(buf, ctx)
149#  define archive_sha256_update(ctx, buf, n)	SHA256_Update(ctx, buf, n)
150#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(CALG_SHA_256)
151#  define ARCHIVE_HAS_SHA256
152typedef SHA256_CTX archive_sha256_ctx;
153#  define archive_sha256_init(ctx)		SHA256_Init(ctx)
154#  define archive_sha256_final(ctx, buf)	SHA256_Final(buf, ctx)
155#  define archive_sha256_update(ctx, buf, n)	SHA256_Update(ctx, buf, n)
156#elif defined(__APPLE__)
157#  define ARCHIVE_HAS_SHA256
158typedef CC_SHA256_CTX archive_sha256_ctx;
159#  define archive_sha256_init(ctx)		CC_SHA256_Init(ctx)
160#  define archive_sha256_final(ctx, buf)	CC_SHA256_Final(buf, ctx)
161#  define archive_sha256_update(ctx, buf, n)	CC_SHA256_Update(ctx, buf, n)
162#endif
163
164#if defined(HAVE_SHA2_H) && defined(HAVE_SHA384_INIT)
165#  include <sha2.h>
166#  define ARCHIVE_HAS_SHA384
167typedef SHA384_CTX archive_sha384_ctx;
168#  define archive_sha384_init(ctx)		SHA384_Init(ctx)
169#  define archive_sha384_final(ctx, buf)	SHA384_Final(buf, ctx)
170#  define archive_sha384_update(ctx, buf, n)	SHA384_Update(ctx, buf, n)
171#elif defined(HAVE_SHA2_H) && defined(HAVE_SHA384INIT)
172#  include <sha2.h>
173#  define ARCHIVE_HAS_SHA384
174typedef SHA384_CTX archive_sha384_ctx;
175#  define archive_sha384_init(ctx)		SHA384Init(ctx)
176#  define archive_sha384_final(ctx, buf)	SHA384Final(buf, ctx)
177#  define archive_sha384_update(ctx, buf, n)	SHA384Update(ctx, buf, n)
178#elif defined(HAVE_OPENSSL_SHA_H) && defined(HAVE_OPENSSL_SHA384_INIT)
179#  include <openssl/sha.h>
180#  define ARCHIVE_HAS_SHA384
181typedef SHA512_CTX archive_sha384_ctx;
182#  define archive_sha384_init(ctx)		SHA384_Init(ctx)
183#  define archive_sha384_final(ctx, buf)	SHA384_Final(buf, ctx)
184#  define archive_sha384_update(ctx, buf, n)	SHA384_Update(ctx, buf, n)
185#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(CALG_SHA_384)
186#  define ARCHIVE_HAS_SHA384
187typedef SHA512_CTX archive_sha384_ctx;
188#  define archive_sha384_init(ctx)		SHA384_Init(ctx)
189#  define archive_sha384_final(ctx, buf)	SHA384_Final(buf, ctx)
190#  define archive_sha384_update(ctx, buf, n)	SHA384_Update(ctx, buf, n)
191#elif defined(__APPLE__)
192#  define ARCHIVE_HAS_SHA384
193typedef CC_SHA512_CTX archive_sha384_ctx;
194#  define archive_sha384_init(ctx)		CC_SHA384_Init(ctx)
195#  define archive_sha384_final(ctx, buf)	CC_SHA384_Final(buf, ctx)
196#  define archive_sha384_update(ctx, buf, n)	CC_SHA384_Update(ctx, buf, n)
197#endif
198
199#if defined(HAVE_SHA2_H) && defined(HAVE_SHA512_INIT)
200#  include <sha2.h>
201#  define ARCHIVE_HAS_SHA512
202typedef SHA512_CTX archive_sha512_ctx;
203#  define archive_sha512_init(ctx)		SHA512_Init(ctx)
204#  define archive_sha512_final(ctx, buf)	SHA512_Final(buf, ctx)
205#  define archive_sha512_update(ctx, buf, n)	SHA512_Update(ctx, buf, n)
206#elif defined(HAVE_SHA2_H) && defined(HAVE_SHA512INIT)
207#  include <sha2.h>
208#  define ARCHIVE_HAS_SHA512
209typedef SHA512_CTX archive_sha512_ctx;
210#  define archive_sha512_init(ctx)		SHA512Init(ctx)
211#  define archive_sha512_final(ctx, buf)	SHA512Final(buf, ctx)
212#  define archive_sha512_update(ctx, buf, n)	SHA512Update(ctx, buf, n)
213#elif defined(HAVE_OPENSSL_SHA_H) && defined(HAVE_OPENSSL_SHA512_INIT)
214#  include <openssl/sha.h>
215#  define ARCHIVE_HAS_SHA512
216typedef SHA512_CTX archive_sha512_ctx;
217#  define archive_sha512_init(ctx)		SHA512_Init(ctx)
218#  define archive_sha512_final(ctx, buf)	SHA512_Final(buf, ctx)
219#  define archive_sha512_update(ctx, buf, n)	SHA512_Update(ctx, buf, n)
220#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(CALG_SHA_512)
221#  define ARCHIVE_HAS_SHA512
222typedef SHA512_CTX archive_sha512_ctx;
223#  define archive_sha512_init(ctx)		SHA512_Init(ctx)
224#  define archive_sha512_final(ctx, buf)	SHA512_Final(buf, ctx)
225#  define archive_sha512_update(ctx, buf, n)	SHA512_Update(ctx, buf, n)
226#elif defined(__APPLE__)
227#  define ARCHIVE_HAS_SHA512
228typedef CC_SHA512_CTX archive_sha512_ctx;
229#  define archive_sha512_init(ctx)		CC_SHA512_Init(ctx)
230#  define archive_sha512_final(ctx, buf)	CC_SHA512_Final(buf, ctx)
231#  define archive_sha512_update(ctx, buf, n)	CC_SHA512_Update(ctx, buf, n)
232#endif
233