30 secure_vector(secure_vector<T>
const&) =
default;
31 secure_vector(secure_vector<T> &&) =
default;
32 explicit secure_vector(
unsigned size): data_(size) {}
33 explicit secure_vector(
unsigned size, T _item): data_(size, _item) {}
34 explicit secure_vector(
const std::vector<T>& c): data_(c) {}
35 secure_vector(std::vector<T>&& c): data_(std::move(c)) {}
36 ~secure_vector() { clean(); }
38 static secure_vector<T> getRandom(
size_t size) {
39 secure_vector<T> ret(size);
40 std::random_device rdev;
42 std::uniform_int_distribution<int> rand_byte{ 0, std::numeric_limits<uint8_t>::max() };
44 std::uniform_int_distribution<uint8_t> rand_byte;
46 std::generate_n((uint8_t*)ret.data_.data(), ret.size()*
sizeof(T), std::bind(rand_byte, std::ref(rdev)));
49 secure_vector<T>& operator=(
const secure_vector<T>& c) {
56 secure_vector<T>& operator=(secure_vector<T>&& c) {
60 data_ = std::move(c.data_);
63 secure_vector<T>& operator=(std::vector<T>&& c) {
68 std::vector<T>& writable() { clean();
return data_; }
69 const std::vector<T>& makeInsecure()
const {
return data_; }
70 const uint8_t* data()
const {
return data_.data(); }
73 clean(data_.begin(), data_.end());
76 void clear() { clean(); data_.clear(); }
78 size_t size()
const {
return data_.size(); }
79 bool empty()
const {
return data_.empty(); }
81 void swap(secure_vector<T>& other) { data_.swap(other.data_); }
82 void resize(
size_t s) {
83 if (s == data_.size())
return;
84 if (s < data_.size()) {
86 clean(data_.begin()+s, data_.end());
90 auto data = std::move(data_);
93 std::copy(data.begin(), data.end(), data_.begin());
94 clean(data.begin(), data.end());
102 static void clean(
const typename std::vector<T>::iterator& i,
const typename std::vector<T>::iterator& j) {
103 volatile uint8_t* b =
reinterpret_cast<uint8_t*
>(&*i);
104 volatile uint8_t* e =
reinterpret_cast<uint8_t*
>(&*j);
108 std::vector<T> data_;