GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_scrypt.cc Lines: 57 68 83.8 %
Date: 2022-08-29 04:21:03 Branches: 28 52 53.8 %

Line Branch Exec Source
1
#include "crypto/crypto_scrypt.h"
2
#include "async_wrap-inl.h"
3
#include "crypto/crypto_util.h"
4
#include "env-inl.h"
5
#include "memory_tracker-inl.h"
6
#include "node_buffer.h"
7
#include "threadpoolwork-inl.h"
8
#include "v8.h"
9
10
namespace node {
11
12
using v8::FunctionCallbackInfo;
13
using v8::Int32;
14
using v8::Just;
15
using v8::Maybe;
16
using v8::Nothing;
17
using v8::Uint32;
18
using v8::Value;
19
20
namespace crypto {
21
#ifndef OPENSSL_NO_SCRYPT
22
23
28
ScryptConfig::ScryptConfig(ScryptConfig&& other) noexcept
24
28
  : mode(other.mode),
25
28
    pass(std::move(other.pass)),
26
28
    salt(std::move(other.salt)),
27
28
    N(other.N),
28
28
    r(other.r),
29
28
    p(other.p),
30
28
    maxmem(other.maxmem),
31
28
    length(other.length) {}
32
33
ScryptConfig& ScryptConfig::operator=(ScryptConfig&& other) noexcept {
34
  if (&other == this) return *this;
35
  this->~ScryptConfig();
36
  return *new (this) ScryptConfig(std::move(other));
37
}
38
39
1
void ScryptConfig::MemoryInfo(MemoryTracker* tracker) const {
40
1
  if (mode == kCryptoJobAsync) {
41
1
    tracker->TrackFieldWithSize("pass", pass.size());
42
1
    tracker->TrackFieldWithSize("salt", salt.size());
43
  }
44
1
}
45
46
28
Maybe<bool> ScryptTraits::EncodeOutput(
47
    Environment* env,
48
    const ScryptConfig& params,
49
    ByteSource* out,
50
    v8::Local<v8::Value>* result) {
51
56
  *result = out->ToArrayBuffer(env);
52
28
  return Just(!result->IsEmpty());
53
}
54
55
40
Maybe<bool> ScryptTraits::AdditionalConfig(
56
    CryptoJobMode mode,
57
    const FunctionCallbackInfo<Value>& args,
58
    unsigned int offset,
59
    ScryptConfig* params) {
60
40
  Environment* env = Environment::GetCurrent(args);
61
62
40
  params->mode = mode;
63
64
80
  ArrayBufferOrViewContents<char> pass(args[offset]);
65
80
  ArrayBufferOrViewContents<char> salt(args[offset + 1]);
66
67
40
  if (UNLIKELY(!pass.CheckSizeInt32())) {
68
    THROW_ERR_OUT_OF_RANGE(env, "pass is too large");
69
    return Nothing<bool>();
70
  }
71
72
40
  if (UNLIKELY(!salt.CheckSizeInt32())) {
73
    THROW_ERR_OUT_OF_RANGE(env, "salt is too large");
74
    return Nothing<bool>();
75
  }
76
77
  params->pass = mode == kCryptoJobAsync
78
80
      ? pass.ToCopy()
79
40
      : pass.ToByteSource();
80
81
  params->salt = mode == kCryptoJobAsync
82
80
      ? salt.ToCopy()
83
40
      : salt.ToByteSource();
84
85

80
  CHECK(args[offset + 2]->IsUint32());  // N
86

80
  CHECK(args[offset + 3]->IsUint32());  // r
87

80
  CHECK(args[offset + 4]->IsUint32());  // p
88

80
  CHECK(args[offset + 5]->IsNumber());  // maxmem
89

80
  CHECK(args[offset + 6]->IsInt32());  // length
90
91
120
  params->N = args[offset + 2].As<Uint32>()->Value();
92
120
  params->r = args[offset + 3].As<Uint32>()->Value();
93
120
  params->p = args[offset + 4].As<Uint32>()->Value();
94
80
  params->maxmem = args[offset + 5]->IntegerValue(env->context()).ToChecked();
95
96
200
  if (EVP_PBE_scrypt(
97
          nullptr,
98
          0,
99
          nullptr,
100
          0,
101
40
          params->N,
102
40
          params->r,
103
40
          params->p,
104
          params->maxmem,
105
          nullptr,
106
40
          0) != 1) {
107
12
    THROW_ERR_CRYPTO_INVALID_SCRYPT_PARAMS(env);
108
12
    return Nothing<bool>();
109
  }
110
111
84
  params->length = args[offset + 6].As<Int32>()->Value();
112
28
  if (params->length < 0) {
113
    THROW_ERR_OUT_OF_RANGE(env, "length must be <= %d", INT_MAX);
114
    return Nothing<bool>();
115
  }
116
117
28
  return Just(true);
118
}
119
120
28
bool ScryptTraits::DeriveBits(
121
    Environment* env,
122
    const ScryptConfig& params,
123
    ByteSource* out) {
124
56
  ByteSource::Builder buf(params.length);
125
126
  // Both the pass and salt may be zero-length at this point
127
128
28
  if (!EVP_PBE_scrypt(params.pass.data<char>(),
129
                      params.pass.size(),
130
                      params.salt.data<unsigned char>(),
131
                      params.salt.size(),
132
28
                      params.N,
133
28
                      params.r,
134
28
                      params.p,
135
28
                      params.maxmem,
136
                      buf.data<unsigned char>(),
137
28
                      params.length)) {
138
    return false;
139
  }
140
28
  *out = std::move(buf).release();
141
28
  return true;
142
}
143
144
#endif  // !OPENSSL_NO_SCRYPT
145
146
}  // namespace crypto
147
}  // namespace node