desCode.h 11 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
/* desCode.h
 *
 * $Id$ */

/*	des - fast & portable DES encryption & decryption.
Niels Möller's avatar
Niels Möller committed
6
 *	Copyright (C) 1992  Dana L. How
Niels Möller's avatar
Niels Möller committed
7
 *	Please see the file `descore.README' for the complete copyright notice.
Niels Möller's avatar
Niels Möller committed
8
9
10
11
 */

#include "des.h"

Niels Möller's avatar
Niels Möller committed
12
13
extern const uint32_t des_keymap[];
extern const uint32_t des_bigmap[];
Niels Möller's avatar
Niels Möller committed
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

/* optional customization:
 * the idea here is to alter the code so it will still run correctly
 * on any machine,  but the quickest on the specific machine in mind.
 * note that these silly tweaks can give you a 15%-20% speed improvement
 * on the sparc -- it's probably even more significant on the 68000. */

/* take care of machines with incredibly few registers */
#if	defined(i386)
#define	REGISTER		/* only x, y, z will be declared register */
#else
#define	REGISTER	register
#endif	/* i386 */

/* is auto inc/dec faster than 7bit unsigned indexing? */
#if	defined(vax) || defined(mc68000)
#define	FIXR		r += 32;
#define	FIXS		s +=  8;
#define	PREV(v,o)	*--v
#define	NEXT(v,o)	*v++
#else
#define	FIXR
#define	FIXS
#define	PREV(v,o)	v[o]
#define	NEXT(v,o)	v[o]
#endif

/* if no machine type, default is indexing, 6 registers and cheap literals */
#if	!defined(i386) && !defined(vax) && !defined(mc68000) && !defined(sparc)
#define	vax
#endif


/* handle a compiler which can't reallocate registers */
/* The BYTE type is used as parameter for the encrypt/decrypt functions.
 * It's pretty bad to have the function prototypes depend on
 * a macro definition that the users of the function doesn't
Niels Möller's avatar
Niels Möller committed
51
 * know about. /Niels */
Niels Möller's avatar
Niels Möller committed
52
#if	0			/* didn't feel like deleting */
Niels Möller's avatar
Niels Möller committed
53
#define	SREGFREE	; s = (uint8_t *) D
Niels Möller's avatar
Niels Möller committed
54
55
#define	DEST		s
#define	D		m0
Niels Möller's avatar
Niels Möller committed
56
#define	BYTE		uint32_t
Niels Möller's avatar
Niels Möller committed
57
58
59
60
#else
#define	SREGFREE
#define	DEST		d
#define	D		d
Niels Möller's avatar
Niels Möller committed
61
#define	BYTE		uint8_t
Niels Möller's avatar
Niels Möller committed
62
63
64
65
66
67
68
69
70
71
72
73
#endif

/* handle constants in the optimal way for 386 & vax */
/* 386: we declare 3 register variables (see above) and use 3 more variables;
 * vax: we use 6 variables, all declared register;
 * we assume address literals are cheap & unrestricted;
 * we assume immediate constants are cheap & unrestricted. */
#if	defined(i386) || defined(vax)
#define	MQ0	 des_bigmap
#define	MQ1	(des_bigmap +  64)
#define	MQ2	(des_bigmap + 128)
#define	MQ3	(des_bigmap + 192)
Niels Möller's avatar
Niels Möller committed
74
75
76
77
78
79
#define	HQ0(z)				/*	z |= 0x01000000L; */
#define	HQ2(z)				/*	z |= 0x03000200L; */
#define	LQ0(z)	0xFCFC & z
#define	LQ1(z)	0xFCFC & z
#define	LQ2(z)	0xFCFC & z
#define	LQ3(z)	0xFCFC & z
Niels Möller's avatar
Niels Möller committed
80
81
82
83
84
85
86
87
88
89
#define	SQ	16
#define	MS0	 des_keymap 
#define	MS1	(des_keymap +  64)
#define	MS2	(des_keymap + 128)
#define	MS3	(des_keymap + 192)
#define	MS4	(des_keymap + 256)
#define	MS5	(des_keymap + 320)
#define	MS6	(des_keymap + 384)
#define	MS7	(des_keymap + 448)
#define	HS(z)
Niels Möller's avatar
Niels Möller committed
90
91
92
93
#define	LS0(z)	0xFC & z
#define	LS1(z)	0xFC & z
#define	LS2(z)	0xFC & z
#define	LS3(z)	0xFC & z
Niels Möller's avatar
Niels Möller committed
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
#define	REGQUICK
#define	SETQUICK
#define	REGSMALL
#define	SETSMALL
#endif	/* defined(i386) || defined(vax) */

