]> Cypherpunks repositories - gostls13.git/commitdiff
crypto: switch block ciphers to detination first.
authorAdam Langley <agl@golang.org>
Wed, 3 Nov 2010 14:43:43 +0000 (10:43 -0400)
committerAdam Langley <agl@golang.org>
Wed, 3 Nov 2010 14:43:43 +0000 (10:43 -0400)
Previously all the functions took two arguments: src, dst. This is the
reverse of the usual Go style and worth changing sooner rather than
later.

Unfortunately, this is a change that the type system doesn't help
with. However, it's not a subtle change: any unittest worth the name
should catch this.

R=rsc, r
CC=golang-dev
https://golang.org/cl/2751042

14 files changed:
src/pkg/crypto/aes/aes_test.go
src/pkg/crypto/aes/block.go
src/pkg/crypto/aes/cipher.go
src/pkg/crypto/block/cbc.go
src/pkg/crypto/block/cfb.go
src/pkg/crypto/block/cipher.go
src/pkg/crypto/block/cmac.go
src/pkg/crypto/block/ctr.go
src/pkg/crypto/block/ecb_test.go
src/pkg/crypto/blowfish/blowfish_test.go
src/pkg/crypto/blowfish/cipher.go
src/pkg/crypto/xtea/block.go
src/pkg/crypto/xtea/cipher.go
src/pkg/crypto/xtea/xtea_test.go

