archive_cryptor_private.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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)
  60. #include <bcrypt.h>
  61. /* Common in other bcrypt implementations, but missing from VS2008. */
  62. #ifndef BCRYPT_SUCCESS
  63. #define BCRYPT_SUCCESS(r) ((NTSTATUS)(r) == STATUS_SUCCESS)
  64. #endif
  65. #define AES_MAX_KEY_SIZE 32
  66. #define AES_BLOCK_SIZE 16
  67. typedef struct {
  68. BCRYPT_ALG_HANDLE hAlg;
  69. BCRYPT_KEY_HANDLE hKey;
  70. PBYTE keyObj;
  71. DWORD keyObj_len;
  72. uint8_t nonce[AES_BLOCK_SIZE];
  73. uint8_t encr_buf[AES_BLOCK_SIZE];
  74. unsigned encr_pos;
  75. } archive_crypto_ctx;
  76. #elif defined(HAVE_LIBMBEDCRYPTO) && defined(HAVE_MBEDTLS_AES_H)
  77. #include <mbedtls/aes.h>
  78. #include <mbedtls/md.h>
  79. #include <mbedtls/pkcs5.h>
  80. #define AES_MAX_KEY_SIZE 32
  81. #define AES_BLOCK_SIZE 16
  82. typedef struct {
  83. mbedtls_aes_context ctx;
  84. uint8_t key[AES_MAX_KEY_SIZE];
  85. unsigned key_len;
  86. uint8_t nonce[AES_BLOCK_SIZE];
  87. uint8_t encr_buf[AES_BLOCK_SIZE];
  88. unsigned encr_pos;
  89. } archive_crypto_ctx;
  90. #elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_AES_H)
  91. #if defined(HAVE_NETTLE_PBKDF2_H)
  92. #include <nettle/pbkdf2.h>
  93. #endif
  94. #include <nettle/aes.h>
  95. typedef struct {
  96. struct aes_ctx ctx;
  97. uint8_t key[AES_MAX_KEY_SIZE];
  98. unsigned key_len;
  99. uint8_t nonce[AES_BLOCK_SIZE];
  100. uint8_t encr_buf[AES_BLOCK_SIZE];
  101. unsigned encr_pos;
  102. } archive_crypto_ctx;
  103. #elif defined(HAVE_LIBCRYPTO)
  104. #include "archive_openssl_evp_private.h"
  105. #define AES_BLOCK_SIZE 16
  106. #define AES_MAX_KEY_SIZE 32
  107. typedef struct {
  108. EVP_CIPHER_CTX *ctx;
  109. const EVP_CIPHER *type;
  110. uint8_t key[AES_MAX_KEY_SIZE];
  111. unsigned key_len;
  112. uint8_t nonce[AES_BLOCK_SIZE];
  113. uint8_t encr_buf[AES_BLOCK_SIZE];
  114. unsigned encr_pos;
  115. } archive_crypto_ctx;
  116. #else
  117. #define AES_BLOCK_SIZE 16
  118. #define AES_MAX_KEY_SIZE 32
  119. typedef int archive_crypto_ctx;
  120. #endif
  121. /* defines */
  122. #define archive_pbkdf2_sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)\
  123. __archive_cryptor.pbkdf2sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)
  124. #define archive_decrypto_aes_ctr_init(ctx, key, key_len) \
  125. __archive_cryptor.decrypto_aes_ctr_init(ctx, key, key_len)
  126. #define archive_decrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \
  127. __archive_cryptor.decrypto_aes_ctr_update(ctx, in, in_len, out, out_len)
  128. #define archive_decrypto_aes_ctr_release(ctx) \
  129. __archive_cryptor.decrypto_aes_ctr_release(ctx)
  130. #define archive_encrypto_aes_ctr_init(ctx, key, key_len) \
  131. __archive_cryptor.encrypto_aes_ctr_init(ctx, key, key_len)
  132. #define archive_encrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \
  133. __archive_cryptor.encrypto_aes_ctr_update(ctx, in, in_len, out, out_len)
  134. #define archive_encrypto_aes_ctr_release(ctx) \
  135. __archive_cryptor.encrypto_aes_ctr_release(ctx)
  136. /* Minimal interface to cryptographic functionality for internal use in
  137. * libarchive */
  138. struct archive_cryptor
  139. {
  140. /* PKCS5 PBKDF2 HMAC-SHA1 */
  141. int (*pbkdf2sha1)(const char *pw, size_t pw_len, const uint8_t *salt,
  142. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  143. size_t derived_key_len);
  144. /* AES CTR mode(little endian version) */
  145. int (*decrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);
  146. int (*decrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,
  147. size_t, uint8_t *, size_t *);
  148. int (*decrypto_aes_ctr_release)(archive_crypto_ctx *);
  149. int (*encrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);
  150. int (*encrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,
  151. size_t, uint8_t *, size_t *);
  152. int (*encrypto_aes_ctr_release)(archive_crypto_ctx *);
  153. };
  154. extern const struct archive_cryptor __archive_cryptor;
  155. #endif