Blame lib/accelerated/x86/aes-xts-x86-aesni.c

Packit Service 991b93
/*
Packit Service 991b93
 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
Packit Service 991b93
 * Copyright (C) 2020 Red Hat, Inc.
Packit Service 991b93
 *
Packit Service 991b93
 * Authors: Nikos Mavrogiannopoulos, Anderson Toshiyuki Sasaki
Packit Service 991b93
 *
Packit Service 991b93
 * This file is part of GnuTLS.
Packit Service 991b93
 *
Packit Service 991b93
 * The GnuTLS is free software; you can redistribute it and/or
Packit Service 991b93
 * modify it under the terms of the GNU Lesser General Public License
Packit Service 991b93
 * as published by the Free Software Foundation; either version 2.1 of
Packit Service 991b93
 * the License, or (at your option) any later version.
Packit Service 991b93
 *
Packit Service 991b93
 * This library is distributed in the hope that it will be useful, but
Packit Service 991b93
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 991b93
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 991b93
 * Lesser General Public License for more details.
Packit Service 991b93
 *
Packit Service 991b93
 * You should have received a copy of the GNU Lesser General Public License
Packit Service 991b93
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit Service 991b93
 *
Packit Service 991b93
 */
Packit Service 991b93
Packit Service 991b93
/*
Packit Service 991b93
 * The following code wraps the CRYPTOGAMS implementation of the AES-XTS cipher
Packit Service 991b93
 * using Intel's AES instruction set.
Packit Service 991b93
 */
Packit Service 991b93
Packit Service 991b93
#include "errors.h"
Packit Service 991b93
#include "gnutls_int.h"
Packit Service 991b93
#include "fips.h"
Packit Service 991b93
#include <gnutls/crypto.h>
Packit Service 991b93
#include <aes-x86.h>
Packit Service 991b93
#include <x86-common.h>
Packit Service 991b93
Packit Service 991b93
struct x86_aes_xts_ctx {
Packit Service 991b93
	AES_KEY block_key;
Packit Service 991b93
	AES_KEY tweak_key;
Packit Service 991b93
	uint8_t iv[16];
Packit Service 991b93
	int enc;
Packit Service 991b93
};
Packit Service 991b93
Packit Service 991b93
static int
Packit Service 991b93
x86_aes_xts_cipher_init(gnutls_cipher_algorithm_t algorithm, void **_ctx,
Packit Service 991b93
					int enc)
Packit Service 991b93
{
Packit Service 991b93
	if (algorithm != GNUTLS_CIPHER_AES_128_XTS &&
Packit Service 991b93
		algorithm != GNUTLS_CIPHER_AES_256_XTS)
Packit Service 991b93
		return GNUTLS_E_INVALID_REQUEST;
Packit Service 991b93
Packit Service 991b93
	*_ctx = gnutls_calloc(1, sizeof(struct x86_aes_xts_ctx));
Packit Service 991b93
	if (*_ctx == NULL) {
Packit Service 991b93
		gnutls_assert();
Packit Service 991b93
		return GNUTLS_E_MEMORY_ERROR;
Packit Service 991b93
	}
Packit Service 991b93
Packit Service 991b93
	((struct x86_aes_xts_ctx *) (*_ctx))->enc = enc;
Packit Service 991b93
Packit Service 991b93
	return 0;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
static int
Packit Service 991b93
x86_aes_xts_cipher_setkey(void *_ctx, const void *userkey, size_t keysize)
Packit Service 991b93
{
Packit Service 991b93
	struct x86_aes_xts_ctx *ctx = _ctx;
Packit Service 991b93
	int ret;
Packit Service 991b93
	size_t keybits;
Packit Service 991b93
	const uint8_t *key = userkey;
Packit Service 991b93
Packit Service 991b93
	if ((keysize != 32) && (keysize != 64))
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit Service 991b93
Packit Service 991b93
	/* Check key block according to FIPS-140-2 IG A.9 */
Packit Service 991b93
	if (_gnutls_fips_mode_enabled()){
Packit Service 6bac9f
		if (gnutls_memcmp(key, key + (keysize / 2), keysize / 2) == 0) {
Packit Service 991b93
			_gnutls_switch_lib_state(LIB_STATE_ERROR);
Packit Service 991b93
			return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit Service 991b93
		}
Packit Service 991b93
	}
Packit Service 991b93
Packit Service 991b93
	/* Size in bits of each half for block and tweak (=keysize * 8 / 2) */
Packit Service 991b93
	keybits = keysize * 4;
Packit Service 991b93
Packit Service 991b93
	if (ctx->enc)
Packit Service 991b93
		ret =
Packit Service 991b93
		    aesni_set_encrypt_key(key, keybits,
Packit Service 991b93
					  ALIGN16(&ctx->block_key));
Packit Service 991b93
	else
Packit Service 991b93
		ret =
Packit Service 991b93
		    aesni_set_decrypt_key(key, keybits,
Packit Service 991b93
					  ALIGN16(&ctx->block_key));
Packit Service 991b93
Packit Service 991b93
	if (ret != 0)
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED);
Packit Service 991b93
Packit Service 991b93
	ret =
Packit Service 991b93
	    aesni_set_encrypt_key(key + (keysize / 2), keybits,
Packit Service 991b93
					  ALIGN16(&ctx->tweak_key));
Packit Service 991b93
	if (ret != 0)
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED);
Packit Service 991b93
Packit Service 991b93
	return 0;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
