Annotation of truecrypt/common/volumes.c, revision 1.1.1.2

1.1.1.2 ! root        1: /* Copyright (C) 2004 TrueCrypt Foundation
1.1       root        2:    This product uses components written by Paul Le Roux <[email protected]> */
                      3: 
                      4: #include "TCdefs.h"
                      5: 
                      6: #include <fcntl.h>
                      7: #include <sys/types.h>
                      8: #include <sys/stat.h>
                      9: #include <io.h>
                     10: #include <time.h>
                     11: 
                     12: #include "crypto.h"
                     13: #include "random.h"
                     14: #include "endian.h"
                     15: #include "fat.h"
                     16: #include "volumes.h"
                     17: 
                     18: #include "pkcs5.h"
                     19: #include "crc.h"
                     20: 
                     21: void _cdecl
                     22: EncryptSector8 (unsigned long *data,
                     23:                unsigned __int64 secNo,
                     24:                unsigned long noSectors,
                     25:                unsigned char *ks,
                     26:                unsigned char *iv,
                     27:                int cipher)
                     28: {
1.1.1.2 ! root       29:        unsigned __int64 *iv64 = (unsigned __int64 *) iv;
        !            30:        unsigned __int64 IV64[3];
        !            31:        unsigned long *IV = (unsigned long *) IV64;
1.1       root       32:        unsigned long sectorIV[2];
                     33:        unsigned long x1, x2;
                     34:        unsigned long j;
                     35: 
                     36:        while (noSectors--)
                     37:        {
                     38:                // Sector encryption is implemented by making IV unique for each
                     39:                // sector and obfuscating cipher text
1.1.1.2 ! root       40:                IV64[0] = iv64[0] ^ secNo;
        !            41:                IV64[1] = iv64[1] ^ secNo;
        !            42:                IV64[2] = iv64[2] ^ secNo;
1.1       root       43: 
                     44:                sectorIV[0] = IV[0];
                     45:                sectorIV[1] = IV[1];
                     46: 
                     47:                x1 = crc32long ( &IV[2] ) ^ crc32long ( &IV[5] );
                     48:                x2 = crc32long ( &IV[3] ) ^ crc32long ( &IV[4] );
                     49: 
                     50:                // CBC encrypt the entire sector
                     51:                for (j = 0; j < 64; j++)
                     52:                {
                     53:                        // CBC
                     54:                        data[0] ^= sectorIV[0];
                     55:                        data[1] ^= sectorIV[1];
                     56: 
                     57:                        encipher_block (cipher, data, ks);
                     58: 
                     59:                        sectorIV[0] = data[0];
                     60:                        sectorIV[1] = data[1];
                     61: 
                     62:                        // Cipher text XOR
                     63:                        data[0] ^= x1;
                     64:                        data[1] ^= x2;
                     65: 
                     66:                        data += 2;
                     67:                }
                     68: 
                     69:                secNo++;
                     70:        }
                     71: }
                     72: 
                     73: void
                     74:   _cdecl
                     75: DecryptSector8 (unsigned long *data,
                     76:                unsigned __int64 secNo,
                     77:                unsigned long noSectors,
                     78:                unsigned char *ks,
                     79:                unsigned char *iv,
                     80:                int cipher)
                     81: {
1.1.1.2 ! root       82:        unsigned __int64 *iv64 = (unsigned __int64 *) iv;
        !            83:        unsigned __int64 IV64[3];
        !            84:        unsigned long *IV = (unsigned long *) IV64;
1.1       root       85:        unsigned long sectorIV[2];
                     86:        unsigned long x1, x2;
                     87:        int j;
                     88: 
                     89:        while (noSectors--)
                     90:        {
1.1.1.2 ! root       91:                IV64[0] = iv64[0] ^ secNo;
        !            92:                IV64[1] = iv64[1] ^ secNo;
        !            93:                IV64[2] = iv64[2] ^ secNo;
1.1       root       94: 
                     95:                sectorIV[0] = IV[0];
                     96:                sectorIV[1] = IV[1];
                     97: 
                     98:                x1 = crc32long ( &IV[2] ) ^ crc32long ( &IV[5] );
                     99:                x2 = crc32long ( &IV[3] ) ^ crc32long ( &IV[4] );
                    100: 
                    101:                // CBC decrypt the sector
                    102:                for (j = 0; j < 64; j++)
                    103:                {
                    104:                        unsigned long a, b;
                    105: 
                    106:                        // Cipher text XOR
                    107:                        data[0] ^= x1;
                    108:                        data[1] ^= x2;
                    109: 
                    110:                        // CBC
                    111:                        a = data[0];
                    112:                        b = data[1];
                    113: 
                    114:                        decipher_block (cipher, data, ks);
                    115: 
                    116:                        data[0] ^= sectorIV[0];
                    117:                        data[1] ^= sectorIV[1];
                    118: 
                    119:                        sectorIV[0] = a;
                    120:                        sectorIV[1] = b;
                    121: 
                    122:                        data += 2;
                    123:                }
                    124: 
                    125:                secNo++;
                    126:        }
                    127: }
                    128: 
