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 "../Data.h"
10
#include "../HexCoding.h"
11

12
#include <nlohmann/json.hpp>
13
#include <optional>
14

15
namespace TW::Keystore {
16

17
enum class ScryptValidationError {
18
    desiredKeyLengthTooLarge,
19
    blockSizeTooLarge,
20
    invalidCostFactor,
21
    overflow,
22
};
23

24
/// Scrypt function parameters.
25 1
struct ScryptParameters {
26
    /// The N parameter of Scrypt encryption algorithm, using 256MB memory and
27
    /// taking approximately 1s CPU time on a modern processor.
28
    static const uint32_t standardN = 1 << 18;
29

30
    /// The P parameter of Scrypt encryption algorithm, using 256MB memory and
31
    /// taking approximately 1s CPU time on a modern processor.
32
    static const uint32_t standardP = 1;
33

34
    /// The N parameter of Scrypt encryption algorithm, using 4MB memory and
35
    /// taking approximately 100ms CPU time on a modern processor.
36
    static const uint32_t lightN = 1 << 12;
37

38
    /// The P parameter of Scrypt encryption algorithm, using 4MB memory and
39
    /// taking approximately 100ms CPU time on a modern processor.
40
    static const uint32_t lightP = 6;
41

42
    /// Default `R` parameter of Scrypt encryption algorithm.
43
    static const uint32_t defaultR = 8;
44

45
    /// Default desired key length of Scrypt encryption algorithm.
46
    static const std::size_t defaultDesiredKeyLength = 32;
47

48
    /// Random salt.
49
    Data salt;
50

51
    /// Desired key length in bytes.
52
    std::size_t desiredKeyLength = defaultDesiredKeyLength;
53

54
    /// CPU/Memory cost factor.
55
    uint32_t n = lightN;
56

57
    /// Parallelization factor (1..232-1 * hLen/MFlen).
58
    uint32_t p = lightP;
59

60
    /// Block size factor.
61
    uint32_t r = defaultR;
62

63
    /// Initializes with default scrypt parameters and a random salt.
64
    ScryptParameters();
65

66
    /// Initializes `ScryptParameters` with all values.
67
    ///
68
    /// @throws ScryptValidationError if the parameters are invalid.
69
    ScryptParameters(const Data& salt, uint32_t n, uint32_t r, uint32_t p, std::size_t desiredKeyLength)
70
        : salt(std::move(salt)), desiredKeyLength(desiredKeyLength), n(n), p(p), r(r) {
71
        auto error = validate();
72
        if (error) {
73
            throw *error;
74
        }
75
    }
76

77
    /// Validates the parameters.
78
    ///
79
    /// - Returns: a `ValidationError` or `nil` if the parameters are valid.
80
    std::optional<ScryptValidationError> validate() const;
81

82
    /// Initializes `ScryptParameters` with a JSON object.
83
    ScryptParameters(const nlohmann::json& json);
84

85
    /// Saves `this` as a JSON object.
86
    nlohmann::json json() const;
87
};
88

89
} // namespace TW::Keystore

Read our documentation on viewing source code .

Loading