From dc2227ac29e5fe56c445dd4499dadd1af0441750 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Tue, 25 Jun 2013 22:19:36 +0200
Subject: [PATCH] New interface for AES-192.

---
 ChangeLog                |  2 ++
 Makefile.in              |  2 ++
 aes-decrypt.c            | 10 +++++++
 aes-encrypt.c            | 10 +++++++
 aes-meta.c               |  3 ---
 aes.h                    | 28 +++++++++++++++++++-
 aes192-meta.c            | 57 ++++++++++++++++++++++++++++++++++++++++
 aes192-set-decrypt-key.c | 46 ++++++++++++++++++++++++++++++++
 aes192-set-encrypt-key.c | 38 +++++++++++++++++++++++++++
 nettle-internal.c        |  2 ++
 nettle-internal.h        |  1 +
 testsuite/aes-test.c     | 34 +++++++++++-------------
 12 files changed, 211 insertions(+), 22 deletions(-)
 create mode 100644 aes192-meta.c
 create mode 100644 aes192-set-decrypt-key.c
 create mode 100644 aes192-set-encrypt-key.c

diff --git a/ChangeLog b/ChangeLog
index 1306b308..31a3dd4f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -16,6 +16,8 @@
 	* 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 99eb564f..e803c328 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -67,6 +67,8 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.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 \
+		 aes192-set-encrypt-key.c aes192-set-decrypt-key.c \
+		 aes192-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 e8696bf5..9ea0e238 100644
--- a/aes-decrypt.c
+++ b/aes-decrypt.c
@@ -355,3 +355,13 @@ aes128_decrypt(const struct aes128_ctx *ctx,
   _aes_decrypt(_AES128_ROUNDS, ctx->keys, &_aes_decrypt_table,
 	       length, dst, src);
 }
+
+void
+aes192_decrypt(const struct aes192_ctx *ctx,
+	       size_t length, uint8_t *dst,
+	       const uint8_t *src)
+{
+  assert(!(length % AES_BLOCK_SIZE) );
+  _aes_decrypt(_AES192_ROUNDS, ctx->keys, &_aes_decrypt_table,
+	       length, dst, src);
+}
diff --git a/aes-encrypt.c b/aes-encrypt.c
index 643276af..c4e3713e 100644
--- a/aes-encrypt.c
+++ b/aes-encrypt.c
@@ -53,3 +53,13 @@ aes128_encrypt(const struct aes128_ctx *ctx,
   _aes_encrypt(_AES128_ROUNDS, ctx->keys, &_aes_encrypt_table,
 	       length, dst, src);
 }
+
+void
+aes192_encrypt(const struct aes192_ctx *ctx,
+	       size_t length, uint8_t *dst,
+	       const uint8_t *src)
+{
+  assert(!(length % AES_BLOCK_SIZE) );
+  _aes_encrypt(_AES192_ROUNDS, ctx->keys, &_aes_encrypt_table,
+	       length, dst, src);
+}
diff --git a/aes-meta.c b/aes-meta.c
index 3db85f67..6bce5a42 100644
--- a/aes-meta.c
+++ b/aes-meta.c
@@ -28,8 +28,5 @@
 
 #include "aes.h"
 
-const struct nettle_cipher nettle_aes192
-= _NETTLE_CIPHER_SEP(aes, AES, 192);
-
 const struct nettle_cipher nettle_aes256
 = _NETTLE_CIPHER_SEP(aes, AES, 256);
