
◇ 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> >;
