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.
12 // A sample test key for when we just want to initialize a cipher
13 var testKey = []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
15 // Test that the block size for tea is correct
16 func TestBlocksize(t *testing.T) {
17 c, err := NewCipher(testKey)
19 t.Fatalf("NewCipher returned error: %s", err)
22 if result := c.BlockSize(); result != BlockSize {
23 t.Errorf("cipher.BlockSize returned %d, but expected %d", result, BlockSize)
27 // Test that invalid key sizes return an error
28 func TestInvalidKeySize(t *testing.T) {
29 var key [KeySize + 1]byte
31 if _, err := NewCipher(key[:]); err == nil {
32 t.Errorf("invalid key size %d didn't result in an error.", len(key))
35 if _, err := NewCipher(key[:KeySize-1]); err == nil {
36 t.Errorf("invalid key size %d didn't result in an error.", KeySize-1)
48 var teaTests = []teaTest{
49 // These were sourced from https://github.com/froydnj/ironclad/blob/master/testing/test-vectors/tea.testvec
52 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
53 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
54 []byte{0x41, 0xea, 0x3a, 0x0a, 0x94, 0xba, 0xa9, 0x40},
58 []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
59 []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
60 []byte{0x31, 0x9b, 0xbe, 0xfb, 0x01, 0x6a, 0xbd, 0xb2},
64 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
65 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
66 []byte{0xed, 0x28, 0x5d, 0xa1, 0x45, 0x5b, 0x33, 0xc1},
71 func TestCipherEncrypt(t *testing.T) {
72 // Test encryption with standard 64 rounds
73 for i, test := range teaTests {
74 c, err := NewCipherWithRounds(test.key, test.rounds)
76 t.Fatalf("#%d: NewCipher returned error: %s", i, err)
79 var ciphertext [BlockSize]byte
80 c.Encrypt(ciphertext[:], test.plaintext)
82 if !bytes.Equal(ciphertext[:], test.ciphertext) {
83 t.Errorf("#%d: incorrect ciphertext. Got %x, wanted %x", i, ciphertext, test.ciphertext)
86 var plaintext2 [BlockSize]byte
87 c.Decrypt(plaintext2[:], ciphertext[:])
89 if !bytes.Equal(plaintext2[:], test.plaintext) {
90 t.Errorf("#%d: incorrect plaintext. Got %x, wanted %x", i, plaintext2, test.plaintext)