cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

Idea: mutable types for keys and better memory handling

Open earonesty opened this issue 5 years ago • 2 comments

Python is notoriously a "bad language" for crypto because of the lack of memory management tools, but we can try.

Each can be implemented separately, but all are probably necessary if we want some decent chance of the library being safe.

  • private and symmetric key types could internally be represented as opaque openssl references, not ints or bytes ... which are automatically zeroed on close() or __del__ methods

  • all bytes key arguments to functions allow bytearray, which "finally" zeroes the mutable argument after use/construction of the internal representation (eventually we could deprecate the use of immutable types for secret key material)

  • add explicit with block handlers that call close() methods of private and symmetric keys, allowing for safe use

  • document the use of close() in all private/symmetric keys for longer-term handles

earonesty avatar Oct 31 '18 16:10 earonesty

I use tests like this to help discover potential issues:

https://gist.github.com/earonesty/e24df86bf815031479e6b761dccd100f

earonesty avatar May 14 '19 21:05 earonesty

This package makes testing for "secrets in memory leaks" easier: https://github.com/AtakamaLLC/pysecbytes

earonesty avatar Dec 19 '19 15:12 earonesty

On deeper review, we do not intend to add APIs for this. While they do provide some value, it would come at the cost of considerable complexity (new APIs, all existing APIs needing to be updated to handle "closed" states), and frankly it's incredibly difficult in Python for a user to correctly use any of these -- even simple idioms like:

with open("path/to/key") as f:
    with load_der_private_key(f.read()) as key:
        # ...
    # key is cleared here

are subtly wrong

Notwithstanding this, in practice lots of progress has been made on this goal, via our objective of using more memory safe code, that is not prone to disclosing the contents of memory on a whim. We have removed substantial attack surface since this issue was filed.

alex avatar Oct 12 '22 13:10 alex