]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/rc4: test the portable version too
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 18 Feb 2014 23:16:07 +0000 (15:16 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 18 Feb 2014 23:16:07 +0000 (15:16 -0800)
Prevent bitrot. (similar to the previous sha1 and md5 CLs)

Fixes #6642

LGTM=agl
R=agl, dave
CC=golang-codereviews
https://golang.org/cl/65690043

src/pkg/crypto/rc4/rc4.go
src/pkg/crypto/rc4/rc4_ref.go
src/pkg/crypto/rc4/rc4_test.go

index 3d717c63b0c81cee2e51aa90b404cc6e91446d02..9acb681bfb64e5f8ae875049ed9d071d4f65a0f2 100644 (file)
@@ -50,3 +50,20 @@ func (c *Cipher) Reset() {
        }
        c.i, c.j = 0, 0
 }
+
+// xorKeyStreamGeneric sets dst to the result of XORing src with the
+// key stream.  Dst and src may be the same slice but otherwise should
+// not overlap.
+//
+// This is the pure Go version. rc4_{amd64,386,arm}* contain assembly
+// implementations. This is here for tests and to prevent bitrot.
+func (c *Cipher) xorKeyStreamGeneric(dst, src []byte) {
+       i, j := c.i, c.j
+       for k, v := range src {
+               i += 1
+               j += uint8(c.s[i])
+               c.s[i], c.s[j] = c.s[j], c.s[i]
+               dst[k] = v ^ uint8(c.s[uint8(c.s[i]+c.s[j])])
+       }
+       c.i, c.j = i, j
+}
index bca4d28e1d1d8a2d6a2b5763395dea9bc88a9c10..bdf5e1db2ddf127801d7b182bacf0e08d726adf6 100644 (file)
@@ -9,12 +9,5 @@ package rc4
 // XORKeyStream sets dst to the result of XORing src with the key stream.
 // Dst and src may be the same slice but otherwise should not overlap.
 func (c *Cipher) XORKeyStream(dst, src []byte) {
-       i, j := c.i, c.j
-       for k, v := range src {
-               i += 1
-               j += uint8(c.s[i])
-               c.s[i], c.s[j] = c.s[j], c.s[i]
-               dst[k] = v ^ uint8(c.s[uint8(c.s[i]+c.s[j])])
-       }
-       c.i, c.j = i, j
+       c.xorKeyStreamGeneric(dst, src)
 }
index 7b4df6791d9aa4629e7c14322cec1478fc0d9863..af7988246329d5c4018db2506ebcfcb19427e071 100644 (file)
@@ -117,19 +117,30 @@ func TestGolden(t *testing.T) {
 }
 
 func TestBlock(t *testing.T) {
+       testBlock(t, (*Cipher).XORKeyStream)
+}
+
+// Test the pure Go version.
+// Because we have assembly for amd64, 386, and arm, this prevents
+// bitrot of the reference implementations.
+func TestBlockGeneric(t *testing.T) {
+       testBlock(t, (*Cipher).xorKeyStreamGeneric)
+}
+
+func testBlock(t *testing.T, xor func(c *Cipher, dst, src []byte)) {
        c1a, _ := NewCipher(golden[0].key)
        c1b, _ := NewCipher(golden[1].key)
        data1 := make([]byte, 1<<20)
        for i := range data1 {
-               c1a.XORKeyStream(data1[i:i+1], data1[i:i+1])
-               c1b.XORKeyStream(data1[i:i+1], data1[i:i+1])
+               xor(c1a, data1[i:i+1], data1[i:i+1])
+               xor(c1b, data1[i:i+1], data1[i:i+1])
        }
 
        c2a, _ := NewCipher(golden[0].key)
        c2b, _ := NewCipher(golden[1].key)
        data2 := make([]byte, 1<<20)
-       c2a.XORKeyStream(data2, data2)
-       c2b.XORKeyStream(data2, data2)
+       xor(c2a, data2, data2)
+       xor(c2b, data2, data2)
 
        if !bytes.Equal(data1, data2) {
                t.Fatalf("bad block")