forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_argon2.cc
More file actions
172 lines (144 loc) Β· 5.37 KB
/
crypto_argon2.cc
File metadata and controls
172 lines (144 loc) Β· 5.37 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "crypto/crypto_argon2.h"
#include "async_wrap-inl.h"
#include "threadpoolwork-inl.h"
#if OPENSSL_VERSION_NUMBER >= 0x30200000L
#ifndef OPENSSL_NO_ARGON2
#include <openssl/core_names.h>
namespace node::crypto {
using v8::FunctionCallbackInfo;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
using v8::MaybeLocal;
using v8::Nothing;
using v8::Object;
using v8::Uint32;
using v8::Value;
Argon2Config::Argon2Config(Argon2Config&& other) noexcept
: mode{other.mode},
pass{std::move(other.pass)},
salt{std::move(other.salt)},
secret{std::move(other.secret)},
ad{std::move(other.ad)},
type{other.type},
iter{other.iter},
lanes{other.lanes},
memcost{other.memcost},
keylen{other.keylen} {}
Argon2Config& Argon2Config::operator=(Argon2Config&& other) noexcept {
if (&other == this) return *this;
this->~Argon2Config();
return *new (this) Argon2Config(std::move(other));
}
void Argon2Config::MemoryInfo(MemoryTracker* tracker) const {
if (mode == kCryptoJobAsync) {
tracker->TrackFieldWithSize("pass", pass.size());
tracker->TrackFieldWithSize("salt", salt.size());
tracker->TrackFieldWithSize("secret", secret.size());
tracker->TrackFieldWithSize("ad", ad.size());
}
}
MaybeLocal<Value> Argon2Traits::EncodeOutput(Environment* env,
const Argon2Config& config,
ByteSource* out) {
return out->ToArrayBuffer(env);
}
Maybe<void> Argon2Traits::AdditionalConfig(
CryptoJobMode mode,
const FunctionCallbackInfo<Value>& args,
unsigned int offset,
Argon2Config* config) {
Environment* env = Environment::GetCurrent(args);
config->mode = mode;
ArrayBufferOrViewContents<char> pass(args[offset]);
ArrayBufferOrViewContents<char> salt(args[offset + 1]);
ArrayBufferOrViewContents<char> secret(args[offset + 6]);
ArrayBufferOrViewContents<char> ad(args[offset + 7]);
if (!pass.CheckSizeInt32()) [[unlikely]] {
THROW_ERR_OUT_OF_RANGE(env, "pass is too large");
return Nothing<void>();
}
if (!salt.CheckSizeInt32()) [[unlikely]] {
THROW_ERR_OUT_OF_RANGE(env, "salt is too large");
return Nothing<void>();
}
if (!secret.CheckSizeInt32()) [[unlikely]] {
THROW_ERR_OUT_OF_RANGE(env, "secret is too large");
return Nothing<void>();
}
if (!ad.CheckSizeInt32()) [[unlikely]] {
THROW_ERR_OUT_OF_RANGE(env, "ad is too large");
return Nothing<void>();
}
const bool isAsync = mode == kCryptoJobAsync;
config->pass = isAsync ? pass.ToCopy() : pass.ToByteSource();
config->salt = isAsync ? salt.ToCopy() : salt.ToByteSource();
config->secret = isAsync ? secret.ToCopy() : secret.ToByteSource();
config->ad = isAsync ? ad.ToCopy() : ad.ToByteSource();
CHECK(args[offset + 2]->IsUint32()); // lanes
CHECK(args[offset + 3]->IsUint32()); // keylen
CHECK(args[offset + 4]->IsUint32()); // memcost
CHECK(args[offset + 5]->IsUint32()); // iter
CHECK(args[offset + 8]->IsUint32()); // type
config->lanes = args[offset + 2].As<Uint32>()->Value();
config->keylen = args[offset + 3].As<Uint32>()->Value();
config->memcost = args[offset + 4].As<Uint32>()->Value();
config->iter = args[offset + 5].As<Uint32>()->Value();
config->type =
static_cast<ncrypto::Argon2Type>(args[offset + 8].As<Uint32>()->Value());
if (!ncrypto::argon2(config->pass,
config->salt,
config->lanes,
config->keylen,
config->memcost,
config->iter,
config->version,
config->secret,
config->ad,
config->type)) {
THROW_ERR_CRYPTO_INVALID_ARGON2_PARAMS(env);
return Nothing<void>();
}
return JustVoid();
}
bool Argon2Traits::DeriveBits(Environment* env,
const Argon2Config& config,
ByteSource* out,
CryptoJobMode mode) {
// If the config.length is zero-length, just return an empty buffer.
// It's useless, yes, but allowed via the API.
if (config.keylen == 0) {
*out = ByteSource();
return true;
}
// Both the pass and salt may be zero-length at this point
auto dp = ncrypto::argon2(config.pass,
config.salt,
config.lanes,
config.keylen,
config.memcost,
config.iter,
config.version,
config.secret,
config.ad,
config.type);
if (!dp) return false;
DCHECK(!dp.isSecure());
*out = ByteSource::Allocated(dp.release());
return true;
}
static constexpr auto kTypeArgon2d = ncrypto::Argon2Type::ARGON2D;
static constexpr auto kTypeArgon2i = ncrypto::Argon2Type::ARGON2I;
static constexpr auto kTypeArgon2id = ncrypto::Argon2Type::ARGON2ID;
void Argon2::Initialize(Environment* env, Local<Object> target) {
Argon2Job::Initialize(env, target);
NODE_DEFINE_CONSTANT(target, kTypeArgon2d);
NODE_DEFINE_CONSTANT(target, kTypeArgon2i);
NODE_DEFINE_CONSTANT(target, kTypeArgon2id);
}
void Argon2::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
Argon2Job::RegisterExternalReferences(registry);
}
} // namespace node::crypto
#endif
#endif