-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: add CSensitiveString using libsodium's memory functions
Using sodium_malloc to create a buffer for our password input gives us the following benefits: - Password is not in the same region as other heap structures. - Password contents do not show up in coredumps.
- Loading branch information
1 parent
9adc28c
commit 0a8e8f0
Showing
15 changed files
with
295 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#pragma once | ||
|
||
#include "./Log.hpp" | ||
#include <cstring> | ||
#include <sodium.h> | ||
|
||
class CSensitiveString { | ||
public: | ||
static constexpr size_t FIXED_BUFFER_SIZE = 4096; | ||
static size_t capacity() { | ||
return FIXED_BUFFER_SIZE; | ||
} | ||
|
||
CSensitiveString(const CSensitiveString&) = delete; | ||
CSensitiveString& operator=(const CSensitiveString&) = delete; | ||
|
||
CSensitiveString() { | ||
RASSERT(sodium_init() >= 0, "sodium_init failed"); | ||
m_pData = (char*)sodium_malloc(FIXED_BUFFER_SIZE); | ||
m_iLength = 0; | ||
m_pData[0] = '\0'; | ||
} | ||
|
||
CSensitiveString(const char* data) { | ||
RASSERT(sodium_init() >= 0, "sodium_init failed"); | ||
m_pData = (char*)sodium_malloc(FIXED_BUFFER_SIZE); | ||
set(data); | ||
} | ||
|
||
~CSensitiveString() { | ||
sodium_free(m_pData); | ||
m_pData = nullptr; | ||
m_iLength = 0; | ||
} | ||
|
||
void set(const char* data) { | ||
const auto LEN = strlen(data); | ||
if (LEN >= FIXED_BUFFER_SIZE) { | ||
Debug::log(ERR, "SensitiveString: data too large"); | ||
clear(); | ||
return; | ||
} | ||
memcpy(m_pData, data, LEN); | ||
m_pData[LEN] = '\0'; | ||
m_iLength = LEN; | ||
} | ||
|
||
void set(const CSensitiveString& other) { | ||
if (other.m_iLength >= FIXED_BUFFER_SIZE) { | ||
Debug::log(ERR, "SensitiveString: data too large"); | ||
clear(); | ||
return; | ||
} | ||
memcpy(m_pData, other.m_pData, other.m_iLength); | ||
m_pData[other.m_iLength] = '\0'; | ||
m_iLength = other.m_iLength; | ||
} | ||
|
||
void extend(char* buf, size_t len) { | ||
if (m_iLength + len >= FIXED_BUFFER_SIZE) { | ||
Debug::log(ERR, "SensitiveString: data too large"); | ||
clear(); | ||
return; | ||
} | ||
memcpy(m_pData + m_iLength, buf, len); | ||
m_iLength += len; | ||
m_pData[m_iLength] = '\0'; | ||
} | ||
|
||
char pop_back() { | ||
if (m_iLength == 0) | ||
return '\0'; | ||
m_iLength--; | ||
const auto C = m_pData[m_iLength]; | ||
m_pData[m_iLength] = '\0'; | ||
return C; | ||
} | ||
|
||
void clear() { | ||
sodium_memzero(m_pData, FIXED_BUFFER_SIZE); | ||
m_iLength = 0; | ||
} | ||
|
||
const char* c_str() const { | ||
return m_pData; | ||
} | ||
|
||
size_t length() const { | ||
return m_iLength; | ||
} | ||
|
||
size_t size() const { | ||
return m_iLength; | ||
} | ||
|
||
char back() const { | ||
if (m_iLength == 0) | ||
return '\0'; | ||
return m_pData[m_iLength - 1]; | ||
} | ||
|
||
bool empty() const { | ||
return m_iLength == 0; | ||
} | ||
|
||
const char* begin() const { | ||
return m_pData; | ||
} | ||
|
||
const char* end() const { | ||
return m_pData + m_iLength; | ||
} | ||
|
||
private: | ||
char* m_pData = nullptr; | ||
size_t m_iLength = 0; | ||
}; |
Oops, something went wrong.