archive_hmac.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifdef ARCHIVE_HMAC_USE_Apple_CommonCrypto
  32. static int
  33. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  34. {
  35. CCHmacInit(ctx, kCCHmacAlgSHA1, key, key_len);
  36. return 0;
  37. }
  38. static void
  39. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  40. size_t data_len)
  41. {
  42. CCHmacUpdate(ctx, data, data_len);
  43. }
  44. static void
  45. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  46. {
  47. CCHmacFinal(ctx, out);
  48. *out_len = 20;
  49. }
  50. static void
  51. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  52. {
  53. memset(ctx, 0, sizeof(*ctx));
  54. }
  55. #elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
  56. static int
  57. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  58. {
  59. BCRYPT_ALG_HANDLE hAlg;
  60. BCRYPT_HASH_HANDLE hHash;
  61. DWORD hash_len;
  62. PBYTE hash;
  63. ULONG result;
  64. NTSTATUS status;
  65. ctx->hAlg = NULL;
  66. status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA1_ALGORITHM,
  67. MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
  68. if (!BCRYPT_SUCCESS(status))
  69. return -1;
  70. status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH, (PUCHAR)&hash_len,
  71. sizeof(hash_len), &result, 0);
  72. if (!BCRYPT_SUCCESS(status)) {
  73. BCryptCloseAlgorithmProvider(hAlg, 0);
  74. return -1;
  75. }
  76. hash = (PBYTE)HeapAlloc(GetProcessHeap(), 0, hash_len);
  77. if (hash == NULL) {
  78. BCryptCloseAlgorithmProvider(hAlg, 0);
  79. return -1;
  80. }
  81. status = BCryptCreateHash(hAlg, &hHash, NULL, 0,
  82. (PUCHAR)key, (ULONG)key_len, BCRYPT_HASH_REUSABLE_FLAG);
  83. if (!BCRYPT_SUCCESS(status)) {
  84. BCryptCloseAlgorithmProvider(hAlg, 0);
  85. HeapFree(GetProcessHeap(), 0, hash);
  86. return -1;
  87. }
  88. ctx->hAlg = hAlg;
  89. ctx->hHash = hHash;
  90. ctx->hash_len = hash_len;
  91. ctx->hash = hash;
  92. return 0;
  93. }
  94. static void
  95. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  96. size_t data_len)
  97. {
  98. BCryptHashData(ctx->hHash, (PUCHAR)(uintptr_t)data, (ULONG)data_len, 0);
  99. }
  100. static void
  101. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  102. {
  103. BCryptFinishHash(ctx->hHash, ctx->hash, ctx->hash_len, 0);
  104. if (ctx->hash_len == *out_len)
  105. memcpy(out, ctx->hash, *out_len);
  106. }
  107. static void
  108. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  109. {
  110. if (ctx->hAlg != NULL) {
  111. BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
  112. HeapFree(GetProcessHeap(), 0, ctx->hash);
  113. ctx->hAlg = NULL;
  114. }
  115. }
  116. #elif defined(HAVE_LIBNETTLE)
  117. static int
  118. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  119. {
  120. hmac_sha1_set_key(ctx, key_len, key);
  121. return 0;
  122. }
  123. static void
  124. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  125. size_t data_len)
  126. {
  127. hmac_sha1_update(ctx, data_len, data);
  128. }
  129. static void
  130. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  131. {
  132. hmac_sha1_digest(ctx, (unsigned)*out_len, out);
  133. }
  134. static void
  135. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  136. {
  137. memset(ctx, 0, sizeof(*ctx));
  138. }
  139. #elif defined(HAVE_LIBCRYPTO)
  140. static int
  141. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  142. {
  143. HMAC_CTX_init(ctx);
  144. HMAC_Init(ctx, key, key_len, EVP_sha1());
  145. return 0;
  146. }
  147. static void
  148. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  149. size_t data_len)
  150. {
  151. HMAC_Update(ctx, data, data_len);
  152. }
  153. static void
  154. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  155. {
  156. unsigned int len = (unsigned int)*out_len;
  157. HMAC_Final(ctx, out, &len);
  158. *out_len = len;
  159. }
  160. static void
  161. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  162. {
  163. HMAC_CTX_cleanup(ctx);
  164. memset(ctx, 0, sizeof(*ctx));
  165. }
  166. #else
  167. /* Stub */
  168. static int
  169. __hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
  170. {
  171. (void)ctx;/* UNUSED */
  172. (void)key;/* UNUSED */
  173. (void)key_len;/* UNUSED */
  174. return -1;
  175. }
  176. static void
  177. __hmac_sha1_update(archive_hmac_sha1_ctx *ctx, const uint8_t *data,
  178. size_t data_len)
  179. {
  180. (void)ctx;/* UNUSED */
  181. (void)data;/* UNUSED */
  182. (void)data_len;/* UNUSED */
  183. }
  184. static void
  185. __hmac_sha1_final(archive_hmac_sha1_ctx *ctx, uint8_t *out, size_t *out_len)
  186. {
  187. (void)ctx;/* UNUSED */
  188. (void)out;/* UNUSED */
  189. (void)out_len;/* UNUSED */
  190. }
  191. static void
  192. __hmac_sha1_cleanup(archive_hmac_sha1_ctx *ctx)
  193. {
  194. (void)ctx;/* UNUSED */
  195. }
  196. #endif
  197. const struct archive_hmac __archive_hmac = {
  198. &__hmac_sha1_init,
  199. &__hmac_sha1_update,
  200. &__hmac_sha1_final,
  201. &__hmac_sha1_cleanup,
  202. };