3 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 * AES low level APIs are deprecated for public use, but still ok for internal
13 * use where we're using them to implement the higher level EVP interface, as is
16 #include "internal/deprecated.h"
18 #include "cipher_aes_xts.h"
19 #include "prov/implementations.h"
20 #include "prov/providercommon.h"
21 #include "prov/providercommonerr.h"
23 /* TODO (3.0) Figure out what flags need to be set */
24 #define AES_XTS_FLAGS (EVP_CIPH_CUSTOM_IV \
25 | EVP_CIPH_ALWAYS_CALL_INIT \
26 | EVP_CIPH_CTRL_INIT \
27 | EVP_CIPH_CUSTOM_COPY)
29 #define AES_XTS_IV_BITS 128
30 #define AES_XTS_BLOCK_BITS 8
32 /* forward declarations */
33 static OSSL_FUNC_cipher_encrypt_init_fn aes_xts_einit;
34 static OSSL_FUNC_cipher_decrypt_init_fn aes_xts_dinit;
35 static OSSL_FUNC_cipher_update_fn aes_xts_stream_update;
36 static OSSL_FUNC_cipher_final_fn aes_xts_stream_final;
37 static OSSL_FUNC_cipher_cipher_fn aes_xts_cipher;
38 static OSSL_FUNC_cipher_freectx_fn aes_xts_freectx;
39 static OSSL_FUNC_cipher_dupctx_fn aes_xts_dupctx;
40 static OSSL_FUNC_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
41 static OSSL_FUNC_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params;
44 * Verify that the two keys are different.
46 * This addresses the vulnerability described in Rogaway's
47 * September 2004 paper:
49 * "Efficient Instantiations of Tweakable Blockciphers and
50 * Refinements to Modes OCB and PMAC".
51 * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
53 * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
55 * "The check for Key_1 != Key_2 shall be done at any place
56 * BEFORE using the keys in the XTS-AES algorithm to process
59 static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
62 if ((!allow_insecure_decrypt || enc)
63 && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
64 ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
71 * Provider dispatch functions
73 static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
74 const unsigned char *iv, size_t ivlen, int enc)
76 PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
77 PROV_CIPHER_CTX *ctx = &xctx->base;
79 if (!ossl_prov_is_running())
85 if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
89 if (keylen != ctx->keylen) {
90 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
93 if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
95 return ctx->hw->init(ctx, key, keylen);
100 static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
101 const unsigned char *iv, size_t ivlen)
103 return aes_xts_init(vctx, key, keylen, iv, ivlen, 1);
106 static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
107 const unsigned char *iv, size_t ivlen)
109 return aes_xts_init(vctx, key, keylen, iv, ivlen, 0);
112 static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
113 size_t kbits, size_t blkbits, size_t ivbits)
115 PROV_AES_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
118 ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
119 flags, ossl_prov_cipher_hw_aes_xts(kbits),
125 static void aes_xts_freectx(void *vctx)
127 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
129 ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
130 OPENSSL_clear_free(ctx, sizeof(*ctx));
133 static void *aes_xts_dupctx(void *vctx)
135 PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
136 PROV_AES_XTS_CTX *ret = NULL;
138 if (!ossl_prov_is_running())
141 if (in->xts.key1 != NULL) {
142 if (in->xts.key1 != &in->ks1)
145 if (in->xts.key2 != NULL) {
146 if (in->xts.key2 != &in->ks2)
149 ret = OPENSSL_malloc(sizeof(*ret));
151 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
154 in->base.hw->copyctx(&ret->base, &in->base);
158 static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
159 size_t outsize, const unsigned char *in, size_t inl)
161 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
163 if (!ossl_prov_is_running()
164 || ctx->xts.key1 == NULL
165 || ctx->xts.key2 == NULL
169 || inl < AES_BLOCK_SIZE)
173 * Impose a limit of 2^20 blocks per data unit as specified by
174 * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
175 * indicated that this was a SHOULD NOT rather than a MUST NOT.
176 * NIST SP 800-38E mandates the same limit.
178 if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
179 ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
183 if (ctx->stream != NULL)
184 (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
185 else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
193 static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
194 size_t outsize, const unsigned char *in,
197 PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
200 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
204 if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
205 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
212 static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
215 if (!ossl_prov_is_running())
221 static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
222 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
226 static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *provctx)
228 return aes_xts_known_settable_ctx_params;
231 static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
233 PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
237 * TODO(3.0) We need a general solution for handling missing parameters
238 * inside set_params and get_params methods.
240 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
244 if (!OSSL_PARAM_get_size_t(p, &keylen)) {
245 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
248 /* The key length can not be modified for xts mode */
249 if (keylen != ctx->keylen)
256 #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
257 static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
258 static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
260 return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
261 flags, 2 * kbits, AES_XTS_BLOCK_BITS, \
264 static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx; \
265 static void *aes_##kbits##_xts_newctx(void *provctx) \
267 return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
268 AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \
270 const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = { \
271 { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \
272 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \
273 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \
274 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \
275 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \
276 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \
277 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \
278 { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \
279 { OSSL_FUNC_CIPHER_GET_PARAMS, \
280 (void (*)(void))aes_##kbits##_##lcmode##_get_params }, \
281 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
282 (void (*)(void))ossl_cipher_generic_gettable_params }, \
283 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
284 (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
285 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
286 (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
287 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
288 (void (*)(void))aes_xts_set_ctx_params }, \
289 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
290 (void (*)(void))aes_xts_settable_ctx_params }, \
294 IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
295 IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);