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

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.10! root        6:    Copyright (c) 2004 TrueCrypt Team, and are covered by TrueCrypt License 2.1
1.1.1.7   root        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.10! root       84:        int retVal = ERR_SUCCESS;
1.1.1.7   root       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
1.1.1.10! root      148:                if (((*((__int64 *) key) ^ *((__int64 *) key+1)) & 0xFEFEFEFEFEFEFEFEULL) == 0
        !           149:                || ((*((__int64 *) key+1) ^ *((__int64 *) key+2)) & 0xFEFEFEFEFEFEFEFEULL) == 0
        !           150:                || ((*((__int64 *) key) ^ *((__int64 *) key+2)) & 0xFEFEFEFEFEFEFEFEULL) == 0)
1.1.1.9   root      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.10! root      257:        int c, retVal = ERR_SUCCESS;
        !           258: 
        !           259:        if (ea == 0)
        !           260:                return ERR_CIPHER_INIT_FAILURE;
1.1.1.5   root      261: 
                    262:        for (c = EAGetFirstCipher (ea); c != 0; c = EAGetNextCipher (ea, c))
                    263:        {
1.1.1.7   root      264:                switch (CipherInit (c, key, ks))
                    265:                {
                    266:                case ERR_CIPHER_INIT_FAILURE:
                    267:                        return ERR_CIPHER_INIT_FAILURE;
                    268: 
                    269:                case ERR_CIPHER_INIT_WEAK_KEY:
                    270:                        retVal = ERR_CIPHER_INIT_WEAK_KEY;              // Non-fatal error
                    271:                        break;
                    272:                }
1.1.1.5   root      273: 
                    274:                key += CipherGetKeySize (c);
                    275:                ks += CipherGetKeyScheduleSize (c);
                    276:        }
1.1.1.7   root      277:        return retVal;
1.1.1.5   root      278: }
                    279: 
1.1.1.8   root      280: 
                    281: int EAInitMode (PCRYPTO_INFO ci)
                    282: {
                    283:        switch (ci->mode)
                    284:        {
                    285:        case LRW:
                    286:                switch (CipherGetBlockSize (EAGetFirstCipher (ci->ea)))
                    287:                {
                    288:                case 8:
                    289:                        return Gf64TabInit (ci->iv, &ci->gf_ctx);
                    290: 
                    291:                case 16:
                    292:                        return Gf128Tab64Init (ci->iv, &ci->gf_ctx);
                    293: 
                    294:                default:
                    295:                        return FALSE;
                    296:                }
                    297:        }
                    298: 
                    299:        return TRUE;
                    300: }
                    301: 
                    302: 
1.1.1.5   root      303: // Returns name of EA, cascaded cipher names are separated by hyphens
                    304: char *EAGetName (char *buf, int ea)
                    305: {
                    306:        int i = EAGetLastCipher(ea);
1.1.1.10! root      307:        strcpy (buf, (i != 0) ? CipherGetName (i) : "?");
1.1.1.5   root      308: 
                    309:        while (i = EAGetPreviousCipher(ea, i))
                    310:        {
                    311:                strcat (buf, "-");
                    312:                strcat (buf, CipherGetName (i));
                    313:        }
                    314: 
                    315:        return buf;
                    316: }
                    317: 
1.1.1.8   root      318: 
                    319: int EAGetByName (char *name)
                    320: {
                    321:        int ea = EAGetFirst ();
                    322:        char n[128];
                    323: 
                    324:        do
                    325:        {
                    326:                EAGetName (n, ea);
                    327:                if (strcmp (n, name) == 0)
                    328:                        return ea;
                    329:        }
                    330:        while (ea = EAGetNext (ea));
                    331: 
                    332:        return 0;
                    333: }
                    334: 
                    335: 
1.1.1.5   root      336: // Returns sum of key sizes of all EA ciphers
                    337: int EAGetKeySize (int ea)
                    338: {
1.1.1.8   root      339:        int i = EAGetFirstCipher (ea);
1.1.1.5   root      340:        int size = CipherGetKeySize (i);
                    341: 
1.1.1.8   root      342:        while (i = EAGetNextCipher (ea, i))
1.1.1.5   root      343:        {
                    344:                size += CipherGetKeySize (i);
                    345:        }
                    346: 
                    347:        return size;
                    348: }
                    349: 
1.1.1.8   root      350: 
                    351: // Returns the first mode of operation of EA
                    352: int EAGetFirstMode (int ea)
                    353: {
                    354:        return (EncryptionAlgorithms[ea].Modes[0]);
                    355: }
                    356: 
                    357: 
                    358: int EAGetNextMode (int ea, int previousModeId)
1.1.1.5   root      359: {
1.1.1.8   root      360:        int c, i = 0;
                    361:        while (c = EncryptionAlgorithms[ea].Modes[i++])
                    362:        {
                    363:                if (c == previousModeId) 
                    364:                        return EncryptionAlgorithms[ea].Modes[i];
                    365:        }
                    366: 
                    367:        return 0;
1.1.1.5   root      368: }
                    369: 
1.1.1.8   root      370: 
1.1.1.5   root      371: // Returns the name of the mode of operation of the whole EA
1.1.1.8   root      372: char *EAGetModeName (int ea, int mode, BOOL capitalLetters)
1.1.1.5   root      373: {
1.1.1.8   root      374:        switch (mode)
1.1.1.5   root      375:        {
1.1.1.8   root      376:        case LRW:
                    377:                return "LRW";
                    378: 
1.1.1.5   root      379:        case CBC:
1.1.1.7   root      380:                {
1.1.1.8   root      381:                        /* Deprecated/legacy */
                    382: 
1.1.1.7   root      383:                        char eaName[100];
                    384:                        EAGetName (eaName, ea);
1.1.1.5   root      385: 
1.1.1.7   root      386:                        if (strcmp (eaName, "Triple DES") == 0)
                    387:                                return capitalLetters ? "Outer-CBC" : "outer-CBC";
1.1.1.5   root      388: 
1.1.1.7   root      389:                        return "CBC";
                    390:                }
1.1.1.5   root      391: 
                    392:        case OUTER_CBC:
1.1.1.8   root      393: 
                    394:                /* Deprecated/legacy */
                    395: 
1.1.1.7   root      396:                return  capitalLetters ? "Outer-CBC" : "outer-CBC";
1.1.1.5   root      397: 
                    398:        case INNER_CBC:
1.1.1.8   root      399: 
                    400:                /* Deprecated/legacy */
                    401: 
1.1.1.7   root      402:                return capitalLetters ? "Inner-CBC" : "inner-CBC";
1.1.1.5   root      403: 
                    404:        }
1.1.1.7   root      405:        return "[unknown]";
1.1.1.5   root      406: }
                    407: 
