2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "cipher_aes_xts.h"
11 #include "internal/provider_algs.h"
12 #include "internal/providercommonerr.h"
14 /* TODO (3.0) Figure out what flags need to be set */
15 #define AES_XTS_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV \
16 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
17 | EVP_CIPH_CUSTOM_COPY)
19 #define AES_XTS_IV_BITS 128
20 #define AES_XTS_BLOCK_BITS 8
23 static const int allow_insecure_decrypt = 0;
25 static const int allow_insecure_decrypt = 1;
26 #endif /* FIPS_MODE */
28 /* forward declarations */
29 static OSSL_OP_cipher_encrypt_init_fn aes_xts_einit;
30 static OSSL_OP_cipher_decrypt_init_fn aes_xts_dinit;
31 static OSSL_OP_cipher_update_fn aes_xts_stream_update;
32 static OSSL_OP_cipher_final_fn aes_xts_stream_final;
33 static OSSL_OP_cipher_cipher_fn aes_xts_cipher;
34 static OSSL_OP_cipher_freectx_fn aes_xts_freectx;
35 static OSSL_OP_cipher_dupctx_fn aes_xts_dupctx;
36 static OSSL_OP_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
37 static OSSL_OP_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params;
40 * Verify that the two keys are different.
42 * This addresses the vulnerability described in Rogaway's
43 * September 2004 paper:
45 * "Efficient Instantiations of Tweakable Blockciphers and
46 * Refinements to Modes OCB and PMAC".
47 * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
49 * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
51 * "The check for Key_1 != Key_2 shall be done at any place
52 * BEFORE using the keys in the XTS-AES algorithm to process
55 static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
58 if ((!allow_insecure_decrypt || enc)
59 && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
60 ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
67 * Provider dispatch functions
69 static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
70 const unsigned char *iv, size_t ivlen, int enc)
72 PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
73 PROV_CIPHER_CTX *ctx = &xctx->base;
78 if (ivlen != ctx->ivlen) {
79 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
82 memcpy(ctx->iv, iv, ivlen);
86 if (keylen != ctx->keylen) {
87 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
90 if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
92 return ctx->hw->init(ctx, key, keylen);
97 static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
98 const unsigned char *iv, size_t ivlen)
100 return aes_xts_init(vctx, key, keylen, iv, ivlen, 1);
103 static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
104 const unsigned char *iv, size_t ivlen)
106 return aes_xts_init(vctx, key, keylen, iv, ivlen, 0);
109 static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
110 size_t kbits, size_t blkbits, size_t ivbits)
112 PROV_AES_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
115 cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode, flags,
116 PROV_CIPHER_HW_aes_xts(kbits), NULL);
121 static void aes_xts_freectx(void *vctx)
123 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
125 OPENSSL_clear_free(ctx, sizeof(*ctx));
128 static void *aes_xts_dupctx(void *vctx)
130 PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
131 PROV_AES_XTS_CTX *ret = NULL;
133 if (in->xts.key1 != NULL) {
134 if (in->xts.key1 != &in->ks1)
137 if (in->xts.key2 != NULL) {
138 if (in->xts.key2 != &in->ks2)
141 ret = OPENSSL_malloc(sizeof(*ret));
143 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
150 static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
151 size_t outsize, const unsigned char *in, size_t inl)
153 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
155 if (ctx->xts.key1 == NULL
156 || ctx->xts.key2 == NULL
160 || inl < AES_BLOCK_SIZE)
164 * Impose a limit of 2^20 blocks per data unit as specifed by
165 * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
166 * indicated that this was a SHOULD NOT rather than a MUST NOT.
167 * NIST SP 800-38E mandates the same limit.
169 if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
170 ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
174 if (ctx->stream != NULL)
175 (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
176 else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
184 static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
185 size_t outsize, const unsigned char *in,
188 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
191 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
195 if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
196 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
203 static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
210 static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
211 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
215 static const OSSL_PARAM *aes_xts_settable_ctx_params(void)
217 return aes_xts_known_settable_ctx_params;
220 static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
222 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
226 * TODO(3.0) We need a general solution for handling missing parameters
227 * inside set_params and get_params methods.
229 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
233 if (!OSSL_PARAM_get_size_t(p, &keylen)) {
234 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
237 /* The key length can not be modified for xts mode */
238 if (keylen != ctx->keylen)
245 #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
246 static OSSL_OP_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
247 static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
249 return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
250 flags, 2 * kbits, AES_XTS_BLOCK_BITS, \
253 static OSSL_OP_cipher_newctx_fn aes_##kbits##_xts_newctx; \
254 static void *aes_##kbits##_xts_newctx(void *provctx) \
256 return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
257 AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \
259 const OSSL_DISPATCH aes##kbits##xts_functions[] = { \
260 { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \
261 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \
262 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \
263 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \
264 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \
265 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \
266 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \
267 { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \
268 { OSSL_FUNC_CIPHER_GET_PARAMS, \
269 (void (*)(void))aes_##kbits##_##lcmode##_get_params }, \
270 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
271 (void (*)(void))cipher_generic_gettable_params }, \
272 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
273 (void (*)(void))cipher_generic_get_ctx_params }, \
274 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
275 (void (*)(void))cipher_generic_gettable_ctx_params }, \
276 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
277 (void (*)(void))aes_xts_set_ctx_params }, \
278 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
279 (void (*)(void))aes_xts_settable_ctx_params }, \
283 IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
284 IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);