Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTPassword Class Reference

#include <OTPassword.hpp>

Public Types

enum  BlockSize { DEFAULT_SIZE = OT_DEFAULT_BLOCKSIZE, LARGER_SIZE = OT_LARGE_BLOCKSIZE }
 

Public Member Functions

EXPORT OTPassword (BlockSize blockSize=DEFAULT_SIZE)
 
EXPORT OTPassword (const OTPassword &rhs)
 
EXPORT OTPassword (const char *input, uint32_t size, BlockSize blockSize=DEFAULT_SIZE)
 
EXPORT OTPassword (const uint8_t *input, uint32_t size, BlockSize blockSize=DEFAULT_SIZE)
 
EXPORT OTPassword (const void *input, uint32_t size, BlockSize blockSize=DEFAULT_SIZE)
 
EXPORT ~OTPassword ()
 
EXPORT OTPasswordoperator= (const OTPassword &rhs)
 
EXPORT bool isPassword () const
 
EXPORT const uint8_t * getPassword_uint8 () const
 
EXPORT const char * getPassword () const
 
EXPORT uint8_t * getPasswordWritable ()
 
EXPORT char * getPasswordWritable_char ()
 
EXPORT int32_t setPassword (const char *input, int32_t size)
 
EXPORT int32_t setPassword_uint8 (const uint8_t *input, uint32_t size)
 
EXPORT bool addChar (uint8_t c)
 
EXPORT int32_t randomizePassword (uint32_t size=DEFAULT_SIZE)
 
EXPORT bool isMemory () const
 
EXPORT const void * getMemory () const
 
EXPORT const uint8_t * getMemory_uint8 () const
 
EXPORT void * getMemoryWritable ()
 
EXPORT int32_t setMemory (const void *input, uint32_t size)
 
EXPORT int32_t addMemory (const void *append, uint32_t size)
 
EXPORT int32_t randomizeMemory (uint32_t size=DEFAULT_SIZE)
 
EXPORT uint32_t getBlockSize () const
 
EXPORT bool Compare (OTPassword &rhs) const
 
EXPORT uint32_t getPasswordSize () const
 
EXPORT uint32_t getMemorySize () const
 
EXPORT void zeroMemory ()
 
EXPORT bool SetSize (uint32_t size)
 

Static Public Member Functions

static EXPORT bool randomizePassword_uint8 (uint8_t *destination, uint32_t size)
 
static EXPORT bool randomizePassword (char *destination, uint32_t size)
 
static EXPORT bool randomizeMemory_uint8 (uint8_t *destination, uint32_t size)
 
static EXPORT bool randomizeMemory (void *destination, uint32_t size)
 
static EXPORT void zeroMemory (uint8_t *szMemory, uint32_t size)
 
static EXPORT void zeroMemory (void *vMemory, uint32_t size)
 