1.1.1.2 ! root      129: void _cdecl
        !           130: EncryptSector16 (unsigned long *data,
        !           131:                unsigned __int64 secNo,
        !           132:                unsigned long noSectors,
        !           133:                unsigned char *ks,
        !           134:                unsigned char *iv,
        !           135:                int cipher)
        !           136: {
        !           137:        unsigned __int64 *iv64 = (unsigned __int64 *) iv;
        !           138:        unsigned __int64 IV64[4];
        !           139:        unsigned long *IV = (unsigned long *) IV64;
        !           140:        unsigned long sectorIV[4];
        !           141:        unsigned long x1, x2;
        !           142:        unsigned long j;
        !           143: 
        !           144:        while (noSectors--)
        !           145:        {
        !           146:                IV64[0] = iv64[0] ^ secNo;
        !           147:                IV64[1] = iv64[1] ^ secNo;
        !           148:                IV64[2] = iv64[2] ^ secNo;
        !           149:                IV64[3] = iv64[3] ^ secNo;
        !           150: 
        !           151:                sectorIV[0] = IV[0];
        !           152:                sectorIV[1] = IV[1];
        !           153:                sectorIV[2] = IV[2];
        !           154:                sectorIV[3] = IV[3];
        !           155: 
        !           156:                x1 = crc32long ( &IV[4] ) ^ crc32long ( &IV[7] );
        !           157:                x2 = crc32long ( &IV[5] ) ^ crc32long ( &IV[6] );
        !           158: 
        !           159:                // CBC encrypt the entire sector
        !           160:                for (j = 0; j < 32; j++)
        !           161:                {
        !           162:                        // CBC
        !           163:                        data[0] ^= sectorIV[0];
        !           164:                        data[1] ^= sectorIV[1];
        !           165:                        data[2] ^= sectorIV[2];
        !           166:                        data[3] ^= sectorIV[3];
        !           167: 
        !           168:                        encipher_block (cipher, data, ks);
        !           169: 
        !           170:                        sectorIV[0] = data[0];
        !           171:                        sectorIV[1] = data[1];
        !           172:                        sectorIV[2] = data[2];
        !           173:                        sectorIV[3] = data[3];
        !           174: 
        !           175:                        // Cipher text XOR
        !           176:                        data[0] ^= x1;
        !           177:                        data[1] ^= x2;
        !           178:                        data[2] ^= x1;
        !           179:                        data[3] ^= x2;
        !           180: 
        !           181:                        data += 4;
        !           182:                }
        !           183: 
        !           184:                secNo++;
        !           185:        }
        !           186: }
        !           187: 
        !           188: 
        !           189: void
        !           190:   _cdecl
        !           191: DecryptSector16 (unsigned long *data,
        !           192:                unsigned __int64 secNo,
        !           193:                unsigned long noSectors,
        !           194:                unsigned char *ks,
        !           195:                unsigned char *iv,
        !           196:                int cipher)
        !           197: {
        !           198:        unsigned __int64 *iv64 = (unsigned __int64 *) iv;
        !           199:        unsigned __int64 IV64[4];
        !           200:        unsigned long *IV = (unsigned long *) IV64;
        !           201:        unsigned long sectorIV[4];
        !           202:        unsigned long x1, x2;
        !           203:        int j;
        !           204: 
        !           205:        while (noSectors--)
        !           206:        {
        !           207:                IV64[0] = iv64[0] ^ secNo;
        !           208:                IV64[1] = iv64[1] ^ secNo;
        !           209:                IV64[2] = iv64[2] ^ secNo;
        !           210:                IV64[3] = iv64[3] ^ secNo;
        !           211: 
        !           212:                sectorIV[0] = IV[0];
        !           213:                sectorIV[1] = IV[1];
        !           214:                sectorIV[2] = IV[2];
        !           215:                sectorIV[3] = IV[3];
        !           216: 
        !           217:                x1 = crc32long ( &IV[4] ) ^ crc32long ( &IV[7] );
        !           218:                x2 = crc32long ( &IV[5] ) ^ crc32long ( &IV[6] );
        !           219: 
        !           220:                // CBC decrypt the sector
        !           221:                for (j = 0; j < 32; j++)
        !           222:                {
        !           223:                        unsigned long a[4];
        !           224: 
        !           225:                        // Cipher text XOR
        !           226:                        data[0] ^= x1;
        !           227:                        data[1] ^= x2;
        !           228:                        data[2] ^= x1;
        !           229:                        data[3] ^= x2;
        !           230: 
        !           231:                        // CBC
        !           232:                        a[0] = data[0];
        !           233:                        a[1] = data[1];
        !           234:                        a[2] = data[2];
        !           235:                        a[3] = data[3];
        !           236: 
        !           237:                        decipher_block (cipher, data, ks);
        !           238: 
        !           239:                        data[0] ^= sectorIV[0];
        !           240:                        data[1] ^= sectorIV[1];
        !           241:                        data[2] ^= sectorIV[2];
        !           242:                        data[3] ^= sectorIV[3];
        !           243: 
        !           244:                        sectorIV[0] = a[0];
        !           245:                        sectorIV[1] = a[1];
        !           246:                        sectorIV[2] = a[2];
        !           247:                        sectorIV[3] = a[3];
        !           248: 
        !           249:                        data += 4;
        !           250:                }
        !           251: 
        !           252:                secNo++;
        !           253:        }
        !           254: }
        !           255: 
