-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCast128.c
339 lines (288 loc) · 12.9 KB
/
Cast128.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Implementation of the CAST-128 cipher as described in "Constructing
Symmetric Ciphers Using the CAST Design Procedure" by Carlisle Adams.
Nortel, under whose aegis the CAST-128 algorithm was developed, have
allowed free use of the algorithm for any purpose. This implementation of
CAST-128 is copyright 1996 Peter Gutmann, and may be used freely for any
purpose provided you don't claim you wrote it.
This code was written for use with cryptlib, my free encryption library
which provides conventional and public-key encryption, key management, and
encrypted data management functions. You can find out more about cryptlib
from http://www.cs.auckland.ac.nz/~pgut001/cryptlib.html */
/***************************************************************************/
/* The following definitions are normally handled by cryptlib, which takes
care of issues like endianness and machine word size. Since it's not
possible to include all of cryptlib here, the following are equivalent
definitions. If you're using this outside cryptlib, you may have to
modify them to suit your system type */
typedef unsigned char BYTE;
typedef unsigned long LONG;
#define MASK32( x ) x
#define mgetBLong(memPtr) \
( ( ( LONG ) memPtr[ 0 ] << 24 ) | ( ( LONG ) memPtr[ 1 ]
<< 16 ) | \
( ( LONG ) memPtr[ 2 ] << 8 ) | ( LONG ) memPtr[ 3 ] ); \
memPtr += 4
#define mputBLong(memPtr,data) \
memPtr[ 0 ] = ( BYTE ) ( ( ( data ) >> 24 ) & 0xFF ); \
memPtr[ 1 ] = ( BYTE ) ( ( ( data ) >> 16 ) & 0xFF ); \
memPtr[ 2 ] = ( BYTE ) ( ( ( data ) >> 8 ) & 0xFF ); \
memPtr[ 3 ] = ( BYTE ) ( ( data ) & 0xFF ); \
memPtr += 4
/***************************************************************************/
#include "cast128.h"
#include "cast128s.h"
/* Left and right rotation operations. Most decent compilers should be
able to recognise these as rotate instructions */
#define ROTL(x,s) ( ( ( x ) << ( s ) ) | ( ( x ) >> ( 32 - ( s ) ) ) )
/* Macros to extract 8-bit values a, b, c, d from a 32-bit value. The cast
is necessary because some compilers prefer ints as array indices */
#define exta(x) ( ( int ) ( ( x >> 24 ) & 0xFF ) )
#define extb(x) ( ( int ) ( ( x >> 16 ) & 0xFF ) )
#define extc(x) ( ( int ) ( ( x >> 8 ) & 0xFF ) )
#define extd(x) ( ( int ) ( ( x ) & 0xFF ) )
/* The CAST f-functions */
#define f1( data ) \
( ( ( S1[ exta( data ) ] ^ S2[ extb( data ) ] ) - \
S3[ extc( data ) ] ) + S4[ extd( data ) ] )
#define f2( data ) \
( ( ( S1[ exta( data ) ] - S2[ extb( data ) ] ) + \
S3[ extc( data ) ] ) ^ S4[ extd( data ) ] )
#define f3( data ) \
( ( ( S1[ exta( data ) ] + S2[ extb( data ) ] ) ^ \
S3[ extc( data ) ] ) - S4[ extd( data ) ] )
/* The individual encrypt/decrypt rounds */
#define round1( count, output, input ) \
tmp = MASK32( K[ count - 1 ] + input ); \
tmp = ROTL( tmp, K[ count + 15 ] ); \
output ^= f1( tmp );
#define round2( count, output, input ) \
tmp = MASK32( K[ count - 1 ] ^ input ); \
tmp = ROTL( tmp, K[ count + 15 ] ); \
output ^= f2( tmp );
#define round3( count, output, input ) \
tmp = MASK32( K[ count - 1 ] - input ); \
tmp = ROTL( tmp, K[ count + 15 ] ); \
output ^= f3( tmp );
/* The CAST encrypt/decrypt functions */
void castEncrypt( LONG *K, BYTE *data )
{
BYTE *dataPtr = data;
LONG L, R, tmp;
L = mgetBLong( dataPtr );
R = mgetBLong( dataPtr );
/* Perform 16 rounds of encryption */
round1( 1, L, R );
round2( 2, R, L );
round3( 3, L, R );
round1( 4, R, L );
round2( 5, L, R );
round3( 6, R, L );
round1( 7, L, R );
round2( 8, R, L );
round3( 9, L, R );
round1( 10, R, L );
round2( 11, L, R );
round3( 12, R, L );
round1( 13, L, R );
round2( 14, R, L );
round3( 15, L, R );
round1( 16, R, L );
dataPtr = data;
mputBLong( dataPtr, R );
mputBLong( dataPtr, L );
}
void castDecrypt( LONG *K, BYTE *data )
{
BYTE *dataPtr = data;
LONG L, R, tmp;
L = mgetBLong( dataPtr );
R = mgetBLong( dataPtr );
/* Perform 16 rounds of decryption */
round1( 16, L, R );
round3( 15, R, L );
round2( 14, L, R );
round1( 13, R, L );
round3( 12, L, R );
round2( 11, R, L );
round1( 10, L, R );
round3( 9, R, L );
round2( 8, L, R );
round1( 7, R, L );
round3( 6, L, R );
round2( 5, R, L );
round1( 4, L, R );
round3( 3, R, L );
round2( 2, L, R );
round1( 1, R, L );
dataPtr = data;
mputBLong( dataPtr, R );
mputBLong( dataPtr, L );
}
/* Unpack a 32-bit value into four 8-bit values */
#define unpackBytes( value, byte1, byte2, byte3, byte4 ) \
byte1 = exta( value ); \
byte2 = extb( value ); \
byte3 = extc( value ); \
byte4 = extd( value );
/* Set up a CAST-128 key */
void castKeyInit( LONG *K, BYTE *userKey )
{
LONG x0x1x2x3, x4x5x6x7, x8x9xAxB, xCxDxExF;
LONG z0z1z2z3, z4z5z6z7, z8z9zAzB, zCzDzEzF;
int z0, z1, z2, z3, z4, z5, z6, z7, z8, z9, zA, zB, zC, zD, zE, zF;
int x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xA, xB, xC, xD, xE, xF;
/* Load the 128-bit user key into the x-values */
x0x1x2x3 = mgetBLong( userKey );
unpackBytes( x0x1x2x3, x0, x1, x2, x3 );
x4x5x6x7 = mgetBLong( userKey );
unpackBytes( x4x5x6x7, x4, x5, x6, x7 );
x8x9xAxB = mgetBLong( userKey );
unpackBytes( x8x9xAxB, x8, x9, xA, xB );
xCxDxExF = mgetBLong( userKey );
unpackBytes( xCxDxExF, xC, xD, xE, xF );
z0z1z2z3 = x0x1x2x3 ^ S5[ xD ] ^ S6[ xF ] ^ S7[ xC ] ^ S8[ xE ] ^
S7[ x8 ];
unpackBytes( z0z1z2z3, z0, z1, z2, z3 );
z4z5z6z7 = x8x9xAxB ^ S5[ z0 ] ^ S6[ z2 ] ^ S7[ z1 ] ^ S8[ z3 ] ^
S8[ xA ];
unpackBytes( z4z5z6z7, z4, z5, z6, z7 );
z8z9zAzB = xCxDxExF ^ S5[ z7 ] ^ S6[ z6 ] ^ S7[ z5 ] ^ S8[ z4 ] ^
S5[ x9 ];
unpackBytes( z8z9zAzB, z8, z9, zA, zB );
zCzDzEzF = x4x5x6x7 ^ S5[ zA ] ^ S6[ z9 ] ^ S7[ zB ] ^ S8[ z8 ] ^
S6[ xB ];
unpackBytes( zCzDzEzF, zC, zD, zE, zF );
K[ 0 ] = S5[ z8 ] ^ S6[ z9 ] ^ S7[ z7 ] ^ S8[ z6 ] ^ S5[ z2 ];
K[ 1 ] = S5[ zA ] ^ S6[ zB ] ^ S7[ z5 ] ^ S8[ z4 ] ^ S6[ z6 ];
K[ 2 ] = S5[ zC ] ^ S6[ zD ] ^ S7[ z3 ] ^ S8[ z2 ] ^ S7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