1.1.1.8   root      408: 
1.1.1.5   root      409: // Returns sum of key schedule sizes of all EA ciphers
                    410: int EAGetKeyScheduleSize (int ea)
                    411: {
                    412:        int i = EAGetFirstCipher(ea);
                    413:        int size = CipherGetKeyScheduleSize (i);
                    414: 
                    415:        while (i = EAGetNextCipher(ea, i))
                    416:        {
                    417:                size += CipherGetKeyScheduleSize (i);
                    418:        }
                    419: 
                    420:        return size;
                    421: }
                    422: 
1.1.1.8   root      423: 
1.1.1.5   root      424: // Returns largest key needed by all EAs
                    425: int EAGetLargestKey ()
                    426: {
                    427:        int ea, key = 0;
                    428: 
                    429:        for (ea = EAGetFirst (); ea != 0 ; ea = EAGetNext (ea))
                    430:        {
                    431:                if (EAGetKeySize (ea) >= key)
                    432:                        key = EAGetKeySize (ea);
                    433:        }
                    434: 
                    435:        return key;
                    436: }
                    437: 
1.1.1.8   root      438: 
1.1.1.5   root      439: // Returns number of ciphers in EA
                    440: int EAGetCipherCount (int ea)
                    441: {
                    442:        int i = 0;
                    443:        while (EncryptionAlgorithms[ea].Ciphers[i++]);
                    444: 
                    445:        return i - 1;
                    446: }
                    447: 
                    448: 
                    449: int EAGetFirstCipher (int ea)
                    450: {
                    451:        return EncryptionAlgorithms[ea].Ciphers[0];
                    452: }
                    453: 
1.1.1.8   root      454: 
1.1.1.5   root      455: int EAGetLastCipher (int ea)
                    456: {
                    457:        int c, i = 0;
                    458:        while (c = EncryptionAlgorithms[ea].Ciphers[i++]);
                    459: 
                    460:        return EncryptionAlgorithms[ea].Ciphers[i - 2];
                    461: }
                    462: 
1.1.1.8   root      463: 
1.1.1.5   root      464: int EAGetNextCipher (int ea, int previousCipherId)
                    465: {
                    466:        int c, i = 0;
                    467:        while (c = EncryptionAlgorithms[ea].Ciphers[i++])
                    468:        {
                    469:                if (c == previousCipherId) 
                    470:                        return EncryptionAlgorithms[ea].Ciphers[i];
                    471:        }
                    472: 
                    473:        return 0;
                    474: }
                    475: 
1.1.1.8   root      476: 
1.1.1.5   root      477: int EAGetPreviousCipher (int ea, int previousCipherId)
                    478: {
                    479:        int c, i = 0;
                    480: 
                    481:        if (EncryptionAlgorithms[ea].Ciphers[i++] == previousCipherId)
                    482:                return 0;
                    483: 
                    484:        while (c = EncryptionAlgorithms[ea].Ciphers[i++])
                    485:        {
                    486:                if (c == previousCipherId) 
                    487:                        return EncryptionAlgorithms[ea].Ciphers[i - 2];
                    488:        }
                    489: 
                    490:        return 0;
                    491: }
                    492: 
1.1.1.8   root      493: 
1.1.1.9   root      494: Hash *HashGet (int id)
1.1.1.5   root      495: {
1.1.1.9   root      496:        int i;
                    497:        for (i = 0; Hashes[i].Id != 0; i++)
                    498:                if (Hashes[i].Id == id)
                    499:                        return &Hashes[i];
                    500: 
                    501:        return 0;
                    502: }
                    503: 
                    504: 
                    505: int HashGetIdByName (char *name)
                    506: {
                    507:        int i;
                    508:        for (i = 0; Hashes[i].Id != 0; i++)
                    509:                if (strcmp (Hashes[i].Name, name) == 0)
                    510:                        return Hashes[i].Id;
                    511: 
                    512:        return 0;
                    513: }
                    514: 
                    515: 
                    516: char *HashGetName (int hashId)
                    517: {
                    518:        return HashGet (hashId) -> Name;
1.1.1.5   root      519: }
                    520: 
