Source file
src/crypto/rc4/rc4_test.go
1
2
3
4
5 package rc4
6
7 import (
8 "bytes"
9 "fmt"
10 "testing"
11 )
12
13 type rc4Test struct {
14 key, keystream []byte
15 }
16
17 var golden = []rc4Test{
18
19
20 {
21 []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
22 []byte{0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79},
23 },
24 {
25 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
26 []byte{0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a},
27 },
28 {
29 []byte{0xef, 0x01, 0x23, 0x45},
30 []byte{0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61},
31 },
32
33
34 {
35 []byte{0x4b, 0x65, 0x79},
36 []byte{0xeb, 0x9f, 0x77, 0x81, 0xb7, 0x34, 0xca, 0x72, 0xa7, 0x19},
37 },
38 {
39 []byte{0x57, 0x69, 0x6b, 0x69},
40 []byte{0x60, 0x44, 0xdb, 0x6d, 0x41, 0xb7},
41 },
42 {
43 []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
44 []byte{
45 0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a,
46 0x8a, 0x06, 0x1e, 0x67, 0x57, 0x6e, 0x92, 0x6d,
47 0xc7, 0x1a, 0x7f, 0xa3, 0xf0, 0xcc, 0xeb, 0x97,
48 0x45, 0x2b, 0x4d, 0x32, 0x27, 0x96, 0x5f, 0x9e,
49 0xa8, 0xcc, 0x75, 0x07, 0x6d, 0x9f, 0xb9, 0xc5,
50 0x41, 0x7a, 0xa5, 0xcb, 0x30, 0xfc, 0x22, 0x19,
51 0x8b, 0x34, 0x98, 0x2d, 0xbb, 0x62, 0x9e, 0xc0,
52 0x4b, 0x4f, 0x8b, 0x05, 0xa0, 0x71, 0x08, 0x50,
53 0x92, 0xa0, 0xc3, 0x58, 0x4a, 0x48, 0xe4, 0xa3,
54 0x0a, 0x39, 0x7b, 0x8a, 0xcd, 0x1d, 0x00, 0x9e,
55 0xc8, 0x7d, 0x68, 0x11, 0xf2, 0x2c, 0xf4, 0x9c,
56 0xa3, 0xe5, 0x93, 0x54, 0xb9, 0x45, 0x15, 0x35,
57 0xa2, 0x18, 0x7a, 0x86, 0x42, 0x6c, 0xca, 0x7d,
58 0x5e, 0x82, 0x3e, 0xba, 0x00, 0x44, 0x12, 0x67,
59 0x12, 0x57, 0xb8, 0xd8, 0x60, 0xae, 0x4c, 0xbd,
60 0x4c, 0x49, 0x06, 0xbb, 0xc5, 0x35, 0xef, 0xe1,
61 0x58, 0x7f, 0x08, 0xdb, 0x33, 0x95, 0x5c, 0xdb,
62 0xcb, 0xad, 0x9b, 0x10, 0xf5, 0x3f, 0xc4, 0xe5,
63 0x2c, 0x59, 0x15, 0x65, 0x51, 0x84, 0x87, 0xfe,
64 0x08, 0x4d, 0x0e, 0x3f, 0x03, 0xde, 0xbc, 0xc9,
65 0xda, 0x1c, 0xe9, 0x0d, 0x08, 0x5c, 0x2d, 0x8a,
66 0x19, 0xd8, 0x37, 0x30, 0x86, 0x16, 0x36, 0x92,
67 0x14, 0x2b, 0xd8, 0xfc, 0x5d, 0x7a, 0x73, 0x49,
68 0x6a, 0x8e, 0x59, 0xee, 0x7e, 0xcf, 0x6b, 0x94,
69 0x06, 0x63, 0xf4, 0xa6, 0xbe, 0xe6, 0x5b, 0xd2,
70 0xc8, 0x5c, 0x46, 0x98, 0x6c, 0x1b, 0xef, 0x34,
71 0x90, 0xd3, 0x7b, 0x38, 0xda, 0x85, 0xd3, 0x2e,
72 0x97, 0x39, 0xcb, 0x23, 0x4a, 0x2b, 0xe7, 0x40,
73 },
74 },
75 }
76
77 func testEncrypt(t *testing.T, desc string, c *Cipher, src, expect []byte) {
78 dst := make([]byte, len(src))
79 c.XORKeyStream(dst, src)
80 for i, v := range dst {
81 if v != expect[i] {
82 t.Fatalf("%s: mismatch at byte %d:\nhave %x\nwant %x", desc, i, dst, expect)
83 }
84 }
85 }
86
87 func TestGolden(t *testing.T) {
88 for gi, g := range golden {
89 data := make([]byte, len(g.keystream))
90 for i := range data {
91 data[i] = byte(i)
92 }
93
94 expect := make([]byte, len(g.keystream))
95 for i := range expect {
96 expect[i] = byte(i) ^ g.keystream[i]
97 }
98
99 for size := 1; size <= len(g.keystream); size++ {
100 c, err := NewCipher(g.key)
101 if err != nil {
102 t.Fatalf("#%d: NewCipher: %v", gi, err)
103 }
104
105 off := 0
106 for off < len(g.keystream) {
107 n := len(g.keystream) - off
108 if n > size {
109 n = size
110 }
111 desc := fmt.Sprintf("#%d@[%d:%d]", gi, off, off+n)
112 testEncrypt(t, desc, c, data[off:off+n], expect[off:off+n])
113 off += n
114 }
115 }
116 }
117 }
118
119 func TestBlock(t *testing.T) {
120 c1a, _ := NewCipher(golden[0].key)
121 c1b, _ := NewCipher(golden[1].key)
122 data1 := make([]byte, 1<<20)
123 for i := range data1 {
124 c1a.XORKeyStream(data1[i:i+1], data1[i:i+1])
125 c1b.XORKeyStream(data1[i:i+1], data1[i:i+1])
126 }
127
128 c2a, _ := NewCipher(golden[0].key)
129 c2b, _ := NewCipher(golden[1].key)
130 data2 := make([]byte, 1<<20)
131 c2a.XORKeyStream(data2, data2)
132 c2b.XORKeyStream(data2, data2)
133
134 if !bytes.Equal(data1, data2) {
135 t.Fatalf("bad block")
136 }
137 }
138
139 func benchmark(b *testing.B, size int64) {
140 buf := make([]byte, size)
141 c, err := NewCipher(golden[0].key)
142 if err != nil {
143 panic(err)
144 }
145 b.SetBytes(size)
146
147 for i := 0; i < b.N; i++ {
148 c.XORKeyStream(buf, buf)
149 }
150 }
151
152 func BenchmarkRC4_128(b *testing.B) {
153 benchmark(b, 128)
154 }
155
156 func BenchmarkRC4_1K(b *testing.B) {
157 benchmark(b, 1024)
158 }
159
160 func BenchmarkRC4_8K(b *testing.B) {
161 benchmark(b, 8096)
162 }
163
View as plain text