]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/crypto/xtea/xtea_test.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / crypto / xtea / xtea_test.go
1 // Copyright 2009 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 xtea
6
7 import (
8 "testing"
9 )
10
11 // A sample test key for when we just want to initialise a cipher
12 var testKey = []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
13
14 // Test that the block size for XTEA is correct
15 func TestBlocksize(t *testing.T) {
16 if BlockSize != 8 {
17 t.Errorf("BlockSize constant - expected 8, got %d", BlockSize)
18 return
19 }
20
21 c, err := NewCipher(testKey)
22 if err != nil {
23 t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err)
24 return
25 }
26
27 result := c.BlockSize()
28 if result != 8 {
29 t.Errorf("BlockSize function - expected 8, gotr %d", result)
30 return
31 }
32 }
33
34 // A series of test values to confirm that the Cipher.table array was initialised correctly
35 var testTable = []uint32{
36 0x00112233, 0x6B1568B8, 0xE28CE030, 0xC5089E2D, 0xC5089E2D, 0x1EFBD3A2, 0xA7845C2A, 0x78EF0917,
37 0x78EF0917, 0x172682D0, 0x5B6AC714, 0x822AC955, 0x3DE68511, 0xDC1DFECA, 0x2062430E, 0x3611343F,
38 0xF1CCEFFB, 0x900469B4, 0xD448ADF8, 0x2E3BE36D, 0xB6C46BF5, 0x994029F2, 0x994029F2, 0xF3335F67,
39 0x6AAAD6DF, 0x4D2694DC, 0x4D2694DC, 0xEB5E0E95, 0x2FA252D9, 0x4551440A, 0x121E10D6, 0xB0558A8F,
40 0xE388BDC3, 0x0A48C004, 0xC6047BC0, 0x643BF579, 0xA88039BD, 0x02736F32, 0x8AFBF7BA, 0x5C66A4A7,
41 0x5C66A4A7, 0xC76AEB2C, 0x3EE262A4, 0x215E20A1, 0x215E20A1, 0x7B515616, 0x03D9DE9E, 0x1988CFCF,
42 0xD5448B8B, 0x737C0544, 0xB7C04988, 0xDE804BC9, 0x9A3C0785, 0x3873813E, 0x7CB7C582, 0xD6AAFAF7,
43 0x4E22726F, 0x309E306C, 0x309E306C, 0x8A9165E1, 0x1319EE69, 0xF595AC66, 0xF595AC66, 0x4F88E1DB,
44 }
45
46 // Test that the cipher context is initialised correctly
47 func TestCipherInit(t *testing.T) {
48 c, err := NewCipher(testKey)
49 if err != nil {
50 t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err)
51 return
52 }
53
54 for i := 0; i < len(c.table); i++ {
55 if c.table[i] != testTable[i] {
56 t.Errorf("NewCipher() failed to initialise Cipher.table[%d] correctly. Expected %08X, got %08X", i, testTable[i], c.table[i])
57 break
58 }
59 }
60 }
61
62 // Test that invalid key sizes return an error
63 func TestInvalidKeySize(t *testing.T) {
64 // Test a long key
65 key := []byte{
66 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
67 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
68 }
69
70 _, err := NewCipher(key)
71 if err == nil {
72 t.Errorf("Invalid key size %d didn't result in an error.", len(key))
73 }
74
75 // Test a short key
76 key = []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}
77
78 _, err = NewCipher(key)
79 if err == nil {
80 t.Errorf("Invalid key size %d didn't result in an error.", len(key))
81 }
82 }
83
84 // Test that we can correctly decode some bytes we have encoded
85 func TestEncodeDecode(t *testing.T) {
86 original := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
87 input := original
88 output := make([]byte, BlockSize)
89
90 c, err := NewCipher(testKey)
91 if err != nil {
92 t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err)
93 return
94 }
95
96 // Encrypt the input block
97 c.Encrypt(output, input)
98
99 // Check that the output does not match the input
100 differs := false
101 for i := 0; i < len(input); i++ {
102 if output[i] != input[i] {
103 differs = true
104 break
105 }
106 }
107 if differs == false {
108 t.Error("Cipher.Encrypt: Failed to encrypt the input block.")
109 return
110 }
111
112 // Decrypt the block we just encrypted
113 input = output
114 output = make([]byte, BlockSize)
115 c.Decrypt(output, input)
116
117 // Check that the output from decrypt matches our initial input
118 for i := 0; i < len(input); i++ {
119 if output[i] != original[i] {
120 t.Errorf("Decrypted byte %d differed. Expected %02X, got %02X\n", i, original[i], output[i])
121 return
122 }
123 }
124 }
125
126 // Test Vectors
127 type CryptTest struct {
128 key []byte
129 plainText []byte
130 cipherText []byte
131 }
132
133 var CryptTests = []CryptTest{
134 // These were sourced from http://www.freemedialibrary.com/index.php/XTEA_test_vectors
135 {
136 []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
137 []byte{0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48},
138 []byte{0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5},
139 },
140 {
141 []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
142 []byte{0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41},
143 []byte{0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8},
144 },
145 {
146 []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
147 []byte{0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f},
148 []byte{0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41},
149 },
150 {
151 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
152 []byte{0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48},
153 []byte{0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5},
154 },
155 {
156 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
157 []byte{0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41},
158 []byte{0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d},
159 },
160 {
161 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
162 []byte{0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55},
163 []byte{0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41},
164 },
165
166 // These vectors are from http://wiki.secondlife.com/wiki/XTEA_Strong_Encryption_Implementation#Bouncy_Castle_C.23_API
167 {
168 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
169 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
170 []byte{0xDE, 0xE9, 0xD4, 0xD8, 0xF7, 0x13, 0x1E, 0xD9},
171 },
172 {
173 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
174 []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
175 []byte{0x06, 0x5C, 0x1B, 0x89, 0x75, 0xC6, 0xA8, 0x16},
176 },
177 {
178 []byte{0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A},
179 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
180 []byte{0x1F, 0xF9, 0xA0, 0x26, 0x1A, 0xC6, 0x42, 0x64},
181 },
182 {
183 []byte{0x01, 0x23, 0x45, 0x67, 0x12, 0x34, 0x56, 0x78, 0x23, 0x45, 0x67, 0x89, 0x34, 0x56, 0x78, 0x9A},
184 []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
185 []byte{0x8C, 0x67, 0x15, 0x5B, 0x2E, 0xF9, 0x1E, 0xAD},
186 },
187 }
188
189 // Test encryption
190 func TestCipherEncrypt(t *testing.T) {
191 for i, tt := range CryptTests {
192 c, err := NewCipher(tt.key)
193 if err != nil {
194 t.Errorf("NewCipher(%d bytes), vector %d = %s", len(tt.key), i, err)
195 continue
196 }
197
198 out := make([]byte, len(tt.plainText))
199 c.Encrypt(out, tt.plainText)
200
201 for j := 0; j < len(out); j++ {
202 if out[j] != tt.cipherText[j] {
203 t.Errorf("Cipher.Encrypt %d: out[%d] = %02X, expected %02X", i, j, out[j], tt.cipherText[j])
204 break
205 }
206 }
207 }
208 }
209
210 // Test decryption
211 func TestCipherDecrypt(t *testing.T) {
212 for i, tt := range CryptTests {
213 c, err := NewCipher(tt.key)
214 if err != nil {
215 t.Errorf("NewCipher(%d bytes), vector %d = %s", len(tt.key), i, err)
216 continue
217 }
218
219 out := make([]byte, len(tt.cipherText))
220 c.Decrypt(out, tt.cipherText)
221
222 for j := 0; j < len(out); j++ {
223 if out[j] != tt.plainText[j] {
224 t.Errorf("Cipher.Decrypt %d: out[%d] = %02X, expected %02X", i, j, out[j], tt.plainText[j])
225 break
226 }
227 }
228 }
229 }
230
231 // Test resetting the cipher context
232 func TestReset(t *testing.T) {
233 c, err := NewCipher(testKey)
234 if err != nil {
235 t.Errorf("NewCipher(%d bytes) = %s", len(testKey), err)
236 return
237 }
238
239 c.Reset()
240 for i := 0; i < len(c.table); i++ {
241 if c.table[i] != 0 {
242 t.Errorf("Cipher.Reset: Failed to clear Cipher.table[%d]. expected 0, got %08X", i, c.table[i])
243 return
244 }
245 }
246 }