1
+ // +build go1.9
2
+
1
3
package s3crypto
2
4
3
5
import (
4
6
"bytes"
5
7
"encoding/hex"
8
+ "encoding/json"
6
9
"fmt"
7
10
"io"
8
11
"io/ioutil"
@@ -15,7 +18,7 @@ func TestAES_GCM_NIST_gcmEncryptExtIV256_PTLen_128_Test_0(t *testing.T) {
15
18
iv , _ := hex .DecodeString ("0d18e06c7c725ac9e362e1ce" )
16
19
key , _ := hex .DecodeString ("31bdadd96698c204aa9ce1448ea94ae1fb4a9a0b3c9d773b51bb1822666b8f22" )
17
20
plaintext , _ := hex .DecodeString ("2db5168e932556f8089a0622981d017d" )
18
- expected , _ := hex .DecodeString ("fa4362189661d163fcd6a56d8bf0405ad636ac1bbedd5cc3ee727dc2ab4a9489 " )
21
+ expected , _ := hex .DecodeString ("fa4362189661d163fcd6a56d8bf0405a " )
19
22
tag , _ := hex .DecodeString ("d636ac1bbedd5cc3ee727dc2ab4a9489" )
20
23
aesgcmTest (t , iv , key , plaintext , expected , tag )
21
24
}
@@ -24,7 +27,7 @@ func TestAES_GCM_NIST_gcmEncryptExtIV256_PTLen_104_Test_3(t *testing.T) {
24
27
iv , _ := hex .DecodeString ("4742357c335913153ff0eb0f" )
25
28
key , _ := hex .DecodeString ("e5a0eb92cc2b064e1bc80891faf1fab5e9a17a9c3a984e25416720e30e6c2b21" )
26
29
plaintext , _ := hex .DecodeString ("8499893e16b0ba8b007d54665a" )
27
- expected , _ := hex .DecodeString ("eb8e6175f1fe38eb1acf95fd5188a8b74bb74fda553e91020a23deed45 " )
30
+ expected , _ := hex .DecodeString ("eb8e6175f1fe38eb1acf95fd51 " )
28
31
tag , _ := hex .DecodeString ("88a8b74bb74fda553e91020a23deed45" )
29
32
aesgcmTest (t , iv , key , plaintext , expected , tag )
30
33
}
@@ -33,7 +36,7 @@ func TestAES_GCM_NIST_gcmEncryptExtIV256_PTLen_256_Test_6(t *testing.T) {
33
36
iv , _ := hex .DecodeString ("a291484c3de8bec6b47f525f" )
34
37
key , _ := hex .DecodeString ("37f39137416bafde6f75022a7a527cc593b6000a83ff51ec04871a0ff5360e4e" )
35
38
plaintext , _ := hex .DecodeString ("fafd94cede8b5a0730394bec68a8e77dba288d6ccaa8e1563a81d6e7ccc7fc97" )
36
- expected , _ := hex .DecodeString ("44dc868006b21d49284016565ffb3979cc4271d967628bf7cdaf86db888e92e501a2b578aa2f41ec6379a44a31cc019c " )
39
+ expected , _ := hex .DecodeString ("44dc868006b21d49284016565ffb3979cc4271d967628bf7cdaf86db888e92e5 " )
37
40
tag , _ := hex .DecodeString ("01a2b578aa2f41ec6379a44a31cc019c" )
38
41
aesgcmTest (t , iv , key , plaintext , expected , tag )
39
42
}
@@ -42,11 +45,62 @@ func TestAES_GCM_NIST_gcmEncryptExtIV256_PTLen_408_Test_8(t *testing.T) {
42
45
iv , _ := hex .DecodeString ("92f258071d79af3e63672285" )
43
46
key , _ := hex .DecodeString ("595f259c55abe00ae07535ca5d9b09d6efb9f7e9abb64605c337acbd6b14fc7e" )
44
47
plaintext , _ := hex .DecodeString ("a6fee33eb110a2d769bbc52b0f36969c287874f665681477a25fc4c48015c541fbe2394133ba490a34ee2dd67b898177849a91" )
45
- expected , _ := hex .DecodeString ("bbca4a9e09ae9690c0f6f8d405e53dccd666aa9c5fa13c8758bc30abe1ddd1bcce0d36a1eaaaaffef20cd3c5970b9673f8a65c26ccecb9976fd6ac9c2c0f372c52c821 " )
48
+ expected , _ := hex .DecodeString ("bbca4a9e09ae9690c0f6f8d405e53dccd666aa9c5fa13c8758bc30abe1ddd1bcce0d36a1eaaaaffef20cd3c5970b9673f8a65c " )
46
49
tag , _ := hex .DecodeString ("26ccecb9976fd6ac9c2c0f372c52c821" )
47
50
aesgcmTest (t , iv , key , plaintext , expected , tag )
48
51
}
49
52
53
+ type KAT struct {
54
+ IV string `json:"iv"`
55
+ Key string `json:"key"`
56
+ Plaintext string `json:"pt"`
57
+ AAD string `json:"aad"`
58
+ CipherText string `json:"ct"`
59
+ Tag string `json:"tag"`
60
+ }
61
+
62
+ func TestAES_GCM_KATS (t * testing.T ) {
63
+ fileContents , err := ioutil .ReadFile ("testdata/aes_gcm.json" )
64
+ if err != nil {
65
+ t .Fatalf ("failed to read KAT file: %v" , err )
66
+ }
67
+
68
+ var kats []KAT
69
+ err = json .Unmarshal (fileContents , & kats )
70
+ if err != nil {
71
+ t .Fatalf ("failed to unmarshal KAT json file: %v" , err )
72
+ }
73
+
74
+ for i , kat := range kats {
75
+ t .Run (fmt .Sprintf ("Case%d" , i ), func (t * testing.T ) {
76
+ if len (kat .AAD ) > 0 {
77
+ t .Skip ("Skipping... SDK implementation does not expose additional authenticated data" )
78
+ }
79
+ iv , err := hex .DecodeString (kat .IV )
80
+ if err != nil {
81
+ t .Fatalf ("failed to decode iv: %v" , err )
82
+ }
83
+ key , err := hex .DecodeString (kat .Key )
84
+ if err != nil {
85
+ t .Fatalf ("failed to decode key: %v" , err )
86
+ }
87
+ plaintext , err := hex .DecodeString (kat .Plaintext )
88
+ if err != nil {
89
+ t .Fatalf ("failed to decode plaintext: %v" , err )
90
+ }
91
+ ciphertext , err := hex .DecodeString (kat .CipherText )
92
+ if err != nil {
93
+ t .Fatalf ("failed to decode ciphertext: %v" , err )
94
+ }
95
+ tag , err := hex .DecodeString (kat .Tag )
96
+ if err != nil {
97
+ t .Fatalf ("failed to decode tag: %v" , err )
98
+ }
99
+ aesgcmTest (t , iv , key , plaintext , ciphertext , tag )
100
+ })
101
+ }
102
+ }
103
+
50
104
func TestGCMEncryptReader_SourceError (t * testing.T ) {
51
105
gcm := & gcmEncryptReader {
52
106
encrypter : & mockCipherAEAD {},
@@ -105,6 +159,8 @@ func TestGCMDecryptReader_DecrypterOpenError(t *testing.T) {
105
159
}
106
160
107
161
func aesgcmTest (t * testing.T , iv , key , plaintext , expected , tag []byte ) {
162
+ t .Helper ()
163
+ const gcmTagSize = 16
108
164
cd := CipherData {
109
165
Key : key ,
110
166
IV : iv ,
@@ -122,11 +178,11 @@ func aesgcmTest(t *testing.T, iv, key, plaintext, expected, tag []byte) {
122
178
}
123
179
124
180
// splitting tag and ciphertext
125
- etag := ciphertext [len (ciphertext )- 16 :]
181
+ etag := ciphertext [len (ciphertext )- gcmTagSize :]
126
182
if ! bytes .Equal (etag , tag ) {
127
183
t .Errorf ("expected tags to be equivalent" )
128
184
}
129
- if ! bytes .Equal (ciphertext , expected ) {
185
+ if ! bytes .Equal (ciphertext [: len ( ciphertext ) - gcmTagSize ] , expected ) {
130
186
t .Errorf ("expected ciphertext to be equivalent" )
131
187
}
132
188
0 commit comments