Annotation of truecrypt/common/fat.c, revision 1.1.1.7

1.1.1.6   root        1: /* Legal Notice: The source code contained in this file has been derived from
                      2:    the source code of Encryption for the Masses 2.02a, which is Copyright (c)
                      3:    1998-99 Paul Le Roux and which is covered by the 'License Agreement for
                      4:    Encryption for the Masses'. Modifications and additions to that source code
                      5:    contained in this file are Copyright (c) 2004-2005 TrueCrypt Foundation and
                      6:    Copyright (c) 2004 TrueCrypt Team, and are covered by TrueCrypt License 2.0
                      7:    the full text of which is contained in the file License.txt included in
                      8:    TrueCrypt binary and source code distribution archives.  */
                      9: 
                     10: #include "Tcdefs.h"
                     11: 
                     12: #include "Crypto.h"
                     13: #include "Random.h"
                     14: #include "Format.h"
                     15: #include "Fat.h"
                     16: #include "Progress.h"
1.1       root       17: 
                     18: #include <time.h>
                     19: 
                     20: void
                     21: GetFatParams (fatparams * ft)
                     22: {
                     23:        int fatsecs;
1.1.1.5   root       24:        if(ft->cluster_size == 0)       // 'Default' cluster size
1.1       root       25:        {
1.1.1.6   root       26:                if (ft->num_sectors * 512I64 >= 256*BYTES_PER_GB)
1.1       root       27:                        ft->cluster_size = 128;
1.1.1.6   root       28:                else if (ft->num_sectors * 512I64 >= 64*BYTES_PER_GB)
1.1       root       29:                        ft->cluster_size = 64;
1.1.1.6   root       30:                else if (ft->num_sectors * 512I64 >= 16*BYTES_PER_GB)
1.1       root       31:                        ft->cluster_size = 32;
1.1.1.6   root       32:                else if (ft->num_sectors * 512I64 >= 8*BYTES_PER_GB)
1.1       root       33:                        ft->cluster_size = 16;
1.1.1.6   root       34:                else if (ft->num_sectors * 512I64 >= 128*BYTES_PER_MB)
1.1       root       35:                        ft->cluster_size = 8;
1.1.1.6   root       36:                else if (ft->num_sectors * 512I64 >= 64*BYTES_PER_MB)
1.1       root       37:                        ft->cluster_size = 4;
1.1.1.6   root       38:                else if (ft->num_sectors * 512I64 >= 32*BYTES_PER_MB)
1.1       root       39:                        ft->cluster_size = 2;
                     40:                else
                     41:                        ft->cluster_size = 1;
                     42:        }
                     43: 
                     44:        // Geometry always set to SECTORS/1/1
                     45:        ft->secs_track = 1; 
                     46:        ft->heads = 1; 
                     47: 
                     48:        ft->dir_entries = 512;
                     49:        ft->fats = 2;
1.1.1.6   root       50:        ft->create_time = (unsigned int) time (NULL);
1.1       root       51:        ft->media = 0xf8;
                     52:        ft->sector_size = SECTOR_SIZE;
1.1.1.6   root       53:        ft->hidden = 63;
1.1       root       54: 
                     55:        ft->size_root_dir = ft->dir_entries * 32;
                     56: 
1.1.1.6   root       57:        // FAT12
1.1       root       58:        ft->size_fat = 12;
1.1.1.6   root       59:        ft->reserved = 2;
                     60:        fatsecs = ft->num_sectors - (ft->size_root_dir + SECTOR_SIZE - 1) / SECTOR_SIZE - ft->reserved;
                     61:        ft->cluster_count = (int) (((__int64) fatsecs * SECTOR_SIZE) / (ft->cluster_size * SECTOR_SIZE + 3));
                     62:        ft->fat_length = (((ft->cluster_count * 3 + 1) >> 1) + SECTOR_SIZE - 1) / SECTOR_SIZE;
1.1       root       63: 
1.1.1.6   root       64:        if (ft->cluster_count >= 4085) // FAT16
1.1       root       65:        {
                     66:                ft->size_fat = 16;
1.1.1.6   root       67:                ft->reserved = 8;
                     68:                fatsecs = ft->num_sectors - (ft->size_root_dir + SECTOR_SIZE - 1) / SECTOR_SIZE - ft->reserved;
                     69:                ft->cluster_count = (int) (((__int64) fatsecs * SECTOR_SIZE) / (ft->cluster_size * SECTOR_SIZE + 4));
                     70:                ft->fat_length = (ft->cluster_count * 2 + SECTOR_SIZE - 1) / SECTOR_SIZE;
1.1       root       71:        }
1.1.1.6   root       72:        
                     73:        if(ft->cluster_count >= 65525) // FAT32
1.1       root       74:        {
                     75:                ft->size_fat = 32;
1.1.1.6   root       76:                ft->reserved = 38;
                     77:                fatsecs = ft->num_sectors - ft->reserved;
1.1       root       78:                ft->size_root_dir = ft->cluster_size * SECTOR_SIZE;
1.1.1.6   root       79:                ft->cluster_count = (int) (((__int64) fatsecs * SECTOR_SIZE) / (ft->cluster_size * SECTOR_SIZE + 8));
                     80:                ft->fat_length = (ft->cluster_count * 4 + SECTOR_SIZE - 1) / SECTOR_SIZE;
1.1       root       81:        }
                     82: 
                     83:        if (ft->num_sectors >= 65536 || ft->size_fat == 32)
                     84:        {
                     85:                ft->sectors = 0;
                     86:                ft->total_sect = ft->num_sectors;
                     87:        }
                     88:        else
                     89:        {
                     90:                ft->sectors = ft->num_sectors;
                     91:                ft->total_sect = 0;
                     92:        }
                     93: }
                     94: 
                     95: void
                     96: PutBoot (fatparams * ft, unsigned char *boot)
                     97: {
                     98:        int cnt = 0;
                     99: 
                    100:        boot[cnt++] = 0xeb;     /* boot jump */
                    101:        boot[cnt++] = 0x3c;
                    102:        boot[cnt++] = 0x90;
1.1.1.6   root      103:        memcpy (boot + cnt, "MSDOS5.0", 8); /* system id */
1.1       root      104:        cnt += 8;
1.1.1.6   root      105:        memcpy (boot + cnt, &ft->sector_size, 2);       /* bytes per sector */
1.1       root      106:        cnt += 2;
1.1.1.6   root      107:        memcpy (boot + cnt, &ft->cluster_size, 1);      /* sectors per cluster */
1.1       root      108:        cnt++;
1.1.1.6   root      109:        memcpy (boot + cnt, &ft->reserved, 2);          /* reserved sectors */
                    110:        cnt += 2;
                    111:        memcpy (boot + cnt, &ft->fats, 1);                      /* 2 fats */
1.1       root      112:        cnt++;
                    113: 
                    114:        if(ft->size_fat == 32)
                    115:        {
                    116:                boot[cnt++] = 0x00;
                    117:                boot[cnt++] = 0x00;
                    118:        }
                    119:        else
                    120:        {
1.1.1.6   root      121:                memcpy (boot + cnt, &ft->dir_entries, 2);       /* 512 root entries */
1.1       root      122:                cnt += 2;
                    123:        }
                    124: 
1.1.1.6   root      125:        memcpy (boot + cnt, &ft->sectors, 2);                   /* # sectors */
1.1       root      126:        cnt += 2;
1.1.1.6   root      127:        memcpy (boot + cnt, &ft->media, 1);                             /* media byte */
1.1       root      128:        cnt++;
                    129: 
                    130:        if(ft->size_fat == 32)  
                    131:        {
                    132:                boot[cnt++] = 0x00;
                    133:                boot[cnt++] = 0x00;
                    134:        }
                    135:        else 
                    136:        { 
1.1.1.6   root      137:                memcpy (boot + cnt, &ft->fat_length, 2);        /* fat size */
1.1       root      138:                cnt += 2;
                    139:        }
                    140: 
1.1.1.6   root      141:        memcpy (boot + cnt, &ft->secs_track, 2);        /* # sectors per track */
1.1       root      142:        cnt += 2;
1.1.1.6   root      143:        memcpy (boot + cnt, &ft->heads, 2);                     /* # heads */
1.1       root      144:        cnt += 2;
1.1.1.6   root      145:        memcpy (boot + cnt, &ft->hidden, 4);            /* # hidden sectors */
                    146:        cnt += 4;
                    147:        memcpy (boot + cnt, &ft->total_sect, 4);        /* # huge sectors */
1.1       root      148:        cnt += 4;
                    149: 
                    150:        if(ft->size_fat == 32)
                    151:        {
                    152:                memcpy (boot + cnt, &ft->fat_length, 4); cnt += 4;      /* fat size 32 */
1.1.1.6   root      153:                boot[cnt++] = 0x00;     /* ExtFlags */
1.1       root      154:                boot[cnt++] = 0x00;
                    155:                boot[cnt++] = 0x00;     /* FSVer */
                    156:                boot[cnt++] = 0x00;
                    157:                boot[cnt++] = 0x02;     /* RootClus */
                    158:                boot[cnt++] = 0x00;
                    159:                boot[cnt++] = 0x00;
                    160:                boot[cnt++] = 0x00;
                    161:                boot[cnt++] = 0x01;     /* FSInfo */
                    162:                boot[cnt++] = 0x00;
                    163:                boot[cnt++] = 0x06;     /* BkBootSec */
                    164:                boot[cnt++] = 0x00;
                    165:                memset(boot+cnt, 0, 12); cnt+=12;       /* Reserved */
                    166:        }
                    167: 
1.1.1.6   root      168:        boot[cnt++] = 0x00;     /* drive number */   // FIXED 80 > 00
1.1       root      169:        boot[cnt++] = 0x00;     /* reserved */
                    170:        boot[cnt++] = 0x29;     /* boot sig */
1.1.1.6   root      171:        memcpy (boot + cnt, &ft->create_time, 4);       /* vol id */
1.1       root      172:        cnt += 4;
1.1.1.6   root      173:        memcpy (boot + cnt, ft->volume_name, 11);       /* vol title */
1.1       root      174:        cnt += 11;
                    175: 
                    176:        switch(ft->size_fat) /* filesystem type */
                    177:        {
                    178:                case 12: memcpy (boot + cnt, "FAT12   ", 8); break;
                    179:                case 16: memcpy (boot + cnt, "FAT16   ", 8); break;
                    180:                case 32: memcpy (boot + cnt, "FAT32   ", 8); break;
                    181:        }
                    182:        cnt += 8;
                    183: 
                    184:        memset (boot + cnt, 0, ft->size_fat==32 ? 420:448);     /* boot code */
                    185:        cnt += ft->size_fat==32 ? 420:448;
                    186:        boot[cnt++] = 0x55;
                    187:        boot[cnt++] = 0xaa;     /* boot sig */
                    188: }
                    189: 
                    190: /* FAT32 FSInfo */
                    191: PutFSInfo (unsigned char *sector)
                    192: {
1.1.1.6   root      193:        memset (sector, 0, 512);
                    194:        sector[3]=0x41; /* LeadSig */
                    195:        sector[2]=0x61; 
                    196:        sector[1]=0x52; 
                    197:        sector[0]=0x52; 
                    198:        sector[484+3]=0x61; /* StrucSig */
                    199:        sector[484+2]=0x41; 
                    200:        sector[484+1]=0x72; 
                    201:        sector[484+0]=0x72; 
                    202:        sector[488+3]=0xff; /* Free_Count */
                    203:        sector[488+2]=0xff;
                    204:        sector[488+1]=0xff;
                    205:        sector[488+0]=0xff;
                    206:        sector[492+3]=0xff; /* Nxt_Free */
                    207:        sector[492+2]=0xff;
                    208:        sector[492+1]=0xff;
                    209:        sector[492+0]=0xff;
                    210:        sector[508+3]=0xaa; /* TrailSig */
                    211:        sector[508+2]=0x55;
                    212:        sector[508+1]=0x00;
                    213:        sector[508+0]=0x00;
1.1       root      214: }
                    215: 
                    216: 
                    217: int
