GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_keygen.cc Lines: 34 42 81.0 %
Date: 2022-06-15 16:51:59 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
48
Maybe<bool> NidKeyPairGenTraits::AdditionalConfig(
35
    CryptoJobMode mode,
36
    const FunctionCallbackInfo<Value>& args,
37
    unsigned int* offset,
38
    NidKeyPairGenConfig* params) {
39

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

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

360
  CHECK(args[*offset]->IsUint32());
68
180
  params->length = static_cast<size_t>(
69
540
      std::trunc(args[*offset].As<Uint32>()->Value() / CHAR_BIT));
70
180
  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
180
  *offset += 1;
79
180
  return Just(true);
80
}
81
82
180
KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(
83
    Environment* env,
84
    SecretKeyGenConfig* params) {
85
180
  CHECK_LE(params->length, INT_MAX);
86
180
  params->out = MallocOpenSSL<char>(params->length);
87
180
  EntropySource(reinterpret_cast<unsigned char*>(params->out), params->length);
88
180
  return KeyGenJobStatus::OK;
89
}
90
91
180
Maybe<bool> SecretKeyGenTraits::EncodeKey(
92
    Environment* env,
93
    SecretKeyGenConfig* params,
94
    Local<Value>* result) {
95
360
  ByteSource out = ByteSource::Allocated(params->out, params->length);
96
  std::shared_ptr<KeyObjectData> data =
97
180
      KeyObjectData::CreateSecret(std::move(out));
98
360
  return Just(KeyObjectHandle::Create(env, data).ToLocal(result));
99
}
100
101
namespace Keygen {
102
848
void Initialize(Environment* env, Local<Object> target) {
103
848
  NidKeyPairGenJob::Initialize(env, target);
104
848
  SecretKeyGenJob::Initialize(env, target);
105
848
}
106
107
5205
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
108
5205
  NidKeyPairGenJob::RegisterExternalReferences(registry);
109
5205
  SecretKeyGenJob::RegisterExternalReferences(registry);
110
5205
}
111
112
}  // namespace Keygen
113
}  // namespace crypto
114
}  // namespace node