1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include "AESParameters.h"
10
#include "PBKDF2Parameters.h"
11
#include "ScryptParameters.h"
12
#include "../Data.h"
13

14
#include <boost/variant.hpp>
15
#include <nlohmann/json.hpp>
16
#include <string>
17

18
namespace TW::Keystore {
19

20
/// Errors thrown when decrypting a key.
21
enum class DecryptionError {
22
    unsupportedKDF,
23
    unsupportedCipher,
24
    unsupportedCoin,
25
    invalidKeyFile,
26
    invalidCipher,
27
    invalidPassword,
28
};
29

30
struct EncryptionParameters {
31
    /// Encrypted data.
32
    Data encrypted;
33

34
    /// Cipher algorithm.
35 1
    std::string cipher = "aes-128-ctr";
36

37
    /// Cipher parameters.
38 1
    AESParameters cipherParams = AESParameters();
39

40
    /// Key derivation function parameters.
41 1
    boost::variant<ScryptParameters, PBKDF2Parameters> kdfParams = ScryptParameters();
42

43
    /// Message authentication code.
44
    Data mac;
45

46 1
    EncryptionParameters() = default;
47

48
    /// Initializes `EncryptionParameters` with standard values.
49
    EncryptionParameters(const Data& encrypted, AESParameters cipherParams, boost::variant<ScryptParameters, PBKDF2Parameters> kdfParams, const Data& mac)
50
        : encrypted(std::move(encrypted))
51
        , cipherParams(std::move(cipherParams))
52
        , kdfParams(std::move(kdfParams))
53
        , mac(std::move(mac)) {}
54

55
    /// Initializes `EncryptionParameters` by encrypting data with a password
56
    /// using standard values.
57
    EncryptionParameters(const Data& password, const Data& data);
58

59
    /// Initializes `EncryptionParameters` with a JSON object.
60
    EncryptionParameters(const nlohmann::json& json);
61

62
    /// Decrypts the payload with the given password.
63
    Data decrypt(const Data& password) const;
64

65
    /// Saves `this` as a JSON object.
66
    nlohmann::json json() const;
67

68 1
    EncryptionParameters(const EncryptionParameters& other) = default;
69
    EncryptionParameters(EncryptionParameters&& other) = default;
70
    EncryptionParameters& operator=(const EncryptionParameters& other) = default;
71 1
    EncryptionParameters& operator=(EncryptionParameters&& other) = default;
72

73
    virtual ~EncryptionParameters();
74
};
75

76
} // namespace TW::Keystore

Read our documentation on viewing source code .

Loading