Source file src/crypto/cipher/ctr_test.go

     1  // Copyright 2015 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  	"fmt"
    14  	"testing"
    15  )
    16  
    17  type noopBlock int
    18  
    19  func (b noopBlock) BlockSize() int        { return int(b) }
    20  func (noopBlock) Encrypt(dst, src []byte) { copy(dst, src) }
    21  func (noopBlock) Decrypt(dst, src []byte) { copy(dst, src) }
    22  
    23  func inc(b []byte) {
    24  	for i := len(b) - 1; i >= 0; i++ {
    25  		b[i]++
    26  		if b[i] != 0 {
    27  			break
    28  		}
    29  	}
    30  }
    31  
    32  func xor(a, b []byte) {
    33  	for i := range a {
    34  		a[i] ^= b[i]
    35  	}
    36  }
    37  
    38  func TestCTR(t *testing.T) {
    39  	for size := 64; size <= 1024; size *= 2 {
    40  		iv := make([]byte, size)
    41  		ctr := cipher.NewCTR(noopBlock(size), iv)
    42  		src := make([]byte, 1024)
    43  		for i := range src {
    44  			src[i] = 0xff
    45  		}
    46  		want := make([]byte, 1024)
    47  		copy(want, src)
    48  		counter := make([]byte, size)
    49  		for i := 1; i < len(want)/size; i++ {
    50  			inc(counter)
    51  			xor(want[i*size:(i+1)*size], counter)
    52  		}
    53  		dst := make([]byte, 1024)
    54  		ctr.XORKeyStream(dst, src)
    55  		if !bytes.Equal(dst, want) {
    56  			t.Errorf("for size %d\nhave %x\nwant %x", size, dst, want)
    57  		}
    58  	}
    59  }
    60  
    61  func TestCTRStream(t *testing.T) {
    62  
    63  	for _, keylen := range []int{128, 192, 256} {
    64  
    65  		t.Run(fmt.Sprintf("AES-%d", keylen), func(t *testing.T) {
    66  			rng := newRandReader(t)
    67  
    68  			key := make([]byte, keylen/8)
    69  			rng.Read(key)
    70  
    71  			block, err := aes.NewCipher(key)
    72  			if err != nil {
    73  				panic(err)
    74  			}
    75  
    76  			cryptotest.TestStreamFromBlock(t, block, cipher.NewCTR)
    77  		})
    78  	}
    79  
    80  	t.Run("DES", func(t *testing.T) {
    81  		rng := newRandReader(t)
    82  
    83  		key := make([]byte, 8)
    84  		rng.Read(key)
    85  
    86  		block, err := des.NewCipher(key)
    87  		if err != nil {
    88  			panic(err)
    89  		}
    90  
    91  		cryptotest.TestStreamFromBlock(t, block, cipher.NewCTR)
    92  	})
    93  }
    94  

View as plain text