GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_hmac.cc Lines: 116 144 80.6 %
Date: 2021-09-20 04:12:42 Branches: 49 93 52.7 %

Line Branch Exec Source
1
#include "crypto/crypto_hmac.h"
2
#include "crypto/crypto_keys.h"
3
#include "crypto/crypto_sig.h"
4
#include "crypto/crypto_util.h"
5
#include "allocated_buffer-inl.h"
6
#include "async_wrap-inl.h"
7
#include "base_object-inl.h"
8
#include "env-inl.h"
9
#include "memory_tracker-inl.h"
10
#include "node_buffer.h"
11
#include "string_bytes.h"
12
#include "threadpoolwork-inl.h"
13
#include "v8.h"
14
15
namespace node {
16
17
using v8::FunctionCallbackInfo;
18
using v8::FunctionTemplate;
19
using v8::HandleScope;
20
using v8::Just;
21
using v8::Local;
22
using v8::Maybe;
23
using v8::MaybeLocal;
24
using v8::Nothing;
25
using v8::Object;
26
using v8::Uint32;
27
using v8::Value;
28
29
namespace crypto {
30
164
Hmac::Hmac(Environment* env, Local<Object> wrap)
31
    : BaseObject(env, wrap),
32
164
      ctx_(nullptr) {
33
164
  MakeWeak();
34
164
}
35
36
void Hmac::MemoryInfo(MemoryTracker* tracker) const {
37
  tracker->TrackFieldWithSize("context", ctx_ ? kSizeOf_HMAC_CTX : 0);
38
}
39
40
4299
void Hmac::Initialize(Environment* env, Local<Object> target) {
41
4299
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
42
43
8598
  t->InstanceTemplate()->SetInternalFieldCount(
44
      Hmac::kInternalFieldCount);
45
4299
  t->Inherit(BaseObject::GetConstructorTemplate(env));
46
47
4299
  env->SetProtoMethod(t, "init", HmacInit);
48
4299
  env->SetProtoMethod(t, "update", HmacUpdate);
49
4299
  env->SetProtoMethod(t, "digest", HmacDigest);
50
51
4299
  env->SetConstructorFunction(target, "Hmac", t);
52
53
4299
  HmacJob::Initialize(env, target);
54
4299
}
55
56
164
void Hmac::New(const FunctionCallbackInfo<Value>& args) {
57
164
  Environment* env = Environment::GetCurrent(args);
58
164
  new Hmac(env, args.This());
59
164
}
60
61
164
void Hmac::HmacInit(const char* hash_type, const char* key, int key_len) {
62
164
  HandleScope scope(env()->isolate());
63
64
164
  const EVP_MD* md = EVP_get_digestbyname(hash_type);
65
164
  if (md == nullptr)
66
1
    return THROW_ERR_CRYPTO_INVALID_DIGEST(env());
67
163
  if (key_len == 0) {
68
6
    key = "";
69
  }
70
163
  ctx_.reset(HMAC_CTX_new());
71

163
  if (!ctx_ || !HMAC_Init_ex(ctx_.get(), key, key_len, md, nullptr)) {
72
    ctx_.reset();
73
    return ThrowCryptoError(env(), ERR_get_error());
74
  }
75
}
76
77
164
void Hmac::HmacInit(const FunctionCallbackInfo<Value>& args) {
78
  Hmac* hmac;
79
164
  ASSIGN_OR_RETURN_UNWRAP(&hmac, args.Holder());
80
164
  Environment* env = hmac->env();
81
82
328
  const node::Utf8Value hash_type(env->isolate(), args[0]);
83
328
  ByteSource key = ByteSource::FromSecretKeyBytes(env, args[1]);
84
164
  hmac->HmacInit(*hash_type, key.get(), key.size());
85
}
86
87
162
bool Hmac::HmacUpdate(const char* data, size_t len) {
88

162
  return ctx_ && HMAC_Update(ctx_.get(),
89
                             reinterpret_cast<const unsigned char*>(data),
90
324
                             len) == 1;
91
}
92
93
162
void Hmac::HmacUpdate(const FunctionCallbackInfo<Value>& args) {
94
162
  Decode<Hmac>(args, [](Hmac* hmac, const FunctionCallbackInfo<Value>& args,
95
162
                        const char* data, size_t size) {
96
162
    Environment* env = Environment::GetCurrent(args);
97
162
    if (UNLIKELY(size > INT_MAX))
98
      return THROW_ERR_OUT_OF_RANGE(env, "data is too long");
99
162
    bool r = hmac->HmacUpdate(data, size);
100
324
    args.GetReturnValue().Set(r);
101
  });
102
162
}
103
104
157
void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
105
157
  Environment* env = Environment::GetCurrent(args);
106
107
  Hmac* hmac;
108
157
  ASSIGN_OR_RETURN_UNWRAP(&hmac, args.Holder());
109
110
157
  enum encoding encoding = BUFFER;
111
157
  if (args.Length() >= 1) {
112
129
    encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
113
  }
114
115
  unsigned char md_value[EVP_MAX_MD_SIZE];
116
157
  unsigned int md_len = 0;
117
118
157
  if (hmac->ctx_) {
119
157
    HMAC_Final(hmac->ctx_.get(), md_value, &md_len);
120
157
    hmac->ctx_.reset();
121
  }
122
123
  Local<Value> error;
124
  MaybeLocal<Value> rc =
125
      StringBytes::Encode(env->isolate(),
126
                          reinterpret_cast<const char*>(md_value),
127
                          md_len,
128
                          encoding,
129
157
                          &error);
130
157
  if (rc.IsEmpty()) {
131
    CHECK(!error.IsEmpty());
132
    env->isolate()->ThrowException(error);
133
    return;
134
  }
135
471
  args.GetReturnValue().Set(rc.FromMaybe(Local<Value>()));
136
}
137
138
102
HmacConfig::HmacConfig(HmacConfig&& other) noexcept
139
102
    : job_mode(other.job_mode),
140
102
      mode(other.mode),
141
102
      key(std::move(other.key)),
142
102
      data(std::move(other.data)),
143
102
      signature(std::move(other.signature)),
144
102
      digest(other.digest) {}
145
146
HmacConfig& HmacConfig::operator=(HmacConfig&& other) noexcept {
147
  if (&other == this) return *this;
148
  this->~HmacConfig();
149
  return *new (this) HmacConfig(std::move(other));
150
}
151
152
void HmacConfig::MemoryInfo(MemoryTracker* tracker) const {
153
  tracker->TrackField("key", key.get());
154
  // If the job is sync, then the HmacConfig does not own the data
155
  if (job_mode == kCryptoJobAsync) {
156
    tracker->TrackFieldWithSize("data", data.size());
157
    tracker->TrackFieldWithSize("signature", signature.size());
158
  }
159
}
160
161
102
Maybe<bool> HmacTraits::AdditionalConfig(
162
    CryptoJobMode mode,
163
    const FunctionCallbackInfo<Value>& args,
164
    unsigned int offset,
165
    HmacConfig* params) {
166
102
  Environment* env = Environment::GetCurrent(args);
167
168
102
  params->job_mode = mode;
169
170

204
  CHECK(args[offset]->IsUint32());  // SignConfiguration::Mode
171
102
  params->mode =
172
306
    static_cast<SignConfiguration::Mode>(args[offset].As<Uint32>()->Value());
173
174

306
  CHECK(args[offset + 1]->IsString());  // Hash
175

204
  CHECK(args[offset + 2]->IsObject());  // Key
176
177
306
  Utf8Value digest(env->isolate(), args[offset + 1]);
178
102
  params->digest = EVP_get_digestbyname(*digest);
179
102
  if (params->digest == nullptr) {
180
    THROW_ERR_CRYPTO_INVALID_DIGEST(env);
181
    return Nothing<bool>();
182
  }
183
184
  KeyObjectHandle* key;
185

204
  ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 2], Nothing<bool>());
