From bb2131cbf83b57550d3af9d7c66e52c61db8cf60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se> Date: Tue, 25 Jun 2013 21:40:49 +0200 Subject: [PATCH] New interface for AES-128. --- ChangeLog | 18 +++++++++++ Makefile.in | 2 ++ aes-decrypt.c | 10 ++++++ aes-encrypt.c | 10 ++++++ aes-meta.c | 3 -- aes.h | 28 +++++++++++++++++ aes128-meta.c | 57 +++++++++++++++++++++++++++++++++ aes128-set-decrypt-key.c | 46 +++++++++++++++++++++++++++ aes128-set-encrypt-key.c | 38 ++++++++++++++++++++++ nettle-internal.c | 5 +++ nettle-internal.h | 2 ++ testsuite/aes-test.c | 68 ++++++++++++++++++++++++---------------- 12 files changed, 257 insertions(+), 30 deletions(-) create mode 100644 aes128-meta.c create mode 100644 aes128-set-decrypt-key.c create mode 100644 aes128-set-encrypt-key.c diff --git a/ChangeLog b/ChangeLog index 9fddab7f..1306b308 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2013-06-25 Niels Möller <nisse@lysator.liu.se> + + * aes.h (struct aes128_ctx): New aes128 declarations. + * aes-decrypt.c (aes128_decrypt): New function. + * aes-encrypt.c (aes128_encrypt): New function. + * aes128-meta.c: New file. + * aes128-set-encrypt-key.c (aes128_set_encrypt_key): New file and + function. + * aes128-set-decrypt-key.c (aes128_set_decrypt_key) + (aes128_invert_key): New file and functions. + * Makefile.in (nettle_SOURCES): Added aes128-set-encrypt-key.c, + aes128-set-decrypt-key.c and aes128-meta.c. + + * nettle-internal.c (nettle_unified_aes128): For testing the old + AES interface. + * testsuite/aes-test.c (test_cipher2): New function. + (test_main): Test both nettle_aes128 and nettle_unified_aes128. + 2013-05-22 Niels Möller <nisse@lysator.liu.se> * Makefile.in (nettle_SOURCES): Added aes-invert-internal.c and diff --git a/Makefile.in b/Makefile.in index 05857f7a..99eb564f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -65,6 +65,8 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \ aes-encrypt-internal.c aes-encrypt.c aes-encrypt-table.c \ aes-invert-internal.c aes-set-key-internal.c \ aes-set-encrypt-key.c aes-set-decrypt-key.c aes-meta.c \ + aes128-set-encrypt-key.c aes128-set-decrypt-key.c \ + aes128-meta.c \ arcfour.c arcfour-crypt.c arcfour-meta.c \ arctwo.c arctwo-meta.c gosthash94-meta.c \ base16-encode.c base16-decode.c base16-meta.c \ diff --git a/aes-decrypt.c b/aes-decrypt.c index d08eac34..e8696bf5 100644 --- a/aes-decrypt.c +++ b/aes-decrypt.c @@ -345,3 +345,13 @@ aes_decrypt(const struct aes_ctx *ctx, _aes_decrypt(ctx->rounds, ctx->keys, &_aes_decrypt_table, length, dst, src); } + +void +aes128_decrypt(const struct aes128_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src) +{ + assert(!(length % AES_BLOCK_SIZE) ); + _aes_decrypt(_AES128_ROUNDS, ctx->keys, &_aes_decrypt_table, + length, dst, src); +} diff --git a/aes-encrypt.c b/aes-encrypt.c index 0077693a..643276af 100644 --- a/aes-encrypt.c +++ b/aes-encrypt.c @@ -43,3 +43,13 @@ aes_encrypt(const struct aes_ctx *ctx, _aes_encrypt(ctx->rounds, ctx->keys, &_aes_encrypt_table, length, dst, src); } + +void +aes128_encrypt(const struct aes128_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src) +{ + assert(!(length % AES_BLOCK_SIZE) ); + _aes_encrypt(_AES128_ROUNDS, ctx->keys, &_aes_encrypt_table, + length, dst, src); +} diff --git a/aes-meta.c b/aes-meta.c index 7b9af273..3db85f67 100644 --- a/aes-meta.c +++ b/aes-meta.c @@ -28,9 +28,6 @@ #include "aes.h" -const struct nettle_cipher nettle_aes128 -= _NETTLE_CIPHER_SEP(aes, AES, 128); - const struct nettle_cipher nettle_aes192 = _NETTLE_CIPHER_SEP(aes, AES, 192); diff --git a/aes.h b/aes.h index 95f7aa5d..74b3fd67 100644 --- a/aes.h +++ b/aes.h @@ -38,6 +38,11 @@ extern "C" { #define aes_invert_key nettle_aes_invert_key #define aes_encrypt nettle_aes_encrypt #define aes_decrypt nettle_aes_decrypt +#define aes128_set_encrypt_key nettle_aes128set_encrypt_key +#define aes128_set_decrypt_key nettle_aes128set_decrypt_key +#define aes128_invert_key nettle_aes128invert_key +#define aes128_encrypt nettle_aes128encrypt +#define aes128_decrypt nettle_aes128decrypt #define AES_BLOCK_SIZE 16 @@ -53,6 +58,8 @@ extern "C" { #define AES_MIN_KEY_SIZE AES128_KEY_SIZE #define AES_MAX_KEY_SIZE AES256_KEY_SIZE +/* Older nettle-2.7 interface */ + #define AES_KEY_SIZE 32 struct aes_ctx @@ -82,6 +89,27 @@ aes_decrypt(const struct aes_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src); +struct aes128_ctx +{ + uint32_t keys[4 * (_AES128_ROUNDS + 1)]; +}; + +void +aes128_set_encrypt_key(struct aes128_ctx *ctx, const uint8_t *key); +void +aes128_set_decrypt_key(struct aes128_ctx *ctx, const uint8_t *key); +void +aes128_invert_key(struct aes128_ctx *dst, + const struct aes128_ctx *src); +void +aes128_encrypt(const struct aes128_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src); +void +aes128_decrypt(const struct aes128_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src); + #ifdef __cplusplus } #endif diff --git a/aes128-meta.c b/aes128-meta.c new file mode 100644 index 00000000..c3068990 --- /dev/null +++ b/aes128-meta.c @@ -0,0 +1,57 @@ +/* aes128-meta.c */ + +/* nettle, low-level cryptographics library + * + * Copyright (C) 2013 Niels Möller + * + * The nettle library 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. + * + * The nettle 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 the nettle library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02111-1301, USA. + */ + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <assert.h> + +#include "nettle-meta.h" + +#include "aes.h" + +static nettle_set_key_func aes128_set_encrypt_key_wrapper; +static nettle_set_key_func aes128_set_decrypt_key_wrapper; + +static void +aes128_set_encrypt_key_wrapper (void *ctx, size_t length, const uint8_t *key) +{ + assert (length == AES128_KEY_SIZE); + aes128_set_encrypt_key (ctx, key); +} + +static void +aes128_set_decrypt_key_wrapper (void *ctx, size_t length, const uint8_t *key) +{ + assert (length == AES128_KEY_SIZE); + aes128_set_decrypt_key (ctx, key); +} + +const struct nettle_cipher nettle_aes128 = + { "aes128", sizeof(struct aes128_ctx), + AES_BLOCK_SIZE, AES128_KEY_SIZE, + aes128_set_encrypt_key_wrapper, + aes128_set_decrypt_key_wrapper, + (nettle_crypt_func *) aes128_encrypt, + (nettle_crypt_func *) aes128_decrypt + }; diff --git a/aes128-set-decrypt-key.c b/aes128-set-decrypt-key.c new file mode 100644 index 00000000..ee342569 --- /dev/null +++ b/aes128-set-decrypt-key.c @@ -0,0 +1,46 @@ +/* aes128-set-decrypt-key.c + * + * Key setup for the aes/rijndael block cipher. + */ + +/* nettle, low-level cryptographics library + * + * Copyright (C) 2013, Niels Möller + * + * The nettle library 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. + * + * The nettle 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 the nettle library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02111-1301, USA. + */ + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <assert.h> + +#include "aes-internal.h" +#include "macros.h" + +void +aes128_invert_key (struct aes128_ctx *dst, const struct aes128_ctx *src) +{ + _aes_invert (_AES128_ROUNDS, dst->keys, src->keys); +} + +void +aes128_set_decrypt_key(struct aes128_ctx *ctx, const uint8_t *key) +{ + aes128_set_encrypt_key (ctx, key); + aes128_invert_key (ctx, ctx); +} diff --git a/aes128-set-encrypt-key.c b/aes128-set-encrypt-key.c new file mode 100644 index 00000000..e9413cb7 --- /dev/null +++ b/aes128-set-encrypt-key.c @@ -0,0 +1,38 @@ +/* aes128-set-encrypt-key.c + * + * Key setup for the aes/rijndael block cipher. + */ + +/* nettle, low-level cryptographics library + * + * Copyright (C) 2013, Niels Möller + * + * The nettle library 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. + * + * The nettle 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 the nettle library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02111-1301, USA. + */ + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <assert.h> + +#include "aes-internal.h" + +void +aes128_set_encrypt_key(struct aes128_ctx *ctx, const uint8_t *key) +{ + _aes_set_key (_AES128_ROUNDS, AES128_KEY_SIZE / 4, ctx->keys, key); +} diff --git a/nettle-internal.c b/nettle-internal.c index 5cd58288..e5a7271d 100644 --- a/nettle-internal.c +++ b/nettle-internal.c @@ -112,3 +112,8 @@ const struct nettle_aead nettle_gcm_aes192 = _NETTLE_AEAD(gcm, GCM, aes, 192); const struct nettle_aead nettle_gcm_aes256 = _NETTLE_AEAD(gcm, GCM, aes, 256); + +/* Old, unified, interface */ +const struct nettle_cipher nettle_unified_aes128 += _NETTLE_CIPHER_SEP(aes, AES, 128); + diff --git a/nettle-internal.h b/nettle-internal.h index e094064f..ec26fe3c 100644 --- a/nettle-internal.h +++ b/nettle-internal.h @@ -64,6 +64,8 @@ extern const struct nettle_cipher nettle_blowfish128; extern const struct nettle_cipher nettle_salsa20; extern const struct nettle_cipher nettle_salsa20r12; +extern const struct nettle_cipher nettle_unified_aes128; + /* Glue to openssl, for comparative benchmarking. Code in * examples/nettle-openssl.c. */ extern const struct nettle_cipher nettle_openssl_aes128; diff --git a/testsuite/aes-test.c b/testsuite/aes-test.c index 62936d1b..1304e8b0 100644 --- a/testsuite/aes-test.c +++ b/testsuite/aes-test.c @@ -1,5 +1,6 @@ #include "testutils.h" #include "aes.h" +#include "nettle-internal.h" static void test_invert(const struct tstring *key, @@ -45,29 +46,42 @@ test_invert(const struct tstring *key, free (data); } +static void +test_cipher2(const struct nettle_cipher *c1, + const struct nettle_cipher *c2, + const struct tstring *key, + const struct tstring *cleartext, + const struct tstring *ciphertext) +{ + test_cipher (c1, key, cleartext, ciphertext); + test_cipher (c2, key, cleartext, ciphertext); +} + void test_main(void) { + /* Test both the new interface and the older unified interface. */ + /* 128 bit keys */ - test_cipher(&nettle_aes128, - SHEX("0001020305060708 0A0B0C0D0F101112"), - SHEX("506812A45F08C889 B97F5980038B8359"), - SHEX("D8F532538289EF7D 06B506A4FD5BE9C9")); + test_cipher2(&nettle_aes128, &nettle_unified_aes128, + SHEX("0001020305060708 0A0B0C0D0F101112"), + SHEX("506812A45F08C889 B97F5980038B8359"), + SHEX("D8F532538289EF7D 06B506A4FD5BE9C9")); - test_cipher(&nettle_aes128, - SHEX("14151617191A1B1C 1E1F202123242526"), - SHEX("5C6D71CA30DE8B8B 00549984D2EC7D4B"), - SHEX("59AB30F4D4EE6E4F F9907EF65B1FB68C")); - - test_cipher(&nettle_aes128, - SHEX("28292A2B2D2E2F30 323334353738393A"), - SHEX("53F3F4C64F8616E4 E7C56199F48F21F6"), - SHEX("BF1ED2FCB2AF3FD4 1443B56D85025CB1")); + test_cipher2(&nettle_aes128, &nettle_unified_aes128, + SHEX("14151617191A1B1C 1E1F202123242526"), + SHEX("5C6D71CA30DE8B8B 00549984D2EC7D4B"), + SHEX("59AB30F4D4EE6E4F F9907EF65B1FB68C")); + + test_cipher2(&nettle_aes128, &nettle_unified_aes128, + SHEX("28292A2B2D2E2F30 323334353738393A"), + SHEX("53F3F4C64F8616E4 E7C56199F48F21F6"), + SHEX("BF1ED2FCB2AF3FD4 1443B56D85025CB1")); - test_cipher(&nettle_aes128, - SHEX("A0A1A2A3A5A6A7A8 AAABACADAFB0B1B2"), - SHEX("F5F4F7F684878689 A6A7A0A1D2CDCCCF"), - SHEX("CE52AF650D088CA5 59425223F4D32694")); + test_cipher2(&nettle_aes128, &nettle_unified_aes128, + SHEX("A0A1A2A3A5A6A7A8 AAABACADAFB0B1B2"), + SHEX("F5F4F7F684878689 A6A7A0A1D2CDCCCF"), + SHEX("CE52AF650D088CA5 59425223F4D32694")); /* 192 bit keys */ @@ -104,16 +118,16 @@ test_main(void) * F.1.1 ECB-AES128-Encrypt */ - test_cipher(&nettle_aes128, - SHEX("2b7e151628aed2a6abf7158809cf4f3c"), - SHEX("6bc1bee22e409f96e93d7e117393172a" - "ae2d8a571e03ac9c9eb76fac45af8e51" - "30c81c46a35ce411e5fbc1191a0a52ef" - "f69f2445df4f9b17ad2b417be66c3710"), - SHEX("3ad77bb40d7a3660a89ecaf32466ef97" - "f5d3d58503b9699de785895a96fdbaaf" - "43b1cd7f598ece23881b00e3ed030688" - "7b0c785e27e8ad3f8223207104725dd4")); + test_cipher2(&nettle_aes128, &nettle_unified_aes128, + SHEX("2b7e151628aed2a6abf7158809cf4f3c"), + SHEX("6bc1bee22e409f96e93d7e117393172a" + "ae2d8a571e03ac9c9eb76fac45af8e51" + "30c81c46a35ce411e5fbc1191a0a52ef" + "f69f2445df4f9b17ad2b417be66c3710"), + SHEX("3ad77bb40d7a3660a89ecaf32466ef97" + "f5d3d58503b9699de785895a96fdbaaf" + "43b1cd7f598ece23881b00e3ed030688" + "7b0c785e27e8ad3f8223207104725dd4")); /* F.1.3 ECB-AES192-Encrypt */ -- GitLab