GIT REPOKMS · HSM–STORAGE1kek = AESGCM.generate_key(256)2dek = AESGCM.generate_key(256)3nonce = os.urandom(12)4ct = AESGCM(dek).encrypt(nonce, doc, None)5wrapped = aes_key_wrap(kek, dek)6del dek7dek = aes_key_unwrap(kek, wrapped)8print(AESGCM(dek).decrypt(nonce, ct, None))9kek2 = AESGCM.generate_key(256)10wrapped = aes_key_wrap(kek2, dek)dek–nonce–ct–wrapped–1 CREATE2 STORE3 USE4 ROTATE5 DESTROY
Back row: the repo, the KMS vault and the storageMiddle: the Python code and the app's memory; front: a key's five life stagesTokens: the data key, the wrapped data key, the encrypted file, the old keyAt the back: the Git repo, the KMS vault that holds the key-encryption key, and the storage. In the middle, Python that encrypts one small file with a data key and wraps that key with the key-encryption key, beside the app's memory. The wrapped key is stored with the ciphertext, unwrapped by the KMS when the file is read, re-wrapped under a new key when the old one's time is up, and the old key is then destroyed. In front, the five stages of a key's life. In the code, kek is a plain Python value so the example runs on its own. In a real system the KEK never leaves the KMS or HSM: the app sends the DEK to be wrapped or unwrapped, and the service does it inside.

Why keys matter

  1. Ciphers like AES are public: anyone can read how they work. Only the key is secret, so whoever holds the key can read everything. Key management means keeping keys safe for their whole life, from creation to destruction.

One file, start to finish

Rotate and destroy

What happens

;; step 1/10 · secret
cipher          public (AES is a published standard)
key             the only secret
key management  create, store, use, rotate, destroy,
                safely, for every key

In the code, kek is a plain Python value so the example runs on its own. In a real system the KEK never leaves the KMS or HSM: the app sends the DEK to be wrapped or unwrapped, and the service does it inside. del removes the name dek, but Python cannot promise the bytes are wiped from memory at once. Key management services and HSMs handle wiping (zeroization) for you; that is one more reason to let them hold keys.