Skip to content

Commit b614c42

Browse files
fix(aes): Add memory zeroing for derived key to enhance security
1 parent 7f87827 commit b614c42

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/Keystore/EncryptionParameters.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "EncryptionParameters.h"
66

7+
#include "memory/memzero_wrapper.h"
78
#include "../Hash.h"
89

910
#include <TrezorCrypto/aes.h>
@@ -106,6 +107,8 @@ EncryptedPayload::EncryptedPayload(const Data& password, const Data& data, const
106107
aes_ctr_encrypt(data.data(), encrypted.data(), static_cast<int>(data.size()), iv.data(), aes_ctr_cbuf_inc, &ctx);
107108
_mac = computeMAC(derivedKey.end() - params.getKeyBytesSize(), derivedKey.end(), encrypted);
108109
}
110+
111+
memzero(derivedKey.data(), derivedKey.size());
109112
}
110113

111114
EncryptedPayload::~EncryptedPayload() {
@@ -134,6 +137,7 @@ Data EncryptedPayload::decrypt(const Data& password) const {
134137
}
135138

136139
if (!isEqualConstantTime(mac, _mac)) {
140+
memzero(derivedKey.data(), derivedKey.size());
137141
throw DecryptionError::invalidPassword;
138142
}
139143

@@ -156,7 +160,9 @@ Data EncryptedPayload::decrypt(const Data& password) const {
156160

157161
aes_ctr_decrypt(encrypted.data(), decrypted.data(), static_cast<int>(encrypted.size()), iv.data(),
158162
aes_ctr_cbuf_inc, &ctx);
163+
memzero(derivedKey.data(), derivedKey.size());
159164
} else {
165+
memzero(derivedKey.data(), derivedKey.size());
160166
throw DecryptionError::unsupportedCipher;
161167
}
162168

0 commit comments

Comments
 (0)