static EXPORT void * safe_memcpy (void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
 
static EXPORT OTPasswordCreateTextBuffer ()
 

Detailed Description

Definition at line 200 of file OTPassword.hpp.

Member Enumeration Documentation

Enumerator
DEFAULT_SIZE 
LARGER_SIZE 

Definition at line 203 of file OTPassword.hpp.

203  {
204  // (128 bytes max length for a password.)
206  // Update: now 32767 bytes if you use this size.
208  };
#define OT_LARGE_BLOCKSIZE
Definition: OTPassword.hpp:170
#define OT_DEFAULT_BLOCKSIZE
Definition: OTPassword.hpp:174

Constructor & Destructor Documentation

opentxs::OTPassword::OTPassword ( OTPassword::BlockSize  theBlockSize = DEFAULT_SIZE)

Definition at line 455 of file OTPassword.cpp.

456  : size_(0)
457  , isText_(true)
458  , isBinary_(false)
459  , isPageLocked_(false)
460  , blockSize_(theBlockSize)
461 {
462  data_[0] = '\0';
463  setPassword_uint8(reinterpret_cast<const uint8_t*>(""), 0);
464 }
EXPORT int32_t setPassword_uint8(const uint8_t *input, uint32_t size)
Definition: OTPassword.cpp:674
opentxs::OTPassword::OTPassword ( const OTPassword rhs)

Definition at line 478 of file OTPassword.cpp.

479  : size_(0)
480  , isText_(rhs.isPassword())
481  , isBinary_(rhs.isMemory())
482  , isPageLocked_(false)
483  , blockSize_(
484  rhs.blockSize_) // The buffer has this size+1 as its static size.
485 {
486  if (isText_) {
487  data_[0] = '\0';
488  setPassword_uint8(rhs.getPassword_uint8(), rhs.getPasswordSize());
489  }
490  else if (isBinary_) {
491  setMemory(rhs.getMemory_uint8(), rhs.getMemorySize());
492  }
493 }
EXPORT int32_t setMemory(const void *input, uint32_t size)
EXPORT int32_t setPassword_uint8(const uint8_t *input, uint32_t size)
Definition: OTPassword.cpp:674
opentxs::OTPassword::OTPassword ( const char *  input,
uint32_t  size,
OTPassword::BlockSize  theBlockSize = DEFAULT_SIZE 
)

Definition at line 495 of file OTPassword.cpp.

497  : size_(0)
498  , isText_(true)
499  , isBinary_(false)
500  , isPageLocked_(false)
501  , blockSize_(theBlockSize) // The buffer has this size+1 as its static size.
502 {
503  data_[0] = '\0';
504 
505  setPassword_uint8(reinterpret_cast<const uint8_t*>(szInput), nInputSize);
506 }
EXPORT int32_t setPassword_uint8(const uint8_t *input, uint32_t size)
Definition: OTPassword.cpp:674
opentxs::OTPassword::OTPassword ( const uint8_t *  input,
uint32_t  size,
OTPassword::BlockSize  theBlockSize = DEFAULT_SIZE 
)

Definition at line 508 of file OTPassword.cpp.

510  : size_(0)
511  , isText_(true)
512  , isBinary_(false)
513  , isPageLocked_(false)
514  , blockSize_(theBlockSize) // The buffer has this size+1 as its static size.
515 {
516  data_[0] = '\0';
517 
518  setPassword_uint8(szInput, nInputSize);
519 }
EXPORT int32_t setPassword_uint8(const uint8_t *input, uint32_t size)
Definition: OTPassword.cpp:674
opentxs::OTPassword::OTPassword ( const void *  input,
uint32_t  size,
OTPassword::BlockSize  theBlockSize = DEFAULT_SIZE 
)

Definition at line 521 of file OTPassword.cpp.

523  : size_(0)
524  , isText_(false)
525  , isBinary_(true)
526  , isPageLocked_(false)
527  , blockSize_(theBlockSize) // The buffer has this size+1 as its static size.
528 {
529  setMemory(vInput, nInputSize);
530 }
EXPORT int32_t setMemory(const void *input, uint32_t size)
opentxs::OTPassword::~OTPassword ( )

Definition at line 532 of file OTPassword.cpp.

533 {
534  if (size_ > 0) zeroMemory();
535 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281

Member Function Documentation

bool opentxs::OTPassword::addChar ( uint8_t  c)

Definition at line 625 of file OTPassword.cpp.

626 {
628  if (getPasswordSize() < getBlockSize()) {
629  data_[size_] = theChar;
630  ++size_;
631  data_[size_] = '\0';
632  return true;
633  }
634  return false;
635 }
EXPORT bool isPassword() const
Definition: OTPassword.cpp:537
#define OT_ASSERT(x)
Definition: Assert.hpp:150
EXPORT uint32_t getPasswordSize() const
Definition: OTPassword.cpp:613
EXPORT uint32_t getBlockSize() const
Definition: OTPassword.cpp:595
int32_t opentxs::OTPassword::addMemory ( const void *  append,
uint32_t  size 
)

Definition at line 944 of file OTPassword.cpp.

945 {
946  OT_ASSERT(nullptr != vAppend);
947 
948  // const char * szFunc = "OTPassword::addMemory";
949 
950  if (0 == nAppendSize) return 0;
951 
952  // If I'm currently at a 0 size, then call setMemory instead.
953  //
954  if (size_ == 0) return setMemory(vAppend, nAppendSize);
955  //
956  // By this point, I know I already have some memory allocated,
957  // and I'm actually appending some other memory onto the end of it.
958  //
959  // Should already be set from the above setMemory call.
960  OT_ASSERT(isBinary_);
961 
962  // Make sure total new size isn't larger than our block size
963  //
964  if ((nAppendSize + size_) > getBlockSize()) {
965  // Truncated password beyond max size.
966  nAppendSize = (getBlockSize() - size_);
967  }
968  // OT_ASSERT(nAppendSize >= 0);
969 
970  if (0 == nAppendSize) return 0;
971 
972  // By this point, I know nAppendSize is larger than 0, AND that appending it
973  // onto the
974  // existing memory of this object will not exceed the total allowed block
975  // size.
976  //
977  // Because we use setMemory when empty, and only use addMemory when we KNOW
978  // something
979  // is already there, therefore we know the page is already locked, so no
980  // need to go
981  // trying to lock it again.
982 
984  static_cast<void*>(&(data_[size_])),
985  static_cast<uint32_t>(nAppendSize), // dest size is based on the source
986  // size, but guaranteed to be >0 and
987  // <=getBlockSize
988  vAppend, static_cast<uint32_t>(nAppendSize)); // Since dest size is
989  // known to be src size or
990  // less (and >0) we use it
991  // as src size. (We may
992  // have truncated... and
993  // we certainly don't want
994  // to copy beyond our own
995  // truncation.)
996 
997  size_ += nAppendSize;
998 
999  return nAppendSize;
1000 }
EXPORT int32_t setMemory(const void *input, uint32_t size)
#define OT_ASSERT(x)
Definition: Assert.hpp:150
EXPORT uint32_t getBlockSize() const
Definition: OTPassword.cpp:595
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
bool opentxs::OTPassword::Compare ( OTPassword rhs) const

Definition at line 637 of file OTPassword.cpp.

638 {
639  OT_ASSERT(isPassword() || isMemory());
640  OT_ASSERT(rhs.isPassword() || rhs.isMemory());
641 
642  if (isPassword() && !rhs.isPassword()) return false;
643  if (isMemory() && !rhs.isMemory()) return false;
644 
645  const uint32_t nThisSize =
647  const uint32_t nRhsSize =
648  rhs.isPassword() ? rhs.getPasswordSize() : rhs.getMemorySize();
649 
650  if (nThisSize != nRhsSize) return false;
651 
652  if (0 ==
654  rhs.isPassword() ? rhs.getPassword_uint8()
655  : rhs.getMemory_uint8(),
656  rhs.isPassword() ? rhs.getPasswordSize() : rhs.getMemorySize()))
657  return true;
658 
659  return false;
660 }
EXPORT const uint8_t * getMemory_uint8() const
Definition: OTPassword.cpp:582
EXPORT uint32_t getMemorySize() const
Definition: OTPassword.cpp:619
EXPORT bool isPassword() const
Definition: OTPassword.cpp:537
#define OT_ASSERT(x)
Definition: Assert.hpp:150
EXPORT uint32_t getPasswordSize() const
Definition: OTPassword.cpp:613
EXPORT const uint8_t * getPassword_uint8() const
Definition: OTPassword.cpp:554
EXPORT bool isMemory() const
Definition: OTPassword.cpp:542
OTPassword * opentxs::OTPassword::CreateTextBuffer ( )
static

