archive_crypto_private.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <openssl/evp.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 <wincrypt.h>
  137. typedef struct {
  138. int valid;
  139. HCRYPTPROV cryptProv;
  140. HCRYPTHASH hash;
  141. } Digest_CTX;
  142. #endif
  143. /* typedefs */
  144. #if defined(ARCHIVE_CRYPTO_MD5_LIBC)
  145. typedef MD5_CTX archive_md5_ctx;
  146. #elif defined(ARCHIVE_CRYPTO_MD5_LIBMD)
  147. typedef MD5_CTX archive_md5_ctx;
  148. #elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM)
  149. typedef CC_MD5_CTX archive_md5_ctx;
  150. #elif defined(ARCHIVE_CRYPTO_MD5_NETTLE)
  151. typedef struct md5_ctx archive_md5_ctx;
  152. #elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL)
  153. typedef EVP_MD_CTX archive_md5_ctx;
  154. #elif defined(ARCHIVE_CRYPTO_MD5_WIN)
  155. typedef Digest_CTX archive_md5_ctx;
  156. #else
  157. typedef unsigned char archive_md5_ctx;
  158. #endif
  159. #if defined(ARCHIVE_CRYPTO_RMD160_LIBC)
  160. typedef RMD160_CTX archive_rmd160_ctx;
  161. #elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD)
  162. typedef RIPEMD160_CTX archive_rmd160_ctx;
  163. #elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE)
  164. typedef struct ripemd160_ctx archive_rmd160_ctx;
  165. #elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
  166. typedef EVP_MD_CTX archive_rmd160_ctx;
  167. #else
  168. typedef unsigned char archive_rmd160_ctx;
  169. #endif
  170. #if defined(ARCHIVE_CRYPTO_SHA1_LIBC)
  171. typedef SHA1_CTX archive_sha1_ctx;
  172. #elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD)
  173. typedef SHA1_CTX archive_sha1_ctx;
  174. #elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM)
  175. typedef CC_SHA1_CTX archive_sha1_ctx;
  176. #elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE)
  177. typedef struct sha1_ctx archive_sha1_ctx;
  178. #elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL)
  179. typedef EVP_MD_CTX archive_sha1_ctx;
  180. #elif defined(ARCHIVE_CRYPTO_SHA1_WIN)
  181. typedef Digest_CTX archive_sha1_ctx;
  182. #else
  183. typedef unsigned char archive_sha1_ctx;
  184. #endif
  185. #if defined(ARCHIVE_CRYPTO_SHA256_LIBC)
  186. typedef SHA256_CTX archive_sha256_ctx;
  187. #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2)
  188. typedef SHA256_CTX archive_sha256_ctx;
  189. #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3)
  190. typedef SHA2_CTX archive_sha256_ctx;
  191. #elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD)
  192. typedef SHA256_CTX archive_sha256_ctx;
  193. #elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM)
  194. typedef CC_SHA256_CTX archive_sha256_ctx;
  195. #elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE)
  196. typedef struct sha256_ctx archive_sha256_ctx;
  197. #elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL)
  198. typedef EVP_MD_CTX archive_sha256_ctx;
  199. #elif defined(ARCHIVE_CRYPTO_SHA256_WIN)
  200. typedef Digest_CTX archive_sha256_ctx;
  201. #else
  202. typedef unsigned char archive_sha256_ctx;
  203. #endif
  204. #if defined(ARCHIVE_CRYPTO_SHA384_LIBC)
  205. typedef SHA384_CTX archive_sha384_ctx;
  206. #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2)
  207. typedef SHA384_CTX archive_sha384_ctx;
  208. #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3)
  209. typedef SHA2_CTX archive_sha384_ctx;
  210. #elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM)
  211. typedef CC_SHA512_CTX archive_sha384_ctx;
  212. #elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE)
  213. typedef struct sha384_ctx archive_sha384_ctx;
  214. #elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL)
  215. typedef EVP_MD_CTX archive_sha384_ctx;
  216. #elif defined(ARCHIVE_CRYPTO_SHA384_WIN)
  217. typedef Digest_CTX archive_sha384_ctx;
  218. #else
  219. typedef unsigned char archive_sha384_ctx;
  220. #endif
  221. #if defined(ARCHIVE_CRYPTO_SHA512_LIBC)
  222. typedef SHA512_CTX archive_sha512_ctx;
  223. #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2)
  224. typedef SHA512_CTX archive_sha512_ctx;
  225. #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3)
  226. typedef SHA2_CTX archive_sha512_ctx;
  227. #elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD)
  228. typedef SHA512_CTX archive_sha512_ctx;
  229. #elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)
  230. typedef CC_SHA512_CTX archive_sha512_ctx;
  231. #elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE)
  232. typedef struct sha512_ctx archive_sha512_ctx;
  233. #elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)
  234. typedef EVP_MD_CTX archive_sha512_ctx;
  235. #elif defined(ARCHIVE_CRYPTO_SHA512_WIN)
  236. typedef Digest_CTX archive_sha512_ctx;
  237. #else
  238. typedef unsigned char archive_sha512_ctx;
  239. #endif
  240. /* defines */
  241. #if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\
  242. defined(ARCHIVE_CRYPTO_MD5_LIBMD) || \
  243. defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\
  244. defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\
  245. defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\
  246. defined(ARCHIVE_CRYPTO_MD5_WIN)
  247. #define ARCHIVE_HAS_MD5
  248. #endif
  249. #define archive_md5_init(ctx)\
  250. __archive_crypto.md5init(ctx)
  251. #define archive_md5_final(ctx, md)\
  252. __archive_crypto.md5final(ctx, md)
  253. #define archive_md5_update(ctx, buf, n)\
  254. __archive_crypto.md5update(ctx, buf, n)
  255. #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\
  256. defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\
  257. defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)
  258. #define ARCHIVE_HAS_RMD160
  259. #endif
  260. #define archive_rmd160_init(ctx)\
  261. __archive_crypto.rmd160init(ctx)
  262. #define archive_rmd160_final(ctx, md)\
  263. __archive_crypto.rmd160final(ctx, md)
  264. #define archive_rmd160_update(ctx, buf, n)\
  265. __archive_crypto.rmd160update(ctx, buf, n)
  266. #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\
  267. defined(ARCHIVE_CRYPTO_SHA1_LIBMD) || \
  268. defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\
  269. defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\
  270. defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\
  271. defined(ARCHIVE_CRYPTO_SHA1_WIN)
  272. #define ARCHIVE_HAS_SHA1
  273. #endif
  274. #define archive_sha1_init(ctx)\
  275. __archive_crypto.sha1init(ctx)
  276. #define archive_sha1_final(ctx, md)\
  277. __archive_crypto.sha1final(ctx, md)
  278. #define archive_sha1_update(ctx, buf, n)\
  279. __archive_crypto.sha1update(ctx, buf, n)
  280. #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\
  281. defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\
  282. defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\
  283. defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\
  284. defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\
  285. defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\
  286. defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\
  287. defined(ARCHIVE_CRYPTO_SHA256_WIN)
  288. #define ARCHIVE_HAS_SHA256
  289. #endif
  290. #define archive_sha256_init(ctx)\
  291. __archive_crypto.sha256init(ctx)
  292. #define archive_sha256_final(ctx, md)\
  293. __archive_crypto.sha256final(ctx, md)
  294. #define archive_sha256_update(ctx, buf, n)\
  295. __archive_crypto.sha256update(ctx, buf, n)
  296. #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\
  297. defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\
  298. defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\
  299. defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\
  300. defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\
  301. defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\
  302. defined(ARCHIVE_CRYPTO_SHA384_WIN)
  303. #define ARCHIVE_HAS_SHA384
  304. #endif
  305. #define archive_sha384_init(ctx)\
  306. __archive_crypto.sha384init(ctx)
  307. #define archive_sha384_final(ctx, md)\
  308. __archive_crypto.sha384final(ctx, md)
  309. #define archive_sha384_update(ctx, buf, n)\
  310. __archive_crypto.sha384update(ctx, buf, n)
  311. #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\
  312. defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\
  313. defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\
  314. defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\
  315. defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\
  316. defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\
  317. defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\
  318. defined(ARCHIVE_CRYPTO_SHA512_WIN)
  319. #define ARCHIVE_HAS_SHA512
  320. #endif
  321. #define archive_sha512_init(ctx)\
  322. __archive_crypto.sha512init(ctx)
  323. #define archive_sha512_final(ctx, md)\
  324. __archive_crypto.sha512final(ctx, md)
  325. #define archive_sha512_update(ctx, buf, n)\
  326. __archive_crypto.sha512update(ctx, buf, n)
  327. /* Minimal interface to crypto functionality for internal use in libarchive */
  328. struct archive_crypto
  329. {
  330. /* Message Digest */
  331. int (*md5init)(archive_md5_ctx *ctx);
  332. int (*md5update)(archive_md5_ctx *, const void *, size_t);
  333. int (*md5final)(archive_md5_ctx *, void *);
  334. int (*rmd160init)(archive_rmd160_ctx *);
  335. int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t);
  336. int (*rmd160final)(archive_rmd160_ctx *, void *);
  337. int (*sha1init)(archive_sha1_ctx *);
  338. int (*sha1update)(archive_sha1_ctx *, const void *, size_t);
  339. int (*sha1final)(archive_sha1_ctx *, void *);
  340. int (*sha256init)(archive_sha256_ctx *);
  341. int (*sha256update)(archive_sha256_ctx *, const void *, size_t);
  342. int (*sha256final)(archive_sha256_ctx *, void *);
  343. int (*sha384init)(archive_sha384_ctx *);
  344. int (*sha384update)(archive_sha384_ctx *, const void *, size_t);
  345. int (*sha384final)(archive_sha384_ctx *, void *);
  346. int (*sha512init)(archive_sha512_ctx *);
  347. int (*sha512update)(archive_sha512_ctx *, const void *, size_t);
  348. int (*sha512final)(archive_sha512_ctx *, void *);
  349. };
  350. extern const struct archive_crypto __archive_crypto;
  351. #endif