diff --git a/aes.h b/aes.h
index 74b3fd67..ca9976e5 100644
--- a/aes.h
+++ b/aes.h
@@ -5,7 +5,7 @@
 
 /* nettle, low-level cryptographics library
  *
- * Copyright (C) 2001 Niels Möller
+ * Copyright (C) 2001, 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
@@ -43,6 +43,11 @@ extern "C" {
 #define aes128_invert_key nettle_aes128invert_key
 #define aes128_encrypt nettle_aes128encrypt
 #define aes128_decrypt nettle_aes128decrypt
+#define aes192_set_encrypt_key nettle_aes192set_encrypt_key
+#define aes192_set_decrypt_key nettle_aes192set_decrypt_key
+#define aes192_invert_key nettle_aes192invert_key
+#define aes192_encrypt nettle_aes192encrypt
+#define aes192_decrypt nettle_aes192decrypt
 
 #define AES_BLOCK_SIZE 16
 
@@ -110,6 +115,27 @@ aes128_decrypt(const struct aes128_ctx *ctx,
 	       size_t length, uint8_t *dst,
 	       const uint8_t *src);
 
+struct aes192_ctx
+{
+  uint32_t keys[4 * (_AES192_ROUNDS + 1)];
+};
+
+void
+aes192_set_encrypt_key(struct aes192_ctx *ctx, const uint8_t *key);
+void
+aes192_set_decrypt_key(struct aes192_ctx *ctx, const uint8_t *key);
+void
+aes192_invert_key(struct aes192_ctx *dst,
+		  const struct aes192_ctx *src);
+void
+aes192_encrypt(const struct aes192_ctx *ctx,
+	       size_t length, uint8_t *dst,
+	       const uint8_t *src);
+void
+aes192_decrypt(const struct aes192_ctx *ctx,
+	       size_t length, uint8_t *dst,
+	       const uint8_t *src);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/aes192-meta.c b/aes192-meta.c
new file mode 100644
index 00000000..0ee0c1aa
--- /dev/null
+++ b/aes192-meta.c
@@ -0,0 +1,57 @@
+/* aes192-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 aes192_set_encrypt_key_wrapper;
+static nettle_set_key_func aes192_set_decrypt_key_wrapper;
+
+static void
+aes192_set_encrypt_key_wrapper (void *ctx, size_t length, const uint8_t *key)
+{
+  assert (length == AES192_KEY_SIZE);
+  aes192_set_encrypt_key (ctx, key);
+}
+
+static void
+aes192_set_decrypt_key_wrapper (void *ctx, size_t length, const uint8_t *key)
+{
+  assert (length == AES192_KEY_SIZE);
+  aes192_set_decrypt_key (ctx, key);
+}
+
+const struct nettle_cipher nettle_aes192 =
+  { "aes192", sizeof(struct aes192_ctx),
+    AES_BLOCK_SIZE, AES192_KEY_SIZE,
+    aes192_set_encrypt_key_wrapper,
+    aes192_set_decrypt_key_wrapper,
+    (nettle_crypt_func *) aes192_encrypt,
+    (nettle_crypt_func *) aes192_decrypt
+  };
diff --git a/aes192-set-decrypt-key.c b/aes192-set-decrypt-key.c
new file mode 100644
index 00000000..496bee69
--- /dev/null
+++ b/aes192-set-decrypt-key.c
@@ -0,0 +1,46 @@
+/* aes192-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
+aes192_invert_key (struct aes192_ctx *dst, const struct aes192_ctx *src)
+{
+  _aes_invert (_AES192_ROUNDS, dst->keys, src->keys); 
+}
+
+void
+aes192_set_decrypt_key(struct aes192_ctx *ctx, const uint8_t *key)
+{
+  aes192_set_encrypt_key (ctx, key);
+  aes192_invert_key (ctx, ctx);
+}
diff --git a/aes192-set-encrypt-key.c b/aes192-set-encrypt-key.c
new file mode 100644
index 00000000..53bf3678
--- /dev/null
+++ b/aes192-set-encrypt-key.c
@@ -0,0 +1,38 @@
+/* aes192-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
+aes192_set_encrypt_key(struct aes192_ctx *ctx, const uint8_t *key)
+{
+  _aes_set_key (_AES192_ROUNDS, AES192_KEY_SIZE / 4, ctx->keys, key);
+}
diff --git a/nettle-internal.c b/nettle-internal.c
index e5a7271d..e258eb44 100644
--- a/nettle-internal.c
+++ b/nettle-internal.c
@@ -117,3 +117,5 @@ nettle_gcm_aes256 = _NETTLE_AEAD(gcm, GCM, aes, 256);
 const struct nettle_cipher nettle_unified_aes128
 = _NETTLE_CIPHER_SEP(aes, AES, 128);
 
+const struct nettle_cipher nettle_unified_aes192
+= _NETTLE_CIPHER_SEP(aes, AES, 192);
diff --git a/nettle-internal.h b/nettle-internal.h
index ec26fe3c..651ff4c0 100644
--- a/nettle-internal.h
+++ b/nettle-internal.h
@@ -65,6 +65,7 @@ extern const struct nettle_cipher nettle_salsa20;
 extern const struct nettle_cipher nettle_salsa20r12;
 
 extern const struct nettle_cipher nettle_unified_aes128;
+extern const struct nettle_cipher nettle_unified_aes192;
 
 /* 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 1304e8b0..14e20e4e 100644
--- a/testsuite/aes-test.c
+++ b/testsuite/aes-test.c
@@ -84,15 +84,13 @@ test_main(void)
 	       SHEX("CE52AF650D088CA5 59425223F4D32694"));
 
   /* 192 bit keys */
