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

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

unix.superglobalmegacorp.com

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