Annotation of truecrypt/common/crypto.c, revision 1.1.1.9

1.1.1.7   root        1: /* Legal Notice: The source code contained in this file has been derived from
                      2:    the source code of Encryption for the Masses 2.02a, which is Copyright (c)
                      3:    1998-99 Paul Le Roux and which is covered by the 'License Agreement for
                      4:    Encryption for the Masses'. Modifications and additions to that source code
1.1.1.9 ! root        5:    contained in this file are Copyright (c) 2004-2006 TrueCrypt Foundation and
1.1.1.7   root        6:    Copyright (c) 2004 TrueCrypt Team, and are covered by TrueCrypt License 2.0
                      7:    the full text of which is contained in the file License.txt included in
                      8:    TrueCrypt binary and source code distribution archives.  */
                      9: 
                     10: #include "Tcdefs.h"
                     11: #include "Crypto.h"
                     12: #include "Crc.h"
                     13: #include "Endian.h"
                     14: 
                     15: #ifdef LINUX_DRIVER
1.1.1.8   root       16: #include <linux/module.h>
1.1.1.7   root       17: #include <linux/string.h>
                     18: #endif
1.1.1.5   root       19: 
                     20: /* Update the following when adding a new cipher or EA:
                     21: 
                     22:    Crypto.h:
                     23:      ID #define
                     24:      MAX_EXPANDED_KEY #define
                     25: 
                     26:    Crypto.c:
                     27:      Ciphers[]
                     28:      EncryptionAlgorithms[]
                     29:      CipherInit()
                     30:      EncipherBlock()
                     31:      DecipherBlock()
1.1.1.7   root       32: 
1.1.1.5   root       33: */
                     34: 
                     35: // Cipher configuration
                     36: static Cipher Ciphers[] =
                     37: {
1.1.1.7   root       38: //                                                             Block Size      Key Size        Key Schedule Size
                     39: //       ID            Name                    (Bytes)         (Bytes)         (Bytes)
1.1.1.5   root       40:        { AES,          "AES",                  16,                     32,                     sizeof(aes_encrypt_ctx)+sizeof(aes_decrypt_ctx) },
                     41:        { BLOWFISH,     "Blowfish",             8,                      56,                     4168                                                                                    },
                     42:        { CAST,         "CAST5",                8,                      16,                     128                                                                                             },
                     43:        { DES56,        "DES",                  8,                      7,                      128                                                                                             },
                     44:        { SERPENT,      "Serpent",              16,                     32,                     140*4                                                                                   },
1.1.1.7   root       45:        { TRIPLEDES,"Triple DES",       8,                      8*3,            128*3                                                                                   },
1.1.1.5   root       46:        { TWOFISH,      "Twofish",              16,                     32,                     TWOFISH_KS                                                                              },
                     47:        { 0,            0,                              0,                      0,                      0                                                                                               }
                     48: };
                     49: 
                     50: // Encryption algorithm configuration
1.1.1.8   root       51: // The following modes have been deprecated (legacy):  CBC, INNER_CBC, OUTER_CBC
1.1.1.5   root       52: static EncryptionAlgorithm EncryptionAlgorithms[] =
                     53: {
1.1.1.8   root       54:        //  Cipher(s)                     Modes
                     55:        { { 0,                                          0 } , { 0, 0, 0 }                       },      // Must be all-zero
                     56:        { { AES,                                        0 } , { LRW, CBC, 0 }           },
                     57:        { { BLOWFISH,                           0 } , { LRW, CBC, 0 }           },
                     58:        { { CAST,                                       0 } , { LRW, CBC, 0 }           },
                     59:        { { SERPENT,                            0 } , { LRW, CBC, 0 }           },
                     60:        { { TRIPLEDES,                          0 } , { LRW, CBC, 0 }           },
                     61:        { { TWOFISH,                            0 } , { LRW, CBC, 0 }           },
                     62:        { { TWOFISH, AES,                       0 } , { LRW, OUTER_CBC, 0 }     },
                     63:        { { SERPENT, TWOFISH, AES,      0 } , { LRW, OUTER_CBC, 0 }     },
                     64:        { { AES, SERPENT,                       0 } , { LRW, OUTER_CBC, 0 }     },
                     65:        { { AES, TWOFISH, SERPENT,      0 } , { LRW, OUTER_CBC, 0 }     },
                     66:        { { SERPENT, TWOFISH,           0 } , { LRW, OUTER_CBC, 0 }     },
1.1.1.9 ! root       67:        { { BLOWFISH, AES,                      0 } , { INNER_CBC, 0, 0 }       },
        !            68:        { { SERPENT, BLOWFISH, AES,     0 } , { INNER_CBC, 0, 0 }       },
1.1.1.8   root       69:        { { 0,                                          0 } , { 0, 0, 0 }                       }       // Must be all-zero
1.1.1.5   root       70: };
                     71: 
1.1.1.9 ! root       72: // Hash algorithms
        !            73: static Hash Hashes[] =
        !            74: {
        !            75:        { RIPEMD160, "RIPEMD-160" },
        !            76:        { SHA1, "SHA-1" },
        !            77:        { WHIRLPOOL, "Whirlpool" },
        !            78:        { 0, 0 }
        !            79: };
