Skip to content

fix(crypto): switch AES-CFB to AES-GCM with HKDF key derivation [C1+H3]#543

Merged
lakhansamani merged 1 commit intomainfrom
fix/c1-h3-aes-gcm-hkdf
Apr 3, 2026
Merged

fix(crypto): switch AES-CFB to AES-GCM with HKDF key derivation [C1+H3]#543
lakhansamani merged 1 commit intomainfrom
fix/c1-h3-aes-gcm-hkdf

Conversation

@lakhansamani
Copy link
Copy Markdown
Contributor

Summary

  • C1 (Critical): Replaces AES-CFB (no integrity) with AES-GCM (authenticated encryption), preventing bit-flip attacks on encrypted session tokens
  • H3 (High): Replaces null-byte key padding with HKDF-SHA256 key derivation, ensuring proper 32-byte key regardless of input length
  • Removes commented-out hardcoded encryption key (L3)

Changes

  • internal/crypto/aes.go: Complete rewrite of EncryptAES/DecryptAES using AES-256-GCM + HKDF
  • internal/crypto/aes_test.go: New test suite covering roundtrip, tamper detection, wrong key, short/long keys

Security Impact

  • Tampered ciphertexts are now detected and rejected (GCM authentication tag)
  • Key derivation uses HKDF-SHA256 instead of null-byte padding, maximizing entropy
  • Function signatures unchanged — drop-in replacement

Test plan

  • All 7 new AES tests pass
  • Verify session token encrypt/decrypt works end-to-end
  • Verify existing sessions will need re-login (expected — new encryption format)

AES-CFB provides no integrity/authentication, allowing attackers to
bit-flip encrypted session tokens to alter user ID or roles without
detection. AES-GCM provides authenticated encryption.

Also replaces null-byte key padding with HKDF-SHA256 key derivation,
ensuring full entropy utilization regardless of input key length.

Fixes: C1 (Critical), H3 (High)
@lakhansamani lakhansamani merged commit 2bab71e into main Apr 3, 2026
@lakhansamani lakhansamani deleted the fix/c1-h3-aes-gcm-hkdf branch April 3, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant