AES fixes.

GitOrigin-RevId: bf30c8c381edb623fec0dfac1c494559f3f3739e
This commit is contained in:
levlam 2020-06-12 19:43:58 +03:00
parent e913c3126b
commit f6ef851fe2
5 changed files with 34 additions and 20 deletions

View File

@ -48,7 +48,7 @@ class SHA1Bench : public td::Benchmark {
}
};
class AesBench : public td::Benchmark {
class AesEcbBench : public td::Benchmark {
public:
alignas(64) unsigned char data[DATA_SIZE];
td::UInt256 key;
@ -72,7 +72,7 @@ class AesBench : public td::Benchmark {
td::MutableSlice data_slice(data, DATA_SIZE);
for (int i = 0; i <= n; i++) {
size_t step = 16;
for (size_t offset = 0; offset + 16 <= data_slice.size(); offset += step) {
for (size_t offset = 0; offset + step <= data_slice.size(); offset += step) {
state.encrypt(data_slice.ubegin() + offset, data_slice.ubegin() + offset, (int)step);
}
}
@ -107,14 +107,14 @@ class AesCtrBench : public td::Benchmark {
}
};
class AESBench : public td::Benchmark {
class AesIgeBench : public td::Benchmark {
public:
alignas(64) unsigned char data[DATA_SIZE];
td::UInt256 key;
td::UInt256 iv;
std::string get_description() const override {
return PSTRING() << "AES OpenSSL [" << (DATA_SIZE >> 10) << "KB]";
return PSTRING() << "AES IGE OpenSSL [" << (DATA_SIZE >> 10) << "KB]";
}
void start_up() override {
@ -256,9 +256,9 @@ class Crc64Bench : public td::Benchmark {
int main() {
td::init_openssl_threads();
td::bench(AesEcbBench());
td::bench(AesCtrBench());
td::bench(AesBench());
td::bench(AESBench());
td::bench(AesIgeBench());
td::bench(Pbkdf2Bench());
td::bench(RandBench());

View File

@ -160,6 +160,7 @@ uint64 Random::Xorshift128plus::operator()() {
seed_[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
return seed_[1] + y;
}
int Random::Xorshift128plus::fast(int min, int max) {
return static_cast<int>((*this)() % (max - min + 1) + min);
}
@ -173,7 +174,7 @@ void Random::Xorshift128plus::bytes(MutableSlice dest) {
cnt = 8;
}
cnt--;
c = buf & 255;
c = static_cast<char>(buf & 255);
buf >>= 8;
}
}

View File

@ -252,9 +252,13 @@ int pq_factorize(Slice pq_str, string *p_str, string *q_str) {
class AesState::Impl {
public:
EVP_CIPHER_CTX *ctx{nullptr};
AES_KEY key;
bool encrypt;
Impl() = default;
Impl(const Impl &from) = delete;
Impl &operator=(const Impl &from) = delete;
Impl(Impl &&from) = delete;
Impl &operator=(Impl &&from) = delete;
~Impl() {
if (ctx != nullptr) {
EVP_CIPHER_CTX_free(ctx);
@ -281,19 +285,24 @@ void AesState::init(Slice key, bool encrypt) {
}
void AesState::encrypt(const uint8 *src, uint8 *dst, int size) {
CHECK(impl_ != nullptr);
CHECK(impl_->encrypt);
CHECK(impl_->ctx);
CHECK(impl_->ctx != nullptr);
CHECK(size % 16 == 0);
int len;
CHECK(1 == EVP_EncryptUpdate(impl_->ctx, dst, &len, src, size));
int res = EVP_EncryptUpdate(impl_->ctx, dst, &len, src, size);
LOG_IF(FATAL, res != 1);
CHECK(len == size);
}
void AesState::decrypt(const uint8 *src, uint8 *dst, int size) {
CHECK(impl_ != nullptr);
CHECK(!impl_->encrypt);
CHECK(impl_->ctx);
CHECK(impl_->ctx != nullptr);
CHECK(size % 16 == 0);
int len;
CHECK(1 == EVP_DecryptUpdate(impl_->ctx, dst, &len, src, size));
int res = EVP_DecryptUpdate(impl_->ctx, dst, &len, src, size);
LOG_IF(FATAL, res != 1);
CHECK(len == size);
}

View File

@ -27,6 +27,7 @@ struct AesState {
AesState(AesState &&from);
AesState &operator=(AesState &&from);
~AesState();
void init(Slice key, bool encrypt);
void encrypt(const uint8 *src, uint8 *dst, int size);
void decrypt(const uint8 *src, uint8 *dst, int size);

View File

@ -9,6 +9,7 @@
#include "td/utils/common.h"
#include "td/utils/crypto.h"
#include "td/utils/logging.h"
#include "td/utils/Random.h"
#include "td/utils/Slice.h"
#include "td/utils/tests.h"
#include "td/utils/UInt.h"
@ -18,13 +19,14 @@
static td::vector<td::string> strings{"", "1", "short test string", td::string(1000000, 'a')};
#if TD_HAVE_OPENSSL
#if TD_HAVE_ZLIB
TEST(Crypto, Aes) {
td::Random::Xorshift128plus rnd(123);
td::UInt256 key;
rnd.bytes(as_slice(key));
std::string plaintext(16, 0);
std::string encrypted(16, 0);
std::string decrypted(16, 0);
td::string plaintext(16, '\0');
td::string encrypted(16, '\0');
td::string decrypted(16, '\0');
rnd.bytes(plaintext);
td::AesState encryptor;
@ -39,6 +41,7 @@ TEST(Crypto, Aes) {
CHECK(decrypted != encrypted);
CHECK(td::crc32(encrypted) == 178892237);
}
#endif
TEST(Crypto, AesCtrState) {
td::vector<td::uint32> answers1{0u, 1141589763u, 596296607u, 3673001485u, 2302125528u,
@ -107,8 +110,8 @@ TEST(Crypto, Sha256State) {
}
TEST(Crypto, PBKDF) {
td::vector<td::string> passwords{"", "qwerty", std::string(1000, 'a')};
td::vector<td::string> salts{"", "qwerty", std::string(1000, 'a')};
td::vector<td::string> passwords{"", "qwerty", td::string(1000, 'a')};
td::vector<td::string> salts{"", "qwerty", td::string(1000, 'a')};
td::vector<int> iteration_counts{1, 2, 1000};
td::vector<td::Slice> answers{
"984LZT0tcqQQjPWr6RL/3Xd2Ftu7J6cOggTzri0Pb60=", "lzmEEdaupDp3rO+SImq4J41NsGaL0denanJfdoCsRcU=",
@ -209,7 +212,7 @@ TEST(Crypto, crc32c_benchmark) {
public:
explicit Crc32cExtendBenchmark(size_t chunk_size) : chunk_size_(chunk_size) {
}
std::string get_description() const override {
td::string get_description() const override {
return PSTRING() << "Crc32c with chunk_size=" << chunk_size_;
}
void start_up_n(int n) override {
@ -219,7 +222,7 @@ TEST(Crypto, crc32c_benchmark) {
} else {
cnt_ = 1;
}
data_ = std::string(n, 'a');
data_ = td::string(n, 'a');
}
void run(int n) override {
td::uint32 res = 0;
@ -236,7 +239,7 @@ TEST(Crypto, crc32c_benchmark) {
private:
size_t chunk_size_;
std::string data_;
td::string data_;
int cnt_;
};
bench(Crc32cExtendBenchmark(2));