ENع
→Key management
الدرس 11 · التشفير

Key management

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

Ciphers are public; the key is the secret. Follow one small file through envelope encryption in Python: a random data key encrypts it, a key-encryption key held by a key management service (KMS) wraps that data key, and the wrapped key is stored beside the data. Then read it back, rotate to a new key without re-encrypting the file, and safely destroy the old key. Along the way, see the most common mistake (keys in code or beside the data) and its fix.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 11 · تفاعلي
01 / 10

The key is the secret

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.

cipher          public (AES is a published standard)
key             the only secret
key management  create, store, use, rotate, destroy,
                safely, for every key
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. The key is the secret

    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.

    cipher          public (AES is a published standard)
    key             the only secret
    key management  create, store, use, rotate, destroy,
                    safely, for every key
  2. Where keys must not live

    A common mistake is keeping a key where code or data lives: typed into source code, or saved beside the files. One leak then exposes both. The fix: keep keys in a key management service (KMS), ideally backed by a hardware security module (HSM).

    mistake   key = "..." written in the source code
              key file saved next to the data
    why bad   whoever sees the repo or copies the disk
              gets the key and the data together
    fix       a KMS holds the key; code asks it to act
    OWASP     "never committed to source code repositories"
  3. Create: random keys

    Keys must come from a secure random generator, never from a password or a pattern. Line 1 makes the key-encryption key (KEK); line 2 makes a data-encryption key (DEK). Each is 256 bits: 32 random bytes.

    line 1   kek: 32 random bytes (a real KMS makes it inside
             and never hands it out; here it stands in)
    line 2   dek: 32 random bytes, one per file
    256 bits = 32 bytes = an AES-256 key
    never    a password, a date, a counter as a key
  4. Use the DEK on the data

    Line 3 picks a fresh 12-byte nonce, a number used once. Line 4 encrypts the file with the DEK using AES-GCM. The 9-byte file becomes 25 bytes: the scrambled data plus a 16-byte tag that detects any change.

    doc      b"pay: 4200"   9 bytes
    nonce    12 random bytes, never reused with this key
    ct       9 bytes scrambled + 16-byte tag = 25 bytes
    tag      decrypting fails if anything was changed
  5. Wrap the DEK with the KEK

    Line 5 encrypts the DEK itself with the KEK. This is called wrapping, and the pattern is envelope encryption. The 32-byte DEK becomes 40 bytes. In a real system the KMS wraps it inside, so the KEK never leaves.

    wrap      encrypt a key with another key
              (AES Key Wrap: NIST SP 800-38F, RFC 3394)
    in        dek      32 bytes
    out       wrapped  40 bytes (32 + an 8-byte check)
    envelope  data under the DEK, DEK under the KEK
  6. Store the wrapped key

    Line 6 drops the plain DEK. The ciphertext, nonce and wrapped DEK are stored together. Anyone who copies the disk gets nothing readable: to unwrap the DEK they would also need the KMS.

    line 6   del dek: the app keeps no plain copy
    stored   ct (25 B) + nonce (12 B) + wrapped (40 B)
    stolen   disk copy alone: unreadable
    needed   the KMS, which checks who is asking
  7. Use: unwrap, then decrypt

    To read the file, the app sends the wrapped DEK to the KMS (line 7). The KMS checks the app is allowed, logs the request, and returns the DEK. Line 8 decrypts and prints b'pay: 4200'.

    line 7   wrapped (40 B) → KMS → dek (32 B)
    KMS      is this app allowed?  log who and when
    line 8   decrypt → b'pay: 4200'
    least privilege: only this app may unwrap
  8. Rotate: a new KEK

    Each key has a cryptoperiod: a set time it may be used. When it ends, line 9 makes KEK v2 and line 10 re-wraps the DEK with it. Only the 40-byte wrapped key changes; the file is not re-encrypted.

    cryptoperiod  how long a key may be used (NIST SP 800-57)
    line 9        kek2: KEK v2, now the active key
    line 10       re-wrap: still 40 bytes, now under v2
    KEK v1        deactivated: wraps nothing new
    the 25-byte ciphertext is untouched
  9. Destroy the old key

    Once nothing needs KEK v1, the KMS destroys it and every copy. That cannot be undone: data wrapped only by it is unreadable for good. So re-wrap first, then destroy. NIST calls doing it on purpose cryptographic erase.

    check     nothing is still wrapped by v1 (OWASP:
              re-wrap DEKs before destroying the old KEK)
    destroy   v1 and all copies: backups, archives, memory
    result    our file: readable (wrapped by v2)
    on purpose  cryptographic erase (NIST SP 800-88)
  10. The whole life of a key

    Every key follows the same path: create, store, use, rotate, destroy. Write down who may use each key and for what, log every use, and plan for a leak: stop using the key, make a new one, re-wrap, check the logs.

      1  Create
      2  Store
      3  Use
      4  Rotate
      5  Destroy
    policy   who may use which key, for what
    logs     every wrap and unwrap
    leak     revoke → new key → re-wrap → review logs

اختبار يتاح بعد الخطوة الأخيرة.