145 #include <sys/types.h>
146 #include <sys/mman.h>
151 #define PAGESIZE sysconf(_SC_PAGESIZE)
156 #define mlock(a, b) \
157 mlock(((void*)(((size_t)(a)) & (~((PAGESIZE)-1)))), \
158 (((((size_t)(a)) + (b)-1) | ((PAGESIZE)-1)) + 1) - \
159 (((size_t)(a)) & (~((PAGESIZE)-1))))
160 #define munlock(a, b) \
161 munlock(((void*)(((size_t)(a)) & (~((PAGESIZE)-1)))), \
162 (((((size_t)(a)) + (b)-1) | ((PAGESIZE)-1)) + 1) - \
163 (((size_t)(a)) & (~((PAGESIZE)-1))))
181 volatile uint8_t* p =
static_cast<volatile uint8_t*
>(v);
182 while (n--) *p++ = c;
238 #elif defined(PREDEF_PLATFORM_UNIX)
239 static bool bWarned =
false;
240 if (
mlock(addr, len) && !bWarned) {
242 otErr <<
"ot_lockPage: WARNING: unable to lock memory. \n"
243 " (Passwords / secret keys may be swapped to disk!)\n";
247 OT_FAIL_MSG(
"ASSERT: ot_lockPage unable to lock memory.");
261 #elif defined(PREDEF_PLATFORM_UNIX)
262 static bool bWarned =
false;
263 if (
munlock(addr, len) && !bWarned) {
265 otErr <<
"ot_unlockPage: WARNING: unable to unlock memory used for "
266 "storing secrets.\n";
270 OT_FAIL_MSG(
"ASSERT: ot_unlockPage unable to unlock secret memory.");
296 isPageLocked_ =
false;
299 otErr <<
"OTPassword::zeroMemory: Error: Memory page was locked, "
300 "but then failed to unlock it.\n";
312 if ((
nullptr != vMemory) && (theSize > 0)) {
313 uint8_t* szMemory =
static_cast<uint8_t*
>(vMemory);
324 SecureZeroMemory(szMemory, theSize);
373 "ASSERT: safe_memcpy: destination buffer too small.\n");
386 ((static_cast<const uint8_t*>(src) > static_cast<uint8_t*>(dest)) &&
387 (static_cast<const uint8_t*>(src) <
388 (static_cast<uint8_t*>(dest) + dest_size))),
389 "ASSERT: safe_memcpy: Unexpected memory overlap, start of src.\n");
391 false == (((static_cast<const uint8_t*>(src) + src_length) >
392 static_cast<uint8_t*>(dest)) &&
393 ((static_cast<const uint8_t*>(src) + src_length) <
394 (static_cast<uint8_t*>(dest) + dest_size))),
395 "ASSERT: safe_memcpy: Unexpected memory overlap, end of src.\n");
396 OT_ASSERT(
false == ((static_cast<const uint8_t*>(src) <=
397 static_cast<uint8_t*>(dest)) &&
398 ((static_cast<const uint8_t*>(src) + src_length) >=
399 (static_cast<uint8_t*>(dest) + dest_size))));
402 bool bSuccess = (0 == memcpy_s(dest, static_cast<size_t>(dest_size), src,
403 static_cast<size_t>(src_length)));
406 (memcpy(dest, src, static_cast<size_t>(src_length)) == dest);
435 throwaway_text[tt] =
'A';
437 throwaway_text[OT_DEFAULT_BLOCKSIZE - 1] =
'\0';
444 &(throwaway_text[0]), OT_DEFAULT_BLOCKSIZE - 1);
446 "OTPassword::CreateTextBuffer: ASSERT: OTPassword * "
447 "pPassUserInput = new OTPassword(&(throwaway_text[0]), "
448 "OT_DEFAULT_BLOCKSIZE-1);");
452 return pPassUserInput;
459 , isPageLocked_(false)
460 , blockSize_(theBlockSize)
480 , isText_(rhs.isPassword())
481 , isBinary_(rhs.isMemory())
482 , isPageLocked_(false)
490 else if (isBinary_) {
500 , isPageLocked_(false)
501 , blockSize_(theBlockSize)
513 , isPageLocked_(false)
514 , blockSize_(theBlockSize)
526 , isPageLocked_(false)
527 , blockSize_(theBlockSize)
557 return (size_ <= 0) ?
reinterpret_cast<const uint8_t*
>(
"") : &(data_[0]);
565 :
static_cast<uint8_t*
>(
static_cast<void*
>(&(data_[0])));
571 return (size_ <= 0) ?
nullptr
572 :
static_cast<char*
>(
static_cast<void*
>(&(data_[0])));
585 return (size_ <= 0) ?
nullptr :
static_cast<const uint8_t*
>(&(data_[0]));
592 return (size_ <= 0) ?
nullptr :
static_cast<void*
>(&(data_[0]));
597 uint32_t nReturn = 0;
599 switch (blockSize_) {
629 data_[size_] = theChar;
645 const uint32_t nThisSize =
647 const uint32_t nRhsSize =
650 if (nThisSize != nRhsSize)
return false;
667 return static_cast<int32_t
>(
669 static_cast<uint32_t>(nInputSize)));
680 const char* szFunc =
"OTPassword::setPassword";
689 if (0 == nInputSize)
return 0;
701 static_cast<size_t>(nInputSize)) <
702 static_cast<size_t>(nInputSize)) {
705 <<
": ERROR: string length of szInput did not match nInputSize.\n";
717 if (!isPageLocked_) {
719 isPageLocked_ =
true;
723 <<
": Error: Failed attempting to lock memory page.\n";
729 strncpy_s(reinterpret_cast<char*>(data_), (1 + nInputSize),
730 reinterpret_cast<const char*>(szInput), nInputSize);
732 strncpy(reinterpret_cast<char*>(data_),
733 reinterpret_cast<const char*>(szInput), nInputSize);
739 data_[nInputSize] =
'\0';
779 otErr << __FUNCTION__ <<
": Error: isBinary_ and isText_ are both "
780 "false. (Should never happen.)\n";
788 reinterpret_cast<uint8_t*>(szDestination), nNewSize);
806 for (uint32_t i = 0; i < nNewSize; ++i) {
807 uint8_t temp = (((szDestination[i]) % 89) + 33);
808 szDestination[i] = temp;
813 szDestination[nNewSize - 1] =
'\0';
825 uint32_t nSize = nNewSize;
834 if (0 == nSize)
return 0;
849 isPageLocked_ =
true;
852 otErr << __FUNCTION__
853 <<
": Error: Failed attempting to lock memory page.\n";
860 static_cast<int32_t>(nSize + 1))) {
877 reinterpret_cast<uint8_t*>(szDestination), nNewSize);
892 uint32_t nSize = nNewSize;
901 if (0 == nSize)
return 0;
917 isPageLocked_ =
true;
920 otErr << __FUNCTION__
921 <<
": Error: Failed attempting to lock memory page.\n";
950 if (0 == nAppendSize)
return 0;
954 if (size_ == 0)
return setMemory(vAppend, nAppendSize);
970 if (0 == nAppendSize)
return 0;
984 static_cast<void*>(&(data_[size_])),
985 static_cast<uint32_t>(nAppendSize),
988 vAppend, static_cast<uint32_t>(nAppendSize));
997 size_ += nAppendSize;
1016 if (0 == nInputSize)
return 0;
1032 isPageLocked_ =
true;
1035 otErr << __FUNCTION__
1036 <<
": Error: Failed attempting to lock memory page.\n";
1045 static_cast<uint32_t>(nInputSize),
1054 vInput, static_cast<uint32_t>(nInputSize));
EXPORT const uint8_t * getMemory_uint8() const
static EXPORT OTCrypto * It()
virtual bool RandomizeMemory(uint8_t *szDestination, uint32_t nNewSize) const =0
EXPORT uint32_t getMemorySize() const
EXPORT OTPassword(BlockSize blockSize=DEFAULT_SIZE)
static EXPORT bool randomizePassword_uint8(uint8_t *destination, uint32_t size)
EXPORT uint8_t * getPasswordWritable()
EXPORT int32_t addMemory(const void *append, uint32_t size)
bool ot_unlockPage(void *addr, size_t len)
EXPORT bool isPassword() const
EXPORT char * getPasswordWritable_char()
EXPORT const void * getMemory() const
EXPORT int32_t setMemory(const void *input, uint32_t size)
EXPORT int32_t setPassword(const char *input, int32_t size)
EXPORT bool addChar(uint8_t c)
static EXPORT bool randomizeMemory_uint8(uint8_t *destination, uint32_t size)
EXPORT OTPassword & operator=(const OTPassword &rhs)
EXPORT int32_t randomizeMemory(uint32_t size=DEFAULT_SIZE)
EXPORT bool SetSize(uint32_t size)
static EXPORT OTPassword * CreateTextBuffer()
static size_t safe_strlen(const char *s, size_t max)
#define OT_LARGE_BLOCKSIZE
void * ot_secure_memset(void *v, uint8_t c, uint32_t n)
#define OT_ASSERT_MSG(x, s)
EXPORT uint32_t getPasswordSize() const
EXPORT const char * getPassword() const
bool ot_lockPage(void *addr, size_t len)
OTLOG_IMPORT OTLogStream otErr
EXPORT const uint8_t * getPassword_uint8() const
EXPORT bool isMemory() const
EXPORT void * getMemoryWritable()
EXPORT int32_t randomizePassword(uint32_t size=DEFAULT_SIZE)
EXPORT int32_t setPassword_uint8(const uint8_t *input, uint32_t size)
EXPORT uint32_t getBlockSize() const
#define OT_DEFAULT_BLOCKSIZE
EXPORT bool Compare(OTPassword &rhs) const
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)