1.1       root      256: // EncryptBuffer
                    257: //
                    258: // Encrypts data in buffer
                    259: // Returns number of bytes encrypted
                    260: //
                    261: // len = length of data in bytes
                    262: int _cdecl 
                    263: EncryptBuffer (unsigned char *buf,
                    264:                unsigned long len,
                    265:                unsigned char *ks,
                    266:                unsigned char *iv,
                    267:                int cipher)
                    268: {
                    269:        unsigned long *data = (unsigned long *) buf;
                    270:        unsigned long *IV = (unsigned long *) iv;
1.1.1.2 ! root      271:        unsigned long bufIV[4];
1.1       root      272:        unsigned long j;
                    273: 
                    274:        len /= get_block_size (cipher);
                    275: 
                    276:        bufIV[0] = IV[0];
                    277:        bufIV[1] = IV[1];
1.1.1.2 ! root      278:        bufIV[2] = IV[2];
        !           279:        bufIV[3] = IV[3];
1.1       root      280: 
                    281:        if (get_block_size (cipher) == 8)
                    282:        {
                    283:                /* CBC encrypt the buffer */
                    284:                for (j = 0; j < len; j++)
                    285:                {
                    286:                        // CBC
                    287:                        data[0] ^= bufIV[0];
                    288:                        data[1] ^= bufIV[1];
                    289: 
                    290:                        encipher_block (cipher, data, ks);
                    291: 
                    292:                        bufIV[0] = data[0];
                    293:                        bufIV[1] = data[1];
                    294: 
                    295:                        // Cipher text XOR
                    296:                        data[0] ^= IV[2];
                    297:                        data[1] ^= IV[3];
                    298: 
                    299:                        data += 2;
                    300:                }
                    301:        }
1.1.1.2 ! root      302:        else if (get_block_size (cipher) == 16)
        !           303:        {
        !           304:                /* CBC encrypt the buffer */
        !           305:                for (j = 0; j < len; j++)
        !           306:                {
        !           307:                        // CBC
        !           308:                        data[0] ^= bufIV[0];
        !           309:                        data[1] ^= bufIV[1];
        !           310:                        data[2] ^= bufIV[2];
        !           311:                        data[3] ^= bufIV[3];
        !           312: 
        !           313:                        encipher_block (cipher, data, ks);
        !           314: 
        !           315:                        bufIV[0] = data[0];
        !           316:                        bufIV[1] = data[1];
        !           317:                        bufIV[2] = data[2];
        !           318:                        bufIV[3] = data[3];
        !           319: 
        !           320:                        // Cipher text XOR
        !           321:                        data[0] ^= IV[2];
        !           322:                        data[1] ^= IV[3];
        !           323:                        data[2] ^= IV[2];
        !           324:                        data[3] ^= IV[3];
        !           325: 
        !           326:                        data += 4;
        !           327:                }
        !           328:        }
1.1       root      329:        else
                    330:                return 0;
                    331: 
                    332:        return len;
                    333: }
                    334: 
                    335: // DecryptBuffer
                    336: //
                    337: // Decrypts data in buffer
                    338: // Returns number of bytes encrypted
                    339: //
                    340: // len = length of data in bytes
                    341: 
                    342: int _cdecl
                    343: DecryptBuffer (unsigned char *buf,
                    344:                unsigned long len,
                    345:                unsigned char *ks,
                    346:                unsigned char *iv,
                    347:                int cipher)
                    348: {
                    349:        unsigned long *data = (unsigned long *)buf;
                    350:        unsigned long *IV = (unsigned long *) iv;
1.1.1.2 ! root      351:        unsigned long bufIV[4];
1.1       root      352:        unsigned long j;
                    353: 
                    354:        len /= get_block_size (cipher);
                    355: 
                    356:        bufIV[0] = IV[0];
                    357:        bufIV[1] = IV[1];
1.1.1.2 ! root      358:        bufIV[2] = IV[2];
        !           359:        bufIV[3] = IV[3];
1.1       root      360: 
                    361:        if (get_block_size (cipher) == 8)
                    362:        {
1.1.1.2 ! root      363:                /* CBC decrypt the buffer */
1.1       root      364:                for (j = 0; j < len; j++)
                    365:                {
                    366:                        unsigned long a, b;
                    367: 
                    368:                        // Cipher text XOR
                    369:                        data[0] ^= IV[2];
                    370:                        data[1] ^= IV[3];
                    371: 
                    372:                        // CBC
                    373:                        a = data[0];
                    374:                        b = data[1];
                    375: 
                    376:                        decipher_block (cipher, data, ks);
                    377: 
                    378:                        data[0] ^= bufIV[0];
                    379:                        data[1] ^= bufIV[1];
                    380: 
                    381:                        bufIV[0] = a;
                    382:                        bufIV[1] = b;
                    383: 
                    384:                        data += 2;
                    385:                }
                    386:        }
1.1.1.2 ! root      387:        else if (get_block_size (cipher) == 16)
        !           388:        {
        !           389:                /* CBC decrypt the buffer */
        !           390:                for (j = 0; j < len; j++)
        !           391:                {
        !           392:                        unsigned long a[4];
        !           393: 
        !           394:                        // Cipher text XOR
        !           395:                        data[0] ^= IV[2];
        !           396:                        data[1] ^= IV[3];
        !           397:                        data[2] ^= IV[2];
        !           398:                        data[3] ^= IV[3];
        !           399: 
        !           400:                        // CBC
        !           401:                        a[0] = data[0];
        !           402:                        a[1] = data[1];
        !           403:                        a[2] = data[2];
        !           404:                        a[3] = data[3];
        !           405: 
        !           406:                        decipher_block (cipher, data, ks);
        !           407: 
        !           408:                        data[0] ^= bufIV[0];
        !           409:                        data[1] ^= bufIV[1];
        !           410:                        data[2] ^= bufIV[2];
        !           411:                        data[3] ^= bufIV[3];
        !           412: 
        !           413:                        bufIV[0] = a[0];
        !           414:                        bufIV[1] = a[1];
        !           415:                        bufIV[2] = a[2];
        !           416:                        bufIV[3] = a[3];
        !           417: 
        !           418:                        data += 4;
        !           419:                }
        !           420:        }
1.1       root      421:        else
                    422:                return 0;
                    423: 
                    424:        return len;
                    425: }
                    426: 
                    427: // Volume header structure:
                    428: //
                    429: // Offset      Length  Description
                    430: // ------------------------------------------
                    431: // Unencrypted:
                    432: // 0           64              Key salt
                    433: // Encrypted:
                    434: // 64          4               Magic 'TRUE'
                    435: // 68          2               Header version
                    436: // 70          2               Required program version
                    437: // 72          4               CRC32 of disk IV and key
                    438: // 76          8               Volume creation time
                    439: // 84          8               Header creation time
                    440: // 92          164             unused
                    441: // 256         32              Disk IV
                    442: // 288         224             Disk key
                    443: 
                    444: int
                    445: VolumeReadHeader (char *encryptedHeader, char *lpszPassword, PCRYPTO_INFO * retInfo)
                    446: {
                    447:        char header[SECTOR_SIZE];
                    448:        unsigned char *input = (unsigned char *) header;
                    449:        KEY_INFO keyInfo;
                    450:        PCRYPTO_INFO cryptoInfo;
                    451:        int nStatus = 0, nKeyLen;
                    452:        char dk[DISKKEY_SIZE];
                    453:        int pkcs5;
                    454:        int headerVersion, requiredVersion;
                    455:        
                    456:        cryptoInfo = *retInfo = crypto_open ();
                    457:        if (cryptoInfo == NULL)
                    458:                return ERR_OUTOFMEMORY;
                    459: 
                    460:        crypto_loadkey (&keyInfo, lpszPassword, strlen (lpszPassword));
                    461: 
                    462:        // PKCS5 is used to derive header key and IV from user password
                    463:        memcpy (keyInfo.key_salt, encryptedHeader + HEADER_USERKEY_SALT, USERKEY_SALT_SIZE);
                    464:        keyInfo.noIterations = USERKEY_ITERATIONS;              
                    465:        pkcs5 = SHA1;
                    466: 
                    467:        if (pkcs5 == SHA1)
                    468:        {
                    469:                derive_sha_key (keyInfo.userKey, keyInfo.keyLength, keyInfo.key_salt,
                    470:                USERKEY_SALT_SIZE, keyInfo.noIterations, dk, DISK_IV_SIZE + MAX_CIPHER_KEY);
                    471:        }
                    472:        // IV for header decryption
                    473:        memcpy (cryptoInfo->iv, dk, DISK_IV_SIZE);
                    474: 
                    475:        // Test all available ciphers
                    476:        for (cryptoInfo->cipher = 1; cryptoInfo->cipher <= LAST_CIPHER_ID; cryptoInfo->cipher++)
                    477:        {
                    478:                // Copy header for decryption
                    479:                memcpy (header, encryptedHeader, SECTOR_SIZE);  
                    480:                input = header;
                    481: 
                    482:                // Try to decrypt header 
                    483:                init_cipher (cryptoInfo->cipher, dk + DISK_IV_SIZE, cryptoInfo->ks);
                    484: 
                    485:                DecryptBuffer (header + HEADER_ENCRYPTEDDATA, HEADER_ENCRYPTEDDATASIZE,
                    486:                                                        cryptoInfo->ks, cryptoInfo->iv, cryptoInfo->cipher);
                    487: 
                    488:                input += HEADER_ENCRYPTEDDATA;
                    489: 
                    490:                // Magic
                    491:                if (mgetLong (input) != 'TRUE')
                    492:                        continue;
                    493: 
                    494:                // Header version
                    495:                headerVersion = mgetWord (input);
                    496: 
                    497:                // Required program version
                    498:                requiredVersion = mgetWord (input);
                    499: 
                    500:                // Check CRC of disk IV and key
                    501:                if (mgetLong (input) != crc32 (header + HEADER_DISKKEY, DISKKEY_SIZE))
                    502:                        continue;
                    503: 
                    504:                // Volume creation time
                    505:                ((unsigned long *)(&cryptoInfo->volume_creation_time))[1] = mgetLong (input);
                    506:                ((unsigned long *)(&cryptoInfo->volume_creation_time))[0] = mgetLong (input);
                    507: 
                    508:                // Header creation time
                    509:                ((unsigned long *)(&cryptoInfo->header_creation_time))[1] = mgetLong (input);
                    510:                ((unsigned long *)(&cryptoInfo->header_creation_time))[0] = mgetLong (input);
                    511: 
                    512:                // Password and cipher OK
                    513: 
                    514:                // Check the version required to handle this volume
                    515:                if (requiredVersion > VERSION_NUM)
                    516:                        return ERR_NEW_VERSION_REQUIRED;
                    517: 
                    518:                // Disk key
                    519:                nKeyLen = DISKKEY_SIZE;
                    520:                memcpy (keyInfo.key, header + HEADER_DISKKEY, nKeyLen);
                    521: 
                    522:                memcpy (cryptoInfo->master_decrypted_key, keyInfo.key, nKeyLen);
                    523:                memcpy (cryptoInfo->key_salt, keyInfo.key_salt, USERKEY_SALT_SIZE);
                    524:                cryptoInfo->pkcs5 = pkcs5;
                    525:                cryptoInfo->noIterations = keyInfo.noIterations;
                    526: 
                    527:                // Init with decrypted master disk key for sector decryption
                    528:                init_cipher (cryptoInfo->cipher, keyInfo.key + DISK_IV_SIZE, cryptoInfo->ks);
                    529: 
                    530:                // Disk IV
                    531:                memcpy (cryptoInfo->iv, keyInfo.key, DISK_IV_SIZE);
                    532: 
                    533:                /* Clear out the temp. key buffer */
                    534:                burn (dk, sizeof(dk));
                    535: 
1.1.1.2 ! root      536:                switch (get_block_size (cryptoInfo->cipher))
        !           537:                {
        !           538:                case 8:
        !           539:                        cryptoInfo->encrypt_sector = &EncryptSector8;
        !           540:                        cryptoInfo->decrypt_sector = &DecryptSector8;
        !           541:                        break;
        !           542:                case 16:
        !           543:                        cryptoInfo->encrypt_sector = &EncryptSector16;
        !           544:                        cryptoInfo->decrypt_sector = &DecryptSector16;
        !           545:                        break;
        !           546:                }
        !           547:                
1.1       root      548:                return 0;
                    549:        }
                    550: 
                    551:        crypto_close(cryptoInfo);
                    552:        burn (&keyInfo, sizeof (keyInfo));
                    553:        return ERR_PASSWORD_WRONG;
                    554: }
                    555: 
                    556: #ifndef DEVICE_DRIVER
                    557: 
                    558: #ifdef VOLFORMAT
                    559: extern HWND hDiskKey;
                    560: extern HWND hKeySalt;
                    561: #endif
                    562: 
                    563: // VolumeWriteHeader:
                    564: // Creates volume header in memory
                    565: int
                    566: VolumeWriteHeader (char *header, int cipher, char *lpszPassword,
                    567:                   int pkcs5, char *masterKey, unsigned __int64 volumeCreationTime, PCRYPTO_INFO * retInfo )
                    568: {
                    569:        unsigned char *p = (unsigned char *) header;
                    570:        KEY_INFO keyInfo;
                    571: 
                    572:        int nUserKeyLen = strlen(lpszPassword);
                    573:        PCRYPTO_INFO cryptoInfo = crypto_open ();
                    574:        char dk[DISKKEY_SIZE];
                    575:        int x;
                    576: 
                    577:        if (cryptoInfo == NULL)
                    578:                return ERR_OUTOFMEMORY;
                    579: 
                    580:        memset (header, 0, SECTOR_SIZE);
1.1.1.2 ! root      581:        VirtualLock (&keyInfo, sizeof (keyInfo));
1.1       root      582: 
                    583:        //// Encryption setup
                    584: 
1.1.1.2 ! root      585:        // Generate disk key and IV
        !           586:        if(masterKey == 0)
        !           587:                RandgetBytes (keyInfo.key, DISKKEY_SIZE, TRUE);
        !           588:        else
        !           589:                memcpy (keyInfo.key, masterKey, DISKKEY_SIZE);
        !           590: 
1.1       root      591:        // User key
                    592:        memcpy (keyInfo.userKey, lpszPassword, nUserKeyLen);
                    593:        keyInfo.keyLength = nUserKeyLen;
                    594:        keyInfo.noIterations = USERKEY_ITERATIONS;
                    595: 
                    596:        // User selected encryption algorithm
                    597:        cryptoInfo->cipher = cipher;
                    598: 
                    599:        // Salt for header key derivation 
1.1.1.2 ! root      600:        RandgetBytes (keyInfo.key_salt, USERKEY_SALT_SIZE, TRUE);
1.1       root      601: 
                    602:        // PKCS5 is used to derive header key and IV from user password
                    603:        if (pkcs5 == SHA1)
                    604:        {
                    605:                derive_sha_key (keyInfo.userKey, keyInfo.keyLength, keyInfo.key_salt,
                    606:                                USERKEY_SALT_SIZE, keyInfo.noIterations, dk, DISK_IV_SIZE + MAX_CIPHER_KEY );
                    607:        }
                    608: 
                    609: 
                    610:        //// Header setup
                    611: 
                    612:        // Salt
                    613:        mputBytes (p, keyInfo.key_salt, USERKEY_SALT_SIZE);     
                    614: 
                    615:        // Magic
                    616:        mputLong (p, 'TRUE');
                    617: 
                    618:        // Header version
                    619:        mputWord (p, 0x0001);
                    620: 
                    621:        // Required program version to handle this volume
                    622:        mputWord (p, VOLUME_VERSION_NUM);
                    623: 
                    624:        // CRC of disk key
                    625:        x = crc32(keyInfo.key, DISKKEY_SIZE);
                    626:        mputLong (p, x);
                    627: 
                    628:        // Time
                    629:        {
                    630:                SYSTEMTIME st;
                    631:                FILETIME ft;
                    632: 
                    633:                // Volume creation time
                    634:                if (volumeCreationTime == 0)
                    635:                {
                    636:                        GetLocalTime (&st);
                    637:                        SystemTimeToFileTime (&st, &ft);
                    638:                }
                    639:                else
                    640:                {
                    641:                        ft.dwHighDateTime = (DWORD)(volumeCreationTime >> 32);
                    642:                        ft.dwLowDateTime = (DWORD)volumeCreationTime;
                    643:                }
                    644:                mputLong (p, ft.dwHighDateTime);
                    645:                mputLong (p, ft.dwLowDateTime);
                    646: 
                    647:                // Password change time
                    648:                GetLocalTime (&st);
                    649:                SystemTimeToFileTime (&st, &ft);
                    650:                mputLong (p, ft.dwHighDateTime);
                    651:                mputLong (p, ft.dwLowDateTime);
                    652:        }
                    653: 
                    654:        // Disk key and IV
                    655:        memcpy (header + HEADER_DISKKEY, keyInfo.key, DISKKEY_SIZE);
                    656: 
                    657: 
                    658:        //// Header encryption
                    659: 
                    660:        memcpy (cryptoInfo->iv, dk, DISK_IV_SIZE);
                    661:        init_cipher (cryptoInfo->cipher, dk + DISK_IV_SIZE, cryptoInfo->ks);
                    662: 
                    663:        EncryptBuffer (header + HEADER_ENCRYPTEDDATA, HEADER_ENCRYPTEDDATASIZE, 
                    664:                        cryptoInfo->ks, cryptoInfo->iv, cryptoInfo->cipher);
                    665: 
                    666: 
                    667:        //// cryptoInfo setup for further use (disk format)
                    668: 
                    669:        // Init with master disk key for sector decryption
                    670:        init_cipher (cryptoInfo->cipher, keyInfo.key + DISK_IV_SIZE, cryptoInfo->ks);
                    671: 
                    672:        // Disk IV
                    673:        memcpy (cryptoInfo->iv, keyInfo.key, DISK_IV_SIZE);
                    674: 
                    675:        // Clear out the temp. key buffer
                    676:        burn (dk, sizeof(dk));
                    677: 
1.1.1.2 ! root      678:        switch (get_block_size (cryptoInfo->cipher))
1.1       root      679:        {
1.1.1.2 ! root      680:        case 8:
1.1       root      681:                cryptoInfo->encrypt_sector = &EncryptSector8;
                    682:                cryptoInfo->decrypt_sector = &DecryptSector8;
1.1.1.2 ! root      683:                break;
        !           684:        case 16:
        !           685:                cryptoInfo->encrypt_sector = &EncryptSector16;
        !           686:                cryptoInfo->decrypt_sector = &DecryptSector16;
        !           687:                break;
1.1       root      688:        }
                    689: 
1.1.1.2 ! root      690: 
1.1       root      691: #ifdef VOLFORMAT
                    692:        {
                    693:                char tmp[64];
                    694:                BOOL dots3 = FALSE;
                    695:                int i, j;
                    696: 
                    697:                j = get_key_size (cipher);
                    698: 
                    699:                if (j > 21)
                    700:                {
                    701:                        dots3 = TRUE;
                    702:                        j = 21;
                    703:                }
                    704: 
                    705:                tmp[0] = 0;
                    706:                for (i = 0; i < j; i++)
                    707:                {
                    708:                        char tmp2[8] =
                    709:                        {0};
                    710:                        sprintf (tmp2, "%02X", (int) (unsigned char) keyInfo.key[i + DISK_IV_SIZE]);
                    711:                        strcat (tmp, tmp2);
                    712:                }
                    713: 
                    714:                if (dots3 == TRUE)
                    715:                {
                    716:                        strcat (tmp, "...");
                    717:                }
                    718: 
                    719: 
                    720:                SetWindowText (hDiskKey, tmp);
                    721: 
                    722:                tmp[0] = 0;
                    723:                for (i = 0; i < 20; i++)
                    724:                {
                    725:                        char tmp2[8];
                    726:                        sprintf (tmp2, "%02X", (int) (unsigned char) keyInfo.key_salt[i]);
                    727:                        strcat (tmp, tmp2);
                    728:                }
                    729: 
                    730:                SetWindowText (hKeySalt, tmp);
                    731:        }
                    732: #endif
                    733: 
                    734:        burn (&keyInfo, sizeof (keyInfo));
1.1.1.2 ! root      735:        VirtualUnlock (&keyInfo, sizeof (keyInfo));
1.1       root      736: 
                    737:        *retInfo = cryptoInfo;
                    738:        return 0;
                    739: }
                    740: 
                    741: 
                    742: #endif                         /* !NT4_DRIVER */

unix.superglobalmegacorp.com

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