Files
skills/public/cryptography-key-management.html
zeekayandHanzo Dev 9e10c1c10e Each llms index points at the other two
hanzoskills.com/llms.txt now opens with the install line, the API
reference index and the company index, so an agent that lands on any
one of the three can find the rest.

robots.txt claimed 537 documents while llms.txt counts 920. The number
is gone from robots.txt; llms.txt derives it.

public/ is regenerated from scripts/site.py, which it had fallen
behind: the copy-prompt menu and agent bar were in the generator and
not in the committed pages. tests/site.mjs passes against the result.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-11 00:27:59 -07:00

499 lines
33 KiB
HTML

<!doctype html>
<html lang="en"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>cryptography-key-management</title>
<meta name="color-scheme" content="dark">
<style>
/* Committed to one visual world: a dark instrument panel. No light variant —
the dot field and the hairline rules only read on black, and a naive
inversion would wash both out. Every colour is painted explicitly so the page
never borrows a host background. */
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
:root{
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
--font-zen-pixel-square:'Zen Pixel Square',monospace;
--mono:var(--font-zen-mono);
--sans:var(--font-zen-sans);
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
--r:999px; --r-soft:14px;
}
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
/* The dot field. A halftone made of one repeating radial-gradient, faded out
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
/* No overflow clip here: the dot field is inset to the box already, and a clip
would cut off the copy-prompt menu. */
.hero{position:relative;border-bottom:1px solid var(--line)}
.hero::before{content:"";position:absolute;inset:0;
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
background-size:7px 7px;
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
pointer-events:none}
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
nav{border-bottom:1px solid var(--line)}
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
justify-content:space-between;align-items:center;gap:1rem}
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--ink);text-decoration:none;
display:inline-flex;align-items:center;gap:.6rem}
.brand i{font-style:normal;color:var(--faint)}
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
text-transform:uppercase;color:var(--soft);text-decoration:none}
nav a.up:hover{color:var(--ink)}
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
h1 em{font-style:normal;color:var(--faint)}
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
line-height:1.2;font-weight:400}
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
.btn.solid:hover{background:#d8d8d4}
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
/* Copy-prompt selector. Each row copies the prompt for its own target. */
.copy{position:relative;display:inline-flex}
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
padding:.4rem;display:flex;flex-direction:column;
box-shadow:0 12px 32px rgba(0,0,0,.75)}
.menu[hidden]{display:none}
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
white-space:nowrap}
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
.mi:hover .mark{color:var(--ink)}
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
letter-spacing:.1em;text-transform:uppercase}
/* Works-with bar: one scrolling row, name and mark per agent. */
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
display:flex;align-items:center}
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
-webkit-overflow-scrolling:touch;
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
.bar::-webkit-scrollbar{display:none}
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
text-decoration:none;background:var(--ground)}
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
.agent .mark{width:.95rem;height:.95rem}
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
p{margin:0 0 1rem;max-width:70ch}
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
li{margin:.22rem 0}
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
text-decoration-color:var(--line-hi)}
a:hover{text-decoration-color:var(--ink)}
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
display:block;overflow-x:auto}
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
.curl{display:block;background:var(--panel);border:1px solid var(--line);
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
white-space:pre;line-height:1.9}
.curl b{color:var(--ink);font-weight:400}
.curl i{font-style:normal;color:var(--faint)}
/* index */
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
#q:focus{outline:none;border-color:var(--soft)}
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
text-transform:uppercase;margin:0 0 2rem}
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
padding-bottom:.45rem}
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
border-radius:var(--r-soft);text-decoration:none}
.row:hover{background:#0a0a0a}
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
color:var(--faint);font-size:.85rem}
footer a{color:var(--soft)}
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
margin:0 0 2rem;font-size:.85rem}
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
border-bottom:1px solid var(--line);padding-bottom:.4rem}
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
.fmap a:hover{text-decoration:underline}
.fmap div{color:var(--faint);line-height:1.5}
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
@media (max-width:640px){
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
.btns{gap:.5rem}
.btn{padding:.75rem 1.15rem;font-size:.68rem}
.copy,.copy .btn{width:100%;justify-content:center}
.menu{left:0;right:0;min-width:0}
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
.strip b{border-right:0;padding-right:0}
.bar{width:100%}
}
</style>
<link rel="alternate" type="text/markdown" href="/cryptography-key-management.md">
<link rel="icon" href="/favicon.svg">
<link rel="canonical" href="https://hanzoskills.com/cryptography-key-management">
</head>
<!-- Reading this with a machine? Every page here is also plain markdown.
This one: https://hanzoskills.com/cryptography-key-management.md
Index: https://hanzoskills.com/llms.txt
Corpus: https://hanzoskills.com/all.md
Append .md to any path for the markdown twin. -->
<body>
<nav><div class="in">
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
<a class="up" href="/">All skills</a>
</div></nav>
<div class="wrap">
<h1>cryptography-key-management</h1><p class="lede">Comprehensive cryptographic key lifecycle management, KMS integration, and compliance</p><h1>Cryptographic Key Management</h1>
<p><strong>Scope</strong>: Key lifecycle (generation, distribution, storage, rotation, destruction), KMS platforms (AWS KMS, GCP KMS, Azure Key Vault, external secret manager), HSM integration, compliance (FIPS 140-2, PCI-DSS, HIPAA, GDPR) <strong>Lines</strong>: ~500 <strong>Last Updated</strong>: 2025-10-27</p>
<h2>When to Use This Skill</h2>
<p>Activate this skill when:</p>
<ul><li>Implementing key generation, distribution, storage, rotation, or destruction</li><li>Integrating with Key Management Services (AWS KMS, GCP KMS, Azure Key Vault, Vault)</li><li>Designing key hierarchies (Root → KEK → DEK)</li><li>Implementing envelope encryption patterns</li><li>Meeting compliance requirements (PCI-DSS, HIPAA, FIPS 140-2, GDPR, SOC 2)</li><li>Managing HSM integration for high-security applications</li><li>Automating key rotation with zero-downtime</li><li>Auditing key usage and access control</li><li>Managing secrets (API keys, passwords, tokens)</li><li>Implementing multi-party computation or key escrow</li></ul>
<h2>Core Concepts</h2>
<h3>Key Management Lifecycle</h3>
<p><strong>Five Phases</strong>:</p>
<pre><code>1. Generation → 2. Distribution → 3. Storage → 4. Rotation → 5. Destruction
↑ │
└──────────────────────────────────────────────────────────────┘
(Cycle repeats)</code></pre>
<p><strong>Critical Principle</strong>: The security of encrypted data depends entirely on the security of the encryption keys.</p>
<h3>Key Hierarchies</h3>
<p><strong>Three-Tier Model</strong> (Recommended):</p>
<pre><code>┌─────────────────────────────────────────────┐
│ Level 1: Root Key (Master Key) │ ← Stored in HSM
│ - Rarely rotated (annually or never) │ ← Rarely used
│ - Encrypts KEKs only │ ← FIPS 140-2 Level 3+
└───────────────────┬─────────────────────────┘
│ Encrypts
▼
┌─────────────────────────────────────────────┐
│ Level 2: Key Encryption Key (KEK) │ ← Stored in KMS
│ - Rotated periodically (quarterly/annually) │ ← Encrypts DEKs
│ - One KEK per application/tenant │
└───────────────────┬─────────────────────────┘
│ Encrypts
▼
┌─────────────────────────────────────────────┐
│ Level 3: Data Encryption Key (DEK) │ ← Stored encrypted with data
│ - Rotated frequently (per file/record) │ ← Encrypts actual data
│ - One DEK per file/record/tenant │
└───────────────────┬─────────────────────────┘
│ Encrypts
▼
┌──────────────────┐
│ Encrypted Data │
└──────────────────┘</code></pre>
<p><strong>Benefits</strong>:</p>
<ul><li><strong>Efficient rotation</strong>: Rotate KEK by re-encrypting DEKs (not data)</li><li><strong>Separation of concerns</strong>: Different keys for different purposes</li><li><strong>Reduced blast radius</strong>: Compromise of one level doesn&#x27;t expose others</li></ul>
<hr>
<h2>Key Generation</h2>
<h3>Cryptographically Secure Random Number Generators (CSRNGs)</h3>
<p><strong>Platform Sources</strong>:</p>
<p>| Platform | CSRNG | Entropy Source | |----------|-------|----------------| | Linux | <code>/dev/urandom</code> | Kernel CSPRNG (hardware RNG, interrupts, disk I/O) | | macOS | <code>/dev/random</code> | Yarrow algorithm | | Windows | <code>CryptGenRandom</code> | Windows CryptoAPI | | AWS KMS | <code>GenerateDataKey</code> | FIPS 140-2 validated | | GCP KMS | <code>GenerateRandomBytes</code> | Cloud HSM |</p>
<p><strong>Python Example</strong>:</p>
<pre><code class="lang-python">import os
import secrets
# Generate 256-bit AES key
key = os.urandom(32) # 32 bytes = 256 bits
# Or use secrets module (Python 3.6+)
key = secrets.token_bytes(32)
hex_key = secrets.token_hex(32) # 64 hex characters</code></pre>
<p><strong>❌ Never use</strong>:</p>
<ul><li><code>random.random()</code> (Python) - Predictable</li><li><code>Math.random()</code> (JavaScript) - Predictable</li><li>Timestamp-based seeds</li><li>Hardcoded keys</li></ul>
<h3>Key Derivation Functions (KDFs)</h3>
<p><strong>For deriving keys from passwords</strong>:</p>
<p>| KDF | Standard | Security | Use Case | |-----|----------|----------|----------| | <strong>Argon2id</strong> | RFC 9106 | Highest (memory-hard) | Password hashing, key derivation (recommended) | | <strong>scrypt</strong> | RFC 7914 | High (memory-hard) | Key derivation from passwords | | <strong>PBKDF2</strong> | RFC 8018 | Moderate | FIPS compliance, legacy systems | | <strong>HKDF</strong> | RFC 5869 | High | Key derivation from shared secrets (not passwords) |</p>
<p><strong>Argon2id Example</strong>:</p>
<pre><code class="lang-python">from argon2 import PasswordHasher
from argon2.low_level import hash_secret_raw, Type
import secrets
password = b&quot;user-password&quot;
salt = secrets.token_bytes(16)
# Derive 256-bit key
key = hash_secret_raw(
secret=password,
salt=salt,
time_cost=3, # Iterations
memory_cost=65536, # 64 MB
parallelism=4, # 4 threads
hash_len=32, # 256-bit key
type=Type.ID # Argon2id
)</code></pre>
<hr>
<h2>Key Storage</h2>
<h3>Storage Tiers</h3>
<p>| Tier | Security | Cost | Use Case | |------|----------|------|----------| | <strong>Tier 1: HSM</strong> | Highest (FIPS 140-2 Level 3/4) | $$$$ | Root keys, CA keys | | <strong>Tier 2: Cloud KMS</strong> | High (FIPS 140-2 Level 2/3) | $$ | Application keys, KEKs | | <strong>Tier 3: Encrypted DB</strong> | Moderate | $ | Encrypted DEKs | | <strong>Tier 4: OS Keystore</strong> | Moderate | $ | Local development | | <strong>Tier 5: Plaintext</strong> | ❌ Never | N/A | N/A |</p>
<h3>Hardware Security Module (HSM)</h3>
<p><strong>FIPS 140-2 Levels</strong>:</p>
<p>| Level | Requirements | Use Case | |-------|-------------|----------| | <strong>Level 1</strong> | Software only | Basic applications | | <strong>Level 2</strong> | Tamper-evident seals, role-based auth | Enterprise | | <strong>Level 3</strong> | Tamper-resistant hardware, identity-based auth | Financial, healthcare (recommended) | | <strong>Level 4</strong> | Environmental protection (voltage, temp) | Government, military |</p>
<p><strong>HSM Vendors</strong>:</p>
<ul><li><strong>Cloud</strong>: AWS CloudHSM, Google Cloud HSM, Azure Dedicated HSM</li><li><strong>On-premise</strong>: Thales Luna, nCipher nShield, Utimaco SecurityServer</li></ul>
<h3>Cloud KMS Platforms</h3>
<p><strong>AWS KMS</strong>:</p>
<pre><code class="lang-python">import boto3
kms = boto3.client(&#x27;kms&#x27;)
# Create key
response = kms.create_key(Description=&#x27;App encryption key&#x27;)
key_id = response[&#x27;KeyMetadata&#x27;][&#x27;KeyId&#x27;]
# Enable automatic rotation
kms.enable_key_rotation(KeyId=key_id)
# Encrypt data
ciphertext = kms.encrypt(KeyId=key_id, Plaintext=b&#x27;Secret data&#x27;)
# Decrypt
plaintext = kms.decrypt(CiphertextBlob=ciphertext[&#x27;CiphertextBlob&#x27;])</code></pre>
<p><strong>external secret manager</strong>:</p>
<pre><code class="lang-bash"># Enable transit engine
vault secrets enable transit
# Create encryption key
vault write -f transit/keys/my-key
# Encrypt
vault write transit/encrypt/my-key plaintext=$(echo &quot;Secret data&quot; | base64)
# Decrypt
vault write transit/decrypt/my-key ciphertext=&quot;vault:v1:...&quot;</code></pre>
<hr>
<h2>Key Rotation</h2>
<h3>Why Rotate Keys?</h3>
<p><strong>Reasons</strong>:</p>
<ol><li>Limit blast radius of compromise</li><li>Compliance requirements (PCI-DSS, HIPAA)</li><li>Cryptographic hygiene (reduce ciphertext under single key)</li><li>Mitigate key exposure (employee departure, system compromise)</li></ol>
<p><strong>Rotation Frequency</strong>:</p>
<ul><li><strong>Root/Master keys</strong>: Annually</li><li><strong>KEKs</strong>: Quarterly</li><li><strong>DEKs</strong>: Monthly or per-dataset</li><li><strong>Session keys</strong>: Per-session (ephemeral)</li><li><strong>API keys/passwords</strong>: 90 days</li><li><strong>Emergency</strong>: Immediately upon suspected compromise</li></ul>
<h3>Rotation Strategies</h3>
<p><strong>Strategy 1: Envelope Encryption (Zero-Downtime)</strong></p>
<p>Re-encrypt KEKs with new root key, DEKs with new KEK. Data stays encrypted with DEKs (no change).</p>
<p><strong>Strategy 2: Versioned Keys (Multi-Version)</strong></p>
<p>Keep old and new keys active. Write new data with new key, read old data with old key. Background job gradually re-encrypts.</p>
<p><strong>AWS KMS Automatic Rotation</strong>:</p>
<pre><code class="lang-bash"># Enable automatic rotation (annual)
aws kms enable-key-rotation --key-id &lt;key-id&gt;
# Check rotation status
aws kms get-key-rotation-status --key-id &lt;key-id&gt;</code></pre>
<hr>
<h2>Access Control</h2>
<h3>Principle of Least Privilege</h3>
<p><strong>Best Practices</strong>:</p>
<ul><li>Separate keys by environment (dev, staging, prod)</li><li>Separate keys by application/tenant</li><li>Use IAM roles (not user credentials) for applications</li><li>Require MFA for key administrative operations</li><li>Use encryption context for additional security</li><li>Audit all key access (CloudTrail, Cloud Audit Logs)</li></ul>
<p><strong>AWS KMS IAM Policy Example</strong>:</p>
<pre><code class="lang-json">{
&quot;Version&quot;: &quot;2012-10-17&quot;,
&quot;Statement&quot;: [{
&quot;Effect&quot;: &quot;Allow&quot;,
&quot;Action&quot;: [&quot;kms:Decrypt&quot;, &quot;kms:GenerateDataKey&quot;],
&quot;Resource&quot;: &quot;arn:aws:kms:us-east-1:123456789012:key/*&quot;,
&quot;Condition&quot;: {
&quot;StringEquals&quot;: {
&quot;kms:EncryptionContext:Department&quot;: &quot;Finance&quot;
}
}
}]
}</code></pre>
<hr>
<h2>Compliance Standards</h2>
<h3>PCI-DSS Requirements</h3>
<p><strong>Requirement 3.6</strong>: Protect cryptographic keys</p>
<ul><li><strong>3.6.1</strong>: Access to keys limited (need-to-know)</li><li><strong>3.6.2</strong>: Keys stored securely</li><li><strong>3.6.3</strong>: Keys distributed securely</li><li><strong>3.6.4</strong>: Keys changed at least annually (or upon suspected compromise)</li><li><strong>3.6.5</strong>: Retirement/destruction of old keys</li><li><strong>3.6.6</strong>: Split knowledge/dual control for manual key operations</li></ul>
<h3>HIPAA Requirements</h3>
<p><strong>Security Rule § 164.312(a)(2)(iv)</strong>: Encryption and decryption (addressable)</p>
<ul><li>Use NIST-approved algorithms</li><li>Encrypt ePHI at rest and in transit</li><li>Secure key management procedures</li><li>Access controls for encryption keys</li><li>Audit logging</li></ul>
<h3>GDPR Requirements</h3>
<p><strong>Article 32</strong>: Security of processing</p>
<ul><li>Encryption of personal data (recommended, not required)</li><li>Pseudonymization and encryption as appropriate technical measures</li><li>Regular testing and evaluation of security measures</li></ul>
<h3>FIPS 140-2 Requirements</h3>
<p><strong>Approved Algorithms</strong>:</p>
<ul><li>Encryption: AES, Triple-DES</li><li>Hashing: SHA-256, SHA-384, SHA-512</li><li>Key Exchange: Diffie-Hellman, ECDH</li><li>Signatures: RSA, ECDSA</li></ul>
<p><strong>❌ Not Approved</strong>:</p>
<ul><li>ChaCha20-Poly1305 (secure but not FIPS-approved)</li><li>MD5, SHA-1 (deprecated)</li></ul>
<hr>
<h2>Best Practices</h2>
<h3>Key Management Checklist</h3>
<p>✅ <strong>Generation</strong>:</p>
<ul><li>Use CSRNG (not predictable random)</li><li>Sufficient key length (AES-256, RSA-2048+)</li><li>Generate in secure environment (HSM, KMS)</li></ul>
<p>✅ <strong>Distribution</strong>:</p>
<ul><li>Use key wrapping (envelope encryption)</li><li>Never transmit keys in plaintext</li><li>Use separate channels for key distribution (out-of-band)</li></ul>
<p>✅ <strong>Storage</strong>:</p>
<ul><li>Never store keys with data</li><li>Use HSM or KMS for master keys</li><li>Encrypt keys at rest (key hierarchy)</li><li>Separate keys by environment</li></ul>
<p>✅ <strong>Rotation</strong>:</p>
<ul><li>Automate rotation schedules</li><li>Use envelope encryption for zero-downtime</li><li>Test rotation procedures regularly</li><li>Maintain key version history</li></ul>
<p>✅ <strong>Destruction</strong>:</p>
<ul><li>Cryptographic erasure (destroy KEK)</li><li>Secure overwrite (DOD 5220.22-M)</li><li>Grace period before deletion (30-90 days)</li><li>Audit log destruction events</li></ul>
<p>✅ <strong>Access Control</strong>:</p>
<ul><li>Principle of least privilege</li><li>Role-based access control (RBAC)</li><li>Require MFA for administrative operations</li><li>Audit all key access</li></ul>
<p>✅ <strong>Monitoring</strong>:</p>
<ul><li>Enable audit logging (CloudTrail, Cloud Audit Logs)</li><li>Monitor for unusual key access patterns</li><li>Alert on failed decryption attempts</li><li>Track key age and rotation status</li></ul>
<hr>
<h2>Anti-Patterns</h2>
<p><strong>❌ Never do</strong>:</p>
<ul><li>Hardcode keys in source code</li><li>Store keys in version control (Git)</li><li>Store keys in plaintext configuration files</li><li>Use the same key for all environments</li><li>Store keys with encrypted data</li><li>Reuse nonces/IVs</li><li>Use weak algorithms (DES, 3DES, RC4)</li><li>Skip key rotation</li><li>Allow public access to keys</li><li>Test before committing code changes</li></ul>
<hr>
<h2>Level 3: Resources</h2>
<h3>Reference Materials</h3>
<p><strong>Comprehensive Documentation</strong>:</p>
<ul><li><a href="resources/REFERENCE.md"><code>resources/REFERENCE.md</code></a> - 2,000+ line technical reference covering:</li><li>Complete key lifecycle (generation, distribution, storage, rotation, destruction)</li><li>KMS platforms (AWS KMS, GCP KMS, Azure Key Vault, external secret manager)</li><li>HSM integration (FIPS 140-2 levels, PKCS#11, vendor comparison)</li><li>Compliance standards (PCI-DSS, HIPAA, GDPR, SOC 2, FIPS 140-2)</li><li>Secrets management patterns</li><li>Access control and audit logging</li><li>Multi-party computation and key escrow</li><li>Detailed implementation examples</li></ul>
<h3>Production Scripts</h3>
<p><strong>Three executable scripts</strong> (all with <code>--help</code> and <code>--json</code> support):</p>
<ol><li><strong><code>scripts/audit_keys.py</code></strong> (848 lines)</li></ol>
<ul><li>Comprehensive key audit across multiple KMS platforms</li><li>Checks key age, rotation status, compliance violations</li><li>Detects unused keys, overly permissive policies</li><li>Security score calculation</li><li>Multi-platform support (AWS KMS, GCP KMS, Azure Key Vault, external secret manager)</li></ul>
<ol><li><strong><code>scripts/rotate_master_keys.py</code></strong> (821 lines)</li></ol>
<ul><li>Automated master key rotation with zero-downtime</li><li>Re-encrypts DEKs without touching data (envelope encryption)</li><li>Rollback capability with backup</li><li>Progress tracking and resume</li><li>Multi-platform support (AWS KMS, GCP KMS, local, Vault)</li></ul>
<ol><li><strong><code>scripts/generate_key_hierarchy.py</code></strong> (698 lines)</li></ol>
<ul><li>Generate complete key hierarchies (Root → KEK → DEK)</li><li>Configurable tiers (2 or 3 levels)</li><li>Multiple algorithms (AES-256, RSA-2048, ECC-P256)</li><li>Key wrapping and export</li><li>ASCII tree visualization</li></ul>
<h3>Production Examples</h3>
<p><strong>Five production-ready examples</strong>:</p>
<ol><li><strong><code>examples/aws_kms_integration.py</code></strong></li></ol>
<ul><li>Create and manage AWS KMS keys</li><li>Envelope encryption with GenerateDataKey</li><li>Automatic key rotation</li><li>Key policies and access control</li><li>Encryption context usage</li></ul>
<ol><li><strong><code>examples/hashicorp_vault_setup.py</code></strong></li></ol>
<ul><li>Transit secrets engine configuration</li><li>Key creation, rotation, and versioning</li><li>Encryption as a service</li><li>Re-wrapping (re-encrypt with new version)</li><li>Auto-rotation configuration</li></ul>
<ol><li><strong><code>examples/key_rotation_automation.py</code></strong></li></ol>
<ul><li>Automated rotation scheduling</li><li>Zero-downtime rotation workflow</li><li>CloudWatch metrics integration</li><li>Rollback procedures</li><li>Monitoring and alerting</li></ul>
<ol><li><strong><code>examples/secrets_management.py</code></strong></li></ol>
<ul><li>Secure API key and password storage</li><li>Secret versioning and rotation</li><li>KMS-backed encryption</li><li>Secure password generation</li><li>Audit logging</li></ul>
<ol><li><strong><code>examples/compliance_config.py</code></strong></li></ol>
<ul><li>Compliance policy templates (PCI-DSS, HIPAA, GDPR, SOC 2, FIPS 140-2)</li><li>Automated compliance validation</li><li>Key lifecycle enforcement</li><li>Audit trail generation</li><li>Violation reporting</li></ul>
<h3>Usage</h3>
<p><strong>Run scripts</strong>:</p>
<pre><code class="lang-bash"># Audit all keys in AWS KMS
./scripts/audit_keys.py --platform aws-kms --region us-east-1 --json
# Rotate master key
./scripts/rotate_master_keys.py --platform aws-kms --key-id alias/master-key
# Generate 3-tier key hierarchy
./scripts/generate_key_hierarchy.py --tiers 3 --dek-count 100 --visualize</code></pre>
<p><strong>Integrate examples</strong>:</p>
<pre><code class="lang-python"># Use AWS KMS integration
from examples.aws_kms_integration import AWSKMSKeyManager
manager = AWSKMSKeyManager(region=&#x27;us-east-1&#x27;)
envelope = manager.encrypt_with_envelope(plaintext, &#x27;alias/my-key&#x27;)
decrypted = manager.decrypt_with_envelope(envelope)</code></pre>
<hr>
<h2>Quick Reference</h2>
<h3>Common Commands</h3>
<p><strong>AWS KMS</strong>:</p>
<pre><code class="lang-bash"># Create key
aws kms create-key --description &quot;App key&quot;
# Enable rotation
aws kms enable-key-rotation --key-id &lt;key-id&gt;
# Encrypt
aws kms encrypt --key-id alias/my-key --plaintext fileb://secret.txt
# Decrypt
aws kms decrypt --ciphertext-blob fileb://encrypted.bin</code></pre>
<p><strong>external secret manager</strong>:</p>
<pre><code class="lang-bash"># Enable transit
vault secrets enable transit
# Create key
vault write -f transit/keys/my-key
# Encrypt
vault write transit/encrypt/my-key plaintext=$(base64 &lt;&lt;&lt; &quot;secret&quot;)
# Rotate
vault write -f transit/keys/my-key/rotate</code></pre>
<p><strong>Google Cloud KMS</strong>:</p>
<pre><code class="lang-bash"># Create key ring
gcloud kms keyrings create my-keyring --location global
# Create key
gcloud kms keys create my-key --location global --keyring my-keyring --purpose encryption
# Encrypt
gcloud kms encrypt --location global --keyring my-keyring --key my-key --plaintext-file secret.txt --ciphertext-file encrypted.bin</code></pre>
<hr>
<h2>Related Skills</h2>
<ul><li><strong>cryptography-encryption-at-rest</strong> - Protecting stored data</li><li><strong>security-tls-configuration</strong> - Certificates and TLS key management</li><li><strong>cryptography-crypto-best-practices</strong> - Cryptographic implementation guidelines</li><li><strong>security-secrets-management</strong> - Application secrets and credentials</li></ul>
<hr>
<h2>Resources</h2>
<h3>Standards and Specifications</h3>
<ul><li><strong>NIST SP 800-57</strong>: Recommendation for Key Management</li><li><strong>NIST SP 800-175B</strong>: Guideline for Using Cryptographic Standards</li><li><strong>FIPS 140-2</strong>: Security Requirements for Cryptographic Modules</li><li><strong>RFC 5869</strong>: HKDF (HMAC-based Key Derivation Function)</li><li><strong>RFC 9106</strong>: Argon2 Memory-Hard Function</li></ul>
<h3>Compliance Resources</h3>
<ul><li><strong>PCI-DSS Requirement 3</strong>: Protect stored cardholder data</li><li><strong>HIPAA Security Rule</strong>: Encryption and Decryption</li><li><strong>GDPR Article 32</strong>: Security of processing</li><li><strong>SOC 2 Trust Services Criteria</strong>: Confidentiality</li></ul>
<h3>Tools and Libraries</h3>
<ul><li><strong>AWS KMS</strong>: https://aws.amazon.com/kms/</li><li><strong>Google Cloud KMS</strong>: https://cloud.google.com/kms</li><li><strong>Azure Key Vault</strong>: https://azure.microsoft.com/en-us/services/key-vault/</li><li><strong>external secret manager</strong>: https://www.vaultproject.io/</li><li><strong>Python cryptography</strong>: https://cryptography.io/</li></ul>
<footer>
<div class="fmap">
<div><b>Build</b>
<a href="https://hanzo.app">hanzo.app</a> app builder
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
<div><b>Run</b>
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
<div><b>Account</b>
<a href="https://hanzo.id">hanzo.id</a> sign in
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
<div><b>Company</b>
<a href="https://hanzo.ai">hanzo.ai</a> canonical
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
</div>
<div class="fend"><a href="/cryptography-key-management.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
</footer>
</div></body></html>