1.1.1.8   root       80: 
1.1.1.7   root       81: /* Return values: 0 = success, ERR_CIPHER_INIT_FAILURE (fatal), ERR_CIPHER_INIT_WEAK_KEY (non-fatal) */
1.1.1.8   root       82: int CipherInit (int cipher, unsigned char *key, unsigned __int8 *ks)
1.1.1.5   root       83: {
1.1.1.7   root       84:        int retVal = 0;
                     85: 
1.1.1.5   root       86:        switch (cipher)
                     87:        {
                     88:        case BLOWFISH:
                     89:                BF_set_key ((BF_KEY *)ks, CipherGetKeySize(BLOWFISH), key);
                     90:                break;
                     91: 
                     92:        case AES:
1.1.1.8   root       93:                if (aes_encrypt_key(key, CipherGetKeySize(AES), (aes_encrypt_ctx *) ks) != EXIT_SUCCESS)
1.1.1.7   root       94:                        return ERR_CIPHER_INIT_FAILURE;
                     95: 
1.1.1.8   root       96:                if (aes_decrypt_key(key, CipherGetKeySize(AES), (aes_decrypt_ctx *) (ks + sizeof(aes_encrypt_ctx))) != EXIT_SUCCESS)
1.1.1.7   root       97:                        return ERR_CIPHER_INIT_FAILURE;
                     98: 
1.1.1.5   root       99:                break;
                    100: 
1.1.1.7   root      101:        case DES56:             
                    102:                /* Included for testing purposes only */
                    103:                switch (des_key_sched ((des_cblock *) key, (struct des_ks_struct *) ks))
                    104:                {
                    105:                case -1:
                    106:                        return ERR_CIPHER_INIT_FAILURE;
                    107:                case -2:
                    108:                        retVal = ERR_CIPHER_INIT_WEAK_KEY;              // Non-fatal error
                    109:                        break;
                    110:                }
1.1.1.5   root      111:                break;
                    112: 
                    113:        case CAST:
                    114:                CAST_set_key((CAST_KEY *) ks, CipherGetKeySize(CAST), key);
                    115:                break;
                    116: 
                    117:        case SERPENT:
                    118:                serpent_set_key (key, CipherGetKeySize(SERPENT) * 8, ks);
                    119:                break;
                    120: 
                    121:        case TRIPLEDES:
1.1.1.7   root      122:                switch (des_key_sched ((des_cblock *) key, (struct des_ks_struct *) ks))
                    123:                {
                    124:                case -1:
                    125:                        return ERR_CIPHER_INIT_FAILURE;
                    126:                case -2:
                    127:                        retVal = ERR_CIPHER_INIT_WEAK_KEY;              // Non-fatal error
                    128:                        break;
                    129:                }
                    130:                switch (des_key_sched ((des_cblock *) ((char*)(key)+8), (struct des_ks_struct *) (ks + CipherGetKeyScheduleSize (DES56))))
                    131:                {
                    132:                case -1:
                    133:                        return ERR_CIPHER_INIT_FAILURE;
                    134:                case -2:
                    135:                        retVal = ERR_CIPHER_INIT_WEAK_KEY;              // Non-fatal error
                    136:                        break;
                    137:                }
                    138:                switch (des_key_sched ((des_cblock *) ((char*)(key)+16), (struct des_ks_struct *) (ks + CipherGetKeyScheduleSize (DES56) * 2)))
                    139:                {
                    140:                case -1:
                    141:                        return ERR_CIPHER_INIT_FAILURE;
                    142:                case -2:
                    143:                        retVal = ERR_CIPHER_INIT_WEAK_KEY;              // Non-fatal error
                    144:                        break;
                    145:                }
1.1.1.9 ! root      146: 
        !           147:                // Verify whether all three DES keys are mutually different
        !           148:                if (((*((__int64 *) key) ^ *((__int64 *) key+1)) & 0xFEFEFEFEFEFEFEFE) == 0
        !           149:                || ((*((__int64 *) key+1) ^ *((__int64 *) key+2)) & 0xFEFEFEFEFEFEFEFE) == 0
        !           150:                || ((*((__int64 *) key) ^ *((__int64 *) key+2)) & 0xFEFEFEFEFEFEFEFE) == 0)
        !           151:                        retVal = ERR_CIPHER_INIT_WEAK_KEY;              // Non-fatal error
        !           152: 
1.1.1.5   root      153:                break;
                    154: 
                    155:        case TWOFISH:
                    156:                twofish_set_key ((TwofishInstance *)ks, (const u4byte *)key, CipherGetKeySize(TWOFISH) * 8);
                    157:                break;
                    158: 
                    159:        }
1.1.1.7   root      160:        return retVal;
1.1.1.5   root      161: }
                    162: 
                    163: void EncipherBlock(int cipher, void *data, void *ks)
                    164: {
                    165:        switch (cipher)
                    166:        {
1.1.1.7   root      167:        case BLOWFISH:          BF_ecb_le_encrypt (data, data, ks, 1); break;
1.1.1.5   root      168:        case AES:                       aes_encrypt (data, data, ks); break;
                    169:        case DES56:                     des_encrypt (data, ks, 1); break;
                    170:        case CAST:                      CAST_ecb_encrypt (data, data, ks, 1); break;
                    171:        case SERPENT:           serpent_encrypt (data, data, ks); break;
                    172:        case TRIPLEDES:         des_ecb3_encrypt (data, data, ks,
                    173:                                                (void*)((char*) ks + CipherGetKeyScheduleSize (DES56)), (void*)((char*) ks + CipherGetKeyScheduleSize (DES56) * 2), 1); break;
                    174:        case TWOFISH:           twofish_encrypt (ks, data, data); break;
                    175:        }
                    176: }
                    177: 
                    178: void DecipherBlock(int cipher, void *data, void *ks)
                    179: {
                    180:        switch (cipher)
                    181:        {
1.1.1.7   root      182:        case BLOWFISH:  BF_ecb_le_encrypt (data, data, ks, 0); break;
1.1.1.5   root      183:        case AES:               aes_decrypt (data, data, (void *) ((char *) ks + sizeof(aes_encrypt_ctx))); break;
                    184:        case DES56:             des_encrypt (data, ks, 0); break;
                    185:        case CAST:              CAST_ecb_encrypt (data, data, ks,0); break;
                    186:        case SERPENT:   serpent_decrypt (data, data, ks); break;
                    187:        case TRIPLEDES: des_ecb3_encrypt (data, data, ks,
                    188:                                        (void*)((char*) ks + CipherGetKeyScheduleSize (DES56)),
                    189:                                        (void*)((char*) ks + CipherGetKeyScheduleSize (DES56) * 2), 0); break;
                    190:        case TWOFISH:   twofish_decrypt (ks, data, data); break;
                    191:        }
                    192: }
                    193: 
                    194: // Ciphers support
                    195: 
                    196: Cipher *CipherGet (int id)
                    197: {
                    198:        int i;
                    199:        for (i = 0; Ciphers[i].Id != 0; i++)
                    200:                if (Ciphers[i].Id == id)
                    201:                        return &Ciphers[i];
                    202: 
                    203:        return 0;
                    204: }
                    205: 
                    206: char *CipherGetName (int cipherId)
                    207: {
                    208:        return CipherGet (cipherId) -> Name;
                    209: }
                    210: 
                    211: int CipherGetBlockSize (int cipherId)
                    212: {
                    213:        return CipherGet (cipherId) -> BlockSize;
                    214: }
                    215: 
                    216: int CipherGetKeySize (int cipherId)
                    217: {
                    218:        return CipherGet (cipherId) -> KeySize;
                    219: }
                    220: 
                    221: int CipherGetKeyScheduleSize (int cipherId)
                    222: {
                    223:        return CipherGet (cipherId) -> KeyScheduleSize;
                    224: }
                    225: 
                    226: 
                    227: // Encryption algorithms support
                    228: 
                    229: int EAGetFirst ()
                    230: {
                    231:        return 1;
                    232: }
                    233: 
                    234: // Returns number of EAs
                    235: int EAGetCount (void)
                    236: {
                    237:        int ea, count = 0;
                    238: 
                    239:        for (ea = EAGetFirst (); ea != 0; ea = EAGetNext (ea))
                    240:        {
                    241:                count++;
                    242:        }
                    243:        return count;
                    244: }
                    245: 
                    246: int EAGetNext (int previousEA)
                    247: {
                    248:        int id = previousEA + 1;
                    249:        if (EncryptionAlgorithms[id].Ciphers[0] != 0) return id;
                    250:        return 0;
                    251: }
                    252: 
1.1.1.8   root      253: 
                    254: // Return values: 0 = success, ERR_CIPHER_INIT_FAILURE (fatal), ERR_CIPHER_INIT_WEAK_KEY (non-fatal)
                    255: int EAInit (int ea, unsigned char *key, unsigned __int8 *ks)
1.1.1.5   root      256: {
1.1.1.7   root      257:        int c, retVal = 0;
1.1.1.5   root      258: 
                    259:        for (c = EAGetFirstCipher (ea); c != 0; c = EAGetNextCipher (ea, c))
                    260:        {
1.1.1.7   root      261:                switch (CipherInit (c, key, ks))
                    262:                {
                    263:                case ERR_CIPHER_INIT_FAILURE:
                    264:                        return ERR_CIPHER_INIT_FAILURE;
                    265: 
                    266:                case ERR_CIPHER_INIT_WEAK_KEY:
                    267:                        retVal = ERR_CIPHER_INIT_WEAK_KEY;              // Non-fatal error
                    268:                        break;
                    269:                }
1.1.1.5   root      270: 
                    271:                key += CipherGetKeySize (c);
                    272:                ks += CipherGetKeyScheduleSize (c);
                    273:        }
1.1.1.7   root      274:        return retVal;
1.1.1.5   root      275: }
                    276: 
