-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathScryptParameters.h
More file actions
103 lines (78 loc) · 3.36 KB
/
ScryptParameters.h
File metadata and controls
103 lines (78 loc) · 3.36 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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#pragma once
#include "Data.h"
#include "TrustWalletCore/TWStoredKeyEncryptionLevel.h"
#include "../HexCoding.h"
#include <nlohmann/json.hpp>
#include <optional>
namespace TW::Keystore {
enum class ScryptValidationError {
desiredKeyLengthTooLarge,
invalidSaltLength,
blockSizeTooLarge,
invalidCostFactor,
overflow,
};
std::string toString(ScryptValidationError error);
/// Scrypt function parameters.
struct ScryptParameters {
/// The N and P parameters of Scrypt encryption algorithm, using 256MB memory and
/// taking approximately 1s CPU time on a modern processor.
static const uint32_t standardN = 1 << 18;
static const uint32_t standardP = 1;
static const uint32_t weakN = 1 << 14;
static const uint32_t weakP = 4;
/// The N and P parameters of Scrypt encryption algorithm, using 4MB memory and
/// taking approximately 100ms CPU time on a modern processor.
static const uint32_t minimalN = 1 << 12;
static const uint32_t minimalP = 6;
/// Default `R` parameter of Scrypt encryption algorithm.
static const uint32_t defaultR = 8;
/// Default desired key length of Scrypt encryption algorithm.
static const std::size_t defaultDesiredKeyLength = 32;
/// Minimum and maximum salt length for Scrypt encryption algorithm.
/// https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf
static const std::size_t minSaltLength = 16;
static const std::size_t maxSaltLength = 1024;
/// Random salt.
Data salt;
/// Desired key length in bytes.
std::size_t desiredKeyLength = defaultDesiredKeyLength;
/// CPU/Memory cost factor.
uint32_t n = minimalN;
/// Parallelization factor (1..232-1 * hLen/MFlen).
uint32_t p = minimalP;
/// Block size factor.
uint32_t r = defaultR;
/// Returns a preset of Scrypt encryption parameters for the given encryption level.
static ScryptParameters getPreset(TWStoredKeyEncryptionLevel preset);
/// Generates Scrypt encryption parameters with the minimal sufficient level (4096), and with a random salt.
static ScryptParameters minimal();
/// Generates Scrypt encryption parameters with the weak sufficient level (16k), and with a random salt.
static ScryptParameters weak();
/// Generates Scrypt encryption parameters with the standard sufficient level (262k), and with a random salt.
static ScryptParameters standard();
/// Initializes with default scrypt parameters and a random salt.
ScryptParameters();
/// Initializes `ScryptParameters` with all values.
///
/// @throws ScryptValidationError if the parameters are invalid.
ScryptParameters(Data salt, uint32_t n, uint32_t r, uint32_t p, std::size_t desiredKeyLength)
: salt(std::move(salt)), desiredKeyLength(desiredKeyLength), n(n), p(p), r(r) {
auto error = validate();
if (error) {
throw *error;
}
}
/// Validates the parameters.
///
/// - Returns: a `ValidationError` or `nil` if the parameters are valid.
std::optional<ScryptValidationError> validate() const;
/// Initializes `ScryptParameters` with a JSON object.
explicit ScryptParameters(const nlohmann::json& json);
/// Saves `this` as a JSON object.
nlohmann::json json() const;
};
} // namespace TW::Keystore