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