secure_allocatorとは?
ブロックチェーンでは「鍵」を扱うため、
Secure ZeroMemory、メモリロック、メモリプロテクト、ならびにSecure Allocatorの実装が必須です。
このうちここでは、Secure Allocatorを扱います。
独自のアロケータとは?
std::vectorやstd::stringなど、動的確保(ヒープ)を活用するクラスに組み込む、任意のロジックです。
まず、デフォルト(未指定)のアロケータは通常の確保および解放となりますので、
スワップを使い、解放後の情報は残ります。
これでは、ブロックチェーンの「鍵」の処理にて、std::vectorやstd::stringが使用できません。
そこで、これらクラスが使用する動的確保および解放に、
::mlock(::VirtualLock)およびSecure ZeroMemoryを組み込むための仕組みです。
サンプルコード
template<typename T>
struct zero_after_free_allocator : public std::allocator<T>
{
typedef std::allocator<T> base;
typedef typename base::size_type size_type;
typedef typename base::difference_type difference_type;
typedef typename base::pointer pointer;
typedef typename base::const_pointer const_pointer;
typedef typename base::reference reference;
typedef typename base::const_reference const_reference;
typedef typename base::value_type value_type;
zero_after_free_allocator() throw() {}
zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
template <typename U>
zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a) {}
~zero_after_free_allocator() throw() {}
template<typename _Other> struct rebind
{
typedef zero_after_free_allocator<_Other> other;
};
void deallocate(T *p, std::size_t n) {
if (p != nullptr) {
cleanse::OPENSSL_cleanse(p, sizeof(T) * n);
}
std::allocator<T>::deallocate(p, n);
}
};
using SecureString = std::basic_string<char, std::char_traits<char>, zero_after_free_allocator<char> >;
□ 本ページをご覧いただきました方は、以下のページもよくご覧いただいております。
■ 集計中.....