/* handle constants in the optimal way for mc68000 */
/* in addition to the core 6 variables, we declare 3 registers holding constants
 * and 4 registers holding address literals.
 * at most 6 data values and 5 address values are actively used at once.
 * we assume address literals are so expensive we never use them;
 * we assume constant index offsets > 127 are expensive, so they are not used.
 * we assume all constants are expensive and put them in registers,
 * including shift counts greater than 8. */
#if	defined(mc68000)
#define	MQ0	m0
#define	MQ1	m1
#define	MQ2	m2
#define	MQ3	m3
#define	HQ0(z)
#define	HQ2(z)
#define	LQ0(z)	k0 & z
#define	LQ1(z)	k0 & z
#define	LQ2(z)	k0 & z
#define	LQ3(z)	k0 & z
#define	SQ	k1
#define	MS0	m0
#define	MS1	m0
#define	MS2	m1
#define	MS3	m1
#define	MS4	m2
#define	MS5	m2
#define	MS6	m3
#define	MS7	m3
#define	HS(z)	z |= k0;
#define	LS0(z)	k1 & z
#define	LS1(z)	k2 & z
#define	LS2(z)	k1 & z
#define	LS3(z)	k2 & z
#define	REGQUICK				\
Niels Möller's avatar
Niels Möller committed
134
135
	register uint32_t k0, k1;		\
	register uint32_t *m0, *m1, *m2, *m3;
Niels Möller's avatar
Niels Möller committed
136
#define	SETQUICK				\
Niels Möller's avatar
Niels Möller committed
137
	; k0 = 0xFCFC				\
Niels Möller's avatar
Niels Möller committed
138
139
140
141
142
143
144
	; k1 = 16				\
	/*k2 = 28 to speed up ROL */		\
	; m0 = des_bigmap			\
	; m1 = m0 + 64				\
	; m2 = m1 + 64				\
	; m3 = m2 + 64
#define	REGSMALL				\
Niels Möller's avatar
Niels Möller committed
145
146
	register uint32_t k0, k1, k2;		\
	register uint32_t *m0, *m1, *m2, *m3;
Niels Möller's avatar
Niels Möller committed
147
#define	SETSMALL				\
Niels Möller's avatar
Niels Möller committed
148
149
150
	; k0 = 0x01000100L			\
	; k1 = 0x0FC				\
	; k2 = 0x1FC				\
Niels Möller's avatar
Niels Möller committed
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
	; m0 = des_keymap			\
	; m1 = m0 + 128				\
	; m2 = m1 + 128				\
	; m3 = m2 + 128
#endif	/* defined(mc68000) */

/* handle constants in the optimal way for sparc */
/* in addition to the core 6 variables, we either declare:
 * 4 registers holding address literals and 1 register holding a constant, or
 * 8 registers holding address literals.
 * up to 14 register variables are declared (sparc has %i0-%i5, %l0-%l7).
 * we assume address literals are so expensive we never use them;
 * we assume any constant with >10 bits is expensive and put it in a register,
 * and any other is cheap and is coded in-line. */
