I have some code in which I want to make absolutely sure that a moved-from std::vector
will not leave secret data around (think about crypto key management). In my class' move constructor, I do something like:
X(X&& rhs): secret_vector{std::move(rhs.secret_vector)}{
rhs.secret_vector.resize(N);
safe_zero(rhs.secret_vector); // zero out all elements
rhs.secret_vector.resize(0);
}
As you can see, I re-use the secret vector after moving from it. I looked at
but it was not absolutely clear that I can do this (I did not understand what "pre-conditions" really are).
My question is: can I resize a moved-from std::vector, perform some operation on it, then resize it back to zero?
std::move
does not move the content of the std::vector. So if you want to zero out the content of the vector, you can simplysafe_zero
it, no need to zerosecret_vector
after it has been moved. – PatchyX x = std::move(y);
, theny
is in a moved-from state. I want to make sure that whatever data is iny
is zeroed in. The standard DOES NOT guarantee that they
vector simply transfer the internal pointer, in fact a perfectly valid (but stupid) move will be to simply copy the data. – Femaleswap
would be valid implementation). – Utriclestd::vector(std::vector&&)
is guaranteedO(1)
? – Femaley
vector transfers it's internal pointer. – PatchyO(1)
complexity for (6)vector( vector&& other ) /*noexcept*/
. – UtricleN
, then resizing to 0. Oh.. you mean that the compiler may just decide to ignore my firstresize(N)
?! That will be bad! – Femaleresize
help. – Corneliacorneliandelete
-ing the move semantics? That may be indeed an option if things tend to get ugly... – Female0
. So I'm not worried aboutresize(N)
. This will simply allocate memory, and nothing will be copied around. Am I missing something here? Remember that first the vector is being moved-from, and that will guarantee that the size of the moved-from vector is zero. I just want to make sure that, no matter what, data does not "stick" around in what's being moved from. Crypto paranoia that you're probably very familiar with... – FemaleSignature s = Signature(some_secret_key)
. In this case, the rhs is a rvalue,s
will invoke the move constructor, and move the rhs intos
. However I want to make sure that "hot" memory in what was rhs is not hot anymore. I can just disable move semantics altogether, but for the sake of learning I wanted to see whether my solution is a valid one. Note that in the case of copying, I'm safe, as the destructor takes care of zeroing the memory out. – Femalestd::vector
will do. It could make an inaccessible copy. (It probably doesn't, but you can't know.) – Corneliacornelianvolatile
– Couch