-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnative_codec_helpers_test.go
More file actions
219 lines (205 loc) · 8.33 KB
/
Copy pathnative_codec_helpers_test.go
File metadata and controls
219 lines (205 loc) · 8.33 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"bytes"
"fmt"
"testing"
)
func TestDecodeStrictStdBase64AcceptsCanonicalPaddedValues(t *testing.T) {
tests := []struct {
name string
value string
want []byte
}{
{name: "empty", value: "", want: nil},
{name: "one byte", value: "TQ==", want: []byte{'M'}},
{name: "two bytes", value: "TWE=", want: []byte("Ma")},
{name: "three bytes", value: "TWFu", want: []byte("Man")},
{name: "standard symbols", value: "+/8=", want: []byte{0xfb, 0xff}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := decodeStrictStdBase64(tt.value)
if err != nil {
t.Fatalf("case %s encoded length %d: decode returned error", tt.name, len(tt.value))
}
if !bytes.Equal(got, tt.want) {
t.Fatalf("case %s encoded length %d: decoded length %d, want %d", tt.name, len(tt.value), len(got), len(tt.want))
}
})
}
}
func TestDecodeStrictStdBase64RejectsMalformedAndNonCanonicalValues(t *testing.T) {
tests := []struct {
name string
value string
}{
{name: "URL-safe alphabet", value: "-_8="},
{name: "missing double padding", value: "TQ"},
{name: "missing single padding", value: "TWE"},
{name: "carriage return", value: "TQ==\r"},
{name: "line feed", value: "TQ==\n"},
{name: "space", value: "T Q=="},
{name: "trailing bytes", value: "TQ==AAAA"},
{name: "impossible length", value: "A"},
{name: "non-canonical trailing bits", value: "TR=="},
{name: "extra padding", value: "TQ==="},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := decodeStrictStdBase64(tt.value)
if err == nil {
t.Fatalf("case %s encoded length %d: decode succeeded with %d bytes", tt.name, len(tt.value), len(got))
}
if len(got) != 0 {
t.Fatalf("case %s encoded length %d: rejected decode returned %d bytes", tt.name, len(tt.value), len(got))
}
})
}
}
func TestPKCS7PadAndUnpadEveryAESPaddingLength(t *testing.T) {
const blockSize = 16
for plainLen := 0; plainLen < blockSize; plainLen++ {
plainLen := plainLen
t.Run(fmt.Sprintf("plain-length-%d", plainLen), func(t *testing.T) {
plain := bytes.Repeat([]byte{0x5a}, plainLen)
before := append([]byte(nil), plain...)
padded, err := pkcs7Pad(plain, blockSize)
if err != nil {
t.Fatalf("plain length %d: pad returned error", plainLen)
}
wantPadLen := blockSize - plainLen
if len(padded) != blockSize {
t.Fatalf("plain length %d: padded length %d, want %d", plainLen, len(padded), blockSize)
}
for i := plainLen; i < len(padded); i++ {
if padded[i] != byte(wantPadLen) {
t.Fatalf("plain length %d: padding byte %d is invalid", plainLen, i-plainLen)
}
}
if !bytes.Equal(plain, before) {
t.Fatalf("plain length %d: pad mutated input", plainLen)
}
unpadded, err := pkcs7Unpad(padded, blockSize)
if err != nil {
t.Fatalf("plain length %d: unpad returned error", plainLen)
}
if !bytes.Equal(unpadded, plain) {
t.Fatalf("plain length %d: unpadded length %d", plainLen, len(unpadded))
}
})
}
}
func TestPKCS7PadAddsFullBlockForAlignedPlaintext(t *testing.T) {
plain := bytes.Repeat([]byte{0x3c}, 16)
padded, err := pkcs7Pad(plain, 16)
if err != nil {
t.Fatal("aligned plaintext: pad returned error")
}
if len(padded) != 32 {
t.Fatalf("aligned plaintext length %d: padded length %d, want 32", len(plain), len(padded))
}
for i := 16; i < len(padded); i++ {
if padded[i] != 16 {
t.Fatalf("aligned plaintext: padding byte %d is invalid", i-16)
}
}
}
func TestPKCS7RejectsInvalidPaddingAndBlockSizes(t *testing.T) {
invalidPadded := []struct {
name string
value []byte
blockSize int
}{
{name: "empty", value: nil, blockSize: 16},
{name: "zero pad byte", value: make([]byte, 16), blockSize: 16},
{name: "pad exceeds block size", value: bytes.Repeat([]byte{17}, 16), blockSize: 16},
{name: "inconsistent tail", value: append(bytes.Repeat([]byte{0x41}, 14), 1, 2), blockSize: 16},
{name: "non-block-aligned", value: append(bytes.Repeat([]byte{0x41}, 14), 1), blockSize: 16},
{name: "zero block size", value: []byte{1}, blockSize: 0},
{name: "negative block size", value: []byte{1}, blockSize: -1},
{name: "oversized block size", value: bytes.Repeat([]byte{1}, 256), blockSize: 256},
}
for _, tt := range invalidPadded {
t.Run("unpad "+tt.name, func(t *testing.T) {
before := append([]byte(nil), tt.value...)
got, err := pkcs7Unpad(tt.value, tt.blockSize)
if err == nil {
t.Fatalf("case %s length %d block size %d: unpad succeeded with length %d", tt.name, len(tt.value), tt.blockSize, len(got))
}
if len(got) != 0 {
t.Fatalf("case %s length %d block size %d: rejected unpad returned %d bytes", tt.name, len(tt.value), tt.blockSize, len(got))
}
if !bytes.Equal(tt.value, before) {
t.Fatalf("case %s length %d block size %d: unpad mutated input", tt.name, len(tt.value), tt.blockSize)
}
})
}
for _, blockSize := range []int{0, -1, 256} {
if got, err := pkcs7Pad([]byte("synthetic"), blockSize); err == nil || len(got) != 0 {
t.Fatalf("block size %d: pad returned length %d with error presence %t", blockSize, len(got), err != nil)
}
}
}
func TestAESCBCPKCS7RoundTripIsExactAndDoesNotMutateInputs(t *testing.T) {
plain := append([]byte("synthetic-unicode-"), []byte("合成🙂\x00payload")...)
key := []byte("0123456789abcdef")
iv := []byte("fedcba9876543210")
plainBefore := append([]byte(nil), plain...)
keyBefore := append([]byte(nil), key...)
ivBefore := append([]byte(nil), iv...)
ciphertext, err := aesCBCEncryptPKCS7(plain, key, iv)
if err != nil {
t.Fatalf("plain length %d key length %d IV length %d: encrypt returned error", len(plain), len(key), len(iv))
}
if len(ciphertext) == 0 || len(ciphertext)%16 != 0 {
t.Fatalf("plain length %d: ciphertext length %d is not a non-empty block multiple", len(plain), len(ciphertext))
}
if !bytes.Equal(plain, plainBefore) || !bytes.Equal(key, keyBefore) || !bytes.Equal(iv, ivBefore) {
t.Fatalf("encrypt mutated an input (plain=%t key=%t IV=%t)", !bytes.Equal(plain, plainBefore), !bytes.Equal(key, keyBefore), !bytes.Equal(iv, ivBefore))
}
ciphertextBefore := append([]byte(nil), ciphertext...)
decrypted, err := aesCBCDecryptPKCS7(ciphertext, key, iv)
if err != nil {
t.Fatalf("ciphertext length %d key length %d IV length %d: decrypt returned error", len(ciphertext), len(key), len(iv))
}
if !bytes.Equal(decrypted, plain) {
t.Fatalf("round trip lengths decrypted=%d plain=%d", len(decrypted), len(plain))
}
if !bytes.Equal(ciphertext, ciphertextBefore) || !bytes.Equal(key, keyBefore) || !bytes.Equal(iv, ivBefore) {
t.Fatalf("decrypt mutated an input (ciphertext=%t key=%t IV=%t)", !bytes.Equal(ciphertext, ciphertextBefore), !bytes.Equal(key, keyBefore), !bytes.Equal(iv, ivBefore))
}
}
func TestAESCBCPKCS7RejectsInvalidKeyIVAndCiphertextSizes(t *testing.T) {
validKey := []byte("0123456789abcdef")
validIV := []byte("fedcba9876543210")
plain := []byte("synthetic")
for _, keyLen := range []int{0, 15, 17} {
key := bytes.Repeat([]byte{0x4b}, keyLen)
if got, err := aesCBCEncryptPKCS7(plain, key, validIV); err == nil || len(got) != 0 {
t.Fatalf("encrypt key length %d: result length %d error presence %t", keyLen, len(got), err != nil)
}
if got, err := aesCBCDecryptPKCS7(bytes.Repeat([]byte{0x31}, 16), key, validIV); err == nil || len(got) != 0 {
t.Fatalf("decrypt key length %d: result length %d error presence %t", keyLen, len(got), err != nil)
}
}
for _, ivLen := range []int{0, 15, 17} {
iv := bytes.Repeat([]byte{0x49}, ivLen)
if got, err := aesCBCEncryptPKCS7(plain, validKey, iv); err == nil || len(got) != 0 {
t.Fatalf("encrypt IV length %d: result length %d error presence %t", ivLen, len(got), err != nil)
}
if got, err := aesCBCDecryptPKCS7(bytes.Repeat([]byte{0x31}, 16), validKey, iv); err == nil || len(got) != 0 {
t.Fatalf("decrypt IV length %d: result length %d error presence %t", ivLen, len(got), err != nil)
}
}
for _, ciphertextLen := range []int{0, 1, 15, 17, 31} {
ciphertext := bytes.Repeat([]byte{0x43}, ciphertextLen)
before := append([]byte(nil), ciphertext...)
got, err := aesCBCDecryptPKCS7(ciphertext, validKey, validIV)
if err == nil || len(got) != 0 {
t.Fatalf("ciphertext length %d: result length %d error presence %t", ciphertextLen, len(got), err != nil)
}
if !bytes.Equal(ciphertext, before) {
t.Fatalf("ciphertext length %d: rejected decrypt mutated input", ciphertextLen)
}
}
}