archive_cryptor_private.h revision 299425
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 __LIBARCHIVE_BUILD 27#error This header is only to be used internally to libarchive. 28#endif 29 30#ifndef ARCHIVE_CRYPTOR_PRIVATE_H_INCLUDED 31#define ARCHIVE_CRYPTOR_PRIVATE_H_INCLUDED 32 33/* 34 * On systems that do not support any recognized crypto libraries, 35 * the archive_cryptor.c file will normally define no usable symbols. 36 * 37 * But some compilers and linkers choke on empty object files, so 38 * define a public symbol that will always exist. This could 39 * be removed someday if this file gains another always-present 40 * symbol definition. 41 */ 42int __libarchive_cryptor_build_hack(void); 43 44#ifdef __APPLE__ 45# include <AvailabilityMacros.h> 46# if MAC_OS_X_VERSION_MAX_ALLOWED >= 1080 47# define ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto 48# endif 49#endif 50 51#ifdef ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto 52#include <CommonCrypto/CommonCryptor.h> 53#include <CommonCrypto/CommonKeyDerivation.h> 54#define AES_BLOCK_SIZE 16 55#define AES_MAX_KEY_SIZE kCCKeySizeAES256 56 57typedef struct { 58 CCCryptorRef ctx; 59 uint8_t key[AES_MAX_KEY_SIZE]; 60 unsigned key_len; 61 uint8_t nonce[AES_BLOCK_SIZE]; 62 uint8_t encr_buf[AES_BLOCK_SIZE]; 63 unsigned encr_pos; 64} archive_crypto_ctx; 65 66#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H) 67#include <Bcrypt.h> 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 76typedef 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_LIBNETTLE) && defined(HAVE_NETTLE_AES_H) 87#if defined(HAVE_NETTLE_PBKDF2_H) 88#include <nettle/pbkdf2.h> 89#endif 90#include <nettle/aes.h> 91 92typedef struct { 93 struct aes_ctx ctx; 94 uint8_t key[AES_MAX_KEY_SIZE]; 95 unsigned key_len; 96 uint8_t nonce[AES_BLOCK_SIZE]; 97 uint8_t encr_buf[AES_BLOCK_SIZE]; 98 unsigned encr_pos; 99} archive_crypto_ctx; 100 101#elif defined(HAVE_LIBCRYPTO) 102#include <openssl/evp.h> 103#define AES_BLOCK_SIZE 16 104#define AES_MAX_KEY_SIZE 32 105 106typedef struct { 107 EVP_CIPHER_CTX ctx; 108 const EVP_CIPHER *type; 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 116#else 117 118#define AES_BLOCK_SIZE 16 119#define AES_MAX_KEY_SIZE 32 120typedef int archive_crypto_ctx; 121 122#endif 123 124/* defines */ 125#define archive_pbkdf2_sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)\ 126 __archive_cryptor.pbkdf2sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len) 127 128#define archive_decrypto_aes_ctr_init(ctx, key, key_len) \ 129 __archive_cryptor.decrypto_aes_ctr_init(ctx, key, key_len) 130#define archive_decrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \ 131 __archive_cryptor.decrypto_aes_ctr_update(ctx, in, in_len, out, out_len) 132#define archive_decrypto_aes_ctr_release(ctx) \ 133 __archive_cryptor.decrypto_aes_ctr_release(ctx) 134 135#define archive_encrypto_aes_ctr_init(ctx, key, key_len) \ 136 __archive_cryptor.encrypto_aes_ctr_init(ctx, key, key_len) 137#define archive_encrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \ 138 __archive_cryptor.encrypto_aes_ctr_update(ctx, in, in_len, out, out_len) 139#define archive_encrypto_aes_ctr_release(ctx) \ 140 __archive_cryptor.encrypto_aes_ctr_release(ctx) 141 142/* Minimal interface to cryptographic functionality for internal use in 143 * libarchive */ 144struct archive_cryptor 145{ 146 /* PKCS5 PBKDF2 HMAC-SHA1 */ 147 int (*pbkdf2sha1)(const char *pw, size_t pw_len, const uint8_t *salt, 148 size_t salt_len, unsigned rounds, uint8_t *derived_key, 149 size_t derived_key_len); 150 /* AES CTR mode(little endian version) */ 151 int (*decrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t); 152 int (*decrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *, 153 size_t, uint8_t *, size_t *); 154 int (*decrypto_aes_ctr_release)(archive_crypto_ctx *); 155 int (*encrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t); 156 int (*encrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *, 157 size_t, uint8_t *, size_t *); 158 int (*encrypto_aes_ctr_release)(archive_crypto_ctx *); 159}; 160 161extern const struct archive_cryptor __archive_cryptor; 162 163#endif 164