1 /* $NetBSD: cast128.c,v 1.8 2005/12/11 12:20:52 christos Exp $ */
2 /* $OpenBSD: cast.c,v 1.2 2000/06/06 06:49:47 deraadt Exp $ */
6 * Written by Steve Reid <sreid@sea-to-sky.net>
7 * 100% Public Domain - no warranty
11 #include <sys/cdefs.h>
12 __KERNEL_RCSID(0, "$NetBSD: cast128.c,v 1.8 2005/12/11 12:20:52 christos Exp $");
14 #include <sys/types.h>
15 #include <crypto/cast128/cast128.h>
16 #include <crypto/cast128/cast128sb.h>
18 /* Macros to access 8-bit bytes out of a 32-bit word */
19 #define U_INT8_Ta(x) ( (u_int8_t) (x>>24) )
20 #define U_INT8_Tb(x) ( (u_int8_t) ((x>>16)&255) )
21 #define U_INT8_Tc(x) ( (u_int8_t) ((x>>8)&255) )
22 #define U_INT8_Td(x) ( (u_int8_t) ((x)&255) )
24 /* Circular left shift */
25 #define ROL(x, n) ( ((x)<<(n)) | ((x)>>(32-(n))) )
27 /* CAST-128 uses three different round functions */
29 t = ROL(key->xkey[i] + r, key->xkey[i+16]); \
30 l ^= ((cast_sbox1[U_INT8_Ta(t)] ^ cast_sbox2[U_INT8_Tb(t)]) - \
31 cast_sbox3[U_INT8_Tc(t)]) + cast_sbox4[U_INT8_Td(t)];
33 t = ROL(key->xkey[i] ^ r, key->xkey[i+16]); \
34 l ^= ((cast_sbox1[U_INT8_Ta(t)] - cast_sbox2[U_INT8_Tb(t)]) + \
35 cast_sbox3[U_INT8_Tc(t)]) ^ cast_sbox4[U_INT8_Td(t)];
37 t = ROL(key->xkey[i] - r, key->xkey[i+16]); \
38 l ^= ((cast_sbox1[U_INT8_Ta(t)] + cast_sbox2[U_INT8_Tb(t)]) ^ \
39 cast_sbox3[U_INT8_Tc(t)]) - cast_sbox4[U_INT8_Td(t)];
42 /***** Encryption Function *****/
44 void cast128_encrypt(const cast128_key
* key
, const u_int8_t
* inblock
,
49 /* Get inblock into l,r */
50 l
= ((u_int32_t
)inblock
[0] << 24) | ((u_int32_t
)inblock
[1] << 16) |
51 ((u_int32_t
)inblock
[2] << 8) | (u_int32_t
)inblock
[3];
52 r
= ((u_int32_t
)inblock
[4] << 24) | ((u_int32_t
)inblock
[5] << 16) |
53 ((u_int32_t
)inblock
[6] << 8) | (u_int32_t
)inblock
[7];
67 /* Only do full 16 rounds if key length > 80 bits */
68 if (key
->rounds
> 12) {
74 /* Put l,r into outblock */
75 outblock
[0] = U_INT8_Ta(r
);
76 outblock
[1] = U_INT8_Tb(r
);
77 outblock
[2] = U_INT8_Tc(r
);
78 outblock
[3] = U_INT8_Td(r
);
79 outblock
[4] = U_INT8_Ta(l
);
80 outblock
[5] = U_INT8_Tb(l
);
81 outblock
[6] = U_INT8_Tc(l
);
82 outblock
[7] = U_INT8_Td(l
);
88 /***** Decryption Function *****/
90 void cast128_decrypt(const cast128_key
* key
, const u_int8_t
* inblock
,
95 /* Get inblock into l,r */
96 r
= ((u_int32_t
)inblock
[0] << 24) | ((u_int32_t
)inblock
[1] << 16) |
97 ((u_int32_t
)inblock
[2] << 8) | (u_int32_t
)inblock
[3];
98 l
= ((u_int32_t
)inblock
[4] << 24) | ((u_int32_t
)inblock
[5] << 16) |
99 ((u_int32_t
)inblock
[6] << 8) | (u_int32_t
)inblock
[7];
101 /* Only do full 16 rounds if key length > 80 bits */
102 if (key
->rounds
> 12) {
120 /* Put l,r into outblock */
121 outblock
[0] = U_INT8_Ta(l
);
122 outblock
[1] = U_INT8_Tb(l
);
123 outblock
[2] = U_INT8_Tc(l
);
124 outblock
[3] = U_INT8_Td(l
);
125 outblock
[4] = U_INT8_Ta(r
);
126 outblock
[5] = U_INT8_Tb(r
);
127 outblock
[6] = U_INT8_Tc(r
);
128 outblock
[7] = U_INT8_Td(r
);
134 /***** Key Schedual *****/
136 void cast128_setkey(cast128_key
* key
, const u_int8_t
* rawkey
, int keybytes
)
138 u_int32_t t
[4], z
[4], x
[4];
141 /* Set number of rounds to 12 or 16, depending on key length */
142 key
->rounds
= (keybytes
<= 10 ? 12 : 16);
144 /* Copy key to workspace x */
145 for (i
= 0; i
< 4; i
++) {
147 t
[i
] = z
[i
] = 0; /* XXX gcc */
148 if ((i
*4+0) < keybytes
) x
[i
] = (u_int32_t
)rawkey
[i
*4+0] << 24;
149 if ((i
*4+1) < keybytes
) x
[i
] |= (u_int32_t
)rawkey
[i
*4+1] << 16;
150 if ((i
*4+2) < keybytes
) x
[i
] |= (u_int32_t
)rawkey
[i
*4+2] << 8;
151 if ((i
*4+3) < keybytes
) x
[i
] |= (u_int32_t
)rawkey
[i
*4+3];
153 /* Generate 32 subkeys, four at a time */
154 for (i
= 0; i
< 32; i
+=4) {
157 t
[0] = z
[0] = x
[0] ^ cast_sbox5
[U_INT8_Tb(x
[3])] ^
158 cast_sbox6
[U_INT8_Td(x
[3])] ^ cast_sbox7
[U_INT8_Ta(x
[3])] ^
159 cast_sbox8
[U_INT8_Tc(x
[3])] ^ cast_sbox7
[U_INT8_Ta(x
[2])];
160 t
[1] = z
[1] = x
[2] ^ cast_sbox5
[U_INT8_Ta(z
[0])] ^
161 cast_sbox6
[U_INT8_Tc(z
[0])] ^ cast_sbox7
[U_INT8_Tb(z
[0])] ^
162 cast_sbox8
[U_INT8_Td(z
[0])] ^ cast_sbox8
[U_INT8_Tc(x
[2])];
163 t
[2] = z
[2] = x
[3] ^ cast_sbox5
[U_INT8_Td(z
[1])] ^
164 cast_sbox6
[U_INT8_Tc(z
[1])] ^ cast_sbox7
[U_INT8_Tb(z
[1])] ^
165 cast_sbox8
[U_INT8_Ta(z
[1])] ^ cast_sbox5
[U_INT8_Tb(x
[2])];
166 t
[3] = z
[3] = x
[1] ^ cast_sbox5
[U_INT8_Tc(z
[2])] ^
167 cast_sbox6
[U_INT8_Tb(z
[2])] ^ cast_sbox7
[U_INT8_Td(z
[2])] ^
168 cast_sbox8
[U_INT8_Ta(z
[2])] ^ cast_sbox6
[U_INT8_Td(x
[2])];
171 t
[0] = x
[0] = z
[2] ^ cast_sbox5
[U_INT8_Tb(z
[1])] ^
172 cast_sbox6
[U_INT8_Td(z
[1])] ^ cast_sbox7
[U_INT8_Ta(z
[1])] ^
173 cast_sbox8
[U_INT8_Tc(z
[1])] ^ cast_sbox7
[U_INT8_Ta(z
[0])];
174 t
[1] = x
[1] = z
[0] ^ cast_sbox5
[U_INT8_Ta(x
[0])] ^
175 cast_sbox6
[U_INT8_Tc(x
[0])] ^ cast_sbox7
[U_INT8_Tb(x
[0])] ^
176 cast_sbox8
[U_INT8_Td(x
[0])] ^ cast_sbox8
[U_INT8_Tc(z
[0])];
177 t
[2] = x
[2] = z
[1] ^ cast_sbox5
[U_INT8_Td(x
[1])] ^
178 cast_sbox6
[U_INT8_Tc(x
[1])] ^ cast_sbox7
[U_INT8_Tb(x
[1])] ^
179 cast_sbox8
[U_INT8_Ta(x
[1])] ^ cast_sbox5
[U_INT8_Tb(z
[0])];
180 t
[3] = x
[3] = z
[3] ^ cast_sbox5
[U_INT8_Tc(x
[2])] ^
181 cast_sbox6
[U_INT8_Tb(x
[2])] ^ cast_sbox7
[U_INT8_Td(x
[2])] ^
182 cast_sbox8
[U_INT8_Ta(x
[2])] ^ cast_sbox6
[U_INT8_Td(z
[0])];
188 key
->xkey
[i
+0] = cast_sbox5
[U_INT8_Ta(t
[2])] ^ cast_sbox6
[U_INT8_Tb(t
[2])] ^
189 cast_sbox7
[U_INT8_Td(t
[1])] ^ cast_sbox8
[U_INT8_Tc(t
[1])];
190 key
->xkey
[i
+1] = cast_sbox5
[U_INT8_Tc(t
[2])] ^ cast_sbox6
[U_INT8_Td(t
[2])] ^
191 cast_sbox7
[U_INT8_Tb(t
[1])] ^ cast_sbox8
[U_INT8_Ta(t
[1])];
192 key
->xkey
[i
+2] = cast_sbox5
[U_INT8_Ta(t
[3])] ^ cast_sbox6
[U_INT8_Tb(t
[3])] ^
193 cast_sbox7
[U_INT8_Td(t
[0])] ^ cast_sbox8
[U_INT8_Tc(t
[0])];
194 key
->xkey
[i
+3] = cast_sbox5
[U_INT8_Tc(t
[3])] ^ cast_sbox6
[U_INT8_Td(t
[3])] ^
195 cast_sbox7
[U_INT8_Tb(t
[0])] ^ cast_sbox8
[U_INT8_Ta(t
[0])];
199 key
->xkey
[i
+0] = cast_sbox5
[U_INT8_Td(t
[0])] ^ cast_sbox6
[U_INT8_Tc(t
[0])] ^
200 cast_sbox7
[U_INT8_Ta(t
[3])] ^ cast_sbox8
[U_INT8_Tb(t
[3])];
201 key
->xkey
[i
+1] = cast_sbox5
[U_INT8_Tb(t
[0])] ^ cast_sbox6
[U_INT8_Ta(t
[0])] ^
202 cast_sbox7
[U_INT8_Tc(t
[3])] ^ cast_sbox8
[U_INT8_Td(t
[3])];
203 key
->xkey
[i
+2] = cast_sbox5
[U_INT8_Td(t
[1])] ^ cast_sbox6
[U_INT8_Tc(t
[1])] ^
204 cast_sbox7
[U_INT8_Ta(t
[2])] ^ cast_sbox8
[U_INT8_Tb(t
[2])];
205 key
->xkey
[i
+3] = cast_sbox5
[U_INT8_Tb(t
[1])] ^ cast_sbox6
[U_INT8_Ta(t
[1])] ^
206 cast_sbox7
[U_INT8_Tc(t
[2])] ^ cast_sbox8
[U_INT8_Td(t
[2])];
211 key
->xkey
[i
+0] ^= cast_sbox5
[U_INT8_Tc(z
[0])];
212 key
->xkey
[i
+1] ^= cast_sbox6
[U_INT8_Tc(z
[1])];
213 key
->xkey
[i
+2] ^= cast_sbox7
[U_INT8_Tb(z
[2])];
214 key
->xkey
[i
+3] ^= cast_sbox8
[U_INT8_Ta(z
[3])];
217 key
->xkey
[i
+0] ^= cast_sbox5
[U_INT8_Ta(x
[2])];
218 key
->xkey
[i
+1] ^= cast_sbox6
[U_INT8_Tb(x
[3])];
219 key
->xkey
[i
+2] ^= cast_sbox7
[U_INT8_Td(x
[0])];
220 key
->xkey
[i
+3] ^= cast_sbox8
[U_INT8_Td(x
[1])];
223 key
->xkey
[i
+0] ^= cast_sbox5
[U_INT8_Tb(z
[2])];
224 key
->xkey
[i
+1] ^= cast_sbox6
[U_INT8_Ta(z
[3])];
225 key
->xkey
[i
+2] ^= cast_sbox7
[U_INT8_Tc(z
[0])];
226 key
->xkey
[i
+3] ^= cast_sbox8
[U_INT8_Tc(z
[1])];
229 key
->xkey
[i
+0] ^= cast_sbox5
[U_INT8_Td(x
[0])];
230 key
->xkey
[i
+1] ^= cast_sbox6
[U_INT8_Td(x
[1])];
231 key
->xkey
[i
+2] ^= cast_sbox7
[U_INT8_Ta(x
[2])];
232 key
->xkey
[i
+3] ^= cast_sbox8
[U_INT8_Tb(x
[3])];
236 key
->xkey
[i
+0] &= 31;
237 key
->xkey
[i
+1] &= 31;
238 key
->xkey
[i
+2] &= 31;
239 key
->xkey
[i
+3] &= 31;
243 for (i
= 0; i
< 4; i
++) {
244 t
[i
] = x
[i
] = z
[i
] = 0;