1 |
|
|
#include "crypto/crypto_hkdf.h" |
2 |
|
|
#include "async_wrap-inl.h" |
3 |
|
|
#include "base_object-inl.h" |
4 |
|
|
#include "crypto/crypto_keys.h" |
5 |
|
|
#include "env-inl.h" |
6 |
|
|
#include "memory_tracker-inl.h" |
7 |
|
|
#include "threadpoolwork-inl.h" |
8 |
|
|
#include "v8.h" |
9 |
|
|
|
10 |
|
|
namespace node { |
11 |
|
|
|
12 |
|
|
using v8::FunctionCallbackInfo; |
13 |
|
|
using v8::Just; |
14 |
|
|
using v8::Maybe; |
15 |
|
|
using v8::Nothing; |
16 |
|
|
using v8::Uint32; |
17 |
|
|
using v8::Value; |
18 |
|
|
|
19 |
|
|
namespace crypto { |
20 |
|
444 |
HKDFConfig::HKDFConfig(HKDFConfig&& other) noexcept |
21 |
|
444 |
: mode(other.mode), |
22 |
|
444 |
length(other.length), |
23 |
|
444 |
digest(other.digest), |
24 |
|
444 |
key(other.key), |
25 |
|
444 |
salt(std::move(other.salt)), |
26 |
|
444 |
info(std::move(other.info)) {} |
27 |
|
|
|
28 |
|
|
HKDFConfig& HKDFConfig::operator=(HKDFConfig&& other) noexcept { |
29 |
|
|
if (&other == this) return *this; |
30 |
|
|
this->~HKDFConfig(); |
31 |
|
|
return *new (this) HKDFConfig(std::move(other)); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
444 |
Maybe<bool> HKDFTraits::EncodeOutput( |
35 |
|
|
Environment* env, |
36 |
|
|
const HKDFConfig& params, |
37 |
|
|
ByteSource* out, |
38 |
|
|
v8::Local<v8::Value>* result) { |
39 |
|
888 |
*result = out->ToArrayBuffer(env); |
40 |
|
444 |
return Just(!result->IsEmpty()); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
448 |
Maybe<bool> HKDFTraits::AdditionalConfig( |
44 |
|
|
CryptoJobMode mode, |
45 |
|
|
const FunctionCallbackInfo<Value>& args, |
46 |
|
|
unsigned int offset, |
47 |
|
|
HKDFConfig* params) { |
48 |
|
448 |
Environment* env = Environment::GetCurrent(args); |
49 |
|
|
|
50 |
|
448 |
params->mode = mode; |
51 |
|
|
|
52 |
✓✗✗✓
|
1344 |
CHECK(args[offset]->IsString()); // Hash |
53 |
✓✗✗✓
|
896 |
CHECK(args[offset + 1]->IsObject()); // Key |
54 |
✓✗✗✓
|
896 |
CHECK(IsAnyByteSource(args[offset + 2])); // Salt |
55 |
✓✗✗✓
|
896 |
CHECK(IsAnyByteSource(args[offset + 3])); // Info |
56 |
✓✗✗✓
|
896 |
CHECK(args[offset + 4]->IsUint32()); // Length |
57 |
|
|
|
58 |
✓✗ |
1344 |
Utf8Value hash(env->isolate(), args[offset]); |
59 |
|
448 |
params->digest = EVP_get_digestbyname(*hash); |
60 |
✓✓ |
448 |
if (params->digest == nullptr) { |
61 |
|
2 |
THROW_ERR_CRYPTO_INVALID_DIGEST(env); |
62 |
|
2 |
return Nothing<bool>(); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
KeyObjectHandle* key; |
66 |
✓✗✗✓
|
892 |
ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 1], Nothing<bool>()); |
67 |
|
446 |
params->key = key->Data(); |
68 |
|
|
|
69 |
✓✗ |
892 |
ArrayBufferOrViewContents<char> salt(args[offset + 2]); |
70 |
✓✗ |
892 |
ArrayBufferOrViewContents<char> info(args[offset + 3]); |
71 |
|
|
|
72 |
✗✓ |
446 |
if (UNLIKELY(!salt.CheckSizeInt32())) { |
73 |
|
|
THROW_ERR_OUT_OF_RANGE(env, "salt is too big"); |
74 |
|
|
return Nothing<bool>(); |
75 |
|
|
} |
76 |
✗✓ |
446 |
if (UNLIKELY(!info.CheckSizeInt32())) { |
77 |
|
|
THROW_ERR_OUT_OF_RANGE(env, "info is too big"); |
78 |
|
|
return Nothing<bool>(); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
params->salt = mode == kCryptoJobAsync |
82 |
✓✓ |
892 |
? salt.ToCopy() |
83 |
|
446 |
: salt.ToByteSource(); |
84 |
|
|
|
85 |
|
|
params->info = mode == kCryptoJobAsync |
86 |
✓✓ |
892 |
? info.ToCopy() |
87 |
|
446 |
: info.ToByteSource(); |
88 |
|
|
|
89 |
✓✗ |
1338 |
params->length = args[offset + 4].As<Uint32>()->Value(); |
90 |
|
446 |
size_t max_length = EVP_MD_size(params->digest) * kMaxDigestMultiplier; |
91 |
✓✓ |
446 |
if (params->length > max_length) { |
92 |
|
2 |
THROW_ERR_CRYPTO_INVALID_KEYLEN(env); |
93 |
|
2 |
return Nothing<bool>(); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
444 |
return Just(true); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
444 |
bool HKDFTraits::DeriveBits( |
100 |
|
|
Environment* env, |
101 |
|
|
const HKDFConfig& params, |
102 |
|
|
ByteSource* out) { |
103 |
|
|
EVPKeyCtxPointer ctx = |
104 |
|
888 |
EVPKeyCtxPointer(EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr)); |
105 |
✓✗✓✗
|
1332 |
if (!ctx || !EVP_PKEY_derive_init(ctx.get()) || |
106 |
|
444 |
!EVP_PKEY_CTX_hkdf_mode(ctx.get(), |
107 |
✓✗ |
444 |
EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND) || |
108 |
✓✗ |
888 |
!EVP_PKEY_CTX_set_hkdf_md(ctx.get(), params.digest) || |
109 |
|
444 |
!EVP_PKEY_CTX_set1_hkdf_salt( |
110 |
✓✗ |
888 |
ctx.get(), params.salt.data<unsigned char>(), params.salt.size()) || |
111 |
|
444 |
!EVP_PKEY_CTX_set1_hkdf_key( |
112 |
|
|
ctx.get(), |
113 |
|
444 |
reinterpret_cast<const unsigned char*>(params.key->GetSymmetricKey()), |
114 |
✓✗✗✓ ✗✓ |
1332 |
params.key->GetSymmetricKeySize()) || |
115 |
|
444 |
!EVP_PKEY_CTX_add1_hkdf_info( |
116 |
|
444 |
ctx.get(), params.info.data<unsigned char>(), params.info.size())) { |
117 |
|
|
return false; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
444 |
size_t length = params.length; |
121 |
|
888 |
ByteSource::Builder buf(length); |
122 |
✗✓ |
444 |
if (EVP_PKEY_derive(ctx.get(), buf.data<unsigned char>(), &length) <= 0) |
123 |
|
|
return false; |
124 |
|
|
|
125 |
|
444 |
*out = std::move(buf).release(); |
126 |
|
444 |
return true; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
void HKDFConfig::MemoryInfo(MemoryTracker* tracker) const { |
130 |
|
|
tracker->TrackField("key", key); |
131 |
|
|
// If the job is sync, then the HKDFConfig does not own the data |
132 |
|
|
if (mode == kCryptoJobAsync) { |
133 |
|
|
tracker->TrackFieldWithSize("salt", salt.size()); |
134 |
|
|
tracker->TrackFieldWithSize("info", info.size()); |
135 |
|
|
} |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
} // namespace crypto |
139 |
|
|
} // namespace node |