-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt_decrypt_test.go
More file actions
181 lines (164 loc) · 4.08 KB
/
Copy pathencrypt_decrypt_test.go
File metadata and controls
181 lines (164 loc) · 4.08 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
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package securly
import (
"errors"
"testing"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type encryptDecryptTest struct {
desc string
encOpts []EncryptOption
input Message
encErr error
decOpts []DecryptOption
decErr error
output *Message // set if different than the input
}
var encryptExternalKey = encryptDecryptTest{
desc: "simple, working with external key",
encOpts: []EncryptOption{
EncryptWithRaw(jwa.ECDH_ES, chainA.Leaf().Public.PublicKey),
},
input: Message{
Payload: []byte("Hello, world."),
},
decOpts: []DecryptOption{
DecryptWithRaw(chainA.Leaf().Private),
},
}
var encryptResponseKey = encryptDecryptTest{
desc: "simple, working with response key",
input: Message{
Payload: []byte("Hello, world."),
Response: mustGenerateResponse(jwa.ECDH_ES, chainA.Leaf().Public),
},
decOpts: []DecryptOption{
DecryptWithRaw(chainA.Leaf().Private),
},
output: &Message{
Payload: []byte("Hello, world."),
},
}
var encryptDecryptTests = []encryptDecryptTest{
encryptExternalKey,
encryptResponseKey,
{
desc: "simple, working with a present but empty response key",
encOpts: []EncryptOption{
EncryptWithRaw(jwa.ECDH_ES, chainA.Leaf().Public.PublicKey),
},
input: Message{
Payload: []byte("Hello, world."),
Response: &Encryption{},
},
decOpts: []DecryptOption{
DecryptWithRaw(chainA.Leaf().Private),
},
output: &Message{
Payload: []byte("Hello, world."),
},
}, {
desc: "no encryption algorithm",
encErr: ErrInvalidEncryptionAlg,
input: Message{
Payload: []byte("Hello, world."),
},
}, {
desc: "invalid encryption algorithm",
encOpts: []EncryptOption{
EncryptWith(jwa.ECDH_ES, nil),
},
encErr: ErrInvalidEncryptionAlg,
}, {
desc: "invalid decryption key",
input: Message{
Payload: []byte("Hello, world."),
Response: mustGenerateResponse(jwa.ECDH_ES, chainA.Leaf().Public),
},
decOpts: []DecryptOption{
DecryptWith(nil),
},
decErr: errUnknown,
}, {
desc: "invalid response key/alg pair",
input: Message{
Payload: []byte("Hello, world."),
Response: &Encryption{
Alg: jwa.DIRECT,
Key: mustFromRaw(chainA.Leaf().Public.PublicKey),
},
},
encErr: errUnknown,
}, {
desc: "invalid response key, missing the alg",
input: Message{
Payload: []byte("Hello, world."),
Response: &Encryption{
Key: mustFromRaw(chainA.Leaf().Public.PublicKey),
},
},
decOpts: []DecryptOption{
DecryptWithRaw(chainA.Leaf().Private),
},
output: &Message{
Payload: []byte("Hello, world."),
},
encErr: ErrInvalidEncryptionAlg,
},
}
func TestEncryptDecrypt(t *testing.T) {
for _, tt := range encryptDecryptTests {
runEncryptDecryptTest(t, tt)
}
}
func runEncryptDecryptTest(t *testing.T, tt encryptDecryptTest) {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
buf, err := tt.input.Encrypt(tt.encOpts...)
if tt.encErr != nil {
assert.Nil(buf)
require.Error(err)
if !errors.Is(errUnknown, tt.encErr) {
require.ErrorIs(err, tt.encErr)
}
return
}
require.NoError(err)
require.NotNil(buf)
msg, err := Decrypt(buf, tt.decOpts...)
if tt.decErr != nil {
assert.Nil(msg)
require.Error(err)
if !errors.Is(errUnknown, tt.decErr) {
require.ErrorIs(err, tt.decErr)
}
return
}
require.NoError(err)
require.NotNil(msg)
want := &tt.input
if tt.output != nil {
want = tt.output
}
assert.Equal(want, msg)
})
}
func TestEncryptingTampering(t *testing.T) {
// Require everything to prevent 100s of duplicate errors.
require := require.New(t)
buf, err := encryptResponseKey.input.Encrypt(encryptResponseKey.encOpts...)
require.NoError(err)
require.NotNil(buf)
for i := 0; i < len(buf); i++ {
tmp := make([]byte, len(buf))
copy(tmp, buf)
tmp[i] = tmp[i] ^ 0xff
msg, err := Decrypt(tmp, encryptExternalKey.decOpts...)
require.Nil(msg, "idx=%d 0x%02x '%c'", i, tmp[i]^0xff, tmp[i]^0xff)
require.Error(err)
}
}