|
|
1.1.1.6 root 1: /* Deprecated/legacy */
2:
1.1 root 3: /* crypto/des/set_key.c */
4: /* Copyright (C) 1995-1997 Eric Young ([email protected])
5: * All rights reserved.
6: *
7: * This package is an SSL implementation written
8: * by Eric Young ([email protected]).
9: * The implementation was written so as to conform with Netscapes SSL.
10: *
11: * This library is free for commercial and non-commercial use as long as
12: * the following conditions are aheared to. The following conditions
13: * apply to all code found in this distribution, be it the RC4, RSA,
14: * lhash, DES, etc., code; not just the SSL code. The SSL documentation
15: * included with this distribution is covered by the same copyright terms
16: * except that the holder is Tim Hudson ([email protected]).
17: *
18: * Copyright remains Eric Young's, and as such any Copyright notices in
19: * the code are not to be removed.
20: * If this package is used in a product, Eric Young should be given attribution
21: * as the author of the parts of the library used.
22: * This can be in the form of a textual message at program startup or
23: * in documentation (online or textual) provided with the package.
24: *
25: * Redistribution and use in source and binary forms, with or without
26: * modification, are permitted provided that the following conditions
27: * are met:
28: * 1. Redistributions of source code must retain the copyright
29: * notice, this list of conditions and the following disclaimer.
30: * 2. Redistributions in binary form must reproduce the above copyright
31: * notice, this list of conditions and the following disclaimer in the
32: * documentation and/or other materials provided with the distribution.
33: * 3. All advertising materials mentioning features or use of this software
34: * must display the following acknowledgement:
35: * "This product includes cryptographic software written by
36: * Eric Young ([email protected])"
37: * The word 'cryptographic' can be left out if the rouines from the library
38: * being used are not cryptographic related :-).
39: * 4. If you include any Windows specific code (or a derivative thereof) from
40: * the apps directory (application code) you must include an acknowledgement:
41: * "This product includes software written by Tim Hudson ([email protected])"
42: *
43: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53: * SUCH DAMAGE.
54: *
55: * The licence and distribution terms for any publically available version or
56: * derivative of this code cannot be changed. i.e. this code cannot simply be
57: * copied and put under another distribution licence
58: * [including the GNU Public Licence.]
59: */
60:
61: /* set_key.c v 1.4 eay 24/9/91
62: * 1.4 Speed up by 400% :-)
63: * 1.3 added register declarations.
64: * 1.2 unrolled make_key_sched a bit more
65: * 1.1 added norm_expand_bits
66: * 1.0 First working version
67: */
68:
1.1.1.2 root 69: /* Adapted for TrueCrypt by the TrueCrypt Foundation */
1.1 root 70:
1.1.1.2 root 71: #include "Des_locl.h"
72: #include "Podd.h"
73: #include "Sk.h"
1.1 root 74:
1.1.1.2 root 75: #pragma intrinsic(memcmp,_lrotr)
1.1 root 76:
77: #ifndef NOPROTO
78: static int check_parity(des_cblock (*key));
79: #else
80: static int check_parity();
81: #endif
82:
1.1.1.2 root 83: int des_check_key=1;
1.1 root 84:
85: void des_set_odd_parity(key)
86: des_cblock (*key);
87: {
88: int i;
89:
1.1.1.2 root 90: for (i=0; i<(int)DES_KEY_SZ; i++)
1.1 root 91: (*key)[i]=odd_parity[(*key)[i]];
92: }
93:
94: static int check_parity(key)
95: des_cblock (*key);
96: {
97: int i;
98:
1.1.1.2 root 99: for (i=0; i<(int)DES_KEY_SZ; i++)
1.1 root 100: {
101: if ((*key)[i] != odd_parity[(*key)[i]])
102: return(0);
103: }
104: return(1);
105: }
106:
107: /* Weak and semi week keys as take from
108: * %A D.W. Davies
109: * %A W.L. Price
110: * %T Security for Computer Networks
111: * %I John Wiley & Sons
112: * %D 1984
113: * Many thanks to [email protected] (Steven Bellovin) for the reference
114: * (and actual cblock values).
115: */
1.1.1.3 root 116: #define NUM_WEAK_KEY 18
1.1 root 117: static des_cblock weak_keys[NUM_WEAK_KEY]={
118: /* weak keys */
119: {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
120: {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
1.1.1.3 root 121: {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
122: {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
1.1 root 123: {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
124: {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
125: /* semi-weak keys */
126: {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
127: {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
128: {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
129: {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
130: {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
131: {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
132: {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
133: {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
134: {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
135: {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
136: {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
137: {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
138:
139: int des_is_weak_key(key)
140: des_cblock (*key);
1.1.1.3 root 141: {
1.1 root 142: int i;
143:
144: for (i=0; i<NUM_WEAK_KEY; i++)
1.1.1.3 root 145: {
1.1 root 146: /* Added == 0 to comparision, I obviously don't run
147: * this section very often :-(, thanks to
148: * [email protected] for the fix
149: * eay 93/06/29
150: * Another problem, I was comparing only the first 4
1.1.1.3 root 151: * bytes, 97/03/18
152: * Parity bits are ignored, TF 2006-04-12 */
1.1.1.4 root 153: if (((*((__int64 *) weak_keys[i]) ^ *((__int64 *) key)) & 0xFEFEFEFEFEFEFEFEULL) == 0)
1.1.1.3 root 154: return(1);
1.1 root 155: }
1.1.1.3 root 156: return(0);
157: }
1.1 root 158:
159: /* NOW DEFINED IN des_local.h
160: * See ecb_encrypt.c for a pseudo description of these macros.
161: * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
162: * (b)^=(t),\
163: * (a)=((a)^((t)<<(n))))
164: */
165:
166: #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
167: (a)=(a)^(t)^(t>>(16-(n))))
168:
169: /* return 0 if key parity is odd (correct),
170: * return -1 if key parity error,
171: * return -2 if illegal weak key.
172: */
173: int des_set_key(key, schedule)
174: des_cblock (*key);
175: des_key_schedule schedule;
176: {
177: static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
178: register DES_LONG c,d,t,s,t2;
179: register unsigned char *in;
180: register DES_LONG *k;
181: register int i;
182:
183: k=(DES_LONG *)schedule;
184: in=(unsigned char *)key;
185:
186: c2l(in,c);
187: c2l(in,d);
188:
189: /* do PC1 in 60 simple operations */
190: /* PERM_OP(d,c,t,4,0x0f0f0f0fL);
191: HPERM_OP(c,t,-2, 0xcccc0000L);
192: HPERM_OP(c,t,-1, 0xaaaa0000L);
193: HPERM_OP(c,t, 8, 0x00ff0000L);
194: HPERM_OP(c,t,-1, 0xaaaa0000L);
195: HPERM_OP(d,t,-8, 0xff000000L);
196: HPERM_OP(d,t, 8, 0x00ff0000L);
197: HPERM_OP(d,t, 2, 0x33330000L);
198: d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
199: d=(d>>8)|((c&0xf0000000L)>>4);
200: c&=0x0fffffffL; */
201:
202: /* I now do it in 47 simple operations :-)
203: * Thanks to John Fletcher ([email protected])
204: * for the inspiration. :-) */
205: PERM_OP (d,c,t,4,0x0f0f0f0fL);
206: HPERM_OP(c,t,-2,0xcccc0000L);
207: HPERM_OP(d,t,-2,0xcccc0000L);
208: PERM_OP (d,c,t,1,0x55555555L);
209: PERM_OP (c,d,t,8,0x00ff00ffL);
210: PERM_OP (d,c,t,1,0x55555555L);
211: d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
212: ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
213: c&=0x0fffffffL;
214:
215: for (i=0; i<ITERATIONS; i++)
216: {
217: if (shifts2[i])
218: { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
219: else
220: { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
221: c&=0x0fffffffL;
222: d&=0x0fffffffL;
223: /* could be a few less shifts but I am to lazy at this
224: * point in time to investigate */
225: s= des_skb[0][ (c )&0x3f ]|
226: des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
227: des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
228: des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
229: ((c>>22L)&0x38)];
230: t= des_skb[4][ (d )&0x3f ]|
231: des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
232: des_skb[6][ (d>>15L)&0x3f ]|
233: des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
234:
235: /* table contained 0213 4657 */
236: t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
237: *(k++)=ROTATE(t2,30)&0xffffffffL;
238:
239: t2=((s>>16L)|(t&0xffff0000L));
240: *(k++)=ROTATE(t2,26)&0xffffffffL;
241: }
1.1.1.2 root 242:
243: if (des_check_key)
244: {
245: //if (!check_parity(key))
246: // return(-1);
247:
248: if (des_is_weak_key(key))
249: return(-2);
250: }
251:
1.1 root 252: return(0);
253: }
254:
1.1.1.5 root 255: int des_key_sched(key, schedule)
1.1 root 256: des_cblock (*key);
257: des_key_schedule schedule;
258: {
259: return(des_set_key(key,schedule));
260: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.