boring_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //go:build boringcrypto
  2. // +build boringcrypto
  3. package noiseutil
  4. import (
  5. "crypto/boring"
  6. "encoding/hex"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestEncryptLockNeeded(t *testing.T) {
  11. assert.True(t, EncryptLockNeeded)
  12. }
  13. // Ensure NewGCMTLS validates the nonce is non-repeating
  14. func TestNewGCMTLS(t *testing.T) {
  15. assert.True(t, boring.Enabled())
  16. // Test Case 16 from GCM Spec:
  17. // - (now dead link): http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf
  18. // - as listed in boringssl tests: https://github.com/google/boringssl/blob/fips-20220613/crypto/cipher_extra/test/cipher_tests.txt#L412-L418
  19. key, _ := hex.DecodeString("feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308")
  20. iv, _ := hex.DecodeString("cafebabefacedbaddecaf888")
  21. plaintext, _ := hex.DecodeString("d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39")
  22. aad, _ := hex.DecodeString("feedfacedeadbeeffeedfacedeadbeefabaddad2")
  23. expected, _ := hex.DecodeString("522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662")
  24. expectedTag, _ := hex.DecodeString("76fc6ece0f4e1768cddf8853bb2d551b")
  25. expected = append(expected, expectedTag...)
  26. var keyArray [32]byte
  27. copy(keyArray[:], key)
  28. c := CipherAESGCM.Cipher(keyArray)
  29. aead := c.(aeadCipher).AEAD
  30. dst := aead.Seal([]byte{}, iv, plaintext, aad)
  31. assert.Equal(t, expected, dst)
  32. // We expect this to fail since we are re-encrypting with a repeat IV
  33. assert.PanicsWithError(t, "boringcrypto: EVP_AEAD_CTX_seal failed", func() {
  34. dst = aead.Seal([]byte{}, iv, plaintext, aad)
  35. })
  36. }