Skip to content

Commit

Permalink
tdutils: use new aes ige for long plaintext
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 4bd8ddd20508e235c0fb8b40ac42b9dcabfed30c
  • Loading branch information
arseny30 committed Jun 15, 2020
1 parent 7e06d91 commit 132caf5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
31 changes: 31 additions & 0 deletions benchmark/bench_crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,36 @@ class AesCbcBench : public td::Benchmark {
}
};

class AesIgeShortBench : public td::Benchmark {
public:
static constexpr int DATA_SIZE = 16;
alignas(64) unsigned char data[DATA_SIZE];
td::UInt256 key;
td::UInt256 iv;

std::string get_description() const override {
return PSTRING() << "AES IGE OpenSSL [" << (DATA_SIZE) << "B]";
}

void start_up() override {
for (int i = 0; i < DATA_SIZE; i++) {
data[i] = 123;
}
td::Random::secure_bytes(as_slice(key));
td::Random::secure_bytes(as_slice(iv));
}

void run(int n) override {
td::MutableSlice data_slice(data, DATA_SIZE);
td::AesIgeState ige;
for (int i = 0; i < n; i++) {
ige.init(as_slice(key), as_slice(iv), true);
ige.encrypt(data_slice, data_slice);
//td::aes_ige_encrypt(as_slice(key), as_slice(iv), data_slice, data_slice);
}
}
};

BENCH(Rand, "std_rand") {
int res = 0;
for (int i = 0; i < n; i++) {
Expand Down Expand Up @@ -285,6 +315,7 @@ class Crc64Bench : public td::Benchmark {
int main() {
td::init_openssl_threads();

td::bench(AesIgeShortBench());
td::bench(AesCtrBench());
td::bench(AesEcbBench());
td::bench(AesIgeBench());
Expand Down
24 changes: 19 additions & 5 deletions tdutils/td/utils/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,10 @@ AesState::~AesState() = default;

void AesState::init(Slice key, bool encrypt) {
CHECK(key.size() == 32);
impl_ = make_unique<Impl>();
impl_->ctx = EVP_CIPHER_CTX_new();
if (!impl_) {
impl_ = make_unique<Impl>();
impl_->ctx = EVP_CIPHER_CTX_new();
}
CHECK(impl_->ctx);

if (encrypt) {
Expand Down Expand Up @@ -446,11 +448,21 @@ static void aes_ige_xcrypt(Slice aes_key, MutableSlice aes_iv, Slice from, Mutab
}

void aes_ige_encrypt(Slice aes_key, MutableSlice aes_iv, Slice from, MutableSlice to) {
aes_ige_xcrypt(aes_key, aes_iv, from, to, true);
if (from.size() <= 128) {
return aes_ige_xcrypt(aes_key, aes_iv, from, to, true);
}
AesIgeState state;
state.init(aes_key, aes_iv, true);
state.encrypt(from, to);
}

void aes_ige_decrypt(Slice aes_key, MutableSlice aes_iv, Slice from, MutableSlice to) {
aes_ige_xcrypt(aes_key, aes_iv, from, to, false);
if (from.size() <= 128) {
return aes_ige_xcrypt(aes_key, aes_iv, from, to, false);
}
AesIgeState state;
state.init(aes_key, aes_iv, false);
state.decrypt(from, to);
}

class AesIgeState::Impl {
Expand Down Expand Up @@ -512,7 +524,9 @@ AesIgeState::~AesIgeState() = default;
void AesIgeState::init(Slice key, Slice iv, bool encrypt) {
CHECK(key.size() == 32);
CHECK(iv.size() == 32);
impl_ = make_unique<Impl>();
if (!impl_) {
impl_ = make_unique<Impl>();
}
impl_->state.init(key, encrypt);
impl_->iv.load(iv.ubegin());
impl_->iv2.load(iv.ubegin() + AES_BLOCK_SIZE);
Expand Down

0 comments on commit 132caf5

Please sign in to comment.