Provides secure-by-default encryption patterns using AES-256-GCM, Argon2id key derivation, and SQLCipher for encrypted SQLite databases. Covers key lifecycle management and secure memory handling.
What the skill costs in Claude's context, stage by stage
No description Claude Code can read, so Claude's list leaves it out: only you can start it.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
MANDATORY READING PROTOCOL: Before implementing ANY encryption, read
references/advanced-patterns.mdfor key derivation andreferences/security-examples.mdfor implementation patterns.
This skill provides secure-by-default patterns for implementing encryption in JARVIS AI Assistant, covering:
Risk Level: HIGH
Justification:
Attack Surface:
import pytest
from cryptography.exceptions import InvalidTag
class TestEncryptionTDD:
"""TDD tests for encryption implementation."""
def test_encrypt_decrypt_roundtrip(self):
"""Test that encryption followed by decryption returns original data."""
from jarvis.security.encryption import SecureEncryption
key = secrets.token_bytes(32)
encryptor = SecureEncryption(key)
plaintext = b"sensitive data for JARVIS"
ciphertext = encryptor.encrypt(plaintext)
decrypted = encryptor.decrypt(ciphertext)
assert decrypted == plaintext
assert ciphertext != plaintext # Must be encrypted
def test_tampered_ciphertext_raises_error(self):
"""Test that tampered ciphertext is rejected."""
from jarvis.security.encryption import SecureEncryption
key = secrets.token_bytes(32)
encryptor = SecureEncryption(key)
ciphertext = encryptor.encrypt(b"secret")
tampered = ciphertext[:-1] + bytes([ciphertext[-1] ^ 0xFF])
with pytest.raises(InvalidTag):
encryptor.decrypt(tampered)
def test_key_derivation_consistency(self):
"""Same password + salt = same key; different salt = different key."""
from jarvis.security.encryption import SecureKeyDerivation
password = "strong_password_123"
salt = secrets.token_bytes(16)
key1, _ = SecureKeyDerivation.derive_key(password, salt)
key2, _ = SecureKeyDerivation.derive_key(password, salt)
assert key1 == key2 and len(key1) == 32
key3, salt3 = SecureKeyDerivation.derive_key(password)
assert key1 != key3 # Different salt = different key
Implement only what's needed to pass the tests. Start with basic encryption/decryption, then add key derivation.
After tests pass, add: memory protection, error handling, AAD support, key caching.
# Run encryption tests with coverage
pytest tests/security/test_encryption.py -v --cov=jarvis.security.encryption --cov-fail-under=90
# Run security-specific tests
pytest tests/security/ -k "encryption or crypto" -v
# Check for timing vulnerabilities
pytest tests/security/test_timing.py -v
# Verify no secrets in output
pytest --log-cli-level=DEBUG 2>&1 | grep -i "key\|secret\|password" && echo "WARNING: Secrets in logs!"
| Language | Library | Version | Notes |
|---|---|---|---|
| Python | cryptography | >=42.0.0 | Uses OpenSSL 3.x backend |
| Python | argon2-cffi | >=23.1.0 | Reference Argon2 implementation |
| TypeScript | @noble/ciphers | >=0.5.0 | Audited pure-JS implementation |
| Rust | ring | >=0.17.0 | BoringSSL-backed |
| Go | crypto/cipher | stdlib | Use with golang.org/x/crypto |
Minimum Version: SQLCipher 4.5.6+ (includes SQLite 3.44.2)
# SQLCipher secure configuration
SQLCIPHER_PRAGMAS = {
'key': None, # Set via secure key injection
'cipher': 'aes-256-gcm',
'kdf_iter': 256000, # PBKDF2 iterations
'cipher_page_size': 4096,
'cipher_kdf_algorithm': 'PBKDF2_HMAC_SHA512',
'cipher_hmac_algorithm': 'HMAC_SHA512',
'cipher_plaintext_header_size': 0,
}
Bad: Deriving key on every operation (~500ms per Argon2id call)
Good - Cache with TTL:
class CachedKeyManager:
def __init__(self, cache_ttl: int = 300):
self._cache: dict[str, tuple[bytes, float]] = {}
self._ttl = cache_ttl
def get_key(self, password: str, salt: bytes) -> bytes:
cache_key = f"{hash(password)}:{salt.hex()}"
if cache_key in self._cache:
key, ts = self._cache[cache_key]
if time.time() - ts < self._ttl:
return key
key, _ = SecureKeyDerivation.derive_key(password, salt)
self._cache[cache_key] = (key, time.time())
return key
Bad: data = f.read() loads entire file into memory
Good - Stream with chunking (64KB chunks):
nonce = secrets.token_bytes(12)
encryptor = Cipher(algorithms.AES(key), modes.GCM(nonce)).encryptor()
with open(input_path, 'rb') as fin, open(output_path, 'wb') as fout:
fout.write(nonce)
while chunk := fin.read(64 * 1024):
fout.write(encryptor.update(chunk))
fout.write(encryptor.finalize() + encryptor.tag)
Bad: PyCryptodome without OpenSSL backend (10-100x slower)
Good: Use cryptography library - auto-detects AES-NI via OpenSSL 3.x backend
Bad - Individual loop with append:
results = []
for record in records:
results.append(encryptor.encrypt(record))
Good - List comprehension with single encryptor:
encryptor = SecureEncryption(key)
results = [encryptor.encrypt(record) for record in records]
# For large batches, use ProcessPoolExecutor for parallelization
Bad - Keys remain in memory:
self.key = SecureKeyDerivation.derive_key(password) # Never cleared
Good - Zero keys after use with context manager:
import ctypes
class SecureKeyHolder:
def __init__(self, password: str):
self._key, self.salt = SecureKeyDerivation.derive_key(password)
def __exit__(self, *args):
if self._key:
key_buffer = (ctypes.c_char * len(self._key)).from_buffer_copy(self._key)
ctypes.memset(key_buffer, 0, len(self._key))
self._key = None
# Usage: with SecureKeyHolder(password) as kh: encrypt(kh._key, data)
from argon2 import PasswordHasher
from argon2.low_level import hash_secret_raw, Type
import secrets
class SecureKeyDerivation:
"""Derive encryption keys from passwords using Argon2id."""
# OWASP recommended parameters for sensitive data
TIME_COST = 3 # Iterations
MEMORY_COST = 65536 # 64 MiB
PARALLELISM = 4 # Threads
HASH_LEN = 32 # 256 bits for AES-256
SALT_LEN = 16 # 128 bits minimum
@classmethod
def derive_key(cls, password: str, salt: bytes = None) -> tuple[bytes, bytes]:
"""
Derive a 256-bit key from password.
Returns:
tuple: (derived_key, salt) for storage
"""
if salt is None:
salt = secrets.token_bytes(cls.SALT_LEN)
# Validate inputs
if not password or len(password) < 12:
raise ValueError("Password must be at least 12 characters")
key = hash_secret_raw(
secret=password.encode('utf-8'),
salt=salt,
time_cost=cls.TIME_COST,
memory_cost=cls.MEMORY_COST,
parallelism=cls.PARALLELISM,
hash_len=cls.HASH_LEN,
type=Type.ID # Argon2id
)
return key, salt
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import secrets
class SecureEncryption:
"""AES-256-GCM authenticated encryption."""
NONCE_SIZE = 12 # 96 bits recommended for GCM
KEY_SIZE = 32 # 256 bits
def __init__(self, key: bytes):
if len(key) != self.KEY_SIZE:
raise ValueError(f"Key must be {self.KEY_SIZE} bytes")
self._aesgcm = AESGCM(key)
def encrypt(self, plaintext: bytes, associated_data: bytes = None) -> bytes:
"""
Encrypt with random nonce, prepended to ciphertext.
Returns:
bytes: nonce || ciphertext || tag
"""
nonce = secrets.token_bytes(self.NONCE_SIZE)
ciphertext = self._aesgcm.encrypt(nonce, plaintext, associated_data)
return nonce + ciphertext
def decrypt(self, ciphertext: bytes, associated_data: bytes = None) -> bytes:
"""
Decrypt and verify authenticity.
Raises:
InvalidTag: If authentication fails
"""
if len(ciphertext) < self.NONCE_SIZE + 16: # nonce + tag minimum
raise ValueError("Ciphertext too short")
nonce = ciphertext[:self.NONCE_SIZE]
actual_ciphertext = ciphertext[self.NONCE_SIZE:]
return self._aesgcm.decrypt(nonce, actual_ciphertext, associated_data)
import sqlcipher3
from contextlib import contextmanager
class EncryptedDatabase:
"""Encrypted SQLite database using SQLCipher."""
def __init__(self, db_path: str, key: bytes):
self._db_path = db_path
self._key = key
self._conn = None
@contextmanager
def connect(self):
"""Context manager for database connections."""
conn = sqlcipher3.connect(self._db_path)
try:
# Apply security pragmas
conn.execute(f"PRAGMA key = \"x'{self._key.hex()}'\";")
conn.execute("PRAGMA cipher = 'aes-256-gcm';")
conn.execute("PRAGMA kdf_iter = 256000;")
conn.execute("PRAGMA cipher_page_size = 4096;")
# Verify encryption is active
result = conn.execute("PRAGMA cipher_version;").fetchone()
if not result:
raise RuntimeError("SQLCipher encryption not active")
yield conn
conn.commit()
except Exception:
conn.rollback()
raise
finally:
conn.close()
def rekey(self, new_key: bytes):
"""Rotate database encryption key."""
with self.connect() as conn:
conn.execute(f"PRAGMA rekey = \"x'{new_key.hex()}'\";")
self._key = new_key
| CVE | Severity | Component | Description | Mitigation |
|---|---|---|---|---|
| CVE-2020-27207 | High | SQLCipher <4.4.1 | Use-after-free in codec pragma | Upgrade to 4.5.6+ |
| CVE-2024-0232 | Medium | SQLite <3.44.0 | Heap use-after-free in JSON | Upgrade SQLCipher 4.5.6+ |
| CVE-2023-42811 | High | aes-gcm (Rust) | Plaintext exposure on auth failure | Upgrade to 0.10.3+ |
| CVE-2024-4603 | Medium | OpenSSL | Key derivation timing attack | Upgrade OpenSSL 3.3+ |
| CVE-2023-48056 | Medium | Crypto libs | IV reuse detection failure | Use random nonces |
| OWASP 2025 | Relevance | Implementation |
|---|---|---|
| A02: Cryptographic Failures | Critical | AES-256-GCM, Argon2id, secure RNG |
| A04: Insecure Design | High | Threat modeling, key rotation |
| A05: Security Misconfiguration | High | Secure defaults, validation |
| A08: Software Integrity Failures | Medium | Authenticated encryption |
Approved Algorithms:
secrets module, /dev/urandom)Prohibited:
See Section 3 (Implementation Workflow - TDD) for comprehensive test examples including:
| Anti-Pattern | Never Do | Always Do |
|---|---|---|
| ECB Mode | modes.ECB() | AESGCM(key) |
| Hardcoded Keys | SECRET_KEY = b"..." | os_keychain.get_key() |
| Predictable Nonces | struct.pack(">Q", time()) | secrets.token_bytes(12) |
| No Auth | modes.CBC(iv) | aesgcm.encrypt(nonce, pt, aad) |
| Weak KDF | sha256(password) | Argon2id.derive_key() |
references/threat-model.mdcryptography library (not custom implementations)secrets.token_bytes(12)grep -i "key\|secret\|password")Key Objectives: AES-256-GCM with random nonces, Argon2id KDF, OS keychain integration, authenticated encryption, key rotation support.
Security Reminders: No custom crypto, use audited libraries, test auth tags, rotate keys on schedule.
References: references/advanced-patterns.md, references/security-examples.md, references/threat-model.md
Encryption done wrong is worse than no encryption - it provides false confidence.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
Python, TypeScript, Go, Rust, SQLite
encryption, cryptography, key-management, sqlcipher, aes-256-gcm, argon2id
/martinholovsky-claude-skills-generator:encryptionfrom martinholovsky-claude-skills-generator
Claude never picks this skill on its own
Its frontmatter has no description. Without a description, Claude Code leaves it out of the list Claude chooses from. Typing /martinholovsky-claude-skills-generator:encryption still runs it.
martinholovsky-claude-skills-generator@cascade-code-general-misc-2 is listed by joshuarweaver/cascade-code-general-misc-2 — review the source before installing
npx claudepluginhub joshuarweaver/cascade-code-general-misc-2 --plugin martinholovsky-claude-skills-generator65 skills in this plugin
/accessibility-wcagAccessibility & WCAG Compliance Skill```yaml/api-expertExpert API architect specializing in RESTful API design, GraphQL, gRPC, and API security. Deep expertise in OpenAPI 3.1, authentication patterns (OAuth2, JWT), rate limiting, pagination, and OWASP API Security Top 10. Use when designing scalable APIs, implementing API gateways, or securing API endpoints./applescriptExpert in AppleScript and JavaScript for Automation (JXA) for macOS system scripting. Specializes in secure script execution, application automation, and system integration. HIGH-RISK skill due to shell command execution and system-wide control capabilities.Other plugins' skills for the same job