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