Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/crypto/des/des_locl.h
39481 views
1
/* $KAME: des_locl.h,v 1.7 2001/09/10 04:03:58 itojun Exp $ */
2
3
/* crypto/des/des_locl.h */
4
/* Copyright (C) 1995-1997 Eric Young ([email protected])
5
* All rights reserved.
6
*
7
* This file is part of an SSL implementation written
8
* by Eric Young ([email protected]).
9
* The implementation was written so as to conform with Netscapes SSL
10
* specification. This library and applications are
11
* FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
12
* as long as the following conditions are aheared to.
13
*
14
* Copyright remains Eric Young's, and as such any Copyright notices in
15
* the code are not to be removed. If this code is used in a product,
16
* Eric Young should be given attribution as the author of the parts used.
17
* This can be in the form of a textual message at program startup or
18
* in documentation (online or textual) provided with the package.
19
*
20
* Redistribution and use in source and binary forms, with or without
21
* modification, are permitted provided that the following conditions
22
* are met:
23
* 1. Redistributions of source code must retain the copyright
24
* notice, this list of conditions and the following disclaimer.
25
* 2. Redistributions in binary form must reproduce the above copyright
26
* notice, this list of conditions and the following disclaimer in the
27
* documentation and/or other materials provided with the distribution.
28
* 3. All advertising materials mentioning features or use of this software
29
* must display the following acknowledgement:
30
* This product includes software developed by Eric Young ([email protected])
31
*
32
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
33
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42
* SUCH DAMAGE.
43
*
44
* The licence and distribution terms for any publically available version or
45
* derivative of this code cannot be changed. i.e. this code cannot simply be
46
* copied and put under another distribution licence
47
* [including the GNU Public Licence.]
48
*/
49
50
#ifndef HEADER_DES_LOCL_H
51
#define HEADER_DES_LOCL_H
52
53
#include <crypto/des/des.h>
54
55
#undef DES_PTR
56
57
#ifdef __STDC__
58
#undef NOPROTO
59
#endif
60
61
#define ITERATIONS 16
62
#define HALF_ITERATIONS 8
63
64
/* used in des_read and des_write */
65
#define MAXWRITE (1024*16)
66
#define BSIZE (MAXWRITE+4)
67
68
#define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
69
l|=((DES_LONG)(*((c)++)))<< 8L, \
70
l|=((DES_LONG)(*((c)++)))<<16L, \
71
l|=((DES_LONG)(*((c)++)))<<24L)
72
73
/* NOTE - c is not incremented as per c2l */
74
#define c2ln(c,l1,l2,n) { \
75
c+=n; \
76
l1=l2=0; \
77
switch (n) { \
78
case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
79
case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
80
case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
81
case 5: l2|=((DES_LONG)(*(--(c)))); \
82
case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
83
case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
84
case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
85
case 1: l1|=((DES_LONG)(*(--(c)))); \
86
} \
87
}
88
89
#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
90
*((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
91
*((c)++)=(unsigned char)(((l)>>16L)&0xff), \
92
*((c)++)=(unsigned char)(((l)>>24L)&0xff))
93
94
/* replacements for htonl and ntohl since I have no idea what to do
95
* when faced with machines with 8 byte longs. */
96
#define HDRSIZE 4
97
98
#define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
99
l|=((DES_LONG)(*((c)++)))<<16L, \
100
l|=((DES_LONG)(*((c)++)))<< 8L, \
101
l|=((DES_LONG)(*((c)++))))
102
103
#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
104
*((c)++)=(unsigned char)(((l)>>16L)&0xff), \
105
*((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
106
*((c)++)=(unsigned char)(((l) )&0xff))
107
108
/* NOTE - c is not incremented as per l2c */
109
#define l2cn(l1,l2,c,n) { \
110
c+=n; \
111
switch (n) { \
112
case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
113
case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
114
case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
115
case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
116
case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
117
case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
118
case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
119
case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
120
} \
121
}
122
123
#define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
124
125
#define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
126
#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
127
u=R^s[S ]; \
128
t=R^s[S+1]
129
130
/* The changes to this macro may help or hinder, depending on the
131
* compiler and the achitecture. gcc2 always seems to do well :-).
132
* Inspired by Dana How <[email protected]>
133
* DO NOT use the alternative version on machines with 8 byte longs.
134
* It does not seem to work on the Alpha, even when DES_LONG is 4
135
* bytes, probably an issue of accessing non-word aligned objects :-( */
136
#ifdef DES_PTR
137
138
/* It recently occurred to me that 0^0^0^0^0^0^0 == 0, so there
139
* is no reason to not xor all the sub items together. This potentially
140
* saves a register since things can be xored directly into L */
141
142
#if defined(DES_RISC1) || defined(DES_RISC2)
143
#ifdef DES_RISC1
144
#define D_ENCRYPT(LL,R,S) { \
145
unsigned int u1,u2,u3; \
146
LOAD_DATA(R,S,u,t,E0,E1,u1); \
147
u2=(int)u>>8L; \
148
u1=(int)u&0xfc; \
149
u2&=0xfc; \
150
t=ROTATE(t,4); \
151
u>>=16L; \
152
LL^= *(const DES_LONG *)(des_SP +u1); \
153
LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
154
u3=(int)(u>>8L); \
155
u1=(int)u&0xfc; \
156
u3&=0xfc; \
157
LL^= *(const DES_LONG *)(des_SP+0x400+u1); \
158
LL^= *(const DES_LONG *)(des_SP+0x600+u3); \
159
u2=(int)t>>8L; \
160
u1=(int)t&0xfc; \
161
u2&=0xfc; \
162
t>>=16L; \
163
LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
164
LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
165
u3=(int)t>>8L; \
166
u1=(int)t&0xfc; \
167
u3&=0xfc; \
168
LL^= *(const DES_LONG *)(des_SP+0x500+u1); \
169
LL^= *(const DES_LONG *)(des_SP+0x700+u3); }
170
#endif /* DES_RISC1 */
171
#ifdef DES_RISC2
172
#define D_ENCRYPT(LL,R,S) { \
173
unsigned int u1,u2,s1,s2; \
174
LOAD_DATA(R,S,u,t,E0,E1,u1); \
175
u2=(int)u>>8L; \
176
u1=(int)u&0xfc; \
177
u2&=0xfc; \
178
t=ROTATE(t,4); \
179
LL^= *(const DES_LONG *)(des_SP +u1); \
180
LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
181
s1=(int)(u>>16L); \
182
s2=(int)(u>>24L); \
183
s1&=0xfc; \
184
s2&=0xfc; \
185
LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
186
LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
187
u2=(int)t>>8L; \
188
u1=(int)t&0xfc; \
189
u2&=0xfc; \
190
LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
191
LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
192
s1=(int)(t>>16L); \
193
s2=(int)(t>>24L); \
194
s1&=0xfc; \
195
s2&=0xfc; \
196
LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
197
LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
198
u2=(int)t>>8L; \
199
u1=(int)t&0xfc; \
200
u2&=0xfc; \
201
LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
202
LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
203
s1=(int)(t>>16L); \
204
s2=(int)(t>>24L); \
205
s1&=0xfc; \
206
s2&=0xfc; \
207
LL^= *(const DES_LONG *)(des_SP+0x500+s1); \
208
LL^= *(const DES_LONG *)(des_SP+0x700+s2); }
209
#endif /* DES_RISC2 */
210
#else /* DES_RISC1 || DES_RISC2 */
211
#define D_ENCRYPT(LL,R,S) { \
212
LOAD_DATA_tmp(R,S,u,t,E0,E1); \
213
t=ROTATE(t,4); \
214
LL^= \
215
*(const DES_LONG *)(des_SP +((u )&0xfc))^ \
216
*(const DES_LONG *)(des_SP+0x200+((u>> 8L)&0xfc))^ \
217
*(const DES_LONG *)(des_SP+0x400+((u>>16L)&0xfc))^ \
218
*(const DES_LONG *)(des_SP+0x600+((u>>24L)&0xfc))^ \
219
*(const DES_LONG *)(des_SP+0x100+((t )&0xfc))^ \
220
*(const DES_LONG *)(des_SP+0x300+((t>> 8L)&0xfc))^ \
221
*(const DES_LONG *)(des_SP+0x500+((t>>16L)&0xfc))^ \
222
*(const DES_LONG *)(des_SP+0x700+((t>>24L)&0xfc)); }
223
#endif /* DES_RISC1 || DES_RISC2 */
224
#else /* original version */
225
226
#if defined(DES_RISC1) || defined(DES_RISC2)
227
#ifdef DES_RISC1
228
#define D_ENCRYPT(LL,R,S) {\
229
unsigned int u1,u2,u3; \
230
LOAD_DATA(R,S,u,t,E0,E1,u1); \
231
u>>=2L; \
232
t=ROTATE(t,6); \
233
u2=(int)u>>8L; \
234
u1=(int)u&0x3f; \
235
u2&=0x3f; \
236
u>>=16L; \
237
LL^=des_SPtrans[0][u1]; \
238
LL^=des_SPtrans[2][u2]; \
239
u3=(int)u>>8L; \
240
u1=(int)u&0x3f; \
241
u3&=0x3f; \
242
LL^=des_SPtrans[4][u1]; \
243
LL^=des_SPtrans[6][u3]; \
244
u2=(int)t>>8L; \
245
u1=(int)t&0x3f; \
246
u2&=0x3f; \
247
t>>=16L; \
248
LL^=des_SPtrans[1][u1]; \
249
LL^=des_SPtrans[3][u2]; \
250
u3=(int)t>>8L; \
251
u1=(int)t&0x3f; \
252
u3&=0x3f; \
253
LL^=des_SPtrans[5][u1]; \
254
LL^=des_SPtrans[7][u3]; }
255
#endif /* DES_RISC1 */
256
#ifdef DES_RISC2
257
#define D_ENCRYPT(LL,R,S) {\
258
unsigned int u1,u2,s1,s2; \
259
LOAD_DATA(R,S,u,t,E0,E1,u1); \
260
u>>=2L; \
261
t=ROTATE(t,6); \
262
u2=(int)u>>8L; \
263
u1=(int)u&0x3f; \
264
u2&=0x3f; \
265
LL^=des_SPtrans[0][u1]; \
266
LL^=des_SPtrans[2][u2]; \
267
s1=(int)u>>16L; \
268
s2=(int)u>>24L; \
269
s1&=0x3f; \
270
s2&=0x3f; \
271
LL^=des_SPtrans[4][s1]; \
272
LL^=des_SPtrans[6][s2]; \
273
u2=(int)t>>8L; \
274
u1=(int)t&0x3f; \
275
u2&=0x3f; \
276
LL^=des_SPtrans[1][u1]; \
277
LL^=des_SPtrans[3][u2]; \
278
s1=(int)t>>16; \
279
s2=(int)t>>24L; \
280
s1&=0x3f; \
281
s2&=0x3f; \
282
LL^=des_SPtrans[5][s1]; \
283
LL^=des_SPtrans[7][s2]; }
284
#endif /* DES_RISC2 */
285
286
#else /* DES_RISC1 || DES_RISC2 */
287
288
#define D_ENCRYPT(LL,R,S) {\
289
LOAD_DATA_tmp(R,S,u,t,E0,E1); \
290
t=ROTATE(t,4); \
291
LL^=\
292
des_SPtrans[0][(u>> 2L)&0x3f]^ \
293
des_SPtrans[2][(u>>10L)&0x3f]^ \
294
des_SPtrans[4][(u>>18L)&0x3f]^ \
295
des_SPtrans[6][(u>>26L)&0x3f]^ \
296
des_SPtrans[1][(t>> 2L)&0x3f]^ \
297
des_SPtrans[3][(t>>10L)&0x3f]^ \
298
des_SPtrans[5][(t>>18L)&0x3f]^ \
299
des_SPtrans[7][(t>>26L)&0x3f]; }
300
#endif /* DES_RISC1 || DES_RISC2 */
301
#endif /* DES_PTR */
302
303
/* IP and FP
304
* The problem is more of a geometric problem that random bit fiddling.
305
0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
306
8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
307
16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
308
24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
309
310
32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
311
40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
312
48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
313
56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
314
315
The output has been subject to swaps of the form
316
0 1 -> 3 1 but the odd and even bits have been put into
317
2 3 2 0
318
different words. The main trick is to remember that
319
t=((l>>size)^r)&(mask);
320
r^=t;
321
l^=(t<<size);
322
can be used to swap and move bits between words.
323
324
So l = 0 1 2 3 r = 16 17 18 19
325
4 5 6 7 20 21 22 23
326
8 9 10 11 24 25 26 27
327
12 13 14 15 28 29 30 31
328
becomes (for size == 2 and mask == 0x3333)
329
t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
330
6^20 7^21 -- -- 4 5 20 21 6 7 22 23
331
10^24 11^25 -- -- 8 9 24 25 10 11 24 25
332
14^28 15^29 -- -- 12 13 28 29 14 15 28 29
333
334
Thanks for hints from Richard Outerbridge - he told me IP&FP
335
could be done in 15 xor, 10 shifts and 5 ands.
336
When I finally started to think of the problem in 2D
337
I first got ~42 operations without xors. When I remembered
338
how to use xors :-) I got it to its final state.
339
*/
340
#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
341
(b)^=(t),\
342
(a)^=((t)<<(n)))
343
344
#define IP(l,r) \
345
{ \
346
register DES_LONG tt; \
347
PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
348
PERM_OP(l,r,tt,16,0x0000ffffL); \
349
PERM_OP(r,l,tt, 2,0x33333333L); \
350
PERM_OP(l,r,tt, 8,0x00ff00ffL); \
351
PERM_OP(r,l,tt, 1,0x55555555L); \
352
}
353
354
#define FP(l,r) \
355
{ \
356
register DES_LONG tt; \
357
PERM_OP(l,r,tt, 1,0x55555555L); \
358
PERM_OP(r,l,tt, 8,0x00ff00ffL); \
359
PERM_OP(l,r,tt, 2,0x33333333L); \
360
PERM_OP(r,l,tt,16,0x0000ffffL); \
361
PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
362
}
363
#endif
364
365