-  
-  test_cipher(&nettle_aes192, 
-	      SHEX("0001020305060708 0A0B0C0D0F101112"
-		   "14151617191A1B1C"),
-	      SHEX("2D33EEF2C0430A8A 9EBF45E809C40BB6"),
-	      SHEX("DFF4945E0336DF4C 1C56BC700EFF837F"));
+  test_cipher2(&nettle_aes192, &nettle_unified_aes192, 
+	       SHEX("0001020305060708 0A0B0C0D0F101112"
+		    "14151617191A1B1C"),
+	       SHEX("2D33EEF2C0430A8A 9EBF45E809C40BB6"),
+	       SHEX("DFF4945E0336DF4C 1C56BC700EFF837F"));
 
-  /* 256 bit keys */
-  
+  /* 256 bit keys */  
   test_cipher(&nettle_aes256,
 	      SHEX("0001020305060708 0A0B0C0D0F101112"
 		   "14151617191A1B1C 1E1F202123242526"),
@@ -131,16 +129,16 @@ test_main(void)
 
   /* F.1.3 ECB-AES192-Encrypt */
 
-  test_cipher(&nettle_aes192,
-	      SHEX("8e73b0f7da0e6452c810f32b809079e5 62f8ead2522c6b7b"),
-	      SHEX("6bc1bee22e409f96e93d7e117393172a"
-		   "ae2d8a571e03ac9c9eb76fac45af8e51"
-		   "30c81c46a35ce411e5fbc1191a0a52ef"
-		   "f69f2445df4f9b17ad2b417be66c3710"),
-	      SHEX("bd334f1d6e45f25ff712a214571fa5cc"
-		   "974104846d0ad3ad7734ecb3ecee4eef"
-		   "ef7afd2270e2e60adce0ba2face6444e"
-		   "9a4b41ba738d6c72fb16691603c18e0e"));
+  test_cipher2(&nettle_aes192, &nettle_unified_aes192, 
+	       SHEX("8e73b0f7da0e6452c810f32b809079e5 62f8ead2522c6b7b"),
+	       SHEX("6bc1bee22e409f96e93d7e117393172a"
+		    "ae2d8a571e03ac9c9eb76fac45af8e51"
+		    "30c81c46a35ce411e5fbc1191a0a52ef"
+		    "f69f2445df4f9b17ad2b417be66c3710"),
+	       SHEX("bd334f1d6e45f25ff712a214571fa5cc"
+		    "974104846d0ad3ad7734ecb3ecee4eef"
+		    "ef7afd2270e2e60adce0ba2face6444e"
+		    "9a4b41ba738d6c72fb16691603c18e0e"));
 
   /* F.1.5 ECB-AES256-Encrypt */
   test_cipher(&nettle_aes256,
-- 
GitLab