GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_keygen.cc Lines: 34 41 82.9 %
Date: 2022-08-31 04:20:31 Branches: 11 24 45.8 %

Line Branch Exec Source
1
#include "crypto/crypto_keygen.h"
2
#include "async_wrap-inl.h"
3
#include "base_object-inl.h"
4
#include "debug_utils-inl.h"
5
#include "env-inl.h"
6
#include "memory_tracker-inl.h"
7
#include "threadpoolwork-inl.h"
8
#include "v8.h"
9
10
#include <cmath>
11
12
namespace node {
13
14
using v8::FunctionCallbackInfo;
15
using v8::Int32;
16
using v8::Just;
17
using v8::Local;
18
using v8::Maybe;
19
using v8::Nothing;
20
using v8::Object;
21
using v8::Uint32;
22
using v8::Value;
23
24
namespace crypto {
25
// NidKeyPairGenJob input arguments:
26
//   1. CryptoJobMode
27
//   2. NID
28
//   3. Public Format
29
//   4. Public Type
30
//   5. Private Format
31
//   6. Private Type
32
//   7. Cipher
33
//   8. Passphrase
34
130
Maybe<bool> NidKeyPairGenTraits::AdditionalConfig(
35
    CryptoJobMode mode,
36
    const FunctionCallbackInfo<Value>& args,
37
    unsigned int* offset,
38
    NidKeyPairGenConfig* params) {
39

260
  CHECK(args[*offset]->IsInt32());
40
390
  params->params.id = args[*offset].As<Int32>()->Value();
41
42
130
  *offset += 1;
43
44
130
  return Just(true);
45
}
46
47
130
EVPKeyCtxPointer NidKeyPairGenTraits::Setup(NidKeyPairGenConfig* params) {
48
  EVPKeyCtxPointer ctx =
49
260
      EVPKeyCtxPointer(EVP_PKEY_CTX_new_id(params->params.id, nullptr));
50

130
  if (!ctx || EVP_PKEY_keygen_init(ctx.get()) <= 0)
51
    return EVPKeyCtxPointer();
52
53
130
  return ctx;
54
}
55
56
void SecretKeyGenConfig::MemoryInfo(MemoryTracker* tracker) const {
57
  if (out != nullptr)
58
    tracker->TrackFieldWithSize("out", length);
59
}
60
61
1195
Maybe<bool> SecretKeyGenTraits::AdditionalConfig(
62
    CryptoJobMode mode,
63
    const FunctionCallbackInfo<Value>& args,
64
    unsigned int* offset,
65
    SecretKeyGenConfig* params) {
66
1195
  Environment* env = Environment::GetCurrent(args);
67

2390
  CHECK(args[*offset]->IsUint32());
68
1195
  params->length = static_cast<size_t>(
69
3585
      std::trunc(args[*offset].As<Uint32>()->Value() / CHAR_BIT));
70
1195
  if (params->length > INT_MAX) {
71
    THROW_ERR_OUT_OF_RANGE(env,
72
                           "length must be less than or equal to %u bits",
73
                           static_cast<uint64_t>(INT_MAX) * CHAR_BIT);
74
    return Nothing<bool>();
75
  }
76
1195
  *offset += 1;
77
1195
  return Just(true);
78
}
79
80
1195
KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(
81
    Environment* env,
82
    SecretKeyGenConfig* params) {
83
1195
  CHECK_LE(params->length, INT_MAX);
84
1195
  params->out = MallocOpenSSL<char>(params->length);
85
1195
  EntropySource(reinterpret_cast<unsigned char*>(params->out), params->length);
86
1195
  return KeyGenJobStatus::OK;
87
}
88
89
1195
Maybe<bool> SecretKeyGenTraits::EncodeKey(
90
    Environment* env,
91
    SecretKeyGenConfig* params,
92
    Local<Value>* result) {
93
2390
  ByteSource out = ByteSource::Allocated(params->out, params->length);
94
  std::shared_ptr<KeyObjectData> data =
95
1195
      KeyObjectData::CreateSecret(std::move(out));
96
2390
  return Just(KeyObjectHandle::Create(env, data).ToLocal(result));
97
}
98
99
namespace Keygen {
100
779
void Initialize(Environment* env, Local<Object> target) {
101
779
  NidKeyPairGenJob::Initialize(env, target);
102
779
  SecretKeyGenJob::Initialize(env, target);
103
779
}
104
105
5422
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
106
5422
  NidKeyPairGenJob::RegisterExternalReferences(registry);
107
5422
  SecretKeyGenJob::RegisterExternalReferences(registry);
108
5422
}
109
110
}  // namespace Keygen
111
}  // namespace crypto
112
}  // namespace node