1.1       root      521: 
                    522: PCRYPTO_INFO
                    523: crypto_open ()
                    524: {
                    525:        /* Do the crt allocation */
1.1.1.9   root      526:        PCRYPTO_INFO cryptoInfo = (PCRYPTO_INFO) TCalloc (sizeof (CRYPTO_INFO));
                    527:        memset (cryptoInfo, 0, sizeof (CRYPTO_INFO));
                    528: 
1.1.1.2   root      529: #ifndef DEVICE_DRIVER
1.1.1.7   root      530: #ifdef _WIN32
1.1.1.2   root      531:        VirtualLock (cryptoInfo, sizeof (CRYPTO_INFO));
                    532: #endif
1.1.1.7   root      533: #endif
1.1.1.2   root      534: 
1.1       root      535:        if (cryptoInfo == NULL)
                    536:                return NULL;
                    537: 
1.1.1.5   root      538:        cryptoInfo->ea = -1;
1.1       root      539:        return cryptoInfo;
                    540: }
                    541: 
                    542: void
                    543: crypto_loadkey (PKEY_INFO keyInfo, char *lpszUserKey, int nUserKeyLen)
                    544: {
                    545:        keyInfo->keyLength = nUserKeyLen;
                    546:        burn (keyInfo->userKey, sizeof (keyInfo->userKey));
                    547:        memcpy (keyInfo->userKey, lpszUserKey, nUserKeyLen);
                    548: }
                    549: 
                    550: void
                    551: crypto_close (PCRYPTO_INFO cryptoInfo)
                    552: {
1.1.1.7   root      553:        if (cryptoInfo != NULL)
                    554:        {
                    555:                burn (cryptoInfo, sizeof (CRYPTO_INFO));
1.1.1.2   root      556: #ifndef DEVICE_DRIVER
1.1.1.7   root      557: #ifdef _WIN32
                    558:                VirtualUnlock (cryptoInfo, sizeof (CRYPTO_INFO));
                    559: #endif
1.1.1.2   root      560: #endif
1.1.1.7   root      561:                TCfree (cryptoInfo);
                    562:        }
1.1       root      563: }
1.1.1.9   root      564: 
                    565: 
                    566: // Detect weak and potentially weak secondary LRW keys.
                    567: // Remark: These tests reduce the key search space by approximately 0.001%
                    568: BOOL DetectWeakSecondaryKey (unsigned char *key, int len)
                    569: {
                    570: #define LRW_MAX_SUCCESSIVE_IDENTICAL_BITS      24
                    571: #define LRW_MIN_HAMMING_WEIGHT_16                      39
                    572: #define LRW_MIN_HAMMING_WEIGHT_8                       15
                    573: 
                    574:        int minWeight = (len == 16 ? LRW_MIN_HAMMING_WEIGHT_16 : LRW_MIN_HAMMING_WEIGHT_8);
                    575:        int i, b, zero = 0, one = 0, zeroTotal = 0, oneTotal = 0;
                    576: 
                    577:        for (i = 0; i < len; i++)
                    578:        {
                    579:                for (b = 7; b >= 0; b--)
                    580:                {
                    581:                        if ((key[i] & (1 << b)) == 0)
                    582:                        {
                    583:                                zeroTotal++;
                    584:                                zero++;
                    585:                                one = 0;
                    586:                        }
                    587:                        else
                    588:                        {
                    589:                                oneTotal++;
                    590:                                one++;
                    591:                                zero = 0;
                    592:                        }
                    593: 
                    594:                        // Maximum number of consecutive identical bit values
                    595:                        if (one >= LRW_MAX_SUCCESSIVE_IDENTICAL_BITS || zero >= LRW_MAX_SUCCESSIVE_IDENTICAL_BITS)
                    596:                                return TRUE;
                    597:                }
                    598:        }
                    599: 
                    600:        // Minimum and maximum Hamming weight
                    601:        if (zeroTotal < minWeight || oneTotal < minWeight)
                    602:                return TRUE;
                    603: 
                    604:        return FALSE;
                    605: }
                    606: 
1.1       root      607: 
1.1.1.8   root      608: // Initializes IV and whitening values for sector encryption/decryption in CBC mode.
                    609: // IMPORTANT: This function has been deprecated (legacy).
1.1.1.5   root      610: static void 
                    611: InitSectorIVAndWhitening (unsigned __int64 secNo,
                    612:        int blockSize,
1.1.1.7   root      613:        unsigned __int32 *iv,
1.1.1.5   root      614:        unsigned __int64 *ivSeed,
1.1.1.7   root      615:        unsigned __int32 *whitening)
1.1       root      616: {
1.1.1.8   root      617: 
                    618:        /* IMPORTANT: This function has been deprecated (legacy) */
                    619: 
1.1.1.5   root      620:        unsigned __int64 iv64[4];
1.1.1.7   root      621:        unsigned __int32 *iv32 = (unsigned __int32 *) iv64;
1.1.1.5   root      622: 
1.1.1.7   root      623:        iv64[0] = ivSeed[0] ^ LE64(secNo);
                    624:        iv64[1] = ivSeed[1] ^ LE64(secNo);
                    625:        iv64[2] = ivSeed[2] ^ LE64(secNo);
1.1.1.5   root      626:        if (blockSize == 16)
1.1       root      627:        {
1.1.1.7   root      628:                iv64[3] = ivSeed[3] ^ LE64(secNo);
1.1.1.5   root      629:        }
                    630: 
                    631:        iv[0] = iv32[0];
                    632:        iv[1] = iv32[1];
                    633: 
                    634:        switch (blockSize)
                    635:        {
                    636:        case 16:
                    637: 
                    638:                // 128-bit block
                    639: 
                    640:                iv[2] = iv32[2];
                    641:                iv[3] = iv32[3];
                    642: 
1.1.1.7   root      643:                whitening[0] = LE32( crc32int ( &iv32[4] ) ^ crc32int ( &iv32[7] ) );
                    644:                whitening[1] = LE32( crc32int ( &iv32[5] ) ^ crc32int ( &iv32[6] ) );
1.1.1.5   root      645:                break;
                    646: 
                    647:        case 8:
                    648: 
                    649:                // 64-bit block
                    650: 
1.1.1.7   root      651:                whitening[0] = LE32( crc32int ( &iv32[2] ) ^ crc32int ( &iv32[5] ) );
                    652:                whitening[1] = LE32( crc32int ( &iv32[3] ) ^ crc32int ( &iv32[4] ) );
1.1.1.5   root      653:                break;
1.1       root      654:        }
                    655: }
                    656: 
1.1.1.5   root      657: 
1.1.1.8   root      658: // EncryptBufferCBC    (deprecated/legacy)
1.1.1.5   root      659: //
                    660: // data:               data to be encrypted
                    661: // len:                        number of bytes to encrypt (must be divisible by the largest cipher block size)
                    662: // ks:                 scheduled key
                    663: // iv:                 IV
                    664: // whitening:  whitening constants
                    665: // ea:                 outer-CBC cascade ID (0 = CBC/inner-CBC)
                    666: // cipher:             CBC/inner-CBC cipher ID (0 = outer-CBC)
                    667: 
                    668: static void
