2 * Copyright (c) 2014 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
32 #include "archive_hmac_private.h"
35 * On systems that do not support any recognized crypto libraries,
36 * the archive_hmac.c file is expected to define no usable symbols.
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
43 int __libarchive_hmac_build_hack(void) {
48 #ifdef ARCHIVE_HMAC_USE_Apple_CommonCrypto
51 __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
53 CCHmacInit(ctx, kCCHmacAlgSHA1, key, key_len);
58 __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
61 CCHmacUpdate(ctx, data, data_len);
65 __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
67 CCHmacFinal(ctx, out);
72 __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
74 memset(ctx, 0, sizeof(*ctx));
77 #elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
79 #ifndef BCRYPT_HASH_REUSABLE_FLAG
80 # define BCRYPT_HASH_REUSABLE_FLAG 0x00000020
84 __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
87 #pragma GCC diagnostic ignored "-Wcast-qual"
89 BCRYPT_ALG_HANDLE hAlg;
90 BCRYPT_HASH_HANDLE hHash;
97 status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA1_ALGORITHM,
98 MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
99 if (!BCRYPT_SUCCESS(status))
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);
107 hash = (PBYTE)HeapAlloc(GetProcessHeap(), 0, hash_len);
109 BCryptCloseAlgorithmProvider(hAlg, 0);
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);
122 ctx->hash_len = hash_len;
129 __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
132 BCryptHashData(ctx->hHash, (PUCHAR)(uintptr_t)data, (ULONG)data_len, 0);
136 __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
138 BCryptFinishHash(ctx->hHash, ctx->hash, ctx->hash_len, 0);
139 if (ctx->hash_len == *out_len)
140 memcpy(out, ctx->hash, *out_len);
144 __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
146 if (ctx->hAlg != NULL) {
147 BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
148 HeapFree(GetProcessHeap(), 0, ctx->hash);
153 #elif defined(HAVE_LIBMBEDCRYPTO) && defined(HAVE_MBEDTLS_MD_H)
156 __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
158 const mbedtls_md_info_t *info;
161 mbedtls_md_init(ctx);
162 info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
164 mbedtls_md_free(ctx);
167 ret = mbedtls_md_setup(ctx, info, 1);
169 mbedtls_md_free(ctx);
172 ret = mbedtls_md_hmac_starts(ctx, key, key_len);
174 mbedtls_md_free(ctx);
181 __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
184 mbedtls_md_hmac_update(ctx, data, data_len);
187 static void __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
189 (void)out_len; /* UNUSED */
191 mbedtls_md_hmac_finish(ctx, out);
194 static void __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
196 mbedtls_md_free(ctx);
197 memset(ctx, 0, sizeof(*ctx));
200 #elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_HMAC_H)
203 __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
205 hmac_sha1_set_key(ctx, key_len, key);
210 __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
213 hmac_sha1_update(ctx, data_len, data);
217 __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
219 hmac_sha1_digest(ctx, (unsigned)*out_len, out);
223 __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
225 memset(ctx, 0, sizeof(*ctx));
228 #elif defined(HAVE_LIBCRYPTO)
231 __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
233 *ctx = HMAC_CTX_new();
236 HMAC_Init_ex(*ctx, key, key_len, EVP_sha1(), NULL);
241 __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
244 HMAC_Update(*ctx, data, data_len);
248 __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
250 unsigned int len = (unsigned int)*out_len;
252 HMAC_Final(*ctx, out, &len);
257 __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
267 __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
269 (void)ctx;/* UNUSED */
270 (void)key;/* UNUSED */
271 (void)key_len;/* UNUSED */
276 __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
279 (void)ctx;/* UNUSED */
280 (void)data;/* UNUSED */
281 (void)data_len;/* UNUSED */
285 __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
287 (void)ctx;/* UNUSED */
288 (void)out;/* UNUSED */
289 (void)out_len;/* UNUSED */
293 __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
295 (void)ctx;/* UNUSED */
300 const struct archive_hmac __archive_hmac = {
304 &__hmac_sha1_cleanup,