Make tables of standard encryption schemes etc.
[u/mdw/catacomb] / rijndael192.c
1 /* -*-c-*-
2 *
3 * $Id: rijndael192.c,v 1.1 2001/05/07 17:32:03 mdw Exp $
4 *
5 * The Rijndael block cipher, 192-bit version
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: rijndael192.c,v $
33 * Revision 1.1 2001/05/07 17:32:03 mdw
34 * New Rijndael block sizes.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <assert.h>
41 #include <stdio.h>
42
43 #include <mLib/bits.h>
44
45 #include "blkc.h"
46 #include "gcipher.h"
47 #include "rijndael.h"
48 #include "rijndael192.h"
49 #include "rijndael-base.h"
50
51 /*----- Main code ---------------------------------------------------------*/
52
53 /* --- @rijndael192_init@ --- *
54 *
55 * Arguments: @rijndael_ctx *k@ = pointer to context to initialize
56 * @const void *buf@ = pointer to buffer of key material
57 * @size_t sz@ = size of the key material
58 *
59 * Returns: ---
60 *
61 * Use: Initializes a Rijndael context with a particular key. This
62 * implementation of Rijndael doesn't impose any particular
63 * limits on the key size except that it must be multiple of 4
64 * bytes long. 256 bits seems sensible, though.
65 */
66
67 void rijndael192_init(rijndael_ctx *k, const void *buf, size_t sz)
68 {
69 rijndael_setup(k, RIJNDAEL192_BLKSZ / 4, buf, sz);
70 }
71
72 /* --- @rijndael192_eblk@, @rijndael192_dblk@ --- *
73 *
74 * Arguments: @const rijndael_ctx *k@ = pointer to Rijndael context
75 * @const uint32 s[4]@ = pointer to source block
76 * @uint32 d[4]@ = pointer to destination block
77 *
78 * Returns: ---
79 *
80 * Use: Low-level block encryption and decryption.
81 */
82
83 #define DO(what, t, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w) do { \
84 aa = what(t, a, b, c, d) ^ *w++; \
85 bb = what(t, b, c, d, e) ^ *w++; \
86 cc = what(t, c, d, e, f) ^ *w++; \
87 dd = what(t, d, e, f, a) ^ *w++; \
88 ee = what(t, e, f, a, b) ^ *w++; \
89 ff = what(t, f, a, b, c) ^ *w++; \
90 } while (0)
91
92 #define UNDO(what, t, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w) do { \
93 aa = what(t, a, f, e, d) ^ *w++; \
94 bb = what(t, b, a, f, e) ^ *w++; \
95 cc = what(t, c, b, a, f) ^ *w++; \
96 dd = what(t, d, c, b, a) ^ *w++; \
97 ee = what(t, e, d, c, b) ^ *w++; \
98 ff = what(t, f, e, d, c) ^ *w++; \
99 } while (0)
100
101 void rijndael192_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
102 {
103 uint32 a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5];
104 uint32 aa, bb, cc, dd, ee, ff;
105 uint32 *w = k->w;
106
107 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; e ^= *w++; f ^= *w++;
108 aa = a; bb = b; cc = c; dd = d; ee = e; ff = f;
109
110 switch (k->nr) {
111 case 14:
112 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
113 case 13:
114 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
115 case 12:
116 default:
117 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
118 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
119 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
120 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
121 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
122 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
123 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
124 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
125 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
126 DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
127 DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
128 }
129 DO(SUB, S, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
130
131 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; dst[4] = e; dst[5] = f;
132 }
133
134 void rijndael192_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
135 {
136 uint32 a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5];
137 uint32 aa, bb, cc, dd, ee, ff;
138 uint32 *w = k->wi;
139
140 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; e ^= *w++; f ^= *w++;
141 aa = a; bb = b; cc = c; dd = d; ee = e; ff = f;
142
143 switch (k->nr) {
144 case 14:
145 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
146 case 13:
147 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
148 case 12:
149 default:
150 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
151 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
152 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
153 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
154 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
155 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
156 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
157 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
158 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
159 UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
160 UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
161 }
162 UNDO(SUB, SI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
163
164 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; dst[4] = e; dst[5] = f;
165 }
166
167 BLKC_TEST(RIJNDAEL192, rijndael192)
168
169 /*----- That's all, folks -------------------------------------------------*/