Temp file that exists only in RAM?
Asked Answered
L

5

6

I'm trying to write an encrpytion using the OTP method. In keeping with the security theories I need the plain text documents to be stored only in memory and never ever written to a physical drive. The tmpnam command appears to be what I need, but from what I can see it saves the file on the disk and not the RAM.

Using C++ is there any (platform independent) method that allows a file to exist only in RAM? I would like to avoid using a RAM disk method if possible.

Thanks

Edit: Thanks, its more just a learning thing for me, I'm new to encryption and just working through different methods, I don't actually plan on using many of them (esspecially OTP due to doubling the original file size because of the "pad").

If I'm totally honest, I'm a Linux user so ditching Windows wouldn't be too bad, I'm looking into using RAM disks for now as FUSE seems a bit overkill for a "learning" thing.

Lynnell answered 15/2, 2009 at 21:8 Comment(0)
L
12

The simple answer is: no, there is no platform independent way. Even keeping the data only in memory, it will still risk being swapped out to disk by the virtual memory manager.

On Windows, you can use VirtualLock() to force the memory to stay in RAM. You can also use CryptProtectMemory() to prevent other processes from reading it.

On POSIX systems (e.g. BSD, Linux) you can use mlock() to lock memory in RAM.

Laskowski answered 15/2, 2009 at 21:21 Comment(0)
K
5

Not really unless you count in-memory streams (like stringstream).

No especially and specifically for security purposes: any piece of data can be swapped to disk on virtual memory systems.

Generally, if you are concerned about security, you have to use platform-specific methods for controlling access: What good is keeping your data in RAM if everyone can read it?

Kearns answered 15/2, 2009 at 21:16 Comment(0)
M
3

You might want to look at TrueCrypt's source code. Getting code at the file system level might be your best bet.

Maestas answered 15/2, 2009 at 21:26 Comment(1)
This is a very good suggestion: TrueCrypt is a fabulous piece of work.Nonentity
T
1

OTP is an awful encryption method for arbitrary files, unless you have a massive amount of entropy that you can guarantee never repeats itself (that's why it's called "one-time"!)

If you want to create a file-like object that only exists in memory and you don't care about Windows, I'd look at writing a custom FUSE filesystem (http://fuse.sourceforge.net/); this way you guarantee what will and will not get written to disk, and your files are accessible by all programs.

Thrill answered 15/2, 2009 at 21:16 Comment(0)
F
1

Using one of std::stringstream or fmemopen will get you file-like access to blocks of memory. If (for security) you want to avoid it being swapped out, use mlock which is probably easiest to use with fmemopen's buffer than std::stringstream. Combining mlock with std::stringstream would probably need to be done via a custom allocator (used as a template parameter).

Flannel answered 16/2, 2009 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.