1.1.1.7   root      669: EncryptBufferCBC (unsigned __int32 *data, 
                    670:                 unsigned int len,
1.1.1.8   root      671:                 unsigned __int8 *ks,
1.1.1.7   root      672:                 unsigned __int32 *iv,
                    673:                 unsigned __int32 *whitening,
1.1.1.5   root      674:                 int ea,
                    675:                 int cipher)
1.1       root      676: {
1.1.1.8   root      677:        /* IMPORTANT: This function has been deprecated (legacy) */
                    678: 
1.1.1.7   root      679:        unsigned __int32 bufIV[4];
1.1.1.5   root      680:        unsigned __int64 i;
                    681:        int blockSize = CipherGetBlockSize (ea != 0 ? EAGetFirstCipher (ea) : cipher);
                    682: 
                    683:        //  IV
                    684:        bufIV[0] = iv[0];
                    685:        bufIV[1] = iv[1];
                    686:        if (blockSize == 16)
                    687:        {
                    688:                bufIV[2] = iv[2];
                    689:                bufIV[3] = iv[3];
                    690:        }
                    691: 
                    692:        // Encrypt each block
                    693:        for (i = 0; i < len/blockSize; i++)
                    694:        {
                    695:                // CBC
                    696:                data[0] ^= bufIV[0];
                    697:                data[1] ^= bufIV[1];
                    698:                if (blockSize == 16)
                    699:                {
                    700:                        data[2] ^= bufIV[2];
                    701:                        data[3] ^= bufIV[3];
                    702:                }
                    703: 
                    704:                if (ea != 0)
                    705:                {
                    706:                        // Outer-CBC
                    707:                        for (cipher = EAGetFirstCipher (ea); cipher != 0; cipher = EAGetNextCipher (ea, cipher))
                    708:                        {
                    709:                                EncipherBlock (cipher, data, ks);
                    710:                                ks += CipherGetKeyScheduleSize (cipher);
                    711:                        }
                    712:                        ks -= EAGetKeyScheduleSize (ea);
                    713:                }
                    714:                else
                    715:                {
                    716:                        // CBC/inner-CBC
                    717:                        EncipherBlock (cipher, data, ks);
                    718:                }
                    719: 
                    720:                // CBC
                    721:                bufIV[0] = data[0];
                    722:                bufIV[1] = data[1];
                    723:                if (blockSize == 16)
                    724:                {
                    725:                        bufIV[2] = data[2];
                    726:                        bufIV[3] = data[3];
                    727:                }
                    728: 
                    729:                // Whitening
                    730:                data[0] ^= whitening[0];
                    731:                data[1] ^= whitening[1];
                    732:                if (blockSize == 16)
                    733:                {
                    734:                        data[2] ^= whitening[0];
                    735:                        data[3] ^= whitening[1];
                    736:                }
                    737: 
1.1.1.7   root      738:                data += blockSize / sizeof(*data);
1.1.1.5   root      739:        }
1.1       root      740: }
                    741: 
1.1.1.5   root      742: 
1.1.1.8   root      743: // DecryptBufferCBC  (deprecated/legacy)
1.1.1.5   root      744: //
                    745: // data:               data to be decrypted
                    746: // len:                        number of bytes to decrypt (must be divisible by the largest cipher block size)
                    747: // ks:                 scheduled key
                    748: // iv:                 IV
                    749: // whitening:  whitening constants
                    750: // ea:                 outer-CBC cascade ID (0 = CBC/inner-CBC)
                    751: // cipher:             CBC/inner-CBC cipher ID (0 = outer-CBC)
                    752: 
                    753: static void
1.1.1.7   root      754: DecryptBufferCBC (unsigned __int32 *data,
                    755:                 unsigned int len,
1.1.1.8   root      756:                 unsigned __int8 *ks,
1.1.1.7   root      757:                 unsigned __int32 *iv,
                    758:                 unsigned __int32 *whitening,
1.1.1.5   root      759:                 int ea,
                    760:                 int cipher)
1.1       root      761: {
1.1.1.8   root      762: 
                    763:        /* IMPORTANT: This function has been deprecated (legacy) */
                    764: 
1.1.1.7   root      765:        unsigned __int32 bufIV[4];
1.1.1.5   root      766:        unsigned __int64 i;
1.1.1.7   root      767:        unsigned __int32 ct[4];
1.1.1.5   root      768:        int blockSize = CipherGetBlockSize (ea != 0 ? EAGetFirstCipher (ea) : cipher);
                    769: 
                    770:        //  IV
                    771:        bufIV[0] = iv[0];
                    772:        bufIV[1] = iv[1];
                    773:        if (blockSize == 16)
1.1       root      774:        {
1.1.1.5   root      775:                bufIV[2] = iv[2];
                    776:                bufIV[3] = iv[3];
                    777:        }
                    778: 
                    779:        // Decrypt each block
                    780:        for (i = 0; i < len/blockSize; i++)
                    781:        {
                    782:                // Dewhitening
                    783:                data[0] ^= whitening[0];
                    784:                data[1] ^= whitening[1];
                    785:                if (blockSize == 16)
                    786:                {
                    787:                        data[2] ^= whitening[0];
                    788:                        data[3] ^= whitening[1];
                    789:                }
                    790: 
                    791:                // CBC
                    792:                ct[0] = data[0];
                    793:                ct[1] = data[1];
                    794:                if (blockSize == 16)
                    795:                {
                    796:                        ct[2] = data[2];
                    797:                        ct[3] = data[3];
                    798:                }
                    799: 
                    800:                if (ea != 0)
                    801:                {
                    802:                        // Outer-CBC
                    803:                        ks += EAGetKeyScheduleSize (ea);
                    804:                        for (cipher = EAGetLastCipher (ea); cipher != 0; cipher = EAGetPreviousCipher (ea, cipher))
                    805:                        {
                    806:                                ks -= CipherGetKeyScheduleSize (cipher);
                    807:                                DecipherBlock (cipher, data, ks);
                    808:                        }
                    809:                }
                    810:                else
                    811:                {
                    812:                        // CBC/inner-CBC
                    813:                        DecipherBlock (cipher, data, ks);
                    814:                }
                    815: 
                    816:                // CBC
                    817:                data[0] ^= bufIV[0];
                    818:                data[1] ^= bufIV[1];
                    819:                bufIV[0] = ct[0];
                    820:                bufIV[1] = ct[1];
                    821:                if (blockSize == 16)
                    822:                {
                    823:                        data[2] ^= bufIV[2];
                    824:                        data[3] ^= bufIV[3];
                    825:                        bufIV[2] = ct[2];
                    826:                        bufIV[3] = ct[3];
                    827:                }
                    828: 
1.1.1.7   root      829:                data += blockSize / sizeof(*data);
1.1       root      830:        }
                    831: }
