2018-12-27 19:06:21 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
#include "td/utils/UInt.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
namespace secure_storage {
|
|
|
|
class Secret;
|
|
|
|
class ValueHash;
|
2018-12-27 20:24:44 +01:00
|
|
|
} // namespace secure_storage
|
2018-12-27 19:38:43 +01:00
|
|
|
|
2018-12-27 19:06:21 +01:00
|
|
|
struct FileEncryptionKey {
|
|
|
|
enum class Type : int32 { None, Secret, Secure };
|
2018-12-27 19:38:43 +01:00
|
|
|
|
2018-12-27 19:06:21 +01:00
|
|
|
FileEncryptionKey() = default;
|
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
FileEncryptionKey(Slice key, Slice iv);
|
|
|
|
|
|
|
|
explicit FileEncryptionKey(const secure_storage::Secret &secret);
|
2018-12-27 19:06:21 +01:00
|
|
|
|
|
|
|
bool is_secret() const {
|
|
|
|
return type_ == Type::Secret;
|
|
|
|
}
|
2018-12-27 19:38:43 +01:00
|
|
|
|
2018-12-27 19:06:21 +01:00
|
|
|
bool is_secure() const {
|
|
|
|
return type_ == Type::Secure;
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
static FileEncryptionKey create();
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
static FileEncryptionKey create_secure_key();
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
const UInt256 &key() const;
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
Slice key_slice() const;
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
secure_storage::Secret secret() const;
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
bool has_value_hash() const;
|
2018-12-27 19:06:21 +01:00
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
void set_value_hash(const secure_storage::ValueHash &value_hash);
|
|
|
|
|
|
|
|
secure_storage::ValueHash value_hash() const;
|
|
|
|
|
|
|
|
UInt256 &mutable_iv();
|
|
|
|
|
|
|
|
Slice iv_slice() const;
|
|
|
|
|
|
|
|
int32 calc_fingerprint() const;
|
2018-12-27 19:06:21 +01:00
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return key_iv_.empty();
|
|
|
|
}
|
2018-12-27 19:38:43 +01:00
|
|
|
|
2018-12-27 19:06:21 +01:00
|
|
|
size_t size() const {
|
|
|
|
return key_iv_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
td::store(key_iv_, storer);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(Type type, ParserT &parser) {
|
|
|
|
td::parse(key_iv_, parser);
|
|
|
|
if (key_iv_.empty()) {
|
|
|
|
type_ = Type::None;
|
|
|
|
} else {
|
|
|
|
if (type_ == Type::Secure) {
|
|
|
|
if (key_iv_.size() != 64) {
|
|
|
|
LOG(ERROR) << "Have wrong key size " << key_iv_.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
type_ = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string key_iv_; // TODO wrong alignment is possible
|
|
|
|
Type type_ = Type::None;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const FileEncryptionKey &lhs, const FileEncryptionKey &rhs) {
|
|
|
|
return lhs.key_iv_ == rhs.key_iv_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator!=(const FileEncryptionKey &lhs, const FileEncryptionKey &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:38:43 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const FileEncryptionKey &key);
|
2018-12-27 19:06:21 +01:00
|
|
|
|
|
|
|
} // namespace td
|