Rearrange the file tree.
[u/mdw/catacomb] / symm / square.c
1 /* -*-c-*-
2 *
3 * The Square block cipher
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <assert.h>
31 #include <stdio.h>
32
33 #include <mLib/bits.h>
34
35 #include "blkc.h"
36 #include "gcipher.h"
37 #include "paranoia.h"
38 #include "square.h"
39 #include "square-tab.h"
40
41 /*----- Global variables --------------------------------------------------*/
42
43 const octet square_keysz[] = { KSZ_RANGE, SQUARE_KEYSZ, 4, 16, 4 };
44
45 /*----- Constant tables ---------------------------------------------------*/
46
47 static const octet S[256] = SQUARE_S, SI[256] = SQUARE_SI;
48 static const uint32 T[4][256] = SQUARE_T, TI[4][256] = SQUARE_TI;
49 static const uint32 U[4][256] = SQUARE_U;
50 static const octet rcon[] = SQUARE_RCON;
51
52 /*----- Main code ---------------------------------------------------------*/
53
54 /* --- @square_init@ --- *
55 *
56 * Arguments: @square_ctx *k@ = pointer to context to initialize
57 * @const void *buf@ = pointer to buffer of key material
58 * @size_t sz@ = size of the key material
59 *
60 * Returns: ---
61 *
62 * Use: Initializes a Square context with a particular key. Square
63 * keys must be a multiple of 32 bits long, and may be at most
64 * 128 bits.
65 */
66
67 void square_init(square_ctx *k, const void *buf, size_t sz)
68 {
69 unsigned nk, nr, nw;
70 unsigned i, j, jj;
71 const octet *p;
72 uint32 ww;
73 uint32 kk[SQUARE_KWORDS];
74
75 /* --- Sort out the key size --- */
76
77 KSZ_ASSERT(square, sz);
78 nk = sz / 4;
79
80 /* --- Fetch the first key words out --- */
81
82 p = buf;
83 for (i = 0; i < nk; i++) {
84 kk[i] = LOAD32_L(p);
85 p += 4;
86 }
87 nr = 8;
88
89 /* --- Expand this material to fill the rest of the table --- */
90
91 nw = (nr + 1) * 4;
92 ww = kk[i - 1];
93 p = rcon;
94 for (; i < nw; i++) {
95 uint32 w = kk[i - nk];
96 if (i % nk == 0) {
97 ww = ROR32(ww, 8);
98 w ^= ww ^ *p++;
99 } else
100 w ^= ww;
101 kk[i] = ww = w;
102 }
103
104 /* --- Make the encryption and decryption keys --- */
105
106 for (i = 0; i < nr * 4; i++) {
107 uint32 w = kk[i];
108 k->w[i] = (U[0][U8(w >> 0)] ^ U[1][U8(w >> 8)] ^
109 U[2][U8(w >> 16)] ^ U[3][U8(w >> 24)]);
110 }
111 for (; i < nw; i++)
112 k->w[i] = kk[i];
113
114 jj = nw;
115 for (i = 0; i < nr * 4; i += 4) {
116 jj -= 4;
117 for (j = 0; j < 4; j++)
118 k->wi[i + j] = kk[jj + j];
119 }
120 for (j = 0; j < 4; j++)
121 k->wi[i + j] = k->w[j];
122
123 BURN(kk);
124 }
125
126 /* --- @square_eblk@, @square_dblk@ --- *
127 *
128 * Arguments: @const square_ctx *k@ = pointer to Square context
129 * @const uint32 s[4]@ = pointer to source block
130 * @uint32 d[4]@ = pointer to destination block
131 *
132 * Returns: ---
133 *
134 * Use: Low-level block encryption and decryption.
135 */
136
137 #define SUB(s, sh, a, b, c, d) \
138 (s[U8((a) >> sh)] << 0 | s[U8((b) >> sh)] << 8 | \
139 s[U8((c) >> sh)] << 16 | s[U8((d) >> sh)] << 24)
140
141 #define MIX(t, sh, a, b, c, d) \
142 (t[0][U8((a) >> sh)] ^ t[1][U8((b) >> sh)] ^ \
143 t[2][U8((c) >> sh)] ^ t[3][U8((d) >> sh)])
144
145 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
146 aa = what(t, 0, a, b, c, d) ^ *w++; \
147 bb = what(t, 8, a, b, c, d) ^ *w++; \
148 cc = what(t, 16, a, b, c, d) ^ *w++; \
149 dd = what(t, 24, a, b, c, d) ^ *w++; \
150 } while (0)
151
152 void square_eblk(const square_ctx *k, const uint32 *s, uint32 *dst)
153 {
154 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
155 uint32 aa, bb, cc, dd;
156 const uint32 *w = k->w;
157
158 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
159
160 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
161 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
162 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
163 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
164 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
165 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
166 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
167 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
168
169 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
170 }
171
172 void square_dblk(const square_ctx *k, const uint32 *s, uint32 *dst)
173 {
174 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
175 uint32 aa, bb, cc, dd;
176 const uint32 *w = k->wi;
177
178 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
179
180 DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
181 DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
182 DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
183 DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
184 DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
185 DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
186 DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
187 DO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
188
189 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
190 }
191
192 BLKC_TEST(SQUARE, square)
193
194 /*----- That's all, folks -------------------------------------------------*/