1.1.1.6   root      218: FormatFat (unsigned __int64 startSector, fatparams * ft, HFILE dev, PCRYPTO_INFO cryptoInfo, diskio_f write, BOOL quickFormat)
1.1       root      219: {
                    220:        int write_buf_cnt = 0;
                    221:        char sector[SECTOR_SIZE], *write_buf;
1.1.1.4   root      222:        unsigned __int64 nSecNo = startSector;
                    223:        LARGE_INTEGER startOffset;
                    224:        LARGE_INTEGER newOffset;
1.1       root      225:        int x, n;
1.1.1.6   root      226:        int retVal;
1.1.1.7 ! root      227:        char temporaryKey[DISKKEY_SIZE];
1.1       root      228: 
1.1.1.4   root      229:        // Seek to start sector
                    230:        startOffset.QuadPart = startSector * SECTOR_SIZE;
1.1.1.6   root      231:        if (!SetFilePointerEx ((HANDLE) dev, startOffset, &newOffset, FILE_BEGIN)
                    232:                || newOffset.QuadPart != startOffset.QuadPart)
1.1.1.4   root      233:        {
                    234:                return ERR_VOL_SEEKING;
                    235:        }
1.1       root      236: 
1.1.1.4   root      237:        /* Write the data area */
1.1       root      238: 
1.1.1.4   root      239:        write_buf = TCalloc (WRITE_BUF_SIZE);
1.1       root      240:        memset (sector, 0, sizeof (sector));
                    241: 
                    242:        PutBoot (ft, (unsigned char *) sector);
1.1.1.6   root      243:        if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    244:                cryptoInfo, write) == FALSE)
1.1       root      245:                goto fail;
                    246: 
                    247:        /* fat32 boot area */
                    248:        if (ft->size_fat == 32)                         
                    249:        {
                    250:                /* fsinfo */
                    251:                PutFSInfo((unsigned char *) sector);
1.1.1.6   root      252:                if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    253:                        cryptoInfo, write) == FALSE)
1.1       root      254:                        goto fail;
                    255: 
                    256:                /* reserved */