1.1.1.8   root      277: 
                    278: int EAInitMode (PCRYPTO_INFO ci)
                    279: {
                    280:        switch (ci->mode)
                    281:        {
                    282:        case LRW:
                    283:                switch (CipherGetBlockSize (EAGetFirstCipher (ci->ea)))
                    284:                {
                    285:                case 8:
                    286:                        return Gf64TabInit (ci->iv, &ci->gf_ctx);
                    287: 
                    288:                case 16:
                    289:                        return Gf128Tab64Init (ci->iv, &ci->gf_ctx);
                    290: 
                    291:                default:
                    292:                        return FALSE;
                    293:                }
                    294:        }
                    295: 
                    296:        return TRUE;
                    297: }
                    298: 
                    299: 
1.1.1.5   root      300: // Returns name of EA, cascaded cipher names are separated by hyphens
                    301: char *EAGetName (char *buf, int ea)
                    302: {
                    303:        int i = EAGetLastCipher(ea);
                    304:        strcpy (buf, CipherGetName (i));
                    305: 
                    306:        while (i = EAGetPreviousCipher(ea, i))
                    307:        {
                    308:                strcat (buf, "-");
                    309:                strcat (buf, CipherGetName (i));
                    310:        }
                    311: 
                    312:        return buf;
                    313: }
                    314: 
1.1.1.8   root      315: 
                    316: int EAGetByName (char *name)
                    317: {
                    318:        int ea = EAGetFirst ();
                    319:        char n[128];
                    320: 
                    321:        do
                    322:        {
                    323:                EAGetName (n, ea);
                    324:                if (strcmp (n, name) == 0)
                    325:                        return ea;
                    326:        }
                    327:        while (ea = EAGetNext (ea));
                    328: 
                    329:        return 0;
                    330: }
                    331: 
                    332: 
1.1.1.5   root      333: // Returns sum of key sizes of all EA ciphers
                    334: int EAGetKeySize (int ea)
                    335: {
1.1.1.8   root      336:        int i = EAGetFirstCipher (ea);
1.1.1.5   root      337:        int size = CipherGetKeySize (i);
                    338: 
1.1.1.8   root      339:        while (i = EAGetNextCipher (ea, i))
1.1.1.5   root      340:        {
                    341:                size += CipherGetKeySize (i);
                    342:        }
                    343: 
                    344:        return size;
                    345: }
                    346: 
1.1.1.8   root      347: 
                    348: // Returns the first mode of operation of EA
                    349: int EAGetFirstMode (int ea)
                    350: {
                    351:        return (EncryptionAlgorithms[ea].Modes[0]);
                    352: }
                    353: 
                    354: 
                    355: int EAGetNextMode (int ea, int previousModeId)
1.1.1.5   root      356: {
1.1.1.8   root      357:        int c, i = 0;
                    358:        while (c = EncryptionAlgorithms[ea].Modes[i++])
                    359:        {
                    360:                if (c == previousModeId) 
                    361:                        return EncryptionAlgorithms[ea].Modes[i];
                    362:        }
                    363: 
                    364:        return 0;
1.1.1.5   root      365: }
                    366: 
1.1.1.8   root      367: 
1.1.1.5   root      368: // Returns the name of the mode of operation of the whole EA
1.1.1.8   root      369: char *EAGetModeName (int ea, int mode, BOOL capitalLetters)
1.1.1.5   root      370: {
1.1.1.8   root      371:        switch (mode)
1.1.1.5   root      372:        {
1.1.1.8   root      373:        case LRW:
                    374:                return "LRW";
                    375: 
1.1.1.5   root      376:        case CBC:
1.1.1.7   root      377:                {
1.1.1.8   root      378:                        /* Deprecated/legacy */
                    379: 
1.1.1.7   root      380:                        char eaName[100];
                    381:                        EAGetName (eaName, ea);
1.1.1.5   root      382: 
1.1.1.7   root      383:                        if (strcmp (eaName, "Triple DES") == 0)
                    384:                                return capitalLetters ? "Outer-CBC" : "outer-CBC";
1.1.1.5   root      385: 
1.1.1.7   root      386:                        return "CBC";
                    387:                }
1.1.1.5   root      388: 
                    389:        case OUTER_CBC:
1.1.1.8   root      390: 
                    391:                /* Deprecated/legacy */
                    392: 
1.1.1.7   root      393:                return  capitalLetters ? "Outer-CBC" : "outer-CBC";
1.1.1.5   root      394: 
                    395:        case INNER_CBC:
1.1.1.8   root      396: 
                    397:                /* Deprecated/legacy */
                    398: 
1.1.1.7   root      399:                return capitalLetters ? "Inner-CBC" : "inner-CBC";
1.1.1.5   root      400: 
                    401:        }
1.1.1.7   root      402:        return "[unknown]";
1.1.1.5   root      403: }
                    404: 
1.1.1.8   root      405: 
1.1.1.5   root      406: // Returns sum of key schedule sizes of all EA ciphers
                    407: int EAGetKeyScheduleSize (int ea)
                    408: {
                    409:        int i = EAGetFirstCipher(ea);
                    410:        int size = CipherGetKeyScheduleSize (i);
                    411: 
                    412:        while (i = EAGetNextCipher(ea, i))
                    413:        {
                    414:                size += CipherGetKeyScheduleSize (i);
                    415:        }
                    416: 
                    417:        return size;
                    418: }
                    419: 
1.1.1.8   root      420: 
1.1.1.5   root      421: // Returns largest key needed by all EAs
                    422: int EAGetLargestKey ()
                    423: {
                    424:        int ea, key = 0;
                    425: 
                    426:        for (ea = EAGetFirst (); ea != 0 ; ea = EAGetNext (ea))
                    427:        {
                    428:                if (EAGetKeySize (ea) >= key)
                    429:                        key = EAGetKeySize (ea);
                    430:        }
                    431: 
                    432:        return key;
                    433: }
                    434: 
1.1.1.8   root      435: 
1.1.1.5   root      436: // Returns number of ciphers in EA
                    437: int EAGetCipherCount (int ea)
                    438: {
                    439:        int i = 0;
                    440:        while (EncryptionAlgorithms[ea].Ciphers[i++]);
                    441: 
                    442:        return i - 1;
                    443: }
                    444: 
                    445: 
                    446: int EAGetFirstCipher (int ea)
                    447: {
                    448:        return EncryptionAlgorithms[ea].Ciphers[0];
                    449: }
                    450: 
1.1.1.8   root      451: 
1.1.1.5   root      452: int EAGetLastCipher (int ea)
                    453: {
                    454:        int c, i = 0;
                    455:        while (c = EncryptionAlgorithms[ea].Ciphers[i++]);
                    456: 
                    457:        return EncryptionAlgorithms[ea].Ciphers[i - 2];
                    458: }
                    459: 
1.1.1.8   root      460: 
1.1.1.5   root      461: int EAGetNextCipher (int ea, int previousCipherId)
                    462: {
                    463:        int c, i = 0;
                    464:        while (c = EncryptionAlgorithms[ea].Ciphers[i++])
                    465:        {
                    466:                if (c == previousCipherId) 
                    467:                        return EncryptionAlgorithms[ea].Ciphers[i];
                    468:        }
                    469: 
                    470:        return 0;
                    471: }
                    472: 
1.1.1.8   root      473: 
1.1.1.5   root      474: int EAGetPreviousCipher (int ea, int previousCipherId)
                    475: {
                    476:        int c, i = 0;
                    477: 
                    478:        if (EncryptionAlgorithms[ea].Ciphers[i++] == previousCipherId)
                    479:                return 0;
                    480: 
                    481:        while (c = EncryptionAlgorithms[ea].Ciphers[i++])
                    482:        {
                    483:                if (c == previousCipherId) 
                    484:                        return EncryptionAlgorithms[ea].Ciphers[i - 2];
                    485:        }
                    486: 
                    487:        return 0;
                    488: }
                    489: 