1.1.1.5   root      832: 
                    833: 
1.1.1.8   root      834: void Xor128 (unsigned __int64 *a, unsigned __int64 *b)
                    835: {
                    836:        *a++ ^= *b++;
                    837:        *a ^= *b;
                    838: }
                    839: 
                    840: 
                    841: void Xor64 (unsigned __int64 *a, unsigned __int64 *b)
                    842: {
                    843:        *a ^= *b;
                    844: }
                    845: 
                    846: 
                    847: void EncryptBufferLRW128 (unsigned __int8 *plainText, unsigned int length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo)
                    848: {
                    849:        int cipher = EAGetFirstCipher (cryptoInfo->ea);
                    850:        int cipherCount = EAGetCipherCount (cryptoInfo->ea);
                    851:        unsigned __int8 *p = plainText;
                    852:        unsigned __int8 *ks = cryptoInfo->ks;
                    853:        unsigned __int8 i[8];
                    854:        unsigned __int8 t[16];
                    855:        unsigned int b;
                    856: 
                    857:        *(unsigned __int64 *)i = BE64(blockIndex);
                    858: 
                    859:        // Note that the maximum supported volume size is 8589934592 GB  (i.e., 2^63 bytes).
                    860: 
                    861:        for (b = 0; b < length >> 4; b++)
                    862:        {
                    863:                Gf128MulBy64Tab (i, t, &cryptoInfo->gf_ctx);
                    864:                Xor128 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    865: 
                    866:                if (cipherCount > 1)
                    867:                {
                    868:                        // Cipher cascade
                    869:                        for (cipher = EAGetFirstCipher (cryptoInfo->ea);
                    870:                                cipher != 0;
                    871:                                cipher = EAGetNextCipher (cryptoInfo->ea, cipher))
                    872:                        {
                    873:                                EncipherBlock (cipher, p, ks);
                    874:                                ks += CipherGetKeyScheduleSize (cipher);
                    875:                        }
                    876:                        ks = cryptoInfo->ks;
                    877:                }
                    878:                else
                    879:                {
                    880:                        EncipherBlock (cipher, p, ks);
                    881:                }
                    882: 
                    883:                Xor128 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    884: 
                    885:                p += 16;
                    886: 
                    887:                if (i[7] != 0xff)
                    888:                        i[7]++;
                    889:                else
                    890:                        *(unsigned __int64 *)i = BE64 ( BE64(*(unsigned __int64 *)i) + 1 );
                    891:        }
                    892: 
                    893:        memset (t, 0, sizeof (t));
                    894: }
                    895: 
                    896: 
                    897: void EncryptBufferLRW64 (unsigned __int8 *plainText, unsigned int length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo)
                    898: {
                    899:        int cipher = EAGetFirstCipher (cryptoInfo->ea);
                    900:        unsigned __int8 *p = plainText;
                    901:        unsigned __int8 *ks = cryptoInfo->ks;
                    902:        unsigned __int8 i[8];
                    903:        unsigned __int8 t[8];
                    904:        unsigned int b;
                    905: 
                    906:        *(unsigned __int64 *)i = BE64(blockIndex);
                    907: 
                    908:        for (b = 0; b < length >> 3; b++)
                    909:        {
                    910:                Gf64MulTab (i, t, &cryptoInfo->gf_ctx);
                    911:                Xor64 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    912: 
                    913:                EncipherBlock (cipher, p, ks);
                    914: 
                    915:                Xor64 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    916: 
                    917:                p += 8;
                    918: 
                    919:                if (i[7] != 0xff)
                    920:                        i[7]++;
                    921:                else
                    922:                        *(unsigned __int64 *)i = BE64 ( BE64(*(unsigned __int64 *)i) + 1 );
                    923:        }
                    924: 
                    925:        memset (t, 0, sizeof (t));
                    926: }
                    927: 
                    928: 
                    929: void DecryptBufferLRW128 (unsigned __int8 *plainText, int length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo)
                    930: {
                    931:        int cipher = EAGetFirstCipher (cryptoInfo->ea);
                    932:        int cipherCount = EAGetCipherCount (cryptoInfo->ea);
                    933:        unsigned __int8 *p = plainText;
                    934:        unsigned __int8 *ks = cryptoInfo->ks;
                    935:        unsigned __int8 i[8];
                    936:        unsigned __int8 t[16];
                    937:        int b;
                    938: 
                    939:        *(unsigned __int64 *)i = BE64(blockIndex);
                    940: 
                    941:        // Note that the maximum supported volume size is 8589934592 GB  (i.e., 2^63 bytes).
                    942: 
                    943:        for (b = 0; b < length >> 4; b++)
                    944:        {
                    945:                Gf128MulBy64Tab (i, t, &cryptoInfo->gf_ctx);
                    946:                Xor128 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    947: 
                    948:                if (cipherCount > 1)
                    949:                {
                    950:                        // Cipher cascade
                    951:                        ks = cryptoInfo->ks + EAGetKeyScheduleSize (cryptoInfo->ea);
                    952: 
                    953:                        for (cipher = EAGetLastCipher (cryptoInfo->ea);
                    954:                                cipher != 0;
                    955:                                cipher = EAGetPreviousCipher (cryptoInfo->ea, cipher))
                    956:                        {
                    957:                                ks -= CipherGetKeyScheduleSize (cipher);
                    958:                                DecipherBlock (cipher, p, ks);
                    959:                        }
                    960:                }
                    961:                else
                    962:                {
                    963:                        DecipherBlock (cipher, p, ks);
                    964:                }
                    965: 
                    966:                Xor128 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    967: 
                    968:                p += 16;
                    969: 
                    970:                if (i[7] != 0xff)
                    971:                        i[7]++;
                    972:                else
                    973:                        *(unsigned __int64 *)i = BE64 ( BE64(*(unsigned __int64 *)i) + 1 );
                    974:        }
                    975: 
                    976:        memset (t, 0, sizeof (t));
                    977: }
                    978: 
                    979: 
                    980: 
                    981: void DecryptBufferLRW64 (unsigned __int8 *plainText, int length, unsigned __int64 blockIndex, PCRYPTO_INFO cryptoInfo)
                    982: {
                    983:        int cipher = EAGetFirstCipher (cryptoInfo->ea);
                    984:        unsigned __int8 *p = plainText;
                    985:        unsigned __int8 *ks = cryptoInfo->ks;
                    986:        unsigned __int8 i[8];
                    987:        unsigned __int8 t[8];
                    988:        int b;
                    989: 
                    990:        *(unsigned __int64 *)i = BE64(blockIndex);
                    991: 
                    992:        for (b = 0; b < length >> 3; b++)
                    993:        {
                    994:                Gf64MulTab (i, t, &cryptoInfo->gf_ctx);
                    995:                Xor64 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                    996: 
                    997:                DecipherBlock (cipher, p, ks);
                    998: 
                    999:                Xor64 ((unsigned __int64 *)p, (unsigned __int64 *)t);
                   1000: 
                   1001:                p += 8;
                   1002: 
                   1003:                if (i[7] != 0xff)
                   1004:                        i[7]++;
                   1005:                else
                   1006:                        *(unsigned __int64 *)i = BE64 ( BE64(*(unsigned __int64 *)i) + 1 );
                   1007:        }
                   1008: 
                   1009:        memset (t, 0, sizeof (t));
                   1010: }
                   1011: 
                   1012: 
