github.com/maenmax/kairep@v0.0.0-20210218001208-55bf3df36788/src/golang.org/x/crypto/tea/tea_test.go (about) 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 5 package tea 6 7 import ( 8 "bytes" 9 "testing" 10 ) 11 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} 14 15 // Test that the block size for tea is correct 16 func TestBlocksize(t *testing.T) { 17 c, err := NewCipher(testKey) 18 if err != nil { 19 t.Fatalf("NewCipher returned error: %s", err) 20 } 21 22 if result := c.BlockSize(); result != BlockSize { 23 t.Errorf("cipher.BlockSize returned %d, but expected %d", result, BlockSize) 24 } 25 } 26 27 // Test that invalid key sizes return an error 28 func TestInvalidKeySize(t *testing.T) { 29 var key [KeySize + 1]byte 30 31 if _, err := NewCipher(key[:]); err == nil { 32 t.Errorf("invalid key size %d didn't result in an error.", len(key)) 33 } 34 35 if _, err := NewCipher(key[:KeySize-1]); err == nil { 36 t.Errorf("invalid key size %d didn't result in an error.", KeySize-1) 37 } 38 } 39 40 // Test Vectors 41 type teaTest struct { 42 rounds int 43 key []byte 44 plaintext []byte 45 ciphertext []byte 46 } 47 48 var teaTests = []teaTest{ 49 // These were sourced from https://github.com/froydnj/ironclad/blob/master/testing/test-vectors/tea.testvec 50 { 51 numRounds, 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}, 55 }, 56 { 57 numRounds, 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}, 61 }, 62 { 63 16, 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}, 67 }, 68 } 69 70 // Test encryption 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) 75 if err != nil { 76 t.Fatalf("#%d: NewCipher returned error: %s", i, err) 77 } 78 79 var ciphertext [BlockSize]byte 80 c.Encrypt(ciphertext[:], test.plaintext) 81 82 if !bytes.Equal(ciphertext[:], test.ciphertext) { 83 t.Errorf("#%d: incorrect ciphertext. Got %x, wanted %x", i, ciphertext, test.ciphertext) 84 } 85 86 var plaintext2 [BlockSize]byte 87 c.Decrypt(plaintext2[:], ciphertext[:]) 88 89 if !bytes.Equal(plaintext2[:], test.plaintext) { 90 t.Errorf("#%d: incorrect plaintext. Got %x, wanted %x", i, plaintext2, test.plaintext) 91 } 92 } 93 }