| 1 |
// Copyright 2010 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 |
|
| 5 |
package cipher_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/aes" |
| 10 |
"crypto/cipher" |
| 11 |
"crypto/des" |
| 12 |
"crypto/internal/cryptotest" |
| 13 |
"crypto/rand" |
| 14 |
"encoding/hex" |
| 15 |
"fmt" |
| 16 |
"testing" |
| 17 |
) |
| 18 |
|
| 19 |
// cfbTests contains the test vectors from |
| 20 |
// https://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, section |
| 21 |
// F.3.13. |
| 22 |
var cfbTests = []struct { |
| 23 |
key, iv, plaintext, ciphertext string |
| 24 |
}{ |
| 25 |
{ |
| 26 |
"2b7e151628aed2a6abf7158809cf4f3c", |
| 27 |
"000102030405060708090a0b0c0d0e0f", |
| 28 |
"6bc1bee22e409f96e93d7e117393172a", |
| 29 |
"3b3fd92eb72dad20333449f8e83cfb4a", |
| 30 |
}, |
| 31 |
{ |
| 32 |
"2b7e151628aed2a6abf7158809cf4f3c", |
| 33 |
"3B3FD92EB72DAD20333449F8E83CFB4A", |
| 34 |
"ae2d8a571e03ac9c9eb76fac45af8e51", |
| 35 |
"c8a64537a0b3a93fcde3cdad9f1ce58b", |
| 36 |
}, |
| 37 |
{ |
| 38 |
"2b7e151628aed2a6abf7158809cf4f3c", |
| 39 |
"C8A64537A0B3A93FCDE3CDAD9F1CE58B", |
| 40 |
"30c81c46a35ce411e5fbc1191a0a52ef", |
| 41 |
"26751f67a3cbb140b1808cf187a4f4df", |
| 42 |
}, |
| 43 |
{ |
| 44 |
"2b7e151628aed2a6abf7158809cf4f3c", |
| 45 |
"26751F67A3CBB140B1808CF187A4F4DF", |
| 46 |
"f69f2445df4f9b17ad2b417be66c3710", |
| 47 |
"c04b05357c5d1c0eeac4c66f9ff7f2e6", |
| 48 |
}, |
| 49 |
} |
| 50 |
|
| 51 |
func TestCFBVectors(t *testing.T) { |
| 52 |
for i, test := range cfbTests { |
| 53 |
key, err := hex.DecodeString(test.key) |
| 54 |
if err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
iv, err := hex.DecodeString(test.iv) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
plaintext, err := hex.DecodeString(test.plaintext) |
| 62 |
if err != nil { |
| 63 |
t.Fatal(err) |
| 64 |
} |
| 65 |
expected, err := hex.DecodeString(test.ciphertext) |
| 66 |
if err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} |
| 69 |
|
| 70 |
block, err := aes.NewCipher(key) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
|
| 75 |
ciphertext := make([]byte, len(plaintext)) |
| 76 |
cfb := cipher.NewCFBEncrypter(block, iv) |
| 77 |
cfb.XORKeyStream(ciphertext, plaintext) |
| 78 |
|
| 79 |
if !bytes.Equal(ciphertext, expected) { |
| 80 |
t.Errorf("#%d: wrong output: got %x, expected %x", i, ciphertext, expected) |
| 81 |
} |
| 82 |
|
| 83 |
cfbdec := cipher.NewCFBDecrypter(block, iv) |
| 84 |
plaintextCopy := make([]byte, len(ciphertext)) |
| 85 |
cfbdec.XORKeyStream(plaintextCopy, ciphertext) |
| 86 |
|
| 87 |
if !bytes.Equal(plaintextCopy, plaintext) { |
| 88 |
t.Errorf("#%d: wrong plaintext: got %x, expected %x", i, plaintextCopy, plaintext) |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestCFBInverse(t *testing.T) { |
| 94 |
block, err := aes.NewCipher(commonKey128) |
| 95 |
if err != nil { |
| 96 |
t.Error(err) |
| 97 |
return |
| 98 |
} |
| 99 |
|
| 100 |
plaintext := []byte("this is the plaintext. this is the plaintext.") |
| 101 |
iv := make([]byte, block.BlockSize()) |
| 102 |
rand.Reader.Read(iv) |
| 103 |
cfb := cipher.NewCFBEncrypter(block, iv) |
| 104 |
ciphertext := make([]byte, len(plaintext)) |
| 105 |
copy(ciphertext, plaintext) |
| 106 |
cfb.XORKeyStream(ciphertext, ciphertext) |
| 107 |
|
| 108 |
cfbdec := cipher.NewCFBDecrypter(block, iv) |
| 109 |
plaintextCopy := make([]byte, len(plaintext)) |
| 110 |
copy(plaintextCopy, ciphertext) |
| 111 |
cfbdec.XORKeyStream(plaintextCopy, plaintextCopy) |
| 112 |
|
| 113 |
if !bytes.Equal(plaintextCopy, plaintext) { |
| 114 |
t.Errorf("got: %x, want: %x", plaintextCopy, plaintext) |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func TestCFBStream(t *testing.T) { |
| 119 |
|
| 120 |
for _, keylen := range []int{128, 192, 256} { |
| 121 |
|
| 122 |
t.Run(fmt.Sprintf("AES-%d", keylen), func(t *testing.T) { |
| 123 |
rng := newRandReader(t) |
| 124 |
|
| 125 |
key := make([]byte, keylen/8) |
| 126 |
rng.Read(key) |
| 127 |
|
| 128 |
block, err := aes.NewCipher(key) |
| 129 |
if err != nil { |
| 130 |
panic(err) |
| 131 |
} |
| 132 |
|
| 133 |
t.Run("Encrypter", func(t *testing.T) { |
| 134 |
cryptotest.TestStreamFromBlock(t, block, cipher.NewCFBEncrypter) |
| 135 |
}) |
| 136 |
t.Run("Decrypter", func(t *testing.T) { |
| 137 |
cryptotest.TestStreamFromBlock(t, block, cipher.NewCFBDecrypter) |
| 138 |
}) |
| 139 |
}) |
| 140 |
} |
| 141 |
|
| 142 |
t.Run("DES", func(t *testing.T) { |
| 143 |
rng := newRandReader(t) |
| 144 |
|
| 145 |
key := make([]byte, 8) |
| 146 |
rng.Read(key) |
| 147 |
|
| 148 |
block, err := des.NewCipher(key) |
| 149 |
if err != nil { |
| 150 |
panic(err) |
| 151 |
} |
| 152 |
|
| 153 |
t.Run("Encrypter", func(t *testing.T) { |
| 154 |
cryptotest.TestStreamFromBlock(t, block, cipher.NewCFBEncrypter) |
| 155 |
}) |
| 156 |
t.Run("Decrypter", func(t *testing.T) { |
| 157 |
cryptotest.TestStreamFromBlock(t, block, cipher.NewCFBDecrypter) |
| 158 |
}) |
| 159 |
}) |
| 160 |
} |