1.1.1.8   root      490: 
1.1.1.9 ! root      491: Hash *HashGet (int id)
1.1.1.5   root      492: {
1.1.1.9 ! root      493:        int i;
        !           494:        for (i = 0; Hashes[i].Id != 0; i++)
        !           495:                if (Hashes[i].Id == id)
        !           496:                        return &Hashes[i];
        !           497: 
        !           498:        return 0;
        !           499: }
        !           500: 
        !           501: 
        !           502: int HashGetIdByName (char *name)
        !           503: {
        !           504:        int i;
        !           505:        for (i = 0; Hashes[i].Id != 0; i++)
        !           506:                if (strcmp (Hashes[i].Name, name) == 0)
        !           507:                        return Hashes[i].Id;
        !           508: 
        !           509:        return 0;
        !           510: }
        !           511: 
        !           512: 
        !           513: char *HashGetName (int hashId)
        !           514: {
        !           515:        return HashGet (hashId) -> Name;
1.1.1.5   root      516: }
                    517: 
1.1       root      518: 
                    519: PCRYPTO_INFO
                    520: crypto_open ()
                    521: {
                    522:        /* Do the crt allocation */
1.1.1.9 ! root      523:        PCRYPTO_INFO cryptoInfo = (PCRYPTO_INFO) TCalloc (sizeof (CRYPTO_INFO));
        !           524:        memset (cryptoInfo, 0, sizeof (CRYPTO_INFO));
        !           525: 
1.1.1.2   root      526: #ifndef DEVICE_DRIVER
1.1.1.7   root      527: #ifdef _WIN32
1.1.1.2   root      528:        VirtualLock (cryptoInfo, sizeof (CRYPTO_INFO));
                    529: #endif
1.1.1.7   root      530: #endif
1.1.1.2   root      531: 
1.1       root      532:        if (cryptoInfo == NULL)
                    533:                return NULL;
                    534: 
1.1.1.5   root      535:        cryptoInfo->ea = -1;
1.1       root      536:        return cryptoInfo;
                    537: }
                    538: 
                    539: void
                    540: crypto_loadkey (PKEY_INFO keyInfo, char *lpszUserKey, int nUserKeyLen)
                    541: {
                    542:        keyInfo->keyLength = nUserKeyLen;
                    543:        burn (keyInfo->userKey, sizeof (keyInfo->userKey));
                    544:        memcpy (keyInfo->userKey, lpszUserKey, nUserKeyLen);
                    545: }
                    546: 
                    547: void
                    548: crypto_close (PCRYPTO_INFO cryptoInfo)
                    549: {
1.1.1.7   root      550:        if (cryptoInfo != NULL)
                    551:        {
                    552:                burn (cryptoInfo, sizeof (CRYPTO_INFO));
1.1.1.2   root      553: #ifndef DEVICE_DRIVER
1.1.1.7   root      554: #ifdef _WIN32
                    555:                VirtualUnlock (cryptoInfo, sizeof (CRYPTO_INFO));
                    556: #endif
1.1.1.2   root      557: #endif
1.1.1.7   root      558:                TCfree (cryptoInfo);
                    559:        }
1.1       root      560: }
1.1.1.9 ! root      561: 
        !           562: 
        !           563: // Detect weak and potentially weak secondary LRW keys.
        !           564: // Remark: These tests reduce the key search space by approximately 0.001%
        !           565: BOOL DetectWeakSecondaryKey (unsigned char *key, int len)
        !           566: {
        !           567: #define LRW_MAX_SUCCESSIVE_IDENTICAL_BITS      24
        !           568: #define LRW_MIN_HAMMING_WEIGHT_16                      39
        !           569: #define LRW_MIN_HAMMING_WEIGHT_8                       15
        !           570: 
        !           571:        int minWeight = (len == 16 ? LRW_MIN_HAMMING_WEIGHT_16 : LRW_MIN_HAMMING_WEIGHT_8);
        !           572:        int i, b, zero = 0, one = 0, zeroTotal = 0, oneTotal = 0;
        !           573: 
        !           574:        for (i = 0; i < len; i++)
        !           575:        {
        !           576:                for (b = 7; b >= 0; b--)
        !           577:                {
        !           578:                        if ((key[i] & (1 << b)) == 0)
        !           579:                        {
        !           580:                                zeroTotal++;
        !           581:                                zero++;
        !           582:                                one = 0;
        !           583:                        }
        !           584:                        else
        !           585:                        {
        !           586:                                oneTotal++;
        !           587:                                one++;
        !           588:                                zero = 0;
        !           589:                        }
        !           590: 
        !           591:                        // Maximum number of consecutive identical bit values
        !           592:                        if (one >= LRW_MAX_SUCCESSIVE_IDENTICAL_BITS || zero >= LRW_MAX_SUCCESSIVE_IDENTICAL_BITS)
        !           593:                                return TRUE;
        !           594:                }
        !           595:        }
        !           596: 
        !           597:        // Minimum and maximum Hamming weight
        !           598:        if (zeroTotal < minWeight || oneTotal < minWeight)
        !           599:                return TRUE;
        !           600: 
        !           601:        return FALSE;
        !           602: }
        !           603: 
1.1       root      604: 
1.1.1.8   root      605: // Initializes IV and whitening values for sector encryption/decryption in CBC mode.
                    606: // IMPORTANT: This function has been deprecated (legacy).
1.1.1.5   root      607: static void 
                    608: InitSectorIVAndWhitening (unsigned __int64 secNo,
                    609:        int blockSize,
1.1.1.7   root      610:        unsigned __int32 *iv,
1.1.1.5   root      611:        unsigned __int64 *ivSeed,
1.1.1.7   root      612:        unsigned __int32 *whitening)
1.1       root      613: {
1.1.1.8   root      614: 
                    615:        /* IMPORTANT: This function has been deprecated (legacy) */
                    616: 
1.1.1.5   root      617:        unsigned __int64 iv64[4];
1.1.1.7   root      618:        unsigned __int32 *iv32 = (unsigned __int32 *) iv64;
1.1.1.5   root      619: 
1.1.1.7   root      620:        iv64[0] = ivSeed[0] ^ LE64(secNo);
                    621:        iv64[1] = ivSeed[1] ^ LE64(secNo);
                    622:        iv64[2] = ivSeed[2] ^ LE64(secNo);
1.1.1.5   root      623:        if (blockSize == 16)
1.1       root      624:        {
1.1.1.7   root      625:                iv64[3] = ivSeed[3] ^ LE64(secNo);
1.1.1.5   root      626:        }
                    627: 
                    628:        iv[0] = iv32[0];
                    629:        iv[1] = iv32[1];
                    630: 
                    631:        switch (blockSize)
                    632:        {
                    633:        case 16:
                    634: 
                    635:                // 128-bit block
                    636: 
                    637:                iv[2] = iv32[2];
                    638:                iv[3] = iv32[3];
                    639: 
1.1.1.7   root      640:                whitening[0] = LE32( crc32int ( &iv32[4] ) ^ crc32int ( &iv32[7] ) );
                    641:                whitening[1] = LE32( crc32int ( &iv32[5] ) ^ crc32int ( &iv32[6] ) );
1.1.1.5   root      642:                break;
                    643: 
                    644:        case 8:
                    645: 
                    646:                // 64-bit block
                    647: 
1.1.1.7   root      648:                whitening[0] = LE32( crc32int ( &iv32[2] ) ^ crc32int ( &iv32[5] ) );
                    649:                whitening[1] = LE32( crc32int ( &iv32[3] ) ^ crc32int ( &iv32[4] ) );
1.1.1.5   root      650:                break;
1.1       root      651:        }
                    652: }
                    653: 
