2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
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"
|
|
|
|
#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();
|
|
|
|
|
|
|
|
int pq_factorize(Slice pq_str, string *p_str, string *q_str);
|
|
|
|
|
|
|
|
void aes_ige_encrypt(const UInt256 &aes_key, UInt256 *aes_iv, Slice from, MutableSlice to);
|
|
|
|
void aes_ige_decrypt(const UInt256 &aes_key, UInt256 *aes_iv, Slice from, MutableSlice to);
|
|
|
|
|
|
|
|
void aes_cbc_encrypt(const UInt256 &aes_key, UInt128 *aes_iv, Slice from, MutableSlice to);
|
|
|
|
void aes_cbc_decrypt(const UInt256 &aes_key, UInt128 *aes_iv, Slice from, MutableSlice to);
|
|
|
|
|
|
|
|
class AesCtrState {
|
|
|
|
public:
|
|
|
|
AesCtrState();
|
|
|
|
AesCtrState(const AesCtrState &from) = delete;
|
|
|
|
AesCtrState &operator=(const AesCtrState &from) = delete;
|
|
|
|
AesCtrState(AesCtrState &&from);
|
|
|
|
AesCtrState &operator=(AesCtrState &&from);
|
|
|
|
~AesCtrState();
|
|
|
|
|
|
|
|
void init(const UInt256 &key, const UInt128 &iv);
|
|
|
|
|
|
|
|
void encrypt(Slice from, MutableSlice to);
|
|
|
|
|
|
|
|
void decrypt(Slice from, MutableSlice to);
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Impl;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<Impl> ctx_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2018-03-26 16:00:26 +02:00
|
|
|
class AesCbcState {
|
|
|
|
public:
|
|
|
|
AesCbcState(const UInt256 &key, const UInt128 &iv);
|
|
|
|
|
|
|
|
void encrypt(Slice from, MutableSlice to);
|
|
|
|
void decrypt(Slice from, MutableSlice to);
|
|
|
|
|
|
|
|
private:
|
|
|
|
UInt256 key_;
|
|
|
|
UInt128 iv_;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct Sha256StateImpl;
|
|
|
|
|
|
|
|
struct Sha256State {
|
|
|
|
Sha256State();
|
|
|
|
Sha256State(Sha256State &&from);
|
|
|
|
Sha256State &operator=(Sha256State &&from);
|
|
|
|
~Sha256State();
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<Sha256StateImpl> impl;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void sha256_init(Sha256State *state);
|
|
|
|
void sha256_update(Slice data, Sha256State *state);
|
|
|
|
void sha256_final(Sha256State *state, MutableSlice output);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TD_HAVE_ZLIB
|
|
|
|
uint32 crc32(Slice data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint64 crc64(Slice data);
|
|
|
|
|
|
|
|
} // namespace td
|