Fabcoin Core
0.16.2
P2P Digital Currency
|
Utility functions for the Crypto++ library. More...
Go to the source code of this file.
Classes | |
struct | CompileAssert< b > |
class | Empty |
An Empty class. More... | |
class | TwoBases< BASE1, BASE2 > |
class | ThreeBases< BASE1, BASE2, BASE3 > |
class | ObjectHolder< T > |
Uses encapsulation to hide an object in derived classes. More... | |
class | NotCopyable |
Ensures an object is not copyable. More... | |
class | NewObject< T > |
An object factory function. More... | |
class | Singleton< T, F, instance > |
Restricts the instantiation of a class to one static object without locks. More... | |
class | GetBlock< T, B, A > |
Access a block of memory. More... | |
class | PutBlock< T, B, A > |
Access a block of memory. More... | |
class | BlockGetAndPut< T, B, GA, PA > |
Access a block of memory. More... | |
class | SafeShifter< overflow > |
Safely shift values when undefined behavior could occur. More... | |
class | SafeShifter< true > |
Shifts a value in the presence of overflow. More... | |
class | SafeShifter< false > |
Shifts a value in the absence of overflow. More... | |
Macros | |
#define | CRYPTOPP_FAST_ROTATE(x) 0 |
#define | SIZE_MAX ((std::numeric_limits<size_t>::max)()) |
#define | CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__) |
#define | CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) |
#define | CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y) |
#define | CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y |
#define | COUNTOF(x) (sizeof(x)/sizeof(x[0])) |
#define | MEMORY_BARRIER() |
#define | RETURN_IF_NONZERO(x) size_t returnedValue = x; if (returnedValue) return returnedValue |
#define | GETBYTE(x, y) (unsigned int)byte((x)>>(8*(y))) |
#define | CRYPTOPP_GET_BYTE_AS_BYTE(x, y) byte((x)>>(8*(y))) |
#define | CRYPTOPP_BLOCK_1(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+0);} size_t SS1() {return sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define | CRYPTOPP_BLOCK_2(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS1());} size_t SS2() {return SS1()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define | CRYPTOPP_BLOCK_3(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS2());} size_t SS3() {return SS2()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define | CRYPTOPP_BLOCK_4(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS3());} size_t SS4() {return SS3()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define | CRYPTOPP_BLOCK_5(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS4());} size_t SS5() {return SS4()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define | CRYPTOPP_BLOCK_6(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS5());} size_t SS6() {return SS5()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define | CRYPTOPP_BLOCK_7(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS6());} size_t SS7() {return SS6()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define | CRYPTOPP_BLOCK_8(n, t, s) t* m_##n() {return (t *)(void *)(m_aggregate+SS7());} size_t SS8() {return SS7()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define | CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate; |
Typedefs | |
typedef LittleEndian | NativeByteOrder |
Functions | |
void | memcpy_s (void *dest, size_t sizeInBytes, const void *src, size_t count) |
Bounds checking replacement for memcpy() More... | |
void | memmove_s (void *dest, size_t sizeInBytes, const void *src, size_t count) |
Bounds checking replacement for memmove() More... | |
template<class T > | |
void | vec_swap (T &a, T &b) |
Swaps two variables which are arrays. More... | |
void * | memset_z (void *ptr, int value, size_t num) |
Memory block initializer and eraser that attempts to survive optimizations. More... | |
template<class T > | |
const T & | STDMIN (const T &a, const T &b) |
Replacement function for std::min. More... | |
template<class T > | |
const T & | STDMAX (const T &a, const T &b) |
Replacement function for std::max. More... | |
template<class T1 , class T2 > | |
const T1 | UnsignedMin (const T1 &a, const T2 &b) |
Safe comparison of values that could be neagtive and incorrectly promoted. More... | |
template<class T1 , class T2 > | |
bool | SafeConvert (T1 from, T2 &to) |
Tests whether a conversion from -> to is safe to perform. More... | |
template<class T > | |
std::string | IntToString (T value, unsigned int base=10) |
Converts a value to a string. More... | |
template<> | |
CRYPTOPP_DLL std::string | IntToString< word64 > (word64 value, unsigned int base) |
Converts an unsigned value to a string. More... | |
template<> | |
CRYPTOPP_DLL std::string | IntToString< Integer > (Integer value, unsigned int base) |
Converts an Integer to a string. More... | |
template<class T > | |
unsigned int | Parity (T value) |
Returns the parity of a value. More... | |
template<class T > | |
unsigned int | BytePrecision (const T &value) |
Returns the number of 8-bit bytes or octets required for a value. More... | |
template<class T > | |
unsigned int | BitPrecision (const T &value) |
Returns the number of bits required for a value. More... | |
unsigned int | TrailingZeros (word32 v) |
Determines the number of trailing 0-bits in a value. More... | |
unsigned int | TrailingZeros (word64 v) |
Determines the number of trailing 0-bits in a value. More... | |
template<class T > | |
T | Crop (T value, size_t bits) |
Truncates the value to the specified number of bits. More... | |
size_t | BitsToBytes (size_t bitCount) |
Returns the number of 8-bit bytes or octets required for the specified number of bits. More... | |
size_t | BytesToWords (size_t byteCount) |
Returns the number of words required for the specified number of bytes. More... | |
size_t | BitsToWords (size_t bitCount) |
Returns the number of words required for the specified number of bits. More... | |
size_t | BitsToDwords (size_t bitCount) |
Returns the number of double words required for the specified number of bits. More... | |
CRYPTOPP_DLL void CRYPTOPP_API | xorbuf (byte *buf, const byte *mask, size_t count) |
Performs an XOR of a buffer with a mask. More... | |
CRYPTOPP_DLL void CRYPTOPP_API | xorbuf (byte *output, const byte *input, const byte *mask, size_t count) |
Performs an XOR of an input buffer with a mask and stores the result in an output buffer. More... | |
CRYPTOPP_DLL bool CRYPTOPP_API | VerifyBufsEqual (const byte *buf1, const byte *buf2, size_t count) |
Performs a near constant-time comparison of two equally sized buffers. More... | |
template<class T > | |
bool | IsPowerOf2 (const T &value) |
Tests whether a value is a power of 2. More... | |
template<class T1 , class T2 > | |
T1 | SaturatingSubtract (const T1 &a, const T2 &b) |
Performs a saturating subtract clamped at 0. More... | |
template<class T1 , class T2 > | |
T1 | SaturatingSubtract1 (const T1 &a, const T2 &b) |
Performs a saturating subtract clamped at 1. More... | |
template<class T1 , class T2 > | |
T2 | ModPowerOf2 (const T1 &a, const T2 &b) |
Reduces a value to a power of 2. More... | |
template<class T1 , class T2 > | |
T1 | RoundDownToMultipleOf (const T1 &n, const T2 &m) |
Rounds a value down to a multiple of a second value. More... | |
template<class T1 , class T2 > | |
T1 | RoundUpToMultipleOf (const T1 &n, const T2 &m) |
Rounds a value up to a multiple of a second value. More... | |
template<class T > | |
unsigned int | GetAlignmentOf () |
Returns the minimum alignment requirements of a type. More... | |
bool | IsAlignedOn (const void *ptr, unsigned int alignment) |
Determines whether ptr is aligned to a minimum value. More... | |
template<class T > | |
bool | IsAligned (const void *ptr) |
Determines whether ptr is minimally aligned. More... | |
ByteOrder | GetNativeByteOrder () |
Returns NativeByteOrder as an enumerated ByteOrder value. More... | |
bool | NativeByteOrderIs (ByteOrder order) |
Determines whether order follows native byte ordering. More... | |
template<class T > | |
CipherDir | GetCipherDir (const T &obj) |
Returns the direction the cipher is being operated. More... | |
CRYPTOPP_DLL void CRYPTOPP_API | CallNewHandler () |
Attempts to reclaim unused memory. More... | |
void | IncrementCounterByOne (byte *inout, unsigned int size) |
Performs an addition with carry on a block of bytes. More... | |
void | IncrementCounterByOne (byte *output, const byte *input, unsigned int size) |
Performs an addition with carry on a block of bytes. More... | |
template<class T > | |
void | ConditionalSwap (bool c, T &a, T &b) |
Performs a branchless swap of values a and b if condition c is true. More... | |
template<class T > | |
void | ConditionalSwapPointers (bool c, T &a, T &b) |
Performs a branchless swap of pointers a and b if condition c is true. More... | |
template<class T > | |
void | SecureWipeBuffer (T *buf, size_t n) |
Sets each element of an array to 0. More... | |
template<class T > | |
void | SecureWipeArray (T *buf, size_t n) |
Sets each element of an array to 0. More... | |
std::string | StringNarrow (const wchar_t *str, bool throwOnError=true) |
Converts a wide character C-string to a multibyte string. More... | |
CRYPTOPP_DLL void *CRYPTOPP_API | UnalignedAllocate (size_t size) |
Allocates a buffer. More... | |
CRYPTOPP_DLL void CRYPTOPP_API | UnalignedDeallocate (void *ptr) |
Frees a buffer allocated with UnalignedAllocate. More... | |
template<class T > | |
T | rotlFixed (T x, unsigned int y) |
Performs a left rotate. More... | |
template<class T > | |
T | rotrFixed (T x, unsigned int y) |
Performs a right rotate. More... | |
template<class T > | |
T | rotlVariable (T x, unsigned int y) |
Performs a left rotate. More... | |
template<class T > | |
T | rotrVariable (T x, unsigned int y) |
Performs a right rotate. More... | |
template<class T > | |
T | rotlMod (T x, unsigned int y) |
Performs a left rotate. More... | |
template<class T > | |
T | rotrMod (T x, unsigned int y) |
Performs a right rotate. More... | |
template<class T > | |
unsigned int | GetByte (ByteOrder order, T value, unsigned int index) |
Gets a byte from a value. More... | |
byte | ByteReverse (byte value) |
Reverses bytes in a 8-bit value. More... | |
word16 | ByteReverse (word16 value) |
Reverses bytes in a 16-bit value. More... | |
word32 | ByteReverse (word32 value) |
Reverses bytes in a 32-bit value. More... | |
word64 | ByteReverse (word64 value) |
Reverses bytes in a 64-bit value. More... | |
byte | BitReverse (byte value) |
Reverses bits in a 8-bit value. More... | |
word16 | BitReverse (word16 value) |
Reverses bits in a 16-bit value. More... | |
word32 | BitReverse (word32 value) |
Reverses bits in a 32-bit value. More... | |
word64 | BitReverse (word64 value) |
Reverses bits in a 64-bit value. More... | |
template<class T > | |
T | BitReverse (T value) |
Reverses bits in a value. More... | |
template<class T > | |
T | ConditionalByteReverse (ByteOrder order, T value) |
Reverses bytes in a value depending upon endianness. More... | |
template<class T > | |
void | ByteReverse (T *out, const T *in, size_t byteCount) |
Reverses bytes in an element from an array of elements. More... | |
template<class T > | |
void | ConditionalByteReverse (ByteOrder order, T *out, const T *in, size_t byteCount) |
Conditionally reverses bytes in an element from an array of elements. More... | |
template<class T > | |
void | GetUserKey (ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen) |
byte | UnalignedGetWordNonTemplate (ByteOrder order, const byte *block, const byte *) |
word16 | UnalignedGetWordNonTemplate (ByteOrder order, const byte *block, const word16 *) |
word32 | UnalignedGetWordNonTemplate (ByteOrder order, const byte *block, const word32 *) |
word64 | UnalignedGetWordNonTemplate (ByteOrder order, const byte *block, const word64 *) |
void | UnalignedbyteNonTemplate (ByteOrder order, byte *block, byte value, const byte *xorBlock) |
void | UnalignedbyteNonTemplate (ByteOrder order, byte *block, word16 value, const byte *xorBlock) |
void | UnalignedbyteNonTemplate (ByteOrder order, byte *block, word32 value, const byte *xorBlock) |
void | UnalignedbyteNonTemplate (ByteOrder order, byte *block, word64 value, const byte *xorBlock) |
template<class T > | |
T | GetWord (bool assumeAligned, ByteOrder order, const byte *block) |
Access a block of memory. More... | |
template<class T > | |
void | GetWord (bool assumeAligned, ByteOrder order, T &result, const byte *block) |
Access a block of memory. More... | |
template<class T > | |
void | PutWord (bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL) |
Access a block of memory. More... | |
template<class T > | |
std::string | WordToString (T value, ByteOrder order=BIG_ENDIAN_ORDER) |
template<class T > | |
T | StringToWord (const std::string &str, ByteOrder order=BIG_ENDIAN_ORDER) |
template<unsigned int bits, class T > | |
T | SafeRightShift (T value) |
template<unsigned int bits, class T > | |
T | SafeLeftShift (T value) |
Utility functions for the Crypto++ library.
Definition in file misc.h.
#define CRYPTOPP_ASSERT_JOIN | ( | X, | |
Y | |||
) | CRYPTOPP_DO_ASSERT_JOIN(X, Y) |
#define CRYPTOPP_BLOCK_1 | ( | n, | |
t, | |||
s | |||
) | t* m_##n() {return (t *)(void *)(m_aggregate+0);} size_t SS1() {return sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define CRYPTOPP_BLOCK_5 | ( | n, | |
t, | |||
s | |||
) | t* m_##n() {return (t *)(void *)(m_aggregate+SS4());} size_t SS5() {return SS4()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define CRYPTOPP_BLOCK_6 | ( | n, | |
t, | |||
s | |||
) | t* m_##n() {return (t *)(void *)(m_aggregate+SS5());} size_t SS6() {return SS5()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define CRYPTOPP_BLOCK_7 | ( | n, | |
t, | |||
s | |||
) | t* m_##n() {return (t *)(void *)(m_aggregate+SS6());} size_t SS7() {return SS6()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define CRYPTOPP_BLOCK_8 | ( | n, | |
t, | |||
s | |||
) | t* m_##n() {return (t *)(void *)(m_aggregate+SS7());} size_t SS8() {return SS7()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);} |
#define CRYPTOPP_BLOCKS_END | ( | i | ) | size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate; |
#define CRYPTOPP_COMPILE_ASSERT | ( | assertion | ) | CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__) |
#define CRYPTOPP_COMPILE_ASSERT_INSTANCE | ( | assertion, | |
instance | |||
) |
typedef LittleEndian NativeByteOrder |
unsigned int BitPrecision | ( | const T & | value | ) |
Reverses bits in a value.
value | the value to reverse |
The template overload of BitReverse operates on signed and unsigned values. Internally the size of T is checked, and then value is cast to a byte, word16, word32 or word64. After the cast, the appropriate BitReverse overload is called.
Definition at line 1784 of file misc.h.
|
inline |
Returns the number of 8-bit bytes or octets required for the specified number of bits.
bitCount | the number of bits |
BitsToBytes is effectively a ceiling function based on 8-bit bytes.
Definition at line 749 of file misc.h.
|
inline |
Returns the number of double words required for the specified number of bits.
bitCount | the number of bits |
BitsToDwords is effectively a ceiling function based on 2*WORD_BITS
. WORD_BITS
is defined in config.h
Definition at line 779 of file misc.h.
|
inline |
Returns the number of words required for the specified number of bits.
bitCount | the number of bits |
BitsToWords is effectively a ceiling function based on WORD_BITS
. WORD_BITS
is defined in config.h
Definition at line 769 of file misc.h.
unsigned int BytePrecision | ( | const T & | value | ) |
Reverses bytes in an element from an array of elements.
T | the class or type |
out | the output array of elements |
in | the input array of elements |
byteCount | the total number of bytes in the array |
Internally, ByteReverse visits each element in the in array calls ByteReverse on it, and writes the result to out.
ByteReverse does not process tail byes, or bytes that are not part of a full element. If T is int (and int is 4 bytes), then byteCount = 10
means only the first 2 elements or 8 bytes are reversed.
The follwoing program should help illustrate the behavior.
vector<word32> v1, v2;
v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4);
v2.resize(v1.size()); ByteReverse<word32>(&v2[0], &v1[0], 16);
cout << "V1: "; for(unsigned int i = 0; i < v1.size(); i++) cout << std::hex << v1[i] << " "; cout << endl;
cout << "V2: "; for(unsigned int i = 0; i < v2.size(); i++) cout << std::hex << v2[i] << " "; cout << endl;
The program above results in the follwoing output.
V1: 00000001 00000002 00000003 00000004 V2: 01000000 02000000 03000000 04000000
Definition at line 1848 of file misc.h.
|
inline |
Returns the number of words required for the specified number of bytes.
byteCount | the number of bytes |
BytesToWords is effectively a ceiling function based on WORD_SIZE
. WORD_SIZE
is defined in config.h
Definition at line 759 of file misc.h.
CRYPTOPP_DLL void CRYPTOPP_API CallNewHandler | ( | ) |
Attempts to reclaim unused memory.
bad_alloc |
In the normal course of running a program, a request for memory normally succeeds. If a call to AlignedAllocate or UnalignedAllocate fails, then CallNewHandler is called in an effort to recover. Internally, CallNewHandler calls set_new_handler(NULL) in an effort to free memory. There is no guarantee CallNewHandler will be able to procure more memory so an allocation succeeds. If the call to set_new_handler fails, then CallNewHandler throws a bad_alloc exception.
Definition at line 200 of file misc.cpp.
Reverses bytes in a value depending upon endianness.
T | the class or type |
order | the ByteOrder of the data |
value | the value to conditionally reverse |
Internally, the ConditionalByteReverse calls NativeByteOrderIs. If order matches native byte order, then the original value is returned. If not, then ByteReverse is called on the value before returning to the caller.
Definition at line 1807 of file misc.h.
|
inline |
Conditionally reverses bytes in an element from an array of elements.
T | the class or type |
order | the ByteOrder of the data |
out | the output array of elements |
in | the input array of elements |
byteCount | the byte count of the arrays |
Internally, ByteReverse visits each element in the in array calls ByteReverse on it depending on the desired endianness, and writes the result to out.
ByteReverse does not process tail byes, or bytes that are not part of a full element. If T is int (and int is 4 bytes), then byteCount = 10
means only the first 2 elements or 8 bytes are reversed.
Definition at line 1870 of file misc.h.
Truncates the value to the specified number of bits.
value | the value to truncate or mask |
bits | the number of bits to truncate or mask |
This function masks the low-order bits of value and returns the result. The mask is created with (1 << bits) - 1
.
Definition at line 737 of file misc.h.
|
inline |
Returns the minimum alignment requirements of a type.
Internally the function calls C++11's alignof
if available. If not available, then the function uses compiler specific extensions such as __alignof
and _alignof_
. If an extension is not available, then the function uses __BIGGEST_ALIGNMENT__
if __BIGGEST_ALIGNMENT__
is smaller than sizeof(T)
. sizeof(T)
is used if all others are not available. In all cases, if CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
is defined, then the function returns 1.
Definition at line 922 of file misc.h.
Returns the direction the cipher is being operated.
obj | the cipher object being queried |
ENCRYPTION
if the cipher obj is being operated in its forward direction, DECRYPTION
otherwiseA cipher can be operated in a "forward" direction (encryption) or a "reverse" direction (decryption). The operations do not have to be symmetric, meaning a second application of the transformation does not necessariy return the original message. That is, E(D(m))
may not equal E(E(m))
; and D(E(m))
may not equal D(D(m))
.
Definition at line 1008 of file misc.h.
|
inline |
Returns NativeByteOrder as an enumerated ByteOrder value.
NativeByteOrder is a typedef depending on the platform. If IS_LITTLE_ENDIAN is set in config.h, then GetNativeByteOrder returns LittleEndian. If IS_BIG_ENDIAN is set, then GetNativeByteOrder returns BigEndian.
Definition at line 985 of file misc.h.
Access a block of memory.
T | class or type |
assumeAligned | flag indicating alignment |
order | the ByteOrder of the data |
block | the byte buffer to be processed |
GetWord() provides alternate read access to a block of memory. The flag assumeAligned indicates if the memory block is aligned for class or type T. The enumeration ByteOrder is BIG_ENDIAN_ORDER or LITTLE_ENDIAN_ORDER.
An example of reading two word32 values from a block of memory is shown below. w
will be 0x03020100
.
word32 w; byte buffer[4] = {0,1,2,3}; w = GetWord<word32>(false, LITTLE_ENDIAN_ORDER, buffer);
Definition at line 2078 of file misc.h.
|
inline |
Access a block of memory.
T | class or type |
assumeAligned | flag indicating alignment |
order | the ByteOrder of the data |
result | the word in the specified byte order |
block | the byte buffer to be processed |
GetWord() provides alternate read access to a block of memory. The flag assumeAligned indicates if the memory block is aligned for class or type T. The enumeration ByteOrder is BIG_ENDIAN_ORDER or LITTLE_ENDIAN_ORDER.
An example of reading two word32 values from a block of memory is shown below. w
will be 0x03020100
.
word32 w; byte buffer[4] = {0,1,2,3}; w = GetWord<word32>(false, LITTLE_ENDIAN_ORDER, buffer);
|
inline |
Performs an addition with carry on a block of bytes.
inout | the byte block |
size | the size of the block, in bytes |
Performs an addition with carry by adding 1 on a block of bytes starting at the least significant byte. Once carry is 0, the function terminates and returns to the caller.
Definition at line 1029 of file misc.h.
Performs an addition with carry on a block of bytes.
output | the destination block of bytes |
input | the source block of bytes |
size | the size of the block |
Performs an addition with carry on a block of bytes starting at the least significant byte. Once carry is 0, the remaining bytes from input are copied to output using memcpy.
The function is close to near-constant time because it operates on all the bytes in the blocks.
Definition at line 1043 of file misc.h.
std::string IntToString | ( | T | value, |
unsigned int | base = 10 |
||
) |
CRYPTOPP_DLL std::string IntToString< Integer > | ( | Integer | value, |
unsigned int | base | ||
) |
Converts an Integer to a string.
value | the Integer to convert |
base | the base to use during the conversion |
This is a template specialization of IntToString(). Use it like IntToString():
// Print integer in base 10 Integer n... std::string s = IntToString(n, 10);
The string is presented with lowercase letters by default. A hack is available to switch to uppercase letters without modifying the function signature.
// Print integer in base 16, uppercase letters Integer n... const unsigned int UPPER = (1 << 31); std::string s = IntToString(n, (UPPER | 16));
Definition at line 4648 of file integer.cpp.
CRYPTOPP_DLL std::string IntToString< word64 > | ( | word64 | value, |
unsigned int | base | ||
) |
Converts an unsigned value to a string.
value | the value to convert |
base | the base to use during the conversion |
this template function specialization was added to suppress Coverity findings on IntToString() with unsigned types.
Definition at line 4714 of file integer.cpp.
|
inline |
Determines whether ptr is minimally aligned.
ptr | the pointer to check for alignment |
Internally the function calls IsAlignedOn with a second parameter of GetAlignmentOf<T>
Definition at line 964 of file misc.h.
|
inline |
Determines whether ptr is aligned to a minimum value.
ptr | the pointer being checked for alignment |
alignment | the alignment value to test the pointer against |
Internally the function tests whether alignment is 1. If so, the function returns true. If not, then the function effectively performs a modular reduction and returns true if the residue is 0
Definition at line 954 of file misc.h.
|
inline |
Tests whether a value is a power of 2.
value | the value to test |
The function creates a mask of value - 1
and returns the result of an AND operation compared to 0. If value is 0 or less than 0, then the function returns false.
Definition at line 818 of file misc.h.
|
inline |
Bounds checking replacement for memcpy()
dest | pointer to the desination memory block |
sizeInBytes | the size of the desination memory block, in bytes |
src | pointer to the source memory block |
count | the size of the source memory block, in bytes |
InvalidArgument |
ISO/IEC TR-24772 provides bounds checking interfaces for potentially unsafe functions like memcpy(), strcpy() and memmove(). However, not all standard libraries provides them, like Glibc. The library's memcpy_s() is a near-drop in replacement. Its only a near-replacement because the library's version throws an InvalidArgument on a bounds violation.
memcpy_s() and memmove_s() are guarded by STDC_WANT_SECURE_LIB. If STDC_WANT_SECURE_LIB is not defined or defined to 0, then the library makes memcpy_s() and memmove_s() available. The library will also optionally make the symbols available if CRYPTOPP_WANT_SECURE_LIB
is defined. CRYPTOPP_WANT_SECURE_LIB
is in config.h, but it is disabled by default.
memcpy_s() will assert the pointers src and dest are not NULL in debug builds. Passing NULL for either pointer is undefined behavior.
Definition at line 366 of file misc.h.
|
inline |
Bounds checking replacement for memmove()
dest | pointer to the desination memory block |
sizeInBytes | the size of the desination memory block, in bytes |
src | pointer to the source memory block |
count | the size of the source memory block, in bytes |
InvalidArgument |
ISO/IEC TR-24772 provides bounds checking interfaces for potentially unsafe functions like memcpy(), strcpy() and memmove(). However, not all standard libraries provides them, like Glibc. The library's memmove_s() is a near-drop in replacement. Its only a near-replacement because the library's version throws an InvalidArgument on a bounds violation.
memcpy_s() and memmove_s() are guarded by STDC_WANT_SECURE_LIB. If STDC_WANT_SECURE_LIB is not defined or defined to 0, then the library makes memcpy_s() and memmove_s() available. The library will also optionally make the symbols available if CRYPTOPP_WANT_SECURE_LIB
is defined. CRYPTOPP_WANT_SECURE_LIB
is in config.h, but it is disabled by default.
memmove_s() will assert the pointers src and dest are not NULL in debug builds. Passing NULL for either pointer is undefined behavior.
Definition at line 408 of file misc.h.
|
inline |
Memory block initializer and eraser that attempts to survive optimizations.
ptr | pointer to the memory block being written |
value | the integer value to write for each byte |
num | the size of the source memory block, in bytes |
Internally the function calls memset with the value value, and receives the return value from memset as a volatile
pointer.
Definition at line 461 of file misc.h.
Reduces a value to a power of 2.
a | the first value |
b | the second value |
a & (b-1)
. b
must be a power of 2. Use IsPowerOf2() to determine if b
is a suitable candidate. Definition at line 875 of file misc.h.
|
inline |
Determines whether order follows native byte ordering.
order | the ordering being tested against native byte ordering |
Definition at line 993 of file misc.h.
unsigned int Parity | ( | T | value | ) |
|
inline |
Access a block of memory.
T | class or type |
assumeAligned | flag indicating alignment |
order | the ByteOrder of the data |
block | the destination byte buffer |
value | the word in the specified byte order |
xorBlock | an optional byte buffer to xor |
PutWord() provides alternate write access to a block of memory. The flag assumeAligned indicates if the memory block is aligned for class or type T. The enumeration ByteOrder is BIG_ENDIAN_ORDER or LITTLE_ENDIAN_ORDER.
Definition at line 2123 of file misc.h.
Performs a left rotate.
T | the word type |
x | the value to rotate |
y | the number of bit positions to rotate the value |
This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits wide.
y must be in the range [0, sizeof(T)*8 - 1]
to avoid undefined behavior. Use rotlMod if the rotate amount y is outside the range.
rotate IMM
instruction because its often faster than a rotate REG
. Immediate rotates can be up to three times faster than their register counterparts. Definition at line 1263 of file misc.h.
Performs a left rotate.
T | the word type |
x | the value to rotate |
y | the number of bit positions to rotate the value |
This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits wide.
y is reduced to the range [0, sizeof(T)*8 - 1]
to avoid undefined behavior.
rotate IMM
or rotate REG
. Definition at line 1340 of file misc.h.
Performs a left rotate.
T | the word type |
x | the value to rotate |
y | the number of bit positions to rotate the value |
This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits wide.
y must be in the range [0, sizeof(T)*8 - 1]
to avoid undefined behavior. Use rotlMod if the rotate amount y is outside the range.
rotate IMM
instruction because its often faster than a rotate REG
. Immediate rotates can be up to three times faster than their register counterparts. Performs a right rotate.
T | the word type |
x | the value to rotate |
y | the number of bit positions to rotate the value |
This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits wide.
y must be in the range [0, sizeof(T)*8 - 1]
to avoid undefined behavior. Use rotrMod if the rotate amount y is outside the range.
rotate IMM
instruction because its often faster than a rotate REG
. Immediate rotates can be up to three times faster than their register counterparts. Definition at line 1285 of file misc.h.
Performs a right rotate.
T | the word type |
x | the value to rotate |
y | the number of bit positions to rotate the value |
This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits wide.
y is reduced to the range [0, sizeof(T)*8 - 1]
to avoid undefined behavior.
rotate IMM
or rotate REG
. Definition at line 1354 of file misc.h.
Performs a right rotate.
T | the word type |
x | the value to rotate |
y | the number of bit positions to rotate the value |
This is a portable C/C++ implementation. The value x to be rotated can be 8 to 64-bits wide.
y must be in the range [0, sizeof(T)*8 - 1]
to avoid undefined behavior. Use rotrMod if the rotate amount y is outside the range.
rotate IMM
instruction because its often faster than a rotate REG
. Immediate rotates can be up to three times faster than their register counterparts. Rounds a value down to a multiple of a second value.
n | the value to reduce |
m | the value to reduce to to a multiple |
RoundDownToMultipleOf is effectively a floor function based on m. The function returns the value n - n%m
. If n is a multiple of m, then the original value is returned.
Definition at line 889 of file misc.h.
Rounds a value up to a multiple of a second value.
n | the value to reduce |
m | the value to reduce to to a multiple |
RoundUpToMultipleOf is effectively a ceiling function based on m. The function returns the value n + n%m
. If n is a multiple of m, then the original value is returned. If the value n would overflow, then an InvalidArgument exception is thrown.
Definition at line 905 of file misc.h.
|
inline |
|
inline |
Performs a saturating subtract clamped at 0.
a | the minuend |
b | the subtrahend |
Saturating arithmetic restricts results to a fixed range. Results that are less than 0 are clamped at 0.
Use of saturating arithmetic in places can be advantageous because it can avoid a branch by using an instruction like a conditional move (CMOVE
).
Definition at line 847 of file misc.h.
Performs a saturating subtract clamped at 1.
a | the minuend |
b | the subtrahend |
Saturating arithmetic restricts results to a fixed range. Results that are less than 1 are clamped at 1.
Use of saturating arithmetic in places can be advantageous because it can avoid a branch by using an instruction like a conditional move (CMOVE
).
Definition at line 862 of file misc.h.
|
inline |
Sets each element of an array to 0.
buf | an array of elements |
n | the number of elements in the array |
The operation performs a wipe or zeroization. The function attempts to survive optimizations and dead code removal.
Definition at line 1192 of file misc.h.
void SecureWipeBuffer | ( | T * | buf, |
size_t | n | ||
) |
Sets each element of an array to 0.
buf | an array of elements |
n | the number of elements in the array |
The operation performs a wipe or zeroization. The function attempts to survive optimizations and dead code removal
Definition at line 1085 of file misc.h.
Replacement function for std::max.
a | the first value |
b | the second value |
a < b
using operator<
STDMAX was provided because the library could not easily use std::min or std::max in Windows or Cygwin 1.1.0
Definition at line 487 of file misc.h.
Replacement function for std::min.
a | the first value |
b | the second value |
b < a
using operator<
STDMIN was provided because the library could not easily use std::min or std::max in Windows or Cygwin 1.1.0
std::string StringNarrow | ( | const wchar_t * | str, |
bool | throwOnError = true |
||
) |
Converts a wide character C-string to a multibyte string.
str | C-string consisting of wide characters |
throwOnError | flag indication the function should throw on error |
StringNarrow converts a wide string to a narrow string using C++ std::wcstombs() under the executing thread's locale. A locale must be set before using this function, and it can be set with std::setlocale() if needed. Upon success, the converted string is returned.
Upon failure with throwOnError as false, the function returns an empty string. If throwOnError as true, the function throws an InvalidArgument() exception.
Definition at line 136 of file misc.cpp.
T StringToWord | ( | const std::string & | str, |
ByteOrder | order = BIG_ENDIAN_ORDER |
||
) |
|
inline |
Determines the number of trailing 0-bits in a value.
v | the 32-bit value to test |
TrailingZeros returns the number of trailing 0-bits in v, starting at the least significant bit position. The return value is undefined if there are no 1-bits set in the value v.
Definition at line 679 of file misc.h.
|
inline |
Determines the number of trailing 0-bits in a value.
v | the 64-bit value to test |
TrailingZeros returns the number of trailing 0-bits in v, starting at the least significant bit position. The return value is undefined if there are no 1-bits set in the value v.
Definition at line 710 of file misc.h.
CRYPTOPP_DLL void* CRYPTOPP_API UnalignedAllocate | ( | size_t | size | ) |
CRYPTOPP_DLL void CRYPTOPP_API UnalignedDeallocate | ( | void * | ptr | ) |
Safe comparison of values that could be neagtive and incorrectly promoted.
a | the first value |
b | the second value |
operator<
.The comparison b < a
is performed and the value returned is a's type T1.
Definition at line 512 of file misc.h.
Swaps two variables which are arrays.
a | the first value |
b | the second value |
C++03 does not provide support for std::swap(__m128i a, __m128i b)
because __m128i
is an unsigned long long[2]
. Most compilers support it out of the box, but Sun Studio C++ compilers 12.2 and 12.3 do not.
Definition at line 449 of file misc.h.
CRYPTOPP_DLL bool CRYPTOPP_API VerifyBufsEqual | ( | const byte * | buf1, |
const byte * | buf2, | ||
size_t | count | ||
) |
Performs a near constant-time comparison of two equally sized buffers.
buf1 | the first buffer |
buf2 | the second buffer |
count | the size of the buffers, in bytes |
The function effectively performs an XOR of the elements in two equally sized buffers and retruns a result based on the XOR operation. The function is near constant-time because CPU micro-code timings could affect the "constant-ness". Calling code is responsible for mitigating timing attacks if the buffers are not equally sized.
Definition at line 96 of file misc.cpp.
std::string WordToString | ( | T | value, |
ByteOrder | order = BIG_ENDIAN_ORDER |
||
) |
CRYPTOPP_DLL void CRYPTOPP_API xorbuf | ( | byte * | buf, |
const byte * | mask, | ||
size_t | count | ||
) |
Performs an XOR of a buffer with a mask.
buf | the buffer to XOR with the mask |
mask | the mask to XOR with the buffer |
count | the size of the buffers, in bytes |
The function effectively visits each element in the buffers and performs buf[i] ^= mask[i]
. buf and mask must be of equal size.
Definition at line 28 of file misc.cpp.
CRYPTOPP_DLL void CRYPTOPP_API xorbuf | ( | byte * | output, |
const byte * | input, | ||
const byte * | mask, | ||
size_t | count | ||
) |
Performs an XOR of an input buffer with a mask and stores the result in an output buffer.
output | the destination buffer |
input | the source buffer to XOR with the mask |
mask | the mask buffer to XOR with the input buffer |
count | the size of the buffers, in bytes |
The function effectively visits each element in the buffers and performs output[i] = input[i] ^ mask[i]
. output, input and mask must be of equal size.