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>
39 #include "square-tab.h"
41 /*----- Global variables --------------------------------------------------*/
43 const octet square_keysz
[] = { KSZ_RANGE
, SQUARE_KEYSZ
, 4, 16, 4 };
45 /*----- Constant tables ---------------------------------------------------*/
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
;
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @square_init@ --- *
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
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
67 void square_init(square_ctx
*k
, const void *buf
, size_t sz
)
73 uint32 kk
[SQUARE_KWORDS
];
75 /* --- Sort out the key size --- */
77 KSZ_ASSERT(square
, sz
);
80 /* --- Fetch the first key words out --- */
83 for (i
= 0; i
< nk
; i
++) {
89 /* --- Expand this material to fill the rest of the table --- */
95 uint32 w
= kk
[i
- nk
];
104 /* --- Make the encryption and decryption keys --- */
106 for (i
= 0; i
< nr
* 4; 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)]);
115 for (i
= 0; i
< nr
* 4; i
+= 4) {
117 for (j
= 0; j
< 4; j
++)
118 k
->wi
[i
+ j
] = kk
[jj
+ j
];
120 for (j
= 0; j
< 4; j
++)
121 k
->wi
[i
+ j
] = k
->w
[j
];
126 /* --- @square_eblk@, @square_dblk@ --- *
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
134 * Use: Low-level block encryption and decryption.
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)
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)])
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++; \
152 void square_eblk(const square_ctx
*k
, const uint32
*s
, uint32
*dst
)
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
;
158 a
^= *w
++; b
^= *w
++; c
^= *w
++; d
^= *w
++;
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
);
169 dst
[0] = a
; dst
[1] = b
; dst
[2] = c
; dst
[3] = d
;
172 void square_dblk(const square_ctx
*k
, const uint32
*s
, uint32
*dst
)
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
;
178 a
^= *w
++; b
^= *w
++; c
^= *w
++; d
^= *w
++;
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
);
189 dst
[0] = a
; dst
[1] = b
; dst
[2] = c
; dst
[3] = d
;
192 BLKC_TEST(SQUARE
, square
)
194 /*----- That's all, folks -------------------------------------------------*/