2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2018-03-26 20:14:15 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
#include "td/utils/SharedSlice.h"
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2018-03-26 18:09:25 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
uint64 pq_factorize(uint64 pq);
|
|
|
|
|
|
|
|
#if TD_HAVE_OPENSSL
|
|
|
|
void init_crypto();
|
|
|
|
|
2020-06-17 05:37:37 +02:00
|
|
|
int pq_factorize(Slice pq_str, string *p_str, string *q_str);
|
|
|
|
|
2020-06-13 03:45:40 +02:00
|
|
|
class AesState {
|
2020-06-12 17:06:40 +02:00
|
|
|
public:
|
|
|
|
AesState();
|
|
|
|
AesState(const AesState &from) = delete;
|
|
|
|
AesState &operator=(const AesState &from) = delete;
|
|
|
|
AesState(AesState &&from);
|
|
|
|
AesState &operator=(AesState &&from);
|
|
|
|
~AesState();
|
2020-06-12 18:43:58 +02:00
|
|
|
|
2020-06-12 17:06:40 +02:00
|
|
|
void init(Slice key, bool encrypt);
|
2020-06-15 22:20:44 +02:00
|
|
|
|
2020-06-12 18:40:17 +02:00
|
|
|
void encrypt(const uint8 *src, uint8 *dst, int size);
|
2020-06-15 22:20:44 +02:00
|
|
|
|
2020-06-12 18:40:17 +02:00
|
|
|
void decrypt(const uint8 *src, uint8 *dst, int size);
|
2020-06-12 17:06:40 +02:00
|
|
|
|
|
|
|
private:
|
2020-06-16 17:19:05 +02:00
|
|
|
struct Impl;
|
2020-06-12 17:06:40 +02:00
|
|
|
unique_ptr<Impl> impl_;
|
|
|
|
};
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
void aes_ige_encrypt(Slice aes_key, MutableSlice aes_iv, Slice from, MutableSlice to);
|
|
|
|
void aes_ige_decrypt(Slice aes_key, MutableSlice aes_iv, Slice from, MutableSlice to);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-17 23:01:37 +02:00
|
|
|
class AesIgeStateImpl;
|
|
|
|
|
2020-06-16 05:16:09 +02:00
|
|
|
class AesIgeState {
|
2020-06-15 14:07:58 +02:00
|
|
|
public:
|
|
|
|
AesIgeState();
|
|
|
|
AesIgeState(const AesIgeState &from) = delete;
|
|
|
|
AesIgeState &operator=(const AesIgeState &from) = delete;
|
|
|
|
AesIgeState(AesIgeState &&from);
|
|
|
|
AesIgeState &operator=(AesIgeState &&from);
|
|
|
|
~AesIgeState();
|
2020-06-15 22:20:44 +02:00
|
|
|
|
2020-06-15 14:07:58 +02:00
|
|
|
void init(Slice key, Slice iv, bool encrypt);
|
2020-06-15 22:20:44 +02:00
|
|
|
|
2020-06-15 14:07:58 +02:00
|
|
|
void encrypt(Slice from, MutableSlice to);
|
2020-06-15 22:20:44 +02:00
|
|
|
|
2020-06-15 14:07:58 +02:00
|
|
|
void decrypt(Slice from, MutableSlice to);
|
|
|
|
|
|
|
|
private:
|
2020-06-17 23:01:37 +02:00
|
|
|
unique_ptr<AesIgeStateImpl> impl_;
|
2020-06-15 14:07:58 +02:00
|
|
|
};
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
void aes_cbc_encrypt(Slice aes_key, MutableSlice aes_iv, Slice from, MutableSlice to);
|
|
|
|
void aes_cbc_decrypt(Slice aes_key, MutableSlice aes_iv, Slice from, MutableSlice to);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
class AesCtrState {
|
|
|
|
public:
|
2020-06-17 21:24:31 +02:00
|
|
|
AesCtrState();
|
2018-12-31 20:04:05 +01:00
|
|
|
AesCtrState(const AesCtrState &from) = delete;
|
|
|
|
AesCtrState &operator=(const AesCtrState &from) = delete;
|
2020-06-17 21:24:31 +02:00
|
|
|
AesCtrState(AesCtrState &&from);
|
|
|
|
AesCtrState &operator=(AesCtrState &&from);
|
|
|
|
~AesCtrState();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
void init(Slice key, Slice iv);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void encrypt(Slice from, MutableSlice to);
|
|
|
|
|
|
|
|
void decrypt(Slice from, MutableSlice to);
|
|
|
|
|
|
|
|
private:
|
2020-06-17 21:24:31 +02:00
|
|
|
class Impl;
|
|
|
|
unique_ptr<Impl> ctx_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2018-03-26 16:00:26 +02:00
|
|
|
class AesCbcState {
|
|
|
|
public:
|
2019-08-12 13:45:57 +02:00
|
|
|
AesCbcState(Slice key256, Slice iv128);
|
2018-03-26 16:00:26 +02:00
|
|
|
|
|
|
|
void encrypt(Slice from, MutableSlice to);
|
|
|
|
void decrypt(Slice from, MutableSlice to);
|
2020-06-26 01:24:13 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
struct Raw {
|
|
|
|
SecureString key;
|
|
|
|
SecureString iv;
|
|
|
|
};
|
|
|
|
const Raw &raw() const {
|
|
|
|
return raw_;
|
|
|
|
}
|
2018-03-26 16:00:26 +02:00
|
|
|
|
|
|
|
private:
|
2020-06-24 13:47:36 +02:00
|
|
|
Raw raw_;
|
2018-03-26 16:00:26 +02:00
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void sha1(Slice data, unsigned char output[20]);
|
|
|
|
|
|
|
|
void sha256(Slice data, MutableSlice output);
|
|
|
|
|
2018-03-21 11:12:39 +01:00
|
|
|
void sha512(Slice data, MutableSlice output);
|
|
|
|
|
2018-08-10 01:35:07 +02:00
|
|
|
string sha256(Slice data) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
string sha512(Slice data) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2019-07-23 02:50:03 +02:00
|
|
|
class Sha256State {
|
|
|
|
public:
|
2018-12-31 20:04:05 +01:00
|
|
|
Sha256State();
|
2019-07-23 02:26:26 +02:00
|
|
|
Sha256State(const Sha256State &other) = delete;
|
|
|
|
Sha256State &operator=(const Sha256State &other) = delete;
|
|
|
|
Sha256State(Sha256State &&other);
|
|
|
|
Sha256State &operator=(Sha256State &&other);
|
2018-12-31 20:04:05 +01:00
|
|
|
~Sha256State();
|
2019-07-23 02:26:26 +02:00
|
|
|
|
2019-07-23 02:50:03 +02:00
|
|
|
void init();
|
|
|
|
|
|
|
|
void feed(Slice data);
|
|
|
|
|
|
|
|
void extract(MutableSlice dest, bool destroy = false);
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Impl;
|
|
|
|
unique_ptr<Impl> impl_;
|
|
|
|
bool is_inited_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void md5(Slice input, MutableSlice output);
|
|
|
|
|
|
|
|
void pbkdf2_sha256(Slice password, Slice salt, int iteration_count, MutableSlice dest);
|
2018-08-03 16:24:39 +02:00
|
|
|
void pbkdf2_sha512(Slice password, Slice salt, int iteration_count, MutableSlice dest);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void hmac_sha256(Slice key, Slice message, MutableSlice dest);
|
2019-07-06 13:29:15 +02:00
|
|
|
void hmac_sha512(Slice key, Slice message, MutableSlice dest);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-26 18:09:25 +02:00
|
|
|
// Interface may be improved
|
|
|
|
Result<BufferSlice> rsa_encrypt_pkcs1_oaep(Slice public_key, Slice data);
|
2018-03-26 20:14:15 +02:00
|
|
|
Result<BufferSlice> rsa_decrypt_pkcs1_oaep(Slice private_key, Slice data);
|
2018-03-26 18:09:25 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void init_openssl_threads();
|
2020-06-06 20:31:47 +02:00
|
|
|
|
|
|
|
Status create_openssl_error(int code, Slice message);
|
|
|
|
|
|
|
|
void clear_openssl_errors(Slice source);
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TD_HAVE_ZLIB
|
|
|
|
uint32 crc32(Slice data);
|
|
|
|
#endif
|
|
|
|
|
2018-12-19 15:48:39 +01:00
|
|
|
#if TD_HAVE_CRC32C
|
|
|
|
uint32 crc32c(Slice data);
|
2019-07-06 13:29:15 +02:00
|
|
|
uint32 crc32c_extend(uint32 old_crc, Slice data);
|
|
|
|
uint32 crc32c_extend(uint32 old_crc, uint32 new_crc, size_t data_size);
|
2018-12-19 15:48:39 +01:00
|
|
|
#endif
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
uint64 crc64(Slice data);
|
2019-07-06 13:29:15 +02:00
|
|
|
uint16 crc16(Slice data);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
} // namespace td
|