archive_hmac.c 7.3 KB

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