...
Run Format

Source file src/crypto/aes/modes_test.go

Documentation: crypto/aes

     1  // Copyright 2016 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 aes
     6  
     7  import (
     8  	"crypto/cipher"
     9  	"testing"
    10  )
    11  
    12  // Check that the optimized implementations of cipher modes will
    13  // be picked up correctly.
    14  
    15  // testInterface can be asserted to check that a type originates
    16  // from this test group.
    17  type testInterface interface {
    18  	InAESPackage() bool
    19  }
    20  
    21  // testBlock implements the cipher.Block interface and any *Able
    22  // interfaces that need to be tested.
    23  type testBlock struct{}
    24  
    25  func (*testBlock) BlockSize() int      { return 0 }
    26  func (*testBlock) Encrypt(a, b []byte) {}
    27  func (*testBlock) Decrypt(a, b []byte) {}
    28  func (*testBlock) NewGCM(int, int) (cipher.AEAD, error) {
    29  	return &testAEAD{}, nil
    30  }
    31  func (*testBlock) NewCBCEncrypter([]byte) cipher.BlockMode {
    32  	return &testBlockMode{}
    33  }
    34  func (*testBlock) NewCBCDecrypter([]byte) cipher.BlockMode {
    35  	return &testBlockMode{}
    36  }
    37  func (*testBlock) NewCTR([]byte) cipher.Stream {
    38  	return &testStream{}
    39  }
    40  
    41  // testAEAD implements the cipher.AEAD interface.
    42  type testAEAD struct{}
    43  
    44  func (*testAEAD) NonceSize() int                         { return 0 }
    45  func (*testAEAD) Overhead() int                          { return 0 }
    46  func (*testAEAD) Seal(a, b, c, d []byte) []byte          { return []byte{} }
    47  func (*testAEAD) Open(a, b, c, d []byte) ([]byte, error) { return []byte{}, nil }
    48  func (*testAEAD) InAESPackage() bool                     { return true }
    49  
    50  // Test the gcmAble interface is detected correctly by the cipher package.
    51  func TestGCMAble(t *testing.T) {
    52  	b := cipher.Block(&testBlock{})
    53  	if _, ok := b.(gcmAble); !ok {
    54  		t.Fatalf("testBlock does not implement the gcmAble interface")
    55  	}
    56  	aead, err := cipher.NewGCM(b)
    57  	if err != nil {
    58  		t.Fatalf("%v", err)
    59  	}
    60  	if _, ok := aead.(testInterface); !ok {
    61  		t.Fatalf("cipher.NewGCM did not use gcmAble interface")
    62  	}
    63  }
    64  
    65  // testBlockMode implements the cipher.BlockMode interface.
    66  type testBlockMode struct{}
    67  
    68  func (*testBlockMode) BlockSize() int          { return 0 }
    69  func (*testBlockMode) CryptBlocks(a, b []byte) {}
    70  func (*testBlockMode) InAESPackage() bool      { return true }
    71  
    72  // Test the cbcEncAble interface is detected correctly by the cipher package.
    73  func TestCBCEncAble(t *testing.T) {
    74  	b := cipher.Block(&testBlock{})
    75  	if _, ok := b.(cbcEncAble); !ok {
    76  		t.Fatalf("testBlock does not implement the cbcEncAble interface")
    77  	}
    78  	bm := cipher.NewCBCEncrypter(b, []byte{})
    79  	if _, ok := bm.(testInterface); !ok {
    80  		t.Fatalf("cipher.NewCBCEncrypter did not use cbcEncAble interface")
    81  	}
    82  }
    83  
    84  // Test the cbcDecAble interface is detected correctly by the cipher package.
    85  func TestCBCDecAble(t *testing.T) {
    86  	b := cipher.Block(&testBlock{})
    87  	if _, ok := b.(cbcDecAble); !ok {
    88  		t.Fatalf("testBlock does not implement the cbcDecAble interface")
    89  	}
    90  	bm := cipher.NewCBCDecrypter(b, []byte{})
    91  	if _, ok := bm.(testInterface); !ok {
    92  		t.Fatalf("cipher.NewCBCDecrypter did not use cbcDecAble interface")
    93  	}
    94  }
    95  
    96  // testStream implements the cipher.Stream interface.
    97  type testStream struct{}
    98  
    99  func (*testStream) XORKeyStream(a, b []byte) {}
   100  func (*testStream) InAESPackage() bool       { return true }
   101  
   102  // Test the ctrAble interface is detected correctly by the cipher package.
   103  func TestCTRAble(t *testing.T) {
   104  	b := cipher.Block(&testBlock{})
   105  	if _, ok := b.(ctrAble); !ok {
   106  		t.Fatalf("testBlock does not implement the ctrAble interface")
   107  	}
   108  	s := cipher.NewCTR(b, []byte{})
   109  	if _, ok := s.(testInterface); !ok {
   110  		t.Fatalf("cipher.NewCTR did not use ctrAble interface")
   111  	}
   112  }
   113  

View as plain text