Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 549
1
2
/*
3
* Fast implementation of the DES, as described in the Federal Register,
4
* Vol. 40, No. 52, p. 12134, March 17, 1975.
5
*
6
* Stuart Levy, Minnesota Supercomputer Center, April 1988.
7
* Currently (2007) [email protected]
8
* NCSA, University of Illinois Urbana-Champaign
9
*
10
* Calling sequence:
11
*
12
* typedef unsigned long keysched[32];
13
*
14
* fsetkey(key, keysched) / * Converts a DES key to a "key schedule" * /
15
* unsigned char key[8];
16
* keysched *ks;
17
*
18
* fencrypt(block, decrypt, keysched) / * En/decrypts one 64-bit block * /
19
* unsigned char block[8]; / * data, en/decrypted in place * /
20
* int decrypt; / * 0=>encrypt, 1=>decrypt * /
21
* keysched *ks; / * key schedule, as set by fsetkey * /
22
*
23
* Key and data block representation:
24
* The 56-bit key (bits 1..64 including "parity" bits 8, 16, 24, ..., 64)
25
* and the 64-bit data block (bits 1..64)
26
* are each stored in arrays of 8 bytes.
27
* Following the NBS numbering, the MSB has the bit number 1, so
28
* key[0] = 128*bit1 + 64*bit2 + ... + 1*bit8, ... through
29
* key[7] = 128*bit57 + 64*bit58 + ... + 1*bit64.
30
* In the key, "parity" bits are not checked; their values are ignored.
31
*
32
*/
33
34
/*
35
===============================================================================
36
License
37
des56.c is licensed under the terms of the MIT license reproduced below.
38
This means that des56.c is free software and can be used for both academic
39
and commercial purposes at absolutely no cost.
40
===============================================================================
41
Copyright (C) 1988 Stuart Levy
42
Permission is hereby granted, free of charge, to any person obtaining a copy
43
of this software and associated documentation files (the "Software"), to deal
44
in the Software without restriction, including without limitation the rights
45
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
46
copies of the Software, and to permit persons to whom the Software is
47
furnished to do so, subject to the following conditions:
48
The above copyright notice and this permission notice shall be included in
49
all copies or substantial portions of the Software.
50
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
53
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
55
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
56
THE SOFTWARE.
57
*/
58
59
60
#include "des56.h"
61
62
63
/*
64
* Key schedule generation.
65
* We begin by pointlessly permuting the 56 useful key bits into
66
* two groups of 28 bits called C and D.
67
* bK_C and bK_D are indexed by C and D bit numbers, respectively,
68
* and give the key bit number (1..64) which should initialize that C/D bit.
69
* This is the "permuted choice 1" table.
70
*/
71
72
static tiny bK_C[28] = {
73
57, 49, 41, 33, 25, 17, 9,
74
1, 58, 50, 42, 34, 26, 18,
75
10, 2, 59, 51, 43, 35, 27,
76
19, 11, 3, 60, 52, 44, 36,
77
};
78
static tiny bK_D[28] = {
79
63, 55, 47, 39, 31, 23, 15,
80
7, 62, 54, 46, 38, 30, 22,
81
14, 6, 61, 53, 45, 37, 29,
82
21, 13, 5, 28, 20, 12, 4,
83
};
84
85
/*
86
* For speed, we invert these, building tables to map groups of
87
* key bits into the corresponding C and D bits.
88
* We represent C and D each as 28 contiguous bits right-justified in a
89
* word, padded on the left with zeros.
90
* If key byte `i' is said to contain bits Ki,0 (MSB) Ki,1 ... Ki,7 (LSB)
91
* then
92
* wC_K4[i][Ki,0 Ki,1 Ki,2 Ki,3] gives the C bits for Ki,0..3,
93
* wD_K4[i][Ki,0 Ki,1 Ki,2 Ki,3] the corresponding D bits,
94
* wC_K3[i][Ki,4 Ki,5 Ki,6] the C bits for Ki,4..6,
95
* and wD_K3[i][Ki,4 Ki,5 Ki,6] the D bits for Ki,4..6.
96
* Ki,7 is ignored since it is the nominal parity bit.
97
* We could just use a single table for [i][Ki,0 .. Ki,6] but that
98
* would take a lot of storage for such a rarely-used function.
99
*/
100
101
static word32 wC_K4[8][16], wC_K3[8][8];
102
static word32 wD_K4[8][16], wD_K3[8][8];
103
104
/*
105
* Successive Ci and Di for the sixteen steps in the key schedule are
106
* created by independent 28-bit left circular shifts on C and D.
107
* The shift count varies with the step number.
108
*/
109
static tiny preshift[16] = {
110
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1,
111
};
112
113
/*
114
* Each step in the key schedule is generated by selecting 48 bits
115
* (8 groups of 6 bits) from the appropriately shifted Ci and Di.
116
* bCD_KS, indexed by the key schedule bit number, gives the bit number
117
* in CD (CD1 = MSB of C, CD28 = LSB of C, CD29 = MSB of D, CD56 = LSB of D)
118
* which determines that bit of the key schedule.
119
* Note that only C bits (1..28) appear in the first (upper) 24 bits of
120
* the key schedule, and D bits (29..56) in the second (lower) 24 bits.
121
* This is the "permuted-choice-2" table.
122
*/
123
124
static tiny bCD_KS[48] = {
125
14, 17, 11, 24, 1, 5,
126
3, 28, 15, 6, 21, 10,
127
23, 19, 12, 4, 26, 8,
128
16, 7, 27, 20, 13, 2,
129
41, 52, 31, 37, 47, 55,
130
30, 40, 51, 45, 33, 48,
131
44, 49, 39, 56, 34, 53,
132
46, 42, 50, 36, 29, 32,
133
};
134
135
/*
136
* We invert bCD_KS into a pair of tables which map groups of 4
137
* C or D bits into corresponding key schedule bits.
138
* We represent each step of the key schedule as 8 groups of 8 bits,
139
* with the 6 real bits right-justified in each 8-bit group.
140
* hKS_C4[i][C4i+1 .. C4i+4] gives the bits in the high order (first four)
141
* key schedule "bytes" which correspond to C bits 4i+1 .. 4i+4.
142
* lKS_D4[i][D4i+1 .. D4i+4] gives the appropriate bits in the latter (last 4)
143
* key schedule bytes, from the corresponding D bits.
144
*/
145
146
static word32 hKS_C4[7][16];
147
static word32 lKS_D4[7][16];
148
149
/*
150
* Encryption/decryption.
151
* Before beginning, and after ending, we perform another useless permutation
152
* on the bits in the data block.
153
*
154
* The initial permutation and its inverse, final permutation
155
* are too simple to need a table for. If we break the input I1 .. I64 into
156
* 8-bit chunks I0,0 I0,1 ... I0,7 I1,0 I1,1 ... I7,7
157
* then the initial permutation sets LR as follows:
158
* L = I7,1 I6,1 I5,1 ... I0,1 I7,3 I6,3 ... I0,3 I7,5 ... I0,5 I7,7 ... I0,7
159
* and
160
* R = I7,0 I6,0 I5,0 ... I0,0 I7,2 I6,2 ... I0,2 I7,4 ... I0,4 I7,6 ... I0,6
161
*
162
* If we number the bits in the final LR similarly,
163
* L = L0,0 L0,1 ... L3,7 R = R0,0 R0,1 ... R3,7
164
* then the output is
165
* O = R0,7 L0,7 R1,7 L1,7 ... R3,7 L3,7 R0,6 L0,6 ... L3,6 R0,5 ... R3,0 L3,0
166
*
167
* To speed I => LR shuffling we use an array of 32-bit values indexed by
168
* 8-bit input bytes.
169
* wL_I8[ 0 I0,1 0 I0,3 0 I0,5 0 I0,7 ] = the corresponding L bits.
170
* Other R and L bits are derived from wL_I8 by shifting.
171
*
172
* To speed LR => O shuffling, an array of 32-bit values indexed by 4-bit lumps:
173
* wO_L4[ L0,4 L0,5 L0,6 L0,7 ] = the corresponding high-order 32 O bits.
174
*/
175
176
static word32 wL_I8[0x55 + 1];
177
static word32 wO_L4[16];
178
179
/*
180
* Core of encryption/decryption.
181
* In each key schedule stage, we:
182
* take 8 overlapping groups of 6 bits each from R
183
* (the NBS tabulates the bit selections in the E table,
184
* but it's so simple we just use shifting to get the right bits)
185
* XOR each group with the corresponding bits from the key schedule
186
* Use the resulting 6 bits as an index into the appropriate S table
187
* (there are 8 such tables, one per group of 6 bits)
188
* Each S entry yields 4 bits.
189
* The 8 groups of 4 bits are catenated into a 32-bit value.
190
* Those 32 bits are permuted according to the P table.
191
* Finally the permuted 32-bit value is XORed with L and becomes
192
* the R value for the next stage, while the previous R becomes the new L.
193
*
194
* Here, we merge the P permutation with the S tables by making the
195
* S entries be 32-bit masks, already suitably permuted.
196
* Also, the bits in each six-bit group must be permuted before use as
197
* an index into the NBS-tabulated S tables.
198
* We rearrange entries in wPS so that natural bit order can be used.
199
*/
200
201
static word32 wPS[8][64];
202
203
static tiny P[32] = {
204
16, 7, 20, 21,
205
29, 12, 28, 17,
206
1, 15, 23, 26,
207
5, 18, 31, 10,
208
2, 8, 24, 14,
209
32, 27, 3, 9,
210
19, 13, 30, 6,
211
22, 11, 4, 25,
212
};
213
214
static tiny S[8][64] = {
215
{
216
14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
217
0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
218
4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
219
15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,
220
},
221
222
{
223
15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,
224
3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,
225
0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,
226
13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,
227
},
228
229
{
230
10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,
231
13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,
232
13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,
233
1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,
234
},
235
236
{
237
7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,
238
13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,
239
10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,
240
3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,
241
},
242
243
{
244
2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,
245
14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,
246
4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,
247
11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,
248
},
249
250
{
251
12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,
252
10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,
253
9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,
254
4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,
255
},
256
257
{
258
4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,
259
13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,
260
1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,
261
6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,
262
},
263
264
{
265
13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,
266
1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,
267
7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,
268
2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11,
269
},
270
};
271
272
static void buildtables( void )
273
{
274
register int i, j;
275
register word32 v;
276
word32 wC_K[64], wD_K[64];
277
word32 hKS_C[28], lKS_D[28];
278
int Smap[64];
279
word32 wP[32];
280
281
#if USG
282
# define ZERO(array) memset((char *)(array), '\0', sizeof(array))
283
#else
284
# if BSD
285
# define ZERO(array) bzero((char *)(array), sizeof(array))
286
# else
287
# define ZERO(array) { register word32 *p = (word32 *)(array); \
288
i = sizeof(array) / sizeof(*p); \
289
do { *p++ = 0; } while(--i > 0); \
290
}
291
# endif
292
#endif
293
294
295
/* Invert permuted-choice-1 (key => C,D) */
296
297
ZERO(wC_K);
298
ZERO(wD_K);
299
v = 1;
300
for(j = 28; --j >= 0; ) {
301
wC_K[ bK_C[j] - 1 ] = wD_K[ bK_D[j] - 1 ] = v;
302
v += v; /* (i.e. v <<= 1) */
303
}
304
305
for(i = 0; i < 64; i++) {
306
int t = 8 >> (i & 3);
307
for(j = 0; j < 16; j++) {
308
if(j & t) {
309
wC_K4[i >> 3][j] |= wC_K[i];
310
wD_K4[i >> 3][j] |= wD_K[i];
311
if(j < 8) {
312
wC_K3[i >> 3][j] |= wC_K[i + 3];
313
wD_K3[i >> 3][j] |= wD_K[i + 3];
314
}
315
}
316
}
317
/* Generate the sequence 0,1,2,3, 8,9,10,11, ..., 56,57,58,59. */
318
if(t == 1) i += 4;
319
}
320
321
/* Invert permuted-choice-2 */
322
323
ZERO(hKS_C);
324
ZERO(lKS_D);
325
v = 1;
326
for(i = 24; (i -= 6) >= 0; ) {
327
j = i+5;
328
do {
329
hKS_C[ bCD_KS[j] - 1 ] = lKS_D[ bCD_KS[j+24] - 28 - 1 ] = v;
330
v += v; /* Like v <<= 1 but may be faster */
331
} while(--j >= i);
332
v <<= 2; /* Keep byte aligned */
333
}
334
335
for(i = 0; i < 28; i++) {
336
v = 8 >> (i & 3);
337
for(j = 0; j < 16; j++) {
338
if(j & v) {
339
hKS_C4[i >> 2][j] |= hKS_C[i];
340
lKS_D4[i >> 2][j] |= lKS_D[i];
341
}
342
}
343
}
344
345
/* Initial permutation */
346
347
for(i = 0; i <= 0x55; i++) {
348
v = 0;
349
if(i & 64) v = (word32) 1 << 24;
350
if(i & 16) v |= (word32) 1 << 16;
351
if(i & 4) v |= (word32) 1 << 8;
352
if(i & 1) v |= 1;
353
wL_I8[i] = v;
354
}
355
356
/* Final permutation */
357
358
for(i = 0; i < 16; i++) {
359
v = 0;
360
if(i & 1) v = (word32) 1 << 24;
361
if(i & 2) v |= (word32) 1 << 16;
362
if(i & 4) v |= (word32) 1 << 8;
363
if(i & 8) v |= (word32) 1;
364
wO_L4[i] = v;
365
}
366
367
/* Funny bit rearrangement on second index into S tables */
368
369
for(i = 0; i < 64; i++) {
370
Smap[i] = (i & 0x20) | (i & 1) << 4 | (i & 0x1e) >> 1;
371
}
372
373
/* Invert permutation P into mask indexed by R bit number */
374
375
v = 1;
376
for(i = 32; --i >= 0; ) {
377
wP[ P[i] - 1 ] = v;
378
v += v;
379
}
380
381
/* Build bit-mask versions of S tables, indexed in natural bit order */
382
383
for(i = 0; i < 8; i++) {
384
for(j = 0; j < 64; j++) {
385
int k, t;
386
387
t = S[i][ Smap[j] ];
388
for(k = 0; k < 4; k++) {
389
if(t & 8)
390
wPS[i][j] |= wP[4*i + k];
391
t += t;
392
}
393
}
394
}
395
}
396
397
398
void fsetkey(char key[8], keysched *ks)
399
{
400
register int i;
401
register word32 C, D;
402
static int built = 0;
403
404
if(!built) {
405
buildtables();
406
built = 1;
407
}
408
409
C = D = 0;
410
for(i = 0; i < 8; i++) {
411
register int v;
412
413
v = key[i] >> 1; /* Discard "parity" bit */
414
C |= wC_K4[i][(v>>3) & 15] | wC_K3[i][v & 7];
415
D |= wD_K4[i][(v>>3) & 15] | wD_K3[i][v & 7];
416
}
417
418
/*
419
* C and D now hold the suitably right-justified
420
* 28 permuted key bits each.
421
*/
422
for(i = 0; i < 16; i++) {
423
#ifdef CRAY
424
#define choice2(x, v) x[6][v&15] | x[5][(v>>4)&15] | x[4][(v>>8)&15] | \
425
x[3][(v>>12)&15] | x[2][(v>>16)&15] | x[1][(v>>20)&15] | \
426
x[0][(v>>24)&15]
427
#else
428
register word32 *ap;
429
430
# define choice2(x, v) ( \
431
ap = &(x)[0][0], \
432
ap[16*6 + (v&15)] | \
433
ap[16*5 + ((v>>4)&15)] | ap[16*4 + ((v>>8)&15)] | \
434
ap[16*3 + ((v>>12)&15)] | ap[16*2 + ((v>>16)&15)] | \
435
ap[16*1 + ((v>>20)&15)] | ap[16*0 + ((v>>24)&15)] )
436
#endif
437
438
439
/* 28-bit left circular shift */
440
C <<= preshift[i];
441
C = ((C >> 28) & 3) | (C & (((word32)1<<28) - 1));
442
ks->KS[i].h = choice2(hKS_C4, C);
443
444
D <<= preshift[i];
445
D = ((D >> 28) & 3) | (D & (((word32)1<<28) - 1));
446
ks->KS[i].l = choice2(lKS_D4, D);
447
}
448
}
449
450
void
451
fencrypt(char block[8], int decrypt, keysched *ks)
452
{
453
int i;
454
register word32 L, R;
455
register struct keystage *ksp;
456
register word32 *ap;
457
458
/* Initial permutation */
459
460
L = R = 0;
461
i = 7;
462
ap = wL_I8;
463
do {
464
register int v;
465
466
v = block[i]; /* Could optimize according to ENDIAN */
467
L = ap[v & 0x55] | (L << 1);
468
R = ap[(v >> 1) & 0x55] | (R << 1);
469
} while(--i >= 0);
470
471
if(decrypt) {
472
ksp = &ks->KS[15];
473
} else {
474
ksp = &ks->KS[0];
475
}
476
477
#ifdef CRAY
478
# define PS(i,j) wPS[i][j]
479
#else
480
# define PS(i,j) ap[64*(i) + (j)]
481
ap = &wPS[0][0];
482
#endif
483
484
i = 16;
485
do {
486
register word32 k, tR;
487
488
tR = (R >> 15) | (R << 17);
489
490
k = ksp->h;
491
L ^= PS(0, ((tR >> 12) ^ (k >> 24)) & 63)
492
| PS(1, ((tR >> 8) ^ (k >> 16)) & 63)
493
| PS(2, ((tR >> 4) ^ (k >> 8)) & 63)
494
| PS(3, (tR ^ k) & 63);
495
496
k = ksp->l;
497
L ^= PS(4, ((R >> 11) ^ (k >> 24)) & 63)
498
| PS(5, ((R >> 7) ^ (k >> 16)) & 63)
499
| PS(6, ((R >> 3) ^ (k >> 8)) & 63)
500
| PS(7, ((tR >> 16) ^ k) & 63);
501
502
tR = L;
503
L = R;
504
R = tR;
505
506
507
if(decrypt)
508
ksp--;
509
else
510
ksp++;
511
} while(--i > 0);
512
{
513
register word32 t;
514
515
#ifdef CRAY
516
# define FP(k) (wO_L4[ (L >> (k)) & 15 ] << 1 | wO_L4[ (R >> (k)) & 15 ])
517
#else
518
# define FP(k) (ap[ (L >> (k)) & 15 ] << 1 | ap[ (R >> (k)) & 15 ])
519
520
ap = wO_L4;
521
#endif
522
523
t = FP(0) | (FP(8) | (FP(16) | (FP(24) << 2)) << 2) << 2;
524
R = FP(4) | (FP(12) | (FP(20) | (FP(28) << 2)) << 2) << 2;
525
L = t;
526
}
527
{
528
register word32 t;
529
register char *bp;
530
531
bp = &block[7];
532
t = R;
533
*bp = t & 255;
534
*--bp = (t >>= 8) & 255;
535
*--bp = (t >>= 8) & 255;
536
*--bp = (t >> 8) & 255;
537
t = L;
538
*--bp = t & 255;
539
*--bp = (t >>= 8) & 255;
540
*--bp = (t >>= 8) & 255;
541
*--bp = (t >> 8) & 255;
542
}
543
}
544
545