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
5package aes
6
7import (
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.
17type testInterface interface {
18	InAESPackage() bool
19}
20
21// testBlock implements the cipher.Block interface and any *Able
22// interfaces that need to be tested.
23type testBlock struct{}
24
25func (*testBlock) BlockSize() int      { return 0 }
26func (*testBlock) Encrypt(a, b []byte) {}
27func (*testBlock) Decrypt(a, b []byte) {}
28func (*testBlock) NewGCM(int, int) (cipher.AEAD, error) {
29	return &testAEAD{}, nil
30}
31func (*testBlock) NewCBCEncrypter([]byte) cipher.BlockMode {
32	return &testBlockMode{}
33}
34func (*testBlock) NewCBCDecrypter([]byte) cipher.BlockMode {
35	return &testBlockMode{}
36}
37func (*testBlock) NewCTR([]byte) cipher.Stream {
38	return &testStream{}
39}
40
41// testAEAD implements the cipher.AEAD interface.
42type testAEAD struct{}
43
44func (*testAEAD) NonceSize() int                         { return 0 }
45func (*testAEAD) Overhead() int                          { return 0 }
46func (*testAEAD) Seal(a, b, c, d []byte) []byte          { return []byte{} }
47func (*testAEAD) Open(a, b, c, d []byte) ([]byte, error) { return []byte{}, nil }
48func (*testAEAD) InAESPackage() bool                     { return true }
49
50// Test the gcmAble interface is detected correctly by the cipher package.
51func 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.
66type testBlockMode struct{}
67
68func (*testBlockMode) BlockSize() int          { return 0 }
69func (*testBlockMode) CryptBlocks(a, b []byte) {}
70func (*testBlockMode) InAESPackage() bool      { return true }
71
72// Test the cbcEncAble interface is detected correctly by the cipher package.
73func 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.
85func 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.
97type testStream struct{}
98
99func (*testStream) XORKeyStream(a, b []byte) {}
100func (*testStream) InAESPackage() bool       { return true }
101
102// Test the ctrAble interface is detected correctly by the cipher package.
103func 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