2017-04-19 20:25:48 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-04-19 20:25:48 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-04-19 20:25:48 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2018-06-06 12:16:21 +00:00
|
|
|
#include "base/bytes.h"
|
2018-08-03 21:48:00 +00:00
|
|
|
#include "base/algorithm.h"
|
2018-07-30 20:34:23 +00:00
|
|
|
#include "base/basic_types.h"
|
2018-06-06 12:16:21 +00:00
|
|
|
|
|
|
|
extern "C" {
|
2017-04-19 20:25:48 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/sha.h>
|
2017-04-24 12:16:38 +00:00
|
|
|
#include <openssl/rand.h>
|
2018-03-27 12:16:00 +00:00
|
|
|
#include <openssl/aes.h>
|
2018-08-03 21:48:00 +00:00
|
|
|
#include <openssl/modes.h>
|
2018-03-27 12:16:00 +00:00
|
|
|
#include <openssl/crypto.h>
|
2018-08-03 21:48:00 +00:00
|
|
|
#include <openssl/evp.h>
|
2018-06-06 12:16:21 +00:00
|
|
|
} // extern "C"
|
2017-04-19 20:25:48 +00:00
|
|
|
|
|
|
|
namespace openssl {
|
|
|
|
|
|
|
|
class Context {
|
|
|
|
public:
|
|
|
|
Context() : _data(BN_CTX_new()) {
|
|
|
|
}
|
|
|
|
Context(const Context &other) = delete;
|
|
|
|
Context(Context &&other) : _data(base::take(other._data)) {
|
|
|
|
}
|
|
|
|
Context &operator=(const Context &other) = delete;
|
|
|
|
Context &operator=(Context &&other) {
|
|
|
|
_data = base::take(other._data);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
~Context() {
|
|
|
|
if (_data) {
|
|
|
|
BN_CTX_free(_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_CTX *raw() const {
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BN_CTX *_data = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class BigNum {
|
|
|
|
public:
|
2017-11-11 19:22:23 +00:00
|
|
|
BigNum() : _data(BN_new()) {
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
BigNum(const BigNum &other) : BigNum() {
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
BigNum &operator=(const BigNum &other) {
|
|
|
|
if (other.failed() || !BN_copy(raw(), other.raw())) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
~BigNum() {
|
|
|
|
BN_clear_free(raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit BigNum(unsigned int word) : BigNum() {
|
|
|
|
setWord(word);
|
|
|
|
}
|
2018-03-27 12:16:00 +00:00
|
|
|
explicit BigNum(bytes::const_span bytes) : BigNum() {
|
2017-04-19 20:25:48 +00:00
|
|
|
setBytes(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWord(unsigned int word) {
|
|
|
|
if (!BN_set_word(raw(), word)) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 12:16:00 +00:00
|
|
|
void setBytes(bytes::const_span bytes) {
|
2017-12-07 05:34:11 +00:00
|
|
|
if (!BN_bin2bn(
|
|
|
|
reinterpret_cast<const unsigned char*>(bytes.data()),
|
|
|
|
bytes.size(),
|
|
|
|
raw())) {
|
2017-04-19 20:25:48 +00:00
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 19:19:46 +00:00
|
|
|
|
|
|
|
void setAdd(const BigNum &a, const BigNum &b) {
|
|
|
|
if (a.failed() || b.failed()) {
|
2017-04-19 20:25:48 +00:00
|
|
|
_failed = true;
|
2018-08-10 19:19:46 +00:00
|
|
|
} else if (!BN_add(raw(), a.raw(), b.raw())) {
|
2017-04-19 20:25:48 +00:00
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 12:16:38 +00:00
|
|
|
void setSub(const BigNum &a, const BigNum &b) {
|
|
|
|
if (a.failed() || b.failed()) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (!BN_sub(raw(), a.raw(), b.raw())) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void setSubWord(unsigned int word) {
|
|
|
|
if (failed()) {
|
|
|
|
return;
|
|
|
|
} else if (!BN_sub_word(raw(), word)) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 19:19:46 +00:00
|
|
|
void setMul(
|
|
|
|
const BigNum &a,
|
|
|
|
const BigNum &b,
|
|
|
|
const Context &context = Context()) {
|
|
|
|
if (a.failed() || b.failed()) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (!BN_mul(raw(), a.raw(), b.raw(), context.raw())) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 11:49:46 +00:00
|
|
|
BN_ULONG setDivWord(BN_ULONG word) {
|
2017-04-24 12:16:38 +00:00
|
|
|
Expects(word != 0);
|
|
|
|
if (failed()) {
|
|
|
|
return (BN_ULONG)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = BN_div_word(raw(), word);
|
|
|
|
if (result == (BN_ULONG)-1) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-08-10 19:19:46 +00:00
|
|
|
void setModSub(
|
|
|
|
const BigNum &a,
|
|
|
|
const BigNum &b,
|
|
|
|
const BigNum &m,
|
|
|
|
const Context &context = Context()) {
|
|
|
|
if (a.failed() || b.failed() || m.failed()) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (a.isNegative() || b.isNegative() || m.isNegative()) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (!BN_mod_sub(raw(), a.raw(), b.raw(), m.raw(), context.raw())) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (isNegative()) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void setModMul(
|
|
|
|
const BigNum &a,
|
|
|
|
const BigNum &b,
|
|
|
|
const BigNum &m,
|
|
|
|
const Context &context = Context()) {
|
|
|
|
if (a.failed() || b.failed() || m.failed()) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (a.isNegative() || b.isNegative() || m.isNegative()) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (!BN_mod_mul(raw(), a.raw(), b.raw(), m.raw(), context.raw())) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (isNegative()) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void setModExp(
|
|
|
|
const BigNum &base,
|
|
|
|
const BigNum &power,
|
|
|
|
const BigNum &m,
|
|
|
|
const Context &context = Context()) {
|
|
|
|
if (base.failed() || power.failed() || m.failed()) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (base.isNegative() || power.isNegative() || m.isNegative()) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (!BN_mod_exp(raw(), base.raw(), power.raw(), m.raw(), context.raw())) {
|
|
|
|
_failed = true;
|
|
|
|
} else if (isNegative()) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
|
|
|
|
bool isNegative() const {
|
2017-04-24 12:16:38 +00:00
|
|
|
return failed() ? false : BN_is_negative(raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isPrime(const Context &context = Context()) const {
|
|
|
|
if (failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
constexpr auto kMillerRabinIterationCount = 30;
|
2017-12-07 05:34:11 +00:00
|
|
|
auto result = BN_is_prime_ex(
|
|
|
|
raw(),
|
|
|
|
kMillerRabinIterationCount,
|
|
|
|
context.raw(),
|
|
|
|
NULL);
|
2017-04-24 12:16:38 +00:00
|
|
|
if (result == 1) {
|
|
|
|
return true;
|
|
|
|
} else if (result != 0) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-05 11:49:46 +00:00
|
|
|
BN_ULONG modWord(BN_ULONG word) const {
|
2017-04-24 12:16:38 +00:00
|
|
|
Expects(word != 0);
|
|
|
|
if (failed()) {
|
|
|
|
return (BN_ULONG)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = BN_mod_word(raw(), word);
|
|
|
|
if (result == (BN_ULONG)-1) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bitsSize() const {
|
|
|
|
return failed() ? 0 : BN_num_bits(raw());
|
|
|
|
}
|
|
|
|
int bytesSize() const {
|
|
|
|
return failed() ? 0 : BN_num_bytes(raw());
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector getBytes() const {
|
2017-04-19 20:25:48 +00:00
|
|
|
if (failed()) {
|
2018-03-27 12:16:00 +00:00
|
|
|
return {};
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
auto length = BN_num_bytes(raw());
|
2018-03-27 12:16:00 +00:00
|
|
|
auto result = bytes::vector(length);
|
2017-12-07 05:34:11 +00:00
|
|
|
auto resultSize = BN_bn2bin(
|
|
|
|
raw(),
|
|
|
|
reinterpret_cast<unsigned char*>(result.data()));
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(resultSize == length);
|
2017-04-19 20:25:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIGNUM *raw() {
|
2017-11-11 19:22:23 +00:00
|
|
|
return _data;
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
|
|
|
const BIGNUM *raw() const {
|
2017-11-11 19:22:23 +00:00
|
|
|
return _data;
|
2017-04-19 20:25:48 +00:00
|
|
|
}
|
2017-12-07 05:34:11 +00:00
|
|
|
BIGNUM *takeRaw() {
|
|
|
|
return base::take(_data);
|
|
|
|
}
|
2017-04-19 20:25:48 +00:00
|
|
|
|
|
|
|
bool failed() const {
|
|
|
|
return _failed;
|
|
|
|
}
|
|
|
|
|
2018-08-10 19:19:46 +00:00
|
|
|
static BigNum Add(const BigNum &a, const BigNum &b) {
|
|
|
|
BigNum result;
|
|
|
|
result.setAdd(a, b);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static BigNum Sub(const BigNum &a, const BigNum &b) {
|
|
|
|
BigNum result;
|
|
|
|
result.setSub(a, b);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static BigNum Mul(
|
|
|
|
const BigNum &a,
|
|
|
|
const BigNum &b,
|
|
|
|
const Context &context = Context()) {
|
|
|
|
BigNum result;
|
|
|
|
result.setMul(a, b, context);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static BigNum ModSub(
|
|
|
|
const BigNum &a,
|
|
|
|
const BigNum &b,
|
|
|
|
const BigNum &mod,
|
|
|
|
const Context &context = Context()) {
|
|
|
|
BigNum result;
|
|
|
|
result.setModSub(a, b, mod, context);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static BigNum ModMul(
|
|
|
|
const BigNum &a,
|
|
|
|
const BigNum &b,
|
|
|
|
const BigNum &mod,
|
|
|
|
const Context &context = Context()) {
|
2017-04-24 12:16:38 +00:00
|
|
|
BigNum result;
|
2018-08-10 19:19:46 +00:00
|
|
|
result.setModMul(a, b, mod, context);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static BigNum ModExp(
|
|
|
|
const BigNum &base,
|
|
|
|
const BigNum &power,
|
|
|
|
const BigNum &mod,
|
|
|
|
const Context &context = Context()) {
|
|
|
|
BigNum result;
|
|
|
|
result.setModExp(base, power, mod, context);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static BigNum Failed() {
|
|
|
|
BigNum result;
|
|
|
|
result._failed = true;
|
2017-04-24 12:16:38 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
private:
|
2017-11-11 19:22:23 +00:00
|
|
|
BIGNUM *_data = nullptr;
|
2017-04-24 12:16:38 +00:00
|
|
|
mutable bool _failed = false;
|
2017-04-19 20:25:48 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-08-03 21:48:00 +00:00
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <typename Context, typename Method, typename Arg>
|
|
|
|
inline void ShaUpdate(Context context, Method method, Arg &&arg) {
|
|
|
|
const auto span = bytes::make_span(arg);
|
|
|
|
method(context, span.data(), span.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Context, typename Method, typename Arg, typename ...Args>
|
|
|
|
inline void ShaUpdate(Context context, Method method, Arg &&arg, Args &&...args) {
|
|
|
|
const auto span = bytes::make_span(arg);
|
|
|
|
method(context, span.data(), span.size());
|
|
|
|
ShaUpdate(context, method, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_type Size, typename Method>
|
|
|
|
inline bytes::vector Sha(Method method, bytes::const_span data) {
|
|
|
|
auto result = bytes::vector(Size);
|
|
|
|
method(
|
2018-03-27 12:16:00 +00:00
|
|
|
reinterpret_cast<const unsigned char*>(data.data()),
|
|
|
|
data.size(),
|
2018-03-21 04:35:32 +00:00
|
|
|
reinterpret_cast<unsigned char*>(result.data()));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:48:00 +00:00
|
|
|
template <
|
|
|
|
size_type Size,
|
|
|
|
typename Context,
|
|
|
|
typename Init,
|
|
|
|
typename Update,
|
|
|
|
typename Finalize,
|
|
|
|
typename ...Args,
|
|
|
|
typename = std::enable_if_t<(sizeof...(Args) > 1)>>
|
|
|
|
bytes::vector Sha(
|
|
|
|
Context context,
|
|
|
|
Init init,
|
|
|
|
Update update,
|
|
|
|
Finalize finalize,
|
|
|
|
Args &&...args) {
|
|
|
|
auto result = bytes::vector(Size);
|
|
|
|
|
|
|
|
init(&context);
|
|
|
|
ShaUpdate(&context, update, args...);
|
|
|
|
finalize(reinterpret_cast<unsigned char*>(result.data()), &context);
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:48:00 +00:00
|
|
|
template <
|
|
|
|
size_type Size,
|
|
|
|
typename Evp>
|
|
|
|
bytes::vector Pbkdf2(
|
|
|
|
bytes::const_span password,
|
|
|
|
bytes::const_span salt,
|
|
|
|
int iterations,
|
|
|
|
Evp evp) {
|
|
|
|
auto result = bytes::vector(Size);
|
|
|
|
PKCS5_PBKDF2_HMAC(
|
|
|
|
reinterpret_cast<const char*>(password.data()),
|
|
|
|
password.size(),
|
|
|
|
reinterpret_cast<const unsigned char*>(salt.data()),
|
|
|
|
salt.size(),
|
|
|
|
iterations,
|
|
|
|
evp,
|
|
|
|
result.size(),
|
2018-03-21 04:35:32 +00:00
|
|
|
reinterpret_cast<unsigned char*>(result.data()));
|
2017-04-19 20:25:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:48:00 +00:00
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
constexpr auto kSha1Size = size_type(SHA_DIGEST_LENGTH);
|
|
|
|
constexpr auto kSha256Size = size_type(SHA256_DIGEST_LENGTH);
|
|
|
|
constexpr auto kSha512Size = size_type(SHA512_DIGEST_LENGTH);
|
|
|
|
|
|
|
|
inline bytes::vector Sha1(bytes::const_span data) {
|
|
|
|
return details::Sha<kSha1Size>(SHA1, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename ...Args,
|
|
|
|
typename = std::enable_if_t<(sizeof...(Args) > 1)>>
|
|
|
|
inline bytes::vector Sha1(Args &&...args) {
|
|
|
|
return details::Sha<kSha1Size>(
|
|
|
|
SHA_CTX(),
|
2018-08-27 11:34:18 +00:00
|
|
|
SHA1_Init,
|
|
|
|
SHA1_Update,
|
|
|
|
SHA1_Final,
|
2018-08-03 21:48:00 +00:00
|
|
|
args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bytes::vector Sha256(bytes::const_span data) {
|
|
|
|
return details::Sha<kSha256Size>(SHA256, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename ...Args,
|
|
|
|
typename = std::enable_if_t<(sizeof...(Args) > 1)>>
|
|
|
|
inline bytes::vector Sha256(Args &&...args) {
|
|
|
|
return details::Sha<kSha256Size>(
|
|
|
|
SHA256_CTX(),
|
|
|
|
SHA256_Init,
|
|
|
|
SHA256_Update,
|
|
|
|
SHA256_Final,
|
|
|
|
args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bytes::vector Sha512(bytes::const_span data) {
|
|
|
|
return details::Sha<kSha512Size>(SHA512, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename ...Args,
|
|
|
|
typename = std::enable_if_t<(sizeof...(Args) > 1)>>
|
|
|
|
inline bytes::vector Sha512(Args &&...args) {
|
|
|
|
return details::Sha<kSha512Size>(
|
|
|
|
SHA512_CTX(),
|
|
|
|
SHA512_Init,
|
|
|
|
SHA512_Update,
|
|
|
|
SHA512_Final,
|
|
|
|
args...);
|
|
|
|
}
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
inline void AddRandomSeed(bytes::const_span data) {
|
|
|
|
RAND_seed(data.data(), data.size());
|
2018-03-19 16:22:27 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 21:48:00 +00:00
|
|
|
inline bytes::vector Pbkdf2Sha512(
|
|
|
|
bytes::const_span password,
|
|
|
|
bytes::const_span salt,
|
|
|
|
int iterations) {
|
|
|
|
return details::Pbkdf2<kSha512Size>(
|
|
|
|
password,
|
|
|
|
salt,
|
|
|
|
iterations,
|
|
|
|
EVP_sha512());
|
|
|
|
}
|
|
|
|
|
2017-04-19 20:25:48 +00:00
|
|
|
} // namespace openssl
|
2018-04-24 08:46:27 +00:00
|
|
|
|
|
|
|
namespace bytes {
|
|
|
|
|
|
|
|
inline void set_random(span destination) {
|
|
|
|
RAND_bytes(
|
|
|
|
reinterpret_cast<unsigned char*>(destination.data()),
|
|
|
|
destination.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace bytes
|