/* * Secure Memory Buffers * (C) 1999-2007,2012 Jack Lloyd * * Distributed under the terms of the Botan license */ #ifndef BOTAN_SECURE_MEMORY_BUFFERS_H__ #define BOTAN_SECURE_MEMORY_BUFFERS_H__ #include #include #include #if defined(BOTAN_HAS_LOCKING_ALLOCATOR) #include #endif namespace Botan { template class secure_allocator { public: typedef T value_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; secure_allocator() noexcept {} ~secure_allocator() noexcept {} pointer address(reference x) const noexcept { return std::addressof(x); } const_pointer address(const_reference x) const noexcept { return std::addressof(x); } pointer allocate(size_type n, const void* = 0) { #if defined(BOTAN_HAS_LOCKING_ALLOCATOR) if(pointer p = static_cast(mlock_allocator::instance().allocate(n, sizeof(T)))) return p; #endif pointer p = new T[n]; clear_mem(p, n); return p; } void deallocate(pointer p, size_type n) { clear_mem(p, n); #if defined(BOTAN_HAS_LOCKING_ALLOCATOR) if(mlock_allocator::instance().deallocate(p, n, sizeof(T))) return; #endif delete [] p; } size_type max_size() const noexcept { return static_cast(-1) / sizeof(T); } template void construct(U* p, Args&&... args) { ::new(static_cast(p)) U(std::forward(args)...); } template void destroy(U* p) { p->~U(); } }; template inline bool operator==(const secure_allocator&, const secure_allocator&) { return true; } template inline bool operator!=(const secure_allocator&, const secure_allocator&) { return false; } template using secure_vector = std::vector>; template std::vector unlock(const secure_vector& in) { std::vector out(in.size()); copy_mem(&out[0], &in[0], in.size()); return out; } template size_t buffer_insert(std::vector& buf, size_t buf_offset, const T input[], size_t input_length) { const size_t to_copy = std::min(input_length, buf.size() - buf_offset); copy_mem(&buf[buf_offset], input, to_copy); return to_copy; } template size_t buffer_insert(std::vector& buf, size_t buf_offset, const std::vector& input) { const size_t to_copy = std::min(input.size(), buf.size() - buf_offset); copy_mem(&buf[buf_offset], &input[0], to_copy); return to_copy; } template std::vector& operator+=(std::vector& out, const std::vector& in) { const size_t copy_offset = out.size(); out.resize(out.size() + in.size()); copy_mem(&out[copy_offset], &in[0], in.size()); return out; } template std::vector& operator+=(std::vector& out, T in) { out.push_back(in); return out; } template std::vector& operator+=(std::vector& out, const std::pair& in) { const size_t copy_offset = out.size(); out.resize(out.size() + in.second); copy_mem(&out[copy_offset], in.first, in.second); return out; } template std::vector& operator+=(std::vector& out, const std::pair& in) { const size_t copy_offset = out.size(); out.resize(out.size() + in.second); copy_mem(&out[copy_offset], in.first, in.second); return out; } /** * Zeroise the values; length remains unchanged * @param vec the vector to zeroise */ template void zeroise(std::vector& vec) { clear_mem(&vec[0], vec.size()); } } #endif