1
0

fips140_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package noiseutil
  2. import (
  3. "crypto/cipher"
  4. "crypto/fips140"
  5. "encoding/hex"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. // Ensure NewAESGCM validates the nonce is non-repeating
  10. func TestNewAESGCM(t *testing.T) {
  11. if !boringEnabled && !fips140.Enabled() {
  12. t.Skip("TestNewAESGCM is only for fips140/boringcrypto")
  13. }
  14. key, _ := hex.DecodeString("feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308")
  15. iv, _ := hex.DecodeString("00000000facedbaddecaf888")
  16. plaintext, _ := hex.DecodeString("d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39")
  17. aad, _ := hex.DecodeString("feedfacedeadbeeffeedfacedeadbeefabaddad2")
  18. expected, _ := hex.DecodeString("6a65c2edd45bd63c7e29f40e3d2ed8ba2b99f4c83135383d5676652f255059ceb24863ff10afb1089db701245da87fb88d3acd5f9dd0770cac220c3c04145caf25e190aeb775e7080401c628")
  19. var keyArray [32]byte
  20. copy(keyArray[:], key)
  21. c := CipherAESGCM.Cipher(keyArray)
  22. aead := c.(cipher.AEAD)
  23. dst := aead.Seal([]byte{}, iv, plaintext, aad)
  24. t.Logf("%x", dst)
  25. assert.Equal(t, expected, dst)
  26. // We expect this to fail since we are re-encrypting with a repeat IV
  27. switch {
  28. case boringEnabled:
  29. assert.PanicsWithError(t, "boringcrypto: EVP_AEAD_CTX_seal failed", func() {
  30. dst = aead.Seal([]byte{}, iv, plaintext, aad)
  31. })
  32. case fips140.Version() == "v1.0.0":
  33. assert.PanicsWithValue(t, "crypto/cipher: counter decreased", func() {
  34. dst = aead.Seal([]byte{}, iv, plaintext, aad)
  35. })
  36. default:
  37. assert.PanicsWithValue(t, "crypto/cipher: counter decreased or remained the same", func() {
  38. dst = aead.Seal([]byte{}, iv, plaintext, aad)
  39. })
  40. }
  41. }