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

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

unix.superglobalmegacorp.com

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