Crypto++
5.6.5
Free C++ class library of cryptographic schemes
|
Go to the documentation of this file.
6 #ifndef CRYPTOPP_SECBLOCK_H
7 #define CRYPTOPP_SECBLOCK_H
13 #if CRYPTOPP_MSC_VERSION
14 # pragma warning(push)
15 # pragma warning(disable: 4700)
16 # if (CRYPTOPP_MSC_VERSION >= 1400)
17 # pragma warning(disable: 6386)
33 typedef size_t size_type;
34 #ifdef CRYPTOPP_MSVCRT6
35 typedef ptrdiff_t difference_type;
37 typedef std::ptrdiff_t difference_type;
40 typedef const T * const_pointer;
41 typedef T & reference;
42 typedef const T & const_reference;
44 pointer address(reference r)
const {
return (&r);}
45 const_pointer address(const_reference r)
const {
return (&r); }
46 void construct(pointer p,
const T& val) {
new (p) T(val);}
47 void destroy(pointer p) {CRYPTOPP_UNUSED(p); p->~T();}
57 #if defined(CRYPTOPP_CXX11_VARIADIC_TEMPLATES) || defined(CRYPTOPP_DOXYGEN_PROCESSING)
66 template<
typename U,
typename... Args>
67 void construct(U* ptr, Args&&... args) {::new ((
void*)ptr) U(std::forward<Args>(args)...);}
91 static void CheckSize(
size_t size)
95 throw InvalidArgument(
"AllocatorBase: requested size would cause integer overflow");
99 #define CRYPTOPP_INHERIT_ALLOCATOR_TYPES \
100 typedef typename AllocatorBase<T>::value_type value_type;\
101 typedef typename AllocatorBase<T>::size_type size_type;\
102 typedef typename AllocatorBase<T>::difference_type difference_type;\
103 typedef typename AllocatorBase<T>::pointer pointer;\
104 typedef typename AllocatorBase<T>::const_pointer const_pointer;\
105 typedef typename AllocatorBase<T>::reference reference;\
106 typedef typename AllocatorBase<T>::const_reference const_reference;
118 template <
class T,
class A>
119 typename A::pointer
StandardReallocate(A& alloc, T *oldPtr,
typename A::size_type oldSize,
typename A::size_type newSize,
bool preserve)
122 if (oldSize == newSize)
127 typename A::pointer newPointer = alloc.allocate(newSize, NULL);
128 const size_t copySize =
STDMIN(oldSize, newSize) *
sizeof(T);
130 if (oldPtr && newPointer) {
memcpy_s(newPointer, copySize, oldPtr, copySize);}
131 alloc.deallocate(oldPtr, oldSize);
136 alloc.deallocate(oldPtr, oldSize);
137 return alloc.allocate(newSize, NULL);
150 template <
class T,
bool T_Align16 = false>
154 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
171 pointer
allocate(size_type size,
const void *ptr = NULL)
174 this->CheckSize(size);
178 #if CRYPTOPP_BOOL_ALIGN16
180 if (T_Align16 && size*
sizeof(T) >= 16)
200 #if CRYPTOPP_BOOL_ALIGN16
201 if (T_Align16 && size*
sizeof(T) >= 16)
221 pointer
reallocate(T *oldPtr, size_type oldSize, size_type newSize,
bool preserve)
247 #if defined(CRYPTOPP_WORD128_AVAILABLE)
250 #if CRYPTOPP_BOOL_X86
267 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
272 pointer allocate(size_type n,
const void* unused = NULL)
274 CRYPTOPP_UNUSED(n); CRYPTOPP_UNUSED(unused);
278 void deallocate(
void *p, size_type n)
280 CRYPTOPP_UNUSED(p); CRYPTOPP_UNUSED(n);
284 CRYPTOPP_CONSTEXPR size_type max_size()
const {
return 0;}
300 template <
class T,
size_t S,
class A = NullAllocator<T>,
bool T_Align16 = false>
304 CRYPTOPP_INHERIT_ALLOCATOR_TYPES
325 if (size <= S && !m_allocated)
328 return GetAlignedArray();
331 return m_fallbackAllocator.allocate(size);
349 if (size <= S && !m_allocated)
352 return GetAlignedArray();
355 return m_fallbackAllocator.allocate(size, hint);
368 if (ptr == GetAlignedArray())
376 m_fallbackAllocator.deallocate(ptr, size);
396 pointer
reallocate(pointer oldPtr, size_type oldSize, size_type newSize,
bool preserve)
398 if (oldPtr == GetAlignedArray() && newSize <= S)
401 if (oldSize > newSize)
406 pointer newPointer =
allocate(newSize, NULL);
407 if (preserve && newSize)
409 const size_t copySize =
STDMIN(oldSize, newSize);
410 memcpy_s(newPointer,
sizeof(T)*newSize, oldPtr,
sizeof(T)*copySize);
416 CRYPTOPP_CONSTEXPR size_type max_size()
const {
return STDMAX(m_fallbackAllocator.max_size(), S);}
421 T* GetAlignedArray() {
return m_array;}
424 T* GetAlignedArray() {
return (CRYPTOPP_BOOL_ALIGN16 && T_Align16) ? (T*)(
void *)(((
byte *)m_array) + (0-(size_t)m_array)%16) : m_array;}
425 CRYPTOPP_ALIGN_DATA(8) T m_array[(CRYPTOPP_BOOL_ALIGN16 && T_Align16) ? S+8/sizeof(T) : S];
428 A m_fallbackAllocator;
436 template <
class T,
class A = AllocatorWithCleanup<T> >
440 typedef typename A::value_type value_type;
441 typedef typename A::pointer iterator;
442 typedef typename A::const_pointer const_iterator;
443 typedef typename A::size_type size_type;
451 : m_size(
size), m_ptr(m_alloc.allocate(
size, NULL)) { }
457 : m_size(t.m_size), m_ptr(m_alloc.allocate(t.m_size, NULL)) {
459 if (t.m_ptr) {
memcpy_s(m_ptr, m_size*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));}
471 : m_size(len), m_ptr(m_alloc.allocate(len, NULL)) {
474 memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));
476 memset(m_ptr, 0, m_size*
sizeof(T));
480 {m_alloc.deallocate(m_ptr, m_size);}
486 operator const void *()
const
491 operator const T *()
const
508 {
return m_ptr+m_size;}
511 const_iterator
end()
const
512 {
return m_ptr+m_size;}
516 typename A::pointer
data() {
return m_ptr;}
519 typename A::const_pointer
data()
const {
return m_ptr;}
524 size_type
size()
const {
return m_size;}
527 bool empty()
const {
return m_size == 0;}
534 const byte *
BytePtr()
const {
return (
const byte *)m_ptr;}
547 if (m_ptr && ptr && len)
548 {
memcpy_s(m_ptr, m_size*
sizeof(T), ptr, len*
sizeof(T));}
560 if (m_ptr && t.m_ptr && t.m_size)
561 {
memcpy_s(m_ptr, m_size*
sizeof(T), t, t.m_size*
sizeof(T));}
585 const size_type oldSize = m_size;
588 Grow(m_size+t.m_size);
589 memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
594 memcpy_s(m_ptr+oldSize, (m_size-oldSize)*
sizeof(T), m_ptr, oldSize*
sizeof(T));
608 if(!t.m_size)
return SecBlock(*
this);
611 if(m_size) {
memcpy_s(result.m_ptr, result.m_size*
sizeof(T), m_ptr, m_size*
sizeof(T));}
612 memcpy_s(result.m_ptr+m_size, (result.m_size-m_size)*
sizeof(T), t.m_ptr, t.m_size*
sizeof(T));
624 return m_size == t.m_size &&
625 VerifyBufsEqual(
reinterpret_cast<const byte*
>(m_ptr),
reinterpret_cast<const byte*
>(t.m_ptr), m_size*
sizeof(T));
647 void New(size_type newSize)
649 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
false);
663 if (m_ptr) {
memset_z(m_ptr, 0, m_size*
sizeof(T));}
675 if (newSize > m_size)
677 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
691 if (newSize > m_size)
693 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
694 memset_z(m_ptr+m_size, 0, (newSize-m_size)*
sizeof(T));
707 m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize,
true);
717 std::swap(m_alloc, b.m_alloc);
718 std::swap(m_size, b.m_size);
719 std::swap(m_ptr, b.m_ptr);
728 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
752 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S> >
765 template <
class T,
unsigned int S,
bool T_Align16 = true>
775 template <
class T,
unsigned int S,
class A = FixedSizeAllocatorWithCleanup<T, S, AllocatorWithCleanup<T> > >
783 template<
class T,
bool A,
class U,
bool B>
784 inline bool operator==(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<U, B>&) {
return (
true);}
785 template<
class T,
bool A,
class U,
bool B>
786 inline bool operator!=(
const CryptoPP::AllocatorWithCleanup<T, A>&,
const CryptoPP::AllocatorWithCleanup<U, B>&) {
return (
false);}
791 template <
class T,
class A>
792 inline void swap(CryptoPP::SecBlock<T, A> &a, CryptoPP::SecBlock<T, A> &b)
797 #if defined(_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || (defined(_STLPORT_VERSION) && !defined(_STLP_MEMBER_TEMPLATE_CLASSES))
799 template <
class _Tp1,
class _Tp2>
800 inline CryptoPP::AllocatorWithCleanup<_Tp2>&
801 __stl_alloc_rebind(CryptoPP::AllocatorWithCleanup<_Tp1>& __a,
const _Tp2*)
803 return (CryptoPP::AllocatorWithCleanup<_Tp2>&)(__a);
809 #if CRYPTOPP_MSC_VERSION
810 # pragma warning(pop)
void * memset_z(void *ptr, int value, size_t num)
Memory block initializer and eraser that attempts to survive optimizations.
Allocates a block of memory with cleanup.
void destroy(U *ptr)
Destroys an U constructed with variadic arguments.
Fixed size stack-based SecBlock with 16-byte alignment.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
bool operator==(const SecBlock< T, A > &t) const
Bitwise compare two SecBlocks.
FixedSizeAllocatorWithCleanup()
Constructs a FixedSizeAllocatorWithCleanup.
void * UnalignedAllocate(size_t size)
Allocates a buffer.
SecBlock(const T *ptr, size_type len)
Construct a SecBlock from an array of elements.
void UnalignedDeallocate(void *ptr)
Frees a buffer allocated with UnalignedAllocate.
void SecureWipeArray(T *buf, size_t n)
Sets each element of an array to 0.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
Base class for all allocators used by SecBlock.
bool empty() const
Determines if the SecBlock is empty.
void Grow(size_type newSize)
Change size and preserve contents.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
pointer reallocate(T *oldPtr, size_type oldSize, size_type newSize, bool preserve)
Reallocates a block of memory.
SecBlockWithHint(size_t size)
construct a SecBlockWithHint with a count of elements
void deallocate(void *ptr, size_type size)
Deallocates a block of memory.
void * AlignedAllocate(size_t size)
Allocates a buffer on 16-byte boundary.
#define SIZE_MAX
The maximum value of a machine word.
Stack-based SecBlock that grows into the heap.
pointer allocate(size_type size, const void *hint)
Allocates a block of memory.
SecBlock(const SecBlock< T, A > &t)
Copy construct a SecBlock from another SecBlock.
byte * BytePtr()
Provides a byte pointer to the first element in the memory block.
A::pointer StandardReallocate(A &alloc, T *oldPtr, typename A::size_type oldSize, typename A::size_type newSize, bool preserve)
Reallocation function.
pointer reallocate(pointer oldPtr, size_type oldSize, size_type newSize, bool preserve)
Reallocates a block of memory.
void construct(U *ptr, Args &&... args)
Constructs a new U using variadic arguments.
void AlignedDeallocate(void *ptr)
Frees a buffer allocated with AlignedAllocate.
void resize(size_type newSize)
Change size and preserve contents.
iterator end()
Provides an iterator pointing beyond the last element in the memory block.
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
const byte * BytePtr() const
Return a byte pointer to the first element in the memory block.
SecBlock< T, A > & operator=(const SecBlock< T, A > &t)
Assign contents from another SecBlock.
void swap(SecBlock< T, A > &b)
Swap contents with another SecBlock.
void CleanGrow(size_type newSize)
Change size and preserve contents.
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
Utility functions for the Crypto++ library.
void CleanNew(size_type newSize)
Change size without preserving contents.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
SecBlock< T, A > & operator+=(const SecBlock< T, A > &t)
Append contents from another SecBlock.
bool VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count)
Performs a near constant-time comparison of two equally sized buffers.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
const_iterator begin() const
Provides a constant iterator pointing to the first element in the memory block.
const_iterator end() const
Provides a constant iterator pointing beyond the last element in the memory block.
SecBlock(size_type size=0)
Construct a SecBlock with space for size elements.
pointer allocate(size_type size, const void *ptr=NULL)
Allocates a block of memory.
SecBlock< T, A > operator+(const SecBlock< T, A > &t)
Construct a SecBlock from this and another SecBlock.
void deallocate(void *ptr, size_type size)
Deallocates a block of memory.
void New(size_type newSize)
Change size without preserving contents.
Fixed size stack-based SecBlock.
size_type max_size() const
Returns the maximum number of elements the allocator can provide.
size_type size() const
Provides the count of elements in the SecBlock.
Template class memeber Rebind.
An invalid argument was detected.
pointer allocate(size_type size)
Allocates a block of memory.
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
Crypto++ library namespace.
FixedSizeSecBlock()
Construct a FixedSizeSecBlock.
Library configuration file.
A::pointer data()
Provides a pointer to the first element in the memory block.
bool operator!=(const OID &lhs, const OID &rhs)
Compare two OIDs for inequality.
Secure memory block with allocator and cleanup.
SecBlock using AllocatorWithCleanup<byte, true> typedef.
A::const_pointer data() const
Provides a pointer to the first element in the memory block.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
void Assign(const SecBlock< T, A > &t)
Copy contents from another SecBlock.
Static secure memory block with cleanup.
bool operator!=(const SecBlock< T, A > &t) const
Bitwise compare two SecBlocks.