Debian Codegraph @

serpent/vectors_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 "encoding/hex"
9 "testing"
10 )
11
12 func fromHex(s string) []byte {
13 b, err := hex.DecodeString(s)
14 if err != nil {
15 panic(err)
16 }
17 return b
18 }
19
20 // Test vectors for serpent
21 var vectors = []struct {
22 key, plaintext, ciphertext string
23 }{
24 // test vectors for 128 bit key from
25 // http://www.cs.technion.ac.il/~biham/Reports/Serpent/Serpent-128-128.verified.test-vectors
26 { // Set 1, vector# 0
27 key: "80000000000000000000000000000000",
28 plaintext: "00000000000000000000000000000000",
29 ciphertext: "264E5481EFF42A4606ABDA06C0BFDA3D",
30 },
31 { // Set 1, vector# 1
32 key: "40000000000000000000000000000000",
33 plaintext: "00000000000000000000000000000000",
34 ciphertext: "4A231B3BC727993407AC6EC8350E8524",
35 },
36 // test vectors for 192 bit key from
37 // http://www.cs.technion.ac.il/~biham/Reports/Serpent/Serpent-192-128.verified.test-vectors
38 { // Set 1, vector# 0
39 key: "800000000000000000000000000000000000000000000000",
40 plaintext: "00000000000000000000000000000000",
41 ciphertext: "9E274EAD9B737BB21EFCFCA548602689",
42 },
43 { // Set 1, vector# 3
44 key: "100000000000000000000000000000000000000000000000",
45 plaintext: "00000000000000000000000000000000",
46 ciphertext: "BEC1E37824CF721E5D87F6CB4EBFB9BE",
47 },
48 // test vectors for 256 bit key from
49 // http://www.cs.technion.ac.il/~biham/Reports/Serpent/Serpent-256-128.verified.test-vectors
50 { // Set 3, vector# 1
51 key: "0101010101010101010101010101010101010101010101010101010101010101",
52 plaintext: "01010101010101010101010101010101",
53 ciphertext: "EC9723B15B2A6489F84C4524FFFC2748",
54 },
55 { // Set 3, vector# 2
56 key: "0202020202020202020202020202020202020202020202020202020202020202",
57 plaintext: "02020202020202020202020202020202",
58 ciphertext: "1187F485538514476184E567DA0421C7",
59 },
60 }
61
62 // Tests all serpent test vectors.
63 func TestVectors(t *testing.T) {
64 for i, v := range vectors {
65 key := fromHex(v.key)
66 plaintext := fromHex(v.plaintext)
67 ciphertext := fromHex(v.ciphertext)
68 c, err := NewCipher(key)
69 if err != nil {
70 t.Fatalf("Test vector %d: Failed to create cipher instance: %s", i, err)
71 }
72
73 buf := make([]byte, BlockSize)
74
75 c.Encrypt(buf, plaintext)
76 if !bytes.Equal(ciphertext, buf) {
77 t.Fatalf("Test vector %d:\nEncryption failed\nFound: %s\nExpected: %s", i, hex.EncodeToString(buf), hex.EncodeToString(ciphertext))
78 }
79 c.Decrypt(buf, buf)
80 if !bytes.Equal(plaintext, buf) {
81 t.Fatalf("Test vector %d:\nDecryption failed\nFound: %s\nExpected: %s", i, hex.EncodeToString(buf), hex.EncodeToString(plaintext))
82 }
83 }
84 }