I'm using AES to encrypt something, through calling the java crypto libraries from Clojure. As a part of this I'm generating a new key that I'm using for the encryption (in my case a "DataKey" from Amazon's KMS). Amazon recommends removing the data key from memory as soon as it's been used.
As a code example, kms-generated-key
is the response I get from the KMS api (via the aws-api library) when I hit the GenerateDataKey
endpoint (https://docs.aws.amazon.com/kms/latest/APIReference/API_GenerateDataKey.html). It contains both the plaintext and encrypted version of the new data key I want to encrypt with.
Then I have my local encrypt function which takes that hash and returns a new hash, including both the encrypted ciphertext and encrypted data key. I'd store both of these for later use.
How can I ensure the plaintext key has been scrubbed from memory?
(defn kms-generated-key
[]
; ... leaving out AWS API request code, but it gives this...
{:CiphertextBlob "...", ; java.io.BufferedInputStream - this is an encrypted version of the new data key
:Plaintext "...", ; java.io.BufferedInputStream - this is the plaintext version of the new data key
:KeyId "arn:aws:..."})
(defn encrypt
[secret-text]
(let [{data-key :Plaintext
encrypted-data-key :CiphertextBlob} kms-generated-key]
{:ciphertext (encrypt-locally-using-data-key data-key secret-text)
:encrypted-data-key encrypted-data-key}))
edit: I only need to do this in clojure running on the JVM, I'm not looking for anything generic across clojurescript / the CLR runtime.