2018-03-26 16:07:04 +02:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-03-26 16:07:04 +02: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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/SecureStorage.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-12-19 22:57:56 +01:00
|
|
|
#include "td/utils/as.h"
|
2018-04-07 01:38:28 +02:00
|
|
|
#include "td/utils/format.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
#include "td/utils/logging.h"
|
2018-03-26 16:07:04 +02:00
|
|
|
#include "td/utils/misc.h"
|
2020-01-03 02:16:05 +01:00
|
|
|
#include "td/utils/port/FileFd.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
#include "td/utils/Random.h"
|
2019-08-14 02:13:34 +02:00
|
|
|
#include "td/utils/SharedSlice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
namespace td {
|
|
|
|
namespace secure_storage {
|
2018-03-27 15:11:15 +02:00
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
// Helpers
|
2018-03-27 15:11:15 +02:00
|
|
|
Result<ValueHash> ValueHash::create(Slice data) {
|
|
|
|
UInt256 hash;
|
|
|
|
if (data.size() != ::td::as_slice(hash).size()) {
|
2018-04-06 15:24:29 +02:00
|
|
|
return Status::Error(PSLICE() << "Wrong hash size " << data.size());
|
2018-03-27 15:11:15 +02:00
|
|
|
}
|
|
|
|
::td::as_slice(hash).copy_from(data);
|
|
|
|
return ValueHash{hash};
|
|
|
|
}
|
|
|
|
|
2018-08-04 08:55:49 +02:00
|
|
|
static AesCbcState calc_aes_cbc_state_hash(Slice hash) {
|
|
|
|
CHECK(hash.size() == 64);
|
2019-08-12 15:53:17 +02:00
|
|
|
SecureString key(32);
|
|
|
|
as_mutable_slice(key).copy_from(hash.substr(0, 32));
|
|
|
|
SecureString iv(16);
|
|
|
|
as_mutable_slice(iv).copy_from(hash.substr(32, 16));
|
2018-08-12 21:25:04 +02:00
|
|
|
LOG(INFO) << "End AES CBC state calculation";
|
2018-03-26 16:07:04 +02:00
|
|
|
return AesCbcState{key, iv};
|
|
|
|
}
|
|
|
|
|
2018-08-04 08:55:49 +02:00
|
|
|
AesCbcState calc_aes_cbc_state_pbkdf2(Slice secret, Slice salt) {
|
2018-08-12 21:25:04 +02:00
|
|
|
LOG(INFO) << "Begin AES CBC state calculation";
|
2018-08-04 08:55:49 +02:00
|
|
|
UInt<512> hash;
|
|
|
|
auto hash_slice = as_slice(hash);
|
|
|
|
pbkdf2_sha512(secret, salt, 100000, hash_slice);
|
|
|
|
return calc_aes_cbc_state_hash(hash_slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
AesCbcState calc_aes_cbc_state_sha512(Slice seed) {
|
2018-08-12 21:25:04 +02:00
|
|
|
LOG(INFO) << "Begin AES CBC state calculation";
|
2018-08-04 08:55:49 +02:00
|
|
|
UInt<512> hash;
|
|
|
|
auto hash_slice = as_slice(hash);
|
|
|
|
sha512(seed, hash_slice);
|
|
|
|
return calc_aes_cbc_state_hash(hash_slice);
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
template <class F>
|
2018-09-13 02:12:40 +02:00
|
|
|
static Status data_view_for_each(const DataView &data, F &&f) {
|
2018-03-26 16:07:04 +02:00
|
|
|
const int64 step = 128 << 10;
|
|
|
|
for (int64 i = 0, size = data.size(); i < size; i += step) {
|
2018-03-27 15:11:15 +02:00
|
|
|
TRY_RESULT(bytes, data.pread(i, min(step, size - i)));
|
2018-03-26 16:07:04 +02:00
|
|
|
TRY_STATUS(f(std::move(bytes)));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
Result<ValueHash> calc_value_hash(const DataView &data_view) {
|
2018-03-26 16:07:04 +02:00
|
|
|
Sha256State state;
|
2019-07-23 02:20:11 +02:00
|
|
|
state.init();
|
2019-04-28 14:00:06 +02:00
|
|
|
TRY_STATUS(data_view_for_each(data_view, [&state](BufferSlice bytes) {
|
2019-07-23 02:20:11 +02:00
|
|
|
state.feed(bytes.as_slice());
|
2018-03-26 16:07:04 +02:00
|
|
|
return Status::OK();
|
2019-04-28 14:00:06 +02:00
|
|
|
}));
|
2018-03-26 16:07:04 +02:00
|
|
|
UInt256 res;
|
2019-07-23 02:20:11 +02:00
|
|
|
state.extract(as_slice(res));
|
2018-03-26 16:07:04 +02:00
|
|
|
return ValueHash{res};
|
|
|
|
}
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
ValueHash calc_value_hash(Slice data) {
|
|
|
|
UInt256 res;
|
|
|
|
sha256(data, as_slice(res));
|
|
|
|
return ValueHash{res};
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
BufferSlice gen_random_prefix(int64 data_size) {
|
2018-03-26 20:14:15 +02:00
|
|
|
BufferSlice buff(narrow_cast<size_t>(((32 + 15 + data_size) & -16) - data_size));
|
2018-03-26 16:07:04 +02:00
|
|
|
Random::secure_bytes(buff.as_slice());
|
|
|
|
buff.as_slice()[0] = narrow_cast<uint8>(buff.size());
|
|
|
|
CHECK((buff.size() + data_size) % 16 == 0);
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FileDataView final : public DataView {
|
2020-01-03 02:16:05 +01:00
|
|
|
public:
|
|
|
|
FileDataView(FileFd &fd, int64 size);
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
int64 size() const final;
|
|
|
|
Result<BufferSlice> pread(int64 offset, int64 size) const final;
|
2020-01-03 02:16:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
FileFd &fd_;
|
|
|
|
int64 size_;
|
|
|
|
};
|
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
FileDataView::FileDataView(FileFd &fd, int64 size) : fd_(fd), size_(size) {
|
|
|
|
}
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
int64 FileDataView::size() const {
|
2018-03-26 16:07:04 +02:00
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
Result<BufferSlice> FileDataView::pread(int64 offset, int64 size) const {
|
2018-03-26 20:14:15 +02:00
|
|
|
auto slice = BufferSlice(narrow_cast<size_t>(size));
|
2018-03-26 16:07:04 +02:00
|
|
|
TRY_RESULT(actual_size, fd_.pread(slice.as_slice(), offset));
|
|
|
|
if (static_cast<int64>(actual_size) != size) {
|
|
|
|
return Status::Error("Not enough data in file");
|
|
|
|
}
|
|
|
|
return std::move(slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferSliceDataView::BufferSliceDataView(BufferSlice buffer_slice) : buffer_slice_(std::move(buffer_slice)) {
|
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
int64 BufferSliceDataView::size() const {
|
2018-03-26 20:14:15 +02:00
|
|
|
return narrow_cast<int64>(buffer_slice_.size());
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
Result<BufferSlice> BufferSliceDataView::pread(int64 offset, int64 size) const {
|
2018-03-26 16:07:04 +02:00
|
|
|
auto end_offset = size + offset;
|
|
|
|
if (this->size() < end_offset) {
|
|
|
|
return Status::Error("Not enough data in BufferSlice");
|
|
|
|
}
|
2018-03-26 20:14:15 +02:00
|
|
|
return BufferSlice(buffer_slice_.as_slice().substr(narrow_cast<size_t>(offset), narrow_cast<size_t>(size)));
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
ConcatDataView::ConcatDataView(const DataView &left, const DataView &right) : left_(left), right_(right) {
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
int64 ConcatDataView::size() const {
|
2018-03-26 16:07:04 +02:00
|
|
|
return left_.size() + right_.size();
|
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
Result<BufferSlice> ConcatDataView::pread(int64 offset, int64 size) const {
|
2018-03-26 16:07:04 +02:00
|
|
|
auto end_offset = size + offset;
|
|
|
|
if (this->size() < end_offset) {
|
|
|
|
return Status::Error("Not enough data in ConcatDataView");
|
|
|
|
}
|
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
auto substr = [](const DataView &slice, int64 offset, int64 size) -> Result<BufferSlice> {
|
2018-03-27 15:11:15 +02:00
|
|
|
auto l = max(int64{0}, offset);
|
|
|
|
auto r = min(slice.size(), offset + size);
|
2018-03-26 16:07:04 +02:00
|
|
|
if (l >= r) {
|
|
|
|
return BufferSlice();
|
|
|
|
}
|
|
|
|
return slice.pread(l, r - l);
|
|
|
|
};
|
|
|
|
|
|
|
|
TRY_RESULT(a, substr(left_, offset, size));
|
|
|
|
TRY_RESULT(b, substr(right_, offset - left_.size(), size));
|
|
|
|
|
|
|
|
if (a.empty()) {
|
|
|
|
return std::move(b);
|
|
|
|
}
|
|
|
|
if (b.empty()) {
|
|
|
|
return std::move(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferSlice res(a.size() + b.size());
|
|
|
|
res.as_slice().copy_from(a.as_slice());
|
|
|
|
res.as_slice().substr(a.size()).copy_from(b.as_slice());
|
|
|
|
return std::move(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Password::Password(std::string password) : password_(std::move(password)) {
|
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
Slice Password::as_slice() const {
|
|
|
|
return password_;
|
|
|
|
}
|
|
|
|
|
2018-08-04 08:55:49 +02:00
|
|
|
static uint8 secret_checksum(Slice secret) {
|
2018-03-27 15:11:15 +02:00
|
|
|
uint32 sum = 0;
|
2018-03-26 16:07:04 +02:00
|
|
|
for (uint8 c : secret) {
|
|
|
|
sum += c;
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
return static_cast<uint8>((255 + 239 - sum % 255) % 255);
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<Secret> Secret::create(Slice secret) {
|
|
|
|
if (secret.size() != 32) {
|
2019-08-26 03:53:22 +02:00
|
|
|
return Status::Error("Wrong secret size");
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
uint32 checksum = secret_checksum(secret);
|
|
|
|
if (checksum != 0) {
|
2018-04-06 15:26:32 +02:00
|
|
|
return Status::Error(PSLICE() << "Wrong checksum " << checksum);
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
UInt256 res;
|
2019-09-15 05:19:46 +02:00
|
|
|
::td::as_slice(res).copy_from(secret);
|
2018-03-27 15:11:15 +02:00
|
|
|
|
|
|
|
UInt256 secret_sha256;
|
|
|
|
sha256(secret, ::td::as_slice(secret_sha256));
|
2018-12-19 23:46:55 +01:00
|
|
|
int64 hash = as<int64>(secret_sha256.raw);
|
2018-03-27 15:11:15 +02:00
|
|
|
return Secret{res, hash};
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Secret Secret::create_new() {
|
|
|
|
UInt256 secret;
|
2019-09-15 05:19:46 +02:00
|
|
|
auto secret_slice = ::td::as_slice(secret);
|
2018-03-26 16:07:04 +02:00
|
|
|
Random::secure_bytes(secret_slice);
|
|
|
|
auto checksum_diff = secret_checksum(secret_slice);
|
2021-10-19 17:11:16 +02:00
|
|
|
auto new_byte = static_cast<uint8>((static_cast<uint32>(secret_slice.ubegin()[0]) + checksum_diff) % 255);
|
2018-03-27 15:11:15 +02:00
|
|
|
secret_slice.ubegin()[0] = new_byte;
|
2018-03-26 16:07:04 +02:00
|
|
|
return create(secret_slice).move_as_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice Secret::as_slice() const {
|
2019-09-15 05:19:46 +02:00
|
|
|
return ::td::as_slice(secret_);
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
int64 Secret::get_hash() const {
|
|
|
|
return hash_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Secret Secret::clone() const {
|
|
|
|
return {secret_, hash_};
|
|
|
|
}
|
|
|
|
|
2018-08-04 08:55:49 +02:00
|
|
|
EncryptedSecret Secret::encrypt(Slice key, Slice salt, EnryptionAlgorithm algorithm) {
|
2020-02-26 16:15:19 +01:00
|
|
|
auto aes_cbc_state = [&] {
|
2018-08-04 08:55:49 +02:00
|
|
|
switch (algorithm) {
|
|
|
|
case EnryptionAlgorithm::Sha512:
|
|
|
|
return calc_aes_cbc_state_sha512(PSLICE() << salt << key << salt);
|
|
|
|
case EnryptionAlgorithm::Pbkdf2:
|
|
|
|
return calc_aes_cbc_state_pbkdf2(key, salt);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-08-12 15:53:17 +02:00
|
|
|
return AesCbcState(Slice(), Slice());
|
2018-08-04 08:55:49 +02:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
UInt256 res;
|
2019-09-15 05:19:46 +02:00
|
|
|
aes_cbc_state.encrypt(as_slice(), ::td::as_slice(res));
|
|
|
|
return EncryptedSecret::create(::td::as_slice(res)).move_as_ok();
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
Secret::Secret(UInt256 secret, int64 hash) : secret_(secret), hash_(hash) {
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<EncryptedSecret> EncryptedSecret::create(Slice encrypted_secret) {
|
|
|
|
if (encrypted_secret.size() != 32) {
|
2018-04-06 15:26:32 +02:00
|
|
|
return Status::Error("Wrong encrypted secret size");
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
UInt256 res;
|
2019-09-15 05:19:46 +02:00
|
|
|
::td::as_slice(res).copy_from(encrypted_secret);
|
2018-03-26 16:07:04 +02:00
|
|
|
return EncryptedSecret{res};
|
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
|
|
|
Result<Secret> EncryptedSecret::decrypt(Slice key, Slice salt, EnryptionAlgorithm algorithm) {
|
2020-02-26 16:15:19 +01:00
|
|
|
auto aes_cbc_state = [&] {
|
2018-08-04 08:55:49 +02:00
|
|
|
switch (algorithm) {
|
|
|
|
case EnryptionAlgorithm::Sha512:
|
|
|
|
return calc_aes_cbc_state_sha512(PSLICE() << salt << key << salt);
|
|
|
|
case EnryptionAlgorithm::Pbkdf2:
|
|
|
|
return calc_aes_cbc_state_pbkdf2(key, salt);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-08-12 15:53:17 +02:00
|
|
|
return AesCbcState(Slice(), Slice());
|
2018-08-04 08:55:49 +02:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
UInt256 res;
|
2019-09-15 05:19:46 +02:00
|
|
|
aes_cbc_state.decrypt(::td::as_slice(encrypted_secret_), ::td::as_slice(res));
|
|
|
|
return Secret::create(::td::as_slice(res));
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Slice EncryptedSecret::as_slice() const {
|
2019-09-15 05:19:46 +02:00
|
|
|
return ::td::as_slice(encrypted_secret_);
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EncryptedSecret::EncryptedSecret(UInt256 encrypted_secret) : encrypted_secret_(encrypted_secret) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Decryptor::Decryptor(AesCbcState aes_cbc_state) : aes_cbc_state_(std::move(aes_cbc_state)) {
|
2019-07-23 02:20:11 +02:00
|
|
|
sha256_state_.init();
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
Result<BufferSlice> Decryptor::append(BufferSlice data) {
|
|
|
|
if (data.empty()) {
|
|
|
|
return BufferSlice();
|
|
|
|
}
|
|
|
|
if (data.size() % 16 != 0) {
|
2020-03-02 10:52:13 +01:00
|
|
|
return Status::Error("Part size must be divisible by 16");
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
aes_cbc_state_.decrypt(data.as_slice(), data.as_slice());
|
2019-07-23 02:20:11 +02:00
|
|
|
sha256_state_.feed(data.as_slice());
|
2018-03-26 16:07:04 +02:00
|
|
|
if (!skipped_prefix_) {
|
|
|
|
to_skip_ = data.as_slice().ubegin()[0];
|
2018-03-27 15:11:15 +02:00
|
|
|
size_t to_skip = min(to_skip_, data.size());
|
2018-04-06 15:26:32 +02:00
|
|
|
if (to_skip_ > data.size()) {
|
|
|
|
to_skip_ = 0; // to fail final to_skip check
|
|
|
|
}
|
2018-03-26 16:07:04 +02:00
|
|
|
skipped_prefix_ = true;
|
|
|
|
data = data.from_slice(data.as_slice().remove_prefix(to_skip));
|
|
|
|
}
|
|
|
|
return std::move(data);
|
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
Result<ValueHash> Decryptor::finish() {
|
|
|
|
if (!skipped_prefix_) {
|
|
|
|
return Status::Error("No data was given");
|
|
|
|
}
|
|
|
|
if (to_skip_ < 32) {
|
|
|
|
return Status::Error("Too small random prefix");
|
|
|
|
}
|
2019-07-23 02:20:11 +02:00
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
UInt256 res;
|
2019-07-23 02:50:03 +02:00
|
|
|
sha256_state_.extract(as_slice(res), true);
|
2018-03-26 16:07:04 +02:00
|
|
|
return ValueHash{res};
|
|
|
|
}
|
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
Encryptor::Encryptor(AesCbcState aes_cbc_state, const DataView &data_view)
|
2018-03-26 16:07:04 +02:00
|
|
|
: aes_cbc_state_(std::move(aes_cbc_state)), data_view_(data_view) {
|
|
|
|
}
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
int64 Encryptor::size() const {
|
2018-03-26 16:07:04 +02:00
|
|
|
return data_view_.size();
|
|
|
|
}
|
|
|
|
|
2018-09-13 02:12:40 +02:00
|
|
|
Result<BufferSlice> Encryptor::pread(int64 offset, int64 size) const {
|
2018-03-26 16:07:04 +02:00
|
|
|
if (offset != current_offset_) {
|
|
|
|
return Status::Error("Arbitrary offset is not supported");
|
|
|
|
}
|
|
|
|
if (size % 16 != 0) {
|
2020-03-02 10:52:13 +01:00
|
|
|
return Status::Error("Part size must be divisible by 16");
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
TRY_RESULT(part, data_view_.pread(offset, size));
|
|
|
|
aes_cbc_state_.encrypt(part.as_slice(), part.as_slice());
|
|
|
|
current_offset_ += size;
|
|
|
|
return std::move(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<EncryptedValue> encrypt_value(const Secret &secret, Slice data) {
|
2018-08-28 00:08:51 +02:00
|
|
|
BufferSliceDataView random_prefix_view{gen_random_prefix(data.size())};
|
|
|
|
BufferSliceDataView data_view{BufferSlice(data)};
|
|
|
|
ConcatDataView full_view{random_prefix_view, data_view};
|
2018-03-26 16:07:04 +02:00
|
|
|
|
|
|
|
TRY_RESULT(hash, calc_value_hash(full_view));
|
|
|
|
|
2018-08-04 08:55:49 +02:00
|
|
|
auto aes_cbc_state = calc_aes_cbc_state_sha512(PSLICE() << secret.as_slice() << hash.as_slice());
|
2019-08-12 15:53:17 +02:00
|
|
|
Encryptor encryptor(std::move(aes_cbc_state), full_view);
|
2018-03-26 16:07:04 +02:00
|
|
|
TRY_RESULT(encrypted_data, encryptor.pread(0, encryptor.size()));
|
|
|
|
return EncryptedValue{std::move(encrypted_data), std::move(hash)};
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<BufferSlice> decrypt_value(const Secret &secret, const ValueHash &hash, Slice data) {
|
2018-08-04 08:55:49 +02:00
|
|
|
auto aes_cbc_state = calc_aes_cbc_state_sha512(PSLICE() << secret.as_slice() << hash.as_slice());
|
2019-08-12 15:53:17 +02:00
|
|
|
Decryptor decryptor(std::move(aes_cbc_state));
|
2018-03-26 16:07:04 +02:00
|
|
|
TRY_RESULT(decrypted_value, decryptor.append(BufferSlice(data)));
|
|
|
|
TRY_RESULT(got_hash, decryptor.finish());
|
|
|
|
if (got_hash.as_slice() != hash.as_slice()) {
|
2018-03-27 15:11:15 +02:00
|
|
|
return Status::Error(PSLICE() << "Hash mismatch " << format::as_hex_dump<4>(got_hash.as_slice()) << " "
|
|
|
|
<< format::as_hex_dump<4>(hash.as_slice()));
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
return std::move(decrypted_value);
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
Result<ValueHash> encrypt_file(const Secret &secret, const string &src, const string &dest) {
|
2018-03-26 16:07:04 +02:00
|
|
|
TRY_RESULT(src_file, FileFd::open(src, FileFd::Flags::Read));
|
|
|
|
TRY_RESULT(dest_file, FileFd::open(dest, FileFd::Flags::Truncate | FileFd::Flags::Write | FileFd::Create));
|
2019-06-19 16:31:25 +02:00
|
|
|
TRY_RESULT(src_file_size, src_file.get_size());
|
2018-03-26 16:07:04 +02:00
|
|
|
|
2018-08-28 00:08:51 +02:00
|
|
|
BufferSliceDataView random_prefix_view(gen_random_prefix(src_file_size));
|
|
|
|
FileDataView data_view(src_file, src_file_size);
|
|
|
|
ConcatDataView full_view(random_prefix_view, data_view);
|
2018-03-26 16:07:04 +02:00
|
|
|
|
|
|
|
TRY_RESULT(hash, calc_value_hash(full_view));
|
|
|
|
|
2018-08-04 08:55:49 +02:00
|
|
|
auto aes_cbc_state = calc_aes_cbc_state_sha512(PSLICE() << secret.as_slice() << hash.as_slice());
|
2019-08-12 15:53:17 +02:00
|
|
|
Encryptor encryptor(std::move(aes_cbc_state), full_view);
|
2018-03-26 16:07:04 +02:00
|
|
|
TRY_STATUS(
|
|
|
|
data_view_for_each(encryptor, [&dest_file](BufferSlice bytes) { return dest_file.write(bytes.as_slice()); }));
|
|
|
|
return std::move(hash);
|
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
Status decrypt_file(const Secret &secret, const ValueHash &hash, const string &src, const string &dest) {
|
2018-03-26 16:07:04 +02:00
|
|
|
TRY_RESULT(src_file, FileFd::open(src, FileFd::Flags::Read));
|
|
|
|
TRY_RESULT(dest_file, FileFd::open(dest, FileFd::Flags::Truncate | FileFd::Flags::Write | FileFd::Create));
|
2019-06-19 16:31:25 +02:00
|
|
|
TRY_RESULT(src_file_size, src_file.get_size());
|
2018-03-26 16:07:04 +02:00
|
|
|
|
2018-08-28 00:08:51 +02:00
|
|
|
FileDataView src_file_view(src_file, src_file_size);
|
2018-03-26 16:07:04 +02:00
|
|
|
|
2018-08-04 08:55:49 +02:00
|
|
|
auto aes_cbc_state = calc_aes_cbc_state_sha512(PSLICE() << secret.as_slice() << hash.as_slice());
|
2019-08-12 15:53:17 +02:00
|
|
|
Decryptor decryptor(std::move(aes_cbc_state));
|
2018-03-26 16:07:04 +02:00
|
|
|
TRY_STATUS(data_view_for_each(src_file_view, [&decryptor, &dest_file](BufferSlice bytes) {
|
|
|
|
TRY_RESULT(decrypted_bytes, decryptor.append(std::move(bytes)));
|
|
|
|
TRY_STATUS(dest_file.write(decrypted_bytes.as_slice()));
|
|
|
|
return Status::OK();
|
|
|
|
}));
|
|
|
|
|
|
|
|
TRY_RESULT(got_hash, decryptor.finish());
|
|
|
|
|
|
|
|
if (hash.as_slice() != got_hash.as_slice()) {
|
|
|
|
return Status::Error("Hash mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-08-04 08:55:49 +02:00
|
|
|
|
2018-03-26 16:07:04 +02:00
|
|
|
} // namespace secure_storage
|
|
|
|
} // namespace td
|