Path: blob/master/Utilities/cmlibarchive/libarchive/archive_cryptor_private.h
5041 views
/*-1* Copyright (c) 2014 Michihiro NAKAJIMA2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425#ifndef ARCHIVE_CRYPTOR_PRIVATE_H_INCLUDED26#define ARCHIVE_CRYPTOR_PRIVATE_H_INCLUDED2728#ifndef __LIBARCHIVE_BUILD29#error This header is only to be used internally to libarchive.30#endif31/*32* On systems that do not support any recognized crypto libraries,33* the archive_cryptor.c file will normally define no usable symbols.34*35* But some compilers and linkers choke on empty object files, so36* define a public symbol that will always exist. This could37* be removed someday if this file gains another always-present38* symbol definition.39*/40int __libarchive_cryptor_build_hack(void);4142#ifdef __APPLE__43# include <AvailabilityMacros.h>44# if MAC_OS_X_VERSION_MAX_ALLOWED >= 108045# define ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto 146# endif47#endif4849#ifdef ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto50#include <CommonCrypto/CommonCryptor.h>51#include <CommonCrypto/CommonKeyDerivation.h>52#define AES_BLOCK_SIZE 1653#define AES_MAX_KEY_SIZE kCCKeySizeAES2565455typedef struct {56CCCryptorRef ctx;57uint8_t key[AES_MAX_KEY_SIZE];58size_t key_len;59uint8_t nonce[AES_BLOCK_SIZE];60uint8_t encr_buf[AES_BLOCK_SIZE];61size_t encr_pos;62} archive_crypto_ctx;6364#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA65#include <bcrypt.h>66#define ARCHIVE_CRYPTOR_USE_CNG 16768/* Common in other bcrypt implementations, but missing from VS2008. */69#ifndef BCRYPT_SUCCESS70#define BCRYPT_SUCCESS(r) ((NTSTATUS)(r) == STATUS_SUCCESS)71#endif7273#define AES_MAX_KEY_SIZE 3274#define AES_BLOCK_SIZE 1675typedef struct {76BCRYPT_ALG_HANDLE hAlg;77BCRYPT_KEY_HANDLE hKey;78PBYTE keyObj;79DWORD keyObj_len;80uint8_t nonce[AES_BLOCK_SIZE];81uint8_t encr_buf[AES_BLOCK_SIZE];82unsigned encr_pos;83} archive_crypto_ctx;8485#elif defined(HAVE_LIBMBEDCRYPTO) && defined(HAVE_MBEDTLS_AES_H)86#include <mbedtls/aes.h>87#include <mbedtls/md.h>88#include <mbedtls/pkcs5.h>89#define ARCHIVE_CRYPTOR_USE_MBED 19091#define AES_MAX_KEY_SIZE 3292#define AES_BLOCK_SIZE 169394typedef struct {95mbedtls_aes_context ctx;96uint8_t key[AES_MAX_KEY_SIZE];97unsigned key_len;98uint8_t nonce[AES_BLOCK_SIZE];99uint8_t encr_buf[AES_BLOCK_SIZE];100unsigned encr_pos;101} archive_crypto_ctx;102103#elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_AES_H)104#if defined(HAVE_NETTLE_PBKDF2_H)105#include <nettle/pbkdf2.h>106#endif107#include <nettle/aes.h>108#include <nettle/version.h>109#define ARCHIVE_CRYPTOR_USE_NETTLE 1110111typedef struct {112#if NETTLE_VERSION_MAJOR < 3113struct aes_ctx ctx;114#else115union {116struct aes128_ctx c128;117struct aes192_ctx c192;118struct aes256_ctx c256;119} ctx;120#endif121uint8_t key[AES_MAX_KEY_SIZE];122unsigned key_len;123uint8_t nonce[AES_BLOCK_SIZE];124uint8_t encr_buf[AES_BLOCK_SIZE];125unsigned encr_pos;126} archive_crypto_ctx;127128#elif defined(HAVE_LIBCRYPTO)129#include "archive_openssl_evp_private.h"130#define ARCHIVE_CRYPTOR_USE_OPENSSL 1131#define AES_BLOCK_SIZE 16132#define AES_MAX_KEY_SIZE 32133134typedef struct {135EVP_CIPHER_CTX *ctx;136const EVP_CIPHER *type;137uint8_t key[AES_MAX_KEY_SIZE];138unsigned key_len;139uint8_t nonce[AES_BLOCK_SIZE];140uint8_t encr_buf[AES_BLOCK_SIZE];141unsigned encr_pos;142} archive_crypto_ctx;143144#else145146#if defined(ARCHIVE_CRYPTO_MD5_WIN) ||\147defined(ARCHIVE_CRYPTO_SHA1_WIN) ||\148defined(ARCHIVE_CRYPTO_SHA256_WIN) ||\149defined(ARCHIVE_CRYPTO_SHA384_WIN) ||\150defined(ARCHIVE_CRYPTO_SHA512_WIN)151#if defined(_WIN32) && !defined(__CYGWIN__) && !(defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA)152#define ARCHIVE_CRYPTOR_USE_WINCRYPT 1153#endif154#endif155156#define AES_BLOCK_SIZE 16157#define AES_MAX_KEY_SIZE 32158typedef int archive_crypto_ctx;159160#endif161162/* defines */163#define archive_pbkdf2_sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)\164__archive_cryptor.pbkdf2sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)165166#define archive_decrypto_aes_ctr_init(ctx, key, key_len) \167__archive_cryptor.decrypto_aes_ctr_init(ctx, key, key_len)168#define archive_decrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \169__archive_cryptor.decrypto_aes_ctr_update(ctx, in, in_len, out, out_len)170#define archive_decrypto_aes_ctr_release(ctx) \171__archive_cryptor.decrypto_aes_ctr_release(ctx)172173#define archive_encrypto_aes_ctr_init(ctx, key, key_len) \174__archive_cryptor.encrypto_aes_ctr_init(ctx, key, key_len)175#define archive_encrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \176__archive_cryptor.encrypto_aes_ctr_update(ctx, in, in_len, out, out_len)177#define archive_encrypto_aes_ctr_release(ctx) \178__archive_cryptor.encrypto_aes_ctr_release(ctx)179180/* Stub return value if no encryption support exists. */181#define CRYPTOR_STUB_FUNCTION -2182183/* Minimal interface to cryptographic functionality for internal use in184* libarchive */185struct archive_cryptor186{187/* PKCS5 PBKDF2 HMAC-SHA1 */188int (*pbkdf2sha1)(const char *pw, size_t pw_len, const uint8_t *salt,189size_t salt_len, unsigned rounds, uint8_t *derived_key,190size_t derived_key_len);191/* AES CTR mode(little endian version) */192int (*decrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);193int (*decrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,194size_t, uint8_t *, size_t *);195int (*decrypto_aes_ctr_release)(archive_crypto_ctx *);196int (*encrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);197int (*encrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,198size_t, uint8_t *, size_t *);199int (*encrypto_aes_ctr_release)(archive_crypto_ctx *);200};201202extern const struct archive_cryptor __archive_cryptor;203204#endif205206207