1.1.1.5   root     1013: // EncryptBuffer
                   1014: //
                   1015: // buf:                        data to be encrypted
                   1016: // len:                        number of bytes to encrypt; must be divisible by the block size (for cascaded
                   1017: //              ciphers divisible by the largest block size used within the cascade)
                   1018: 
                   1019: void 
1.1.1.7   root     1020: EncryptBuffer (unsigned __int32 *buf,
1.1.1.5   root     1021:                           unsigned __int64 len,
1.1.1.8   root     1022:                           PCRYPTO_INFO cryptoInfo)
1.1.1.5   root     1023: {
                   1024: 
1.1.1.8   root     1025:        switch (cryptoInfo->mode)
1.1.1.5   root     1026:        {
1.1.1.8   root     1027:        case LRW:
                   1028:                switch (CipherGetBlockSize (EAGetFirstCipher (cryptoInfo->ea)))
                   1029:                {
                   1030:                case 8:
                   1031:                        EncryptBufferLRW64 ((unsigned __int8 *)buf, (unsigned int) len, 1, cryptoInfo);
                   1032:                        break;
                   1033: 
                   1034:                case 16:
                   1035:                        EncryptBufferLRW128 ((unsigned __int8 *)buf, (unsigned int) len, 1, cryptoInfo);
                   1036:                        break;
                   1037:                }
                   1038:                break;
                   1039: 
1.1.1.5   root     1040:        case CBC:
                   1041:        case INNER_CBC:
                   1042:                {
1.1.1.8   root     1043:                        /* Deprecated/legacy */
1.1.1.5   root     1044: 
1.1.1.8   root     1045:                        unsigned __int8 *ks = cryptoInfo->ks;
                   1046:                        int cipher;
                   1047:                        for (cipher = EAGetFirstCipher (cryptoInfo->ea);
                   1048:                                cipher != 0;
                   1049:                                cipher = EAGetNextCipher (cryptoInfo->ea, cipher))
                   1050:                        {
                   1051:                                EncryptBufferCBC (buf,
                   1052:                                        (unsigned int) len,
                   1053:                                        ks,
                   1054:                                        (unsigned __int32 *) cryptoInfo->iv,
                   1055:                                        (unsigned __int32 *) &cryptoInfo->iv[8],
                   1056:                                        0,
                   1057:                                        cipher);
1.1.1.5   root     1058: 
1.1.1.8   root     1059:                                ks += CipherGetKeyScheduleSize (cipher);
                   1060:                        }
                   1061:                }
1.1.1.5   root     1062:                break;
                   1063: 
                   1064:        case OUTER_CBC:
                   1065: 
1.1.1.8   root     1066:                /* Deprecated/legacy */
                   1067: 
1.1.1.5   root     1068:                EncryptBufferCBC (buf,
1.1.1.7   root     1069:                        (unsigned int) len,
1.1.1.8   root     1070:                        cryptoInfo->ks,
                   1071:                        (unsigned __int32 *) cryptoInfo->iv,
                   1072:                        (unsigned __int32 *) &cryptoInfo->iv[8],
                   1073:                        cryptoInfo->ea,
1.1.1.5   root     1074:                        0);
                   1075: 
                   1076:                break;
                   1077:        }
                   1078: }
                   1079: 
1.1.1.8   root     1080: // Convert sector number to the index of the first LRW block in the sector.
                   1081: // Note that the maximum supported volume size is 8589934592 GB  (i.e., 2^63 bytes).
                   1082: unsigned __int64 LRWSector2Index (unsigned __int64 sector, int blockSize, PCRYPTO_INFO ci)
                   1083: {
                   1084:        if (ci->hiddenVolume)
                   1085:                sector -= ci->hiddenVolumeOffset / SECTOR_SIZE;
                   1086:        else
                   1087:                sector -= HEADER_SIZE / SECTOR_SIZE;    // Compensate for the volume header size
                   1088: 
                   1089:        switch (blockSize)
                   1090:        {
                   1091:        case 8:
                   1092:                return (sector << 6) | 1;
                   1093: 
                   1094:        case 16:
                   1095:                return (sector << 5) | 1;
                   1096:        }
                   1097: 
                   1098:        return 0;
                   1099: }
                   1100: 
                   1101: 
