Debian Codegraph @

serpent/serpent_test.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file.
3
4 package serpent
5
6 import (
7 "bytes"
8 "crypto/cipher"
9 "encoding/hex"
10 "testing"
11 )
12
13 var recoverFail = func(t *testing.T) {
14 if err := recover(); err == nil {
15 t.Fatal("Recover expected error, but no one occured")
16 }
17 }
18
19 var badKeys = [][]byte{
20 make([]byte, 15),
21 make([]byte, 17),
22 make([]byte, 23),
23 make([]byte, 25),
24 make([]byte, 31),
25 make([]byte, 33),
26 }
27
28 func TestEncrypt(t *testing.T) {
29 encFail := func(t *testing.T, c cipher.Block, srcLen, dstLen int) {
30 defer recoverFail(t)
31 src := make([]byte, srcLen)
32 dst := make([]byte, dstLen)
33 c.Encrypt(dst, src)
34 }
35
36 c, err := NewCipher(make([]byte, 16))
37 if err != nil {
38 t.Fatalf("Failed to create serpent cipher: %s", err)
39 }
40 encFail(t, c, BlockSize-1, BlockSize)
41 encFail(t, c, BlockSize, BlockSize-1)
42 }
43
44 func TestDecrypt(t *testing.T) {
45 decFail := func(t *testing.T, c cipher.Block, srcLen, dstLen int) {
46 defer recoverFail(t)
47 src := make([]byte, srcLen)
48 dst := make([]byte, dstLen)
49 c.Decrypt(dst, src)
50 }
51
52 c, err := NewCipher(make([]byte, 16))
53 if err != nil {
54 t.Fatalf("Failed to create serpent cipher: %s", err)
55 }
56 decFail(t, c, BlockSize-1, BlockSize)
57 decFail(t, c, BlockSize, BlockSize-1)
58 }
59
60 func TestEncryptDecrypt(t *testing.T) {
61 c, err := NewCipher(make([]byte, 16))
62 if err != nil {
63 t.Fatalf("Failed to create serpent cipher: %s", err)
64 }
65
66 src := make([]byte, 32)
67 dst := make([]byte, 32)
68
69 c.Encrypt(dst, src)
70 c.Encrypt(dst[16:], src[:16])
71 c.Decrypt(dst, dst)
72 c.Decrypt(dst[16:], dst[16:])
73
74 if !bytes.Equal(src, dst) {
75 t.Fatalf("En / decryption sequence failed\nFound: %s\nExpected: %s", hex.EncodeToString(dst), hex.EncodeToString(src))
76 }
77 }
78
79 func TestNewCipher(t *testing.T) {
80 var (
81 key128 [16]byte
82 key192 [24]byte
83 key256 [32]byte
84 )
85 _, err := NewCipher(key128[:])
86 if err != nil {
87 t.Fatalf("NewCipher rejected valid key with length: %d", len(key128))
88 }
89 _, err = NewCipher(key192[:])
90 if err != nil {
91 t.Fatalf("NewCipher rejected valid key with length: %d", len(key192))
92 }
93 _, err = NewCipher(key256[:])
94 if err != nil {
95 t.Fatalf("NewCipher rejected valid key with length: %d", len(key256))
96 }
97
98 for i, v := range badKeys {
99 _, err := NewCipher(v)
100 if err == nil {
101 t.Fatalf("NewCipher accpeted bad key %d with length: %d", i, len(v))
102 }
103 }
104 }
105
106 func TestBlockSize(t *testing.T) {
107 s := new(subkeys)
108 if bs := s.BlockSize(); bs != BlockSize {
109 t.Fatalf("BlockSize() returned unexpected value: %d", bs)
110 }
111 }
112
113 // Tests the S-Box 0 and its inverse.
114 func TestSBox0(t *testing.T) {
115 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
116 for i := 0; i < 16; i++ {
117 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
118
119 i0, i1, i2, i3 := v0, v1, v2, v3
120 sb0(&v0, &v1, &v2, &v3)
121 sb0Inv(&v0, &v1, &v2, &v3)
122
123 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
124 t.Fatal("Sbox 0 failed")
125 }
126 }
127 }
128
129 // Tests the S-Box 1 and its inverse.
130 func TestSBox1(t *testing.T) {
131 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
132 for i := 0; i < 16; i++ {
133 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
134
135 i0, i1, i2, i3 := v0, v1, v2, v3
136 sb1(&v0, &v1, &v2, &v3)
137 sb1Inv(&v0, &v1, &v2, &v3)
138
139 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
140 t.Fatal("sbox 1 failed")
141 }
142 }
143 }
144
145 // Tests the S-Box 2 and its inverse.
146 func TestSBox2(t *testing.T) {
147 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
148 for i := 0; i < 16; i++ {
149 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
150
151 i0, i1, i2, i3 := v0, v1, v2, v3
152 sb2(&v0, &v1, &v2, &v3)
153 sb2Inv(&v0, &v1, &v2, &v3)
154
155 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
156 t.Fatal("sbox 2 failed")
157 }
158 }
159 }
160
161 // Tests the S-Box 3 and its inverse.
162 func TestSBox3(t *testing.T) {
163 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
164 for i := 0; i < 16; i++ {
165 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
166
167 i0, i1, i2, i3 := v0, v1, v2, v3
168 sb3(&v0, &v1, &v2, &v3)
169 sb3Inv(&v0, &v1, &v2, &v3)
170
171 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
172 t.Fatal("sbox 3 failed")
173 }
174 }
175 }
176
177 // Tests the S-Box 4 and its inverse.
178 func TestSBox4(t *testing.T) {
179 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
180 for i := 0; i < 16; i++ {
181 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
182
183 i0, i1, i2, i3 := v0, v1, v2, v3
184 sb4(&v0, &v1, &v2, &v3)
185 sb4Inv(&v0, &v1, &v2, &v3)
186
187 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
188 t.Fatal("sbox 4 failed")
189 }
190 }
191 }
192
193 // Tests the S-Box 5 and its inverse.
194 func TestSBox5(t *testing.T) {
195 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
196 for i := 0; i < 16; i++ {
197 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
198
199 i0, i1, i2, i3 := v0, v1, v2, v3
200 sb5(&v0, &v1, &v2, &v3)
201 sb5Inv(&v0, &v1, &v2, &v3)
202
203 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
204 t.Fatal("sbox 5 failed")
205 }
206 }
207 }
208
209 // Tests the S-Box 6 and its inverse.
210 func TestSBox6(t *testing.T) {
211 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
212 for i := 0; i < 16; i++ {
213 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
214
215 i0, i1, i2, i3 := v0, v1, v2, v3
216 sb6(&v0, &v1, &v2, &v3)
217 sb6Inv(&v0, &v1, &v2, &v3)
218
219 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
220 t.Fatal("sbox 6 failed")
221 }
222 }
223 }
224
225 // Tests the S-Box 7 and its inverse.
226 func TestSBox7(t *testing.T) {
227 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
228 for i := 0; i < 16; i++ {
229 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
230
231 i0, i1, i2, i3 := v0, v1, v2, v3
232 sb7(&v0, &v1, &v2, &v3)
233 sb7Inv(&v0, &v1, &v2, &v3)
234
235 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
236 t.Fatal("sbox 7 failed")
237 }
238 }
239 }
240
241 // Tests the linear transformation and its inverse
242 func TestLinear(t *testing.T) {
243 v0, v1, v2, v3 := uint32(0), uint32(0), uint32(0), uint32(0)
244 for i := 0; i < 16; i++ {
245 v0, v1, v2, v3 = v3+v0+uint32(i), v0+v1, v1+v2, v2+v3
246 i0, i1, i2, i3 := v0, v1, v2, v3
247 linear(&v0, &v1, &v2, &v3)
248 linearInv(&v0, &v1, &v2, &v3)
249
250 if i0 != v0 || i1 != v1 || i2 != v2 || i3 != v3 {
251 t.Fatal("linear function failed")
252 }
253 }
254 }
255
256 // Benchmarks
257
258 func BenchmarkEncrypt_16(b *testing.B) { benchmarkEncrypt(b, 16) }
259 func BenchmarkDecrypt_16(b *testing.B) { benchmarkDecrypt(b, 16) }
260 func BenchmarkEncrypt_1K(b *testing.B) { benchmarkEncrypt(b, 1024) }
261 func BenchmarkDecrypt_1K(b *testing.B) { benchmarkDecrypt(b, 1024) }
262
263 func benchmarkEncrypt(b *testing.B, size int) {
264 c, err := NewCipher(make([]byte, 16))
265 if err != nil {
266 b.Fatalf("Failed to create Serpent instance: %s", err)
267 }
268 buf := make([]byte, c.BlockSize())
269 b.SetBytes(int64(size - (size % c.BlockSize())))
270
271 n := size / c.BlockSize()
272 b.ResetTimer()
273 for i := 0; i < b.N; i++ {
274 for j := 0; j < n; j++ {
275 c.Encrypt(buf, buf)
276 }
277 }
278 }
279
280 func benchmarkDecrypt(b *testing.B, size int) {
281 c, err := NewCipher(make([]byte, 16))
282 if err != nil {
283 b.Fatalf("Failed to create Serpent instance: %s", err)
284 }
285 buf := make([]byte, c.BlockSize())
286 b.SetBytes(int64(size - (size % c.BlockSize())))
287
288 n := size / c.BlockSize()
289 b.ResetTimer()
290 for i := 0; i < b.N; i++ {
291 for j := 0; j < n; j++ {
292 c.Decrypt(buf, buf)
293 }
294 }
295 }