2012-10-16 34 views
5

我有一個程序使用一次性時間片加密將兩個文件異或。由於密鑰文件具有這種敏感特性,因此我不希望密鑰文件的任何痕跡出現在計算機硬盤驅動器上,因爲這可能會危及安全性。C - 在RAM中運行程序

問題是,如何在RAM中運行程序以避免在HD上留下任何痕跡?或者,從閃存驅動器運行該程序是否包含密鑰文件到閃存驅動器的痕跡?

下面是密鑰文件是如何在程序處理:

/* Check if keyfile can be opened. */ 
if((keyfile = fopen(argv[3], "rb"))== NULL) 
{ 
printf("Can't open keyfile.\n"); 
printf("Please enter a valid filename.\n"); 
printf("USAGE: OTP <source file> <output file> <keyfile>\n"); 
perror("Error"); 
return(1); 
}        

/* Get size of keyfile */ 
fstat(fileno(keyfile), &keybuf); 

/* Check if keyfile is the same size as, or bigger than the sourcefile */ 
if((keybuf.st_size) < (statbuf.st_size)) 
{ 
printf("Source file is larger than keyfile.\n"); 
printf("This significantly reduces cryptographic strength.\n"); 
printf("Do you wish to continue? (Y/N)\n"); 
fgets(buffer, 20, stdin); 
sscanf(buffer, "%c", &ans); 
if(ans == 'n' || ans == 'N') 
{ 
return (1); 
} 
if(ans == 'y' || ans == 'Y') 
{ 
    printf("Proceeding with Encryption/Decryption.\n"); 
    } 

/* Encrypt/Decrypt and write to output file. */ 
while(count < (statbuf.st_size)) 
{ 
key=fgetc(keyfile); 
data=fgetc(sourcefile); 

output=(key^data); 

fputc(output,destfile); 
count++; 
} 

/* Close files. */ 
fclose(keyfile); 
fclose(sourcefile); 
fclose(destfile); 

我讀到一篇inram功能谷歌搜索這個時候,但這似乎並沒有爲我需要什麼來。

回答

4

我假設您正在從某些外部媒體讀取密鑰文件,並且擔心將進程與包含OTP的I/O緩衝區一起交換到磁盤。您可能同樣擔心正在編寫的純文本。如果你在一個posix系統(如linux),那麼你應該看看mlockmlockall函數。這些調用會將內存頁鎖定到RAM中,並禁止它們交換到磁盤。手冊頁專門爲這些調用提出安全用例。另一個選項可能是mmap這些文件。雖然它不具有相同的保證,但由於映射頁面將由外部媒體支持,所以我懷疑它們會出現在交換空間中。

+0

謝謝!這似乎正是我需要的。你的假設是非常正確的。 – youjustreadthis

+0

嗨,再次,我最近的另一個線程:http://stackoverflow.com/questions/12990214/is-this-usage-of-mlockall-correct#12990658並得到了一個令人困惑的答案那裏,似乎與你有什麼矛盾在這裏說。你可能會看看並給出你的意見的答案? @Geoff Reedy – youjustreadthis