| 1 |
package crypto_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/aes" |
| 6 |
stdcipher "crypto/cipher" |
| 7 |
"encoding/base64" |
| 8 |
"io" |
| 9 |
"strconv" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/crypto" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
aesGCMNonceSize = 12 |
| 20 |
aesGCMTagSize = 16 |
| 21 |
aesGCMOverhead = aesGCMNonceSize + aesGCMTagSize |
| 22 |
) |
| 23 |
|
| 24 |
func newCipher(tb testing.TB, key string) *crypto.Cipher { |
| 25 |
tb.Helper() |
| 26 |
|
| 27 |
cipher, err := crypto.New(key) |
| 28 |
require.NoError(tb, err) |
| 29 |
|
| 30 |
return cipher |
| 31 |
} |
| 32 |
|
| 33 |
func TestDeriveKey(t *testing.T) { |
| 34 |
t.Parallel() |
| 35 |
|
| 36 |
tests := []struct { |
| 37 |
name string |
| 38 |
key string |
| 39 |
}{ |
| 40 |
{ |
| 41 |
name: "simple key", |
| 42 |
key: "mykey", |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "empty key", |
| 46 |
key: "", |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "long key", |
| 50 |
key: "this is a very long key that should still produce a 32-byte derived key", |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "special characters", |
| 54 |
key: "key!@#$%^&*()_+-=[]{}|;:,.<>?", |
| 55 |
}, |
| 56 |
} |
| 57 |
|
| 58 |
for _, tc := range tests { |
| 59 |
t.Run(tc.name, func(t *testing.T) { |
| 60 |
t.Parallel() |
| 61 |
|
| 62 |
derivedKey, err := crypto.DeriveKey(tc.key) |
| 63 |
require.NoError(t, err) |
| 64 |
|
| 65 |
// Check that the result is a 32-byte array |
| 66 |
require.Len(t, derivedKey[:], 32, "expected key length 32") |
| 67 |
|
| 68 |
// Check that the same key produces the same result |
| 69 |
derivedKey2, err := crypto.DeriveKey(tc.key) |
| 70 |
require.NoError(t, err) |
| 71 |
require.True(t, bytes.Equal(derivedKey[:], derivedKey2[:]), "DeriveKey is not deterministic") |
| 72 |
}) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestDeriveKeyDifferentInputs(t *testing.T) { |
| 77 |
t.Parallel() |
| 78 |
|
| 79 |
key1, err := crypto.DeriveKey("key1") |
| 80 |
require.NoError(t, err) |
| 81 |
key2, err := crypto.DeriveKey("key2") |
| 82 |
require.NoError(t, err) |
| 83 |
|
| 84 |
// Different inputs should produce different keys |
| 85 |
require.False(t, bytes.Equal(key1[:], key2[:]), "different keys should produce different derived keys") |
| 86 |
} |
| 87 |
|
| 88 |
func TestNewCipher(t *testing.T) { |
| 89 |
t.Parallel() |
| 90 |
|
| 91 |
encryptionKey := "test-key" |
| 92 |
cipher, err := crypto.New(encryptionKey) |
| 93 |
require.NoError(t, err) |
| 94 |
|
| 95 |
require.NotNil(t, cipher, "expected cipher to be non-nil") |
| 96 |
|
| 97 |
plainText := []byte("ping") |
| 98 |
encrypted, err := cipher.EncryptBytes(plainText) |
| 99 |
require.NoError(t, err, "EncryptBytes failed") |
| 100 |
|
| 101 |
decrypted, err := cipher.DecryptBytes(encrypted) |
| 102 |
require.NoError(t, err, "DecryptBytes failed") |
| 103 |
require.Equal(t, plainText, decrypted, "round trip failed") |
| 104 |
} |
| 105 |
|
| 106 |
func TestEncryptBytesBasic(t *testing.T) { |
| 107 |
t.Parallel() |
| 108 |
|
| 109 |
cipher := newCipher(t, "test-key") |
| 110 |
plainText := []byte("hello world") |
| 111 |
|
| 112 |
encrypted, err := cipher.EncryptBytes(plainText) |
| 113 |
require.NoError(t, err, "EncryptBytes failed") |
| 114 |
|
| 115 |
expectedSize := len(plainText) + aesGCMOverhead |
| 116 |
require.Len(t, encrypted, expectedSize) |
| 117 |
|
| 118 |
key, err := crypto.DeriveKey("test-key") |
| 119 |
require.NoError(t, err) |
| 120 |
block, err := aes.NewCipher(key[:]) |
| 121 |
require.NoError(t, err) |
| 122 |
aead, err := stdcipher.NewGCMWithRandomNonce(block) |
| 123 |
require.NoError(t, err) |
| 124 |
|
| 125 |
decrypted, err := aead.Open(nil, nil, encrypted, nil) |
| 126 |
require.NoError(t, err) |
| 127 |
require.Equal(t, plainText, decrypted) |
| 128 |
} |
| 129 |
|
| 130 |
func TestEncryptBytesEmpty(t *testing.T) { |
| 131 |
t.Parallel() |
| 132 |
|
| 133 |
cipher := newCipher(t, "test-key") |
| 134 |
plainText := []byte("") |
| 135 |
|
| 136 |
encrypted, err := cipher.EncryptBytes(plainText) |
| 137 |
require.NoError(t, err, "EncryptBytes failed") |
| 138 |
|
| 139 |
require.Len(t, encrypted, aesGCMOverhead) |
| 140 |
} |
| 141 |
|
| 142 |
func TestEncryptBytesRandomNonce(t *testing.T) { |
| 143 |
t.Parallel() |
| 144 |
|
| 145 |
cipher := newCipher(t, "test-key") |
| 146 |
plainText := []byte("same plaintext") |
| 147 |
|
| 148 |
encrypted1, err1 := cipher.EncryptBytes(plainText) |
| 149 |
require.NoError(t, err1, "first EncryptBytes failed") |
| 150 |
|
| 151 |
encrypted2, err2 := cipher.EncryptBytes(plainText) |
| 152 |
require.NoError(t, err2, "second EncryptBytes failed") |
| 153 |
|
| 154 |
// Same plaintext encrypted twice should produce different ciphertexts (due to random nonce) |
| 155 |
require.NotEqual(t, encrypted1, encrypted2, "same plaintext should produce different ciphertexts due to random nonce") |
| 156 |
} |
| 157 |
|
| 158 |
func TestDecryptBytesBasic(t *testing.T) { |
| 159 |
t.Parallel() |
| 160 |
|
| 161 |
cipher := newCipher(t, "test-key") |
| 162 |
plainText := []byte("hello world") |
| 163 |
|
| 164 |
encrypted, err := cipher.EncryptBytes(plainText) |
| 165 |
require.NoError(t, err, "EncryptBytes failed") |
| 166 |
|
| 167 |
encryptedCopy := bytes.Clone(encrypted) |
| 168 |
|
| 169 |
decrypted, err := cipher.DecryptBytes(encrypted) |
| 170 |
require.NoError(t, err, "DecryptBytes failed") |
| 171 |
require.Equal(t, plainText, decrypted, "decrypted text does not match original") |
| 172 |
require.Equal(t, encryptedCopy, encrypted, "DecryptBytes must not modify ciphertext") |
| 173 |
} |
| 174 |
|
| 175 |
func TestDecryptBytesTampered(t *testing.T) { |
| 176 |
t.Parallel() |
| 177 |
|
| 178 |
cipher := newCipher(t, "test-key") |
| 179 |
plainText := []byte("hello world") |
| 180 |
|
| 181 |
encrypted, err := cipher.EncryptBytes(plainText) |
| 182 |
require.NoError(t, err, "EncryptBytes failed") |
| 183 |
|
| 184 |
encrypted[len(encrypted)/2] ^= 0xFF |
| 185 |
|
| 186 |
_, err = cipher.DecryptBytes(encrypted) |
| 187 |
require.ErrorIs(t, err, crypto.ErrAuthenticationFailed) |
| 188 |
} |
| 189 |
|
| 190 |
func TestDecryptBytesWrongKey(t *testing.T) { |
| 191 |
t.Parallel() |
| 192 |
|
| 193 |
cipher1 := newCipher(t, "key1") |
| 194 |
cipher2 := newCipher(t, "key2") |
| 195 |
|
| 196 |
plainText := []byte("secret message") |
| 197 |
|
| 198 |
encrypted, err := cipher1.EncryptBytes(plainText) |
| 199 |
require.NoError(t, err, "EncryptBytes failed") |
| 200 |
|
| 201 |
// Try to decrypt with different key |
| 202 |
_, err = cipher2.DecryptBytes(encrypted) |
| 203 |
require.ErrorIs(t, err, crypto.ErrAuthenticationFailed) |
| 204 |
} |
| 205 |
|
| 206 |
func TestDecryptBytesShortData(t *testing.T) { |
| 207 |
t.Parallel() |
| 208 |
|
| 209 |
cipher := newCipher(t, "test-key") |
| 210 |
|
| 211 |
tests := []struct { |
| 212 |
name string |
| 213 |
data []byte |
| 214 |
}{ |
| 215 |
{name: "empty", data: []byte("")}, |
| 216 |
{name: "too short", data: []byte("short")}, |
| 217 |
{name: "just nonce size", data: make([]byte, aesGCMNonceSize)}, |
| 218 |
{name: "nonce + 1 byte", data: make([]byte, aesGCMNonceSize+1)}, |
| 219 |
{ |
| 220 |
name: "nonce + tag - 1 byte", |
| 221 |
data: make([]byte, aesGCMOverhead-1), |
| 222 |
}, |
| 223 |
} |
| 224 |
|
| 225 |
for _, tt := range tests { |
| 226 |
t.Run(tt.name, func(t *testing.T) { |
| 227 |
t.Parallel() |
| 228 |
|
| 229 |
_, err := cipher.DecryptBytes(tt.data) |
| 230 |
require.ErrorIs(t, err, crypto.ErrCipherTextBlockSize) |
| 231 |
}) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func TestRoundTrip(t *testing.T) { |
| 236 |
t.Parallel() |
| 237 |
|
| 238 |
cipher := newCipher(t, "my-secret-key") |
| 239 |
|
| 240 |
testCases := []struct { |
| 241 |
name string |
| 242 |
plainText []byte |
| 243 |
}{ |
| 244 |
{"single byte", []byte("a")}, |
| 245 |
{"small text", []byte("hello")}, |
| 246 |
{"longer text", []byte("the quick brown fox jumps over the lazy dog")}, |
| 247 |
{"with newlines", []byte("line1\nline2\nline3")}, |
| 248 |
{"binary data", []byte{0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD}}, |
| 249 |
{"large data", bytes.Repeat([]byte("test"), 1000)}, |
| 250 |
} |
| 251 |
|
| 252 |
for _, tc := range testCases { |
| 253 |
t.Run(tc.name, func(t *testing.T) { |
| 254 |
t.Parallel() |
| 255 |
|
| 256 |
encrypted, err := cipher.EncryptBytes(tc.plainText) |
| 257 |
require.NoError(t, err, "EncryptBytes failed") |
| 258 |
|
| 259 |
decrypted, err := cipher.DecryptBytes(encrypted) |
| 260 |
require.NoError(t, err, "DecryptBytes failed") |
| 261 |
require.Equal(t, tc.plainText, decrypted, "round trip failed") |
| 262 |
}) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func TestEncryptBytesWithTimeUsesRawURLBase64(t *testing.T) { |
| 267 |
t.Parallel() |
| 268 |
|
| 269 |
for _, tc := range []struct { |
| 270 |
name string |
| 271 |
plainText []byte |
| 272 |
}{ |
| 273 |
{name: "small payload", plainText: []byte("hello world")}, |
| 274 |
{name: "large payload", plainText: bytes.Repeat([]byte("a"), 512)}, |
| 275 |
} { |
| 276 |
t.Run(tc.name, func(t *testing.T) { |
| 277 |
t.Parallel() |
| 278 |
|
| 279 |
cipher := newCipher(t, "test-key") |
| 280 |
|
| 281 |
encrypted, err := cipher.EncryptBytesWithTime(tc.plainText) |
| 282 |
require.NoError(t, err) |
| 283 |
|
| 284 |
require.NotContains(t, string(encrypted), "=") |
| 285 |
|
| 286 |
decrypted, err := cipher.DecryptBytesWithTime(encrypted) |
| 287 |
require.NoError(t, err) |
| 288 |
require.Equal(t, tc.plainText, decrypted) |
| 289 |
}) |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
func TestEncryptStringWithTimeUsesRawURLBase64(t *testing.T) { |
| 294 |
t.Parallel() |
| 295 |
|
| 296 |
for _, tc := range []struct { |
| 297 |
name string |
| 298 |
plainText []byte |
| 299 |
}{ |
| 300 |
{name: "small payload", plainText: []byte("hello world")}, |
| 301 |
{name: "large payload", plainText: bytes.Repeat([]byte("a"), 512)}, |
| 302 |
} { |
| 303 |
t.Run(tc.name, func(t *testing.T) { |
| 304 |
t.Parallel() |
| 305 |
|
| 306 |
cipher := newCipher(t, "test-key") |
| 307 |
|
| 308 |
encrypted, err := cipher.EncryptStringWithTime(tc.plainText) |
| 309 |
require.NoError(t, err) |
| 310 |
|
| 311 |
require.NotContains(t, encrypted, "=") |
| 312 |
|
| 313 |
decrypted, err := cipher.DecryptStringWithTime(encrypted) |
| 314 |
require.NoError(t, err) |
| 315 |
require.Equal(t, tc.plainText, decrypted) |
| 316 |
}) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
func TestDecryptBytesWithTimeMaxAge(t *testing.T) { |
| 321 |
t.Parallel() |
| 322 |
|
| 323 |
const key = "test-key" |
| 324 |
|
| 325 |
issued := time.Now().Add(-3 * time.Minute).Unix() |
| 326 |
plainText := strconv.AppendInt(nil, issued, 10) |
| 327 |
plainText = append(plainText, " payload"...) |
| 328 |
cipher := newCipher(t, key) |
| 329 |
encrypted, err := cipher.EncryptBytes(plainText) |
| 330 |
require.NoError(t, err) |
| 331 |
|
| 332 |
encoded := make([]byte, base64.RawURLEncoding.EncodedLen(len(encrypted))) |
| 333 |
base64.RawURLEncoding.Encode(encoded, encrypted) |
| 334 |
|
| 335 |
_, err = cipher.DecryptBytesWithTime(encoded) |
| 336 |
require.ErrorContains(t, err, "expired after 2m0s") |
| 337 |
|
| 338 |
longLivedCipher, err := crypto.NewWithMaxAge(key, 4*time.Minute) |
| 339 |
require.NoError(t, err) |
| 340 |
decrypted, err := longLivedCipher.DecryptBytesWithTime(encoded) |
| 341 |
require.NoError(t, err) |
| 342 |
require.Equal(t, []byte("payload"), decrypted) |
| 343 |
} |
| 344 |
|
| 345 |
func TestDecryptStringWithTime(t *testing.T) { |
| 346 |
t.Parallel() |
| 347 |
|
| 348 |
cipher := newCipher(t, "test-key") |
| 349 |
|
| 350 |
encrypted, err := cipher.EncryptBytesWithTime([]byte("hello world")) |
| 351 |
require.NoError(t, err) |
| 352 |
|
| 353 |
decrypted, err := cipher.DecryptStringWithTime(string(encrypted)) |
| 354 |
require.NoError(t, err) |
| 355 |
require.Equal(t, []byte("hello world"), decrypted) |
| 356 |
} |
| 357 |
|
| 358 |
func TestDecryptStringWithTimeInto(t *testing.T) { |
| 359 |
t.Parallel() |
| 360 |
|
| 361 |
cipher := newCipher(t, "test-key") |
| 362 |
|
| 363 |
encrypted, err := cipher.EncryptStringWithTime([]byte("hello world")) |
| 364 |
require.NoError(t, err) |
| 365 |
|
| 366 |
var scratch [128]byte |
| 367 |
|
| 368 |
decryptedLen, err := cipher.DecryptStringWithTimeInto(scratch[:], encrypted) |
| 369 |
require.NoError(t, err) |
| 370 |
require.Equal(t, []byte("hello world"), scratch[:decryptedLen]) |
| 371 |
|
| 372 |
requiredLen, err := cipher.DecryptStringWithTimeInto(scratch[:1], encrypted) |
| 373 |
require.ErrorIs(t, err, io.ErrShortBuffer) |
| 374 |
require.Equal(t, base64.RawURLEncoding.DecodedLen(len(encrypted)), requiredLen) |
| 375 |
} |
| 376 |
|
| 377 |
func TestCipherConsistency(t *testing.T) { |
| 378 |
t.Parallel() |
| 379 |
|
| 380 |
// Two ciphers with the same key should decrypt each other's output |
| 381 |
key := "consistent-key" |
| 382 |
cipher1 := newCipher(t, key) |
| 383 |
cipher2 := newCipher(t, key) |
| 384 |
|
| 385 |
plainText := []byte("consistency test") |
| 386 |
|
| 387 |
encrypted1, err := cipher1.EncryptBytes(plainText) |
| 388 |
require.NoError(t, err, "cipher1 EncryptBytes failed") |
| 389 |
|
| 390 |
decrypted2, err := cipher2.DecryptBytes(encrypted1) |
| 391 |
require.NoError(t, err, "cipher2 DecryptBytes failed") |
| 392 |
require.Equal(t, plainText, decrypted2, "ciphers with same key should be compatible") |
| 393 |
} |
| 394 |
|
| 395 |
func TestMultipleEncryptionRounds(t *testing.T) { |
| 396 |
t.Parallel() |
| 397 |
|
| 398 |
cipher := newCipher(t, "test-key") |
| 399 |
plainTexts := [][]byte{ |
| 400 |
[]byte("first message"), |
| 401 |
[]byte("second message"), |
| 402 |
[]byte("third message"), |
| 403 |
} |
| 404 |
|
| 405 |
for i, plainText := range plainTexts { |
| 406 |
encrypted, err := cipher.EncryptBytes(plainText) |
| 407 |
require.NoError(t, err, "round %d EncryptBytes failed", i+1) |
| 408 |
|
| 409 |
decrypted, err := cipher.DecryptBytes(encrypted) |
| 410 |
require.NoError(t, err, "round %d DecryptBytes failed", i+1) |
| 411 |
require.Equal(t, plainText, decrypted, "round %d failed", i+1) |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func TestCipherConcurrentUse(t *testing.T) { |
| 416 |
t.Parallel() |
| 417 |
|
| 418 |
const workerCount = 16 |
| 419 |
|
| 420 |
cipher := newCipher(t, "test-key") |
| 421 |
plainText := []byte("concurrent message") |
| 422 |
|
| 423 |
var wg sync.WaitGroup |
| 424 |
|
| 425 |
for range workerCount { |
| 426 |
wg.Go(func() { |
| 427 |
for range 100 { |
| 428 |
encrypted, err := cipher.EncryptBytes(plainText) |
| 429 |
if err != nil { |
| 430 |
t.Errorf("EncryptBytes failed: %v", err) |
| 431 |
|
| 432 |
return |
| 433 |
} |
| 434 |
|
| 435 |
decrypted, err := cipher.DecryptBytes(encrypted) |
| 436 |
if err != nil { |
| 437 |
t.Errorf("DecryptBytes failed: %v", err) |
| 438 |
|
| 439 |
return |
| 440 |
} |
| 441 |
|
| 442 |
if !bytes.Equal(plainText, decrypted) { |
| 443 |
t.Errorf("decrypted text %q does not match %q", decrypted, plainText) |
| 444 |
|
| 445 |
return |
| 446 |
} |
| 447 |
} |
| 448 |
}) |
| 449 |
} |
| 450 |
|
| 451 |
wg.Wait() |
| 452 |
} |
| 453 |
|
| 454 |
func BenchmarkDeriveKey(b *testing.B) { |
| 455 |
key := "benchmark-key" |
| 456 |
|
| 457 |
b.ResetTimer() |
| 458 |
|
| 459 |
for b.Loop() { |
| 460 |
if _, err := crypto.DeriveKey(key); err != nil { |
| 461 |
b.Fatal(err) |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
func BenchmarkNewCipher(b *testing.B) { |
| 467 |
key := "benchmark-key" |
| 468 |
|
| 469 |
b.ResetTimer() |
| 470 |
|
| 471 |
for b.Loop() { |
| 472 |
if _, err := crypto.New(key); err != nil { |
| 473 |
b.Fatal(err) |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
func BenchmarkEncryptBytes(b *testing.B) { |
| 479 |
cipher := newCipher(b, "benchmark-key") |
| 480 |
plainText := []byte("benchmark plaintext") |
| 481 |
|
| 482 |
b.ResetTimer() |
| 483 |
|
| 484 |
for b.Loop() { |
| 485 |
_, _ = cipher.EncryptBytes(plainText) |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
func BenchmarkDecryptBytes(b *testing.B) { |
| 490 |
cipher := newCipher(b, "benchmark-key") |
| 491 |
plainText := []byte("benchmark plaintext") |
| 492 |
encrypted, _ := cipher.EncryptBytes(plainText) |
| 493 |
|
| 494 |
b.ResetTimer() |
| 495 |
|
| 496 |
for b.Loop() { |
| 497 |
_, _ = cipher.DecryptBytes(encrypted) |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
func BenchmarkRoundTrip(b *testing.B) { |
| 502 |
cipher := newCipher(b, "benchmark-key") |
| 503 |
plainText := []byte("benchmark plaintext") |
| 504 |
|
| 505 |
b.ResetTimer() |
| 506 |
|
| 507 |
for b.Loop() { |
| 508 |
encrypted, _ := cipher.EncryptBytes(plainText) |
| 509 |
_, _ = cipher.DecryptBytes(encrypted) |
| 510 |
} |
| 511 |
} |