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>
40 /*----- Global variables --------------------------------------------------*/
42 const octet square_keysz
[] = { KSZ_RANGE
, SQUARE_KEYSZ
, 4, 16, 4 };
44 /*----- Constant tables ---------------------------------------------------*/
46 extern const octet square_s
[256], square_si
[256];
47 extern const uint32 square_t
[4][256], square_ti
[4][256];
48 extern const uint32 square_u
[4][256];
49 extern const octet square_rcon
[32];
56 #define RCON square_rcon
58 /*----- Main code ---------------------------------------------------------*/
60 /* --- @square_init@ --- *
62 * Arguments: @square_ctx *k@ = pointer to context to initialize
63 * @const void *buf@ = pointer to buffer of key material
64 * @size_t sz@ = size of the key material
68 * Use: Initializes a Square context with a particular key. Square
69 * keys must be a multiple of 32 bits long, and may be at most
73 void square_init(square_ctx
*k
, const void *buf
, size_t sz
)
79 uint32 kk
[SQUARE_KWORDS
];
81 /* --- Sort out the key size --- */
83 KSZ_ASSERT(square
, sz
);
86 /* --- Fetch the first key words out --- */
89 for (i
= 0; i
< nk
; i
++) {
95 /* --- Expand this material to fill the rest of the table --- */
100 for (; i
< nw
; i
++) {
101 uint32 w
= kk
[i
- nk
];
110 /* --- Make the encryption and decryption keys --- */
112 for (i
= 0; i
< nr
* 4; i
++) {
114 k
->w
[i
] = (U
[0][U8(w
>> 0)] ^ U
[1][U8(w
>> 8)] ^
115 U
[2][U8(w
>> 16)] ^ U
[3][U8(w
>> 24)]);
121 for (i
= 0; i
< nr
* 4; i
+= 4) {
123 for (j
= 0; j
< 4; j
++)
124 k
->wi
[i
+ j
] = kk
[jj
+ j
];
126 for (j
= 0; j
< 4; j
++)
127 k
->wi
[i
+ j
] = k
->w
[j
];
132 /* --- @square_eblk@, @square_dblk@ --- *
134 * Arguments: @const square_ctx *k@ = pointer to Square context
135 * @const uint32 s[4]@ = pointer to source block
136 * @uint32 d[4]@ = pointer to destination block
140 * Use: Low-level block encryption and decryption.
143 #define SUB(s, sh, a, b, c, d) \
144 (s[U8((a) >> sh)] << 0 | s[U8((b) >> sh)] << 8 | \
145 s[U8((c) >> sh)] << 16 | s[U8((d) >> sh)] << 24)
147 #define MIX(t, sh, a, b, c, d) \
148 (t[0][U8((a) >> sh)] ^ t[1][U8((b) >> sh)] ^ \
149 t[2][U8((c) >> sh)] ^ t[3][U8((d) >> sh)])
151 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
152 aa = what(t, 0, a, b, c, d) ^ *w++; \
153 bb = what(t, 8, a, b, c, d) ^ *w++; \
154 cc = what(t, 16, a, b, c, d) ^ *w++; \
155 dd = what(t, 24, a, b, c, d) ^ *w++; \
158 void square_eblk(const square_ctx
*k
, const uint32
*s
, uint32
*dst
)
160 uint32 a
= s
[0], b
= s
[1], c
= s
[2], d
= s
[3];
161 uint32 aa
, bb
, cc
, dd
;
162 const uint32
*w
= k
->w
;
164 a
^= *w
++; b
^= *w
++; c
^= *w
++; d
^= *w
++;
166 DO(MIX
, T
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
167 DO(MIX
, T
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
168 DO(MIX
, T
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
169 DO(MIX
, T
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
170 DO(MIX
, T
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
171 DO(MIX
, T
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
172 DO(MIX
, T
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
173 DO(SUB
, S
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
175 dst
[0] = a
; dst
[1] = b
; dst
[2] = c
; dst
[3] = d
;
178 void square_dblk(const square_ctx
*k
, const uint32
*s
, uint32
*dst
)
180 uint32 a
= s
[0], b
= s
[1], c
= s
[2], d
= s
[3];
181 uint32 aa
, bb
, cc
, dd
;
182 const uint32
*w
= k
->wi
;
184 a
^= *w
++; b
^= *w
++; c
^= *w
++; d
^= *w
++;
186 DO(MIX
, TI
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
187 DO(MIX
, TI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
188 DO(MIX
, TI
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
189 DO(MIX
, TI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
190 DO(MIX
, TI
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
191 DO(MIX
, TI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
192 DO(MIX
, TI
, aa
, bb
, cc
, dd
, a
, b
, c
, d
, w
);
193 DO(SUB
, SI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
195 dst
[0] = a
; dst
[1] = b
; dst
[2] = c
; dst
[3] = d
;
198 BLKC_TEST(SQUARE
, square
)
200 /*----- That's all, folks -------------------------------------------------*/