Source file src/crypto/cipher/cbc_test.go

     1  // Copyright 2024 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  	"crypto/aes"
     9  	"crypto/cipher"
    10  	"crypto/des"
    11  	"crypto/internal/cryptotest"
    12  	"fmt"
    13  	"io"
    14  	"math/rand"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  // Test CBC Blockmode against the general cipher.BlockMode interface tester
    20  func TestCBCBlockMode(t *testing.T) {
    21  	for _, keylen := range []int{128, 192, 256} {
    22  
    23  		t.Run(fmt.Sprintf("AES-%d", keylen), func(t *testing.T) {
    24  			rng := newRandReader(t)
    25  
    26  			key := make([]byte, keylen/8)
    27  			rng.Read(key)
    28  
    29  			block, err := aes.NewCipher(key)
    30  			if err != nil {
    31  				panic(err)
    32  			}
    33  
    34  			cryptotest.TestBlockMode(t, block, cipher.NewCBCEncrypter, cipher.NewCBCDecrypter)
    35  		})
    36  	}
    37  
    38  	t.Run("DES", func(t *testing.T) {
    39  		rng := newRandReader(t)
    40  
    41  		key := make([]byte, 8)
    42  		rng.Read(key)
    43  
    44  		block, err := des.NewCipher(key)
    45  		if err != nil {
    46  			panic(err)
    47  		}
    48  
    49  		cryptotest.TestBlockMode(t, block, cipher.NewCBCEncrypter, cipher.NewCBCDecrypter)
    50  	})
    51  }
    52  
    53  func newRandReader(t *testing.T) io.Reader {
    54  	seed := time.Now().UnixNano()
    55  	t.Logf("Deterministic RNG seed: 0x%x", seed)
    56  	return rand.New(rand.NewSource(seed))
    57  }
    58  

View as plain text