3 * $Id: square.c,v 1.3 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
41 #include "square-tab.h"
43 /*----- Global variables --------------------------------------------------*/
45 const octet square_keysz
[] = { KSZ_RANGE
, SQUARE_KEYSZ
, 4, 16, 4 };
47 /*----- Constant tables ---------------------------------------------------*/
49 static const octet S
[256] = SQUARE_S
, SI
[256] = SQUARE_SI
;
50 static const uint32 T
[4][256] = SQUARE_T
, TI
[4][256] = SQUARE_TI
;
51 static const uint32 U
[4][256] = SQUARE_U
;
52 static const octet rcon
[] = SQUARE_RCON
;
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @square_init@ --- *
58 * Arguments: @square_ctx *k@ = pointer to context to initialize
59 * @const void *buf@ = pointer to buffer of key material
60 * @size_t sz@ = size of the key material
64 * Use: Initializes a Square context with a particular key. Square
65 * keys must be a multiple of 32 bits long, and may be at most
69 void square_init(square_ctx
*k
, const void *buf
, size_t sz
)
75 uint32 kk
[SQUARE_KWORDS
];
77 /* --- Sort out the key size --- */
79 KSZ_ASSERT(square
, sz
);
82 /* --- Fetch the first key words out --- */
85 for (i
= 0; i
< nk
; i
++) {
91 /* --- Expand this material to fill the rest of the table --- */
97 uint32 w
= kk
[i
- nk
];
106 /* --- Make the encryption and decryption keys --- */
108 for (i
= 0; i
< nr
* 4; i
++) {
110 k
->w
[i
] = (U
[0][U8(w
>> 0)] ^ U
[1][U8(w
>> 8)] ^
111 U
[2][U8(w
>> 16)] ^ U
[3][U8(w
>> 24)]);
117 for (i
= 0; i
< nr
* 4; i
+= 4) {
119 for (j
= 0; j
< 4; j
++)
120 k
->wi
[i
+ j
] = kk
[jj
+ j
];
122 for (j
= 0; j
< 4; j
++)
123 k
->wi
[i
+ j
] = k
->w
[j
];
128 /* --- @square_eblk@, @square_dblk@ --- *
130 * Arguments: @const square_ctx *k@ = pointer to Square context
131 * @const uint32 s[4]@ = pointer to source block
132 * @uint32 d[4]@ = pointer to destination block
136 * Use: Low-level block encryption and decryption.
139 #define SUB(s, sh, a, b, c, d) \
140 (s[U8((a) >> sh)] << 0 | s[U8((b) >> sh)] << 8 | \
141 s[U8((c) >> sh)] << 16 | s[U8((d) >> sh)] << 24)
143 #define MIX(t, sh, a, b, c, d) \
144 (t[0][U8((a) >> sh)] ^ t[1][U8((b) >> sh)] ^ \
145 t[2][U8((c) >> sh)] ^ t[3][U8((d) >> sh)])
147 #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \
148 aa = what(t, 0, a, b, c, d) ^ *w++; \
149 bb = what(t, 8, a, b, c, d) ^ *w++; \
150 cc = what(t, 16, a, b, c, d) ^ *w++; \
151 dd = what(t, 24, a, b, c, d) ^ *w++; \
154 void square_eblk(const square_ctx
*k
, const uint32
*s
, uint32
*dst
)
156 uint32 a
= s
[0], b
= s
[1], c
= s
[2], d
= s
[3];
157 uint32 aa
, bb
, cc
, dd
;
158 const uint32
*w
= k
->w
;
160 a
^= *w
++; b
^= *w
++; c
^= *w
++; d
^= *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(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(SUB
, S
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
171 dst
[0] = a
; dst
[1] = b
; dst
[2] = c
; dst
[3] = d
;
174 void square_dblk(const square_ctx
*k
, const uint32
*s
, uint32
*dst
)
176 uint32 a
= s
[0], b
= s
[1], c
= s
[2], d
= s
[3];
177 uint32 aa
, bb
, cc
, dd
;
178 const uint32
*w
= k
->wi
;
180 a
^= *w
++; b
^= *w
++; c
^= *w
++; d
^= *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(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(SUB
, SI
, a
, b
, c
, d
, aa
, bb
, cc
, dd
, w
);
191 dst
[0] = a
; dst
[1] = b
; dst
[2] = c
; dst
[3] = d
;
194 BLKC_TEST(SQUARE
, square
)
196 /*----- That's all, folks -------------------------------------------------*/