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

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