-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathEncryptionParameters.h
More file actions
107 lines (80 loc) · 3.15 KB
/
EncryptionParameters.h
File metadata and controls
107 lines (80 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#pragma once
#include "AESParameters.h"
#include "Data.h"
#include "PBKDF2Parameters.h"
#include "ScryptParameters.h"
#include <TrustWalletCore/TWStoredKeyEncryption.h>
#include <TrustWalletCore/TWStoredKeyEncryptionLevel.h>
#include <nlohmann/json.hpp>
#include <string>
#include <variant>
namespace TW::Keystore {
/// Set of parameters used when encoding
struct EncryptionParameters {
std::int32_t getKeyBytesSize() const noexcept {
return cipherParams.mKeyLength;
}
std::string cipher() const noexcept {
return cipherParams.mCipher;
}
/// Cipher parameters.
AESParameters cipherParams = AESParameters();
/// Key derivation function parameters.
std::variant<ScryptParameters, PBKDF2Parameters> kdfParams = ScryptParameters();
EncryptionParameters() = default;
/// Initializes with standard values.
EncryptionParameters(AESParameters cipherParams, std::variant<ScryptParameters, PBKDF2Parameters> kdfParams)
: cipherParams(std::move(cipherParams)), kdfParams(std::move(kdfParams)) {
}
/// Initializes with a JSON object.
explicit EncryptionParameters(const nlohmann::json& json);
/// Saves `this` as a JSON object.
nlohmann::json json() const;
EncryptionParameters(const EncryptionParameters& other) = default;
EncryptionParameters(EncryptionParameters&& other) = default;
EncryptionParameters& operator=(const EncryptionParameters& other) = default;
EncryptionParameters& operator=(EncryptionParameters&& other) = default;
virtual ~EncryptionParameters() = default;
};
/// Errors thrown when decrypting a key.
enum class DecryptionError {
unsupportedKDF,
unsupportedCipher,
unsupportedCoin,
invalidKeyFile,
invalidCipher,
invalidPassword,
};
/// An encrypted payload data
struct EncryptedPayload {
public:
EncryptionParameters params;
/// Encrypted data.
Data encrypted;
/// Message authentication code.
Data _mac;
EncryptedPayload() = default;
/// Initializes with standard values.
EncryptedPayload(EncryptionParameters params, Data encrypted, Data mac)
: params(std::move(params))
, encrypted(std::move(encrypted))
, _mac(std::move(mac)) {}
/// Initializes by encrypting data with a password using standard values.
/// Note that we enforce to use Scrypt as KDF for new wallets encryption.
EncryptedPayload(const Data& password, const Data& data, const AESParameters& cipherParams, const ScryptParameters& scryptParams);
/// Initializes with a JSON object.
explicit EncryptedPayload(const nlohmann::json& json);
/// Decrypts the payload with the given password.
Data decrypt(const Data& password) const;
/// Saves `this` as a JSON object.
nlohmann::json json() const;
EncryptedPayload(const EncryptedPayload& other) = default;
EncryptedPayload(EncryptedPayload&& other) = default;
EncryptedPayload& operator=(const EncryptedPayload& other) = default;
EncryptedPayload& operator=(EncryptedPayload&& other) = default;
virtual ~EncryptedPayload();
};
} // namespace TW::Keystore