1.1.1.4   root      257:                while (nSecNo - startSector < 6)
1.1       root      258:                {
                    259:                        memset (sector, 0, sizeof (sector));
                    260:                        sector[508+3]=0xaa; /* TrailSig */
                    261:                        sector[508+2]=0x55;
1.1.1.6   root      262:                        if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    263:                                cryptoInfo, write) == FALSE)
1.1       root      264:                                goto fail;
                    265:                }
                    266:                
                    267:                /* bootsector backup */
                    268:                memset (sector, 0, sizeof (sector));
                    269:                PutBoot (ft, (unsigned char *) sector);
1.1.1.6   root      270:                if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    271:                                 cryptoInfo, write) == FALSE)
1.1       root      272:                        goto fail;
                    273: 
                    274:                PutFSInfo((unsigned char *) sector);
1.1.1.6   root      275:                if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    276:                        cryptoInfo, write) == FALSE)
1.1       root      277:                        goto fail;
1.1.1.6   root      278:        }
1.1       root      279: 
1.1.1.6   root      280:        /* reserved */
                    281:        while (nSecNo - startSector < ft->reserved)
                    282:        {
                    283:                memset (sector, 0, sizeof (sector));
                    284:                if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    285:                        cryptoInfo, write) == FALSE)
                    286:                        goto fail;
