diff options
Diffstat (limited to 'src/crypto/cipher/cipher_test.go')
-rw-r--r-- | src/crypto/cipher/cipher_test.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/crypto/cipher/cipher_test.go b/src/crypto/cipher/cipher_test.go index 1faa7b87e5..4d7cd6b5dd 100644 --- a/src/crypto/cipher/cipher_test.go +++ b/src/crypto/cipher/cipher_test.go @@ -5,8 +5,10 @@ package cipher_test import ( + "bytes" "crypto/aes" "crypto/cipher" + "crypto/des" "testing" ) @@ -34,3 +36,55 @@ func mustPanic(t *testing.T, msg string, f func()) { }() f() } + +func TestEmptyPlaintext(t *testing.T) { + var key [16]byte + a, err := aes.NewCipher(key[:16]) + if err != nil { + t.Fatal(err) + } + d, err := des.NewCipher(key[:8]) + if err != nil { + t.Fatal(err) + } + + s := 16 + pt := make([]byte, s) + ct := make([]byte, s) + for i := 0; i < 16; i++ { + pt[i], ct[i] = byte(i), byte(i) + } + + assertEqual := func(name string, got, want []byte) { + if !bytes.Equal(got, want) { + t.Fatalf("%s: got %v, want %v", name, got, want) + } + } + + for _, b := range []cipher.Block{a, d} { + iv := make([]byte, b.BlockSize()) + cbce := cipher.NewCBCEncrypter(b, iv) + cbce.CryptBlocks(ct, pt[:0]) + assertEqual("CBC encrypt", ct, pt) + + cbcd := cipher.NewCBCDecrypter(b, iv) + cbcd.CryptBlocks(ct, pt[:0]) + assertEqual("CBC decrypt", ct, pt) + + cfbe := cipher.NewCFBEncrypter(b, iv) + cfbe.XORKeyStream(ct, pt[:0]) + assertEqual("CFB encrypt", ct, pt) + + cfbd := cipher.NewCFBDecrypter(b, iv) + cfbd.XORKeyStream(ct, pt[:0]) + assertEqual("CFB decrypt", ct, pt) + + ctr := cipher.NewCTR(b, iv) + ctr.XORKeyStream(ct, pt[:0]) + assertEqual("CTR", ct, pt) + + ofb := cipher.NewOFB(b, iv) + ofb.XORKeyStream(ct, pt[:0]) + assertEqual("OFB", ct, pt) + } +} |