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