Blame lib/accelerated/aarch64/aes-gcm-aarch64.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2011-2016 Free Software Foundation, Inc.
Packit aea12f
 * Copyright (C) 2016-2018 Red Hat, Inc.
Packit aea12f
 *
Packit aea12f
 * Author: Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS.
Packit aea12f
 *
Packit aea12f
 * The GnuTLS is free software; you can redistribute it and/or
Packit aea12f
 * modify it under the terms of the GNU Lesser General Public License
Packit aea12f
 * as published by the Free Software Foundation; either version 2.1 of
Packit aea12f
 * the License, or (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * This library is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * Lesser General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU Lesser General Public License
Packit aea12f
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit aea12f
 *
Packit aea12f
 */
Packit aea12f
Packit aea12f
/*
Packit aea12f
 * The following code is an implementation of the AES-GCM cipher
Packit aea12f
 * using the AES and neon instruction sets.
Packit aea12f
 */
Packit aea12f
Packit aea12f
#include "errors.h"
Packit aea12f
#include "gnutls_int.h"
Packit aea12f
#include <gnutls/crypto.h>
Packit aea12f
#include "errors.h"
Packit aea12f
#include <aes-aarch64.h>
Packit aea12f
#include <aarch64-common.h>
Packit aea12f
#include <nettle/memxor.h>
Packit aea12f
#include <nettle/macros.h>
Packit aea12f
#include <byteswap.h>
Packit aea12f
Packit aea12f
#define GCM_BLOCK_SIZE 16
Packit aea12f
#define INC32(block) INCREMENT(4, block + GCM_BLOCK_SIZE - 4)
Packit aea12f
Packit aea12f
/* GCM mode */
Packit aea12f
Packit aea12f
typedef struct {
Packit aea12f
	uint64_t hi, lo;
Packit aea12f
} u128;
Packit aea12f
Packit aea12f
/* This is the gcm128 structure used in openssl. It
Packit aea12f
 * is compatible with the included assembly code.
Packit aea12f
 */
Packit aea12f
struct gcm128_context {
Packit aea12f
	union {
Packit aea12f
		uint64_t u[2];
Packit aea12f
		uint32_t d[4];
Packit aea12f
		uint8_t c[16];
Packit aea12f
	} Yi, EKi, EK0, len, Xi, H;
Packit aea12f
	u128 Htable[16];
Packit aea12f
};
Packit aea12f
Packit aea12f
struct aes_gcm_ctx {
Packit aea12f
	AES_KEY expanded_key;
Packit aea12f
	struct gcm128_context gcm;
Packit aea12f
	unsigned finished;
Packit aea12f
	unsigned auth_finished;
Packit aea12f
};
Packit aea12f
Packit aea12f
void gcm_init_v8(u128 Htable[16], const uint64_t Xi[2]);
Packit aea12f
void gcm_ghash_v8(uint64_t Xi[2], const u128 Htable[16],
Packit aea12f
		     const uint8_t * inp, size_t len);
Packit aea12f
void gcm_gmult_v8(uint64_t Xi[2], const u128 Htable[16]);
Packit aea12f
Packit aea12f
static void aes_gcm_deinit(void *_ctx)
Packit aea12f
{
Packit aea12f
	struct aes_gcm_ctx *ctx = _ctx;
Packit aea12f
Packit aea12f
	zeroize_temp_key(ctx, sizeof(*ctx));
Packit aea12f
	gnutls_free(ctx);
Packit aea12f
}
Packit aea12f
Packit aea12f
static int
Packit aea12f
aes_gcm_cipher_init(gnutls_cipher_algorithm_t algorithm, void **_ctx,
Packit aea12f
		    int enc)
Packit aea12f
{
Packit aea12f
	/* we use key size to distinguish */
Packit aea12f
	if (algorithm != GNUTLS_CIPHER_AES_128_GCM &&
Packit Service 991b93
	    algorithm != GNUTLS_CIPHER_AES_192_GCM &&
Packit aea12f
	    algorithm != GNUTLS_CIPHER_AES_256_GCM)
Packit aea12f
		return GNUTLS_E_INVALID_REQUEST;
Packit aea12f
Packit aea12f
	*_ctx = gnutls_calloc(1, sizeof(struct aes_gcm_ctx));
Packit aea12f
	if (*_ctx == NULL) {
Packit aea12f
		gnutls_assert();
Packit aea12f
		return GNUTLS_E_MEMORY_ERROR;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
static int
Packit aea12f
aes_gcm_cipher_setkey(void *_ctx, const void *userkey, size_t keysize)
Packit aea12f
{
Packit aea12f
	struct aes_gcm_ctx *ctx = _ctx;
Packit aea12f
	int ret;
Packit aea12f
Packit aea12f
	CHECK_AES_KEYSIZE(keysize);
Packit aea12f
Packit aea12f
	ret =
Packit aea12f
	    aes_v8_set_encrypt_key(userkey, keysize * 8,
Packit aea12f
				  ALIGN16(&ctx->expanded_key));
Packit aea12f
	if (ret != 0)
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_ENCRYPTION_FAILED);
Packit aea12f
Packit aea12f
	aes_v8_encrypt(ctx->gcm.H.c, ctx->gcm.H.c, ALIGN16(&ctx->expanded_key));
Packit aea12f
Packit aea12f
	ctx->gcm.H.u[0] = bswap_64(ctx->gcm.H.u[0]);
Packit aea12f
	ctx->gcm.H.u[1] = bswap_64(ctx->gcm.H.u[1]);
Packit aea12f
Packit aea12f
	gcm_init_v8(ctx->gcm.Htable, ctx->gcm.H.u);
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
static int aes_gcm_setiv(void *_ctx, const void *iv, size_t iv_size)
Packit aea12f
{
Packit aea12f
	struct aes_gcm_ctx *ctx = _ctx;
Packit aea12f
Packit aea12f
	if (iv_size != GCM_BLOCK_SIZE - 4)
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit aea12f
Packit aea12f
	memset(ctx->gcm.Xi.c, 0, sizeof(ctx->gcm.Xi.c));
Packit aea12f
	memset(ctx->gcm.len.c, 0, sizeof(ctx->gcm.len.c));
Packit aea12f
Packit aea12f
	memcpy(ctx->gcm.Yi.c, iv, GCM_BLOCK_SIZE - 4);
Packit aea12f
	ctx->gcm.Yi.c[GCM_BLOCK_SIZE - 4] = 0;
Packit aea12f
	ctx->gcm.Yi.c[GCM_BLOCK_SIZE - 3] = 0;
Packit aea12f
	ctx->gcm.Yi.c[GCM_BLOCK_SIZE - 2] = 0;
Packit aea12f
	ctx->gcm.Yi.c[GCM_BLOCK_SIZE - 1] = 1;
Packit aea12f
Packit aea12f
	aes_v8_encrypt(ctx->gcm.Yi.c, ctx->gcm.EK0.c,
Packit aea12f
			ALIGN16(&ctx->expanded_key));
Packit aea12f
	ctx->gcm.Yi.c[GCM_BLOCK_SIZE - 1] = 2;
Packit aea12f
	ctx->finished = 0;
Packit aea12f
	ctx->auth_finished = 0;
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
static void
Packit aea12f
gcm_ghash(struct aes_gcm_ctx *ctx, const uint8_t * src, size_t src_size)
Packit aea12f
{
Packit aea12f
	size_t rest = src_size % GCM_BLOCK_SIZE;
Packit aea12f
	size_t aligned_size = src_size - rest;
Packit aea12f
Packit aea12f
	if (aligned_size > 0)
Packit aea12f
		gcm_ghash_v8(ctx->gcm.Xi.u, ctx->gcm.Htable, src,
Packit aea12f
				aligned_size);
Packit aea12f
Packit aea12f
	if (rest > 0) {
Packit aea12f
		memxor(ctx->gcm.Xi.c, src + aligned_size, rest);
Packit aea12f
		gcm_gmult_v8(ctx->gcm.Xi.u, ctx->gcm.Htable);
Packit aea12f
	}
Packit aea12f
}
Packit aea12f
Packit aea12f
static void
Packit aea12f
ctr32_encrypt_blocks_inplace(const unsigned char *in, unsigned char *out,
Packit aea12f
			     size_t blocks, const AES_KEY *key,
Packit aea12f
			     const unsigned char ivec[16])
Packit aea12f
{
Packit aea12f
	unsigned i;
Packit aea12f
	uint8_t ctr[16];
Packit aea12f
	uint8_t tmp[16];
Packit aea12f
Packit aea12f
	memcpy(ctr, ivec, 16);
Packit aea12f
Packit aea12f
	for (i=0;i
Packit aea12f
		aes_v8_encrypt(ctr, tmp, key);
Packit aea12f
		memxor3(out, tmp, in, 16);
Packit aea12f
Packit aea12f
		out += 16;
Packit aea12f
		in += 16;
Packit aea12f
		INC32(ctr);
Packit aea12f
	}
Packit aea12f
}
Packit aea12f
Packit aea12f
static void
Packit aea12f
ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
Packit aea12f
		     size_t blocks, const AES_KEY *key,
Packit aea12f
		     const unsigned char ivec[16])
Packit aea12f
{
Packit aea12f
	unsigned i;
Packit aea12f
	uint8_t ctr[16];
Packit aea12f
Packit aea12f
	if (in == out)
Packit aea12f
		return ctr32_encrypt_blocks_inplace(in, out, blocks, key, ivec);
Packit aea12f
Packit aea12f
	memcpy(ctr, ivec, 16);
Packit aea12f
Packit aea12f
	for (i=0;i
Packit aea12f
		aes_v8_encrypt(ctr, out, key);
Packit aea12f
		memxor(out, in, 16);
Packit aea12f
Packit aea12f
		out += 16;
Packit aea12f
		in += 16;
Packit aea12f
		INC32(ctr);
Packit aea12f
	}
Packit aea12f
}
Packit aea12f
Packit aea12f
static inline void
Packit aea12f
ctr_encrypt_last(struct aes_gcm_ctx *ctx, const uint8_t * src,
Packit aea12f
		 uint8_t * dst, size_t pos, size_t length)
Packit aea12f
{
Packit aea12f
	uint8_t tmp[GCM_BLOCK_SIZE];
Packit aea12f
	uint8_t out[GCM_BLOCK_SIZE];
Packit aea12f
Packit aea12f
	memcpy(tmp, &src[pos], length);
Packit aea12f
	ctr32_encrypt_blocks(tmp, out, 1,
Packit aea12f
			     ALIGN16(&ctx->expanded_key),
Packit aea12f
			     ctx->gcm.Yi.c);
Packit aea12f
Packit aea12f
	memcpy(&dst[pos], out, length);
Packit aea12f
Packit aea12f
}
Packit aea12f
Packit aea12f
static int
Packit aea12f
aes_gcm_encrypt(void *_ctx, const void *src, size_t src_size,
Packit aea12f
		void *dst, size_t length)
Packit aea12f
{
Packit aea12f
	struct aes_gcm_ctx *ctx = _ctx;
Packit aea12f
	int blocks = src_size / GCM_BLOCK_SIZE;
Packit aea12f
	int exp_blocks = blocks * GCM_BLOCK_SIZE;
Packit aea12f
	int rest = src_size - (exp_blocks);
Packit aea12f
	uint32_t counter;
Packit aea12f
Packit aea12f
	if (unlikely(ctx->finished))
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit aea12f
Packit aea12f
	if (blocks > 0) {
Packit aea12f
		ctr32_encrypt_blocks(src, dst,
Packit aea12f
				     blocks,
Packit aea12f
				     ALIGN16(&ctx->expanded_key),
Packit aea12f
				     ctx->gcm.Yi.c);
Packit aea12f
Packit aea12f
		counter = _gnutls_read_uint32(ctx->gcm.Yi.c + 12);
Packit aea12f
		counter += blocks;
Packit aea12f
		_gnutls_write_uint32(counter, ctx->gcm.Yi.c + 12);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (rest > 0) {	/* last incomplete block */
Packit aea12f
		ctr_encrypt_last(ctx, src, dst, exp_blocks, rest);
Packit aea12f
		ctx->finished = 1;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	gcm_ghash(ctx, dst, src_size);
Packit aea12f
	ctx->gcm.len.u[1] += src_size;
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
static int
Packit aea12f
aes_gcm_decrypt(void *_ctx, const void *src, size_t src_size,
Packit aea12f
		void *dst, size_t dst_size)
Packit aea12f
{
Packit aea12f
	struct aes_gcm_ctx *ctx = _ctx;
Packit aea12f
	int blocks = src_size / GCM_BLOCK_SIZE;
Packit aea12f
	int exp_blocks = blocks * GCM_BLOCK_SIZE;
Packit aea12f
	int rest = src_size - (exp_blocks);
Packit aea12f
	uint32_t counter;
Packit aea12f
Packit aea12f
	if (unlikely(ctx->finished))
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit aea12f
Packit aea12f
	gcm_ghash(ctx, src, src_size);
Packit aea12f
	ctx->gcm.len.u[1] += src_size;
Packit aea12f
Packit aea12f
	if (blocks > 0) {
Packit aea12f
		ctr32_encrypt_blocks(src, dst,
Packit aea12f
				     blocks,
Packit aea12f
				     ALIGN16(&ctx->expanded_key),
Packit aea12f
				     ctx->gcm.Yi.c);
Packit aea12f
Packit aea12f
		counter = _gnutls_read_uint32(ctx->gcm.Yi.c + 12);
Packit aea12f
		counter += blocks;
Packit aea12f
		_gnutls_write_uint32(counter, ctx->gcm.Yi.c + 12);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	if (rest > 0) { /* last incomplete block */
Packit aea12f
		ctr_encrypt_last(ctx, src, dst, exp_blocks, rest);
Packit aea12f
		ctx->finished = 1;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
static int aes_gcm_auth(void *_ctx, const void *src, size_t src_size)
Packit aea12f
{
Packit aea12f
	struct aes_gcm_ctx *ctx = _ctx;
Packit aea12f
Packit aea12f
	if (unlikely(ctx->auth_finished))
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit aea12f
Packit aea12f
	gcm_ghash(ctx, src, src_size);
Packit aea12f
	ctx->gcm.len.u[0] += src_size;
Packit aea12f
Packit aea12f
	if (src_size % GCM_BLOCK_SIZE != 0)
Packit aea12f
		ctx->auth_finished = 1;
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
Packit aea12f
static void aes_gcm_tag(void *_ctx, void *tag, size_t tagsize)
Packit aea12f
{
Packit aea12f
	struct aes_gcm_ctx *ctx = _ctx;
Packit aea12f
	uint8_t buffer[GCM_BLOCK_SIZE];
Packit aea12f
	uint64_t alen, clen;
Packit aea12f
Packit aea12f
	alen = ctx->gcm.len.u[0] * 8;
Packit aea12f
	clen = ctx->gcm.len.u[1] * 8;
Packit aea12f
Packit aea12f
	_gnutls_write_uint64(alen, buffer);
Packit aea12f
	_gnutls_write_uint64(clen, &buffer[8]);
Packit aea12f
Packit aea12f
	gcm_ghash_v8(ctx->gcm.Xi.u, ctx->gcm.Htable, buffer,
Packit aea12f
			GCM_BLOCK_SIZE);
Packit aea12f
Packit aea12f
	ctx->gcm.Xi.u[0] ^= ctx->gcm.EK0.u[0];
Packit aea12f
	ctx->gcm.Xi.u[1] ^= ctx->gcm.EK0.u[1];
Packit aea12f
Packit aea12f
	memcpy(tag, ctx->gcm.Xi.c, MIN(GCM_BLOCK_SIZE, tagsize));
Packit aea12f
}
Packit aea12f
Packit aea12f
#include "../x86/aes-gcm-aead.h"
Packit aea12f
Packit aea12f
const gnutls_crypto_cipher_st _gnutls_aes_gcm_aarch64 = {
Packit aea12f
	.init = aes_gcm_cipher_init,
Packit aea12f
	.setkey = aes_gcm_cipher_setkey,
Packit aea12f
	.setiv = aes_gcm_setiv,
Packit aea12f
	.aead_encrypt = aes_gcm_aead_encrypt,
Packit aea12f
	.aead_decrypt = aes_gcm_aead_decrypt,
Packit aea12f
	.encrypt = aes_gcm_encrypt,
Packit aea12f
	.decrypt = aes_gcm_decrypt,
Packit aea12f
	.deinit = aes_gcm_deinit,
Packit aea12f
	.tag = aes_gcm_tag,
Packit aea12f
	.auth = aes_gcm_auth,
Packit aea12f
};