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#include "archive_platform.h"
27
28#ifdef HAVE_STRING_H
29#include <string.h>
30#endif
31#include "archive.h"
32#include "archive_hmac_private.h"
33
34/*
35 * On systems that do not support any recognized crypto libraries,
36 * the archive_hmac.c file is expected to define no usable symbols.
37 *
38 * But some compilers and linkers choke on empty object files, so
39 * define a public symbol that will always exist.  This could
40 * be removed someday if this file gains another always-present
41 * symbol definition.
42 */
43int __libarchive_hmac_build_hack(void) {
44	return 0;
45}
46
47
48#ifdef ARCHIVE_HMAC_USE_Apple_CommonCrypto
49
50static int
51__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
52{
53	CCHmacInit(ctx, kCCHmacAlgSHA1, key, key_len);
54	return 0;
55}
56
57static void
58__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
59    size_t data_len)
60{
61	CCHmacUpdate(ctx, data, data_len);
62}
63
64static void
65__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
66{
67	CCHmacFinal(ctx, out);
68	*out_len = 20;
69}
70
71static void
72__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
73{
74	memset(ctx, 0, sizeof(*ctx));
75}
76
77#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
78
79#ifndef BCRYPT_HASH_REUSABLE_FLAG
80# define BCRYPT_HASH_REUSABLE_FLAG 0x00000020
81#endif
82
83static int
84__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
85{
86#ifdef __GNUC__
87#pragma GCC diagnostic ignored "-Wcast-qual"
88#endif
89	BCRYPT_ALG_HANDLE hAlg;
90	BCRYPT_HASH_HANDLE hHash;
91	DWORD hash_len;
92	PBYTE hash;
93	ULONG result;
94	NTSTATUS status;
95
96	ctx->hAlg = NULL;
97	status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA1_ALGORITHM,
98		MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
99	if (!BCRYPT_SUCCESS(status))
100		return -1;
101	status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, (PUCHAR)&hash_len,
102		sizeof(hash_len), &result, 0);
103	if (!BCRYPT_SUCCESS(status)) {
104		BCryptCloseAlgorithmProvider(hAlg, 0);
105		return -1;
106	}
107	hash = (PBYTE)HeapAlloc(GetProcessHeap(), 0, hash_len);
108	if (hash == NULL) {
109		BCryptCloseAlgorithmProvider(hAlg, 0);
110		return -1;
111	}
112	status = BCryptCreateHash(hAlg, &hHash, NULL, 0,
113		(PUCHAR)key, (ULONG)key_len, BCRYPT_HASH_REUSABLE_FLAG);
114	if (!BCRYPT_SUCCESS(status)) {
115		BCryptCloseAlgorithmProvider(hAlg, 0);
116		HeapFree(GetProcessHeap(), 0, hash);
117		return -1;
118	}
119
120	ctx->hAlg = hAlg;
121	ctx->hHash = hHash;
122	ctx->hash_len = hash_len;
123	ctx->hash = hash;
124
125	return 0;
126}
127
128static void
129__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
130	size_t data_len)
131{
132	BCryptHashData(ctx->hHash, (PUCHAR)(uintptr_t)data, (ULONG)data_len, 0);
133}
134
135static void
136__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
137{
138	BCryptFinishHash(ctx->hHash, ctx->hash, ctx->hash_len, 0);
139	if (ctx->hash_len == *out_len)
140		memcpy(out, ctx->hash, *out_len);
141}
142
143static void
144__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
145{
146	if (ctx->hAlg != NULL) {
147		BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
148		HeapFree(GetProcessHeap(), 0, ctx->hash);
149		ctx->hAlg = NULL;
150	}
151}
152
153#elif defined(HAVE_LIBMBEDCRYPTO) && defined(HAVE_MBEDTLS_MD_H)
154
155static int
156__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
157{
158        const mbedtls_md_info_t *info;
159        int ret;
160
161        mbedtls_md_init(ctx);
162        info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
163        if (info == NULL) {
164                mbedtls_md_free(ctx);
165                return (-1);
166        }
167        ret = mbedtls_md_setup(ctx, info, 1);
168        if (ret != 0) {
169                mbedtls_md_free(ctx);
170                return (-1);
171        }
172	ret = mbedtls_md_hmac_starts(ctx, key, key_len);
173	if (ret != 0) {
174		mbedtls_md_free(ctx);
175		return (-1);
176	}
177	return 0;
178}
179
180static void
181__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
182    size_t data_len)
183{
184	mbedtls_md_hmac_update(ctx, data, data_len);
185}
186
187static void __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
188{
189	(void)out_len;	/* UNUSED */
190
191	mbedtls_md_hmac_finish(ctx, out);
192}
193
194static void __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
195{
196	mbedtls_md_free(ctx);
197	memset(ctx, 0, sizeof(*ctx));
198}
199
200#elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_HMAC_H)
201
202static int
203__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
204{
205	hmac_sha1_set_key(ctx, key_len, key);
206	return 0;
207}
208
209static void
210__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
211    size_t data_len)
212{
213	hmac_sha1_update(ctx, data_len, data);
214}
215
216static void
217__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
218{
219	hmac_sha1_digest(ctx, (unsigned)*out_len, out);
220}
221
222static void
223__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
224{
225	memset(ctx, 0, sizeof(*ctx));
226}
227
228#elif defined(HAVE_LIBCRYPTO)
229
230static int
231__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
232{
233	*ctx = HMAC_CTX_new();
234	if (*ctx == NULL)
235		return -1;
236	HMAC_Init_ex(*ctx, key, key_len, EVP_sha1(), NULL);
237	return 0;
238}
239
240static void
241__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
242    size_t data_len)
243{
244	HMAC_Update(*ctx, data, data_len);
245}
246
247static void
248__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
249{
250	unsigned int len = (unsigned int)*out_len;
251
252	HMAC_Final(*ctx, out, &len);
253	*out_len = len;
254}
255
256static void
257__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
258{
259	HMAC_CTX_free(*ctx);
260	*ctx = NULL;
261}
262
263#else
264
265/* Stub */
266static int
267__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
268{
269	(void)ctx;/* UNUSED */
270	(void)key;/* UNUSED */
271	(void)key_len;/* UNUSED */
272	return -1;
273}
274
275static void
276__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
277    size_t data_len)
278{
279	(void)ctx;/* UNUSED */
280	(void)data;/* UNUSED */
281	(void)data_len;/* UNUSED */
282}
283
284static void
285__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
286{
287	(void)ctx;/* UNUSED */
288	(void)out;/* UNUSED */
289	(void)out_len;/* UNUSED */
290}
291
292static void
293__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
294{
295	(void)ctx;/* UNUSED */
296}
297
298#endif
299
300const struct archive_hmac __archive_hmac = {
301	&__hmac_sha1_init,
302	&__hmac_sha1_update,
303	&__hmac_sha1_final,
304	&__hmac_sha1_cleanup,
305};
306