#if	defined(sparc)
#define	MQ0	m0
#define	MQ1	m1
#define	MQ2	m2
#define	MQ3	m3
#define	HQ0(z)
#define	HQ2(z)
#define	LQ0(z)	k0 & z
#define	LQ1(z)	k0 & z
#define	LQ2(z)	k0 & z
#define	LQ3(z)	k0 & z
#define	SQ	16
#define	MS0	m0
#define	MS1	m1
#define	MS2	m2
#define	MS3	m3
#define	MS4	m4
#define	MS5	m5
#define	MS6	m6
#define	MS7	m7
#define	HS(z)
Niels Möller's avatar
Niels Möller committed
186
187
188
189
#define	LS0(z)	0xFC & z
#define	LS1(z)	0xFC & z
#define	LS2(z)	0xFC & z
#define	LS3(z)	0xFC & z
Niels Möller's avatar
Niels Möller committed
190
#define	REGQUICK				\
Niels Möller's avatar
Niels Möller committed
191
192
	register uint32_t k0;			\
	register uint32_t *m0, *m1, *m2, *m3;
Niels Möller's avatar
Niels Möller committed
193
#define	SETQUICK				\
Niels Möller's avatar
Niels Möller committed
194
	; k0 = 0xFCFC				\
Niels Möller's avatar
Niels Möller committed
195
196
197
198
199
	; m0 = des_bigmap			\
	; m1 = m0 + 64				\
	; m2 = m1 + 64				\
	; m3 = m2 + 64
#define	REGSMALL				\
Niels Möller's avatar
Niels Möller committed
200
	register uint32_t *m0, *m1, *m2, *m3, *m4, *m5, *m6, *m7;
Niels Möller's avatar
Niels Möller committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#define	SETSMALL				\
	; m0 = des_keymap			\
	; m1 = m0 + 64				\
	; m2 = m1 + 64				\
	; m3 = m2 + 64				\
	; m4 = m3 + 64				\
	; m5 = m4 + 64				\
	; m6 = m5 + 64				\
	; m7 = m6 + 64
#endif	/* defined(sparc) */


/* some basic stuff */

/* generate addresses from a base and an index */
Niels Möller's avatar
Niels Möller committed
216
217
218
/* FIXME: This is used only as *ADD(msi,lsi(z)) or *ADD(mqi,lqi(z)).
 * Why not use plain indexing instead? /Niels */
#define	ADD(b,x)	(uint32_t *) ((uint8_t *)b + (x))
Niels Möller's avatar
Niels Möller committed
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

/* low level rotate operations */
#define	NOP(d,c,o)
#define	ROL(d,c,o)	d = d << c | d >> o
#define	ROR(d,c,o)	d = d >> c | d << o
#define	ROL1(d)		ROL(d, 1, 31)
#define	ROR1(d)		ROR(d, 1, 31)

/* elementary swap for doing IP/FP */
#define	SWAP(x,y,m,b)				\
	z  = ((x >> b) ^ y) & m;		\
	x ^= z << b;				\
	y ^= z


/* the following macros contain all the important code fragments */

/* load input data, then setup special registers holding constants */
#define	TEMPQUICK(LOAD)				\
	REGQUICK				\
	LOAD()					\
	SETQUICK
#define	TEMPSMALL(LOAD)				\
	REGSMALL				\
	LOAD()					\
	SETSMALL

/* load data */
#define	LOADDATA(x,y)				\
	FIXS					\
	y  = PREV(s, 7); y<<= 8;		\
	y |= PREV(s, 6); y<<= 8;		\
	y |= PREV(s, 5); y<<= 8;		\
	y |= PREV(s, 4);			\
	x  = PREV(s, 3); x<<= 8;		\
	x |= PREV(s, 2); x<<= 8;		\
	x |= PREV(s, 1); x<<= 8;		\
	x |= PREV(s, 0)				\
	SREGFREE
/* load data without initial permutation and put into efficient position */
#define	LOADCORE()				\
	LOADDATA(x, y);				\
	ROR1(x);				\
	ROR1(y)
/* load data, do the initial permutation and put into efficient position */
#define	LOADFIPS()				\
	LOADDATA(y, x);				\
Niels Möller's avatar
Niels Möller committed
266
267
268
269
	SWAP(x, y, 0x0F0F0F0FL, 004);		\
	SWAP(y, x, 0x0000FFFFL, 020);		\
	SWAP(x, y, 0x33333333L, 002);		\
	SWAP(y, x, 0x00FF00FFL, 010);		\
