-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt_test.go
More file actions
185 lines (166 loc) · 5.36 KB
/
Copy pathencrypt_test.go
File metadata and controls
185 lines (166 loc) · 5.36 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
// SPDX-FileCopyrightText: Copyright 2026 Securosys SA
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"encoding/base64"
"net/http"
"strings"
"testing"
helpers "github.com/securosys-com/tsb-client-go/helpers"
)
const (
testEncryptPayload = "MDEyMzQ1Njc4OWFiY2RlZg=="
testEncryptAAD = ""
testRSAEncryptKeyLabel = "go-client-test-rsa-encrypt"
testAESEncryptKeyLabel = "go-client-test-aes-encrypt"
testChaCha20EncryptLabel = "go-client-test-chacha20-encrypt"
testCamelliaEncryptLabel = "go-client-test-camellia-encrypt"
testTDEAEncryptKeyLabel = "go-client-test-tdea-encrypt"
defaultEncryptTagLength = -1
defaultAESGCMTagLength = 128
expectedDecryptStatus = http.StatusOK
expectedEncryptStatus = http.StatusOK
)
type testEncryptDecryptCase struct {
keyType string
label string
keySize float64
cipherAlgorithms []CipherAlgorithm
attributes map[string]bool
}
func TestCreateEncryptDecryptAndDeleteKeysWithTSB(t *testing.T) {
tsbClient := newTestTSBClientFromEnv(t)
for _, tc := range testEncryptDecryptCases() {
t.Run(tc.keyType, func(t *testing.T) {
deleteTestKeyIfExists(t, tsbClient, tc.label)
defer deleteTestKeyIfExists(t, tsbClient, tc.label)
label, err := tsbClient.CreateOrUpdateKey(
context.Background(),
tc.label,
testKeyPassword,
tc.attributes,
tc.keyType,
tc.keySize,
nil,
"",
false,
)
requireNoError(t, err)
for _, cipherAlgorithm := range tc.cipherAlgorithms {
t.Run(string(cipherAlgorithm), func(t *testing.T) {
tagLength := testTagLengthForCipherAlgorithm(cipherAlgorithm)
encryptResponse, statusCode, err := tsbClient.Encrypt(
context.Background(),
label,
testKeyPassword,
testEncryptPayload,
cipherAlgorithm,
tagLength,
testEncryptAAD,
)
requireNoError(t, err)
if statusCode != expectedEncryptStatus {
t.Fatalf("encrypt status code = %d, want %d", statusCode, expectedEncryptStatus)
}
if encryptResponse.EncryptedPayload == "" {
t.Fatal("encrypted payload is empty")
}
vector := ""
if encryptResponse.InitializationVector != nil {
vector = *encryptResponse.InitializationVector
}
decryptResponse, statusCode, err := tsbClient.Decrypt(
context.Background(),
label,
testKeyPassword,
encryptResponse.EncryptedPayload,
vector,
cipherAlgorithm,
tagLength,
testEncryptAAD,
)
requireNoError(t, err)
if statusCode != expectedDecryptStatus {
t.Fatalf("decrypt status code = %d, want %d", statusCode, expectedDecryptStatus)
}
expectedPayload := testDecryptPayloadForCipherAlgorithm(t, cipherAlgorithm, tc.keySize)
if decryptResponse.Payload != expectedPayload {
t.Fatalf("decrypted payload = %q, want %q", decryptResponse.Payload, expectedPayload)
}
})
}
})
}
}
func testTagLengthForCipherAlgorithm(cipherAlgorithm CipherAlgorithm) int {
if cipherAlgorithm == CipherAlgorithmAESGCM {
return defaultAESGCMTagLength
}
return defaultEncryptTagLength
}
func testDecryptPayloadForCipherAlgorithm(t *testing.T, cipherAlgorithm CipherAlgorithm, keySize float64) string {
t.Helper()
if cipherAlgorithm != CipherAlgorithmRSANoPadding {
return testEncryptPayload
}
payload, err := base64.StdEncoding.DecodeString(testEncryptPayload)
requireNoError(t, err)
keySizeBytes := int(keySize) / 8
if len(payload) > keySizeBytes {
t.Fatalf("test payload length = %d bytes, want at most %d bytes", len(payload), keySizeBytes)
}
paddedPayload := make([]byte, keySizeBytes)
copy(paddedPayload[keySizeBytes-len(payload):], payload)
return base64.StdEncoding.EncodeToString(paddedPayload)
}
func testEncryptDecryptCases() []testEncryptDecryptCase {
return []testEncryptDecryptCase{
{
keyType: keyTypeRSA,
label: testRSAEncryptKeyLabel,
keySize: defaultRSAKeySize,
cipherAlgorithms: RSA_CIPHER_ALGORITHM,
attributes: testKeyAttributes(),
},
{
keyType: keyTypeAES,
label: testAESEncryptKeyLabel,
keySize: defaultAESKeySize,
cipherAlgorithms: AES_CIPHER_ALGORITHM,
attributes: testKeyAttributes(),
},
{
keyType: keyTypeChaCha20,
label: testChaCha20EncryptLabel,
keySize: defaultChaCha20Size,
cipherAlgorithms: CHACHA20_CIPHER_ALGORITHM,
attributes: testKeyAttributes(),
},
{
keyType: keyTypeCamellia,
label: testCamelliaEncryptLabel,
keySize: defaultCamelliaSize,
cipherAlgorithms: CAMELLIA_CIPHER_ALGORITHM,
attributes: testKeyAttributes(),
},
{
keyType: keyTypeTDEA,
label: testTDEAEncryptKeyLabel,
keySize: defaultTDEAKeySize,
cipherAlgorithms: TDEA_CIPHER_ALGORITHM,
attributes: testKeyAttributes(),
},
}
}
func TestEncryptDecryptTestCasesUseSupportedTypes(t *testing.T) {
supported := make(map[string]struct{}, len(helpers.SUPPORTED_ENCRYPT_DECRYPT_KEYS))
for _, keyType := range helpers.SUPPORTED_ENCRYPT_DECRYPT_KEYS {
supported[strings.ToUpper(keyType)] = struct{}{}
}
for _, tc := range testEncryptDecryptCases() {
if _, ok := supported[strings.ToUpper(tc.keyType)]; !ok {
t.Fatalf("test key type %q is not listed in helpers.SUPPORTED_ENCRYPT_DECRYPT_KEYS", tc.keyType)
}
}
}