Debian Codegraph @

src/CAST.c

raw symbols SCIP
1 /*
2 cast.c -- implementation of CAST-128 (aka CAST5) as described in RFC2144
3
4 Written in 1997 by Wim Lewis <[email protected]> based entirely on RFC2144.
5 Minor modifications made in 2002 by Andrew M. Kuchling <[email protected]>.
6
7 ===================================================================
8 The contents of this file are dedicated to the public domain. To
9 the extent that dedication to the public domain is not available,
10 everyone is granted a worldwide, perpetual, royalty-free,
11 non-exclusive license to exercise all rights associated with the
12 contents of this file for any purpose whatsoever.
13 No rights are reserved.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 ===================================================================
24
25 Consult your local laws for possible restrictions on use, distribution, and
26 import/export. RFC2144 states that this algorithm "is available worldwide
27 on a royalty-free basis for commercial and non-commercial uses".
28
29 This code is a pretty straightforward transliteration of the RFC into C.
30 It has not been optimized much at all: byte-order-independent arithmetic
31 operations are used where order-dependent pointer ops or unions might be
32 faster; the code could be rearranged to give the optimizer a better
33 chance to speed things up; etc.
34
35 This code requires a vaguely ANSI-ish compiler.
36
37 compile -DTEST to include main() which performs the tests
38 specified in RFC2144
39
40 Tested with gcc 2.5.8 on i486, i586, i686, hp pa-risc, mc68040, sparc;
41 also with gcc 2.7.2 and (with minor changes) native Sun compiler on sparc
42
43 */
44
45 #include "common.h"
46 #include "endianess.h"
47 #include "block_base.h"
48
49 FAKE_INIT(raw_cast)
50
51 #define MODULE_NAME CAST
52 #define BLOCK_SIZE 8
53 #define KEY_SIZE 0
54
55 /* this struct probably belongs in cast.h */
56 struct block_state {
57 /* masking and rotate keys */
58 uint32_t Km[16];
59 uint8_t Kr[16];
60 /* number of rounds (depends on original unpadded keylength) */
61 unsigned rounds;
62 };
63
64 /* these are the eight 32*256 S-boxes */
65 #include "cast5.c"
66
67 /* this is the round function f(D, Km, Kr) */
68 static uint32_t castfunc(uint32_t D, uint32_t Kmi, uint8_t Kri, unsigned type)
69 {
70 uint32_t I, f;
71 uint8_t Ibe[4];
72 #define Ia Ibe[0]
73 #define Ib Ibe[1]
74 #define Ic Ibe[2]
75 #define Id Ibe[3]
76
77 switch(type) {
78 case 0:
79 I = (Kmi + D) ;
80 break;
81 case 1:
82 I = (Kmi ^ D) ;
83 break;
84 default:
85 case 2:
86 I = (Kmi - D) ;
87 break;
88 }
89
90 if (Kri>0) {
91 I = ( I << Kri ) | ( I >> ( 32-Kri ) );
92 }
93 STORE_U32_BIG(Ibe, I);
94
95 switch(type) {
96 case 0:
97 f = ((S1[Ia] ^ S2[Ib]) - S3[Ic]) + S4[Id];
98 break;
99 case 1:
100 f = ((S1[Ia] - S2[Ib]) + S3[Ic]) ^ S4[Id];
101 break;
102 default:
103 case 2:
104 f = ((S1[Ia] + S2[Ib]) ^ S3[Ic]) - S4[Id];
105 break;
106 }
107
108 return f;
109 }
110
111 /* encrypts/decrypts one block of data according to the key schedule
112 pointed to by `key'. Encrypts if decrypt=0, otherwise decrypts. */
113 static void castcrypt(struct block_state *key, uint8_t *block, int decrypt)
114 {
115 uint32_t L, R, tmp, f;
116 uint32_t Kmi;
117 uint8_t Kri;
118 unsigned functype, round;
119
120 L = LOAD_U32_BIG(block + 0);
121 R = LOAD_U32_BIG(block + 4);
122
123 for(round = 0; round < key->rounds; round ++) {
124
125 if (!decrypt) {
126 Kmi = key->Km[round];
127 Kri = key->Kr[round];
128 functype = round % 3;
129 } else {
130 Kmi = key->Km[(key->rounds) - round - 1];
131 Kri = key->Kr[(key->rounds) - round - 1];
132 functype = (((key->rounds) - round - 1) % 3);
133 }
134
135 f = castfunc(R, Kmi, Kri, functype);
136
137 tmp = L;
138 L = R;
139 R = tmp ^ f;
140 }
141
142 STORE_U32_BIG(block + 0, R);
143 STORE_U32_BIG(block + 4, L);
144 }
145
146 /* fetch a uint8_t from an array of uint32_ts */
147 #define b(a,n) (((a)[n/4] >> (24-((n&3)*8))) & 0xFF)
148
149 /* key schedule round functions */
150
151 #define XZRound(T, F, ki1, ki2, ki3, ki4, \
152 si11, si12, si13, si14, si15,\
153 si25,\
154 si35,\
155 si45 ) \
156 T[0] = F[ki1] ^ S5[si11 ] ^ S6[si12 ] ^ S7[si13 ] ^ S8[si14 ] ^ S7[si15];\
157 T[1] = F[ki2] ^ S5[b(T, 0)] ^ S6[b(T,2)] ^ S7[b(T, 1)] ^ S8[b(T,3)] ^ S8[si25];\
158 T[2] = F[ki3] ^ S5[b(T, 7)] ^ S6[b(T,6)] ^ S7[b(T, 5)] ^ S8[b(T,4)] ^ S5[si35];\
159 T[3] = F[ki4] ^ S5[b(T,10)] ^ S6[b(T,9)] ^ S7[b(T,11)] ^ S8[b(T,8)] ^ S6[si45];
160
161 #define zxround() XZRound(z, x, 0, 2, 3, 1, \
162 b(x,13), b(x,15), b(x,12), b(x,14),\
163 b(x, 8), b(x,10), b(x, 9), b(x,11))
164
165 #define xzround() XZRound(x, z, 2, 0, 1, 3, \
166 b(z,5), b(z,7), b(z,4), b(z,6), \
167 b(z,0), b(z,2), b(z,1), b(z,3))
168
169 #define Kround(T, base, F,\
170 i11, i12, i13, i14, i15,\
171 i21, i22, i23, i24, i25,\
172 i31, i32, i33, i34, i35,\
173 i41, i42, i43, i44, i45)\
174 T[base+0] = S5[b(F,i11)] ^ S6[b(F,i12)] ^ S7[b(F,i13)] ^ S8[b(F,i14)] ^ S5[b(F,i15)];\
175 T[base+1] = S5[b(F,i21)] ^ S6[b(F,i22)] ^ S7[b(F,i23)] ^ S8[b(F,i24)] ^ S6[b(F,i25)];\
176 T[base+2] = S5[b(F,i31)] ^ S6[b(F,i32)] ^ S7[b(F,i33)] ^ S8[b(F,i34)] ^ S7[b(F,i35)];\
177 T[base+3] = S5[b(F,i41)] ^ S6[b(F,i42)] ^ S7[b(F,i43)] ^ S8[b(F,i44)] ^ S8[b(F,i45)];
178
179 /* generates sixteen 32-bit subkeys based on a 4x32-bit input key;
180 modifies the input key *in as well. */
181 static void schedulekeys_half(uint32_t *in, uint32_t *keys)
182 {
183 uint32_t x[4], z[4];
184
185 x[0] = in[0];
186 x[1] = in[1];
187 x[2] = in[2];
188 x[3] = in[3];
189
190 zxround();
191 Kround(keys, 0, z,
192 8, 9, 7, 6, 2,
193 10, 11, 5, 4, 6,
194 12, 13, 3, 2, 9,
195 14, 15, 1, 0, 12);
196 xzround();
197 Kround(keys, 4, x,
198 3, 2, 12, 13, 8,
199 1, 0, 14, 15, 13,
200 7, 6, 8, 9, 3,
201 5, 4, 10, 11, 7);
202 zxround();
203 Kround(keys, 8, z,
204 3, 2, 12, 13, 9,
205 1, 0, 14, 15, 12,
206 7, 6, 8, 9, 2,
207 5, 4, 10, 11, 6);
208 xzround();
209 Kround(keys, 12, x,
210 8, 9, 7, 6, 3,
211 10, 11, 5, 4, 7,
212 12, 13, 3, 2, 8,
213 14, 15, 1, 0, 13);
214
215 in[0] = x[0];
216 in[1] = x[1];
217 in[2] = x[2];
218 in[3] = x[3];
219 }
220
221 /* generates a key schedule from an input key */
222 static void castschedulekeys(struct block_state *schedule, const uint8_t *key, size_t keybytes)
223 {
224 uint32_t x[4];
225 uint8_t paddedkey[16];
226 uint32_t Kr_wide[16];
227 unsigned i;
228
229 for(i = 0; i < keybytes; i++)
230 paddedkey[i] = key[i];
231 for( ; i < 16 ; i++)
232 paddedkey[i] = 0;
233
234 if (keybytes <= 10)
235 schedule->rounds = 12;
236 else
237 schedule->rounds = 16;
238
239 x[0] = LOAD_U32_BIG(paddedkey + 0);
240 x[1] = LOAD_U32_BIG(paddedkey + 4);
241 x[2] = LOAD_U32_BIG(paddedkey + 8);
242 x[3] = LOAD_U32_BIG(paddedkey + 12);
243
244 schedulekeys_half(x, schedule->Km);
245 schedulekeys_half(x, Kr_wide);
246
247 for(i = 0; i < 16; i ++) {
248 /* The Kr[] subkeys are used for 32-bit circular shifts,
249 so we only need to keep them modulo 32 */
250 schedule->Kr[i] = (uint8_t)(Kr_wide[i] & 0x1F);
251 }
252 }
253
254 static int
255 block_init(struct block_state *self, const uint8_t *key, size_t keylength)
256 {
257 /* make sure the key length is within bounds */
258 if (keylength < 5 || keylength > 16) {
259 return ERR_KEY_SIZE;
260 }
261
262 /* do the actual key schedule setup */
263 castschedulekeys(self, key, keylength);
264 return 0;
265 }
266
267 static void block_finalize(struct block_state* self)
268 {
269 }
270
271 static void
272 block_encrypt(struct block_state *self, const uint8_t *in, uint8_t *out)
273 {
274 memcpy(out, in, 8);
275 castcrypt(self, out, 0);
276 }
277
278 static void block_decrypt(struct block_state *self, const uint8_t *in, uint8_t *out)
279 {
280 memcpy(out, in, 8);
281 castcrypt(self, out, 1);
282 }
283
284 #include "block_common.c"