Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace AES-CBC with AES-GCM in SecureDataManager #627

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 11 additions & 46 deletions src/lib/data_mgr/SecureDataManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ void SecureDataManager::initObject()
// Set the initial login state
soLoggedIn = userLoggedIn = false;

// Set the magic
magic = ByteString("524A52"); // RJR

// Get a mutex
dataMgrMutex = MutexFactory::i()->getMutex();
}
Expand Down Expand Up @@ -131,25 +128,15 @@ bool SecureDataManager::pbeEncryptKey(const ByteString& passphrase, ByteString&

// Encrypt the data
ByteString block;
ByteString aad;

if (!aes->encryptInit(pbeKey, SymMode::CBC, IV))
{
delete pbeKey;

return false;
}

// First, add the magic
if (!aes->encryptUpdate(magic, block))
if (!aes->encryptInit(pbeKey, SymMode::GCM, IV, true, 0, aad, 16))
{
delete pbeKey;

return false;
}

encryptedKey += block;

// Then, add the key itself
ByteString key;

{
Expand Down Expand Up @@ -269,9 +256,10 @@ bool SecureDataManager::login(const ByteString& passphrase, const ByteString& en
// Decrypt the key data
ByteString decryptedKeyData;
ByteString finalBlock;
ByteString aad;

// NOTE: The login will fail here if incorrect passphrase is supplied
if (!aes->decryptInit(pbeKey, SymMode::CBC, IV) ||
if (!aes->decryptInit(pbeKey, SymMode::GCM, IV, 0, true, aad, 16) ||
!aes->decryptUpdate(encryptedKeyData, decryptedKeyData) ||
!aes->decryptFinal(finalBlock))
{
Expand All @@ -284,23 +272,8 @@ bool SecureDataManager::login(const ByteString& passphrase, const ByteString& en

decryptedKeyData += finalBlock;

// Check the magic
if (decryptedKeyData.substr(0, 3) != magic)
{
// The passphrase was incorrect
DEBUG_MSG("Incorrect passphrase supplied");

return false;
}

// Strip off the magic
ByteString key = decryptedKeyData.substr(3);

// And mask the key
decryptedKeyData.wipe();

MutexLocker lock(dataMgrMutex);
remask(key);
remask(decryptedKeyData);

return true;
}
Expand Down Expand Up @@ -340,9 +313,10 @@ bool SecureDataManager::reAuthenticate(const ByteString& passphrase, const ByteS
// Decrypt the key data
ByteString decryptedKeyData;
ByteString finalBlock;
ByteString aad;

// NOTE: The login will fail here if incorrect passphrase is supplied
if (!aes->decryptInit(pbeKey, SymMode::CBC, IV) ||
if (!aes->decryptInit(pbeKey, SymMode::GCM, IV, true, 0, aad, 16) ||
!aes->decryptUpdate(encryptedKeyData, decryptedKeyData) ||
!aes->decryptFinal(finalBlock))
{
Expand All @@ -353,17 +327,6 @@ bool SecureDataManager::reAuthenticate(const ByteString& passphrase, const ByteS

delete pbeKey;

decryptedKeyData += finalBlock;

// Check the magic
if (decryptedKeyData.substr(0, 3) != magic)
{
// The passphrase was incorrect
DEBUG_MSG("Incorrect passphrase supplied");

return false;
}

// And mask the key
decryptedKeyData.wipe();

Expand Down Expand Up @@ -434,8 +397,9 @@ bool SecureDataManager::decrypt(const ByteString& encrypted, ByteString& plainte
}

ByteString finalBlock;
ByteString aad;

if (!aes->decryptInit(&theKey, SymMode::CBC, IV) ||
if (!aes->decryptInit(&theKey, SymMode::GCM, IV, true, 0, aad, 16) ||
!aes->decryptUpdate(encrypted.substr(aes->getBlockSize()), plaintext) ||
!aes->decryptFinal(finalBlock))
{
Expand Down Expand Up @@ -478,8 +442,9 @@ bool SecureDataManager::encrypt(const ByteString& plaintext, ByteString& encrypt
if (!rng->generateRandom(IV, aes->getBlockSize())) return false;

ByteString finalBlock;
ByteString aad;

if (!aes->encryptInit(&theKey, SymMode::CBC, IV) ||
if (!aes->encryptInit(&theKey, SymMode::GCM, IV, true, 0, aad, 16) ||
!aes->encryptUpdate(plaintext, encrypted) ||
!aes->encryptFinal(finalBlock))
{
Expand Down
3 changes: 0 additions & 3 deletions src/lib/data_mgr/SecureDataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ class SecureDataManager
// The masked version of the actual key
ByteString maskedKey;

// The "magic" data used to detect if a PIN was likely to be correct
ByteString magic;

// The mask; this is not a stack member but a heap member. This
// hopefully ensures that the mask ends up in a memory location
// that is not logically linked to the masked key
Expand Down