1.1.1.5   root     1102: // EncryptSectors
                   1103: //
                   1104: // buf:                        data to be encrypted
                   1105: // secNo:              sector number relative to volume start
                   1106: // noSectors:  number of sectors in buffer
                   1107: // ks:                 scheduled key
                   1108: // iv:                 IV
                   1109: // ea:                 encryption algorithm
                   1110: 
                   1111: void _cdecl 
1.1.1.7   root     1112: EncryptSectors (unsigned __int32 *buf,
1.1.1.5   root     1113:                unsigned __int64 secNo,
                   1114:                unsigned __int64 noSectors,
1.1.1.8   root     1115:                PCRYPTO_INFO ci)
                   1116: {
                   1117:        int ea = ci->ea;
                   1118:        void *iv = ci->iv;                                      // Deprecated/legacy
                   1119:        unsigned __int8 *ks = ci->ks;
                   1120:        unsigned __int64 *iv64 = (unsigned __int64 *) iv;       // Deprecated/legacy
                   1121:        unsigned __int32 sectorIV[4];           // Deprecated/legacy
                   1122:        unsigned __int32 secWhitening[2];       // Deprecated/legacy
1.1.1.5   root     1123:        int cipher;
                   1124: 
1.1.1.8   root     1125:        switch (ci->mode)
1.1.1.5   root     1126:        {
1.1.1.8   root     1127:        case LRW:
                   1128:                {
                   1129:                        switch (CipherGetBlockSize (EAGetFirstCipher (ea)))
                   1130:                        {
                   1131:                        case 8:
                   1132:                                EncryptBufferLRW64 ((unsigned __int8 *)buf,
                   1133:                                        (unsigned int) noSectors * SECTOR_SIZE,
                   1134:                                        LRWSector2Index (secNo, 8, ci),
                   1135:                                        ci);
                   1136:                                break;
                   1137: 
                   1138:                        case 16:
                   1139:                                EncryptBufferLRW128 ((unsigned __int8 *)buf,
                   1140:                                        (unsigned int) noSectors * SECTOR_SIZE,
                   1141:                                        LRWSector2Index (secNo, 16, ci),
                   1142:                                        ci);
                   1143:                                break;
                   1144:                        }
                   1145:                }
                   1146:                break;
                   1147: 
1.1.1.5   root     1148:        case CBC:
                   1149:        case INNER_CBC:
                   1150: 
1.1.1.8   root     1151:                /* Deprecated/legacy */
                   1152: 
1.1.1.5   root     1153:                while (noSectors--)
                   1154:                {
                   1155:                        for (cipher = EAGetFirstCipher (ea); cipher != 0; cipher = EAGetNextCipher (ea, cipher))
                   1156:                        {
                   1157:                                InitSectorIVAndWhitening (secNo, CipherGetBlockSize (cipher), sectorIV, iv64, secWhitening);
                   1158: 
                   1159:                                EncryptBufferCBC (buf,
                   1160:                                        SECTOR_SIZE,
                   1161:                                        ks,
                   1162:                                        sectorIV,
                   1163:                                        secWhitening,
                   1164:                                        0,
                   1165:                                        cipher);
                   1166: 
                   1167:                                ks += CipherGetKeyScheduleSize (cipher);
                   1168:                        }
                   1169:                        ks -= EAGetKeyScheduleSize (ea);
1.1.1.7   root     1170:                        buf += SECTOR_SIZE / sizeof(*buf);
1.1.1.5   root     1171:                        secNo++;
                   1172:                }
                   1173:                break;
                   1174: 
                   1175:        case OUTER_CBC:
                   1176: 
1.1.1.8   root     1177:                /* Deprecated/legacy */
                   1178: 
1.1.1.5   root     1179:                while (noSectors--)
                   1180:                {
                   1181:                        InitSectorIVAndWhitening (secNo, CipherGetBlockSize (EAGetFirstCipher (ea)), sectorIV, iv64, secWhitening);
                   1182: 
                   1183:                        EncryptBufferCBC (buf,
                   1184:                                SECTOR_SIZE,
                   1185:                                ks,
                   1186:                                sectorIV,
                   1187:                                secWhitening,
                   1188:                                ea,
                   1189:                                0);
                   1190: 
1.1.1.7   root     1191:                        buf += SECTOR_SIZE / sizeof(*buf);
1.1.1.5   root     1192:                        secNo++;
                   1193:                }
                   1194:                break;
                   1195:        }
                   1196: }
                   1197: 
                   1198: // DecryptBuffer
                   1199: //
                   1200: // buf:                        data to be decrypted
                   1201: // len:                        number of bytes to decrypt; must be divisible by the block size (for cascaded
                   1202: //              ciphers divisible by the largest block size used within the cascade)
                   1203: void 
