GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_keygen.cc Lines: 30 38 78.9 %
Date: 2021-09-20 04:12:42 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
24
Maybe<bool> NidKeyPairGenTraits::AdditionalConfig(
36
    CryptoJobMode mode,
37
    const FunctionCallbackInfo<Value>& args,
38
    unsigned int* offset,
39
    NidKeyPairGenConfig* params) {
40

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

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

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