186
102
  params->key = key->Data();
187
188
306
  ArrayBufferOrViewContents<char> data(args[offset + 3]);
189
102
  if (UNLIKELY(!data.CheckSizeInt32())) {
190
    THROW_ERR_OUT_OF_RANGE(env, "data is too big");
191
    return Nothing<bool>();
192
  }
193
  params->data = mode == kCryptoJobAsync
194
204
      ? data.ToCopy()
195
102
      : data.ToByteSource();
196
197

306
  if (!args[offset + 4]->IsUndefined()) {
198
148
    ArrayBufferOrViewContents<char> signature(args[offset + 4]);
199
74
    if (UNLIKELY(!signature.CheckSizeInt32())) {
200
      THROW_ERR_OUT_OF_RANGE(env, "signature is too big");
201
      return Nothing<bool>();
202
    }
203
    params->signature = mode == kCryptoJobAsync
204
148
        ? signature.ToCopy()
205
74
        : signature.ToByteSource();
206
  }
207
208
102
  return Just(true);
209
}
210
211
102
bool HmacTraits::DeriveBits(
212
    Environment* env,
213
    const HmacConfig& params,
214
    ByteSource* out) {
215
204
  HMACCtxPointer ctx(HMAC_CTX_new());
216
217

204
  if (!ctx ||
218
102
      !HMAC_Init_ex(
219
          ctx.get(),
220
102
          params.key->GetSymmetricKey(),
221
102
          params.key->GetSymmetricKeySize(),
222
102
          params.digest,
223
          nullptr)) {
224
    return false;
225
  }
226
227
102
  if (!HMAC_Update(
228
          ctx.get(),
229
          params.data.data<unsigned char>(),
230
          params.data.size())) {
231
    return false;
232
  }
233
234
102
  char* data = MallocOpenSSL<char>(EVP_MAX_MD_SIZE);
235
204
  ByteSource buf = ByteSource::Allocated(data, EVP_MAX_MD_SIZE);
236
102
  unsigned char* ptr = reinterpret_cast<unsigned char*>(data);
237
  unsigned int len;
238
239
102
  if (!HMAC_Final(ctx.get(), ptr, &len)) {
240
    return false;
241
  }
242
243
102
  buf.Resize(len);
244
102
  *out = std::move(buf);
245
246
102
  return true;
247
}
248
249
102
Maybe<bool> HmacTraits::EncodeOutput(
250
    Environment* env,
251
    const HmacConfig& params,
252
    ByteSource* out,
253
    Local<Value>* result) {
254
102
  switch (params.mode) {
255
28
    case SignConfiguration::kSign:
256
28
      *result = out->ToArrayBuffer(env);
257
28
      break;
258
74
    case SignConfiguration::kVerify:
259
120
      *result =
260
148
          out->size() > 0 &&
261
74
          out->size() == params.signature.size() &&
262
66
          memcmp(out->get(), params.signature.get(), out->size()) == 0
263
46
              ? v8::True(env->isolate())
264
28
              : v8::False(env->isolate());
265
74
      break;
266
    default:
267
      UNREACHABLE();
268
  }
269
102
  return Just(!result->IsEmpty());
270
}
271
272
}  // namespace crypto
273
}  // namespace node