archive_hmac.c revision 315432
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 BCRYPT_ALG_HANDLE hAlg; 87 BCRYPT_HASH_HANDLE hHash; 88 DWORD hash_len; 89 PBYTE hash; 90 ULONG result; 91 NTSTATUS status; 92 93 ctx->hAlg = NULL; 94 status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA1_ALGORITHM, 95 MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG); 96 if (!BCRYPT_SUCCESS(status)) 97 return -1; 98 status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, (PUCHAR)&hash_len, 99 sizeof(hash_len), &result, 0); 100 if (!BCRYPT_SUCCESS(status)) { 101 BCryptCloseAlgorithmProvider(hAlg, 0); 102 return -1; 103 } 104 hash = (PBYTE)HeapAlloc(GetProcessHeap(), 0, hash_len); 105 if (hash == NULL) { 106 BCryptCloseAlgorithmProvider(hAlg, 0); 107 return -1; 108 } 109 status = BCryptCreateHash(hAlg, &hHash, NULL, 0, 110 (PUCHAR)key, (ULONG)key_len, BCRYPT_HASH_REUSABLE_FLAG); 111 if (!BCRYPT_SUCCESS(status)) { 112 BCryptCloseAlgorithmProvider(hAlg, 0); 113 HeapFree(GetProcessHeap(), 0, hash); 114 return -1; 115 } 116 117 ctx->hAlg = hAlg; 118 ctx->hHash = hHash; 119 ctx->hash_len = hash_len; 120 ctx->hash = hash; 121 122 return 0; 123} 124 125static void 126__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data, 127 size_t data_len) 128{ 129 BCryptHashData(ctx->hHash, (PUCHAR)(uintptr_t)data, (ULONG)data_len, 0); 130} 131 132static void 133__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len) 134{ 135 BCryptFinishHash(ctx->hHash, ctx->hash, ctx->hash_len, 0); 136 if (ctx->hash_len == *out_len) 137 memcpy(out, ctx->hash, *out_len); 138} 139 140static void 141__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx) 142{ 143 if (ctx->hAlg != NULL) { 144 BCryptCloseAlgorithmProvider(ctx->hAlg, 0); 145 HeapFree(GetProcessHeap(), 0, ctx->hash); 146 ctx->hAlg = NULL; 147 } 148} 149 150#elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_HMAC_H) 151 152static int 153__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len) 154{ 155 hmac_sha1_set_key(ctx, key_len, key); 156 return 0; 157} 158 159static void 160__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data, 161 size_t data_len) 162{ 163 hmac_sha1_update(ctx, data_len, data); 164} 165 166static void 167__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len) 168{ 169 hmac_sha1_digest(ctx, (unsigned)*out_len, out); 170} 171 172static void 173__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx) 174{ 175 memset(ctx, 0, sizeof(*ctx)); 176} 177 178#elif defined(HAVE_LIBCRYPTO) 179 180static int 181__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len) 182{ 183 *ctx = HMAC_CTX_new(); 184 if (*ctx == NULL) 185 return -1; 186 HMAC_Init_ex(*ctx, key, key_len, EVP_sha1(), NULL); 187 return 0; 188} 189 190static void 191__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data, 192 size_t data_len) 193{ 194 HMAC_Update(*ctx, data, data_len); 195} 196 197static void 198__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len) 199{ 200 unsigned int len = (unsigned int)*out_len; 201 HMAC_Final(*ctx, out, &len); 202 *out_len = len; 203} 204 205static void 206__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx) 207{ 208 HMAC_CTX_free(*ctx); 209 *ctx = NULL; 210} 211 212#else 213 214/* Stub */ 215static int 216__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len) 217{ 218 (void)ctx;/* UNUSED */ 219 (void)key;/* UNUSED */ 220 (void)key_len;/* UNUSED */ 221 return -1; 222} 223 224static void 225__hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data, 226 size_t data_len) 227{ 228 (void)ctx;/* UNUSED */ 229 (void)data;/* UNUSED */ 230 (void)data_len;/* UNUSED */ 231} 232 233static void 234__hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len) 235{ 236 (void)ctx;/* UNUSED */ 237 (void)out;/* UNUSED */ 238 (void)out_len;/* UNUSED */ 239} 240 241static void 242__hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx) 243{ 244 (void)ctx;/* UNUSED */ 245} 246 247#endif 248 249const struct archive_hmac __archive_hmac = { 250 &__hmac_sha1_init, 251 &__hmac_sha1_update, 252 &__hmac_sha1_final, 253 &__hmac_sha1_cleanup, 254}; 255