skills/self-managed-cryptography-pattern/SKILL.md
Security pattern for systems that manage cryptographic keys themselves rather than delegating to an external service. Use when the application must store, retrieve, and manage cryptographic keys directly. Implementation of Cryptographic Key Management pattern. Covers key storage security, key derivation from passwords, limiting key exposure, and protecting key confidentiality and integrity throughout the lifecycle.
npx skillsauth add igbuend/grimbard self-managed-cryptography-patternInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
In this pattern, the system itself manages the cryptographic keys, including storage and retrieval when necessary. The system is responsible for ensuring the confidentiality and integrity of cryptographic keys throughout their lifetime.
When using self-managed cryptography, the system must handle:
| Role | Type | Responsibility | |------|------|----------------| | Application | Entity | Interacts with cryptographic library and manages keys | | Cryptographer | Cryptographic Primitive | Library performing cryptographic operations | | Key Storage | Storage | Persistently stores cryptographic key material |
Note: The Application inherits the Entity role from the parent Cryptographic Key Management pattern.
Application → [generate_key(keyConf)] → Cryptographer
Cryptographer → [key] → Application
Application → [store_key(id, key)] → Key Storage
The Application requests key generation from the Cryptographer, then stores the returned key in Key Storage with a chosen identifier.
Application → [get_key(id)] → Key Storage
Key Storage → [key] → Application
Application → [crypto_action(input, key, config)] → Cryptographer
Cryptographer → [output] → Application
To use a stored key, the Application retrieves it from Key Storage, then provides it to the Cryptographer along with the input data.
| Aspect | Self-Managed Cryptography | Cryptography as a Service | |--------|---------------------------|---------------------------| | Key possession | Application holds actual key material | System holds only key identifiers | | Key storage | Managed by application | Managed by external service | | Key exposure risk | Higher (keys in application memory) | Lower (keys never exposed) | | Control | Full control over keys | Dependent on service provider | | Responsibility | Full responsibility for key security | Shared with service provider |
Never implement custom key generation algorithms.
Generating good (random and unpredictable) cryptographic keys is complex. Always use the appropriate functions offered by the cryptographic library.
When deriving keys from user-provided passwords:
Recommended KDFs: | Function | Notes | |----------|-------| | Argon2 | Modern, recommended for new applications | | scrypt | Memory-hard, good alternative | | PBKDF2 | Use if FIPS-140 compliance is required |
Verify that your cryptographic library uses up-to-date key derivation functions.
The Key Storage must protect cryptographic keys throughout their lifecycle:
Platform Security Features (Preferred):
When Platform Features Unavailable:
Key Integrity Verification:
Key Recovery:
Both integrity and confidentiality of keys must be protected:
Exception: Confidentiality can be relaxed for public keys only (e.g., when verifying digital signatures).
The identifier determines which key is used for cryptographic actions.
Risk: An attacker who can tamper with the id (e.g., change it to match a known key) can negate all security guarantees.
Required Protections:
Since the Application processes actual cryptographic keys, minimize exposure:
| Practice | Description | |----------|-------------| | Minimize time in memory | Load keys only when needed, clear immediately after | | Zeroize memory | Overwrite key material before freeing memory | | Avoid logging | Never log key material | | Limit copies | Minimize the number of key copies in memory | | Secure memory | Use secure memory allocation when available |
Choose Self-Managed Cryptography when:
Choose Cryptography as a Service when:
development
Security anti-pattern for Cross-Site Scripting vulnerabilities (CWE-79). Use when generating or reviewing code that renders HTML, handles user input in web pages, uses innerHTML/document.write, or builds dynamic web content. Covers Reflected, Stored, and DOM-based XSS. AI code has 86% XSS failure rate.
development
Security anti-pattern for XPath injection vulnerabilities (CWE-643). Use when generating or reviewing code that queries XML documents, constructs XPath expressions, or handles user input in XML operations. Detects unescaped quotes and special characters in XPath queries.
development
Security anti-pattern for weak password hashing (CWE-327, CWE-759). Use when generating or reviewing code that stores or verifies user passwords. Detects use of MD5, SHA1, SHA256 without salt, or missing password hashing entirely. Recommends bcrypt, Argon2, or scrypt.
development
Security anti-pattern for weak encryption (CWE-326, CWE-327). Use when generating or reviewing code that encrypts data, handles encryption keys, or uses cryptographic modes. Detects DES, ECB mode, static IVs, and custom crypto implementations.