Skip to content

Commit

Permalink
Simplify AesCtrState more.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 7601dc25f84a96828c333c30cab35eef5836d06c
  • Loading branch information
levlam committed Jun 17, 2020
1 parent cdbe6f5 commit 3464f04
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions tdutils/td/utils/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,10 @@ struct AesCtrCounterPack {
return reinterpret_cast<const uint8 *>(this);
}

size_t size() const {
static size_t size() {
return sizeof(blocks);
}

Slice as_slice() const {
return Slice(raw(), size());
}
MutableSlice as_mutable_slice() {
return MutableSlice(raw(), size());
}

void init(AesBlock block) {
blocks[0] = block;
for (size_t i = 1; i < BLOCK_COUNT; i++) {
Expand Down Expand Up @@ -662,30 +655,32 @@ class AesCtrState::Impl {
auto *dst = to.ubegin();
auto n = from.size();
while (n != 0) {
if (current_.empty()) {
size_t left = encrypted_counter_.raw() + AesCtrCounterPack::size() - current_;
if (left == 0) {
fill();
left = AesCtrCounterPack::size();
}
size_t min_n = td::min(n, current_.size());
XorBytes::run(src, current_.ubegin(), dst, min_n);
size_t min_n = td::min(n, left);
XorBytes::run(src, current_, dst, min_n);
src += min_n;
dst += min_n;
n -= min_n;
current_.remove_prefix(min_n);
current_ += min_n;
}
}

private:
Evp evp_;

uint8 *current_;
AesBlock counter_;
AesCtrCounterPack encrypted_counter_;
Slice current_;

void fill() {
encrypted_counter_.init(counter_);
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_.as_slice();
current_ = encrypted_counter_.raw();
evp_.encrypt(current_, current_, static_cast<int>(AesCtrCounterPack::size()));
}
};

Expand Down

0 comments on commit 3464f04

Please sign in to comment.