3 * $Id: square.c,v 1.2 2001/05/07 15:44:02 mdw Exp $
5 * The Square block cipher
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2001/05/07 15:44:02 mdw
34 * Simplify implementation.
36 * Revision 1.1 2000/07/15 20:51:58 mdw
41 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/bits.h>
52 #include "square-tab.h"
54 /*----- Global variables --------------------------------------------------*/
56 const octet square_keysz[] = { KSZ_RANGE, SQUARE_KEYSZ, 4, 16, 4 };
58 /*----- Constant tables ---------------------------------------------------*/
60 static const octet S[256] = SQUARE_S, SI[256] = SQUARE_SI;
61 static const uint32 T[4][256] = SQUARE_T, TI[4][256] = SQUARE_TI;
62 static const uint32 U[4][256] = SQUARE_U;
63 static const octet rcon[] = SQUARE_RCON;
65 /*----- Main code ---------------------------------------------------------*/
67 /* --- @square_init@ --- *
69 * Arguments: @square_ctx *k@ = pointer to context to initialize
70 * @const void *buf@ = pointer to buffer of key material
71 * @size_t sz@ = size of the key material
75 * Use: Initializes a Square context with a particular key. Square
76 * keys must be a multiple of 32 bits long, and may be at most
80 void square_init(square_ctx *k, const void *buf, size_t sz)
86 uint32 kk[SQUARE_KWORDS];
88 /* --- Sort out the key size --- */
90 KSZ_ASSERT(square, sz);
93 /* --- Fetch the first key words out --- */
96 for (i = 0; i < nk; i++) {
102 /* --- Expand this material to fill the rest of the table --- */
107 for (; i < nw; i++) {
108 uint32 w = kk[i - nk];
117 /* --- Make the encryption and decryption keys --- */
119 for (i = 0; i < nr * 4; i++) {
121 k->w[i] = (U[0][U8(w >> 0)] ^ U[1][U8(w >> 8)] ^
122 U[2][U8(w >> 16)] ^ U[3][U8(w >> 24)]);
128 for (i = 0; i < nr * 4; i += 4) {
130 for (j = 0; j < 4; j++)
131 k->wi[i + j] = kk[jj + j];
133 for (j = 0; j < 4; j++)
134 k->wi[i + j] = k->w[j];
139 /* --- @square_eblk@, @square_dblk@ --- *
141 * Arguments: @const square_ctx *k@ = pointer to Square context
142 * @const uint32 s[4]@ = pointer to source block
143 * @uint32 d[4]@ = pointer to destination block
147 * Use: Low-level block encryption and decryption.
150 #define SUB(s, sh, a, b, c, d) \
151 (s[U8((a) >> sh)] << 0 | s[U8((b) >> sh)] << 8 | \
152 s[U8((c) >> sh)] << 16 | s[U8((d) >> sh)] << 24)
154 #define MIX(t, sh, a, b, c, d) \
155 (t[0][U8((a) >> sh)] ^ t[1][U8((b) >> sh)] ^ \
156 t[2][U8((c) >> sh)] ^ t[3][U8((d) >> sh)])
158 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
159 aa = what(t, 0, a, b, c, d) ^ *w++; \
160 bb = what(t, 8, a, b, c, d) ^ *w++; \
161 cc = what(t, 16, a, b, c, d) ^ *w++; \
162 dd = what(t, 24, a, b, c, d) ^ *w++; \
165 void square_eblk(const square_ctx *k, const uint32 *s, uint32 *dst)
167 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
168 uint32 aa, bb, cc, dd;
171 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
173 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
174 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
175 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
176 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
177 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
178 DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w);
179 DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w);
180 DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w);
182 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
185 void square_dblk(const square_ctx *k, const uint32 *s, uint32 *dst)
187 uint32 a = s[0], b = s[1], c = s[2], d = s[3];
188 uint32 aa, bb, cc, dd;
191 a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++;
193 DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
194 DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
195 DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
196 DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
197 DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
198 DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w);
199 DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w);
200 DO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w);
202 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
205 BLKC_TEST(SQUARE, square)
207 /*----- That's all, folks -------------------------------------------------*/