rc4_test.go 4.6 KB

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