Definition at line 429 of file OTPassword.cpp.

430 {
431  // Caller MUST delete!
432 
433  char throwaway_text[OT_DEFAULT_BLOCKSIZE];
434  for (int32_t tt = 0; tt < OT_DEFAULT_BLOCKSIZE; ++tt) {
435  throwaway_text[tt] = 'A'; // todo optimization...
436  }
437  throwaway_text[OT_DEFAULT_BLOCKSIZE - 1] = '\0';
438  // We don't use the above memory, except to force OTPassword to create
439  // itself
440  // at a certain password size, so we can pass that buffer and size on to any
441  // C-style function that needs them to "already exist."
442  //
443  OTPassword* pPassUserInput = new OTPassword(
444  &(throwaway_text[0]), OT_DEFAULT_BLOCKSIZE - 1); // text mode.
445  OT_ASSERT_MSG(nullptr != pPassUserInput,
446  "OTPassword::CreateTextBuffer: ASSERT: OTPassword * "
447  "pPassUserInput = new OTPassword(&(throwaway_text[0]), "
448  "OT_DEFAULT_BLOCKSIZE-1);");
449  // Below this point, pPassUserInput must be returned, or deleted. (Or it
450  // will leak.)
451 
452  return pPassUserInput;
453 }
EXPORT OTPassword(BlockSize blockSize=DEFAULT_SIZE)
Definition: OTPassword.cpp:455
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
#define OT_DEFAULT_BLOCKSIZE
Definition: OTCallback.hpp:176
uint32_t opentxs::OTPassword::getBlockSize ( ) const

