Simplify AesCtrState more.

GitOrigin-RevId: 7601dc25f84a96828c333c30cab35eef5836d06c
This commit is contained in:
levlam 2020-06-17 22:49:04 +03:00
parent cdbe6f5002
commit 3464f04e57

View File

@ -353,17 +353,10 @@ struct AesCtrCounterPack {
return reinterpret_cast<const uint8 *>(this); return reinterpret_cast<const uint8 *>(this);
} }
size_t size() const { static size_t size() {
return sizeof(blocks); return sizeof(blocks);
} }
Slice as_slice() const {
return Slice(raw(), size());
}
MutableSlice as_mutable_slice() {
return MutableSlice(raw(), size());
}
void init(AesBlock block) { void init(AesBlock block) {
blocks[0] = block; blocks[0] = block;
for (size_t i = 1; i < BLOCK_COUNT; i++) { for (size_t i = 1; i < BLOCK_COUNT; i++) {
@ -662,30 +655,32 @@ class AesCtrState::Impl {
auto *dst = to.ubegin(); auto *dst = to.ubegin();
auto n = from.size(); auto n = from.size();
while (n != 0) { while (n != 0) {
if (current_.empty()) { size_t left = encrypted_counter_.raw() + AesCtrCounterPack::size() - current_;
if (left == 0) {
fill(); fill();
left = AesCtrCounterPack::size();
} }
size_t min_n = td::min(n, current_.size()); size_t min_n = td::min(n, left);
XorBytes::run(src, current_.ubegin(), dst, min_n); XorBytes::run(src, current_, dst, min_n);
src += min_n; src += min_n;
dst += min_n; dst += min_n;
n -= min_n; n -= min_n;
current_.remove_prefix(min_n); current_ += min_n;
} }
} }
private: private:
Evp evp_; Evp evp_;
uint8 *current_;
AesBlock counter_; AesBlock counter_;
AesCtrCounterPack encrypted_counter_; AesCtrCounterPack encrypted_counter_;
Slice current_;
void fill() { void fill() {
encrypted_counter_.init(counter_); encrypted_counter_.init(counter_);
counter_ = encrypted_counter_.blocks[AesCtrCounterPack::BLOCK_COUNT - 1].inc(); counter_ = encrypted_counter_.blocks[AesCtrCounterPack::BLOCK_COUNT - 1].inc();
evp_.encrypt(encrypted_counter_.raw(), encrypted_counter_.raw(), static_cast<int>(encrypted_counter_.size())); current_ = encrypted_counter_.raw();
current_ = encrypted_counter_.as_slice(); evp_.encrypt(current_, current_, static_cast<int>(AesCtrCounterPack::size()));
} }
}; };