|
| 1 | +package ocrypto |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +const testKey = "test-key-12345678901234567890123" |
| 13 | + |
| 14 | +func TestNewAESProtectedKey(t *testing.T) { |
| 15 | + key := make([]byte, 32) |
| 16 | + _, err := rand.Read(key) |
| 17 | + require.NoError(t, err) |
| 18 | + |
| 19 | + protectedKey, err := NewAESProtectedKey(key) |
| 20 | + require.NoError(t, err) |
| 21 | + assert.NotNil(t, protectedKey) |
| 22 | + assert.Equal(t, key, protectedKey.rawKey) |
| 23 | +} |
| 24 | + |
| 25 | +func TestAESProtectedKey_DecryptAESGCM(t *testing.T) { |
| 26 | + // Generate a random 256-bit key |
| 27 | + key := make([]byte, 32) |
| 28 | + _, err := rand.Read(key) |
| 29 | + require.NoError(t, err) |
| 30 | + |
| 31 | + protectedKey, err := NewAESProtectedKey(key) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + // Test data |
| 35 | + plaintext := []byte("Hello, World!") |
| 36 | + |
| 37 | + // Encrypt the data first using the same key |
| 38 | + aesGcm, err := NewAESGcm(key) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + encrypted, err := aesGcm.Encrypt(plaintext) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + // Extract IV and ciphertext (first 12 bytes are IV for GCM standard nonce size) |
| 45 | + iv := encrypted[:GcmStandardNonceSize] |
| 46 | + ciphertext := encrypted[GcmStandardNonceSize:] |
| 47 | + |
| 48 | + // Test decryption |
| 49 | + decrypted, err := protectedKey.DecryptAESGCM(iv, ciphertext, 16) // 16 is standard GCM tag size |
| 50 | + require.NoError(t, err) |
| 51 | + assert.Equal(t, plaintext, decrypted) |
| 52 | +} |
| 53 | + |
| 54 | +func TestAESProtectedKey_DecryptAESGCM_InvalidKey(t *testing.T) { |
| 55 | + // Empty key should fail |
| 56 | + _, err := NewAESProtectedKey([]byte{}) |
| 57 | + require.Error(t, err) |
| 58 | + assert.ErrorIs(t, err, ErrEmptyKeyData) |
| 59 | +} |
| 60 | + |
| 61 | +func TestAESProtectedKey_Export_NoEncapsulator(t *testing.T) { |
| 62 | + key := []byte(testKey) // 32 bytes |
| 63 | + protectedKey, err := NewAESProtectedKey(key) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + exported, err := protectedKey.Export(nil) |
| 67 | + require.Error(t, err) |
| 68 | + require.ErrorContains(t, err, "encapsulator cannot be nil") |
| 69 | + assert.Nil(t, exported) |
| 70 | +} |
| 71 | + |
| 72 | +func TestAESProtectedKey_Export_WithEncapsulator(t *testing.T) { |
| 73 | + key := []byte(testKey) // 32 bytes |
| 74 | + protectedKey, err := NewAESProtectedKey(key) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + // Mock encapsulator |
| 78 | + mockEncapsulator := &mockEncapsulator{ |
| 79 | + encryptFunc: func(data []byte) ([]byte, error) { |
| 80 | + // Simple XOR encryption for testing |
| 81 | + result := make([]byte, len(data)) |
| 82 | + for i, b := range data { |
| 83 | + result[i] = b ^ 0xFF |
| 84 | + } |
| 85 | + return result, nil |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + exported, err := protectedKey.Export(mockEncapsulator) |
| 90 | + require.NoError(t, err) |
| 91 | + |
| 92 | + // Verify it was encrypted (should be different from original) |
| 93 | + assert.NotEqual(t, key, exported) |
| 94 | + assert.Len(t, exported, len(key)) |
| 95 | + |
| 96 | + // Verify we can decrypt it back |
| 97 | + for i, b := range exported { |
| 98 | + assert.Equal(t, key[i], b^0xFF) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestAESProtectedKey_Export_EncapsulatorError(t *testing.T) { |
| 103 | + key := []byte(testKey) // 32 bytes |
| 104 | + protectedKey, err := NewAESProtectedKey(key) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + mockEncapsulator := &mockEncapsulator{ |
| 108 | + encryptFunc: func(_ []byte) ([]byte, error) { |
| 109 | + return nil, assert.AnError |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + _, err = protectedKey.Export(mockEncapsulator) |
| 114 | + require.Error(t, err) |
| 115 | + assert.Contains(t, err.Error(), "failed to encrypt key data for export") |
| 116 | +} |
| 117 | + |
| 118 | +func TestAESProtectedKey_VerifyBinding(t *testing.T) { |
| 119 | + key := []byte(testKey) // 32 bytes |
| 120 | + protectedKey, err := NewAESProtectedKey(key) |
| 121 | + require.NoError(t, err) |
| 122 | + |
| 123 | + policy := []byte("test-policy-data") |
| 124 | + ctx := context.Background() |
| 125 | + |
| 126 | + // Generate the expected HMAC |
| 127 | + expectedHMAC := protectedKey.generateHMACDigest(policy) |
| 128 | + |
| 129 | + // Verify binding should succeed with correct HMAC |
| 130 | + err = protectedKey.VerifyBinding(ctx, policy, expectedHMAC) |
| 131 | + assert.NoError(t, err) |
| 132 | +} |
| 133 | + |
| 134 | +func TestAESProtectedKey_VerifyBinding_Mismatch(t *testing.T) { |
| 135 | + key := []byte(testKey) // 32 bytes |
| 136 | + protectedKey, err := NewAESProtectedKey(key) |
| 137 | + require.NoError(t, err) |
| 138 | + |
| 139 | + policy := []byte("test-policy-data") |
| 140 | + wrongBinding := []byte("wrong-binding-data") |
| 141 | + ctx := context.Background() |
| 142 | + |
| 143 | + err = protectedKey.VerifyBinding(ctx, policy, wrongBinding) |
| 144 | + require.Error(t, err) |
| 145 | + assert.Equal(t, ErrPolicyHMACMismatch, err) |
| 146 | +} |
| 147 | + |
| 148 | +func TestAESProtectedKey_VerifyBinding_DifferentPolicyData(t *testing.T) { |
| 149 | + key := []byte(testKey) // 32 bytes |
| 150 | + protectedKey, err := NewAESProtectedKey(key) |
| 151 | + require.NoError(t, err) |
| 152 | + |
| 153 | + ctx := context.Background() |
| 154 | + |
| 155 | + // Generate HMAC for first policy |
| 156 | + policy1 := []byte("policy-data-1") |
| 157 | + hmac1 := protectedKey.generateHMACDigest(policy1) |
| 158 | + |
| 159 | + // Try to verify with different policy data |
| 160 | + policy2 := []byte("policy-data-2") |
| 161 | + err = protectedKey.VerifyBinding(ctx, policy2, hmac1) |
| 162 | + require.Error(t, err) |
| 163 | + assert.Equal(t, ErrPolicyHMACMismatch, err) |
| 164 | +} |
| 165 | + |
| 166 | +func TestAESProtectedKey_InterfaceCompliance(t *testing.T) { |
| 167 | + key := make([]byte, 32) |
| 168 | + protectedKey, err := NewAESProtectedKey(key) |
| 169 | + require.NoError(t, err) |
| 170 | + |
| 171 | + // Ensure it implements the ProtectedKey interface |
| 172 | + assert.Implements(t, (*ProtectedKey)(nil), protectedKey) |
| 173 | +} |
| 174 | + |
| 175 | +// Mock encapsulator for testing |
| 176 | +type mockEncapsulator struct { |
| 177 | + encryptFunc func([]byte) ([]byte, error) |
| 178 | + publicKeyPEMFunc func() (string, error) |
| 179 | + ephemeralKeyFunc func() []byte |
| 180 | +} |
| 181 | + |
| 182 | +func (m *mockEncapsulator) Encapsulate(_ ProtectedKey) ([]byte, error) { |
| 183 | + return nil, nil |
| 184 | +} |
| 185 | + |
| 186 | +func (m *mockEncapsulator) Encrypt(data []byte) ([]byte, error) { |
| 187 | + if m.encryptFunc != nil { |
| 188 | + return m.encryptFunc(data) |
| 189 | + } |
| 190 | + return data, nil |
| 191 | +} |
| 192 | + |
| 193 | +func (m *mockEncapsulator) PublicKeyAsPEM() (string, error) { |
| 194 | + if m.publicKeyPEMFunc != nil { |
| 195 | + return m.publicKeyPEMFunc() |
| 196 | + } |
| 197 | + return "", nil |
| 198 | +} |
| 199 | + |
| 200 | +func (m *mockEncapsulator) EphemeralKey() []byte { |
| 201 | + if m.ephemeralKeyFunc != nil { |
| 202 | + return m.ephemeralKeyFunc() |
| 203 | + } |
| 204 | + return nil |
| 205 | +} |
0 commit comments