archive_cryptor_private.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 __LIBARCHIVE_BUILD
  26. #error This header is only to be used internally to libarchive.
  27. #endif
  28. #ifndef ARCHIVE_CRYPTOR_PRIVATE_H_INCLUDED
  29. #define ARCHIVE_CRYPTOR_PRIVATE_H_INCLUDED
  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_LIBNETTLE) && defined(HAVE_NETTLE_AES_H)
  77. #if defined(HAVE_NETTLE_PBKDF2_H)
  78. #include <nettle/pbkdf2.h>
  79. #endif
  80. #include <nettle/aes.h>
  81. typedef struct {
  82. struct aes_ctx ctx;
  83. uint8_t key[AES_MAX_KEY_SIZE];
  84. unsigned key_len;
  85. uint8_t nonce[AES_BLOCK_SIZE];
  86. uint8_t encr_buf[AES_BLOCK_SIZE];
  87. unsigned encr_pos;
  88. } archive_crypto_ctx;
  89. #elif defined(HAVE_LIBCRYPTO)
  90. #include "archive_openssl_evp_private.h"
  91. #define AES_BLOCK_SIZE 16
  92. #define AES_MAX_KEY_SIZE 32
  93. typedef struct {
  94. EVP_CIPHER_CTX *ctx;
  95. const EVP_CIPHER *type;
  96. uint8_t key[AES_MAX_KEY_SIZE];
  97. unsigned key_len;
  98. uint8_t nonce[AES_BLOCK_SIZE];
  99. uint8_t encr_buf[AES_BLOCK_SIZE];
  100. unsigned encr_pos;
  101. } archive_crypto_ctx;
  102. #else
  103. #define AES_BLOCK_SIZE 16
  104. #define AES_MAX_KEY_SIZE 32
  105. typedef int archive_crypto_ctx;
  106. #endif
  107. /* defines */
  108. #define archive_pbkdf2_sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)\
  109. __archive_cryptor.pbkdf2sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)
  110. #define archive_decrypto_aes_ctr_init(ctx, key, key_len) \
  111. __archive_cryptor.decrypto_aes_ctr_init(ctx, key, key_len)
  112. #define archive_decrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \
  113. __archive_cryptor.decrypto_aes_ctr_update(ctx, in, in_len, out, out_len)
  114. #define archive_decrypto_aes_ctr_release(ctx) \
  115. __archive_cryptor.decrypto_aes_ctr_release(ctx)
  116. #define archive_encrypto_aes_ctr_init(ctx, key, key_len) \
  117. __archive_cryptor.encrypto_aes_ctr_init(ctx, key, key_len)
  118. #define archive_encrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \
  119. __archive_cryptor.encrypto_aes_ctr_update(ctx, in, in_len, out, out_len)
  120. #define archive_encrypto_aes_ctr_release(ctx) \
  121. __archive_cryptor.encrypto_aes_ctr_release(ctx)
  122. /* Minimal interface to cryptographic functionality for internal use in
  123. * libarchive */
  124. struct archive_cryptor
  125. {
  126. /* PKCS5 PBKDF2 HMAC-SHA1 */
  127. int (*pbkdf2sha1)(const char *pw, size_t pw_len, const uint8_t *salt,
  128. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  129. size_t derived_key_len);
  130. /* AES CTR mode(little endian version) */
  131. int (*decrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);
  132. int (*decrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,
  133. size_t, uint8_t *, size_t *);
  134. int (*decrypto_aes_ctr_release)(archive_crypto_ctx *);
  135. int (*encrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);
  136. int (*encrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,
  137. size_t, uint8_t *, size_t *);
  138. int (*encrypto_aes_ctr_release)(archive_crypto_ctx *);
  139. };
  140. extern const struct archive_cryptor __archive_cryptor;
  141. #endif