GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_keygen.cc Lines: 34 42 81.0 %
Date: 2022-03-17 03:14:58 Branches: 11 24 45.8 %

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

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

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

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