Repository navigation
Expand file tree
/
Copy pathencryption_test.go
More file actions
215 lines (196 loc) · 5.78 KB
/
Copy pathencryption_test.go
File metadata and controls
215 lines (196 loc) · 5.78 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
// Copyright IBM Corp. 2022, 2025
// SPDX-License-Identifier: MPL-2.0
package nodeenrollment
import (
"context"
"crypto/ecdh"
"crypto/rand"
"testing"
"github.com/google/go-cmp/cmp"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"github.com/hashicorp/go-kms-wrapping/v2/aead"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
)
type testNode struct {
keyId string
priv []byte
otherPub []byte
}
func (t testNode) X25519EncryptionKey() (string, []byte, error) {
privKey, err := ecdh.X25519().NewPrivateKey(t.priv)
if err != nil {
return "", nil, err
}
otherPubKey, err := ecdh.X25519().NewPublicKey(t.otherPub)
if err != nil {
return "", nil, err
}
encKey, err := privKey.ECDH(otherPubKey)
if err != nil {
return "", nil, err
}
return t.keyId, encKey, err
}
func (t testNode) PreviousX25519EncryptionKey() (string, []byte, error) {
return "", nil, nil
}
var _ X25519KeyProducer = (*testNode)(nil)
func Test_EncryptionDecryption(t *testing.T) {
t.Parallel()
tRequire := require.New(t)
ctx := context.Background()
wrapper := aead.TestWrapper(t)
curve := ecdh.X25519()
node1Priv, err := curve.GenerateKey(rand.Reader)
tRequire.NoError(err)
node1Pub := node1Priv.PublicKey()
node2Priv, err := curve.GenerateKey(rand.Reader)
tRequire.NoError(err)
node2Pub := node2Priv.PublicKey()
node1 := &testNode{priv: node1Priv.Bytes(), otherPub: node2Pub.Bytes()}
node2 := &testNode{priv: node2Priv.Bytes(), otherPub: node1Pub.Bytes()}
encryptMsg := &wrapping.BlobInfo{
Ciphertext: []byte("foo"),
Iv: []byte("bar"),
Hmac: []byte("baz"),
}
tests := []struct {
name string
node *testNode
otherNode *testNode
encryptId string
decryptId string
encryptMsg proto.Message
decryptMsg proto.Message
encryptKeySource X25519KeyProducer
decryptKeySource X25519KeyProducer
encDecWrapper wrapping.Wrapper
wantErrContains string
wantEncErrContains string
wantDecErrContains string
}{
{
name: "valid",
node: node1,
encryptId: "foo",
decryptId: "foo",
encryptMsg: encryptMsg,
decryptMsg: new(wrapping.BlobInfo),
encryptKeySource: node1,
decryptKeySource: node1,
},
{
name: "valid-with-wrapper",
node: node1,
encryptId: "foo",
decryptId: "foo",
encryptMsg: encryptMsg,
decryptMsg: new(wrapping.BlobInfo),
encryptKeySource: node1,
decryptKeySource: node1,
encDecWrapper: wrapper,
},
{
name: "mismatched-id",
node: node1,
encryptId: "foo",
decryptId: "bar",
encryptMsg: encryptMsg,
decryptMsg: new(wrapping.BlobInfo),
encryptKeySource: node1,
decryptKeySource: node1,
wantDecErrContains: "message authentication failed",
},
{
name: "empty-encrypt-msg",
node: node1,
encryptId: "foo",
decryptId: "bar",
encryptMsg: nil,
decryptMsg: new(wrapping.BlobInfo),
encryptKeySource: node1,
decryptKeySource: node1,
wantEncErrContains: "incoming message is nil",
},
{
name: "empty-decrypt-msg",
node: node1,
encryptId: "foo",
decryptId: "bar",
encryptMsg: encryptMsg,
decryptMsg: nil,
encryptKeySource: node1,
decryptKeySource: node1,
wantDecErrContains: "incoming result message is nil",
},
{
name: "empty-encrypt-keyproducer",
node: node1,
encryptId: "foo",
decryptId: "bar",
encryptMsg: encryptMsg,
decryptMsg: new(wrapping.BlobInfo),
encryptKeySource: nil,
decryptKeySource: node1,
wantEncErrContains: "incoming key source is nil",
},
{
name: "empty-decrypt-keyproducer",
node: node1,
encryptId: "foo",
decryptId: "bar",
encryptMsg: encryptMsg,
decryptMsg: new(wrapping.BlobInfo),
encryptKeySource: node1,
decryptKeySource: nil,
wantDecErrContains: "incoming key source is nil",
},
{
name: "ensure-same-keys",
node: node1,
otherNode: node2,
encryptId: "foo",
decryptId: "foo",
encryptKeySource: node1,
decryptKeySource: node1,
wantErrContains: "unknown type",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
subtAssert, subtRequire := assert.New(t), require.New(t)
_, sharedKeyFromNode, err := tt.node.X25519EncryptionKey()
subtRequire.NoError(err)
if tt.otherNode != nil {
_, sharedKeyFromOtherNode, err := tt.otherNode.X25519EncryptionKey()
subtRequire.NoError(err)
subtAssert.Equal(sharedKeyFromNode, sharedKeyFromOtherNode)
return
}
if tt.encryptKeySource != nil {
tt.encryptKeySource.(*testNode).keyId = tt.encryptId
}
ct, err := EncryptMessage(ctx, tt.encryptMsg, tt.encryptKeySource, WithStorageWrapper(tt.encDecWrapper))
if tt.wantEncErrContains != "" {
subtRequire.Error(err)
subtAssert.Contains(err.Error(), tt.wantEncErrContains)
return
}
subtRequire.NoError(err)
if tt.decryptKeySource != nil {
tt.decryptKeySource.(*testNode).keyId = tt.decryptId
}
err = DecryptMessage(ctx, ct, tt.decryptKeySource, tt.decryptMsg, WithStorageWrapper(tt.encDecWrapper))
if tt.wantDecErrContains != "" {
subtRequire.Error(err)
subtAssert.Contains(err.Error(), tt.wantDecErrContains)
return
}
subtRequire.NoError(err)
subtAssert.Empty(cmp.Diff(tt.encryptMsg, tt.decryptMsg, protocmp.Transform()))
})
}
}