archive_hmac.c 6.2 KB

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