Rearrange the file tree.
[u/mdw/catacomb] / symm / rijndael256.c
diff --git a/symm/rijndael256.c b/symm/rijndael256.c
new file mode 100644 (file)
index 0000000..9fb7298
--- /dev/null
@@ -0,0 +1,159 @@
+/* -*-c-*-
+ *
+ * The Rijndael block cipher, 256-bit version
+ *
+ * (c) 2001 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb 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 Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <assert.h>
+#include <stdio.h>
+
+#include <mLib/bits.h>
+
+#include "blkc.h"
+#include "gcipher.h"
+#include "rijndael.h"
+#include "rijndael256.h"
+#include "rijndael-base.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @rijndael256_init@ --- *
+ *
+ * Arguments:  @rijndael_ctx *k@ = pointer to context to initialize
+ *             @const void *buf@ = pointer to buffer of key material
+ *             @size_t sz@ = size of the key material
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a Rijndael context with a particular key.  This
+ *             implementation of Rijndael doesn't impose any particular
+ *             limits on the key size except that it must be multiple of 4
+ *             bytes long.  256 bits seems sensible, though.
+ */
+
+void rijndael256_init(rijndael_ctx *k, const void *buf, size_t sz)
+{
+  rijndael_setup(k, RIJNDAEL256_BLKSZ / 4, buf, sz);
+}
+
+/* --- @rijndael256_eblk@, @rijndael256_dblk@ --- *
+ *
+ * Arguments:  @const rijndael_ctx *k@ = pointer to Rijndael context
+ *             @const uint32 s[4]@ = pointer to source block
+ *             @uint32 d[4]@ = pointer to destination block
+ *
+ * Returns:    ---
+ *
+ * Use:                Low-level block encryption and decryption.
+ */
+
+#define DO(what, t,                                                    \
+          aa, bb, cc, dd, ee, ff, gg, hh,                              \
+          a, b, c, d, e, f, g, h, w) do {                              \
+  aa = what(t, a, b, d, e) ^ *w++;                                     \
+  bb = what(t, b, c, e, f) ^ *w++;                                     \
+  cc = what(t, c, d, f, g) ^ *w++;                                     \
+  dd = what(t, d, e, g, h) ^ *w++;                                     \
+  ee = what(t, e, f, h, a) ^ *w++;                                     \
+  ff = what(t, f, g, a, b) ^ *w++;                                     \
+  gg = what(t, g, h, b, c) ^ *w++;                                     \
+  hh = what(t, h, a, c, d) ^ *w++;                                     \
+} while (0)
+
+#define UNDO(what, t,                                                  \
+            aa, bb, cc, dd, ee, ff, gg, hh,                            \
+            a, b, c, d, e, f, g, h, w) do {                            \
+  aa = what(t, a, h, f, e) ^ *w++;                                     \
+  bb = what(t, b, a, g, f) ^ *w++;                                     \
+  cc = what(t, c, b, h, g) ^ *w++;                                     \
+  dd = what(t, d, c, a, h) ^ *w++;                                     \
+  ee = what(t, e, d, b, a) ^ *w++;                                     \
+  ff = what(t, f, e, c, b) ^ *w++;                                     \
+  gg = what(t, g, f, d, c) ^ *w++;                                     \
+  hh = what(t, h, g, e, d) ^ *w++;                                     \
+} while (0)
+
+void rijndael256_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
+{
+  uint32 a = s[0], b = s[1], c = s[2], d = s[3];
+  uint32 e = s[4], f = s[5], g = s[6], h = s[7];
+  uint32 aa, bb, cc, dd, ee, ff, gg, hh;
+  const uint32 *w = k->w;
+
+  a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
+  e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++;
+
+  DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  DO(SUB, S, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+
+  dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
+  dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h;
+}
+
+void rijndael256_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
+{
+  uint32 a = s[0], b = s[1], c = s[2], d = s[3];
+  uint32 e = s[4], f = s[5], g = s[6], h = s[7];
+  uint32 aa, bb, cc, dd, ee, ff, gg, hh;
+  const uint32 *w = k->wi;
+
+  a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
+  e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++;
+
+  UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+  UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w);
+  UNDO(SUB, SI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w);
+
+  dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
+  dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h;
+}
+
+BLKC_TEST(RIJNDAEL256, rijndael256)
+
+/*----- That's all, folks -------------------------------------------------*/