OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package twofish | 5 package twofish |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "testing" | 9 "testing" |
10 ) | 10 ) |
(...skipping 18 matching lines...) Expand all Loading... |
29 a0, b0 := x/16, x%16 | 29 a0, b0 := x/16, x%16 |
30 for i := 0; i < 2; i++ { | 30 for i := 0; i < 2; i++ { |
31 a1 := a0 ^ b0 | 31 a1 := a0 ^ b0 |
32 b1 := (a0 ^ ((b0 << 3) | (b0 >> 1)) ^ (a0 << 3)) & 15 | 32 b1 := (a0 ^ ((b0 << 3) | (b0 >> 1)) ^ (a0 << 3)) & 15 |
33 a0 = qbox[qi][2*i][a1] | 33 a0 = qbox[qi][2*i][a1] |
34 b0 = qbox[qi][2*i+1][b1] | 34 b0 = qbox[qi][2*i+1][b1] |
35 } | 35 } |
36 return (b0 << 4) + a0 | 36 return (b0 << 4) + a0 |
37 } | 37 } |
38 | 38 |
| 39 // Test if the sbox saved as variable contains the values as defined by its func
tion |
39 func TestSbox(t *testing.T) { | 40 func TestSbox(t *testing.T) { |
40 » for n := 0; n < 2; n++ { | 41 » for n := range sbox { |
41 » » for m := 0; m < 256; m++ { | 42 » » for m := range sbox[n] { |
42 if genSbox(n, byte(m)) != sbox[n][m] { | 43 if genSbox(n, byte(m)) != sbox[n][m] { |
43 t.Errorf("#%d|%d: sbox value = %d want %d", n, m
, sbox[n][m], genSbox(n, byte(m))) | 44 t.Errorf("#%d|%d: sbox value = %d want %d", n, m
, sbox[n][m], genSbox(n, byte(m))) |
44 } | 45 } |
45 } | 46 } |
46 } | 47 } |
47 } | 48 } |
48 | 49 |
| 50 // All tests are designed for direct encryption |
| 51 // please note that this ist not the recommended way for data bigger than the bl
ocksize |
49 var testVectors = []struct { | 52 var testVectors = []struct { |
50 key []byte | 53 key []byte |
51 dec []byte | 54 dec []byte |
52 enc []byte | 55 enc []byte |
53 }{ | 56 }{ |
54 // These tests are extracted from LibTom | 57 // These tests are extracted from LibTom |
55 { | 58 { |
56 []byte{0x9F, 0x58, 0x9F, 0x5C, 0xF6, 0x12, 0x2C, 0x32, 0xB6, 0xB
F, 0xEC, 0x2F, 0x2A, 0xE8, 0xC3, 0x5A}, | 59 []byte{0x9F, 0x58, 0x9F, 0x5C, 0xF6, 0x12, 0x2C, 0x32, 0xB6, 0xB
F, 0xEC, 0x2F, 0x2A, 0xE8, 0xC3, 0x5A}, |
57 []byte{0xD4, 0x91, 0xDB, 0x16, 0xE7, 0xB1, 0xC3, 0x9E, 0x86, 0xC
B, 0x08, 0x6B, 0x78, 0x9F, 0x54, 0x19}, | 60 []byte{0xD4, 0x91, 0xDB, 0x16, 0xE7, 0xB1, 0xC3, 0x9E, 0x86, 0xC
B, 0x08, 0x6B, 0x78, 0x9F, 0x54, 0x19}, |
58 []byte{0x01, 0x9F, 0x98, 0x09, 0xDE, 0x17, 0x11, 0x85, 0x8F, 0xA
A, 0xC3, 0xA3, 0xBA, 0x20, 0xFB, 0xC3}, | 61 []byte{0x01, 0x9F, 0x98, 0x09, 0xDE, 0x17, 0x11, 0x85, 0x8F, 0xA
A, 0xC3, 0xA3, 0xBA, 0x20, 0xFB, 0xC3}, |
(...skipping 27 matching lines...) Expand all Loading... |
86 []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xD
C, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, | 89 []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0xFE, 0xD
C, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, |
87 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x
99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, | 90 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x
99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, |
88 }, | 91 }, |
89 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0
0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | 92 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0
0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
90 []byte{0x37, 0x52, 0x7B, 0xE0, 0x05, 0x23, 0x34, 0xB8, 0x9F, 0x0
C, 0xFC, 0xCA, 0xE8, 0x7C, 0xFA, 0x20}, | 93 []byte{0x37, 0x52, 0x7B, 0xE0, 0x05, 0x23, 0x34, 0xB8, 0x9F, 0x0
C, 0xFC, 0xCA, 0xE8, 0x7C, 0xFA, 0x20}, |
91 }, | 94 }, |
92 } | 95 } |
93 | 96 |
94 func TestCipher(t *testing.T) { | 97 func TestCipher(t *testing.T) { |
95 for n, tt := range testVectors { | 98 for n, tt := range testVectors { |
96 » » // Test if the plaintext (dec) is encrypts to the given | 99 » » // Test if the known plaintext (dec) is encrypted into the known
crypttext (enc) |
97 » » // ciphertext (enc) using the given key. Test also if enc can | 100 » » // using the known key. Test also if enc can be decrypted again
into dec. |
98 » » // be decrypted again into dec. | |
99 c, err := NewCipher(tt.key) | 101 c, err := NewCipher(tt.key) |
100 if err != nil { | 102 if err != nil { |
101 t.Errorf("#%d: NewCipher: %v", n, err) | 103 t.Errorf("#%d: NewCipher: %v", n, err) |
102 return | 104 return |
103 } | 105 } |
104 | 106 |
105 buf := make([]byte, 16) | 107 buf := make([]byte, 16) |
106 c.Encrypt(buf, tt.dec) | 108 c.Encrypt(buf, tt.dec) |
107 if !bytes.Equal(buf, tt.enc) { | 109 if !bytes.Equal(buf, tt.enc) { |
108 t.Errorf("#%d: encrypt = %x want %x", n, buf, tt.enc) | 110 t.Errorf("#%d: encrypt = %x want %x", n, buf, tt.enc) |
109 } | 111 } |
110 c.Decrypt(buf, tt.enc) | 112 c.Decrypt(buf, tt.enc) |
111 if !bytes.Equal(buf, tt.dec) { | 113 if !bytes.Equal(buf, tt.dec) { |
112 t.Errorf("#%d: decrypt = %x want %x", n, buf, tt.dec) | 114 t.Errorf("#%d: decrypt = %x want %x", n, buf, tt.dec) |
113 } | 115 } |
114 | 116 |
115 » » // Test that 16 zero bytes, encrypted 1000 times then decrypted | 117 » » // Test if 16 zero bytes can be encrypt 1000 times, decrypted a
nd come |
116 » » // 1000 times results in zero bytes again. | 118 » » // back where we started using all different test keys |
117 zero := make([]byte, 16) | 119 zero := make([]byte, 16) |
118 buf = make([]byte, 16) | 120 buf = make([]byte, 16) |
119 for i := 0; i < 1000; i++ { | 121 for i := 0; i < 1000; i++ { |
120 c.Encrypt(buf, buf) | 122 c.Encrypt(buf, buf) |
121 } | 123 } |
122 for i := 0; i < 1000; i++ { | 124 for i := 0; i < 1000; i++ { |
123 c.Decrypt(buf, buf) | 125 c.Decrypt(buf, buf) |
124 } | 126 } |
125 if !bytes.Equal(buf, zero) { | 127 if !bytes.Equal(buf, zero) { |
126 t.Errorf("#%d: encrypt/decrypt 1000: have %x want %x", n
, buf, zero) | 128 t.Errorf("#%d: encrypt/decrypt 1000: have %x want %x", n
, buf, zero) |
127 } | 129 } |
128 } | 130 } |
129 } | 131 } |
OLD | NEW |