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

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