1.1       root      287:        }
                    288: 
                    289:        /* write fat */
                    290:        for (x = 1; x <= ft->fats; x++)
                    291:        {
                    292:                for (n = 0; n < ft->fat_length; n++)
                    293:                {
                    294:                        memset (sector, 0, SECTOR_SIZE);
                    295: 
                    296:                        if (n == 0)
                    297:                        {
                    298:                                unsigned char fat_sig[12];
                    299:                                if (ft->size_fat == 32)
                    300:                                {
                    301:                                        fat_sig[0] = (unsigned char) ft->media;
                    302:                                        fat_sig[1] = fat_sig[2] = 0xff;
                    303:                                        fat_sig[3] = 0x0f;
                    304:                                        fat_sig[4] = fat_sig[5] = fat_sig[6] = 0xff;
                    305:                                        fat_sig[7] = 0x0f;
                    306:                                        fat_sig[8] = fat_sig[9] = fat_sig[10] = 0xff;
                    307:                                        fat_sig[11] = 0x0f;
                    308:                                        memcpy (sector, fat_sig, 12);
                    309:                                }                               
                    310:                                else if (ft->size_fat == 16)
                    311:                                {
                    312:                                        fat_sig[0] = (unsigned char) ft->media;
                    313:                                        fat_sig[1] = 0xff;
                    314:                                        fat_sig[2] = 0xff;
                    315:                                        fat_sig[3] = 0xff;
                    316:                                        memcpy (sector, fat_sig, 4);
                    317:                                }
                    318:                                else if (ft->size_fat == 12)
                    319:                                {
                    320:                                        fat_sig[0] = (unsigned char) ft->media;
                    321:                                        fat_sig[1] = 0xff;
                    322:                                        fat_sig[2] = 0xff;
                    323:                                        fat_sig[3] = 0x00;
                    324:                                        memcpy (sector, fat_sig, 4);
                    325:                                }
                    326:                        }
                    327: 
1.1.1.6   root      328:                        if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    329:                                    cryptoInfo, write) == FALSE)
1.1       root      330:                                goto fail;
                    331:                }
                    332:        }
                    333: 
                    334: 
                    335:        /* write rootdir */
                    336:        for (x = 0; x < ft->size_root_dir / SECTOR_SIZE; x++)
                    337:        {
                    338:                memset (sector, 0, SECTOR_SIZE);
1.1.1.6   root      339:                if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    340:                                 cryptoInfo, write) == FALSE)
1.1       root      341:                        goto fail;
                    342: 
                    343:        }
                    344: 
