Annotation of truecrypt/crypto/des_enc.c, revision 1.1.1.3

1.1       root        1: /* crypto/des/des_enc.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: 
1.1.1.2   root       59: /* Adapted for TrueCrypt by the TrueCrypt Foundation */
1.1       root       60: 
1.1.1.2   root       61: #include "Des_locl.h"
1.1.1.3 ! root       62: #include "Common/Endian.h"
1.1       root       63: 
                     64: void des_encrypt(data, ks, enc)
                     65: DES_LONG *data;
                     66: des_key_schedule ks;
                     67: int enc;
                     68:        {
                     69:        register DES_LONG l,r,t,u;
                     70: #ifdef DES_PTR
                     71:        register unsigned char *des_SP=(unsigned char *)des_SPtrans;
                     72: #endif
                     73: #ifndef DES_UNROLL
                     74:        register int i;
                     75: #endif
                     76:        register DES_LONG *s;
                     77: 
1.1.1.2   root       78:        r=LE32(data[0]);
                     79:        l=LE32(data[1]);
1.1       root       80: 
                     81:        IP(r,l);
                     82:        /* Things have been modified so that the initial rotate is
                     83:         * done outside the loop.  This required the
                     84:         * des_SPtrans values in sp.h to be rotated 1 bit to the right.
                     85:         * One perl script later and things have a 5% speed up on a sparc2.
                     86:         * Thanks to Richard Outerbridge <[email protected]>
                     87:         * for pointing this out. */
                     88:        /* clear the top bits on machines with 8byte longs */
                     89:        /* shift left by 2 */
                     90:        r=ROTATE(r,29)&0xffffffffL;
                     91:        l=ROTATE(l,29)&0xffffffffL;
                     92: 
                     93:        s=(DES_LONG *)ks;
                     94:        /* I don't know if it is worth the effort of loop unrolling the
                     95:         * inner loop */
                     96:        if (enc)
                     97:                {
                     98: #ifdef DES_UNROLL
                     99:                D_ENCRYPT(l,r, 0); /*  1 */
                    100:                D_ENCRYPT(r,l, 2); /*  2 */
                    101:                D_ENCRYPT(l,r, 4); /*  3 */
                    102:                D_ENCRYPT(r,l, 6); /*  4 */
                    103:                D_ENCRYPT(l,r, 8); /*  5 */
                    104:                D_ENCRYPT(r,l,10); /*  6 */
                    105:                D_ENCRYPT(l,r,12); /*  7 */
                    106:                D_ENCRYPT(r,l,14); /*  8 */
                    107:                D_ENCRYPT(l,r,16); /*  9 */
                    108:                D_ENCRYPT(r,l,18); /*  10 */
                    109:                D_ENCRYPT(l,r,20); /*  11 */
                    110:                D_ENCRYPT(r,l,22); /*  12 */
                    111:                D_ENCRYPT(l,r,24); /*  13 */
                    112:                D_ENCRYPT(r,l,26); /*  14 */
                    113:                D_ENCRYPT(l,r,28); /*  15 */
                    114:                D_ENCRYPT(r,l,30); /*  16 */
                    115: #else
                    116:                for (i=0; i<32; i+=8)
                    117:                        {
                    118:                        D_ENCRYPT(l,r,i+0); /*  1 */
                    119:                        D_ENCRYPT(r,l,i+2); /*  2 */
                    120:                        D_ENCRYPT(l,r,i+4); /*  3 */
                    121:                        D_ENCRYPT(r,l,i+6); /*  4 */
                    122:                        }
                    123: #endif
                    124:                }
                    125:        else
                    126:                {
                    127: #ifdef DES_UNROLL
                    128:                D_ENCRYPT(l,r,30); /* 16 */
                    129:                D_ENCRYPT(r,l,28); /* 15 */
                    130:                D_ENCRYPT(l,r,26); /* 14 */
                    131:                D_ENCRYPT(r,l,24); /* 13 */
                    132:                D_ENCRYPT(l,r,22); /* 12 */
                    133:                D_ENCRYPT(r,l,20); /* 11 */
                    134:                D_ENCRYPT(l,r,18); /* 10 */
                    135:                D_ENCRYPT(r,l,16); /*  9 */
                    136:                D_ENCRYPT(l,r,14); /*  8 */
                    137:                D_ENCRYPT(r,l,12); /*  7 */
                    138:                D_ENCRYPT(l,r,10); /*  6 */
                    139:                D_ENCRYPT(r,l, 8); /*  5 */
                    140:                D_ENCRYPT(l,r, 6); /*  4 */
                    141:                D_ENCRYPT(r,l, 4); /*  3 */
                    142:                D_ENCRYPT(l,r, 2); /*  2 */
                    143:                D_ENCRYPT(r,l, 0); /*  1 */
                    144: #else
                    145:                for (i=30; i>0; i-=8)
                    146:                        {
                    147:                        D_ENCRYPT(l,r,i-0); /* 16 */
                    148:                        D_ENCRYPT(r,l,i-2); /* 15 */
                    149:                        D_ENCRYPT(l,r,i-4); /* 14 */
                    150:                        D_ENCRYPT(r,l,i-6); /* 13 */
                    151:                        }
                    152: #endif
                    153:                }
                    154: 
                    155:        /* rotate and clear the top bits on machines with 8byte longs */
                    156:        l=ROTATE(l,3)&0xffffffffL;
                    157:        r=ROTATE(r,3)&0xffffffffL;
                    158: 
                    159:        FP(r,l);
1.1.1.2   root      160:        data[0]=LE32(l);
                    161:        data[1]=LE32(r);
1.1       root      162:        l=r=t=u=0;
                    163:        }
                    164: 
                    165: void des_encrypt2(data, ks, enc)
                    166: DES_LONG *data;
                    167: des_key_schedule ks;
                    168: int enc;
                    169:        {
                    170:        register DES_LONG l,r,t,u;
                    171: #ifdef DES_PTR
                    172:        register unsigned char *des_SP=(unsigned char *)des_SPtrans;
                    173: #endif
                    174: #ifndef DES_UNROLL
                    175:        register int i;
                    176: #endif
                    177:        register DES_LONG *s;
                    178: 
                    179:        r=data[0];
                    180:        l=data[1];
                    181: 
                    182:        /* Things have been modified so that the initial rotate is
                    183:         * done outside the loop.  This required the
                    184:         * des_SPtrans values in sp.h to be rotated 1 bit to the right.
                    185:         * One perl script later and things have a 5% speed up on a sparc2.
                    186:         * Thanks to Richard Outerbridge <[email protected]>
                    187:         * for pointing this out. */
                    188:        /* clear the top bits on machines with 8byte longs */
                    189:        r=ROTATE(r,29)&0xffffffffL;
                    190:        l=ROTATE(l,29)&0xffffffffL;
                    191: 
                    192:        s=(DES_LONG *)ks;
                    193:        /* I don't know if it is worth the effort of loop unrolling the
                    194:         * inner loop */
                    195:        if (enc)
                    196:                {
                    197: #ifdef DES_UNROLL
                    198:                D_ENCRYPT(l,r, 0); /*  1 */
                    199:                D_ENCRYPT(r,l, 2); /*  2 */
                    200:                D_ENCRYPT(l,r, 4); /*  3 */
                    201:                D_ENCRYPT(r,l, 6); /*  4 */
                    202:                D_ENCRYPT(l,r, 8); /*  5 */
                    203:                D_ENCRYPT(r,l,10); /*  6 */
                    204:                D_ENCRYPT(l,r,12); /*  7 */
                    205:                D_ENCRYPT(r,l,14); /*  8 */
                    206:                D_ENCRYPT(l,r,16); /*  9 */
                    207:                D_ENCRYPT(r,l,18); /*  10 */
                    208:                D_ENCRYPT(l,r,20); /*  11 */
                    209:                D_ENCRYPT(r,l,22); /*  12 */
                    210:                D_ENCRYPT(l,r,24); /*  13 */
                    211:                D_ENCRYPT(r,l,26); /*  14 */
                    212:                D_ENCRYPT(l,r,28); /*  15 */
                    213:                D_ENCRYPT(r,l,30); /*  16 */
                    214: #else
                    215:                for (i=0; i<32; i+=8)
                    216:                        {
                    217:                        D_ENCRYPT(l,r,i+0); /*  1 */
                    218:                        D_ENCRYPT(r,l,i+2); /*  2 */
                    219:                        D_ENCRYPT(l,r,i+4); /*  3 */
                    220:                        D_ENCRYPT(r,l,i+6); /*  4 */
                    221:                        }
                    222: #endif
                    223:                }
                    224:        else
                    225:                {
                    226: #ifdef DES_UNROLL
                    227:                D_ENCRYPT(l,r,30); /* 16 */
                    228:                D_ENCRYPT(r,l,28); /* 15 */
                    229:                D_ENCRYPT(l,r,26); /* 14 */
                    230:                D_ENCRYPT(r,l,24); /* 13 */
                    231:                D_ENCRYPT(l,r,22); /* 12 */
                    232:                D_ENCRYPT(r,l,20); /* 11 */
                    233:                D_ENCRYPT(l,r,18); /* 10 */
                    234:                D_ENCRYPT(r,l,16); /*  9 */
                    235:                D_ENCRYPT(l,r,14); /*  8 */
                    236:                D_ENCRYPT(r,l,12); /*  7 */
                    237:                D_ENCRYPT(l,r,10); /*  6 */
                    238:                D_ENCRYPT(r,l, 8); /*  5 */
                    239:                D_ENCRYPT(l,r, 6); /*  4 */
                    240:                D_ENCRYPT(r,l, 4); /*  3 */
                    241:                D_ENCRYPT(l,r, 2); /*  2 */
                    242:                D_ENCRYPT(r,l, 0); /*  1 */
                    243: #else
                    244:                for (i=30; i>0; i-=8)
                    245:                        {
                    246:                        D_ENCRYPT(l,r,i-0); /* 16 */
                    247:                        D_ENCRYPT(r,l,i-2); /* 15 */
                    248:                        D_ENCRYPT(l,r,i-4); /* 14 */
                    249:                        D_ENCRYPT(r,l,i-6); /* 13 */
                    250:                        }
                    251: #endif
                    252:                }
                    253:        /* rotate and clear the top bits on machines with 8byte longs */
                    254:        data[0]=ROTATE(l,3)&0xffffffffL;
                    255:        data[1]=ROTATE(r,3)&0xffffffffL;
                    256:        l=r=t=u=0;
                    257:        }
                    258: 
                    259: void des_encrypt3(data,ks1,ks2,ks3)
                    260: DES_LONG *data;
                    261: des_key_schedule ks1;
                    262: des_key_schedule ks2;
                    263: des_key_schedule ks3;
                    264:        {
                    265:        register DES_LONG l,r;
                    266: 
                    267:        l=data[0];
                    268:        r=data[1];
                    269:        IP(l,r);
                    270:        data[0]=l;
                    271:        data[1]=r;
                    272:        des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
                    273:        des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
                    274:        des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
                    275:        l=data[0];
                    276:        r=data[1];
                    277:        FP(r,l);
                    278:        data[0]=l;
                    279:        data[1]=r;
                    280:        }
                    281: 
                    282: void des_decrypt3(data,ks1,ks2,ks3)
                    283: DES_LONG *data;
                    284: des_key_schedule ks1;
                    285: des_key_schedule ks2;
                    286: des_key_schedule ks3;
                    287:        {
                    288:        register DES_LONG l,r;
                    289: 
                    290:        l=data[0];
                    291:        r=data[1];
                    292:        IP(l,r);
                    293:        data[0]=l;
                    294:        data[1]=r;
                    295:        des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
                    296:        des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
                    297:        des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
                    298:        l=data[0];
                    299:        r=data[1];
                    300:        FP(r,l);
                    301:        data[0]=l;
                    302:        data[1]=r;
                    303:        }
                    304: 
                    305: #ifndef DES_DEFAULT_OPTIONS
                    306: 
                    307: void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
                    308: des_cblock (*input);
                    309: des_cblock (*output);
                    310: long length;
                    311: des_key_schedule schedule;
                    312: des_cblock (*ivec);
                    313: int enc;
                    314:        {
                    315:        register DES_LONG tin0,tin1;
                    316:        register DES_LONG tout0,tout1,xor0,xor1;
                    317:        register unsigned char *in,*out;
                    318:        register long l=length;
                    319:        DES_LONG tin[2];
                    320:        unsigned char *iv;
                    321: 
                    322:        in=(unsigned char *)input;
                    323:        out=(unsigned char *)output;
                    324:        iv=(unsigned char *)ivec;
                    325: 
                    326:        if (enc)
                    327:                {
                    328:                c2l(iv,tout0);
                    329:                c2l(iv,tout1);
                    330:                for (l-=8; l>=0; l-=8)
                    331:                        {
                    332:                        c2l(in,tin0);
                    333:                        c2l(in,tin1);
                    334:                        tin0^=tout0; tin[0]=tin0;
                    335:                        tin1^=tout1; tin[1]=tin1;
                    336:                        des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
                    337:                        tout0=tin[0]; l2c(tout0,out);
                    338:                        tout1=tin[1]; l2c(tout1,out);
                    339:                        }
                    340:                if (l != -8)
                    341:                        {
                    342:                        c2ln(in,tin0,tin1,l+8);
                    343:                        tin0^=tout0; tin[0]=tin0;
                    344:                        tin1^=tout1; tin[1]=tin1;
                    345:                        des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
                    346:                        tout0=tin[0]; l2c(tout0,out);
                    347:                        tout1=tin[1]; l2c(tout1,out);
                    348:                        }
                    349:                iv=(unsigned char *)ivec;
                    350:                l2c(tout0,iv);
                    351:                l2c(tout1,iv);
                    352:                }
                    353:        else
                    354:                {
                    355:                c2l(iv,xor0);
                    356:                c2l(iv,xor1);
                    357:                for (l-=8; l>=0; l-=8)
                    358:                        {
                    359:                        c2l(in,tin0); tin[0]=tin0;
                    360:                        c2l(in,tin1); tin[1]=tin1;
                    361:                        des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
                    362:                        tout0=tin[0]^xor0;
                    363:                        tout1=tin[1]^xor1;
                    364:                        l2c(tout0,out);
                    365:                        l2c(tout1,out);
                    366:                        xor0=tin0;
                    367:                        xor1=tin1;
                    368:                        }
                    369:                if (l != -8)
                    370:                        {
                    371:                        c2l(in,tin0); tin[0]=tin0;
                    372:                        c2l(in,tin1); tin[1]=tin1;
                    373:                        des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
                    374:                        tout0=tin[0]^xor0;
                    375:                        tout1=tin[1]^xor1;
                    376:                        l2cn(tout0,tout1,out,l+8);
                    377:                        xor0=tin0;
                    378:                        xor1=tin1;
                    379:                        }
                    380: 
                    381:                iv=(unsigned char *)ivec;
                    382:                l2c(xor0,iv);
                    383:                l2c(xor1,iv);
                    384:                }
                    385:        tin0=tin1=tout0=tout1=xor0=xor1=0;
                    386:        tin[0]=tin[1]=0;
                    387:        }
                    388: 
                    389: void des_ede3_cbc_encrypt(input, output, length, ks1, ks2, ks3, ivec, enc)
                    390: des_cblock (*input);
                    391: des_cblock (*output);
                    392: long length;
                    393: des_key_schedule ks1;
                    394: des_key_schedule ks2;
                    395: des_key_schedule ks3;
                    396: des_cblock (*ivec);
                    397: int enc;
                    398:        {
                    399:        register DES_LONG tin0,tin1;
                    400:        register DES_LONG tout0,tout1,xor0,xor1;
                    401:        register unsigned char *in,*out;
                    402:        register long l=length;
                    403:        DES_LONG tin[2];
                    404:        unsigned char *iv;
                    405: 
                    406:        in=(unsigned char *)input;
                    407:        out=(unsigned char *)output;
                    408:        iv=(unsigned char *)ivec;
                    409: 
                    410:        if (enc)
                    411:                {
                    412:                c2l(iv,tout0);
                    413:                c2l(iv,tout1);
                    414:                for (l-=8; l>=0; l-=8)
                    415:                        {
                    416:                        c2l(in,tin0);
                    417:                        c2l(in,tin1);
                    418:                        tin0^=tout0;
                    419:                        tin1^=tout1;
                    420: 
                    421:                        tin[0]=tin0;
                    422:                        tin[1]=tin1;
                    423:                        des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
                    424:                        tout0=tin[0];
                    425:                        tout1=tin[1];
                    426: 
                    427:                        l2c(tout0,out);
                    428:                        l2c(tout1,out);
                    429:                        }
                    430:                if (l != -8)
                    431:                        {
                    432:                        c2ln(in,tin0,tin1,l+8);
                    433:                        tin0^=tout0;
                    434:                        tin1^=tout1;
                    435: 
                    436:                        tin[0]=tin0;
                    437:                        tin[1]=tin1;
                    438:                        des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
                    439:                        tout0=tin[0];
                    440:                        tout1=tin[1];
                    441: 
                    442:                        l2c(tout0,out);
                    443:                        l2c(tout1,out);
                    444:                        }
                    445:                iv=(unsigned char *)ivec;
                    446:                l2c(tout0,iv);
                    447:                l2c(tout1,iv);
                    448:                }
                    449:        else
                    450:                {
                    451:                register DES_LONG t0,t1;
                    452: 
                    453:                c2l(iv,xor0);
                    454:                c2l(iv,xor1);
                    455:                for (l-=8; l>=0; l-=8)
                    456:                        {
                    457:                        c2l(in,tin0);
                    458:                        c2l(in,tin1);
                    459: 
                    460:                        t0=tin0;
                    461:                        t1=tin1;
                    462: 
                    463:                        tin[0]=tin0;
                    464:                        tin[1]=tin1;
                    465:                        des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
                    466:                        tout0=tin[0];
                    467:                        tout1=tin[1];
                    468: 
                    469:                        tout0^=xor0;
                    470:                        tout1^=xor1;
                    471:                        l2c(tout0,out);
                    472:                        l2c(tout1,out);
                    473:                        xor0=t0;
                    474:                        xor1=t1;
                    475:                        }
                    476:                if (l != -8)
                    477:                        {
                    478:                        c2l(in,tin0);
                    479:                        c2l(in,tin1);
                    480:                        
                    481:                        t0=tin0;
                    482:                        t1=tin1;
                    483: 
                    484:                        tin[0]=tin0;
                    485:                        tin[1]=tin1;
                    486:                        des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
                    487:                        tout0=tin[0];
                    488:                        tout1=tin[1];
                    489:                
                    490:                        tout0^=xor0;
                    491:                        tout1^=xor1;
                    492:                        l2cn(tout0,tout1,out,l+8);
                    493:                        xor0=t0;
                    494:                        xor1=t1;
                    495:                        }
                    496: 
                    497:                iv=(unsigned char *)ivec;
                    498:                l2c(xor0,iv);
                    499:                l2c(xor1,iv);
                    500:                }
                    501:        tin0=tin1=tout0=tout1=xor0=xor1=0;
                    502:        tin[0]=tin[1]=0;
                    503:        }
                    504: 
                    505: #endif /* DES_DEFAULT_OPTIONS */

unix.superglobalmegacorp.com

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