2026-02-16 15:25:54 +00:00
|
|
|
package crypt
|
|
|
|
|
|
|
|
|
|
import (
|
test(phase0): expand test coverage, security audit, and benchmarks
Add 29 new tests across auth/, crypt/, and trust/ packages:
- auth: concurrent sessions, token uniqueness, challenge expiry boundary,
empty password, long/unicode usernames, air-gapped round-trip, expired refresh
- crypt: wrong passphrase, empty/large plaintext, KDF determinism, HKDF info
separation, checksum edge cases
- trust: concurrent registry operations, tier validation, token expiry boundary,
empty ScopedRepos behaviour, unknown capabilities
Add benchmark suites:
- crypt: Argon2, ChaCha20, AES-GCM, HMAC (1KB/1MB payloads)
- trust: PolicyEvaluate (100 agents), RegistryGet, RegistryRegister
Security audit documented in FINDINGS.md:
- F1: LTHN hash used for password verification (medium)
- F2: PGP private keys not zeroed after use (low, upstream limitation)
- F3: Empty ScopedRepos bypasses repo scope check (medium)
- F4: go vet clean, no math/rand, no secrets in error messages
All tests pass with -race. go vet clean.
Co-Authored-By: Virgil <virgil@lethean.io>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 01:14:41 +00:00
|
|
|
"bytes"
|
2026-02-16 15:25:54 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
test(phase0): expand test coverage, security audit, and benchmarks
Add 29 new tests across auth/, crypt/, and trust/ packages:
- auth: concurrent sessions, token uniqueness, challenge expiry boundary,
empty password, long/unicode usernames, air-gapped round-trip, expired refresh
- crypt: wrong passphrase, empty/large plaintext, KDF determinism, HKDF info
separation, checksum edge cases
- trust: concurrent registry operations, tier validation, token expiry boundary,
empty ScopedRepos behaviour, unknown capabilities
Add benchmark suites:
- crypt: Argon2, ChaCha20, AES-GCM, HMAC (1KB/1MB payloads)
- trust: PolicyEvaluate (100 agents), RegistryGet, RegistryRegister
Security audit documented in FINDINGS.md:
- F1: LTHN hash used for password verification (medium)
- F2: PGP private keys not zeroed after use (low, upstream limitation)
- F3: Empty ScopedRepos bypasses repo scope check (medium)
- F4: go vet clean, no math/rand, no secrets in error messages
All tests pass with -race. go vet clean.
Co-Authored-By: Virgil <virgil@lethean.io>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 01:14:41 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2026-02-16 15:25:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestEncryptDecrypt_Good(t *testing.T) {
|
|
|
|
|
plaintext := []byte("hello, world!")
|
|
|
|
|
passphrase := []byte("correct-horse-battery-staple")
|
|
|
|
|
|
|
|
|
|
encrypted, err := Encrypt(plaintext, passphrase)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.NotEqual(t, plaintext, encrypted)
|
|
|
|
|
|
|
|
|
|
decrypted, err := Decrypt(encrypted, passphrase)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, plaintext, decrypted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEncryptDecrypt_Bad(t *testing.T) {
|
|
|
|
|
plaintext := []byte("secret data")
|
|
|
|
|
passphrase := []byte("correct-passphrase")
|
|
|
|
|
wrongPassphrase := []byte("wrong-passphrase")
|
|
|
|
|
|
|
|
|
|
encrypted, err := Encrypt(plaintext, passphrase)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
_, err = Decrypt(encrypted, wrongPassphrase)
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEncryptDecryptAES_Good(t *testing.T) {
|
|
|
|
|
plaintext := []byte("hello, AES world!")
|
|
|
|
|
passphrase := []byte("my-secure-passphrase")
|
|
|
|
|
|
|
|
|
|
encrypted, err := EncryptAES(plaintext, passphrase)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.NotEqual(t, plaintext, encrypted)
|
|
|
|
|
|
|
|
|
|
decrypted, err := DecryptAES(encrypted, passphrase)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, plaintext, decrypted)
|
|
|
|
|
}
|
test(phase0): expand test coverage, security audit, and benchmarks
Add 29 new tests across auth/, crypt/, and trust/ packages:
- auth: concurrent sessions, token uniqueness, challenge expiry boundary,
empty password, long/unicode usernames, air-gapped round-trip, expired refresh
- crypt: wrong passphrase, empty/large plaintext, KDF determinism, HKDF info
separation, checksum edge cases
- trust: concurrent registry operations, tier validation, token expiry boundary,
empty ScopedRepos behaviour, unknown capabilities
Add benchmark suites:
- crypt: Argon2, ChaCha20, AES-GCM, HMAC (1KB/1MB payloads)
- trust: PolicyEvaluate (100 agents), RegistryGet, RegistryRegister
Security audit documented in FINDINGS.md:
- F1: LTHN hash used for password verification (medium)
- F2: PGP private keys not zeroed after use (low, upstream limitation)
- F3: Empty ScopedRepos bypasses repo scope check (medium)
- F4: go vet clean, no math/rand, no secrets in error messages
All tests pass with -race. go vet clean.
Co-Authored-By: Virgil <virgil@lethean.io>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 01:14:41 +00:00
|
|
|
|
|
|
|
|
// --- Phase 0 Additions ---
|
|
|
|
|
|
|
|
|
|
// TestWrongPassphraseDecrypt_Bad verifies wrong passphrase returns error, not corrupt data.
|
|
|
|
|
func TestWrongPassphraseDecrypt_Bad(t *testing.T) {
|
|
|
|
|
plaintext := []byte("sensitive payload")
|
|
|
|
|
passphrase := []byte("correct-passphrase")
|
|
|
|
|
wrongPassphrase := []byte("wrong-passphrase")
|
|
|
|
|
|
|
|
|
|
encrypted, err := Encrypt(plaintext, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
decrypted, err := Decrypt(encrypted, wrongPassphrase)
|
|
|
|
|
assert.Error(t, err, "wrong passphrase must return an error")
|
|
|
|
|
assert.Nil(t, decrypted, "wrong passphrase must not return partial data")
|
|
|
|
|
|
|
|
|
|
// Same for AES variant
|
|
|
|
|
encryptedAES, err := EncryptAES(plaintext, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
decryptedAES, err := DecryptAES(encryptedAES, wrongPassphrase)
|
|
|
|
|
assert.Error(t, err, "wrong passphrase must return an error (AES)")
|
|
|
|
|
assert.Nil(t, decryptedAES, "wrong passphrase must not return partial data (AES)")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestEmptyPlaintextRoundTrip_Good verifies encrypt/decrypt of empty plaintext.
|
|
|
|
|
func TestEmptyPlaintextRoundTrip_Good(t *testing.T) {
|
|
|
|
|
passphrase := []byte("test-passphrase")
|
|
|
|
|
|
|
|
|
|
// ChaCha20
|
|
|
|
|
encrypted, err := Encrypt([]byte{}, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, encrypted, "ciphertext should include salt + nonce even for empty plaintext")
|
|
|
|
|
|
|
|
|
|
decrypted, err := Decrypt(encrypted, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Empty(t, decrypted, "decrypted empty plaintext should be empty")
|
|
|
|
|
|
|
|
|
|
// AES-GCM
|
|
|
|
|
encryptedAES, err := EncryptAES([]byte{}, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotEmpty(t, encryptedAES)
|
|
|
|
|
|
|
|
|
|
decryptedAES, err := DecryptAES(encryptedAES, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Empty(t, decryptedAES)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestLargePlaintextRoundTrip_Good verifies encrypt/decrypt of a 1MB payload.
|
|
|
|
|
func TestLargePlaintextRoundTrip_Good(t *testing.T) {
|
|
|
|
|
passphrase := []byte("large-payload-passphrase")
|
|
|
|
|
plaintext := bytes.Repeat([]byte("X"), 1024*1024) // 1MB
|
|
|
|
|
|
|
|
|
|
// ChaCha20
|
|
|
|
|
encrypted, err := Encrypt(plaintext, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Greater(t, len(encrypted), len(plaintext), "ciphertext should be larger than plaintext")
|
|
|
|
|
|
|
|
|
|
decrypted, err := Decrypt(encrypted, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, plaintext, decrypted)
|
|
|
|
|
|
|
|
|
|
// AES-GCM
|
|
|
|
|
encryptedAES, err := EncryptAES(plaintext, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
decryptedAES, err := DecryptAES(encryptedAES, passphrase)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, plaintext, decryptedAES)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestDecryptCiphertextTooShort_Ugly verifies short ciphertext is rejected.
|
|
|
|
|
func TestDecryptCiphertextTooShort_Ugly(t *testing.T) {
|
|
|
|
|
_, err := Decrypt([]byte("short"), []byte("pass"))
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
assert.Contains(t, err.Error(), "too short")
|
|
|
|
|
|
|
|
|
|
_, err = DecryptAES([]byte("short"), []byte("pass"))
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
assert.Contains(t, err.Error(), "too short")
|
|
|
|
|
}
|