@@ -104,11 +104,10 @@ func (v *VMKEncryption) IsEncrypting() bool {
104
104
// in the end, Keys and KeysBase64 are removed and replaced with
105
105
// EncryptedKeys and Nonces in the resulting JSON
106
106
// Root token is left untouched
107
- func (v * VMKEncryption ) EncryptInitResponse (initResp types.InitResponse ) (types.InitResponse , error ) {
108
- var encryptedResponse types.InitResponse
107
+ func (v * VMKEncryption ) EncryptInitResponse (initResp * types.InitResponse ) error {
109
108
// Check prerequisite (key has been loaded)
110
109
if ! v .encrypting {
111
- return encryptedResponse , fmt .Errorf ("Cannot encrypt init response as key has not been loaded" )
110
+ return fmt .Errorf ("cannot encrypt init response as key has not been loaded" )
112
111
}
113
112
114
113
newKeys := make ([]string , len (initResp .Keys ))
@@ -118,12 +117,12 @@ func (v *VMKEncryption) EncryptInitResponse(initResp types.InitResponse) (types.
118
117
119
118
plainText , err := hex .DecodeString (hexPlaintext )
120
119
if err != nil {
121
- return encryptedResponse , fmt .Errorf ("failed to decode hex bytes of keyshare (details omitted): %w" , err )
120
+ return fmt .Errorf ("failed to decode hex bytes of keyshare (details omitted): %w" , err )
122
121
}
123
122
124
123
keyShare , nonce , err := v .gcmEncryptKeyShare (plainText , i ) // Wrap using a unique AES key
125
124
if err != nil {
126
- return encryptedResponse , fmt .Errorf ("failed to wrap key %d: %w" , i , err )
125
+ return fmt .Errorf ("failed to wrap key %d: %w" , i , err )
127
126
}
128
127
129
128
newKeys [i ] = hex .EncodeToString (keyShare )
@@ -133,51 +132,53 @@ func (v *VMKEncryption) EncryptInitResponse(initResp types.InitResponse) (types.
133
132
wipeKey (nonce ) // Clear out nonce
134
133
}
135
134
136
- encryptedResponse .EncryptedKeys = newKeys
137
- encryptedResponse .Nonces = newNonces
138
- return encryptedResponse , nil
135
+ initResp .EncryptedKeys = newKeys
136
+ initResp .Nonces = newNonces
137
+ initResp .Keys = nil // strings are immutable, must wait for GC
138
+ initResp .KeysBase64 = nil // strings are immutable, must wait for GC
139
+ return nil
139
140
}
140
141
141
142
// DecryptInitResponse processes the InitResponse and decrypts the key shares
142
143
// in the end, EncryptedKeys and Nonces are removed and replaced with
143
144
// Keys and KeysBase64 in the resulting JSON like the init response was originally
144
145
// Root token is left untouched
145
- func (v * VMKEncryption ) DecryptInitResponse (initResp types.InitResponse ) (types.InitResponse , error ) {
146
- var decryptedResponse types.InitResponse
147
-
146
+ func (v * VMKEncryption ) DecryptInitResponse (initResp * types.InitResponse ) error {
148
147
// Check prerequisite (key has been loaded)
149
148
if ! v .encrypting {
150
- return decryptedResponse , fmt .Errorf ("Cannot decrypt init response as key has not been loaded" )
149
+ return fmt .Errorf ("cannot decrypt init response as key has not been loaded" )
151
150
}
152
151
153
152
newKeys := make ([]string , len (initResp .EncryptedKeys ))
154
153
newKeysBase64 := make ([]string , len (initResp .EncryptedKeys ))
155
154
156
155
for i , hexCiphertext := range initResp .EncryptedKeys {
157
-
158
156
hexNonce := initResp .Nonces [i ]
159
157
nonce , err := hex .DecodeString (hexNonce )
160
158
if err != nil {
161
- return decryptedResponse , fmt .Errorf ("failed to decode hex bytes of nonce: %w" , err )
159
+ return fmt .Errorf ("failed to decode hex bytes of nonce: %w" , err )
162
160
}
163
161
164
162
cipherText , err := hex .DecodeString (hexCiphertext )
165
163
if err != nil {
166
- return decryptedResponse , fmt .Errorf ("failed to decode hex bytes of ciphertext: %w" , err )
164
+ return fmt .Errorf ("failed to decode hex bytes of ciphertext: %w" , err )
167
165
}
168
166
169
167
keyShare , err := v .gcmDecryptKeyShare (cipherText , nonce , i ) // Unwrap using a unique AES key
170
168
if err != nil {
171
- return decryptedResponse , fmt .Errorf ("failed to unwrap key %d: %w" , i , err )
169
+ return fmt .Errorf ("failed to unwrap key %d: %w" , i , err )
172
170
}
173
171
174
172
newKeys [i ] = hex .EncodeToString (keyShare )
175
173
newKeysBase64 [i ] = base64 .StdEncoding .EncodeToString (keyShare )
176
174
}
177
175
178
- decryptedResponse .Keys = newKeys
179
- decryptedResponse .KeysBase64 = newKeysBase64
180
- return decryptedResponse , nil
176
+ initResp .Keys = newKeys
177
+ initResp .KeysBase64 = newKeysBase64
178
+ initResp .EncryptedKeys = nil
179
+ initResp .Nonces = nil
180
+
181
+ return nil
181
182
}
182
183
183
184
//
0 commit comments