chiark / gitweb /
ec-bin (ec_binproj): Make curve setup faster.
[catacomb] / rijndael192.c
1 /* -*-c-*-
2  *
3  * $Id: rijndael192.c,v 1.2 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
31
32 #include <assert.h>
33 #include <stdio.h>
34
35 #include <mLib/bits.h>
36
37 #include "blkc.h"
38 #include "gcipher.h"
39 #include "rijndael.h"
40 #include "rijndael192.h"
41 #include "rijndael-base.h"
42
43 /*----- Main code ---------------------------------------------------------*/
44
45 /* --- @rijndael192_init@ --- *
46  *
47  * Arguments:   @rijndael_ctx *k@ = pointer to context to initialize
48  *              @const void *buf@ = pointer to buffer of key material
49  *              @size_t sz@ = size of the key material
50  *
51  * Returns:     ---
52  *
53  * Use:         Initializes a Rijndael context with a particular key.  This
54  *              implementation of Rijndael doesn't impose any particular
55  *              limits on the key size except that it must be multiple of 4
56  *              bytes long.  256 bits seems sensible, though.
57  */
58
59 void rijndael192_init(rijndael_ctx *k, const void *buf, size_t sz)
60 {
61   rijndael_setup(k, RIJNDAEL192_BLKSZ / 4, buf, sz);
62 }
63
64 /* --- @rijndael192_eblk@, @rijndael192_dblk@ --- *
65  *
66  * Arguments:   @const rijndael_ctx *k@ = pointer to Rijndael context
67  *              @const uint32 s[4]@ = pointer to source block
68  *              @uint32 d[4]@ = pointer to destination block
69  *
70  * Returns:     ---
71  *
72  * Use:         Low-level block encryption and decryption.
73  */
74
75 #define DO(what, t, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w) do {   \
76   aa = what(t, a, b, c, d) ^ *w++;                                      \
77   bb = what(t, b, c, d, e) ^ *w++;                                      \
78   cc = what(t, c, d, e, f) ^ *w++;                                      \
79   dd = what(t, d, e, f, a) ^ *w++;                                      \
80   ee = what(t, e, f, a, b) ^ *w++;                                      \
81   ff = what(t, f, a, b, c) ^ *w++;                                      \
82 } while (0)
83
84 #define UNDO(what, t, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w) do { \
85   aa = what(t, a, f, e, d) ^ *w++;                                      \
86   bb = what(t, b, a, f, e) ^ *w++;                                      \
87   cc = what(t, c, b, a, f) ^ *w++;                                      \
88   dd = what(t, d, c, b, a) ^ *w++;                                      \
89   ee = what(t, e, d, c, b) ^ *w++;                                      \
90   ff = what(t, f, e, d, c) ^ *w++;                                      \
91 } while (0)
92
93 void rijndael192_eblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
94 {
95   uint32 a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5];
96   uint32 aa, bb, cc, dd, ee, ff;
97   uint32 *w = k->w;
98
99   a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; e ^= *w++; f ^= *w++;
100   aa = a; bb = b; cc = c; dd = d; ee = e; ff = f;
101
102   switch (k->nr) {
103     case 14:
104       DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
105     case 13:
106       DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
107     case 12:
108     default:
109       DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
110       DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
111       DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
112       DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
113       DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
114       DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
115       DO(MIX, T, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
116       DO(MIX, T, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
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   }      
121   DO(SUB, S, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
122
123   dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; dst[4] = e; dst[5] = f;
124 }
125
126 void rijndael192_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst)
127 {
128   uint32 a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5];
129   uint32 aa, bb, cc, dd, ee, ff;
130   uint32 *w = k->wi;
131
132   a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; e ^= *w++; f ^= *w++;
133   aa = a; bb = b; cc = c; dd = d; ee = e; ff = f;
134
135   switch (k->nr) {
136     case 14:
137       UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
138     case 13:
139       UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
140     case 12:
141     default:
142       UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
143       UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
144       UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
145       UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
146       UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
147       UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
148       UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, a, b, c, d, e, f, w);
149       UNDO(MIX, TI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
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   }      
154   UNDO(SUB, SI, a, b, c, d, e, f, aa, bb, cc, dd, ee, ff, w);
155
156   dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; dst[4] = e; dst[5] = f;
157 }
158
159 BLKC_TEST(RIJNDAEL192, rijndael192)
160
161 /*----- That's all, folks -------------------------------------------------*/