ext-cryptopp/secblock.h

468 lines
12 KiB
C
Raw Normal View History

2002-10-04 17:31:41 +00:00
// secblock.h - written and placed in the public domain by Wei Dai
#ifndef CRYPTOPP_SECBLOCK_H
#define CRYPTOPP_SECBLOCK_H
#include "config.h"
#include "misc.h"
#include <assert.h>
NAMESPACE_BEGIN(CryptoPP)
// ************** secure memory allocation ***************
template<class T>
class AllocatorBase
{
public:
typedef T value_type;
typedef size_t size_type;
#ifdef CRYPTOPP_MSVCRT6
2002-10-04 17:31:41 +00:00
typedef ptrdiff_t difference_type;
#else
typedef std::ptrdiff_t difference_type;
#endif
typedef T * pointer;
typedef const T * const_pointer;
typedef T & reference;
typedef const T & const_reference;
pointer address(reference r) const {return (&r);}
const_pointer address(const_reference r) const {return (&r); }
void construct(pointer p, const T& val) {new (p) T(val);}
void destroy(pointer p) {p->~T();}
size_type max_size() const {return ~size_type(0)/sizeof(T);} // switch to std::numeric_limits<T>::max later
protected:
static void CheckSize(size_t n)
{
if (n > ~size_t(0) / sizeof(T))
throw InvalidArgument("AllocatorBase: requested size would cause integer overflow");
}
2002-10-04 17:31:41 +00:00
};
#define CRYPTOPP_INHERIT_ALLOCATOR_TYPES \
typedef typename AllocatorBase<T>::value_type value_type;\
typedef typename AllocatorBase<T>::size_type size_type;\
typedef typename AllocatorBase<T>::difference_type difference_type;\
typedef typename AllocatorBase<T>::pointer pointer;\
typedef typename AllocatorBase<T>::const_pointer const_pointer;\
typedef typename AllocatorBase<T>::reference reference;\
typedef typename AllocatorBase<T>::const_reference const_reference;
2006-04-13 03:37:26 +00:00
#if defined(_MSC_VER) && (_MSC_VER < 1300)
// this pragma causes an internal compiler error if placed immediately before std::swap(a, b)
#pragma warning(push)
#pragma warning(disable: 4700) // VC60 workaround: don't know how to get rid of this warning
#endif
2002-10-04 17:31:41 +00:00
template <class T, class A>
typename A::pointer StandardReallocate(A& a, T *p, typename A::size_type oldSize, typename A::size_type newSize, bool preserve)
{
if (oldSize == newSize)
return p;
if (preserve)
{
2007-05-05 19:03:05 +00:00
typename A::pointer newPointer = a.allocate(newSize, NULL);
2006-03-13 13:26:41 +00:00
memcpy_s(newPointer, sizeof(T)*newSize, p, sizeof(T)*STDMIN(oldSize, newSize));
2002-10-04 17:31:41 +00:00
a.deallocate(p, oldSize);
return newPointer;
}
else
{
a.deallocate(p, oldSize);
return a.allocate(newSize, NULL);
}
}
2006-04-13 03:37:26 +00:00
#if defined(_MSC_VER) && (_MSC_VER < 1300)
#pragma warning(pop)
#endif
2007-04-16 00:22:35 +00:00
template <class T, bool T_Align16 = false>
2002-10-04 17:31:41 +00:00
class AllocatorWithCleanup : public AllocatorBase<T>
{
public:
CRYPTOPP_INHERIT_ALLOCATOR_TYPES
pointer allocate(size_type n, const void * = NULL)
{
this->CheckSize(n);
if (n == 0)
2002-10-04 17:31:41 +00:00
return NULL;
2007-04-16 00:22:35 +00:00
#if CRYPTOPP_BOOL_ALIGN16_ENABLED
if (T_Align16 && n*sizeof(T) >= 16)
return (pointer)AlignedAllocate(n*sizeof(T));
#endif
return (pointer)UnalignedAllocate(n*sizeof(T));
2002-10-04 17:31:41 +00:00
}
void deallocate(void *p, size_type n)
{
SecureWipeArray((pointer)p, n);
2007-04-16 00:22:35 +00:00
#if CRYPTOPP_BOOL_ALIGN16_ENABLED
if (T_Align16 && n*sizeof(T) >= 16)
return AlignedDeallocate(p);
#endif
UnalignedDeallocate(p);
2002-10-04 17:31:41 +00:00
}
pointer reallocate(T *p, size_type oldSize, size_type newSize, bool preserve)
{
return StandardReallocate(*this, p, oldSize, newSize, preserve);
}
// VS.NET STL enforces the policy of "All STL-compliant allocators have to provide a
// template class member called rebind".
2007-04-16 00:22:35 +00:00
template <class U> struct rebind { typedef AllocatorWithCleanup<U, T_Align16> other; };
2007-05-04 15:24:09 +00:00
#if _MSC_VER >= 1500
AllocatorWithCleanup() {}
template <class U, bool A> AllocatorWithCleanup(const AllocatorWithCleanup<U, A> &) {}
#endif
2002-10-04 17:31:41 +00:00
};
CRYPTOPP_DLL_TEMPLATE_CLASS AllocatorWithCleanup<byte>;
CRYPTOPP_DLL_TEMPLATE_CLASS AllocatorWithCleanup<word16>;
CRYPTOPP_DLL_TEMPLATE_CLASS AllocatorWithCleanup<word32>;
2007-04-16 00:22:35 +00:00
CRYPTOPP_DLL_TEMPLATE_CLASS AllocatorWithCleanup<word64>;
2007-05-04 15:24:09 +00:00
#if CRYPTOPP_BOOL_X86
CRYPTOPP_DLL_TEMPLATE_CLASS AllocatorWithCleanup<word, true>; // for Integer
#endif
2002-10-04 17:31:41 +00:00
template <class T>
class NullAllocator : public AllocatorBase<T>
{
public:
CRYPTOPP_INHERIT_ALLOCATOR_TYPES
pointer allocate(size_type n, const void * = NULL)
{
assert(false);
return NULL;
}
void deallocate(void *p, size_type n)
{
assert(false);
}
size_type max_size() const {return 0;}
2002-10-04 17:31:41 +00:00
};
// This allocator can't be used with standard collections because
// they require that all objects of the same allocator type are equivalent.
// So this is for use with SecBlock only.
2007-04-16 00:22:35 +00:00
template <class T, size_t S, class A = NullAllocator<T>, bool T_Align16 = false>
2002-10-04 17:31:41 +00:00
class FixedSizeAllocatorWithCleanup : public AllocatorBase<T>
{
public:
CRYPTOPP_INHERIT_ALLOCATOR_TYPES
FixedSizeAllocatorWithCleanup() : m_allocated(false) {}
2002-10-04 17:31:41 +00:00
pointer allocate(size_type n)
{
2007-04-16 00:22:35 +00:00
assert(IsAlignedOn(m_array, 8));
if (n <= S && !m_allocated)
2002-10-04 17:31:41 +00:00
{
m_allocated = true;
2007-04-16 00:22:35 +00:00
return GetAlignedArray();
2002-10-04 17:31:41 +00:00
}
else
return m_fallbackAllocator.allocate(n);
}
pointer allocate(size_type n, const void *hint)
{
if (n <= S && !m_allocated)
2002-10-04 17:31:41 +00:00
{
m_allocated = true;
2007-04-16 00:22:35 +00:00
return GetAlignedArray();
2002-10-04 17:31:41 +00:00
}
else
return m_fallbackAllocator.allocate(n, hint);
}
void deallocate(void *p, size_type n)
{
2007-04-16 00:22:35 +00:00
if (p == GetAlignedArray())
2002-10-04 17:31:41 +00:00
{
assert(n <= S);
2002-10-04 17:31:41 +00:00
assert(m_allocated);
m_allocated = false;
SecureWipeArray((pointer)p, n);
2002-10-04 17:31:41 +00:00
}
else
m_fallbackAllocator.deallocate(p, n);
}
pointer reallocate(pointer p, size_type oldSize, size_type newSize, bool preserve)
{
2007-04-16 00:22:35 +00:00
if (p == GetAlignedArray() && newSize <= S)
{
assert(oldSize <= S);
if (oldSize > newSize)
SecureWipeArray(p+newSize, oldSize-newSize);
2002-10-04 17:31:41 +00:00
return p;
}
2002-10-04 17:31:41 +00:00
pointer newPointer = allocate(newSize, NULL);
if (preserve)
memcpy(newPointer, p, sizeof(T)*STDMIN(oldSize, newSize));
deallocate(p, oldSize);
return newPointer;
2002-10-04 17:31:41 +00:00
}
size_type max_size() const {return STDMAX(m_fallbackAllocator.max_size(), S);}
2002-10-04 17:31:41 +00:00
private:
2007-05-05 18:29:44 +00:00
#ifdef __BORLANDC__
T* GetAlignedArray() {return m_array;}
T m_array[S];
#else
2009-03-13 02:55:23 +00:00
T* GetAlignedArray() {return (CRYPTOPP_BOOL_ALIGN16_ENABLED && T_Align16) ? (T*)(((byte *)m_array) + (0-(size_t)m_array)%16) : m_array;}
CRYPTOPP_ALIGN_DATA(8) T m_array[(CRYPTOPP_BOOL_ALIGN16_ENABLED && T_Align16) ? S+8/sizeof(T) : S];
2007-05-05 18:29:44 +00:00
#endif
A m_fallbackAllocator;
2002-10-04 17:31:41 +00:00
bool m_allocated;
};
//! a block of memory allocated using A
template <class T, class A = AllocatorWithCleanup<T> >
class SecBlock
{
public:
2005-07-12 04:23:32 +00:00
typedef typename A::value_type value_type;
typedef typename A::pointer iterator;
typedef typename A::const_pointer const_iterator;
typedef typename A::size_type size_type;
2007-05-05 18:29:44 +00:00
explicit SecBlock(size_type size=0)
2002-10-04 17:31:41 +00:00
: m_size(size) {m_ptr = m_alloc.allocate(size, NULL);}
SecBlock(const SecBlock<T, A> &t)
2006-03-13 13:26:41 +00:00
: m_size(t.m_size) {m_ptr = m_alloc.allocate(m_size, NULL); memcpy_s(m_ptr, m_size*sizeof(T), t.m_ptr, m_size*sizeof(T));}
2005-07-12 04:23:32 +00:00
SecBlock(const T *t, size_type len)
2002-10-04 17:31:41 +00:00
: m_size(len)
{
m_ptr = m_alloc.allocate(len, NULL);
if (t == NULL)
memset_z(m_ptr, 0, len*sizeof(T));
2002-10-04 17:31:41 +00:00
else
memcpy(m_ptr, t, len*sizeof(T));
}
~SecBlock()
{m_alloc.deallocate(m_ptr, m_size);}
#ifdef __BORLANDC__
operator T *() const
{return (T*)m_ptr;}
#else
2002-10-04 17:31:41 +00:00
operator const void *() const
{return m_ptr;}
operator void *()
{return m_ptr;}
operator const T *() const
{return m_ptr;}
operator T *()
{return m_ptr;}
#endif
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
// T *operator +(size_type offset)
// {return m_ptr+offset;}
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
// const T *operator +(size_type offset) const
// {return m_ptr+offset;}
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
// T& operator[](size_type index)
// {assert(index >= 0 && index < m_size); return m_ptr[index];}
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
// const T& operator[](size_type index) const
// {assert(index >= 0 && index < m_size); return m_ptr[index];}
2002-10-04 17:31:41 +00:00
iterator begin()
{return m_ptr;}
const_iterator begin() const
{return m_ptr;}
iterator end()
{return m_ptr+m_size;}
const_iterator end() const
{return m_ptr+m_size;}
typename A::pointer data() {return m_ptr;}
typename A::const_pointer data() const {return m_ptr;}
size_type size() const {return m_size;}
bool empty() const {return m_size == 0;}
2007-04-16 00:22:35 +00:00
byte * BytePtr() {return (byte *)m_ptr;}
const byte * BytePtr() const {return (const byte *)m_ptr;}
2006-03-13 13:26:41 +00:00
size_type SizeInBytes() const {return m_size*sizeof(T);}
2007-04-16 00:22:35 +00:00
//! set contents and size
2005-07-12 04:23:32 +00:00
void Assign(const T *t, size_type len)
2002-10-04 17:31:41 +00:00
{
New(len);
2006-03-13 13:26:41 +00:00
memcpy_s(m_ptr, m_size*sizeof(T), t, len*sizeof(T));
2002-10-04 17:31:41 +00:00
}
2007-04-16 00:22:35 +00:00
//! copy contents and size from another SecBlock
2002-10-04 17:31:41 +00:00
void Assign(const SecBlock<T, A> &t)
{
if (this != &t)
{
New(t.m_size);
memcpy_s(m_ptr, m_size*sizeof(T), t.m_ptr, m_size*sizeof(T));
}
2002-10-04 17:31:41 +00:00
}
SecBlock<T, A>& operator=(const SecBlock<T, A> &t)
2002-10-04 17:31:41 +00:00
{
Assign(t);
return *this;
}
2007-04-16 00:22:35 +00:00
// append to this object
SecBlock<T, A>& operator+=(const SecBlock<T, A> &t)
{
2005-07-12 04:23:32 +00:00
size_type oldSize = m_size;
Grow(m_size+t.m_size);
2006-03-13 13:26:41 +00:00
memcpy_s(m_ptr+oldSize, m_size*sizeof(T), t.m_ptr, t.m_size*sizeof(T));
return *this;
}
2007-04-16 00:22:35 +00:00
// append operator
SecBlock<T, A> operator+(const SecBlock<T, A> &t)
{
SecBlock<T, A> result(m_size+t.m_size);
2006-03-13 13:26:41 +00:00
memcpy_s(result.m_ptr, result.m_size*sizeof(T), m_ptr, m_size*sizeof(T));
memcpy_s(result.m_ptr+m_size, t.m_size*sizeof(T), t.m_ptr, t.m_size*sizeof(T));
return result;
}
2002-10-04 17:31:41 +00:00
bool operator==(const SecBlock<T, A> &t) const
{
return m_size == t.m_size && VerifyBufsEqual(m_ptr, t.m_ptr, m_size*sizeof(T));
2002-10-04 17:31:41 +00:00
}
bool operator!=(const SecBlock<T, A> &t) const
{
return !operator==(t);
}
2007-04-16 00:22:35 +00:00
//! change size, without preserving contents
2005-07-12 04:23:32 +00:00
void New(size_type newSize)
2002-10-04 17:31:41 +00:00
{
m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize, false);
m_size = newSize;
}
2007-04-16 00:22:35 +00:00
//! change size and set contents to 0
2005-07-12 04:23:32 +00:00
void CleanNew(size_type newSize)
2002-10-04 17:31:41 +00:00
{
New(newSize);
2009-03-13 02:55:23 +00:00
memset_z(m_ptr, 0, m_size*sizeof(T));
2002-10-04 17:31:41 +00:00
}
2007-04-16 00:22:35 +00:00
//! change size only if newSize > current size. contents are preserved
2005-07-12 04:23:32 +00:00
void Grow(size_type newSize)
2002-10-04 17:31:41 +00:00
{
if (newSize > m_size)
{
m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize, true);
m_size = newSize;
}
}
2007-04-16 00:22:35 +00:00
//! change size only if newSize > current size. contents are preserved and additional area is set to 0
2005-07-12 04:23:32 +00:00
void CleanGrow(size_type newSize)
2002-10-04 17:31:41 +00:00
{
if (newSize > m_size)
{
m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize, true);
memset(m_ptr+m_size, 0, (newSize-m_size)*sizeof(T));
m_size = newSize;
}
}
2007-04-16 00:22:35 +00:00
//! change size and preserve contents
2005-07-12 04:23:32 +00:00
void resize(size_type newSize)
2002-10-04 17:31:41 +00:00
{
m_ptr = m_alloc.reallocate(m_ptr, m_size, newSize, true);
m_size = newSize;
}
2007-04-16 00:22:35 +00:00
//! swap contents and size with another SecBlock
void swap(SecBlock<T, A> &b)
{
std::swap(m_alloc, b.m_alloc);
std::swap(m_size, b.m_size);
std::swap(m_ptr, b.m_ptr);
}
2002-10-04 17:31:41 +00:00
//private:
A m_alloc;
2005-07-12 04:23:32 +00:00
size_type m_size;
2002-10-04 17:31:41 +00:00
T *m_ptr;
};
typedef SecBlock<byte> SecByteBlock;
2009-03-13 02:55:23 +00:00
typedef SecBlock<byte, AllocatorWithCleanup<byte, true> > AlignedSecByteBlock;
2002-10-04 17:31:41 +00:00
typedef SecBlock<word> SecWordBlock;
2007-04-16 00:22:35 +00:00
//! a SecBlock with fixed size, allocated statically
2002-10-04 17:31:41 +00:00
template <class T, unsigned int S, class A = FixedSizeAllocatorWithCleanup<T, S> >
class FixedSizeSecBlock : public SecBlock<T, A>
{
public:
explicit FixedSizeSecBlock() : SecBlock<T, A>(S) {}
};
2009-03-13 02:55:23 +00:00
template <class T, unsigned int S, bool T_Align16 = true>
class FixedSizeAlignedSecBlock : public FixedSizeSecBlock<T, S, FixedSizeAllocatorWithCleanup<T, S, NullAllocator<T>, T_Align16> >
2007-04-16 00:22:35 +00:00
{
};
//! a SecBlock that preallocates size S statically, and uses the heap when this size is exceeded
2002-10-04 17:31:41 +00:00
template <class T, unsigned int S, class A = FixedSizeAllocatorWithCleanup<T, S, AllocatorWithCleanup<T> > >
class SecBlockWithHint : public SecBlock<T, A>
{
public:
2005-07-12 04:23:32 +00:00
explicit SecBlockWithHint(size_t size) : SecBlock<T, A>(size) {}
2002-10-04 17:31:41 +00:00
};
2007-05-04 15:24:09 +00:00
template<class T, bool A, class U, bool B>
inline bool operator==(const CryptoPP::AllocatorWithCleanup<T, A>&, const CryptoPP::AllocatorWithCleanup<U, B>&) {return (true);}
template<class T, bool A, class U, bool B>
inline bool operator!=(const CryptoPP::AllocatorWithCleanup<T, A>&, const CryptoPP::AllocatorWithCleanup<U, B>&) {return (false);}
2002-10-04 17:31:41 +00:00
NAMESPACE_END
NAMESPACE_BEGIN(std)
template <class T, class A>
inline void swap(CryptoPP::SecBlock<T, A> &a, CryptoPP::SecBlock<T, A> &b)
{
a.swap(b);
}
#if defined(_STLP_DONT_SUPPORT_REBIND_MEMBER_TEMPLATE) || (defined(_STLPORT_VERSION) && !defined(_STLP_MEMBER_TEMPLATE_CLASSES))
// working for STLport 5.1.3 and MSVC 6 SP5
2002-12-06 22:02:46 +00:00
template <class _Tp1, class _Tp2>
inline CryptoPP::AllocatorWithCleanup<_Tp2>&
__stl_alloc_rebind(CryptoPP::AllocatorWithCleanup<_Tp1>& __a, const _Tp2*)
{
return (CryptoPP::AllocatorWithCleanup<_Tp2>&)(__a);
}
#endif
2002-10-04 17:31:41 +00:00
NAMESPACE_END
#endif