aescts_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package aescts
  2. import (
  3. "encoding/hex"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. func TestAesCts_Encrypt_Decrypt(t *testing.T) {
  8. iv := make([]byte, 16)
  9. key, _ := hex.DecodeString("636869636b656e207465726979616b69")
  10. var tests = []struct {
  11. plain string
  12. cipher string
  13. nextIV string
  14. }{
  15. //Test vectors from RFC 3962 Appendix B
  16. {"4920776f756c64206c696b652074686520", "c6353568f2bf8cb4d8a580362da7ff7f97", "c6353568f2bf8cb4d8a580362da7ff7f"},
  17. {"4920776f756c64206c696b65207468652047656e6572616c20476175277320", "fc00783e0efdb2c1d445d4c8eff7ed2297687268d6ecccc0c07b25e25ecfe5", "fc00783e0efdb2c1d445d4c8eff7ed22"},
  18. {"4920776f756c64206c696b65207468652047656e6572616c2047617527732043", "39312523a78662d5be7fcbcc98ebf5a897687268d6ecccc0c07b25e25ecfe584", "39312523a78662d5be7fcbcc98ebf5a8"},
  19. {"4920776f756c64206c696b65207468652047656e6572616c20476175277320436869636b656e2c20706c656173652c", "97687268d6ecccc0c07b25e25ecfe584b3fffd940c16a18c1b5549d2f838029e39312523a78662d5be7fcbcc98ebf5", "b3fffd940c16a18c1b5549d2f838029e"},
  20. {"4920776f756c64206c696b65207468652047656e6572616c20476175277320436869636b656e2c20706c656173652c20", "97687268d6ecccc0c07b25e25ecfe5849dad8bbb96c4cdc03bc103e1a194bbd839312523a78662d5be7fcbcc98ebf5a8", "9dad8bbb96c4cdc03bc103e1a194bbd8"},
  21. {"4920776f756c64206c696b65207468652047656e6572616c20476175277320436869636b656e2c20706c656173652c20616e6420776f6e746f6e20736f75702e", "97687268d6ecccc0c07b25e25ecfe58439312523a78662d5be7fcbcc98ebf5a84807efe836ee89a526730dbc2f7bc8409dad8bbb96c4cdc03bc103e1a194bbd8", "4807efe836ee89a526730dbc2f7bc840"},
  22. }
  23. for i, test := range tests {
  24. m, _ := hex.DecodeString(test.plain)
  25. niv, c, err := Encrypt(key, iv, m)
  26. if err != nil {
  27. t.Errorf("Encryption failed for test %v: %v", i+1, err)
  28. }
  29. assert.Equal(t, test.cipher, hex.EncodeToString(c), "Encrypted result not as expected")
  30. assert.Equal(t, test.nextIV, hex.EncodeToString(niv), "Next state IV not as expected")
  31. }
  32. //t.Log("AES CTS Encryption tests finished")
  33. for i, test := range tests {
  34. b, _ := hex.DecodeString(test.cipher)
  35. p, err := Decrypt(key, iv, b)
  36. if err != nil {
  37. t.Errorf("Decryption failed for test %v: %v", i+1, err)
  38. }
  39. assert.Equal(t, test.plain, hex.EncodeToString(p), "Decrypted result not as expected")
  40. }
  41. //t.Log("AES CTS Decryption tests finished")
  42. }