2018-03-26 16:07:04 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
2020-08-13 15:57:28 +02:00
|
|
|
#include "td/telegram/SecureStorage.h"
|
|
|
|
|
2018-03-26 20:16:38 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2018-03-26 16:07:04 +02:00
|
|
|
#include "td/utils/filesystem.h"
|
|
|
|
#include "td/utils/port/path.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-03-26 20:16:38 +02:00
|
|
|
#include "td/utils/tests.h"
|
2018-03-26 16:07:04 +02:00
|
|
|
|
|
|
|
TEST(SecureStorage, secret) {
|
2022-01-12 19:23:08 +01:00
|
|
|
auto secret = td::secure_storage::Secret::create_new();
|
|
|
|
td::string key = "cucumber";
|
|
|
|
auto encrypted_secret = secret.encrypt(key, "", td::secure_storage::EnryptionAlgorithm::Sha512);
|
2018-03-26 16:07:04 +02:00
|
|
|
ASSERT_TRUE(encrypted_secret.as_slice() != secret.as_slice());
|
2022-01-12 19:23:08 +01:00
|
|
|
auto decrypted_secret = encrypted_secret.decrypt(key, "", td::secure_storage::EnryptionAlgorithm::Sha512).ok();
|
2018-03-26 16:07:04 +02:00
|
|
|
ASSERT_TRUE(secret.as_slice() == decrypted_secret.as_slice());
|
2022-01-12 19:23:08 +01:00
|
|
|
ASSERT_TRUE(encrypted_secret.decrypt("notcucumber", "", td::secure_storage::EnryptionAlgorithm::Sha512).is_error());
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SecureStorage, simple) {
|
2022-01-12 19:23:08 +01:00
|
|
|
td::BufferSlice value("Small tale about cucumbers");
|
|
|
|
auto value_secret = td::secure_storage::Secret::create_new();
|
2018-03-26 16:07:04 +02:00
|
|
|
|
2018-03-26 20:14:15 +02:00
|
|
|
{
|
2022-01-12 19:23:08 +01:00
|
|
|
td::secure_storage::BufferSliceDataView value_view(value.copy());
|
|
|
|
td::BufferSlice prefix = td::secure_storage::gen_random_prefix(value_view.size());
|
|
|
|
td::secure_storage::BufferSliceDataView prefix_view(std::move(prefix));
|
|
|
|
td::secure_storage::ConcatDataView full_value_view(prefix_view, value_view);
|
|
|
|
auto hash = td::secure_storage::calc_value_hash(full_value_view).move_as_ok();
|
2018-03-26 16:07:04 +02:00
|
|
|
|
2022-01-12 19:23:08 +01:00
|
|
|
td::secure_storage::Encryptor encryptor(
|
|
|
|
td::secure_storage::calc_aes_cbc_state_sha512(PSLICE() << value_secret.as_slice() << hash.as_slice()),
|
|
|
|
full_value_view);
|
2018-03-26 20:14:15 +02:00
|
|
|
auto encrypted_value = encryptor.pread(0, encryptor.size()).move_as_ok();
|
2018-03-26 16:07:04 +02:00
|
|
|
|
2022-01-12 19:23:08 +01:00
|
|
|
td::secure_storage::Decryptor decryptor(
|
|
|
|
td::secure_storage::calc_aes_cbc_state_sha512(PSLICE() << value_secret.as_slice() << hash.as_slice()));
|
2018-03-26 20:14:15 +02:00
|
|
|
auto res = decryptor.append(encrypted_value.copy()).move_as_ok();
|
|
|
|
auto decrypted_hash = decryptor.finish().ok();
|
|
|
|
ASSERT_TRUE(decrypted_hash.as_slice() == hash.as_slice());
|
|
|
|
ASSERT_TRUE(res.as_slice() == value.as_slice());
|
|
|
|
}
|
2018-03-26 16:07:04 +02:00
|
|
|
|
|
|
|
{
|
2022-01-12 19:23:08 +01:00
|
|
|
auto encrypted_value = td::secure_storage::encrypt_value(value_secret, value.as_slice()).move_as_ok();
|
2018-03-26 16:07:04 +02:00
|
|
|
auto decrypted_value =
|
2022-01-12 19:23:08 +01:00
|
|
|
td::secure_storage::decrypt_value(value_secret, encrypted_value.hash, encrypted_value.data.as_slice())
|
|
|
|
.move_as_ok();
|
2018-03-26 16:07:04 +02:00
|
|
|
ASSERT_TRUE(decrypted_value.as_slice() == value.as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-01-12 19:23:08 +01:00
|
|
|
td::string value_path = "value.txt";
|
|
|
|
td::string encrypted_path = "encrypted.txt";
|
|
|
|
td::string decrypted_path = "decrypted.txt";
|
2018-03-26 16:07:04 +02:00
|
|
|
td::unlink(value_path).ignore();
|
|
|
|
td::unlink(encrypted_path).ignore();
|
|
|
|
td::unlink(decrypted_path).ignore();
|
2022-01-12 19:23:08 +01:00
|
|
|
td::string file_value(100000, 'a');
|
2018-10-26 16:11:20 +02:00
|
|
|
td::write_file(value_path, file_value).ensure();
|
2022-01-12 19:23:08 +01:00
|
|
|
auto hash = td::secure_storage::encrypt_file(value_secret, value_path, encrypted_path).move_as_ok();
|
|
|
|
td::secure_storage::decrypt_file(value_secret, hash, encrypted_path, decrypted_path).ensure();
|
2018-03-26 22:20:56 +02:00
|
|
|
ASSERT_TRUE(td::read_file(decrypted_path).move_as_ok().as_slice() == file_value);
|
2018-03-26 16:07:04 +02:00
|
|
|
}
|
|
|
|
}
|