Niels Möller's avatar
Niels Möller committed
270
	ROR1(x);				\
Niels Möller's avatar
Niels Möller committed
271
	z  = (x ^ y) & 0x55555555L;		\
Niels Möller's avatar
Niels Möller committed
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
	y ^= z;					\
	x ^= z;					\
	ROR1(y)


/* core encryption/decryption operations */
/* S box mapping and P perm */
#define	KEYMAPSMALL(x,z,mq0,mq1,hq,lq0,lq1,sq,ms0,ms1,ms2,ms3,hs,ls0,ls1,ls2,ls3)\
	hs(z)					\
	x ^= *ADD(ms3, ls3(z));			\
	z>>= 8;					\
	x ^= *ADD(ms2, ls2(z));			\
	z>>= 8;					\
	x ^= *ADD(ms1, ls1(z));			\
	z>>= 8;					\
	x ^= *ADD(ms0, ls0(z))
/* alternate version: use 64k of tables */
#define	KEYMAPQUICK(x,z,mq0,mq1,hq,lq0,lq1,sq,ms0,ms1,ms2,ms3,hs,ls0,ls1,ls2,ls3)\
	hq(z)					\
	x ^= *ADD(mq0, lq0(z));			\
	z>>= sq;				\
	x ^= *ADD(mq1, lq1(z))
/* apply 24 key bits and do the odd  s boxes */
#define	S7S1(x,y,z,r,m,KEYMAP,LOAD)		\
	z  = LOAD(r, m);			\
	z ^= y;					\
	KEYMAP(x,z,MQ0,MQ1,HQ0,LQ0,LQ1,SQ,MS0,MS1,MS2,MS3,HS,LS0,LS1,LS2,LS3)
/* apply 24 key bits and do the even s boxes */
#define	S6S0(x,y,z,r,m,KEYMAP,LOAD)		\
	z  = LOAD(r, m);			\
	z ^= y;					\
	ROL(z, 4, 28);				\
	KEYMAP(x,z,MQ2,MQ3,HQ2,LQ2,LQ3,SQ,MS4,MS5,MS6,MS7,HS,LS0,LS1,LS2,LS3)
/* actual iterations.  equivalent except for UPDATE & swapping m and n */
#define	ENCR(x,y,z,r,m,n,KEYMAP)		\
	S7S1(x,y,z,r,m,KEYMAP,NEXT);		\
	S6S0(x,y,z,r,n,KEYMAP,NEXT)
#define	DECR(x,y,z,r,m,n,KEYMAP)		\
	S6S0(x,y,z,r,m,KEYMAP,PREV);		\
	S7S1(x,y,z,r,n,KEYMAP,PREV)

/* write out result in correct byte order */
#define	SAVEDATA(x,y)				\
	NEXT(DEST, 0) = x; x>>= 8;		\
	NEXT(DEST, 1) = x; x>>= 8;		\
	NEXT(DEST, 2) = x; x>>= 8;		\
	NEXT(DEST, 3) = x;			\
	NEXT(DEST, 4) = y; y>>= 8;		\
	NEXT(DEST, 5) = y; y>>= 8;		\
	NEXT(DEST, 6) = y; y>>= 8;		\
	NEXT(DEST, 7) = y
/* write out result */
#define	SAVECORE()				\
	ROL1(x);				\
	ROL1(y);				\
	SAVEDATA(y, x)
/* do final permutation and write out result */
#define	SAVEFIPS()				\
	ROL1(x);				\
Niels Möller's avatar
Niels Möller committed
331
	z  = (x ^ y) & 0x55555555L;		\
Niels Möller's avatar
Niels Möller committed
332
333
334
	y ^= z;					\
	x ^= z;					\
	ROL1(y);				\
Niels Möller's avatar
Niels Möller committed
335
336
337
338
	SWAP(x, y, 0x00FF00FFL, 010);		\
	SWAP(y, x, 0x33333333L, 002);		\
	SWAP(x, y, 0x0000FFFFL, 020);		\
	SWAP(y, x, 0x0F0F0F0FL, 004);		\
