archive_cryptor_private.h revision 358088
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 */
41int __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
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
56typedef struct {
57	CCCryptorRef	ctx;
58	uint8_t		key[AES_MAX_KEY_SIZE];
59	unsigned	key_len;
60	uint8_t		nonce[AES_BLOCK_SIZE];
61	uint8_t		encr_buf[AES_BLOCK_SIZE];
62	unsigned	encr_pos;
63} archive_crypto_ctx;
64
65#elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
66#include <bcrypt.h>
67
68/* Common in other bcrypt implementations, but missing from VS2008. */
69#ifndef BCRYPT_SUCCESS
70#define BCRYPT_SUCCESS(r) ((NTSTATUS)(r) == STATUS_SUCCESS)
71#endif
72
73#define AES_MAX_KEY_SIZE 32
74#define AES_BLOCK_SIZE 16
75typedef struct {
76	BCRYPT_ALG_HANDLE hAlg;
77	BCRYPT_KEY_HANDLE hKey;
78	PBYTE		keyObj;
79	DWORD		keyObj_len;
80	uint8_t		nonce[AES_BLOCK_SIZE];
81	uint8_t		encr_buf[AES_BLOCK_SIZE];
82	unsigned	encr_pos;
83} archive_crypto_ctx;
84
85#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
90#define AES_MAX_KEY_SIZE 32
91#define AES_BLOCK_SIZE 16
92
93typedef struct {
94	mbedtls_aes_context	ctx;
95	uint8_t		key[AES_MAX_KEY_SIZE];
96	unsigned	key_len;
97	uint8_t		nonce[AES_BLOCK_SIZE];
98	uint8_t		encr_buf[AES_BLOCK_SIZE];
99	unsigned	encr_pos;
100} archive_crypto_ctx;
101
102#elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_AES_H)
103#if defined(HAVE_NETTLE_PBKDF2_H)
104#include <nettle/pbkdf2.h>
105#endif
106#include <nettle/aes.h>
107
108typedef struct {
109	struct aes_ctx	ctx;
110	uint8_t		key[AES_MAX_KEY_SIZE];
111	unsigned	key_len;
112	uint8_t		nonce[AES_BLOCK_SIZE];
113	uint8_t		encr_buf[AES_BLOCK_SIZE];
114	unsigned	encr_pos;
115} archive_crypto_ctx;
116
117#elif defined(HAVE_LIBCRYPTO)
118#include "archive_openssl_evp_private.h"
119#define AES_BLOCK_SIZE	16
120#define AES_MAX_KEY_SIZE 32
121
122typedef struct {
123	EVP_CIPHER_CTX	*ctx;
124	const EVP_CIPHER *type;
125	uint8_t		key[AES_MAX_KEY_SIZE];
126	unsigned	key_len;
127	uint8_t		nonce[AES_BLOCK_SIZE];
128	uint8_t		encr_buf[AES_BLOCK_SIZE];
129	unsigned	encr_pos;
130} archive_crypto_ctx;
131
132#else
133
134#define AES_BLOCK_SIZE	16
135#define AES_MAX_KEY_SIZE 32
136typedef int archive_crypto_ctx;
137
138#endif
139
140/* defines */
141#define archive_pbkdf2_sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)\
142  __archive_cryptor.pbkdf2sha1(pw, pw_len, salt, salt_len, rounds, dk, dk_len)
143
144#define archive_decrypto_aes_ctr_init(ctx, key, key_len) \
145  __archive_cryptor.decrypto_aes_ctr_init(ctx, key, key_len)
146#define archive_decrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \
147  __archive_cryptor.decrypto_aes_ctr_update(ctx, in, in_len, out, out_len)
148#define archive_decrypto_aes_ctr_release(ctx) \
149  __archive_cryptor.decrypto_aes_ctr_release(ctx)
150
151#define archive_encrypto_aes_ctr_init(ctx, key, key_len) \
152  __archive_cryptor.encrypto_aes_ctr_init(ctx, key, key_len)
153#define archive_encrypto_aes_ctr_update(ctx, in, in_len, out, out_len) \
154  __archive_cryptor.encrypto_aes_ctr_update(ctx, in, in_len, out, out_len)
155#define archive_encrypto_aes_ctr_release(ctx) \
156  __archive_cryptor.encrypto_aes_ctr_release(ctx)
157
158/* Minimal interface to cryptographic functionality for internal use in
159 * libarchive */
160struct archive_cryptor
161{
162  /* PKCS5 PBKDF2 HMAC-SHA1 */
163  int (*pbkdf2sha1)(const char *pw, size_t pw_len, const uint8_t *salt,
164    size_t salt_len, unsigned rounds, uint8_t *derived_key,
165    size_t derived_key_len);
166  /* AES CTR mode(little endian version) */
167  int (*decrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);
168  int (*decrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,
169    size_t, uint8_t *, size_t *);
170  int (*decrypto_aes_ctr_release)(archive_crypto_ctx *);
171  int (*encrypto_aes_ctr_init)(archive_crypto_ctx *, const uint8_t *, size_t);
172  int (*encrypto_aes_ctr_update)(archive_crypto_ctx *, const uint8_t *,
173    size_t, uint8_t *, size_t *);
174  int (*encrypto_aes_ctr_release)(archive_crypto_ctx *);
175};
176
177extern const struct archive_cryptor __archive_cryptor;
178
179#endif
180