archive_cryptor_private.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef ARCHIVE_CRYPTOR_PRIVATE_H_INCLUDED
  26. #define ARCHIVE_CRYPTOR_PRIVATE_H_INCLUDED
  27. #ifndef __LIBARCHIVE_BUILD
  28. #error This header is only to be used internally to libarchive.
  29. #endif
  30. /*
  31. * On systems that do not support any recognized crypto libraries,
  32. * the archive_cryptor.c file will normally define no usable symbols.
  33. *
  34. * But some compilers and linkers choke on empty object files, so
  35. * define a public symbol that will always exist. This could
  36. * be removed someday if this file gains another always-present
  37. * symbol definition.
  38. */
  39. int __libarchive_cryptor_build_hack(void);
  40. #ifdef __APPLE__
  41. # include <AvailabilityMacros.h>
  42. # if MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
  43. # define ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto
  44. # endif
  45. #endif
  46. #ifdef ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto
  47. #include <CommonCrypto/CommonCryptor.h>
  48. #include <CommonCrypto/CommonKeyDerivation.h>
  49. #define AES_BLOCK_SIZE 16
  50. #define AES_MAX_KEY_SIZE kCCKeySizeAES256
  51. typedef struct {
  52. CCCryptorRef ctx;
  53. uint8_t key[AES_MAX_KEY_SIZE];
  54. unsigned key_len;
  55. uint8_t nonce[AES_BLOCK_SIZE];
  56. uint8_t encr_buf[AES_BLOCK_SIZE];
  57. unsigned encr_pos;
  58. } archive_crypto_ctx;
  59. #elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA
  60. #include <bcrypt.h>
  61. #define ARCHIVE_CRYPTOR_USE_CNG 1
  62. /* Common in other bcrypt implementations, but missing from VS2008. */
  63. #ifndef BCRYPT_SUCCESS
  64. #define BCRYPT_SUCCESS(r) ((NTSTATUS)(r) == STATUS_SUCCESS)
  65. #endif
  66. #define AES_MAX_KEY_SIZE 32
  67. #define AES_BLOCK_SIZE 16
  68. typedef struct {
  69. BCRYPT_ALG_HANDLE hAlg;
  70. BCRYPT_KEY_HANDLE hKey;
  71. PBYTE keyObj;
  72. DWORD keyObj_len;
  73. uint8_t nonce[AES_BLOCK_SIZE];
  74. uint8_t encr_buf[AES_BLOCK_SIZE];
  75. unsigned encr_pos;
  76. } archive_crypto_ctx;
  77. #elif defined(HAVE_LIBMBEDCRYPTO) && defined(HAVE_MBEDTLS_AES_H)
  78. #include <mbedtls/aes.h>
  79. #include <mbedtls/md.h>
  80. #include <mbedtls/pkcs5.h>
  81. #define ARCHIVE_CRYPTOR_USE_MBED 1
  82. #define AES_MAX_KEY_SIZE 32
  83. #define AES_BLOCK_SIZE 16
  84. typedef struct {
  85. mbedtls_aes_context ctx;
  86. uint8_t key[AES_MAX_KEY_SIZE];
  87. unsigned key_len;
  88. uint8_t nonce[AES_BLOCK_SIZE];
  89. uint8_t encr_buf[AES_BLOCK_SIZE];
  90. unsigned encr_pos;
  91. } archive_crypto_ctx;
  92. #elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_AES_H)
  93. #if defined(HAVE_NETTLE_PBKDF2_H)
  94. #include <nettle/pbkdf2.h>
  95. #endif
  96. #include <nettle/aes.h>
  97. #include <nettle/version.h>
  98. #define ARCHIVE_CRYPTOR_USE_NETTLE 1
  99. typedef struct {
  100. #if NETTLE_VERSION_MAJOR < 3
  101. struct aes_ctx ctx;
  102. #else
  103. union {
  104. struct aes128_ctx c128;
  105. struct aes192_ctx c192;
  106. struct aes256_ctx c256;
  107. } ctx;
  108. #endif
  109. uint8_t key[AES_MAX_KEY_SIZE];
  110. unsigned key_len;
  111. uint8_t nonce[AES_BLOCK_SIZE];
  112. uint8_t encr_buf[AES_BLOCK_SIZE];
  113. unsigned encr_pos;
  114. } archive_crypto_ctx;
  115. #elif defined(HAVE_LIBCRYPTO)
  116. #include "archive_openssl_evp_private.h"
  117. #define ARCHIVE_CRYPTOR_USE_OPENSSL 1
  118. #define AES_BLOCK_SIZE 16
  119. #define AES_MAX_KEY_SIZE 32
  120. typedef struct {
  121. EVP_CIPHER_CTX *ctx;
  122. const EVP_CIPHER *type;
  123. uint8_t key[AES_MAX_KEY_SIZE];
  124. unsigned key_len;
  125. uint8_t nonce[AES_BLOCK_SIZE];
  126. uint8_t encr_buf[AES_BLOCK_SIZE];
  127. unsigned encr_pos;
  128. } archive_crypto_ctx;
  129. #else
  130. #if defined(_WIN32) && !defined(__CYGWIN__) && !(defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA)
  131. #define ARCHIVE_CRYPTOR_USE_WINCRYPT 1
  132. #endif
  133. #define AES_BLOCK_SIZE 16
  134. #define AES_MAX_KEY_SIZE 32
  135. typedef int archive_crypto_ctx;
  136. #endif
  137. /* defines */
  138. #define archive_pbkdf2_sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)\
  139. __archive_cryptor.pbkdf2sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)
  140. #define archive_decrypto_aes_ctr_init(ctx, key, key_len) \
  141. __archive_cryptor.decrypto_aes_ctr_init(ctx, key, key_len)
  142. #define archive_decrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \
  143. __archive_cryptor.decrypto_aes_ctr_update(ctx, in, in_len, out, out_len)
  144. #define archive_decrypto_aes_ctr_release(ctx) \
  145. __archive_cryptor.decrypto_aes_ctr_release(ctx)
  146. #define archive_encrypto_aes_ctr_init(ctx, key, key_len) \
  147. __archive_cryptor.encrypto_aes_ctr_init(ctx, key, key_len)
  148. #define archive_encrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \
  149. __archive_cryptor.encrypto_aes_ctr_update(ctx, in, in_len, out, out_len)
  150. #define archive_encrypto_aes_ctr_release(ctx) \
  151. __archive_cryptor.encrypto_aes_ctr_release(ctx)
  152. /* Minimal interface to cryptographic functionality for internal use in
  153. * libarchive */
  154. struct archive_cryptor
  155. {
  156. /* PKCS5 PBKDF2 HMAC-SHA1 */
  157. int (*pbkdf2sha1)(const char *pw, size_t pw_len, const uint8_t *salt,
  158. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  159. size_t derived_key_len);
  160. /* AES CTR mode(little endian version) */
  161. int (*decrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);
  162. int (*decrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,
  163. size_t, uint8_t *, size_t *);
  164. int (*decrypto_aes_ctr_release)(archive_crypto_ctx *);
  165. int (*encrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);
  166. int (*encrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,
  167. size_t, uint8_t *, size_t *);
  168. int (*encrypto_aes_ctr_release)(archive_crypto_ctx *);
  169. };
  170. extern const struct archive_cryptor __archive_cryptor;
  171. #endif