1.1.1.5   root      654: 
1.1.1.8   root      655: // EncryptBufferCBC    (deprecated/legacy)
1.1.1.5   root      656: //
                    657: // data:               data to be encrypted
                    658: // len:                        number of bytes to encrypt (must be divisible by the largest cipher block size)
                    659: // ks:                 scheduled key
                    660: // iv:                 IV
                    661: // whitening:  whitening constants
                    662: // ea:                 outer-CBC cascade ID (0 = CBC/inner-CBC)
                    663: // cipher:             CBC/inner-CBC cipher ID (0 = outer-CBC)
                    664: 
                    665: static void
1.1.1.7   root      666: EncryptBufferCBC (unsigned __int32 *data, 
                    667:                 unsigned int len,
1.1.1.8   root      668:                 unsigned __int8 *ks,
1.1.1.7   root      669:                 unsigned __int32 *iv,
                    670:                 unsigned __int32 *whitening,
1.1.1.5   root      671:                 int ea,
                    672:                 int cipher)
1.1       root      673: {
1.1.1.8   root      674:        /* IMPORTANT: This function has been deprecated (legacy) */
                    675: 
1.1.1.7   root      676:        unsigned __int32 bufIV[4];
1.1.1.5   root      677:        unsigned __int64 i;
                    678:        int blockSize = CipherGetBlockSize (ea != 0 ? EAGetFirstCipher (ea) : cipher);
                    679: 
                    680:        //  IV
                    681:        bufIV[0] = iv[0];
                    682:        bufIV[1] = iv[1];
                    683:        if (blockSize == 16)
                    684:        {
                    685:                bufIV[2] = iv[2];
                    686:                bufIV[3] = iv[3];
                    687:        }
                    688: 
                    689:        // Encrypt each block
                    690:        for (i = 0; i < len/blockSize; i++)
                    691:        {
                    692:                // CBC
                    693:                data[0] ^= bufIV[0];
                    694:                data[1] ^= bufIV[1];
                    695:                if (blockSize == 16)
                    696:                {
                    697:                        data[2] ^= bufIV[2];
                    698:                        data[3] ^= bufIV[3];
                    699:                }
                    700: 
                    701:                if (ea != 0)
                    702:                {
                    703:                        // Outer-CBC
                    704:                        for (cipher = EAGetFirstCipher (ea); cipher != 0; cipher = EAGetNextCipher (ea, cipher))
                    705:                        {
                    706:                                EncipherBlock (cipher, data, ks);
                    707:                                ks += CipherGetKeyScheduleSize (cipher);
                    708:                        }
                    709:                        ks -= EAGetKeyScheduleSize (ea);
                    710:                }
                    711:                else
                    712:                {
                    713:                        // CBC/inner-CBC
                    714:                        EncipherBlock (cipher, data, ks);
                    715:                }
                    716: 
                    717:                // CBC
                    718:                bufIV[0] = data[0];
                    719:                bufIV[1] = data[1];
                    720:                if (blockSize == 16)
                    721:                {
                    722:                        bufIV[2] = data[2];
                    723:                        bufIV[3] = data[3];
                    724:                }
                    725: 
                    726:                // Whitening
                    727:                data[0] ^= whitening[0];
                    728:                data[1] ^= whitening[1];
                    729:                if (blockSize == 16)
                    730:                {
                    731:                        data[2] ^= whitening[0];
                    732:                        data[3] ^= whitening[1];
                    733:                }
                    734: 
1.1.1.7   root      735:                data += blockSize / sizeof(*data);
1.1.1.5   root      736:        }
1.1       root      737: }
                    738: 
1.1.1.5   root      739: 
1.1.1.8   root      740: // DecryptBufferCBC  (deprecated/legacy)
1.1.1.5   root      741: //
                    742: // data:               data to be decrypted
                    743: // len:                        number of bytes to decrypt (must be divisible by the largest cipher block size)
                    744: // ks:                 scheduled key
                    745: // iv:                 IV
                    746: // whitening:  whitening constants
                    747: // ea:                 outer-CBC cascade ID (0 = CBC/inner-CBC)
                    748: // cipher:             CBC/inner-CBC cipher ID (0 = outer-CBC)
                    749: 
                    750: static void
1.1.1.7   root      751: DecryptBufferCBC (unsigned __int32 *data,
                    752:                 unsigned int len,
1.1.1.8   root      753:                 unsigned __int8 *ks,
1.1.1.7   root      754:                 unsigned __int32 *iv,
                    755:                 unsigned __int32 *whitening,
1.1.1.5   root      756:                 int ea,
                    757:                 int cipher)
1.1       root      758: {
1.1.1.8   root      759: 
                    760:        /* IMPORTANT: This function has been deprecated (legacy) */
                    761: 
1.1.1.7   root      762:        unsigned __int32 bufIV[4];
1.1.1.5   root      763:        unsigned __int64 i;
1.1.1.7   root      764:        unsigned __int32 ct[4];
1.1.1.5   root      765:        int blockSize = CipherGetBlockSize (ea != 0 ? EAGetFirstCipher (ea) : cipher);
                    766: 
                    767:        //  IV
                    768:        bufIV[0] = iv[0];
                    769:        bufIV[1] = iv[1];
                    770:        if (blockSize == 16)
1.1       root      771:        {
1.1.1.5   root      772:                bufIV[2] = iv[2];
                    773:                bufIV[3] = iv[3];
                    774:        }
                    775: 
                    776:        // Decrypt each block
                    777:        for (i = 0; i < len/blockSize; i++)
                    778:        {
                    779:                // Dewhitening
                    780:                data[0] ^= whitening[0];
                    781:                data[1] ^= whitening[1];
                    782:                if (blockSize == 16)
                    783:                {
                    784:                        data[2] ^= whitening[0];
                    785:                        data[3] ^= whitening[1];
                    786:                }
                    787: 
                    788:                // CBC
                    789:                ct[0] = data[0];
                    790:                ct[1] = data[1];
                    791:                if (blockSize == 16)
                    792:                {
                    793:                        ct[2] = data[2];
                    794:                        ct[3] = data[3];
                    795:                }
                    796: 
                    797:                if (ea != 0)
                    798:                {
                    799:                        // Outer-CBC
                    800:                        ks += EAGetKeyScheduleSize (ea);
                    801:                        for (cipher = EAGetLastCipher (ea); cipher != 0; cipher = EAGetPreviousCipher (ea, cipher))
                    802:                        {
                    803:                                ks -= CipherGetKeyScheduleSize (cipher);
                    804:                                DecipherBlock (cipher, data, ks);
                    805:                        }
                    806:                }
                    807:                else
                    808:                {
                    809:                        // CBC/inner-CBC
                    810:                        DecipherBlock (cipher, data, ks);
                    811:                }
                    812: 
                    813:                // CBC
                    814:                data[0] ^= bufIV[0];
                    815:                data[1] ^= bufIV[1];
                    816:                bufIV[0] = ct[0];
                    817:                bufIV[1] = ct[1];
                    818:                if (blockSize == 16)
                    819:                {
                    820:                        data[2] ^= bufIV[2];
                    821:                        data[3] ^= bufIV[3];
                    822:                        bufIV[2] = ct[2];
                    823:                        bufIV[3] = ct[3];
                    824:                }
                    825: 
1.1.1.7   root      826:                data += blockSize / sizeof(*data);
1.1       root      827:        }
                    828: }
1.1.1.5   root      829: 
                    830: 
