2019-09-14 07:27:49 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2019-09-14 07:27:49 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2019-12-06 01:09:49 +08:00
|
|
|
/*
|
|
|
|
* AES low level APIs are deprecated for public use, but still ok for internal
|
|
|
|
* use where we're using them to implement the higher level EVP interface, as is
|
|
|
|
* the case here.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2019-09-14 07:27:49 +08:00
|
|
|
#include "cipher_aes_xts.h"
|
2019-10-04 21:20:48 +08:00
|
|
|
#include "prov/implementations.h"
|
2020-09-08 10:56:34 +08:00
|
|
|
#include "prov/providercommon.h"
|
2019-10-04 21:25:59 +08:00
|
|
|
#include "prov/providercommonerr.h"
|
2019-09-14 07:27:49 +08:00
|
|
|
|
|
|
|
/* TODO (3.0) Figure out what flags need to be set */
|
2019-09-25 15:07:20 +08:00
|
|
|
#define AES_XTS_FLAGS (EVP_CIPH_CUSTOM_IV \
|
|
|
|
| EVP_CIPH_ALWAYS_CALL_INIT \
|
|
|
|
| EVP_CIPH_CTRL_INIT \
|
2019-09-14 07:27:49 +08:00
|
|
|
| EVP_CIPH_CUSTOM_COPY)
|
|
|
|
|
|
|
|
#define AES_XTS_IV_BITS 128
|
|
|
|
#define AES_XTS_BLOCK_BITS 8
|
|
|
|
|
|
|
|
/* forward declarations */
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_cipher_encrypt_init_fn aes_xts_einit;
|
|
|
|
static OSSL_FUNC_cipher_decrypt_init_fn aes_xts_dinit;
|
|
|
|
static OSSL_FUNC_cipher_update_fn aes_xts_stream_update;
|
|
|
|
static OSSL_FUNC_cipher_final_fn aes_xts_stream_final;
|
|
|
|
static OSSL_FUNC_cipher_cipher_fn aes_xts_cipher;
|
|
|
|
static OSSL_FUNC_cipher_freectx_fn aes_xts_freectx;
|
|
|
|
static OSSL_FUNC_cipher_dupctx_fn aes_xts_dupctx;
|
|
|
|
static OSSL_FUNC_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
|
|
|
|
static OSSL_FUNC_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params;
|
2019-09-14 07:27:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that the two keys are different.
|
|
|
|
*
|
|
|
|
* This addresses the vulnerability described in Rogaway's
|
|
|
|
* September 2004 paper:
|
|
|
|
*
|
|
|
|
* "Efficient Instantiations of Tweakable Blockciphers and
|
|
|
|
* Refinements to Modes OCB and PMAC".
|
|
|
|
* (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
|
|
|
|
*
|
|
|
|
* FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
|
|
|
|
* that:
|
|
|
|
* "The check for Key_1 != Key_2 shall be done at any place
|
|
|
|
* BEFORE using the keys in the XTS-AES algorithm to process
|
|
|
|
* data with them."
|
|
|
|
*/
|
|
|
|
static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
|
|
|
|
int enc)
|
|
|
|
{
|
|
|
|
if ((!allow_insecure_decrypt || enc)
|
|
|
|
&& CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Provider dispatch functions
|
|
|
|
*/
|
|
|
|
static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *iv, size_t ivlen, int enc)
|
|
|
|
{
|
|
|
|
PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
|
|
|
|
PROV_CIPHER_CTX *ctx = &xctx->base;
|
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
|
|
|
|
2019-09-14 07:27:49 +08:00
|
|
|
ctx->enc = enc;
|
|
|
|
|
|
|
|
if (iv != NULL) {
|
2019-10-08 07:19:10 +08:00
|
|
|
if (!cipher_generic_initiv(vctx, iv, ivlen))
|
2019-09-14 07:27:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (key != NULL) {
|
|
|
|
if (keylen != ctx->keylen) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
|
|
|
|
return 0;
|
|
|
|
return ctx->hw->init(ctx, key, keylen);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *iv, size_t ivlen)
|
|
|
|
{
|
|
|
|
return aes_xts_init(vctx, key, keylen, iv, ivlen, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *iv, size_t ivlen)
|
|
|
|
{
|
|
|
|
return aes_xts_init(vctx, key, keylen, iv, ivlen, 0);
|
|
|
|
}
|
|
|
|
|
2019-09-15 18:06:28 +08:00
|
|
|
static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
|
|
|
|
size_t kbits, size_t blkbits, size_t ivbits)
|
2019-09-14 07:27:49 +08:00
|
|
|
{
|
|
|
|
PROV_AES_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
|
|
|
|
|
|
|
if (ctx != NULL) {
|
2019-09-15 18:06:28 +08:00
|
|
|
cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode, flags,
|
2019-09-14 07:27:49 +08:00
|
|
|
PROV_CIPHER_HW_aes_xts(kbits), NULL);
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void aes_xts_freectx(void *vctx)
|
|
|
|
{
|
|
|
|
PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
|
|
|
|
|
2020-06-23 23:47:31 +08:00
|
|
|
cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
|
2019-09-14 07:27:49 +08:00
|
|
|
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *aes_xts_dupctx(void *vctx)
|
|
|
|
{
|
|
|
|
PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
|
|
|
|
PROV_AES_XTS_CTX *ret = NULL;
|
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return NULL;
|
|
|
|
|
2019-09-14 07:27:49 +08:00
|
|
|
if (in->xts.key1 != NULL) {
|
|
|
|
if (in->xts.key1 != &in->ks1)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (in->xts.key2 != NULL) {
|
|
|
|
if (in->xts.key2 != &in->ks2)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret = OPENSSL_malloc(sizeof(*ret));
|
|
|
|
if (ret == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-11-18 11:13:05 +08:00
|
|
|
in->base.hw->copyctx(&ret->base, &in->base);
|
2019-09-14 07:27:49 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
|
|
|
|
size_t outsize, const unsigned char *in, size_t inl)
|
|
|
|
{
|
|
|
|
PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
|
|
|
|
|
2020-09-08 10:56:34 +08:00
|
|
|
if (!ossl_prov_is_running()
|
|
|
|
|| ctx->xts.key1 == NULL
|
2019-09-14 07:27:49 +08:00
|
|
|
|| ctx->xts.key2 == NULL
|
2019-10-08 07:19:10 +08:00
|
|
|
|| !ctx->base.iv_set
|
2019-09-14 07:27:49 +08:00
|
|
|
|| out == NULL
|
|
|
|
|| in == NULL
|
|
|
|
|| inl < AES_BLOCK_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
2019-12-01 07:18:47 +08:00
|
|
|
* Impose a limit of 2^20 blocks per data unit as specified by
|
2019-09-14 07:27:49 +08:00
|
|
|
* IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
|
|
|
|
* indicated that this was a SHOULD NOT rather than a MUST NOT.
|
|
|
|
* NIST SP 800-38E mandates the same limit.
|
|
|
|
*/
|
|
|
|
if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->stream != NULL)
|
|
|
|
(*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
|
|
|
|
else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
|
|
|
|
ctx->base.enc))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*outl = inl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
|
|
|
|
size_t outsize, const unsigned char *in,
|
|
|
|
size_t inl)
|
|
|
|
{
|
|
|
|
PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
|
|
|
|
|
|
|
|
if (outsize < inl) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
|
|
|
|
size_t outsize)
|
|
|
|
{
|
2020-09-08 10:56:34 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
2019-09-14 07:27:49 +08:00
|
|
|
*outl = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
|
|
|
|
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
|
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
|
|
|
|
2020-08-07 11:20:18 +08:00
|
|
|
static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *provctx)
|
2019-09-14 07:27:49 +08:00
|
|
|
{
|
|
|
|
return aes_xts_known_settable_ctx_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
|
|
|
const OSSL_PARAM *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO(3.0) We need a general solution for handling missing parameters
|
|
|
|
* inside set_params and get_params methods.
|
|
|
|
*/
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
|
|
|
|
if (p != NULL) {
|
|
|
|
size_t keylen;
|
|
|
|
|
|
|
|
if (!OSSL_PARAM_get_size_t(p, &keylen)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* The key length can not be modified for xts mode */
|
|
|
|
if (keylen != ctx->keylen)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
|
2019-09-14 07:27:49 +08:00
|
|
|
static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
|
|
|
|
{ \
|
|
|
|
return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
|
|
|
|
flags, 2 * kbits, AES_XTS_BLOCK_BITS, \
|
|
|
|
AES_XTS_IV_BITS); \
|
|
|
|
} \
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx; \
|
2019-09-14 07:27:49 +08:00
|
|
|
static void *aes_##kbits##_xts_newctx(void *provctx) \
|
|
|
|
{ \
|
2019-09-15 18:06:28 +08:00
|
|
|
return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
|
2019-09-14 07:27:49 +08:00
|
|
|
AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \
|
|
|
|
} \
|
|
|
|
const OSSL_DISPATCH aes##kbits##xts_functions[] = { \
|
|
|
|
{ OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
|
|
|
|
(void (*)(void))aes_##kbits##_##lcmode##_get_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
|
|
|
|
(void (*)(void))cipher_generic_gettable_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
|
|
|
|
(void (*)(void))cipher_generic_get_ctx_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
|
|
|
|
(void (*)(void))cipher_generic_gettable_ctx_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
|
|
|
|
(void (*)(void))aes_xts_set_ctx_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
|
|
|
|
(void (*)(void))aes_xts_settable_ctx_params }, \
|
|
|
|
{ 0, NULL } \
|
|
|
|
}
|
|
|
|
|
|
|
|
IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
|
|
|
|
IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);
|