tdutils: use new aes ige for long plaintext

GitOrigin-RevId: 4bd8ddd20508e235c0fb8b40ac42b9dcabfed30c
This commit is contained in:
Arseny Smirnov 2020-06-15 19:23:40 +03:00
parent 7e06d91739
commit 132caf5c8f
2 changed files with 50 additions and 5 deletions

View File

@ -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++) {
@ -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());

View File

@ -393,8 +393,10 @@ AesState::~AesState() = default;
void AesState::init(Slice key, bool encrypt) {
CHECK(key.size() == 32);
if (!impl_) {
impl_ = make_unique<Impl>();
impl_->ctx = EVP_CIPHER_CTX_new();
}
CHECK(impl_->ctx);
if (encrypt) {
@ -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 {
@ -512,7 +524,9 @@ AesIgeState::~AesIgeState() = default;
void AesIgeState::init(Slice key, Slice iv, bool encrypt) {
CHECK(key.size() == 32);
CHECK(iv.size() == 32);
if (!impl_) {
impl_ = make_unique<Impl>();
}
impl_->state.init(key, encrypt);
impl_->iv.load(iv.ubegin());
impl_->iv2.load(iv.ubegin() + AES_BLOCK_SIZE);