1.1.1.8   root      831: void Xor128 (unsigned __int64 *a, unsigned __int64 *b)
                    832: {
                    833:        *a++ ^= *b++;
                    834:        *a ^= *b;
                    835: }
                    836: 
                    837: 
                    838: void Xor64 (unsigned __int64 *a, unsigned __int64 *b)
                    839: {
                    840:        *a ^= *b;
                    841: }
                    842: 
                    843: 
                    844: void EncryptBufferLRW128 (unsigned __int8 *plainText, unsigned int length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo)
                    845: {
                    846:        int cipher = EAGetFirstCipher (cryptoInfo->ea);
                    847:        int cipherCount = EAGetCipherCount (cryptoInfo->ea);
                    848:        unsigned __int8 *p = plainText;
                    849:        unsigned __int8 *ks = cryptoInfo->ks;
                    850:        unsigned __int8 i[8];
                    851:        unsigned __int8 t[16];
                    852:        unsigned int b;
                    853: 
                    854:        *(unsigned __int64 *)i = BE64(blockIndex);
                    855: 
                    856:        // Note that the maximum supported volume size is 8589934592 GB  (i.e., 2^63 bytes).
                    857: 
                    858:        for (b = 0; b < length >> 4; b++)
                    859:        {
                    860:                Gf128MulBy64Tab (i, t, &cryptoInfo->gf_ctx);
                    861:                Xor128 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    862: 
                    863:                if (cipherCount > 1)
                    864:                {
                    865:                        // Cipher cascade
                    866:                        for (cipher = EAGetFirstCipher (cryptoInfo->ea);
                    867:                                cipher != 0;
                    868:                                cipher = EAGetNextCipher (cryptoInfo->ea, cipher))
                    869:                        {
                    870:                                EncipherBlock (cipher, p, ks);
                    871:                                ks += CipherGetKeyScheduleSize (cipher);
                    872:                        }
                    873:                        ks = cryptoInfo->ks;
                    874:                }
                    875:                else
                    876:                {
                    877:                        EncipherBlock (cipher, p, ks);
                    878:                }
                    879: 
                    880:                Xor128 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    881: 
                    882:                p += 16;
                    883: 
                    884:                if (i[7] != 0xff)
                    885:                        i[7]++;
                    886:                else
                    887:                        *(unsigned __int64 *)i = BE64 ( BE64(*(unsigned __int64 *)i) + 1 );
                    888:        }
                    889: 
                    890:        memset (t, 0, sizeof (t));
                    891: }
                    892: 
                    893: 
                    894: void EncryptBufferLRW64 (unsigned __int8 *plainText, unsigned int length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo)
                    895: {
                    896:        int cipher = EAGetFirstCipher (cryptoInfo->ea);
                    897:        unsigned __int8 *p = plainText;
                    898:        unsigned __int8 *ks = cryptoInfo->ks;
                    899:        unsigned __int8 i[8];
                    900:        unsigned __int8 t[8];
                    901:        unsigned int b;
                    902: 
                    903:        *(unsigned __int64 *)i = BE64(blockIndex);
                    904: 
                    905:        for (b = 0; b < length >> 3; b++)
                    906:        {
                    907:                Gf64MulTab (i, t, &cryptoInfo->gf_ctx);
                    908:                Xor64 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    909: 
                    910:                EncipherBlock (cipher, p, ks);
                    911: 
                    912:                Xor64 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    913: 
                    914:                p += 8;
                    915: 
                    916:                if (i[7] != 0xff)
                    917:                        i[7]++;
                    918:                else
                    919:                        *(unsigned __int64 *)i = BE64 ( BE64(*(unsigned __int64 *)i) + 1 );
                    920:        }
                    921: 
                    922:        memset (t, 0, sizeof (t));
                    923: }
                    924: 
                    925: 
                    926: void DecryptBufferLRW128 (unsigned __int8 *plainText, int length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo)
                    927: {
                    928:        int cipher = EAGetFirstCipher (cryptoInfo->ea);
                    929:        int cipherCount = EAGetCipherCount (cryptoInfo->ea);
                    930:        unsigned __int8 *p = plainText;
                    931:        unsigned __int8 *ks = cryptoInfo->ks;
                    932:        unsigned __int8 i[8];
                    933:        unsigned __int8 t[16];
                    934:        int b;
                    935: 
                    936:        *(unsigned __int64 *)i = BE64(blockIndex);
                    937: 
                    938:        // Note that the maximum supported volume size is 8589934592 GB  (i.e., 2^63 bytes).
                    939: 
                    940:        for (b = 0; b < length >> 4; b++)
                    941:        {
                    942:                Gf128MulBy64Tab (i, t, &cryptoInfo->gf_ctx);
                    943:                Xor128 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    944: 
                    945:                if (cipherCount > 1)
                    946:                {
                    947:                        // Cipher cascade
                    948:                        ks = cryptoInfo->ks + EAGetKeyScheduleSize (cryptoInfo->ea);
                    949: 
                    950:                        for (cipher = EAGetLastCipher (cryptoInfo->ea);
                    951:                                cipher != 0;
                    952:                                cipher = EAGetPreviousCipher (cryptoInfo->ea, cipher))
                    953:                        {
                    954:                                ks -= CipherGetKeyScheduleSize (cipher);
                    955:                                DecipherBlock (cipher, p, ks);
                    956:                        }
                    957:                }
                    958:                else
                    959:                {
                    960:                        DecipherBlock (cipher, p, ks);
                    961:                }
                    962: 
                    963:                Xor128 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    964: 
                    965:                p += 16;
                    966: 
                    967:                if (i[7] != 0xff)
                    968:                        i[7]++;
                    969:                else
                    970:                        *(unsigned __int64 *)i = BE64 ( BE64(*(unsigned __int64 *)i) + 1 );
                    971:        }
                    972: 
                    973:        memset (t, 0, sizeof (t));
                    974: }
                    975: 
                    976: 
                    977: 
                    978: void DecryptBufferLRW64 (unsigned __int8 *plainText, int length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo)
                    979: {
                    980:        int cipher = EAGetFirstCipher (cryptoInfo->ea);
                    981:        unsigned __int8 *p = plainText;
                    982:        unsigned __int8 *ks = cryptoInfo->ks;
                    983:        unsigned __int8 i[8];
                    984:        unsigned __int8 t[8];
                    985:        int b;
                    986: 
                    987:        *(unsigned __int64 *)i = BE64(blockIndex);
                    988: 
                    989:        for (b = 0; b < length >> 3; b++)
                    990:        {
                    991:                Gf64MulTab (i, t, &cryptoInfo->gf_ctx);
                    992:                Xor64 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    993: 
                    994:                DecipherBlock (cipher, p, ks);
                    995: 
                    996:                Xor64 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    997: 
                    998:                p += 8;
                    999: 
                   1000:                if (i[7] != 0xff)
                   1001:                        i[7]++;
                   1002:                else
                   1003:                        *(unsigned __int64 *)i = BE64 ( BE64(*(unsigned __int64 *)i) + 1 );
                   1004:        }
                   1005: 
                   1006:        memset (t, 0, sizeof (t));
                   1007: }
                   1008: 
                   1009: 
1.1.1.5   root     1010: // EncryptBuffer
                   1011: //
                   1012: // buf:                        data to be encrypted
                   1013: // len:                        number of bytes to encrypt; must be divisible by the block size (for cascaded
                   1014: //              ciphers divisible by the largest block size used within the cascade)
                   1015: 
                   1016: void 