Definition at line 595 of file OTPassword.cpp.

596 {
597  uint32_t nReturn = 0;
598 
599  switch (blockSize_) {
601  nReturn = static_cast<uint32_t>(OT_DEFAULT_BLOCKSIZE);
602  break;
604  nReturn = static_cast<uint32_t>(OT_LARGE_BLOCKSIZE);
605  break;
606  default:
607  break;
608  }
609 
610  return nReturn;
611 }
#define OT_LARGE_BLOCKSIZE
Definition: OTCallback.hpp:172
#define OT_DEFAULT_BLOCKSIZE
Definition: OTCallback.hpp:176
const void * opentxs::OTPassword::getMemory ( ) const

Definition at line 577 of file OTPassword.cpp.

578 {
579  return reinterpret_cast<const void*>(getMemory_uint8());
580 }
EXPORT const uint8_t * getMemory_uint8() const
Definition: OTPassword.cpp:582
const uint8_t * opentxs::OTPassword::getMemory_uint8 ( ) const

Definition at line 582 of file OTPassword.cpp.

583 {
584  OT_ASSERT(isBinary_);
585  return (size_ <= 0) ? nullptr : static_cast<const uint8_t*>(&(data_[0]));
586 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
uint32_t opentxs::OTPassword::getMemorySize ( ) const

Definition at line 619 of file OTPassword.cpp.

620 {
621  OT_ASSERT(isBinary_);
622  return size_;
623 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
void * opentxs::OTPassword::getMemoryWritable ( )

Definition at line 589 of file OTPassword.cpp.

590 {
591  OT_ASSERT(isBinary_);
592  return (size_ <= 0) ? nullptr : static_cast<void*>(&(data_[0]));
593 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
const char * opentxs::OTPassword::getPassword ( ) const

Definition at line 547 of file OTPassword.cpp.

548 {
549  return reinterpret_cast<const char*>(getPassword_uint8());
550 }
EXPORT const uint8_t * getPassword_uint8() const
Definition: OTPassword.cpp:554
const uint8_t * opentxs::OTPassword::getPassword_uint8 ( ) const

Definition at line 554 of file OTPassword.cpp.

555 {
556  OT_ASSERT(isText_);
557  return (size_ <= 0) ? reinterpret_cast<const uint8_t*>("") : &(data_[0]);
558 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
uint32_t opentxs::OTPassword::getPasswordSize ( ) const

Definition at line 613 of file OTPassword.cpp.

614 {
615  OT_ASSERT(isText_);
616  return size_;
617 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
uint8_t * opentxs::OTPassword::getPasswordWritable ( )

Definition at line 560 of file OTPassword.cpp.

561 {
562  OT_ASSERT(isText_);
563  return (size_ <= 0)
564  ? nullptr
565  : static_cast<uint8_t*>(static_cast<void*>(&(data_[0])));
566 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
char * opentxs::OTPassword::getPasswordWritable_char ( )

Definition at line 568 of file OTPassword.cpp.

569 {
570  OT_ASSERT(isText_);
571  return (size_ <= 0) ? nullptr
572  : static_cast<char*>(static_cast<void*>(&(data_[0])));
573 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
bool opentxs::OTPassword::isMemory ( ) const

Definition at line 542 of file OTPassword.cpp.

543 {
544  return isBinary_;
545 }
bool opentxs::OTPassword::isPassword ( ) const

Definition at line 537 of file OTPassword.cpp.

538 {
539  return isText_;
540 }
OTPassword & opentxs::OTPassword::operator= ( const OTPassword rhs)

Definition at line 466 of file OTPassword.cpp.

467 {
468  if (rhs.isPassword()) {
469  setPassword_uint8(rhs.getPassword_uint8(), rhs.getPasswordSize());
470  }
471  else if (rhs.isMemory()) {
472  setMemory(rhs.getMemory_uint8(), rhs.getMemorySize());
473  }
474 
475  return *this;
476 }
EXPORT int32_t setMemory(const void *input, uint32_t size)
EXPORT int32_t setPassword_uint8(const uint8_t *input, uint32_t size)
Definition: OTPassword.cpp:674
int32_t opentxs::OTPassword::randomizeMemory ( uint32_t  size = DEFAULT_SIZE)

Definition at line 890 of file OTPassword.cpp.

891 {
892  uint32_t nSize = nNewSize;
893 
894  // Wipe whatever was in there before.
895  //
896  if (size_ > 0) zeroMemory();
897 
898  isBinary_ = true;
899  isText_ = false;
900 
901  if (0 == nSize) return 0;
902 
903  // Make sure no input size is larger than our block size
904  //
905  if (nSize > getBlockSize())
906  nSize = getBlockSize(); // Truncated password beyond max size.
907 
908 #ifndef _WIN32
909  //
910  // Lock the memory page, before we randomize 'size bytes' of the data.
911  // (If it's not already locked, which I doubt it will be.)
912  //
913  if (!isPageLocked_) // it won't be locked already, since we just zero'd it
914  // (above.) But I check this anyway...
915  {
916  if (ot_lockPage(static_cast<void*>(&(data_[0])), getBlockSize())) {
917  isPageLocked_ = true;
918  }
919  else {
920  otErr << __FUNCTION__
921  << ": Error: Failed attempting to lock memory page.\n";
922  }
923  }
924 #endif
925 
926  //
927  if (!OTPassword::randomizeMemory_uint8(&(data_[0]), nSize)) {
928  // randomizeMemory (above) already logs, so I'm not logging again twice
929  // here.
930  //
931  zeroMemory();
932  return -1;
933  }
934 
935  size_ = nSize;
936 
937  return size_;
938 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
static EXPORT bool randomizeMemory_uint8(uint8_t *destination, uint32_t size)
Definition: OTPassword.cpp:881
bool ot_lockPage(void *addr, size_t len)
Definition: OTPassword.cpp:234
OTLOG_IMPORT OTLogStream otErr
EXPORT uint32_t getBlockSize() const
Definition: OTPassword.cpp:595
bool opentxs::OTPassword::randomizeMemory ( void *  destination,
uint32_t  size 
)
static

Definition at line 874 of file OTPassword.cpp.

875 {
877  reinterpret_cast<uint8_t*>(szDestination), nNewSize);
878 }
static EXPORT bool randomizeMemory_uint8(uint8_t *destination, uint32_t size)
Definition: OTPassword.cpp:881
bool opentxs::OTPassword::randomizeMemory_uint8 ( uint8_t *  destination,
uint32_t  size 
)
static

Definition at line 881 of file OTPassword.cpp.

883 {
884  return OTCrypto::It()->RandomizeMemory(szDestination, nNewSize);
885 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
virtual bool RandomizeMemory(uint8_t *szDestination, uint32_t nNewSize) const =0
int32_t opentxs::OTPassword::randomizePassword ( uint32_t  size = DEFAULT_SIZE)

Definition at line 823 of file OTPassword.cpp.

824 {
825  uint32_t nSize = nNewSize;
826 
827  // Wipe whatever was in there before.
828  //
829  if (size_ > 0) zeroMemory();
830 
831  isBinary_ = false;
832  isText_ = true;
833 
834  if (0 == nSize) return 0;
835 
836  // Make sure no input size is larger than our block size
837  //
838  if (nSize > getBlockSize())
839  nSize = getBlockSize(); // Truncated password beyond max size.
840 #ifndef _WIN32
841  //
842  // Lock the memory page, before we randomize 'size bytes' of the data.
843  // (If it's not already locked, which I doubt it will be.)
844  //
845  if (!isPageLocked_) // it won't be locked already, since we just zero'd it
846  // (above.) But I check this anyway...
847  {
848  if (ot_lockPage(static_cast<void*>(&(data_[0])), getBlockSize())) {
849  isPageLocked_ = true;
850  }
851  else {
852  otErr << __FUNCTION__
853  << ": Error: Failed attempting to lock memory page.\n";
854  }
855  }
856 #endif
857 
858  //
859  if (!OTPassword::randomizePassword_uint8(&(data_[0]),
860  static_cast<int32_t>(nSize + 1))) {
861  // randomizeMemory (above) already logs, so I'm not logging again twice
862  // here.
863  //
864  zeroMemory();
865  return -1;
866  }
867 
868  size_ = nSize;
869 
870  return size_;
871 }
static EXPORT bool randomizePassword_uint8(uint8_t *destination, uint32_t size)
Definition: OTPassword.cpp:792
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
bool ot_lockPage(void *addr, size_t len)
Definition: OTPassword.cpp:234
OTLOG_IMPORT OTLogStream otErr
EXPORT uint32_t getBlockSize() const
Definition: OTPassword.cpp:595
bool opentxs::OTPassword::randomizePassword ( char *  destination,
uint32_t  size 
)
static

Definition at line 785 of file OTPassword.cpp.

786 {
788  reinterpret_cast<uint8_t*>(szDestination), nNewSize);
789 }
static EXPORT bool randomizePassword_uint8(uint8_t *destination, uint32_t size)
Definition: OTPassword.cpp:792
bool opentxs::OTPassword::randomizePassword_uint8 ( uint8_t *  destination,
uint32_t  size 
)
static

Definition at line 792 of file OTPassword.cpp.

794 {
795  OT_ASSERT(nullptr != szDestination);
796  OT_ASSERT(nNewSize > 0);
797 
798  // const char * szFunc = "OTPassword::randomizePassword(static)";
799 
800  if (OTPassword::randomizeMemory_uint8(szDestination, nNewSize)) {
801 
802  // This loop converts an array of binary bytes into the
803  // same array, where each byte is translated to a byte
804  // between the values of 33 and 122 (visible ASCII.)
805  //
806  for (uint32_t i = 0; i < nNewSize; ++i) {
807  uint8_t temp = (((szDestination[i]) % 89) + 33);
808  szDestination[i] = temp;
809  }
810 
811  // Add the nullptr terminator...
812  //
813  szDestination[nNewSize - 1] = '\0';
814 
815  return true;
816  }
817  return false;
818 }
static EXPORT bool randomizeMemory_uint8(uint8_t *destination, uint32_t size)
Definition: OTPassword.cpp:881
#define OT_ASSERT(x)
Definition: Assert.hpp:150
void * opentxs::OTPassword::safe_memcpy ( void *  dest,
uint32_t  dsize,
const void *  src,
uint32_t  ssize,
bool  zeroSource = false 
)
static

Definition at line 357 of file OTPassword.cpp.

363 {
364  // Make sure they aren't null.
365  OT_ASSERT(nullptr != dest);
366  OT_ASSERT(nullptr != src);
367 
368  // Make sure they aren't the same pointer.
369  OT_ASSERT(src != dest);
370 
371  // Make sure it will fit.
372  OT_ASSERT_MSG(src_length <= dest_size,
373  "ASSERT: safe_memcpy: destination buffer too small.\n");
374 
375  // Make sure they don't overlap.
376  // First assert does the beginning of the string, makes sure it's not within
377  // the bounds of the destination
378  // string. Second assert does the same thing for the end of the string.
379  // Finally a third is needed to make sure
380  // we're not in a situation where the beginning is less than the dest
381  // beginning, yet the end is also more than
382  // the dest ending!
383  //
385  false ==
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))));
400 
401 #ifdef _WIN32
402  bool bSuccess = (0 == memcpy_s(dest, static_cast<size_t>(dest_size), src,
403  static_cast<size_t>(src_length)));
404 #else
405  bool bSuccess =
406  (memcpy(dest, src, static_cast<size_t>(src_length)) == dest);
407 #endif
408 
409  if (bSuccess) {
410  if (bZeroSource) {
411  OTPassword::zeroMemory(const_cast<void*>(src), src_length);
412  }
413 
414  return dest;
415  }
416 
417  return nullptr;
418 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
#define OT_ASSERT(x)
Definition: Assert.hpp:150
#define OT_ASSERT_MSG(x, s)
Definition: Assert.hpp:155
int32_t opentxs::OTPassword::setMemory ( const void *  input,
uint32_t  size 
)

Definition at line 1005 of file OTPassword.cpp.

1006 {
1007  OT_ASSERT(nullptr != vInput);
1008 
1009  // Wipe whatever was in there before.
1010  //
1011  if (size_ > 0) zeroMemory();
1012 
1013  isBinary_ = true;
1014  isText_ = false;
1015 
1016  if (0 == nInputSize) return 0;
1017 
1018  // Make sure no input size is larger than our block size
1019  //
1020  if (nInputSize > getBlockSize())
1021  nInputSize = getBlockSize(); // Truncated password beyond max size.
1022 
1023 #ifndef _WIN32
1024  //
1025  // Lock the memory page, before we copy the data over.
1026  // (If it's not already locked, which I doubt it will be.)
1027  //
1028  if (!isPageLocked_) // it won't be locked already, since we just zero'd it
1029  // (above.) But I check this anyway...
1030  {
1031  if (ot_lockPage(static_cast<void*>(&(data_[0])), getBlockSize())) {
1032  isPageLocked_ = true;
1033  }
1034  else {
1035  otErr << __FUNCTION__
1036  << ": Error: Failed attempting to lock memory page.\n";
1037  }
1038  }
1039 #endif
1040 
1041  OTPassword::safe_memcpy(static_cast<void*>(&(data_[0])),
1042  // dest size is based on the source
1043  // size, but guaranteed to be >0 and
1044  // <=getBlockSize
1045  static_cast<uint32_t>(nInputSize),
1046  // Since dest size is known
1047  // to be src size or less
1048  // (and >0) we use it as src
1049  // size. (We may have
1050  // truncated... and we
1051  // certainly don't want to
1052  // copy beyond our own
1053  // truncation.)
1054  vInput, static_cast<uint32_t>(nInputSize));
1055 
1056  size_ = nInputSize;
1057  return size_;
1058 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
#define OT_ASSERT(x)
Definition: Assert.hpp:150
bool ot_lockPage(void *addr, size_t len)
Definition: OTPassword.cpp:234
OTLOG_IMPORT OTLogStream otErr
EXPORT uint32_t getBlockSize() const
Definition: OTPassword.cpp:595
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
int32_t opentxs::OTPassword::setPassword ( const char *  input,
int32_t  size 
)

Definition at line 665 of file OTPassword.cpp.

666 {
667  return static_cast<int32_t>(
668  setPassword_uint8(reinterpret_cast<const uint8_t*>(szInput),
669  static_cast<uint32_t>(nInputSize)));
670 }
EXPORT int32_t setPassword_uint8(const uint8_t *input, uint32_t size)
Definition: OTPassword.cpp:674
int32_t opentxs::OTPassword::setPassword_uint8 ( const uint8_t *  input,
uint32_t  size 
)

Definition at line 674 of file OTPassword.cpp.

676 {
677  OT_ASSERT(nullptr != szInput);
678 
679  // cppcheck-suppress variableScope
680  const char* szFunc = "OTPassword::setPassword";
681 
682  // Wipe whatever was in there before.
683  //
684  if (size_ > 0) zeroMemory();
685 
686  isBinary_ = false;
687  isText_ = true;
688 
689  if (0 == nInputSize) return 0;
690 
691  // Make sure no input size is larger than our block size
692  //
693  if (nInputSize > getBlockSize())
694  nInputSize = getBlockSize(); // Truncated password beyond max size.
695 
696  // The szInput string passed into this function should never
697  // be a different size than what is passed in. For example it shouldn't
698  // be SMALLER than what the user claims either. If it is, we error out.
699  //
700  if (OTString::safe_strlen(reinterpret_cast<const char*>(szInput),
701  static_cast<size_t>(nInputSize)) <
702  static_cast<size_t>(nInputSize)) {
703  otErr
704  << szFunc
705  << ": ERROR: string length of szInput did not match nInputSize.\n";
706  return (-1);
707  }
708 
709 #ifndef _WIN32
710 
711  //
712  // Lock the memory page, before we copy the data over.
713  // (If it's not already locked, which I doubt it will be.)
714  //
715  // it won't be locked already, since we just zero'd it
716  // (above.) But I check this anyway...
717  if (!isPageLocked_) {
718  if (ot_lockPage(static_cast<void*>(&(data_[0])), getBlockSize())) {
719  isPageLocked_ = true;
720  }
721  else {
722  otErr << szFunc
723  << ": Error: Failed attempting to lock memory page.\n";
724  }
725  }
726 #endif
727 
728 #ifdef _WIN32
729  strncpy_s(reinterpret_cast<char*>(data_), (1 + nInputSize),
730  reinterpret_cast<const char*>(szInput), nInputSize);
731 #else
732  strncpy(reinterpret_cast<char*>(data_),
733  reinterpret_cast<const char*>(szInput), nInputSize);
734 #endif
735 
736  // force a null terminator in the 129th byte (at index 128.)
737  // (Or at the 6th byte (at index 5), if the size is 5 bytes int64_t.)
738  //
739  data_[nInputSize] = '\0';
740  size_ = nInputSize;
741 
742  return size_;
743 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
static size_t safe_strlen(const char *s, size_t max)
Definition: OTString.cpp:388
#define OT_ASSERT(x)
Definition: Assert.hpp:150
bool ot_lockPage(void *addr, size_t len)
Definition: OTPassword.cpp:234
OTLOG_IMPORT OTLogStream otErr
EXPORT uint32_t getBlockSize() const
Definition: OTPassword.cpp:595
bool opentxs::OTPassword::SetSize ( uint32_t  size)

Definition at line 758 of file OTPassword.cpp.

759 {
760  if (isBinary_) {
761  if (uSize > getBlockSize())
762  uSize = getBlockSize(); // Truncated password beyond max size.
763  size_ = uSize;
764  return true;
765  }
766  else if (isText_) {
767  // Cannot be as much as the blocksize,
768  // because no room for null-terminator.
769  if (uSize >= getBlockSize()) {
770  uSize = getBlockSize() - 1; // Truncated password to blocksize-1.
771  }
772  // The actual null-terminator.
773  data_[uSize] = '\0';
774  // If size is 3, the terminator is at
775  size_ = uSize;
776  // data_[3] (which is the 4th byte.)
777  return true;
778  }
779  otErr << __FUNCTION__ << ": Error: isBinary_ and isText_ are both "
780  "false. (Should never happen.)\n";
781  return false;
782 }
OTLOG_IMPORT OTLogStream otErr
EXPORT uint32_t getBlockSize() const
Definition: OTPassword.cpp:595
void opentxs::OTPassword::zeroMemory ( )

Definition at line 281 of file OTPassword.cpp.

282 {
283  size_ = 0;
284 
285  OTPassword::zeroMemory(static_cast<void*>(&(data_[0])),
286  static_cast<uint32_t>(getBlockSize()));
287 
288 //
289 #ifndef _WIN32
290  // UNLOCK the page, now that we're AFTER the point where
291  // the memory was safely ZERO'd out.
292  //
293  if (isPageLocked_) {
294  if (ot_unlockPage(static_cast<void*>(&(data_[0])),
295  static_cast<uint32_t>(getBlockSize()))) {
296  isPageLocked_ = false;
297  }
298  else
299  otErr << "OTPassword::zeroMemory: Error: Memory page was locked, "
300  "but then failed to unlock it.\n";
301  }
302 #endif
303 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
bool ot_unlockPage(void *addr, size_t len)
Definition: OTPassword.cpp:257
OTLOG_IMPORT OTLogStream otErr
EXPORT uint32_t getBlockSize() const
Definition: OTPassword.cpp:595
void opentxs::OTPassword::zeroMemory ( uint8_t *  szMemory,
uint32_t  size 
)
static

Definition at line 319 of file OTPassword.cpp.

320 {
321 #ifdef _WIN32
322 
323  //
324  SecureZeroMemory(szMemory, theSize);
325 
326 // NOTE: Both SecureZeroMemory, and the pragma solution commented-out below,
327 // are acceptable for Windows. I put both here for my notes.
328 //
329 //#pragma optimize("", off)
330 // memset(szMemory, 0, theSize);
331 //#pragma optimize("", on)
332 
333 // Dr. UNIX, I presume? So, we meet again...
334 #else
335  ot_secure_memset(szMemory, static_cast<uint8_t>(0), theSize);
336 #endif
337 }
void * ot_secure_memset(void *v, uint8_t c, uint32_t n)
Definition: OTPassword.cpp:177
void opentxs::OTPassword::zeroMemory ( void *  vMemory,
uint32_t  size 
)
static

Definition at line 306 of file OTPassword.cpp.

307 {
308  // OT_ASSERT_MSG((nullptr != vMemory) && (theSize >
309  // 0),"OTPassword::zeroMemory: ASSERT: vMemory is nullptr or theSize is
310  // 0.");
311 
312  if ((nullptr != vMemory) && (theSize > 0)) {
313  uint8_t* szMemory = static_cast<uint8_t*>(vMemory);
314  OTPassword::zeroMemory(szMemory, theSize);
315  }
316 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281

The documentation for this class was generated from the following files: