|
5 | 5 | package cipher_test
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "bytes" |
8 | 9 | "crypto/aes"
|
9 | 10 | "crypto/cipher"
|
| 11 | + "crypto/des" |
10 | 12 | "testing"
|
11 | 13 | )
|
12 | 14 |
|
@@ -34,3 +36,55 @@ func mustPanic(t *testing.T, msg string, f func()) {
|
34 | 36 | }()
|
35 | 37 | f()
|
36 | 38 | }
|
| 39 | + |
| 40 | +func TestEmptyPlaintext(t *testing.T) { |
| 41 | + var key [16]byte |
| 42 | + a, err := aes.NewCipher(key[:16]) |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + d, err := des.NewCipher(key[:8]) |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + |
| 51 | + s := 16 |
| 52 | + pt := make([]byte, s) |
| 53 | + ct := make([]byte, s) |
| 54 | + for i := 0; i < 16; i++ { |
| 55 | + pt[i], ct[i] = byte(i), byte(i) |
| 56 | + } |
| 57 | + |
| 58 | + assertEqual := func(name string, got, want []byte) { |
| 59 | + if !bytes.Equal(got, want) { |
| 60 | + t.Fatalf("%s: got %v, want %v", name, got, want) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + for _, b := range []cipher.Block{a, d} { |
| 65 | + iv := make([]byte, b.BlockSize()) |
| 66 | + cbce := cipher.NewCBCEncrypter(b, iv) |
| 67 | + cbce.CryptBlocks(ct, pt[:0]) |
| 68 | + assertEqual("CBC encrypt", ct, pt) |
| 69 | + |
| 70 | + cbcd := cipher.NewCBCDecrypter(b, iv) |
| 71 | + cbcd.CryptBlocks(ct, pt[:0]) |
| 72 | + assertEqual("CBC decrypt", ct, pt) |
| 73 | + |
| 74 | + cfbe := cipher.NewCFBEncrypter(b, iv) |
| 75 | + cfbe.XORKeyStream(ct, pt[:0]) |
| 76 | + assertEqual("CFB encrypt", ct, pt) |
| 77 | + |
| 78 | + cfbd := cipher.NewCFBDecrypter(b, iv) |
| 79 | + cfbd.XORKeyStream(ct, pt[:0]) |
| 80 | + assertEqual("CFB decrypt", ct, pt) |
| 81 | + |
| 82 | + ctr := cipher.NewCTR(b, iv) |
| 83 | + ctr.XORKeyStream(ct, pt[:0]) |
| 84 | + assertEqual("CTR", ct, pt) |
| 85 | + |
| 86 | + ofb := cipher.NewOFB(b, iv) |
| 87 | + ofb.XORKeyStream(ct, pt[:0]) |
| 88 | + assertEqual("OFB", ct, pt) |
| 89 | + } |
| 90 | +} |
0 commit comments