HardenedBSD src tree
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

258 Zeilen
6.2 KiB

  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_LIBNETTLE) && defined(HAVE_NETTLE_HMAC_H)
  135. static int
  136. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  137. {
  138. hmac_sha1_set_key(ctx, key_len, key);
  139. return 0;
  140. }
  141. static void
  142. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  143. size_t data_len)
  144. {
  145. hmac_sha1_update(ctx, data_len, data);
  146. }
  147. static void
  148. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  149. {
  150. hmac_sha1_digest(ctx, (unsigned)*out_len, out);
  151. }
  152. static void
  153. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  154. {
  155. memset(ctx, 0, sizeof(*ctx));
  156. }
  157. #elif defined(HAVE_LIBCRYPTO)
  158. static int
  159. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  160. {
  161. *ctx = HMAC_CTX_new();
  162. if (*ctx == NULL)
  163. return -1;
  164. HMAC_Init_ex(*ctx, key, key_len, EVP_sha1(), NULL);
  165. return 0;
  166. }
  167. static void
  168. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  169. size_t data_len)
  170. {
  171. HMAC_Update(*ctx, data, data_len);
  172. }
  173. static void
  174. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  175. {
  176. unsigned int len = (unsigned int)*out_len;
  177. HMAC_Final(*ctx, out, &len);
  178. *out_len = len;
  179. }
  180. static void
  181. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  182. {
  183. HMAC_CTX_free(*ctx);
  184. *ctx = NULL;
  185. }
  186. #else
  187. /* Stub */
  188. static int
  189. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  190. {
  191. (void)ctx;/* UNUSED */
  192. (void)key;/* UNUSED */
  193. (void)key_len;/* UNUSED */
  194. return -1;
  195. }
  196. static void
  197. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  198. size_t data_len)
  199. {
  200. (void)ctx;/* UNUSED */
  201. (void)data;/* UNUSED */
  202. (void)data_len;/* UNUSED */
  203. }
  204. static void
  205. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  206. {
  207. (void)ctx;/* UNUSED */
  208. (void)out;/* UNUSED */
  209. (void)out_len;/* UNUSED */
  210. }
  211. static void
  212. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  213. {
  214. (void)ctx;/* UNUSED */
  215. }
  216. #endif
  217. const struct archive_hmac __archive_hmac = {
  218. &__hmac_sha1_init,
  219. &__hmac_sha1_update,
  220. &__hmac_sha1_final,
  221. &__hmac_sha1_cleanup,
  222. };