Niels Möller's avatar
Niels Möller committed
339
340
341
342
343
344
345
346
	SAVEDATA(x, y)


/* the following macros contain the encryption/decryption skeletons */

#define	ENCRYPT(NAME, TEMP, LOAD, KEYMAP, SAVE)	\
						\
void						\
Niels Möller's avatar
Niels Möller committed
347
NAME(REGISTER BYTE *D,				\
Niels Möller's avatar
Niels Möller committed
348
349
     REGISTER const uint32_t *r,		\
     REGISTER const uint8_t *s)			\
Niels Möller's avatar
Niels Möller committed
350
{						\
Niels Möller's avatar
Niels Möller committed
351
	register uint32_t x, y, z;		\
Niels Möller's avatar
Niels Möller committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
						\
	/* declare temps & load data */		\
	TEMP(LOAD);				\
						\
	/* do the 16 iterations */		\
	ENCR(x,y,z,r, 0, 1,KEYMAP);		\
	ENCR(y,x,z,r, 2, 3,KEYMAP);		\
	ENCR(x,y,z,r, 4, 5,KEYMAP);		\
	ENCR(y,x,z,r, 6, 7,KEYMAP);		\
	ENCR(x,y,z,r, 8, 9,KEYMAP);		\
	ENCR(y,x,z,r,10,11,KEYMAP);		\
	ENCR(x,y,z,r,12,13,KEYMAP);		\
	ENCR(y,x,z,r,14,15,KEYMAP);		\
	ENCR(x,y,z,r,16,17,KEYMAP);		\
	ENCR(y,x,z,r,18,19,KEYMAP);		\
	ENCR(x,y,z,r,20,21,KEYMAP);		\
	ENCR(y,x,z,r,22,23,KEYMAP);		\
	ENCR(x,y,z,r,24,25,KEYMAP);		\
	ENCR(y,x,z,r,26,27,KEYMAP);		\
	ENCR(x,y,z,r,28,29,KEYMAP);		\
	ENCR(y,x,z,r,30,31,KEYMAP);		\
						\
	/* save result */			\
	SAVE();					\
						\
	return;					\
}

#define	DECRYPT(NAME, TEMP, LOAD, KEYMAP, SAVE)	\
						\
void						\
Niels Möller's avatar
Niels Möller committed
383
NAME(REGISTER BYTE *D,				\
Niels Möller's avatar
Niels Möller committed
384
385
     REGISTER const uint32_t *r,		\
     REGISTER const uint8_t *s)			\
Niels Möller's avatar
Niels Möller committed
386
{						\
Niels Möller's avatar
Niels Möller committed
387
	register uint32_t x, y, z;		\
Niels Möller's avatar
Niels Möller committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
						\
	/* declare temps & load data */		\
	TEMP(LOAD);				\
						\
	/* do the 16 iterations */		\
	FIXR					\
	DECR(x,y,z,r,31,30,KEYMAP);		\
	DECR(y,x,z,r,29,28,KEYMAP);		\
	DECR(x,y,z,r,27,26,KEYMAP);		\
	DECR(y,x,z,r,25,24,KEYMAP);		\
	DECR(x,y,z,r,23,22,KEYMAP);		\
	DECR(y,x,z,r,21,20,KEYMAP);		\
	DECR(x,y,z,r,19,18,KEYMAP);		\
	DECR(y,x,z,r,17,16,KEYMAP);		\
	DECR(x,y,z,r,15,14,KEYMAP);		\
	DECR(y,x,z,r,13,12,KEYMAP);		\
	DECR(x,y,z,r,11,10,KEYMAP);		\
	DECR(y,x,z,r, 9, 8,KEYMAP);		\
	DECR(x,y,z,r, 7, 6,KEYMAP);		\
	DECR(y,x,z,r, 5, 4,KEYMAP);		\
	DECR(x,y,z,r, 3, 2,KEYMAP);		\
	DECR(y,x,z,r, 1, 0,KEYMAP);		\
						\
	/* save result */			\
	SAVE();					\
						\
	return;					\
}