GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_keygen.cc Lines: 34 42 81.0 %
Date: 2022-08-16 04:20:39 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
    const std::string msg{
72
      SPrintF("length must be less than or equal to %s bits",
73
              static_cast<uint64_t>(INT_MAX) * CHAR_BIT)
74
    };
75
    THROW_ERR_OUT_OF_RANGE(env, msg.c_str());
76
    return Nothing<bool>();
77
  }
78
1195
  *offset += 1;
79
1195
  return Just(true);
80
}
81
82
1195
KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(
83
    Environment* env,
84
    SecretKeyGenConfig* params) {
85
1195
  CHECK_LE(params->length, INT_MAX);
86
1195
  params->out = MallocOpenSSL<char>(params->length);
87
1195
  EntropySource(reinterpret_cast<unsigned char*>(params->out), params->length);
88
1195
  return KeyGenJobStatus::OK;
89
}
90
91
1195
Maybe<bool> SecretKeyGenTraits::EncodeKey(
92
    Environment* env,
93
    SecretKeyGenConfig* params,
94
    Local<Value>* result) {
95
2390
  ByteSource out = ByteSource::Allocated(params->out, params->length);
96
  std::shared_ptr<KeyObjectData> data =
97
1195
      KeyObjectData::CreateSecret(std::move(out));
98
2390
  return Just(KeyObjectHandle::Create(env, data).ToLocal(result));
99
}
100
101
namespace Keygen {
102
770
void Initialize(Environment* env, Local<Object> target) {
103
770
  NidKeyPairGenJob::Initialize(env, target);
104
770
  SecretKeyGenJob::Initialize(env, target);
105
770
}
106
107
5338
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
108
5338
  NidKeyPairGenJob::RegisterExternalReferences(registry);
109
5338
  SecretKeyGenJob::RegisterExternalReferences(registry);
110
5338
}
111
112
}  // namespace Keygen
113
}  // namespace crypto
114
}  // namespace node