tea_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tea
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. // A sample test key for when we just want to initialize a cipher
  10. var testKey = []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
  11. // Test that the block size for tea is correct
  12. func TestBlocksize(t *testing.T) {
  13. c, err := NewCipher(testKey)
  14. if err != nil {
  15. t.Fatalf("NewCipher returned error: %s", err)
  16. }
  17. if result := c.BlockSize(); result != BlockSize {
  18. t.Errorf("cipher.BlockSize returned %d, but expected %d", result, BlockSize)
  19. }
  20. }
  21. // Test that invalid key sizes return an error
  22. func TestInvalidKeySize(t *testing.T) {
  23. var key [KeySize + 1]byte
  24. if _, err := NewCipher(key[:]); err == nil {
  25. t.Errorf("invalid key size %d didn't result in an error.", len(key))
  26. }
  27. if _, err := NewCipher(key[:KeySize-1]); err == nil {
  28. t.Errorf("invalid key size %d didn't result in an error.", KeySize-1)
  29. }
  30. }
  31. // Test Vectors
  32. type teaTest struct {
  33. rounds int
  34. key []byte
  35. plaintext []byte
  36. ciphertext []byte
  37. }
  38. var teaTests = []teaTest{
  39. // These were sourced from https://github.com/froydnj/ironclad/blob/master/testing/test-vectors/tea.testvec
  40. {
  41. numRounds,
  42. []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  43. []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  44. []byte{0x41, 0xea, 0x3a, 0x0a, 0x94, 0xba, 0xa9, 0x40},
  45. },
  46. {
  47. numRounds,
  48. []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  49. []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  50. []byte{0x31, 0x9b, 0xbe, 0xfb, 0x01, 0x6a, 0xbd, 0xb2},
  51. },
  52. {
  53. 16,
  54. []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  55. []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  56. []byte{0xed, 0x28, 0x5d, 0xa1, 0x45, 0x5b, 0x33, 0xc1},
  57. },
  58. }
  59. // Test encryption
  60. func TestCipherEncrypt(t *testing.T) {
  61. // Test encryption with standard 64 rounds
  62. for i, test := range teaTests {
  63. c, err := NewCipherWithRounds(test.key, test.rounds)
  64. if err != nil {
  65. t.Fatalf("#%d: NewCipher returned error: %s", i, err)
  66. }
  67. var ciphertext [BlockSize]byte
  68. c.Encrypt(ciphertext[:], test.plaintext)
  69. if !bytes.Equal(ciphertext[:], test.ciphertext) {
  70. t.Errorf("#%d: incorrect ciphertext. Got %x, wanted %x", i, ciphertext, test.ciphertext)
  71. }
  72. var plaintext2 [BlockSize]byte
  73. c.Decrypt(plaintext2[:], ciphertext[:])
  74. if !bytes.Equal(plaintext2[:], test.plaintext) {
  75. t.Errorf("#%d: incorrect plaintext. Got %x, wanted %x", i, plaintext2, test.plaintext)
  76. }
  77. }
  78. }