1 /*-
2 * Copyright (c) 2014 Michihiro NAKAJIMA
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 
26 #ifndef __LIBARCHIVE_BUILD
27 #error This header is only to be used internally to libarchive.
28 #endif
29 
30 #ifndef ARCHIVE_HMAC_PRIVATE_H_INCLUDED
31 #define ARCHIVE_HMAC_PRIVATE_H_INCLUDED
32 
33 /*
34  * On systems that do not support any recognized crypto libraries,
35  * the archive_hmac.c file is expected to define no usable symbols.
36  *
37  * But some compilers and linkers choke on empty object files, so
38  * define a public symbol that will always exist.  This could
39  * be removed someday if this file gains another always-present
40  * symbol definition.
41  */
42 int __libarchive_hmac_build_hack(void);
43 
44 #ifdef __APPLE__
45 # include <AvailabilityMacros.h>
46 # if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
47 #  define ARCHIVE_HMAC_USE_Apple_CommonCrypto
48 # endif
49 #endif
50 
51 #ifdef ARCHIVE_HMAC_USE_Apple_CommonCrypto
52 #include <CommonCrypto/CommonHMAC.h>
53 
54 typedef	CCHmacContext archive_hmac_sha1_ctx;
55 
56 #elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
57 #include <bcrypt.h>
58 
59 typedef struct {
60 	BCRYPT_ALG_HANDLE	hAlg;
61 	BCRYPT_HASH_HANDLE	hHash;
62 	DWORD				hash_len;
63 	PBYTE				hash;
64 
65 } archive_hmac_sha1_ctx;
66 
67 #elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_HMAC_H)
68 #include <nettle/hmac.h>
69 
70 typedef	struct hmac_sha1_ctx archive_hmac_sha1_ctx;
71 
72 #elif defined(HAVE_LIBCRYPTO)
73 #include <openssl/hmac.h>
74 
75 typedef	HMAC_CTX archive_hmac_sha1_ctx;
76 
77 #else
78 
79 typedef int archive_hmac_sha1_ctx;
80 
81 #endif
82 
83 
84 /* HMAC */
85 #define archive_hmac_sha1_init(ctx, key, key_len)\
86 	__archive_hmac.__hmac_sha1_init(ctx, key, key_len)
87 #define archive_hmac_sha1_update(ctx, data, data_len)\
88 	__archive_hmac.__hmac_sha1_update(ctx, data, data_len)
89 #define archive_hmac_sha1_final(ctx, out, out_len)\
90   	__archive_hmac.__hmac_sha1_final(ctx, out, out_len)
91 #define archive_hmac_sha1_cleanup(ctx)\
92 	__archive_hmac.__hmac_sha1_cleanup(ctx)
93 
94 
95 struct archive_hmac {
96 	/* HMAC */
97 	int (*__hmac_sha1_init)(archive_hmac_sha1_ctx *, const uint8_t *,
98 		size_t);
99 	void (*__hmac_sha1_update)(archive_hmac_sha1_ctx *, const uint8_t *,
100 		size_t);
101 	void (*__hmac_sha1_final)(archive_hmac_sha1_ctx *, uint8_t *, size_t *);
102 	void (*__hmac_sha1_cleanup)(archive_hmac_sha1_ctx *);
103 };
104 
105 extern const struct archive_hmac __archive_hmac;
106 #endif /* ARCHIVE_HMAC_PRIVATE_H_INCLUDED */
107