Annotation of truecrypt/crypto/set_key.c, revision 1.1.1.5

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.