-
Notifications
You must be signed in to change notification settings - Fork 479
/
vmkencryption.go
266 lines (211 loc) · 8.17 KB
/
vmkencryption.go
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
// Copyright (c) 2020 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
// US Export Control Classification Number (ECCN): 5D002TSU
//
package secretstore
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/edgexfoundry/edgex-go/internal/security/kdf"
"github.com/edgexfoundry/edgex-go/internal/security/pipedhexreader"
"github.com/edgexfoundry/edgex-go/internal/security/secretstoreclient"
"github.com/edgexfoundry/go-mod-secrets/pkg/token/fileioperformer"
)
/*
THE FOLLOWING DISCLAIMER IS REQUIRED TO BE CARRIED WITH THE BELOW CODE
DO NOT REMOVE EXCEPT BY PERMISSION OF THE AUTHOR
Vault Master Key Encryption Feature
The purpose of this feature is to provide a secure way to unlock Vault
without requiring human intervention to supply the Vault Master Key.
This feature requires a sufficiently secure source of randomness in order
to unlock the Vault. This randomness should have at least 256 bits of entropy
and be backed by hardware secure storage.
If using a TPM, the secret should be bound to platform
configuration registers to attest the system state.
The randomness should be output as a string of hex-encoded octets to standard
output and the executable (or path to the executable) should be specified in the
IMK_HOOK environment variable.
In addition, the underlying platform that serves as the execution platform for
EdgeX must be secured against the ability for an attacker to intercept the
source of randomness between retrieving it out of hardware secure storage and
passing it to security-secretstore-setup. This may entail use of secure boot,
dm-verity protected partitions, volume encryption, orchestrator hardening,
and other measures.
*/
const aesKeyLength = 32 // for AES-256
type VMKEncryption struct {
fileOpener fileioperformer.FileIoPerformer
pipedHexReader pipedhexreader.PipedHexReader
kdf kdf.KeyDeriver
encrypting bool
ikm []byte
}
// NewVMKEncryption - constructor
func NewVMKEncryption(fileOpener fileioperformer.FileIoPerformer,
pipedHexReader pipedhexreader.PipedHexReader,
kdf kdf.KeyDeriver) *VMKEncryption {
return &VMKEncryption{
fileOpener: fileOpener,
pipedHexReader: pipedHexReader,
kdf: kdf,
encrypting: false,
}
}
// LoadIKM loads input key material from the specified path
func (v *VMKEncryption) LoadIKM(ikmBinPath string) error {
if ikmBinPath == "" {
return fmt.Errorf("ikmBinPath is required")
}
ikm, err := v.pipedHexReader.ReadHexBytesFromExe(ikmBinPath)
if err != nil {
return fmt.Errorf("Error reading input key material from IKM_HOOK - encryption not enabled: %w", err)
}
v.ikm = ikm
v.encrypting = true
return nil
}
// WipeIKM scrubs the input key material from memory
func (v *VMKEncryption) WipeIKM() {
// Note: make() is defined to zero-fill the array
copy(v.ikm, make([]byte, len(v.ikm)))
v.encrypting = false
}
// IsEncrypting scrubs the input key material from memory
func (v *VMKEncryption) IsEncrypting() bool {
return v.encrypting
}
// EncryptInitResponse processes the InitResponse and encrypts the key shares
// in the end, Keys and KeysBase64 are removed and replaced with
// EncryptedKeys and Nonces in the resulting JSON
// Root token is left untouched
func (v *VMKEncryption) EncryptInitResponse(initResp *secretstoreclient.InitResponse) error {
// Check prerequisite (key has been loaded)
if !v.encrypting {
return fmt.Errorf("Cannot encrypt init response as key has not been loaded")
}
newKeys := make([]string, len(initResp.Keys))
newNonces := make([]string, len(initResp.Keys))
for i, hexPlaintext := range initResp.Keys {
plainText, err := hex.DecodeString(hexPlaintext)
if err != nil {
return fmt.Errorf("failed to decode hex bytes of keyshare (details omitted): %w", err)
}
keyShare, nonce, err := v.gcmEncryptKeyshare(plainText, i) // Wrap using a unique AES key
if err != nil {
return fmt.Errorf("failed to wrap key %d: %w", i, err)
}
newKeys[i] = hex.EncodeToString(keyShare)
newNonces[i] = hex.EncodeToString(nonce)
wipeKey(keyShare) // Clear out binary version of encrypted key
wipeKey(nonce) // Clear out nonce
}
initResp.EncryptedKeys = newKeys
initResp.Nonces = newNonces
initResp.Keys = nil // strings are immutable, must wait for GC
initResp.KeysBase64 = nil // strings are immutable, must wait for GC
return nil
}
// DecryptInitResponse processes the InitResponse and decrypts the key shares
// in the end, EncryptedKeys and Nonces are removed and replaced with
// Keys and KeysBase64 in the resulting JSON like the init response was originally
// Root token is left untouched
func (v *VMKEncryption) DecryptInitResponse(initResp *secretstoreclient.InitResponse) error {
// Check prerequisite (key has been loaded)
if !v.encrypting {
return fmt.Errorf("Cannot decrypt init response as key has not been loaded")
}
newKeys := make([]string, len(initResp.EncryptedKeys))
newKeysBase64 := make([]string, len(initResp.EncryptedKeys))
for i, hexCiphertext := range initResp.EncryptedKeys {
hexNonce := initResp.Nonces[i]
nonce, err := hex.DecodeString(hexNonce)
if err != nil {
return fmt.Errorf("failed to decode hex bytes of nonce: %w", err)
}
cipherText, err := hex.DecodeString(hexCiphertext)
if err != nil {
return fmt.Errorf("failed to decode hex bytes of ciphertext: %w", err)
}
keyShare, err := v.gcmDecryptKeyshare(cipherText, nonce, i) // Unwrap using a unique AES key
if err != nil {
return fmt.Errorf("failed to unwrap key %d: %w", i, err)
}
newKeys[i] = hex.EncodeToString(keyShare)
newKeysBase64[i] = base64.StdEncoding.EncodeToString(keyShare)
}
initResp.Keys = newKeys
initResp.KeysBase64 = newKeysBase64
initResp.EncryptedKeys = nil
initResp.Nonces = nil
return nil
}
//
// Internal methods
//
// gcmEncryptKeyshare encrypts each key share with a unique key
// from the key derivation function based on passing the info
// string vault0, vault1, ... et cetera to the KDF.
func (v *VMKEncryption) gcmEncryptKeyshare(keyshare []byte, counter int) ([]byte, []byte, error) {
defer wipeKey(keyshare) // wipe original keyshare on exit
info := fmt.Sprintf("vault%d", counter)
key, err := v.kdf.DeriveKey(v.ikm, aesKeyLength, info)
if err != nil {
return nil, nil, fmt.Errorf("failed to derive encryption key for vault master key share %w", err)
}
defer wipeKey(key) // wipe encryption key on exit
block, err := aes.NewCipher(key)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize block cipher: %w", err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, nil, fmt.Errorf("failed to initialize AES cipher: %w", err)
}
nonce := make([]byte, aesgcm.NonceSize())
if _, err := rand.Read(nonce); err != nil {
return nil, nil, fmt.Errorf("failed to initialize random nonce: %w", err)
}
// Encrypt the key share (plaintext to be wiped on exit by deferred function)
ciphertext := aesgcm.Seal(nil, nonce, keyshare, nil)
return ciphertext, nonce, nil
}
// gcmDecryptKeyshare decrypts each key share with a unique key
// from the key derivation function based on passing the info
// string vault0, vault1, ... et cetera to the KDF.
func (v *VMKEncryption) gcmDecryptKeyshare(keyshare []byte, nonce []byte, counter int) ([]byte, error) {
defer wipeKey(keyshare) // wipe original (encrypted) keyshare on exit (not technically needed)
info := fmt.Sprintf("vault%d", counter)
key, err := v.kdf.DeriveKey(v.ikm, aesKeyLength, info)
if err != nil {
return nil, fmt.Errorf("failed to derive encryption key for vault master key share %w", err)
}
defer wipeKey(key) // wipe encryption key on exit
block, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("failed to initialize block cipher: %w", err)
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return nil, fmt.Errorf("failed to initialize AES cipher: %w", err)
}
// Decrypt key share; on error, erase any partial results
plaintext, err := aesgcm.Open(nil, nonce, keyshare, nil)
if err != nil {
if plaintext != nil {
wipeKey(plaintext)
}
return nil, fmt.Errorf("failed to decrypt ciphertext: %w", err)
}
return plaintext, nil
}
func wipeKey(key []byte) {
blank := make([]byte, len(key)) // zero-filled
copy(key, blank)
}