I would do something like this (pseudo code):
1. load sensitive encrypted data from file
2. decrypt the data
3. do something with the unencrypted data
4. override the data safely / securely (for example with random data)
The time that the sensitive data lies plain (unencrypted) in memory should be as short as possible.
The plain data must not be leaked in any way.
A. Can such a program be written in Haskell or OCAML?
B. Can it be prevented that the data gets leaked, i.e. by being silently copied in the background by the garbage collector?
C. Can the plain data be properly overridden in memory?
As far as I know garbage collectors (GCs) can make copies of data silently in the background. I guess that is done by generational GC algorithms, but I don't know for sure.
I know that it still would be possible for an attacker to get the plain data if the attacker manages to get the memory of the program at the right time / state. I just consider to do that to raise security because I do not have the context (i.e. OS, swapping etc.) under control.
ScrubbedBytes
, which is implemented inmemory
package and is used precisely for this purpose bycryptonite
library: stackage.org/haddock/nightly-2020-06-01/memory-0.15.0/… It is allocated as pinned, so it doesn't move and memory is cleaned before being garbage collected. – Forewoman