static int x86_aes_xts_setiv(void *_ctx, const void *iv, size_t iv_size)
Packit Service 991b93
{
Packit Service 991b93
	struct x86_aes_xts_ctx *ctx = _ctx;
Packit Service 991b93
Packit Service 991b93
	if (iv_size != 16)
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit Service 991b93
Packit Service 991b93
	memcpy(ctx->iv, iv, 16);
Packit Service 991b93
	return 0;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
static int
Packit Service 991b93
x86_aes_xts_encrypt(void *_ctx, const void *src, size_t src_size,
Packit Service 991b93
	    void *dst, size_t dst_size)
Packit Service 991b93
{
Packit Service 991b93
	struct x86_aes_xts_ctx *ctx = _ctx;
Packit Service 991b93
Packit Service 991b93
	if (src_size < 16)
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit Service 991b93
Packit Service 991b93
	aesni_xts_encrypt(src, dst, src_size, ALIGN16(&ctx->block_key),
Packit Service 991b93
			  ALIGN16(&ctx->tweak_key), ctx->iv);
Packit Service 991b93
	return 0;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
static int
Packit Service 991b93
x86_aes_xts_decrypt(void *_ctx, const void *src, size_t src_size,
Packit Service 991b93
	    void *dst, size_t dst_size)
Packit Service 991b93
{
Packit Service 991b93
	struct x86_aes_xts_ctx *ctx = _ctx;
Packit Service 991b93
Packit Service 991b93
	if (src_size < 16)
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit Service 991b93
Packit Service 991b93
	aesni_xts_decrypt(src, dst, src_size, ALIGN16(&ctx->block_key),
Packit Service 991b93
			  ALIGN16(&ctx->tweak_key), ctx->iv);
Packit Service 991b93
	return 0;
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
static void x86_aes_xts_deinit(void *_ctx)
Packit Service 991b93
{
Packit Service 991b93
	struct x86_aes_xts_ctx *ctx = _ctx;
Packit Service 991b93
Packit Service 991b93
	zeroize_temp_key(ctx, sizeof(*ctx));
Packit Service 991b93
	gnutls_free(ctx);
Packit Service 991b93
}
Packit Service 991b93
Packit Service 991b93
const gnutls_crypto_cipher_st _gnutls_aes_xts_x86_aesni = {
Packit Service 991b93
	.init = x86_aes_xts_cipher_init,
Packit Service 991b93
	.setkey = x86_aes_xts_cipher_setkey,
Packit Service 991b93
	.setiv = x86_aes_xts_setiv,
Packit Service 991b93
	.encrypt = x86_aes_xts_encrypt,
Packit Service 991b93
	.decrypt = x86_aes_xts_decrypt,
Packit Service 991b93
	.deinit = x86_aes_xts_deinit,
Packit Service 991b93
};
Packit Service 991b93