3 * The Square block cipher
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
33 #include <mLib/bits.h>
34 #include <mLib/macros.h>
41 /*----- Global variables --------------------------------------------------*/
43 const octet square_keysz
[] = { KSZ_RANGE
, SQUARE_KEYSZ
, 4, 16, 4 };
45 /*----- Constant tables ---------------------------------------------------*/
47 extern const octet square_s
[256], square_si
[256];
48 extern const uint32 square_t
[4][256], square_ti
[4][256];
49 extern const uint32 square_u
[4][256];
50 extern const octet square_rcon
[32];
57 #define RCON square_rcon
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @square_init@ --- *
63 * Arguments: @square_ctx *k@ = pointer to context to initialize
64 * @const void *buf@ = pointer to buffer of key material
65 * @size_t sz@ = size of the key material
69 * Use: Initializes a Square context with a particular key. Square
70 * keys must be a multiple of 32 bits long, and may be at most
74 void square_init(square_ctx
*k
, const void *buf
, size_t sz
)
80 uint32 kk
[SQUARE_KWORDS
];
82 /* --- Sort out the key size --- */
84 KSZ_ASSERT(square
, sz
);
87 /* --- Fetch the first key words out --- */
90 for (i
= 0; i
< nk
; i
++) {
96 /* --- GCC complains about an out-of-bounds subscript here --- *
98 * This is impossible. Thanks to @KSZ_ASSERT@, we know that @4 <= sz <=
99 * 16@ and hence @1 <= i <= nk <= 4@; but @SQUARE_KWORDS == 36@.
102 ww
= MUFFLE_WARNINGS_EXPR(GCC_WARNING("-Warray-bounds"), kk
[i
- 1]);
104 /* --- Expand this material to fill the rest of the table --- */
108 for (; i
< nw
; i
++) {
109 uint32 w
= kk
[i
- nk
];
118 /* --- Make the encryption and decryption keys --- */
120 for (i
= 0; i
< nr
* 4; i
++) {
122 k
->w
[i
] = (U
[0][U8(w
>> 0)] ^ U
[1][U8(w
>> 8)] ^
123 U
[2][U8(w
>> 16)] ^ U
[3][U8(w
>> 24)]);
129 for (i
= 0; i
< nr
* 4; i
+= 4) {
131 for (j
= 0; j
< 4; j
++)
132 k
->wi
[i
+ j
] = kk
[jj
+ j
];
134 for (j
= 0; j
< 4; j
++)
135 k
->wi
[i
+ j
] = k
->w
[j
];
140 /* --- @square_eblk@, @square_dblk@ --- *
142 * Arguments: @const square_ctx *k@ = pointer to Square context
143 * @const uint32 s[4]@ = pointer to source block
144 * @uint32 d[4]@ = pointer to destination block
148 * Use: Low-level block encryption and decryption.
151 #define SUB(s, sh, a, b, c, d) \
152 (s[U8((a) >> sh)] << 0 | s[U8((b) >> sh)] << 8 | \
153 s[U8((c) >> sh)] << 16 | s[U8((d) >> sh)] << 24)
155 #define MIX(t, sh, a, b, c, d) \
156 (t[0][U8((a) >> sh)] ^ t[1][U8((b) >> sh)] ^ \
157 t[2][U8((c) >> sh)] ^ t[3][U8((d) >> sh)])
159 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
160 aa = what(t, 0, a, b, c, d) ^ *w++; \
161 bb = what(t, 8, a, b, c, d) ^ *w++; \
162 cc = what(t, 16, a, b, c, d) ^ *w++; \
163 dd = what(t, 24, a, b, c, d) ^ *w++; \
166 void square_eblk(const square_ctx
*k
, const uint32
*s
, uint32
*dst
)
168 uint32 a
= s
[0], b
= s
[1], c
= s
[2], d
= s
[3];
169 uint32 aa
, bb
, cc
, dd
;
170 const uint32
*w
= k
->w
;
172 a
^= *w
++; b
^= *w
++; c
^= *w
++; d
^= *w
++;
174 DO(MIX
, T
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
175 DO(MIX
, T
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
176 DO(MIX
, T
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
177 DO(MIX
, T
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
178 DO(MIX
, T
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
179 DO(MIX
, T
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
180 DO(MIX
, T
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
181 DO(SUB
, S
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
183 dst
[0] = a
; dst
[1] = b
; dst
[2] = c
; dst
[3] = d
;
186 void square_dblk(const square_ctx
*k
, const uint32
*s
, uint32
*dst
)
188 uint32 a
= s
[0], b
= s
[1], c
= s
[2], d
= s
[3];
189 uint32 aa
, bb
, cc
, dd
;
190 const uint32
*w
= k
->wi
;
192 a
^= *w
++; b
^= *w
++; c
^= *w
++; d
^= *w
++;
194 DO(MIX
, TI
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
195 DO(MIX
, TI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
196 DO(MIX
, TI
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
197 DO(MIX
, TI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
198 DO(MIX
, TI
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
199 DO(MIX
, TI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
200 DO(MIX
, TI
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
201 DO(SUB
, SI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
203 dst
[0] = a
; dst
[1] = b
; dst
[2] = c
; dst
[3] = d
;
206 BLKC_TEST(SQUARE
, square
)
208 /*----- That's all, folks -------------------------------------------------*/