summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2016-09-29 15:41:23 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2016-10-03 13:53:05 +0200
commit3cb9087e30aa7b69e69d6846cf8ba8281ee7f877 (patch)
tree967dd8f1815f0721a507aa5467efc9f141d80f85
parentc5032beda17213eea05e842739dafae50e87f39b (diff)
downloadgnutls-3cb9087e30aa7b69e69d6846cf8ba8281ee7f877.tar.gz
aarch64: added optimized AES-CCM mode
-rw-r--r--lib/accelerated/aarch64/Makefile.am2
-rw-r--r--lib/accelerated/aarch64/aarch64-common.c14
-rw-r--r--lib/accelerated/aarch64/aes-aarch64.h2
-rw-r--r--lib/accelerated/aarch64/aes-ccm-aarch64.c155
4 files changed, 171 insertions, 2 deletions
diff --git a/lib/accelerated/aarch64/Makefile.am b/lib/accelerated/aarch64/Makefile.am
index edc1edd495..7ccc855367 100644
--- a/lib/accelerated/aarch64/Makefile.am
+++ b/lib/accelerated/aarch64/Makefile.am
@@ -41,7 +41,7 @@ EXTRA_DIST = README
noinst_LTLIBRARIES = libaarch64.la
libaarch64_la_SOURCES = aarch64-common.c aarch64-common.h sha-aarch64.h sha-aarch64.c \
- hmac-sha-aarch64.c aes-cbc-aarch64.c aes-gcm-aarch64.c aes-aarch64.h
+ hmac-sha-aarch64.c aes-cbc-aarch64.c aes-gcm-aarch64.c aes-aarch64.h aes-ccm-aarch64.c
if ASM_AARCH64
libaarch64_la_SOURCES += elf/sha1-armv8.s elf/sha512-armv8.s elf/sha256-armv8.s \
diff --git a/lib/accelerated/aarch64/aarch64-common.c b/lib/accelerated/aarch64/aarch64-common.c
index 310ea5508c..11f6a7466e 100644
--- a/lib/accelerated/aarch64/aarch64-common.c
+++ b/lib/accelerated/aarch64/aarch64-common.c
@@ -226,6 +226,20 @@ void _register_aarch64_crypto(unsigned capabilities)
if (ret < 0) {
gnutls_assert();
}
+
+ ret =
+ gnutls_crypto_single_cipher_register
+ (GNUTLS_CIPHER_AES_128_CCM, 90, &_gnutls_aes_ccm_aarch64, 0);
+ if (ret < 0) {
+ gnutls_assert();
+ }
+
+ ret =
+ gnutls_crypto_single_cipher_register
+ (GNUTLS_CIPHER_AES_256_CCM, 90, &_gnutls_aes_ccm_aarch64, 0);
+ if (ret < 0) {
+ gnutls_assert();
+ }
}
return;
diff --git a/lib/accelerated/aarch64/aes-aarch64.h b/lib/accelerated/aarch64/aes-aarch64.h
index 35d5926cde..55b815173c 100644
--- a/lib/accelerated/aarch64/aes-aarch64.h
+++ b/lib/accelerated/aarch64/aes-aarch64.h
@@ -24,7 +24,7 @@ void aes_v8_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *
void aes_v8_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key);
extern const gnutls_crypto_cipher_st _gnutls_aes_gcm_aarch64;
-
extern const gnutls_crypto_cipher_st _gnutls_aes_cbc_aarch64;
+extern const gnutls_crypto_cipher_st _gnutls_aes_ccm_aarch64;
#endif
diff --git a/lib/accelerated/aarch64/aes-ccm-aarch64.c b/lib/accelerated/aarch64/aes-ccm-aarch64.c
new file mode 100644
index 0000000000..590ee6b6ab
--- /dev/null
+++ b/lib/accelerated/aarch64/aes-ccm-aarch64.c
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2014-2016 Red Hat, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * The GnuTLS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+/*
+ * The following code is an implementation of the AES-128-CCM cipher
+ * on aarch64.
+ */
+
+#include "errors.h"
+#include "gnutls_int.h"
+
+#ifdef HAVE_LIBNETTLE
+
+#include <gnutls/crypto.h>
+#include "errors.h"
+#include <aarch64-common.h>
+#include <byteswap.h>
+#include <nettle/ccm.h>
+#include <aes-aarch64.h>
+
+typedef struct ccm_aarch64_aes_ctx {
+ AES_KEY key;
+} ccm_aarch64_aes_ctx;
+
+/* CCM mode
+ */
+static void aarch64_aes_encrypt(const void *_ctx,
+ size_t length, uint8_t * dst,
+ const uint8_t * src)
+{
+ AES_KEY *ctx = (void*)_ctx;
+ unsigned i;
+
+ for (i=0;i<length;i+=16) {
+ aes_v8_encrypt(src, dst, ctx);
+ src+=16;
+ dst+=16;
+ }
+}
+
+static int
+aes_ccm_cipher_init(gnutls_cipher_algorithm_t algorithm, void **ctx,
+ int enc)
+{
+ /* we use key size to distinguish */
+ if (algorithm != GNUTLS_CIPHER_AES_128_CCM &&
+ algorithm != GNUTLS_CIPHER_AES_256_CCM &&
+ algorithm != GNUTLS_CIPHER_AES_128_CCM_8 &&
+ algorithm != GNUTLS_CIPHER_AES_256_CCM_8)
+ return GNUTLS_E_INVALID_REQUEST;
+
+ *ctx = gnutls_calloc(1, sizeof(ccm_aarch64_aes_ctx));
+ if (*ctx == NULL) {
+ gnutls_assert();
+ return GNUTLS_E_MEMORY_ERROR;
+ }
+
+ return 0;
+}
+
+static int
+aes_ccm_cipher_setkey(void *_ctx, const void *key, size_t length)
+{
+ struct ccm_aarch64_aes_ctx *ctx = _ctx;
+ aes_v8_set_encrypt_key(key, length*8, &ctx->key);
+
+ return 0;
+}
+
+static int
+aes_ccm_aead_encrypt(void *_ctx,
+ const void *nonce, size_t nonce_size,
+ const void *auth, size_t auth_size,
+ size_t tag_size,
+ const void *plain, size_t plain_size,
+ void *encr, size_t encr_size)
+{
+ struct ccm_aarch64_aes_ctx *ctx = _ctx;
+ /* proper AEAD cipher */
+
+ if (unlikely(encr_size < plain_size + tag_size))
+ return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
+
+ ccm_encrypt_message(&ctx->key, aarch64_aes_encrypt,
+ nonce_size, nonce,
+ auth_size, auth,
+ tag_size,
+ plain_size+tag_size, encr,
+ plain);
+ return 0;
+}
+
+static int
+aes_ccm_aead_decrypt(void *_ctx,
+ const void *nonce, size_t nonce_size,
+ const void *auth, size_t auth_size,
+ size_t tag_size,
+ const void *encr, size_t encr_size,
+ void *plain, size_t plain_size)
+{
+ struct ccm_aarch64_aes_ctx *ctx = _ctx;
+ int ret;
+
+ if (unlikely(encr_size < tag_size))
+ return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
+
+ ret = ccm_decrypt_message(&ctx->key, aarch64_aes_encrypt,
+ nonce_size, nonce,
+ auth_size, auth,
+ tag_size,
+ encr_size-tag_size, plain,
+ encr);
+ if (unlikely(ret == 0))
+ return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
+
+ return 0;
+}
+
+
+static void aes_ccm_deinit(void *_ctx)
+{
+ struct ccm_aarch64_aes_ctx *ctx = _ctx;
+
+ zeroize_temp_key(ctx, sizeof(*ctx));
+ gnutls_free(ctx);
+}
+
+const gnutls_crypto_cipher_st _gnutls_aes_ccm_aarch64 = {
+ .init = aes_ccm_cipher_init,
+ .setkey = aes_ccm_cipher_setkey,
+ .aead_encrypt = aes_ccm_aead_encrypt,
+ .aead_decrypt = aes_ccm_aead_decrypt,
+ .deinit = aes_ccm_deinit,
+};
+
+#endif