|
|
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: #ifdef LINUX_DRIVER 76: #include <linux/string.h> 77: #else 78: #pragma intrinsic(memcmp,_lrotr) 79: #endif 1.1 root 80: 81: #ifndef NOPROTO 82: static int check_parity(des_cblock (*key)); 83: #else 84: static int check_parity(); 85: #endif 86: 1.1.1.2 root 87: int des_check_key=1; 1.1 root 88: 89: void des_set_odd_parity(key) 90: des_cblock (*key); 91: { 92: int i; 93: 1.1.1.2 root 94: for (i=0; i<(int)DES_KEY_SZ; i++) 1.1 root 95: (*key)[i]=odd_parity[(*key)[i]]; 96: } 97: 98: static int check_parity(key) 99: des_cblock (*key); 100: { 101: int i; 102: 1.1.1.2 root 103: for (i=0; i<(int)DES_KEY_SZ; i++) 1.1 root 104: { 105: if ((*key)[i] != odd_parity[(*key)[i]]) 106: return(0); 107: } 108: return(1); 109: } 110: 111: /* Weak and semi week keys as take from 112: * %A D.W. Davies 113: * %A W.L. Price 114: * %T Security for Computer Networks 115: * %I John Wiley & Sons 116: * %D 1984 117: * Many thanks to [email protected] (Steven Bellovin) for the reference 118: * (and actual cblock values). 119: */ 1.1.1.3 root 120: #define NUM_WEAK_KEY 18 1.1 root 121: static des_cblock weak_keys[NUM_WEAK_KEY]={ 122: /* weak keys */ 123: {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}, 124: {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE}, 1.1.1.3 root 125: {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E}, 126: {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1}, 1.1 root 127: {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}, 128: {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0}, 129: /* semi-weak keys */ 130: {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE}, 131: {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01}, 132: {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1}, 133: {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E}, 134: {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1}, 135: {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01}, 136: {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE}, 137: {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E}, 138: {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E}, 139: {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01}, 140: {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE}, 141: {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}}; 142: 143: int des_is_weak_key(key) 144: des_cblock (*key); 1.1.1.3 root 145: { 1.1 root 146: int i; 147: 148: for (i=0; i<NUM_WEAK_KEY; i++) 1.1.1.3 root 149: { 1.1 root 150: /* Added == 0 to comparision, I obviously don't run 151: * this section very often :-(, thanks to 152: * [email protected] for the fix 153: * eay 93/06/29 154: * Another problem, I was comparing only the first 4 1.1.1.3 root 155: * bytes, 97/03/18 156: * Parity bits are ignored, TF 2006-04-12 */ 1.1.1.4 root 157: if (((*((__int64 *) weak_keys[i]) ^ *((__int64 *) key)) & 0xFEFEFEFEFEFEFEFEULL) == 0) 1.1.1.3 root 158: return(1); 1.1 root 159: } 1.1.1.3 root 160: return(0); 161: } 1.1 root 162: 163: /* NOW DEFINED IN des_local.h 164: * See ecb_encrypt.c for a pseudo description of these macros. 165: * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\ 166: * (b)^=(t),\ 167: * (a)=((a)^((t)<<(n)))) 168: */ 169: 170: #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\ 171: (a)=(a)^(t)^(t>>(16-(n)))) 172: 173: /* return 0 if key parity is odd (correct), 174: * return -1 if key parity error, 175: * return -2 if illegal weak key. 176: */ 177: int des_set_key(key, schedule) 178: des_cblock (*key); 179: des_key_schedule schedule; 180: { 181: static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0}; 182: register DES_LONG c,d,t,s,t2; 183: register unsigned char *in; 184: register DES_LONG *k; 185: register int i; 186: 187: k=(DES_LONG *)schedule; 188: in=(unsigned char *)key; 189: 190: c2l(in,c); 191: c2l(in,d); 192: 193: /* do PC1 in 60 simple operations */ 194: /* PERM_OP(d,c,t,4,0x0f0f0f0fL); 195: HPERM_OP(c,t,-2, 0xcccc0000L); 196: HPERM_OP(c,t,-1, 0xaaaa0000L); 197: HPERM_OP(c,t, 8, 0x00ff0000L); 198: HPERM_OP(c,t,-1, 0xaaaa0000L); 199: HPERM_OP(d,t,-8, 0xff000000L); 200: HPERM_OP(d,t, 8, 0x00ff0000L); 201: HPERM_OP(d,t, 2, 0x33330000L); 202: d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L); 203: d=(d>>8)|((c&0xf0000000L)>>4); 204: c&=0x0fffffffL; */ 205: 206: /* I now do it in 47 simple operations :-) 207: * Thanks to John Fletcher ([email protected]) 208: * for the inspiration. :-) */ 209: PERM_OP (d,c,t,4,0x0f0f0f0fL); 210: HPERM_OP(c,t,-2,0xcccc0000L); 211: HPERM_OP(d,t,-2,0xcccc0000L); 212: PERM_OP (d,c,t,1,0x55555555L); 213: PERM_OP (c,d,t,8,0x00ff00ffL); 214: PERM_OP (d,c,t,1,0x55555555L); 215: d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) | 216: ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L)); 217: c&=0x0fffffffL; 218: 219: for (i=0; i<ITERATIONS; i++) 220: { 221: if (shifts2[i]) 222: { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); } 223: else 224: { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); } 225: c&=0x0fffffffL; 226: d&=0x0fffffffL; 227: /* could be a few less shifts but I am to lazy at this 228: * point in time to investigate */ 229: s= des_skb[0][ (c )&0x3f ]| 230: des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]| 231: des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]| 232: des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) | 233: ((c>>22L)&0x38)]; 234: t= des_skb[4][ (d )&0x3f ]| 235: des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]| 236: des_skb[6][ (d>>15L)&0x3f ]| 237: des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)]; 238: 239: /* table contained 0213 4657 */ 240: t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL; 241: *(k++)=ROTATE(t2,30)&0xffffffffL; 242: 243: t2=((s>>16L)|(t&0xffff0000L)); 244: *(k++)=ROTATE(t2,26)&0xffffffffL; 245: } 1.1.1.2 root 246: 247: if (des_check_key) 248: { 249: //if (!check_parity(key)) 250: // return(-1); 251: 252: if (des_is_weak_key(key)) 253: return(-2); 254: } 255: 1.1 root 256: return(0); 257: } 258: 1.1.1.5 root 259: int des_key_sched(key, schedule) 1.1 root 260: des_cblock (*key); 261: des_key_schedule schedule; 262: { 263: return(des_set_key(key,schedule)); 264: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.