-1

C based application is using aes with key size of 256. Data is available in binary form, it is encrypted and is written in the binary file. Requirment is to decrypt this binary file in RAM (i.e on the fly / real time encryption). Question is how to achieve on the fly encryption in efficient way? Any good web links or code references for understanding on the fly encryption are required.

In more simple way the question is how to decrypt large files in memory using c (Linux)? Like in truecrypt.

neutrino
  • 17
  • 10
  • If you are using a [block cipher mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation) like CTR or XTS then random access is no problem. See http://stackoverflow.com/a/22958889/371137 for a detailed discussion. – Perseids Apr 17 '14 at 06:57

1 Answers1

0

Use mmap on the file; the file is then opened as a datastream in memory. For example, a simple memory-changing function that XOR's each byte in a file on a large (say, 400Gb) file:

// The encryption function
void xor_ram  (unsigned char *buffer, long len) {
     while (len--) *buffer ^= *buffer++;
}

// The file we want to encrypt
int fd = open ("/path/to/file", O_RDWR);

// Figure out the file length
FILE *tmpf = fdopen (fd, "r");
fseek (tmpf, 0, SEEK_END);
long length = ftell (tmpf);

// Memory map the file using the fd
unsigned char *mapped_file = mmap (NULL, length, 
                                   PROT_READ | PROT_WRITE, MAP_PRIVATE,
                                   fd, 0); 
// Call the encryption function
xor_ram (mapped_file, length);

// All done now
munmap (mapped_file, length);
close (fd);

You can read the manpage for mmap here: http://unixhelp.ed.ac.uk/CGI/man-cgi?mmap Although you should really find the documentation for mmap on your particular platform (man mmap if you're on a unix system of some sort, or search the platforms libraries if not).

Lelanthran
  • 1,510
  • 12
  • 18
  • I hope Mmap will give me some performance efficiency by keeping encrypted file in the memory.I was loking for some efficient way to read the encrypted file block by block and decrypting each block in memory, I have some how achieved it. My next step will be mmap. Thank u for your help – neutrino Apr 30 '14 at 08:48
  • If you're using `int fd = open ("/path/to/file", O_RDWR);`, you're already using POSIX functionality. Just use `fstat()` to get the file size. – Andrew Henle Nov 19 '19 at 17:42