diff --git a/ChangeLog b/ChangeLog index 31a3dd4f52ff98f5208693632f831afcac0f7805..82980eb196df13ecebbe84b9036b2bd9693c13e5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2013-06-25 Niels Möller <nisse@lysator.liu.se> + * aes-meta.c: Deleted file. + + Analogous changes for new aes192 and aes256 interface. + * aes.h (struct aes128_ctx): New aes128 declarations. * aes-decrypt.c (aes128_decrypt): New function. * aes-encrypt.c (aes128_encrypt): New function. @@ -16,8 +20,6 @@ * testsuite/aes-test.c (test_cipher2): New function. (test_main): Test both nettle_aes128 and nettle_unified_aes128. - Analogous changes för aes192. - 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 e803c328118f24e06c59c6a1dc11f97a9e1a9705..ed44f8b710ac22e5611e80abe1de67c248691a3f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -64,11 +64,13 @@ all-here: $(TARGETS) $(DOCTARGETS) 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 \ + aes-set-encrypt-key.c aes-set-decrypt-key.c \ aes128-set-encrypt-key.c aes128-set-decrypt-key.c \ aes128-meta.c \ aes192-set-encrypt-key.c aes192-set-decrypt-key.c \ aes192-meta.c \ + aes256-set-encrypt-key.c aes256-set-decrypt-key.c \ + aes256-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 9ea0e23897164d9fdb06c32307197da0e2c0c3a0..d0fefc4f363b1bc447e6dc09be4f406ad1cc1a43 100644 --- a/aes-decrypt.c +++ b/aes-decrypt.c @@ -365,3 +365,13 @@ aes192_decrypt(const struct aes192_ctx *ctx, _aes_decrypt(_AES192_ROUNDS, ctx->keys, &_aes_decrypt_table, length, dst, src); } + +void +aes256_decrypt(const struct aes256_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src) +{ + assert(!(length % AES_BLOCK_SIZE) ); + _aes_decrypt(_AES256_ROUNDS, ctx->keys, &_aes_decrypt_table, + length, dst, src); +} diff --git a/aes-encrypt.c b/aes-encrypt.c index c4e3713e6add142b7ea9d7da6c43b28a7ddba636..8d55ca18d32103878f139e13ad96c8e39c6dbae7 100644 --- a/aes-encrypt.c +++ b/aes-encrypt.c @@ -63,3 +63,13 @@ aes192_encrypt(const struct aes192_ctx *ctx, _aes_encrypt(_AES192_ROUNDS, ctx->keys, &_aes_encrypt_table, length, dst, src); } + +void +aes256_encrypt(const struct aes256_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src) +{ + assert(!(length % AES_BLOCK_SIZE) ); + _aes_encrypt(_AES256_ROUNDS, ctx->keys, &_aes_encrypt_table, + length, dst, src); +} diff --git a/aes.h b/aes.h index ca9976e55a5be6da2853d74414ebad2cfeab91f7..0982aa698212b8afb7a6c3a40e9caa1b86eb223f 100644 --- a/aes.h +++ b/aes.h @@ -48,6 +48,11 @@ extern "C" { #define aes192_invert_key nettle_aes192invert_key #define aes192_encrypt nettle_aes192encrypt #define aes192_decrypt nettle_aes192decrypt +#define aes256_set_encrypt_key nettle_aes256_set_encrypt_key +#define aes256_set_decrypt_key nettle_aes256_set_decrypt_key +#define aes256_invert_key nettle_aes256_invert_key +#define aes256_encrypt nettle_aes256_encrypt +#define aes256_decrypt nettle_aes256_decrypt #define AES_BLOCK_SIZE 16 @@ -136,6 +141,27 @@ aes192_decrypt(const struct aes192_ctx *ctx, size_t length, uint8_t *dst, const uint8_t *src); +struct aes256_ctx +{ + uint32_t keys[4 * (_AES256_ROUNDS + 1)]; +}; + +void +aes256_set_encrypt_key(struct aes256_ctx *ctx, const uint8_t *key); +void +aes256_set_decrypt_key(struct aes256_ctx *ctx, const uint8_t *key); +void +aes256_invert_key(struct aes256_ctx *dst, + const struct aes256_ctx *src); +void +aes256_encrypt(const struct aes256_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src); +void +aes256_decrypt(const struct aes256_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src); + #ifdef __cplusplus } #endif diff --git a/aes256-meta.c b/aes256-meta.c new file mode 100644 index 0000000000000000000000000000000000000000..197441e0ed529f492c4be692e325a87c8c0bae18 --- /dev/null +++ b/aes256-meta.c @@ -0,0 +1,57 @@ +/* aes256-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 aes256_set_encrypt_key_wrapper; +static nettle_set_key_func aes256_set_decrypt_key_wrapper; + +static void +aes256_set_encrypt_key_wrapper (void *ctx, size_t length, const uint8_t *key) +{ + assert (length == AES256_KEY_SIZE); + aes256_set_encrypt_key (ctx, key); +} + +static void +aes256_set_decrypt_key_wrapper (void *ctx, size_t length, const uint8_t *key) +{ + assert (length == AES256_KEY_SIZE); + aes256_set_decrypt_key (ctx, key); +} + +const struct nettle_cipher nettle_aes256 = + { "aes256", sizeof(struct aes256_ctx), + AES_BLOCK_SIZE, AES256_KEY_SIZE, + aes256_set_encrypt_key_wrapper, + aes256_set_decrypt_key_wrapper, + (nettle_crypt_func *) aes256_encrypt, + (nettle_crypt_func *) aes256_decrypt + }; diff --git a/aes256-set-decrypt-key.c b/aes256-set-decrypt-key.c new file mode 100644 index 0000000000000000000000000000000000000000..60b70e294bb54b9816fb1a062a4b0c226dfc2753 --- /dev/null +++ b/aes256-set-decrypt-key.c @@ -0,0 +1,46 @@ +/* aes256-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 +aes256_invert_key (struct aes256_ctx *dst, const struct aes256_ctx *src) +{ + _aes_invert (_AES256_ROUNDS, dst->keys, src->keys); +} + +void +aes256_set_decrypt_key(struct aes256_ctx *ctx, const uint8_t *key) +{ + aes256_set_encrypt_key (ctx, key); + aes256_invert_key (ctx, ctx); +} diff --git a/aes-meta.c b/aes256-set-encrypt-key.c similarity index 74% rename from aes-meta.c rename to aes256-set-encrypt-key.c index 6bce5a4226dbf869397596fe163cfeaec0e86da9..9e11ff123e64a1e22c480beb880341192387d949 100644 --- a/aes-meta.c +++ b/aes256-set-encrypt-key.c @@ -1,8 +1,11 @@ -/* aes-meta.c */ +/* aes256-set-encrypt-key.c + * + * Key setup for the aes/rijndael block cipher. + */ /* nettle, low-level cryptographics library * - * Copyright (C) 2002 Niels Möller + * 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 @@ -24,9 +27,12 @@ # include "config.h" #endif -#include "nettle-meta.h" +#include <assert.h> -#include "aes.h" +#include "aes-internal.h" -const struct nettle_cipher nettle_aes256 -= _NETTLE_CIPHER_SEP(aes, AES, 256); +void +aes256_set_encrypt_key(struct aes256_ctx *ctx, const uint8_t *key) +{ + _aes_set_key (_AES256_ROUNDS, AES256_KEY_SIZE / 4, ctx->keys, key); +} diff --git a/nettle-internal.c b/nettle-internal.c index e258eb44c5cdc101383676e0ffcd568eb79a9c92..8308df69f1a99090996377ea82a984357e49c553 100644 --- a/nettle-internal.c +++ b/nettle-internal.c @@ -119,3 +119,6 @@ const struct nettle_cipher nettle_unified_aes128 const struct nettle_cipher nettle_unified_aes192 = _NETTLE_CIPHER_SEP(aes, AES, 192); + +const struct nettle_cipher nettle_unified_aes256 += _NETTLE_CIPHER_SEP(aes, AES, 256); diff --git a/nettle-internal.h b/nettle-internal.h index 651ff4c01416c2d77785b0407d8f4e43f545eabc..09881ce9b607228d03ab393645a60085c08a1ba0 100644 --- a/nettle-internal.h +++ b/nettle-internal.h @@ -66,6 +66,7 @@ extern const struct nettle_cipher nettle_salsa20r12; extern const struct nettle_cipher nettle_unified_aes128; extern const struct nettle_cipher nettle_unified_aes192; +extern const struct nettle_cipher nettle_unified_aes256; /* Glue to openssl, for comparative benchmarking. Code in * examples/nettle-openssl.c. */ diff --git a/testsuite/aes-test.c b/testsuite/aes-test.c index 14e20e4ef1fc4246b5ef122034036582fe22340b..964114e3c84b0c221921b94d86e39cb75f8a4103 100644 --- a/testsuite/aes-test.c +++ b/testsuite/aes-test.c @@ -90,25 +90,25 @@ test_main(void) SHEX("2D33EEF2C0430A8A 9EBF45E809C40BB6"), SHEX("DFF4945E0336DF4C 1C56BC700EFF837F")); - /* 256 bit keys */ - test_cipher(&nettle_aes256, - SHEX("0001020305060708 0A0B0C0D0F101112" - "14151617191A1B1C 1E1F202123242526"), - SHEX("834EADFCCAC7E1B30664B1ABA44815AB"), - SHEX("1946DABF6A03A2A2 C3D0B05080AED6FC")); + /* 256 bit keys */ + test_cipher2(&nettle_aes256, &nettle_unified_aes256, + SHEX("0001020305060708 0A0B0C0D0F101112" + "14151617191A1B1C 1E1F202123242526"), + SHEX("834EADFCCAC7E1B30664B1ABA44815AB"), + SHEX("1946DABF6A03A2A2 C3D0B05080AED6FC")); /* This test case has been problematic with the CBC test case */ - test_cipher(&nettle_aes256, - SHEX("8d ae 93 ff fc 78 c9 44" - "2a bd 0c 1e 68 bc a6 c7" - "05 c7 84 e3 5a a9 11 8b" - "d3 16 aa 54 9b 44 08 9e"), - SHEX("a5 ce 55 d4 21 15 a1 c6 4a a4 0c b2 ca a6 d1 37"), - /* In the cbc test, I once got the bad value - * "b2 a0 6c d2 2f df 7d 2c 26 d2 42 88 8f 20 74 a2" */ - SHEX("1f 94 fc 85 f2 36 21 06" - "4a ea e3 c9 cc 38 01 0e")); + test_cipher2(&nettle_aes256, &nettle_unified_aes256, + SHEX("8d ae 93 ff fc 78 c9 44" + "2a bd 0c 1e 68 bc a6 c7" + "05 c7 84 e3 5a a9 11 8b" + "d3 16 aa 54 9b 44 08 9e"), + SHEX("a5 ce 55 d4 21 15 a1 c6 4a a4 0c b2 ca a6 d1 37"), + /* In the cbc test, I once got the bad value + * "b2 a0 6c d2 2f df 7d 2c 26 d2 42 88 8f 20 74 a2" */ + SHEX("1f 94 fc 85 f2 36 21 06" + "4a ea e3 c9 cc 38 01 0e")); /* From draft NIST spec on AES modes. * @@ -141,17 +141,17 @@ test_main(void) "9a4b41ba738d6c72fb16691603c18e0e")); /* F.1.5 ECB-AES256-Encrypt */ - test_cipher(&nettle_aes256, - SHEX("603deb1015ca71be2b73aef0857d7781" - "1f352c073b6108d72d9810a30914dff4"), - SHEX("6bc1bee22e409f96e93d7e117393172a" - "ae2d8a571e03ac9c9eb76fac45af8e51" - "30c81c46a35ce411e5fbc1191a0a52ef" - "f69f2445df4f9b17ad2b417be66c3710"), - SHEX("f3eed1bdb5d2a03c064b5a7e3db181f8" - "591ccb10d410ed26dc5ba74a31362870" - "b6ed21b99ca6f4f9f153e7b1beafed1d" - "23304b7a39f9f3ff067d8d8f9e24ecc7")); + test_cipher2(&nettle_aes256, &nettle_unified_aes256, + SHEX("603deb1015ca71be2b73aef0857d7781" + "1f352c073b6108d72d9810a30914dff4"), + SHEX("6bc1bee22e409f96e93d7e117393172a" + "ae2d8a571e03ac9c9eb76fac45af8e51" + "30c81c46a35ce411e5fbc1191a0a52ef" + "f69f2445df4f9b17ad2b417be66c3710"), + SHEX("f3eed1bdb5d2a03c064b5a7e3db181f8" + "591ccb10d410ed26dc5ba74a31362870" + "b6ed21b99ca6f4f9f153e7b1beafed1d" + "23304b7a39f9f3ff067d8d8f9e24ecc7")); /* Test aes_invert_key with src != dst */ test_invert(SHEX("0001020305060708 0A0B0C0D0F101112"),