index f8cec0366afe77aa06deec36c4f6ab2299d28797..2136d447d0163ea2bd29732ab2dca20034364e4e 100644 (file)
@@ -283,7 +283,7 @@ func TestEncryptBlock(t *testing.T) {
                dec := make([]uint32, n)
                expandKey(tt.key, enc, dec)
                out := make([]byte, len(tt.in))
-               encryptBlock(enc, tt.in, out)
+               encryptBlock(enc, out, tt.in)
                for j, v := range out {
                        if v != tt.out[j] {
                                t.Errorf("encryptBlock %d: out[%d] = %#x, want %#x", i, j, v, tt.out[j])
@@ -301,7 +301,7 @@ func TestDecryptBlock(t *testing.T) {
                dec := make([]uint32, n)
                expandKey(tt.key, enc, dec)
                plain := make([]byte, len(tt.in))
-               decryptBlock(dec, tt.out, plain)
+               decryptBlock(dec, plain, tt.out)
                for j, v := range plain {
                        if v != tt.in[j] {
                                t.Errorf("decryptBlock %d: plain[%d] = %#x, want %#x", i, j, v, tt.in[j])
@@ -320,7 +320,7 @@ func TestCipherEncrypt(t *testing.T) {
                        continue
                }
                out := make([]byte, len(tt.in))
-               c.Encrypt(tt.in, out)
+               c.Encrypt(out, tt.in)
                for j, v := range out {
                        if v != tt.out[j] {
                                t.Errorf("Cipher.Encrypt %d: out[%d] = %#x, want %#x", i, j, v, tt.out[j])
@@ -339,7 +339,7 @@ func TestCipherDecrypt(t *testing.T) {
                        continue
                }
                plain := make([]byte, len(tt.in))
-               c.Decrypt(tt.out, plain)
+               c.Decrypt(plain, tt.out)
                for j, v := range plain {
                        if v != tt.in[j] {
                                t.Errorf("decryptBlock %d: plain[%d] = %#x, want %#x", i, j, v, tt.in[j])
index a502554bd5b48b7f00893d7c4ac5bce592d65c7f..130cd011c946e1bc35574cbbd513d0c0798124a2 100644 (file)
@@ -37,7 +37,7 @@
 package aes
 
 // Encrypt one block from src into dst, using the expanded key xk.
-func encryptBlock(xk []uint32, src, dst []byte) {
+func encryptBlock(xk []uint32, dst, src []byte) {
        var s0, s1, s2, s3, t0, t1, t2, t3 uint32
 
        s0 = uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
@@ -82,7 +82,7 @@ func encryptBlock(xk []uint32, src, dst []byte) {
 }
 
 // Decrypt one block from src into dst, using the expanded key xk.
-func decryptBlock(xk []uint32, src, dst []byte) {
+func decryptBlock(xk []uint32, dst, src []byte) {
        var s0, s1, s2, s3, t0, t1, t2, t3 uint32
 
        s0 = uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
index 44e905e013c8756eae058618b96b4dd40bc957f9..3a9d0231845eeeec08b2fc500b32784bcfb24a21 100644 (file)
@@ -53,11 +53,11 @@ func (c *Cipher) BlockSize() int { return BlockSize }
 // Note that for amounts of data larger than a block,
 // it is not safe to just call Encrypt on successive blocks;
 // instead, use an encryption mode like CBC (see crypto/block/cbc.go).
-func (c *Cipher) Encrypt(src, dst []byte) { encryptBlock(c.enc, src, dst) }
+func (c *Cipher) Encrypt(dst, src []byte) { encryptBlock(c.enc, dst, src) }
 
 // Decrypt decrypts the 16-byte buffer src using the key k
 // and stores the result in dst.
-func (c *Cipher) Decrypt(src, dst []byte) { decryptBlock(c.dec, src, dst) }
+func (c *Cipher) Decrypt(dst, src []byte) { decryptBlock(c.dec, dst, src) }
 
 // Reset zeros the key data, so that it will no longer
 // appear in the process's memory.
index b0b8bf638e69e5444e84885970e311bf15c260c2..23229c09f70681f97d42563f9efb282b2b7e227f 100644 (file)
@@ -34,7 +34,7 @@ func newCBC(c Cipher, iv []byte) *cbcCipher {
 
 func (x *cbcCipher) BlockSize() int { return x.blockSize }
 
-func (x *cbcCipher) Encrypt(src, dst []byte) {
+func (x *cbcCipher) Encrypt(dst, src []byte) {
        for i := 0; i < x.blockSize; i++ {
                x.iv[i] ^= src[i]
        }
@@ -44,8 +44,8 @@ func (x *cbcCipher) Encrypt(src, dst []byte) {
        }
 }
 
-func (x *cbcCipher) Decrypt(src, dst []byte) {
-       x.c.Decrypt(src, x.tmp)
+func (x *cbcCipher) Decrypt(dst, src []byte) {
+       x.c.Decrypt(x.tmp, src)
        for i := 0; i < x.blockSize; i++ {
                x.tmp[i] ^= x.iv[i]
                x.iv[i] = src[i]
index 2c84b32c5fc97403ce5de4d071632cf767315c96..f20c0a04f66819274fba8f31b93170ee506b9af0 100644 (file)
@@ -40,9 +40,9 @@ func newCFB(c Cipher, s int, iv []byte) *cfbCipher {
 
 func (x *cfbCipher) BlockSize() int { return x.blockSize }
 
-func (x *cfbCipher) Encrypt(src, dst []byte) {
+func (x *cfbCipher) Encrypt(dst, src []byte) {
        // Encrypt old IV and xor prefix with src to make dst.
-       x.c.Encrypt(x.iv, x.tmp)
+       x.c.Encrypt(x.tmp, x.iv)
        for i := 0; i < x.blockSize; i++ {
                dst[i] = src[i] ^ x.tmp[i]
        }
@@ -57,9 +57,9 @@ func (x *cfbCipher) Encrypt(src, dst []byte) {
        }
 }
 
-func (x *cfbCipher) Decrypt(src, dst []byte) {
+func (x *cfbCipher) Decrypt(dst, src []byte) {
        // Encrypt [sic] old IV and xor prefix with src to make dst.
-       x.c.Encrypt(x.iv, x.tmp)
+       x.c.Encrypt(x.tmp, x.iv)
        for i := 0; i < x.blockSize; i++ {
                dst[i] = src[i] ^ x.tmp[i]
        }
index f95c7a76e319d9e580460eaccb73d3131e23deaa..a50d05c2942b258e6226ba8be50d15301b6b96f3 100644 (file)
@@ -18,16 +18,16 @@ type Cipher interface {
 
        // Encrypt encrypts the first block in src into dst.
        // Src and dst may point at the same memory.
-       Encrypt(src, dst []byte)
+       Encrypt(dst, src []byte)
 
        // Decrypt decrypts the first block in src into dst.
        // Src and dst may point at the same memory.
-       Decrypt(src, dst []byte)
+       Decrypt(dst, src []byte)
 }
 
 // Utility routines
 
-func shift1(src, dst []byte) byte {
+func shift1(dst, src []byte) byte {
        var b byte
        for i := len(src) - 1; i >= 0; i-- {
                bb := src[i] >> 7
index 6082299ab5e4a95b1b8b57c6255e64bf298438bd..b85cde72e1213d2fd7d599a49fc171b288ed2fc6 100644 (file)
@@ -52,7 +52,7 @@ func NewCMAC(c Cipher) hash.Hash {
        if shift1(d.k1, d.k1) != 0 {
                d.k1[n-1] ^= r
        }
-       if shift1(d.k1, d.k2) != 0 {
+       if shift1(d.k2, d.k1) != 0 {
                d.k2[n-1] ^= r
        }
 
index bb9aaaaa0ab9305f8e8565edf05ca0a415714bb0..5d65c0c9a9e9e0ef50bae8200483b07debc02c12 100644 (file)
@@ -32,7 +32,7 @@ func newCTRStream(c Cipher, ctr []byte) *ctrStream {
 
 func (x *ctrStream) Next() []byte {
        // Next block is encryption of counter.
-       x.c.Encrypt(x.ctr, x.out)
+       x.c.Encrypt(x.out, x.ctr)
 
        // Increment counter
        for i := len(x.ctr) - 1; i >= 0; i-- {
index 1e991e1dde0640eb9824dae457e76939ae91b4b3..6f79d929a6e56511e73b0c5a1daffce84a6972ff 100644 (file)
@@ -22,7 +22,7 @@ type IncCipher struct {
 
 func (c *IncCipher) BlockSize() int { return c.blockSize }
 
-func (c *IncCipher) Encrypt(src, dst []byte) {
+func (c *IncCipher) Encrypt(dst, src []byte) {
        if !c.encrypting {
                panic("encrypt: not encrypting")
        }
@@ -35,7 +35,7 @@ func (c *IncCipher) Encrypt(src, dst []byte) {
        }
 }
 
-func (c *IncCipher) Decrypt(src, dst []byte) {
+func (c *IncCipher) Decrypt(dst, src []byte) {
        if c.encrypting {
                panic("decrypt: not decrypting")
        }
index 7f510f7fdb1dd7e26c605fe27f34dc4f5e57ece5..3a7ab6c2a8d4eacf5f37256184a702e21fd79a2b 100644 (file)
@@ -163,7 +163,7 @@ func TestCipherEncrypt(t *testing.T) {
                        continue
                }
                ct := make([]byte, len(tt.out))
-               c.Encrypt(tt.in, ct)
+               c.Encrypt(ct, tt.in)
                for j, v := range ct {
                        if v != tt.out[j] {
                                t.Errorf("Cipher.Encrypt, test vector #%d: cipher-text[%d] = %#x, expected %#x", i, j, v, tt.out[j])
@@ -181,7 +181,7 @@ func TestCipherDecrypt(t *testing.T) {
                        continue
                }
                pt := make([]byte, len(tt.in))
-               c.Decrypt(tt.out, pt)
+               c.Decrypt(pt, tt.out)
                for j, v := range pt {
                        if v != tt.in[j] {
                                t.Errorf("Cipher.Decrypt, test vector #%d: plain-text[%d] = %#x, expected %#x", i, j, v, tt.in[j])
index ee0def85e565c0fbb154a51119f6ba80a1eec082..947f762d8bc54d1c025242eba07d3117d9e65d9d 100644 (file)
@@ -50,7 +50,7 @@ func (c *Cipher) BlockSize() int { return BlockSize }
 // Note that for amounts of data larger than a block,
 // it is not safe to just call Encrypt on successive blocks;
 // instead, use an encryption mode like CBC (see crypto/block/cbc.go).
-func (c *Cipher) Encrypt(src, dst []byte) {
+func (c *Cipher) Encrypt(dst, src []byte) {
        l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
        r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
        l, r = encryptBlock(l, r, c)
@@ -60,7 +60,7 @@ func (c *Cipher) Encrypt(src, dst []byte) {
 
 // Decrypt decrypts the 8-byte buffer src using the key k
 // and stores the result in dst.
-func (c *Cipher) Decrypt(src, dst []byte) {
+func (c *Cipher) Decrypt(dst, src []byte) {
        l := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
        r := uint32(src[4])<<24 | uint32(src[5])<<16 | uint32(src[6])<<8 | uint32(src[7])
        l, r = decryptBlock(l, r, c)
index dfb82e1e2f80f44e977cb35322a9472f02e93287..3ac36d038f85cd6c2fc3746dcb85bc5a0dc1a849 100644 (file)
@@ -36,7 +36,7 @@ func uint32ToBlock(v0, v1 uint32, dst []byte) {
 }
 
 // encryptBlock encrypts a single 8 byte block using XTEA.
-func encryptBlock(c *Cipher, src, dst []byte) {
+func encryptBlock(c *Cipher, dst, src []byte) {
        v0, v1 := blockToUint32(src)
 
        // Two rounds of XTEA applied per loop
@@ -51,7 +51,7 @@ func encryptBlock(c *Cipher, src, dst []byte) {
 }
 
 // decryptBlock decrypt a single 8 byte block using XTEA.
-func decryptBlock(c *Cipher, src, dst []byte) {
+func decryptBlock(c *Cipher, dst, src []byte) {
        v0, v1 := blockToUint32(src)
 
        // Two rounds of XTEA applied per loop
index 144fe9434bc425d347efa732cab7398a699f7183..b0fa2a1844d849d3278e3e5fb5772254f9371eb0 100644 (file)
@@ -55,10 +55,10 @@ func (c *Cipher) BlockSize() int { return BlockSize }
 // Note that for amounts of data larger than a block,
 // it is not safe to just call Encrypt on successive blocks;
 // instead, use an encryption mode like CBC (see crypto/block/cbc.go).
-func (c *Cipher) Encrypt(src, dst []byte) { encryptBlock(c, src, dst) }
+func (c *Cipher) Encrypt(dst, src []byte) { encryptBlock(c, dst, src) }
 
 // Decrypt decrypts the 8 byte buffer src using the key k and stores the result in dst.
-func (c *Cipher) Decrypt(src, dst []byte) { decryptBlock(c, src, dst) }
+func (c *Cipher) Decrypt(dst, src []byte) { decryptBlock(c, dst, src) }
 
 // Reset zeros the table, so that it will no longer appear in the process's memory.
 func (c *Cipher) Reset() {
index 7fe3468f150879984657e6d278f80a9dd0c71c77..03934f1695edf17d425c99b710135bdf5928e518 100644 (file)
@@ -94,7 +94,7 @@ func TestEncodeDecode(t *testing.T) {
        }
 
        // Encrypt the input block
-       c.Encrypt(input, output)
+       c.Encrypt(output, input)
 
        // Check that the output does not match the input
        differs := false
@@ -112,7 +112,7 @@ func TestEncodeDecode(t *testing.T) {
        // Decrypt the block we just encrypted
        input = output
        output = make([]byte, BlockSize)
-       c.Decrypt(input, output)
+       c.Decrypt(output, input)
 
        // Check that the output from decrypt matches our initial input
        for i := 0; i < len(input); i++ {
@@ -196,7 +196,7 @@ func TestCipherEncrypt(t *testing.T) {
                }
 
                out := make([]byte, len(tt.plainText))
-               c.Encrypt(tt.plainText, out)
+               c.Encrypt(out, tt.plainText)
 
                for j := 0; j < len(out); j++ {
                        if out[j] != tt.cipherText[j] {
@@ -217,7 +217,7 @@ func TestCipherDecrypt(t *testing.T) {
                }
 
                out := make([]byte, len(tt.cipherText))
-               c.Decrypt(tt.cipherText, out)
+               c.Decrypt(out, tt.cipherText)
 
                for j := 0; j < len(out); j++ {
                        if out[j] != tt.plainText[j] {