Path: blob/master/Utilities/cmlibarchive/libarchive/archive_cryptor_private.h
3153 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_CommonCrypto46# 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];58unsigned key_len;59uint8_t nonce[AES_BLOCK_SIZE];60uint8_t encr_buf[AES_BLOCK_SIZE];61unsigned encr_pos;62} archive_crypto_ctx;6364#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA65#include <bcrypt.h>6667/* Common in other bcrypt implementations, but missing from VS2008. */68#ifndef BCRYPT_SUCCESS69#define BCRYPT_SUCCESS(r) ((NTSTATUS)(r) == STATUS_SUCCESS)70#endif7172#define AES_MAX_KEY_SIZE 3273#define AES_BLOCK_SIZE 1674typedef struct {75BCRYPT_ALG_HANDLE hAlg;76BCRYPT_KEY_HANDLE hKey;77PBYTE keyObj;78DWORD keyObj_len;79uint8_t nonce[AES_BLOCK_SIZE];80uint8_t encr_buf[AES_BLOCK_SIZE];81unsigned encr_pos;82} archive_crypto_ctx;8384#elif defined(HAVE_LIBMBEDCRYPTO) && defined(HAVE_MBEDTLS_AES_H)85#include <mbedtls/aes.h>86#include <mbedtls/md.h>87#include <mbedtls/pkcs5.h>8889#define AES_MAX_KEY_SIZE 3290#define AES_BLOCK_SIZE 169192typedef struct {93mbedtls_aes_context ctx;94uint8_t key[AES_MAX_KEY_SIZE];95unsigned key_len;96uint8_t nonce[AES_BLOCK_SIZE];97uint8_t encr_buf[AES_BLOCK_SIZE];98unsigned encr_pos;99} archive_crypto_ctx;100101#elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_AES_H)102#if defined(HAVE_NETTLE_PBKDF2_H)103#include <nettle/pbkdf2.h>104#endif105#include <nettle/aes.h>106#include <nettle/version.h>107108typedef struct {109#if NETTLE_VERSION_MAJOR < 3110struct aes_ctx ctx;111#else112union {113struct aes128_ctx c128;114struct aes192_ctx c192;115struct aes256_ctx c256;116} ctx;117#endif118uint8_t key[AES_MAX_KEY_SIZE];119unsigned key_len;120uint8_t nonce[AES_BLOCK_SIZE];121uint8_t encr_buf[AES_BLOCK_SIZE];122unsigned encr_pos;123} archive_crypto_ctx;124125#elif defined(HAVE_LIBCRYPTO)126#include "archive_openssl_evp_private.h"127#define AES_BLOCK_SIZE 16128#define AES_MAX_KEY_SIZE 32129130typedef struct {131EVP_CIPHER_CTX *ctx;132const EVP_CIPHER *type;133uint8_t key[AES_MAX_KEY_SIZE];134unsigned key_len;135uint8_t nonce[AES_BLOCK_SIZE];136uint8_t encr_buf[AES_BLOCK_SIZE];137unsigned encr_pos;138} archive_crypto_ctx;139140#else141142#define AES_BLOCK_SIZE 16143#define AES_MAX_KEY_SIZE 32144typedef int archive_crypto_ctx;145146#endif147148/* defines */149#define archive_pbkdf2_sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)\150__archive_cryptor.pbkdf2sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)151152#define archive_decrypto_aes_ctr_init(ctx, key, key_len) \153__archive_cryptor.decrypto_aes_ctr_init(ctx, key, key_len)154#define archive_decrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \155__archive_cryptor.decrypto_aes_ctr_update(ctx, in, in_len, out, out_len)156#define archive_decrypto_aes_ctr_release(ctx) \157__archive_cryptor.decrypto_aes_ctr_release(ctx)158159#define archive_encrypto_aes_ctr_init(ctx, key, key_len) \160__archive_cryptor.encrypto_aes_ctr_init(ctx, key, key_len)161#define archive_encrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \162__archive_cryptor.encrypto_aes_ctr_update(ctx, in, in_len, out, out_len)163#define archive_encrypto_aes_ctr_release(ctx) \164__archive_cryptor.encrypto_aes_ctr_release(ctx)165166/* Minimal interface to cryptographic functionality for internal use in167* libarchive */168struct archive_cryptor169{170/* PKCS5 PBKDF2 HMAC-SHA1 */171int (*pbkdf2sha1)(const char *pw, size_t pw_len, const uint8_t *salt,172size_t salt_len, unsigned rounds, uint8_t *derived_key,173size_t derived_key_len);174/* AES CTR mode(little endian version) */175int (*decrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);176int (*decrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,177size_t, uint8_t *, size_t *);178int (*decrypto_aes_ctr_release)(archive_crypto_ctx *);179int (*encrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);180int (*encrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,181size_t, uint8_t *, size_t *);182int (*encrypto_aes_ctr_release)(archive_crypto_ctx *);183};184185extern const struct archive_cryptor __archive_cryptor;186187#endif188189190