1.1.1.7   root     1204: DecryptBuffer (unsigned __int32 *buf,
1.1.1.5   root     1205:                unsigned __int64 len,
1.1.1.8   root     1206:                PCRYPTO_INFO cryptoInfo)
1.1.1.5   root     1207: {
1.1.1.8   root     1208:        switch (cryptoInfo->mode)
1.1.1.5   root     1209:        {
1.1.1.8   root     1210:        case LRW:
                   1211:                switch (CipherGetBlockSize (EAGetFirstCipher (cryptoInfo->ea)))
                   1212:                {
                   1213:                case 8:
                   1214:                        DecryptBufferLRW64 ((unsigned __int8 *)buf, (unsigned int) len, 1, cryptoInfo);
                   1215:                        break;
                   1216: 
                   1217:                case 16:
                   1218:                        DecryptBufferLRW128 ((unsigned __int8 *)buf, (unsigned int) len, 1, cryptoInfo);
                   1219:                        break;
                   1220:                }
                   1221:                break;
                   1222: 
1.1.1.5   root     1223:        case CBC:
                   1224:        case INNER_CBC:
                   1225:                {
                   1226: 
1.1.1.8   root     1227:                        /* Deprecated/legacy */
                   1228: 
                   1229:                        unsigned __int8 *ks = cryptoInfo->ks + EAGetKeyScheduleSize (cryptoInfo->ea);
                   1230:                        int cipher;
                   1231:                        for (cipher = EAGetLastCipher (cryptoInfo->ea);
                   1232:                                cipher != 0;
                   1233:                                cipher = EAGetPreviousCipher (cryptoInfo->ea, cipher))
                   1234:                        {
                   1235:                                ks -= CipherGetKeyScheduleSize (cipher);
                   1236: 
                   1237:                                DecryptBufferCBC (buf,
                   1238:                                        (unsigned int) len,
                   1239:                                        ks,
                   1240:                                        (unsigned __int32 *) cryptoInfo->iv,
                   1241:                                        (unsigned __int32 *) &cryptoInfo->iv[8],
                   1242:                                        0,
                   1243:                                        cipher);
                   1244:                        }
1.1.1.5   root     1245:                }
                   1246:                break;
                   1247: 
                   1248:        case OUTER_CBC:
                   1249: 
1.1.1.8   root     1250:                /* Deprecated/legacy */
                   1251: 
1.1.1.5   root     1252:                DecryptBufferCBC (buf,
1.1.1.7   root     1253:                        (unsigned int) len,
1.1.1.8   root     1254:                        cryptoInfo->ks,
                   1255:                        (unsigned __int32 *) cryptoInfo->iv,
                   1256:                        (unsigned __int32 *) &cryptoInfo->iv[8],
                   1257:                        cryptoInfo->ea,
1.1.1.5   root     1258:                        0);
                   1259: 
                   1260:                break;
                   1261:        }
                   1262: }
                   1263: 
                   1264: // DecryptSectors
                   1265: //
                   1266: // buf:                        data to be decrypted
                   1267: // secNo:              sector number relative to volume start
                   1268: // noSectors:  number of sectors in buffer
                   1269: // ks:                 scheduled key
                   1270: // iv:                 IV
                   1271: // ea:                 encryption algorithm
                   1272: 
                   1273: void _cdecl 
1.1.1.7   root     1274: DecryptSectors (unsigned __int32 *buf,
1.1.1.5   root     1275:                unsigned __int64 secNo,
                   1276:                unsigned __int64 noSectors,
1.1.1.8   root     1277:                PCRYPTO_INFO ci
                   1278: )
                   1279: {
                   1280:        int ea = ci->ea;
                   1281:        void *iv = ci->iv;                                              // Deprecated/legacy
                   1282:        unsigned __int8 *ks = ci->ks;
                   1283:        unsigned __int64 *iv64 = (unsigned __int64 *) iv;       // Deprecated/legacy
                   1284:        unsigned __int32 sectorIV[4];                   // Deprecated/legacy
                   1285:        unsigned __int32 secWhitening[2];               // Deprecated/legacy
1.1.1.5   root     1286:        int cipher;
                   1287: 
1.1.1.8   root     1288:        switch (ci->mode)
1.1.1.5   root     1289:        {
1.1.1.8   root     1290:        case LRW:
                   1291:                {
                   1292:                        switch (CipherGetBlockSize (EAGetFirstCipher (ea)))
                   1293:                        {
                   1294:                        case 8:
                   1295:                                DecryptBufferLRW64 ((unsigned __int8 *)buf,
                   1296:                                        (unsigned int) noSectors * SECTOR_SIZE,
                   1297:                                        LRWSector2Index (secNo, 8, ci),
                   1298:                                        ci);
                   1299:                                break;
                   1300: 
                   1301:                        case 16:
                   1302:                                DecryptBufferLRW128 ((unsigned __int8 *)buf,
                   1303:                                        (unsigned int) noSectors * SECTOR_SIZE,
                   1304:                                        LRWSector2Index (secNo, 16, ci),
                   1305:                                        ci);
                   1306:                                break;
                   1307:                        }
                   1308:                }
                   1309:                break;
                   1310: 
1.1.1.5   root     1311:        case CBC:
                   1312:        case INNER_CBC:
                   1313: 
1.1.1.8   root     1314:                /* Deprecated/legacy */
                   1315: 
1.1.1.5   root     1316:                while (noSectors--)
                   1317:                {
                   1318:                        ks += EAGetKeyScheduleSize (ea);
                   1319:                        for (cipher = EAGetLastCipher (ea); cipher != 0; cipher = EAGetPreviousCipher (ea, cipher))
                   1320:                        {
                   1321:                                InitSectorIVAndWhitening (secNo, CipherGetBlockSize (cipher), sectorIV, iv64, secWhitening);
                   1322: 
                   1323:                                ks -= CipherGetKeyScheduleSize (cipher);
                   1324: 
                   1325:                                DecryptBufferCBC (buf,
                   1326:                                        SECTOR_SIZE,
                   1327:                                        ks,
                   1328:                                        sectorIV,
                   1329:                                        secWhitening,
                   1330:                                        0,
                   1331:                                        cipher);
                   1332:                        }
1.1.1.7   root     1333:                        buf += SECTOR_SIZE / sizeof(*buf);
1.1.1.5   root     1334:                        secNo++;
                   1335:                }
                   1336:                break;
                   1337: 
                   1338:        case OUTER_CBC:
                   1339: 
1.1.1.8   root     1340:                /* Deprecated/legacy */
                   1341: 
1.1.1.5   root     1342:                while (noSectors--)
                   1343:                {
                   1344:                        InitSectorIVAndWhitening (secNo, CipherGetBlockSize (EAGetFirstCipher (ea)), sectorIV, iv64, secWhitening);
                   1345: 
                   1346:                        DecryptBufferCBC (buf,
                   1347:                                SECTOR_SIZE,
                   1348:                                ks,
                   1349:                                sectorIV,
                   1350:                                secWhitening,
                   1351:                                ea,
                   1352:                                0);
                   1353: 
1.1.1.7   root     1354:                        buf += SECTOR_SIZE / sizeof(*buf);
1.1.1.5   root     1355:                        secNo++;
                   1356:                }
                   1357:                break;
                   1358:        }
                   1359: }
                   1360: 

unix.superglobalmegacorp.com

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