1.1.1.7   root     1017: EncryptBuffer (unsigned __int32 *buf,
1.1.1.5   root     1018:                           unsigned __int64 len,
1.1.1.8   root     1019:                           PCRYPTO_INFO cryptoInfo)
1.1.1.5   root     1020: {
                   1021: 
1.1.1.8   root     1022:        switch (cryptoInfo->mode)
1.1.1.5   root     1023:        {
1.1.1.8   root     1024:        case LRW:
                   1025:                switch (CipherGetBlockSize (EAGetFirstCipher (cryptoInfo->ea)))
                   1026:                {
                   1027:                case 8:
                   1028:                        EncryptBufferLRW64 ((unsigned __int8 *)buf, (unsigned int) len, 1, cryptoInfo);
                   1029:                        break;
                   1030: 
                   1031:                case 16:
                   1032:                        EncryptBufferLRW128 ((unsigned __int8 *)buf, (unsigned int) len, 1, cryptoInfo);
                   1033:                        break;
                   1034:                }
                   1035:                break;
                   1036: 
1.1.1.5   root     1037:        case CBC:
                   1038:        case INNER_CBC:
                   1039:                {
1.1.1.8   root     1040:                        /* Deprecated/legacy */
1.1.1.5   root     1041: 
1.1.1.8   root     1042:                        unsigned __int8 *ks = cryptoInfo->ks;
                   1043:                        int cipher;
                   1044:                        for (cipher = EAGetFirstCipher (cryptoInfo->ea);
                   1045:                                cipher != 0;
                   1046:                                cipher = EAGetNextCipher (cryptoInfo->ea, cipher))
                   1047:                        {
                   1048:                                EncryptBufferCBC (buf,
                   1049:                                        (unsigned int) len,
                   1050:                                        ks,
                   1051:                                        (unsigned __int32 *) cryptoInfo->iv,
                   1052:                                        (unsigned __int32 *) &cryptoInfo->iv[8],
                   1053:                                        0,
                   1054:                                        cipher);
1.1.1.5   root     1055: 
1.1.1.8   root     1056:                                ks += CipherGetKeyScheduleSize (cipher);
                   1057:                        }
                   1058:                }
1.1.1.5   root     1059:                break;
                   1060: 
                   1061:        case OUTER_CBC:
                   1062: 
1.1.1.8   root     1063:                /* Deprecated/legacy */
                   1064: 
1.1.1.5   root     1065:                EncryptBufferCBC (buf,
1.1.1.7   root     1066:                        (unsigned int) len,
1.1.1.8   root     1067:                        cryptoInfo->ks,
                   1068:                        (unsigned __int32 *) cryptoInfo->iv,
                   1069:                        (unsigned __int32 *) &cryptoInfo->iv[8],
                   1070:                        cryptoInfo->ea,
1.1.1.5   root     1071:                        0);
                   1072: 
                   1073:                break;
                   1074:        }
                   1075: }
                   1076: 
1.1.1.8   root     1077: // Convert sector number to the index of the first LRW block in the sector.
                   1078: // Note that the maximum supported volume size is 8589934592 GB  (i.e., 2^63 bytes).
                   1079: unsigned __int64 LRWSector2Index (unsigned __int64 sector, int blockSize, PCRYPTO_INFO ci)
                   1080: {
                   1081:        if (ci->hiddenVolume)
                   1082:                sector -= ci->hiddenVolumeOffset / SECTOR_SIZE;
                   1083:        else
                   1084:                sector -= HEADER_SIZE / SECTOR_SIZE;    // Compensate for the volume header size
                   1085: 
                   1086:        switch (blockSize)
                   1087:        {
                   1088:        case 8:
                   1089:                return (sector << 6) | 1;
                   1090: 
                   1091:        case 16:
                   1092:                return (sector << 5) | 1;
                   1093:        }
                   1094: 
                   1095:        return 0;
                   1096: }
                   1097: 
                   1098: 
1.1.1.5   root     1099: // EncryptSectors
                   1100: //
                   1101: // buf:                        data to be encrypted
                   1102: // secNo:              sector number relative to volume start
                   1103: // noSectors:  number of sectors in buffer
                   1104: // ks:                 scheduled key
                   1105: // iv:                 IV
                   1106: // ea:                 encryption algorithm
                   1107: 
                   1108: void _cdecl 
1.1.1.7   root     1109: EncryptSectors (unsigned __int32 *buf,
1.1.1.5   root     1110:                unsigned __int64 secNo,
                   1111:                unsigned __int64 noSectors,
1.1.1.8   root     1112:                PCRYPTO_INFO ci)
                   1113: {
                   1114:        int ea = ci->ea;
                   1115:        void *iv = ci->iv;                                      // Deprecated/legacy
                   1116:        unsigned __int8 *ks = ci->ks;
                   1117:        unsigned __int64 *iv64 = (unsigned __int64 *) iv;       // Deprecated/legacy
                   1118:        unsigned __int32 sectorIV[4];           // Deprecated/legacy
                   1119:        unsigned __int32 secWhitening[2];       // Deprecated/legacy
1.1.1.5   root     1120:        int cipher;
                   1121: 
1.1.1.8   root     1122:        switch (ci->mode)
1.1.1.5   root     1123:        {
1.1.1.8   root     1124:        case LRW:
                   1125:                {
                   1126:                        switch (CipherGetBlockSize (EAGetFirstCipher (ea)))
                   1127:                        {
                   1128:                        case 8:
                   1129:                                EncryptBufferLRW64 ((unsigned __int8 *)buf,
                   1130:                                        (unsigned int) noSectors * SECTOR_SIZE,
                   1131:                                        LRWSector2Index (secNo, 8, ci),
                   1132:                                        ci);
                   1133:                                break;
                   1134: 
                   1135:                        case 16:
                   1136:                                EncryptBufferLRW128 ((unsigned __int8 *)buf,
                   1137:                                        (unsigned int) noSectors * SECTOR_SIZE,
                   1138:                                        LRWSector2Index (secNo, 16, ci),
                   1139:                                        ci);
                   1140:                                break;
                   1141:                        }
                   1142:                }
                   1143:                break;
                   1144: 
1.1.1.5   root     1145:        case CBC:
                   1146:        case INNER_CBC:
                   1147: 
1.1.1.8   root     1148:                /* Deprecated/legacy */
                   1149: 
1.1.1.5   root     1150:                while (noSectors--)
                   1151:                {
                   1152:                        for (cipher = EAGetFirstCipher (ea); cipher != 0; cipher = EAGetNextCipher (ea, cipher))
                   1153:                        {
                   1154:                                InitSectorIVAndWhitening (secNo, CipherGetBlockSize (cipher), sectorIV, iv64, secWhitening);
                   1155: 
                   1156:                                EncryptBufferCBC (buf,
                   1157:                                        SECTOR_SIZE,
                   1158:                                        ks,
                   1159:                                        sectorIV,
                   1160:                                        secWhitening,
                   1161:                                        0,
                   1162:                                        cipher);
                   1163: 
                   1164:                                ks += CipherGetKeyScheduleSize (cipher);
                   1165:                        }
                   1166:                        ks -= EAGetKeyScheduleSize (ea);
1.1.1.7   root     1167:                        buf += SECTOR_SIZE / sizeof(*buf);
1.1.1.5   root     1168:                        secNo++;
                   1169:                }
                   1170:                break;
                   1171: 
                   1172:        case OUTER_CBC:
                   1173: 
1.1.1.8   root     1174:                /* Deprecated/legacy */
                   1175: 
1.1.1.5   root     1176:                while (noSectors--)
                   1177:                {
                   1178:                        InitSectorIVAndWhitening (secNo, CipherGetBlockSize (EAGetFirstCipher (ea)), sectorIV, iv64, secWhitening);
                   1179: 
                   1180:                        EncryptBufferCBC (buf,
                   1181:                                SECTOR_SIZE,
                   1182:                                ks,
                   1183:                                sectorIV,
                   1184:                                secWhitening,
                   1185:                                ea,
                   1186:                                0);
                   1187: 
1.1.1.7   root     1188:                        buf += SECTOR_SIZE / sizeof(*buf);
1.1.1.5   root     1189:                        secNo++;
                   1190:                }
                   1191:                break;
                   1192:        }
                   1193: }
                   1194: 
                   1195: // DecryptBuffer
                   1196: //
                   1197: // buf:                        data to be decrypted
                   1198: // len:                        number of bytes to decrypt; must be divisible by the block size (for cascaded
                   1199: //              ciphers divisible by the largest block size used within the cascade)
                   1200: void 
