diff options
| author | Manuel Sabin <msabin27@gmail.com> | 2024-06-27 15:06:55 -0400 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-08-02 14:54:04 +0000 |
| commit | c5f2e4e56e2d3b13737de597be62f3020d59c137 (patch) | |
| tree | f5cd3b39bb71f1567c691ff8d501ad345c2e15d9 /src/crypto/cipher | |
| parent | a9ad410801a762282eddad132c5346462881a2cb (diff) | |
| download | go-c5f2e4e56e2d3b13737de597be62f3020d59c137.tar.xz | |
crypto/internal/cryptotest: add tests for the cipher.Stream interface
This CL creates tests for the cipher.Stream interface in the new
cryptotest package. This set of tests is called from the tests of
implementations of the Stream interface e.g. ctr_test.go, ofb_test.go,
rc4_test.go, etc.
Updates #25309
Change-Id: I57204ef9f4c0ec09b94e88466deb03c6715e411d
Reviewed-on: https://go-review.googlesource.com/c/go/+/595564
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Russell Webb <russell.webb@protonmail.com>
Diffstat (limited to 'src/crypto/cipher')
| -rw-r--r-- | src/crypto/cipher/cfb_test.go | 47 | ||||
| -rw-r--r-- | src/crypto/cipher/ctr_test.go | 38 | ||||
| -rw-r--r-- | src/crypto/cipher/ofb_test.go | 37 |
3 files changed, 122 insertions, 0 deletions
diff --git a/src/crypto/cipher/cfb_test.go b/src/crypto/cipher/cfb_test.go index 72f62e69d3..67033d9a3b 100644 --- a/src/crypto/cipher/cfb_test.go +++ b/src/crypto/cipher/cfb_test.go @@ -8,8 +8,11 @@ import ( "bytes" "crypto/aes" "crypto/cipher" + "crypto/des" + "crypto/internal/cryptotest" "crypto/rand" "encoding/hex" + "fmt" "testing" ) @@ -111,3 +114,47 @@ func TestCFBInverse(t *testing.T) { t.Errorf("got: %x, want: %x", plaintextCopy, plaintext) } } + +func TestCFBStream(t *testing.T) { + + for _, keylen := range []int{128, 192, 256} { + + t.Run(fmt.Sprintf("AES-%d", keylen), func(t *testing.T) { + rng := newRandReader(t) + + key := make([]byte, keylen/8) + rng.Read(key) + + block, err := aes.NewCipher(key) + if err != nil { + panic(err) + } + + t.Run("Encrypter", func(t *testing.T) { + cryptotest.TestStreamFromBlock(t, block, cipher.NewCFBEncrypter) + }) + t.Run("Decrypter", func(t *testing.T) { + cryptotest.TestStreamFromBlock(t, block, cipher.NewCFBDecrypter) + }) + }) + } + + t.Run("DES", func(t *testing.T) { + rng := newRandReader(t) + + key := make([]byte, 8) + rng.Read(key) + + block, err := des.NewCipher(key) + if err != nil { + panic(err) + } + + t.Run("Encrypter", func(t *testing.T) { + cryptotest.TestStreamFromBlock(t, block, cipher.NewCFBEncrypter) + }) + t.Run("Decrypter", func(t *testing.T) { + cryptotest.TestStreamFromBlock(t, block, cipher.NewCFBDecrypter) + }) + }) +} diff --git a/src/crypto/cipher/ctr_test.go b/src/crypto/cipher/ctr_test.go index e5cce576c7..4bb9deab80 100644 --- a/src/crypto/cipher/ctr_test.go +++ b/src/crypto/cipher/ctr_test.go @@ -6,7 +6,11 @@ package cipher_test import ( "bytes" + "crypto/aes" "crypto/cipher" + "crypto/des" + "crypto/internal/cryptotest" + "fmt" "testing" ) @@ -53,3 +57,37 @@ func TestCTR(t *testing.T) { } } } + +func TestCTRStream(t *testing.T) { + + for _, keylen := range []int{128, 192, 256} { + + t.Run(fmt.Sprintf("AES-%d", keylen), func(t *testing.T) { + rng := newRandReader(t) + + key := make([]byte, keylen/8) + rng.Read(key) + + block, err := aes.NewCipher(key) + if err != nil { + panic(err) + } + + cryptotest.TestStreamFromBlock(t, block, cipher.NewCTR) + }) + } + + t.Run("DES", func(t *testing.T) { + rng := newRandReader(t) + + key := make([]byte, 8) + rng.Read(key) + + block, err := des.NewCipher(key) + if err != nil { + panic(err) + } + + cryptotest.TestStreamFromBlock(t, block, cipher.NewCTR) + }) +} diff --git a/src/crypto/cipher/ofb_test.go b/src/crypto/cipher/ofb_test.go index 8d3c5d3a38..036b76c45c 100644 --- a/src/crypto/cipher/ofb_test.go +++ b/src/crypto/cipher/ofb_test.go @@ -14,6 +14,9 @@ import ( "bytes" "crypto/aes" "crypto/cipher" + "crypto/des" + "crypto/internal/cryptotest" + "fmt" "testing" ) @@ -100,3 +103,37 @@ func TestOFB(t *testing.T) { } } } + +func TestOFBStream(t *testing.T) { + + for _, keylen := range []int{128, 192, 256} { + + t.Run(fmt.Sprintf("AES-%d", keylen), func(t *testing.T) { + rng := newRandReader(t) + + key := make([]byte, keylen/8) + rng.Read(key) + + block, err := aes.NewCipher(key) + if err != nil { + panic(err) + } + + cryptotest.TestStreamFromBlock(t, block, cipher.NewOFB) + }) + } + + t.Run("DES", func(t *testing.T) { + rng := newRandReader(t) + + key := make([]byte, 8) + rng.Read(key) + + block, err := des.NewCipher(key) + if err != nil { + panic(err) + } + + cryptotest.TestStreamFromBlock(t, block, cipher.NewOFB) + }) +} |
