// Returns size of memory (in case truncation is necessary.) // Returns -1 in case of error. // int32_t OTPassword::setMemory(const void* vInput, uint32_t nInputSize) { OT_ASSERT(nullptr != vInput); // Wipe whatever was in there before. // if (size_ > 0) zeroMemory(); isBinary_ = true; isText_ = false; if (0 == nInputSize) return 0; // Make sure no input size is larger than our block size // if (nInputSize > getBlockSize()) nInputSize = getBlockSize(); // Truncated password beyond max size. #ifndef _WIN32 // // Lock the memory page, before we copy the data over. // (If it's not already locked, which I doubt it will be.) // if (!isPageLocked_) // it won't be locked already, since we just zero'd it // (above.) But I check this anyway... { if (ot_lockPage(static_cast<void*>(&(data_[0])), getBlockSize())) { isPageLocked_ = true; } else { otErr << __FUNCTION__ << ": Error: Failed attempting to lock memory page.\n"; } } #endif OTPassword::safe_memcpy(static_cast<void*>(&(data_[0])), // dest size is based on the source // size, but guaranteed to be >0 and // <=getBlockSize nInputSize, // Since dest size is known // to be src size or less // (and >0) we use it as src // size. (We may have // truncated... and we // certainly don't want to // copy beyond our own // truncation.) vInput, nInputSize); size_ = nInputSize; return size_; }
// --------------------------------------------------------- // Returns size of memory (in case truncation is necessary.) // Returns -1 in case of error. // int32_t OTPassword::randomizeMemory(uint32_t nNewSize/*=DEFAULT_SIZE*/) { const char * szFunc = "OTPassword::randomizeMemory"; uint32_t nSize = nNewSize; // --------------------------------- // Wipe whatever was in there before. // if (m_nPasswordSize > 0) zeroMemory(); // --------------------------------- if (0 > nSize) return (-1); // --------------------------------- m_bIsBinary = true; m_bIsText = false; // --------------------------------- if (0 == nSize) return 0; // --------------------------------- // Make sure no input size is larger than our block size // if (nSize > getBlockSize()) nSize = getBlockSize(); // Truncated password beyond max size. #ifndef _WIN32 // // Lock the memory page, before we randomize 'size bytes' of the data. // (If it's not already locked, which I doubt it will be.) // if (!m_bIsPageLocked) // it won't be locked already, since we just zero'd it (above.) But I check this anyway... { if (ot_lockPage(static_cast<void *>(&(m_szPassword[0])), getBlockSize())) { m_bIsPageLocked = true; } else OTLog::vError("%s: Error: Failed attempting to lock memory page.\n", szFunc); } #endif // --------------------------------- // if (!OTPassword::randomizeMemory_uint8(&(m_szPassword[0]), nSize)) { // randomizeMemory (above) already logs, so I'm not logging again twice here. // zeroMemory(); return -1; } // -------------------------------------------------- m_nPasswordSize = nSize; return m_nPasswordSize; }
// --------------------------------------------------------- // Returns size of memory (in case truncation is necessary.) // Returns -1 in case of error. // int32_t OTPassword::setMemory(const void * vInput, uint32_t nInputSize) { OT_ASSERT(NULL != vInput); const char * szFunc = "OTPassword::setMemory"; // --------------------------------- // Wipe whatever was in there before. // if (m_nPasswordSize > 0) zeroMemory(); // --------------------------------- if (0 > nInputSize) return (-1); // --------------------------------- m_bIsBinary = true; m_bIsText = false; // --------------------------------- if (0 == nInputSize) return 0; // --------------------------------- // Make sure no input size is larger than our block size // if (nInputSize > getBlockSize()) nInputSize = getBlockSize(); // Truncated password beyond max size. // --------------------------------- #ifndef _WIN32 // // Lock the memory page, before we copy the data over. // (If it's not already locked, which I doubt it will be.) // if (!m_bIsPageLocked) // it won't be locked already, since we just zero'd it (above.) But I check this anyway... { if (ot_lockPage(static_cast<void *>(&(m_szPassword[0])), getBlockSize())) { m_bIsPageLocked = true; } else OTLog::vError("%s: Error: Failed attempting to lock memory page.\n", szFunc); } #endif // --------------------------------- OTPassword::safe_memcpy(static_cast<void *>(&(m_szPassword[0])), static_cast<uint32_t>(nInputSize), // dest size is based on the source size, but guaranteed to be >0 and <=getBlockSize vInput, static_cast<uint32_t>(nInputSize)); // Since dest size is known to be src size or less (and >0) we use it as src size. (We may have truncated... and we certainly don't want to copy beyond our own truncation.) // --------------------------------- m_nPasswordSize = nInputSize; // --------------------------------- return m_nPasswordSize; }
// Returns size of memory (in case truncation is necessary.) // Returns -1 in case of error. // int32_t OTPassword::randomizeMemory(uint32_t nNewSize) { uint32_t nSize = nNewSize; // Wipe whatever was in there before. // if (size_ > 0) zeroMemory(); isBinary_ = true; isText_ = false; if (0 == nSize) return 0; // Make sure no input size is larger than our block size // if (nSize > getBlockSize()) nSize = getBlockSize(); // Truncated password beyond max size. #ifndef _WIN32 // // Lock the memory page, before we randomize 'size bytes' of the data. // (If it's not already locked, which I doubt it will be.) // if (!isPageLocked_) // it won't be locked already, since we just zero'd it // (above.) But I check this anyway... { if (ot_lockPage(static_cast<void*>(&(data_[0])), getBlockSize())) { isPageLocked_ = true; } else { otErr << __FUNCTION__ << ": Error: Failed attempting to lock memory page.\n"; } } #endif // if (!OTPassword::randomizeMemory_uint8(&(data_[0]), nSize)) { // randomizeMemory (above) already logs, so I'm not logging again twice // here. // zeroMemory(); return -1; } size_ = nSize; return size_; }
// This adds a null terminator. // int32_t OTPassword::setPassword_uint8(const uint8_t * szInput, uint32_t nInputSize) { OT_ASSERT(NULL != szInput); const char * szFunc = "OTPassword::setPassword"; // --------------------------------- // Wipe whatever was in there before. // if (m_nPasswordSize > 0) zeroMemory(); // --------------------------------- if (0 > nInputSize) return (-1); // --------------------------------- m_bIsBinary = false; m_bIsText = true; // --------------------------------- if (0 == nInputSize) return 0; // --------------------------------- // Make sure no input size is larger than our block size // if (nInputSize > getBlockSize()) nInputSize = getBlockSize(); // Truncated password beyond max size. // --------------------------------- // The szInput string passed into this function should never // be a different size than what is passed in. For example it shouldn't // be SMALLER than what the user claims either. If it is, we error out. // if (OTString::safe_strlen(reinterpret_cast<const char *>(szInput), static_cast<size_t>(nInputSize)) < static_cast<size_t>(nInputSize)) { OTLog::vError("%s: ERROR: string length of szInput did not match nInputSize.\n", szFunc); // std::cerr << "OTPassword::setPassword: ERROR: string length of szInput did not match nInputSize." << std::endl; return (-1); } #ifndef _WIN32 // --------------------------------- // // Lock the memory page, before we copy the data over. // (If it's not already locked, which I doubt it will be.) // if (!m_bIsPageLocked) // it won't be locked already, since we just zero'd it (above.) But I check this anyway... { if (ot_lockPage(static_cast<void *>(&(m_szPassword[0])), getBlockSize())) { m_bIsPageLocked = true; } else OTLog::vError("%s: Error: Failed attempting to lock memory page.\n", szFunc); } #endif // --------------------------------- #ifdef _WIN32 strncpy_s(reinterpret_cast<char *>(m_szPassword), (1 + nInputSize), reinterpret_cast<const char *>(szInput), nInputSize); #else strncpy(reinterpret_cast<char *>(m_szPassword), reinterpret_cast<const char *>(szInput), nInputSize); #endif // --------------------------------- // force a null terminator in the 129th byte (at index 128.) // (Or at the 6th byte (at index 5), if the size is 5 bytes long.) // m_szPassword[nInputSize] = '\0'; m_nPasswordSize = nInputSize; // --------------------------------- return m_nPasswordSize; }
// This adds a null terminator. // int32_t OTPassword::setPassword_uint8(const uint8_t* szInput, uint32_t nInputSize) { OT_ASSERT(nullptr != szInput); // cppcheck-suppress variableScope const char* szFunc = "OTPassword::setPassword"; // Wipe whatever was in there before. // if (size_ > 0) zeroMemory(); isBinary_ = false; isText_ = true; if (0 == nInputSize) return 0; // Make sure no input size is larger than our block size // if (nInputSize > getBlockSize()) nInputSize = getBlockSize(); // Truncated password beyond max size. // The szInput string passed into this function should never // be a different size than what is passed in. For example it shouldn't // be SMALLER than what the user claims either. If it is, we error out. // if (String::safe_strlen(reinterpret_cast<const char*>(szInput), static_cast<size_t>(nInputSize)) < static_cast<size_t>(nInputSize)) { otErr << szFunc << ": ERROR: string length of szInput did not match nInputSize.\n"; return (-1); } #ifndef _WIN32 // // Lock the memory page, before we copy the data over. // (If it's not already locked, which I doubt it will be.) // // it won't be locked already, since we just zero'd it // (above.) But I check this anyway... if (!isPageLocked_) { if (ot_lockPage(static_cast<void*>(&(data_[0])), getBlockSize())) { isPageLocked_ = true; } else { otErr << szFunc << ": Error: Failed attempting to lock memory page.\n"; } } #endif #ifdef _WIN32 strncpy_s(reinterpret_cast<char*>(data_), (1 + nInputSize), reinterpret_cast<const char*>(szInput), nInputSize); #else strncpy(reinterpret_cast<char*>(data_), reinterpret_cast<const char*>(szInput), nInputSize); #endif // force a null terminator in the 129th byte (at index 128.) // (Or at the 6th byte (at index 5), if the size is 5 bytes int64_t.) // data_[nInputSize] = '\0'; size_ = nInputSize; return size_; }