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