1.1.1.7 ! root      345:        /* Fill the rest of the data area with random data */
1.1.1.4   root      346: 
1.1       root      347:        if(!quickFormat)
                    348:        {
1.1.1.7 ! root      349:                /* Generate a random temporary key set to be used for "dummy" encryption that will fill
        !           350:                the free disk space (data area) with random data.  This is necessary for plausible
        !           351:                deniability of hidden volumes (and also reduces the amount of predictable plaintext
        !           352:                within the volume). */
1.1       root      353: 
1.1.1.7 ! root      354:                RandgetBytes (temporaryKey, DISKKEY_SIZE, FALSE);                               // Temporary master key
        !           355:                RandgetBytes (cryptoInfo->iv, sizeof cryptoInfo->iv, FALSE);    // Secondary key (LRW mode)
1.1.1.6   root      356: 
1.1.1.7 ! root      357:                retVal = EAInit (cryptoInfo->ea, temporaryKey, cryptoInfo->ks);
1.1.1.6   root      358:                if (retVal != 0)
1.1.1.7 ! root      359:                {
        !           360:                        burn (temporaryKey, sizeof(temporaryKey));
1.1.1.6   root      361:                        return retVal;
1.1.1.7 ! root      362:                }
        !           363:                if (!EAInitMode (cryptoInfo))
        !           364:                {
        !           365:                        burn (temporaryKey, sizeof(temporaryKey));
        !           366:                        return ERR_MODE_INIT_FAILED;
        !           367:                }
1.1       root      368: 
1.1.1.6   root      369:                x = ft->num_sectors - ft->reserved - ft->size_root_dir / SECTOR_SIZE - ft->fat_length * 2;
1.1       root      370:                while (x--)
                    371:                {
1.1.1.7 ! root      372:                        /* Generate random plaintext. Note that reused plaintext blocks are not a concern
        !           373:                        here since LRW mode is designed to hide patterns. Furthermore, patterns in plaintext
        !           374:                        do occur commonly on media in the "real world", so it might actually be a fatal
        !           375:                        mistake to try to avoid them completely. */
        !           376: #if RNG_POOL_SIZE < SECTOR_SIZE
        !           377:                        RandpeekBytes (sector, RNG_POOL_SIZE);
        !           378:                        RandpeekBytes (sector + RNG_POOL_SIZE, SECTOR_SIZE - RNG_POOL_SIZE);
        !           379: #else
        !           380:                        RandpeekBytes (sector, SECTOR_SIZE);
        !           381: #endif
        !           382: 
        !           383:                        // Encrypt the random plaintext and write it to the disk
1.1.1.6   root      384:                        if (WriteSector (dev, sector, write_buf, &write_buf_cnt, &nSecNo,
                    385:                                cryptoInfo, write) == FALSE)
1.1       root      386:                                goto fail;
                    387:                }
1.1.1.3   root      388:                UpdateProgressBar (nSecNo);
1.1       root      389:        }
1.1.1.3   root      390:        else
                    391:                UpdateProgressBar (ft->num_sectors);
                    392:                
1.1       root      393:        if (write_buf_cnt != 0 && (*write) (dev, write_buf, write_buf_cnt) == HFILE_ERROR)
                    394:                goto fail;
                    395: 
                    396:        TCfree (write_buf);
1.1.1.7 ! root      397:        burn (temporaryKey, sizeof(temporaryKey));
1.1       root      398:        return 0;
                    399: 
1.1.1.7 ! root      400: fail:
1.1       root      401: 
                    402:        TCfree (write_buf);
1.1.1.7 ! root      403:        burn (temporaryKey, sizeof(temporaryKey));
1.1       root      404:        return ERR_OS_ERROR;
                    405: }

unix.superglobalmegacorp.com

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