archive_digest_private.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2011 Andres Mejia
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef __LIBARCHIVE_BUILD
  27. #error This header is only to be used internally to libarchive.
  28. #endif
  29. #ifndef ARCHIVE_CRYPTO_PRIVATE_H_INCLUDED
  30. #define ARCHIVE_CRYPTO_PRIVATE_H_INCLUDED
  31. /*
  32. * Crypto support in various Operating Systems:
  33. *
  34. * NetBSD:
  35. * - MD5 and SHA1 in libc: without _ after algorithm name
  36. * - SHA2 in libc: with _ after algorithm name
  37. *
  38. * OpenBSD:
  39. * - MD5, SHA1 and SHA2 in libc: without _ after algorithm name
  40. * - OpenBSD 4.4 and earlier have SHA2 in libc with _ after algorithm name
  41. *
  42. * DragonFly and FreeBSD:
  43. * - MD5 libmd: without _ after algorithm name
  44. * - SHA1, SHA256 and SHA512 in libmd: with _ after algorithm name
  45. *
  46. * Mac OS X (10.4 and later):
  47. * - MD5, SHA1 and SHA2 in libSystem: with CC_ prefix and _ after algorithm name
  48. *
  49. * OpenSSL:
  50. * - MD5, SHA1 and SHA2 in libcrypto: with _ after algorithm name
  51. *
  52. * Windows:
  53. * - MD5, SHA1 and SHA2 in archive_crypto.c using Windows crypto API
  54. */
  55. /* libc crypto headers */
  56. #if defined(ARCHIVE_CRYPTO_MD5_LIBC)
  57. #include <md5.h>
  58. #endif
  59. #if defined(ARCHIVE_CRYPTO_RMD160_LIBC)
  60. #include <rmd160.h>
  61. #endif
  62. #if defined(ARCHIVE_CRYPTO_SHA1_LIBC)
  63. #include <sha1.h>
  64. #endif
  65. #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\
  66. defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\
  67. defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\
  68. defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\
  69. defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\
  70. defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\
  71. defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\
  72. defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\
  73. defined(ARCHIVE_CRYPTO_SHA512_LIBC3)
  74. #include <sha2.h>
  75. #endif
  76. /* libmd crypto headers */
  77. #if defined(ARCHIVE_CRYPTO_MD5_LIBMD) ||\
  78. defined(ARCHIVE_CRYPTO_RMD160_LIBMD) ||\
  79. defined(ARCHIVE_CRYPTO_SHA1_LIBMD) ||\
  80. defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\
  81. defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
  82. #define ARCHIVE_CRYPTO_LIBMD 1
  83. #endif
  84. #if defined(ARCHIVE_CRYPTO_MD5_LIBMD)
  85. #include <md5.h>
  86. #endif
  87. #if defined(ARCHIVE_CRYPTO_RMD160_LIBMD)
  88. #include <ripemd.h>
  89. #endif
  90. #if defined(ARCHIVE_CRYPTO_SHA1_LIBMD)
  91. #include <sha.h>
  92. #endif
  93. #if defined(ARCHIVE_CRYPTO_SHA256_LIBMD)
  94. #include <sha256.h>
  95. #endif
  96. #if defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
  97. #include <sha512.h>
  98. #endif
  99. /* libSystem crypto headers */
  100. #if defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\
  101. defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\
  102. defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\
  103. defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\
  104. defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)
  105. #include <CommonCrypto/CommonDigest.h>
  106. #endif
  107. /* Nettle crypto headers */
  108. #if defined(ARCHIVE_CRYPTO_MD5_NETTLE)
  109. #include <nettle/md5.h>
  110. #endif
  111. #if defined(ARCHIVE_CRYPTO_RMD160_NETTLE)
  112. #include <nettle/ripemd160.h>
  113. #endif
  114. #if defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\
  115. defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\
  116. defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\
  117. defined(ARCHIVE_CRYPTO_SHA512_NETTLE)
  118. #include <nettle/sha.h>
  119. #endif
  120. /* OpenSSL crypto headers */
  121. #if defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\
  122. defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) ||\
  123. defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\
  124. defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\
  125. defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\
  126. defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)
  127. #define ARCHIVE_CRYPTO_OPENSSL 1
  128. #include "archive_openssl_evp_private.h"
  129. #endif
  130. /* Windows crypto headers */
  131. #if defined(ARCHIVE_CRYPTO_MD5_WIN) ||\
  132. defined(ARCHIVE_CRYPTO_SHA1_WIN) ||\
  133. defined(ARCHIVE_CRYPTO_SHA256_WIN) ||\
  134. defined(ARCHIVE_CRYPTO_SHA384_WIN) ||\
  135. defined(ARCHIVE_CRYPTO_SHA512_WIN)
  136. #include <windows.h>
  137. #include <wincrypt.h>
  138. typedef struct {
  139. int valid;
  140. HCRYPTPROV cryptProv;
  141. HCRYPTHASH hash;
  142. } Digest_CTX;
  143. #endif
  144. /* typedefs */
  145. #if defined(ARCHIVE_CRYPTO_MD5_LIBC)
  146. typedef MD5_CTX archive_md5_ctx;
  147. #elif defined(ARCHIVE_CRYPTO_MD5_LIBMD)
  148. typedef MD5_CTX archive_md5_ctx;
  149. #elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM)
  150. typedef CC_MD5_CTX archive_md5_ctx;
  151. #elif defined(ARCHIVE_CRYPTO_MD5_NETTLE)
  152. typedef struct md5_ctx archive_md5_ctx;
  153. #elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL)
  154. typedef EVP_MD_CTX *archive_md5_ctx;
  155. #elif defined(ARCHIVE_CRYPTO_MD5_WIN)
  156. typedef Digest_CTX archive_md5_ctx;
  157. #else
  158. typedef unsigned char archive_md5_ctx;
  159. #endif
  160. #if defined(ARCHIVE_CRYPTO_RMD160_LIBC)
  161. typedef RMD160_CTX archive_rmd160_ctx;
  162. #elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD)
  163. typedef RIPEMD160_CTX archive_rmd160_ctx;
  164. #elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE)
  165. typedef struct ripemd160_ctx archive_rmd160_ctx;
  166. #elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
  167. typedef EVP_MD_CTX *archive_rmd160_ctx;
  168. #else
  169. typedef unsigned char archive_rmd160_ctx;
  170. #endif
  171. #if defined(ARCHIVE_CRYPTO_SHA1_LIBC)
  172. typedef SHA1_CTX archive_sha1_ctx;
  173. #elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD)
  174. typedef SHA1_CTX archive_sha1_ctx;
  175. #elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM)
  176. typedef CC_SHA1_CTX archive_sha1_ctx;
  177. #elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE)
  178. typedef struct sha1_ctx archive_sha1_ctx;
  179. #elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL)
  180. typedef EVP_MD_CTX *archive_sha1_ctx;
  181. #elif defined(ARCHIVE_CRYPTO_SHA1_WIN)
  182. typedef Digest_CTX archive_sha1_ctx;
  183. #else
  184. typedef unsigned char archive_sha1_ctx;
  185. #endif
  186. #if defined(ARCHIVE_CRYPTO_SHA256_LIBC)
  187. typedef SHA256_CTX archive_sha256_ctx;
  188. #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2)
  189. typedef SHA256_CTX archive_sha256_ctx;
  190. #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3)
  191. typedef SHA2_CTX archive_sha256_ctx;
  192. #elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD)
  193. typedef SHA256_CTX archive_sha256_ctx;
  194. #elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM)
  195. typedef CC_SHA256_CTX archive_sha256_ctx;
  196. #elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE)
  197. typedef struct sha256_ctx archive_sha256_ctx;
  198. #elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL)
  199. typedef EVP_MD_CTX *archive_sha256_ctx;
  200. #elif defined(ARCHIVE_CRYPTO_SHA256_WIN)
  201. typedef Digest_CTX archive_sha256_ctx;
  202. #else
  203. typedef unsigned char archive_sha256_ctx;
  204. #endif
  205. #if defined(ARCHIVE_CRYPTO_SHA384_LIBC)
  206. typedef SHA384_CTX archive_sha384_ctx;
  207. #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2)
  208. typedef SHA384_CTX archive_sha384_ctx;
  209. #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3)
  210. typedef SHA2_CTX archive_sha384_ctx;
  211. #elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM)
  212. typedef CC_SHA512_CTX archive_sha384_ctx;
  213. #elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE)
  214. typedef struct sha384_ctx archive_sha384_ctx;
  215. #elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL)
  216. typedef EVP_MD_CTX *archive_sha384_ctx;
  217. #elif defined(ARCHIVE_CRYPTO_SHA384_WIN)
  218. typedef Digest_CTX archive_sha384_ctx;
  219. #else
  220. typedef unsigned char archive_sha384_ctx;
  221. #endif
  222. #if defined(ARCHIVE_CRYPTO_SHA512_LIBC)
  223. typedef SHA512_CTX archive_sha512_ctx;
  224. #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2)
  225. typedef SHA512_CTX archive_sha512_ctx;
  226. #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3)
  227. typedef SHA2_CTX archive_sha512_ctx;
  228. #elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
  229. typedef SHA512_CTX archive_sha512_ctx;
  230. #elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)
  231. typedef CC_SHA512_CTX archive_sha512_ctx;
  232. #elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE)
  233. typedef struct sha512_ctx archive_sha512_ctx;
  234. #elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)
  235. typedef EVP_MD_CTX *archive_sha512_ctx;
  236. #elif defined(ARCHIVE_CRYPTO_SHA512_WIN)
  237. typedef Digest_CTX archive_sha512_ctx;
  238. #else
  239. typedef unsigned char archive_sha512_ctx;
  240. #endif
  241. /* defines */
  242. #if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\
  243. defined(ARCHIVE_CRYPTO_MD5_LIBMD) || \
  244. defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\
  245. defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\
  246. defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\
  247. defined(ARCHIVE_CRYPTO_MD5_WIN)
  248. #define ARCHIVE_HAS_MD5
  249. #endif
  250. #define archive_md5_init(ctx)\
  251. __archive_digest.md5init(ctx)
  252. #define archive_md5_final(ctx, md)\
  253. __archive_digest.md5final(ctx, md)
  254. #define archive_md5_update(ctx, buf, n)\
  255. __archive_digest.md5update(ctx, buf, n)
  256. #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\
  257. defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\
  258. defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
  259. #define ARCHIVE_HAS_RMD160
  260. #endif
  261. #define archive_rmd160_init(ctx)\
  262. __archive_digest.rmd160init(ctx)
  263. #define archive_rmd160_final(ctx, md)\
  264. __archive_digest.rmd160final(ctx, md)
  265. #define archive_rmd160_update(ctx, buf, n)\
  266. __archive_digest.rmd160update(ctx, buf, n)
  267. #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\
  268. defined(ARCHIVE_CRYPTO_SHA1_LIBMD) || \
  269. defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\
  270. defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\
  271. defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\
  272. defined(ARCHIVE_CRYPTO_SHA1_WIN)
  273. #define ARCHIVE_HAS_SHA1
  274. #endif
  275. #define archive_sha1_init(ctx)\
  276. __archive_digest.sha1init(ctx)
  277. #define archive_sha1_final(ctx, md)\
  278. __archive_digest.sha1final(ctx, md)
  279. #define archive_sha1_update(ctx, buf, n)\
  280. __archive_digest.sha1update(ctx, buf, n)
  281. #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\
  282. defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\
  283. defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\
  284. defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\
  285. defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\
  286. defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\
  287. defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\
  288. defined(ARCHIVE_CRYPTO_SHA256_WIN)
  289. #define ARCHIVE_HAS_SHA256
  290. #endif
  291. #define archive_sha256_init(ctx)\
  292. __archive_digest.sha256init(ctx)
  293. #define archive_sha256_final(ctx, md)\
  294. __archive_digest.sha256final(ctx, md)
  295. #define archive_sha256_update(ctx, buf, n)\
  296. __archive_digest.sha256update(ctx, buf, n)
  297. #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\
  298. defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\
  299. defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\
  300. defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\
  301. defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\
  302. defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\
  303. defined(ARCHIVE_CRYPTO_SHA384_WIN)
  304. #define ARCHIVE_HAS_SHA384
  305. #endif
  306. #define archive_sha384_init(ctx)\
  307. __archive_digest.sha384init(ctx)
  308. #define archive_sha384_final(ctx, md)\
  309. __archive_digest.sha384final(ctx, md)
  310. #define archive_sha384_update(ctx, buf, n)\
  311. __archive_digest.sha384update(ctx, buf, n)
  312. #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\
  313. defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\
  314. defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\
  315. defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\
  316. defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\
  317. defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\
  318. defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\
  319. defined(ARCHIVE_CRYPTO_SHA512_WIN)
  320. #define ARCHIVE_HAS_SHA512
  321. #endif
  322. #define archive_sha512_init(ctx)\
  323. __archive_digest.sha512init(ctx)
  324. #define archive_sha512_final(ctx, md)\
  325. __archive_digest.sha512final(ctx, md)
  326. #define archive_sha512_update(ctx, buf, n)\
  327. __archive_digest.sha512update(ctx, buf, n)
  328. /* Minimal interface to digest functionality for internal use in libarchive */
  329. struct archive_digest
  330. {
  331. /* Message Digest */
  332. int (*md5init)(archive_md5_ctx *ctx);
  333. int (*md5update)(archive_md5_ctx *, const void *, size_t);
  334. int (*md5final)(archive_md5_ctx *, void *);
  335. int (*rmd160init)(archive_rmd160_ctx *);
  336. int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t);
  337. int (*rmd160final)(archive_rmd160_ctx *, void *);
  338. int (*sha1init)(archive_sha1_ctx *);
  339. int (*sha1update)(archive_sha1_ctx *, const void *, size_t);
  340. int (*sha1final)(archive_sha1_ctx *, void *);
  341. int (*sha256init)(archive_sha256_ctx *);
  342. int (*sha256update)(archive_sha256_ctx *, const void *, size_t);
  343. int (*sha256final)(archive_sha256_ctx *, void *);
  344. int (*sha384init)(archive_sha384_ctx *);
  345. int (*sha384update)(archive_sha384_ctx *, const void *, size_t);
  346. int (*sha384final)(archive_sha384_ctx *, void *);
  347. int (*sha512init)(archive_sha512_ctx *);
  348. int (*sha512update)(archive_sha512_ctx *, const void *, size_t);
  349. int (*sha512final)(archive_sha512_ctx *, void *);
  350. };
  351. extern const struct archive_digest __archive_digest;
  352. #endif