1.1.1.7   root     1201: DecryptBuffer (unsigned __int32 *buf,
1.1.1.5   root     1202:                unsigned __int64 len,
1.1.1.8   root     1203:                PCRYPTO_INFO cryptoInfo)
1.1.1.5   root     1204: {
1.1.1.8   root     1205:        switch (cryptoInfo->mode)
1.1.1.5   root     1206:        {
1.1.1.8   root     1207:        case LRW:
                   1208:                switch (CipherGetBlockSize (EAGetFirstCipher (cryptoInfo->ea)))
                   1209:                {
                   1210:                case 8:
                   1211:                        DecryptBufferLRW64 ((unsigned __int8 *)buf, (unsigned int) len, 1, cryptoInfo);
                   1212:                        break;
                   1213: 
                   1214:                case 16:
                   1215:                        DecryptBufferLRW128 ((unsigned __int8 *)buf, (unsigned int) len, 1, cryptoInfo);
                   1216:                        break;
                   1217:                }
                   1218:                break;
                   1219: 
1.1.1.5   root     1220:        case CBC:
                   1221:        case INNER_CBC:
                   1222:                {
                   1223: 
1.1.1.8   root     1224:                        /* Deprecated/legacy */
                   1225: 
                   1226:                        unsigned __int8 *ks = cryptoInfo->ks + EAGetKeyScheduleSize (cryptoInfo->ea);
                   1227:                        int cipher;
                   1228:                        for (cipher = EAGetLastCipher (cryptoInfo->ea);
                   1229:                                cipher != 0;
                   1230:                                cipher = EAGetPreviousCipher (cryptoInfo->ea, cipher))
                   1231:                        {
                   1232:                                ks -= CipherGetKeyScheduleSize (cipher);
                   1233: 
                   1234:                                DecryptBufferCBC (buf,
                   1235:                                        (unsigned int) len,
                   1236:                                        ks,
                   1237:                                        (unsigned __int32 *) cryptoInfo->iv,
                   1238:                                        (unsigned __int32 *) &cryptoInfo->iv[8],
                   1239:                                        0,
                   1240:                                        cipher);
                   1241:                        }
1.1.1.5   root     1242:                }
                   1243:                break;
                   1244: 
                   1245:        case OUTER_CBC:
                   1246: 
1.1.1.8   root     1247:                /* Deprecated/legacy */
                   1248: 
1.1.1.5   root     1249:                DecryptBufferCBC (buf,
1.1.1.7   root     1250:                        (unsigned int) len,
1.1.1.8   root     1251:                        cryptoInfo->ks,
                   1252:                        (unsigned __int32 *) cryptoInfo->iv,
                   1253:                        (unsigned __int32 *) &cryptoInfo->iv[8],
                   1254:                        cryptoInfo->ea,
1.1.1.5   root     1255:                        0);
                   1256: 
                   1257:                break;
                   1258:        }
                   1259: }
                   1260: 
                   1261: // DecryptSectors
                   1262: //
                   1263: // buf:                        data to be decrypted
                   1264: // secNo:              sector number relative to volume start
                   1265: // noSectors:  number of sectors in buffer
                   1266: // ks:                 scheduled key
                   1267: // iv:                 IV
                   1268: // ea:                 encryption algorithm
                   1269: 
                   1270: void _cdecl 
1.1.1.7   root     1271: DecryptSectors (unsigned __int32 *buf,
1.1.1.5   root     1272:                unsigned __int64 secNo,
                   1273:                unsigned __int64 noSectors,
1.1.1.8   root     1274:                PCRYPTO_INFO ci
                   1275: )
                   1276: {
                   1277:        int ea = ci->ea;
                   1278:        void *iv = ci->iv;                                              // Deprecated/legacy
                   1279:        unsigned __int8 *ks = ci->ks;
                   1280:        unsigned __int64 *iv64 = (unsigned __int64 *) iv;       // Deprecated/legacy
                   1281:        unsigned __int32 sectorIV[4];                   // Deprecated/legacy
                   1282:        unsigned __int32 secWhitening[2];               // Deprecated/legacy
1.1.1.5   root     1283:        int cipher;
                   1284: 
1.1.1.8   root     1285:        switch (ci->mode)
1.1.1.5   root     1286:        {
1.1.1.8   root     1287:        case LRW:
                   1288:                {
                   1289:                        switch (CipherGetBlockSize (EAGetFirstCipher (ea)))
                   1290:                        {
                   1291:                        case 8:
                   1292:                                DecryptBufferLRW64 ((unsigned __int8 *)buf,
                   1293:                                        (unsigned int) noSectors * SECTOR_SIZE,
                   1294:                                        LRWSector2Index (secNo, 8, ci),
                   1295:                                        ci);
                   1296:                                break;
                   1297: 
                   1298:                        case 16:
                   1299:                                DecryptBufferLRW128 ((unsigned __int8 *)buf,
                   1300:                                        (unsigned int) noSectors * SECTOR_SIZE,
                   1301:                                        LRWSector2Index (secNo, 16, ci),
                   1302:                                        ci);
                   1303:                                break;
                   1304:                        }
                   1305:                }
                   1306:                break;
                   1307: 
1.1.1.5   root     1308:        case CBC:
                   1309:        case INNER_CBC:
                   1310: 
1.1.1.8   root     1311:                /* Deprecated/legacy */
                   1312: 
1.1.1.5   root     1313:                while (noSectors--)
                   1314:                {
                   1315:                        ks += EAGetKeyScheduleSize (ea);
                   1316:                        for (cipher = EAGetLastCipher (ea); cipher != 0; cipher = EAGetPreviousCipher (ea, cipher))
                   1317:                        {
                   1318:                                InitSectorIVAndWhitening (secNo, CipherGetBlockSize (cipher), sectorIV, iv64, secWhitening);
                   1319: 
                   1320:                                ks -= CipherGetKeyScheduleSize (cipher);
                   1321: 
                   1322:                                DecryptBufferCBC (buf,
                   1323:                                        SECTOR_SIZE,
                   1324:                                        ks,
                   1325:                                        sectorIV,
                   1326:                                        secWhitening,
                   1327:                                        0,
                   1328:                                        cipher);
                   1329:                        }
1.1.1.7   root     1330:                        buf += SECTOR_SIZE / sizeof(*buf);
1.1.1.5   root     1331:                        secNo++;
                   1332:                }
                   1333:                break;
                   1334: 
                   1335:        case OUTER_CBC:
                   1336: 
1.1.1.8   root     1337:                /* Deprecated/legacy */
                   1338: 
1.1.1.5   root     1339:                while (noSectors--)
                   1340:                {
                   1341:                        InitSectorIVAndWhitening (secNo, CipherGetBlockSize (EAGetFirstCipher (ea)), sectorIV, iv64, secWhitening);
                   1342: 
                   1343:                        DecryptBufferCBC (buf,
                   1344:                                SECTOR_SIZE,
                   1345:                                ks,
                   1346:                                sectorIV,
                   1347:                                secWhitening,
                   1348:                                ea,
                   1349:                                0);
                   1350: 
1.1.1.7   root     1351:                        buf += SECTOR_SIZE / sizeof(*buf);
1.1.1.5   root     1352:                        secNo++;
                   1353:                }
                   1354:                break;
                   1355:        }
                   1356: }
                   1357: 

unix.superglobalmegacorp.com

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