T09 · Insecure Skill Coding Practices
- Location
armorclaw/crypto.py:83- Finding
Non-invertible cryptographic fallback can irreversibly corrupt vault secrets
- Content
View full analysis
bytes: pad_len = 16 - (len(data) % 16) return data + bytes([pad_len] * pad_len) def _unpad(data: bytes) -> bytes: pad_len = data[-1] return data[:-pad_len] def _xor(a: bytes, b: bytes) -> bytes: return bytes(x ^ y for x, y in zip(a, b)) def _aes_block(key: bytes, block: bytes) -> bytes: """Single AES block using hashlib as a PRF (simplified, for fallback only).""" return hashlib.sha256(key + block).digest()[:16] def _aes_encrypt_stdlib(data: bytes, key: bytes, iv: bytes) -> bytes: padded = _pad(data) blocks = [padded[i:i+16] for i in range(0, len(padded), 16)] ct, prev = [], iv for block in blocks: xored = _xor(block, prev) encrypted = _aes_block(key, xored) ct.append(encrypted) prev = encrypted return b"".join(ct) def _aes_decrypt_stdlib(ct: bytes, key: bytes, iv: bytes) -> bytes: blocks = [ct[i:i+16] for i in range(0, len(ct), 16)] pt, prev = [], iv for block in blocks: decrypted = _aes_block(key, block) pt.append(_xor(decrypted, prev)) prev = block return _unpad(b"".join(pt)) ``` ### Technical Analysis The fallback is described as AES-CBC, but `_aes_block()` is a truncated SHA-256 operation. A cryptographic hash is a one-way function and is not an invertible block cipher. The purported decryption operation hashes each ciphertext block again instead of applying the inverse of the encryption transformation. The `cryptography` package is only an optional dependency. Therefore, a normal i ...[truncated 1296 chars]- Remediation
View remediation
=42.0` a mandatory runtime dependency rather than an optional extra. - Remove `_aes_encrypt_stdlib()`, `_aes_decrypt_stdlib()`, and the SHA-256 block construction. - Fail closed with a clear installation error if an approved cryptographic implementation is unavailable. - Prefer an authenticated-encryption construction such as AES-256-GCM or ChaCha20-Poly1305. - Version the ciphertext format so implementations can distinguish secure formats from any data generated by the defective fallback. - Add tests that force the missing-dependency branch and verify that initialization or writes fail safely. - Provide a detection or migration utility for vault entries that may have been generated by the fallback. ]]>
