2e8eb64a |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: rijndael256.c,v 1.2 2004/04/08 01:36:15 mdw Exp $ |
2e8eb64a |
4 | * |
5 | * The Rijndael block cipher, 256-bit version |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
45c0fd36 |
10 | /*----- Licensing notice --------------------------------------------------* |
2e8eb64a |
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. |
45c0fd36 |
18 | * |
2e8eb64a |
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. |
45c0fd36 |
23 | * |
2e8eb64a |
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 | |
2e8eb64a |
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 "rijndael256.h" |
41 | #include "rijndael-base.h" |
42 | |
43 | /*----- Main code ---------------------------------------------------------*/ |
44 | |
45 | /* --- @rijndael256_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 rijndael256_init(rijndael_ctx *k, const void *buf, size_t sz) |
60 | { |
61 | rijndael_setup(k, RIJNDAEL256_BLKSZ / 4, buf, sz); |
62 | } |
63 | |
64 | /* --- @rijndael256_eblk@, @rijndael256_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, \ |
76 | aa, bb, cc, dd, ee, ff, gg, hh, \ |
77 | a, b, c, d, e, f, g, h, w) do { \ |
78 | aa = what(t, a, b, d, e) ^ *w++; \ |
79 | bb = what(t, b, c, e, f) ^ *w++; \ |
80 | cc = what(t, c, d, f, g) ^ *w++; \ |
81 | dd = what(t, d, e, g, h) ^ *w++; \ |
82 | ee = what(t, e, f, h, a) ^ *w++; \ |
83 | ff = what(t, f, g, a, b) ^ *w++; \ |
84 | gg = what(t, g, h, b, c) ^ *w++; \ |
85 | hh = what(t, h, a, c, d) ^ *w++; \ |
86 | } while (0) |
87 | |
88 | #define UNDO(what, t, \ |
89 | aa, bb, cc, dd, ee, ff, gg, hh, \ |
90 | a, b, c, d, e, f, g, h, w) do { \ |
91 | aa = what(t, a, h, f, e) ^ *w++; \ |
92 | bb = what(t, b, a, g, f) ^ *w++; \ |
93 | cc = what(t, c, b, h, g) ^ *w++; \ |
94 | dd = what(t, d, c, a, h) ^ *w++; \ |
95 | ee = what(t, e, d, b, a) ^ *w++; \ |
96 | ff = what(t, f, e, c, b) ^ *w++; \ |
97 | gg = what(t, g, f, d, c) ^ *w++; \ |
98 | hh = what(t, h, g, e, d) ^ *w++; \ |
99 | } while (0) |
100 | |
101 | void rijndael256_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]; |
104 | uint32 e = s[4], f = s[5], g = s[6], h = s[7]; |
105 | uint32 aa, bb, cc, dd, ee, ff, gg, hh; |
78ec50fa |
106 | const uint32 *w = k->w; |
2e8eb64a |
107 | |
108 | a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; |
109 | e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++; |
110 | |
111 | DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
112 | DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
113 | DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
114 | DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
115 | DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
116 | DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
117 | DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
118 | DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
119 | DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
120 | DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
121 | DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
122 | DO(MIX, T, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
123 | DO(MIX, T, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
124 | DO(SUB, S, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
125 | |
126 | dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; |
127 | dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h; |
128 | } |
129 | |
130 | void rijndael256_dblk(const rijndael_ctx *k, const uint32 *s, uint32 *dst) |
131 | { |
132 | uint32 a = s[0], b = s[1], c = s[2], d = s[3]; |
133 | uint32 e = s[4], f = s[5], g = s[6], h = s[7]; |
134 | uint32 aa, bb, cc, dd, ee, ff, gg, hh; |
78ec50fa |
135 | const uint32 *w = k->wi; |
2e8eb64a |
136 | |
137 | a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; |
138 | e ^= *w++; f ^= *w++; g ^= *w++; h ^= *w++; |
139 | |
140 | UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
141 | UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
142 | UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
143 | UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
144 | UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
145 | UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
146 | UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
147 | UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
148 | UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
149 | UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
150 | UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
151 | UNDO(MIX, TI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
152 | UNDO(MIX, TI, aa, bb, cc, dd, ee, ff, gg, hh, a, b, c, d, e, f, g, h, w); |
153 | UNDO(SUB, SI, a, b, c, d, e, f, g, h, aa, bb, cc, dd, ee, ff, gg, hh, w); |
154 | |
155 | dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; |
156 | dst[4] = e; dst[5] = f; dst[6] = g; dst[7] = h; |
157 | } |
158 | |
159 | BLKC_TEST(RIJNDAEL256, rijndael256) |
160 | |
161 | /*----- That's all, folks -------------------------------------------------*/ |