GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/crypto/crypto_keygen.cc Lines: 31 39 79.5 %
Date: 2021-05-29 04:12:01 Branches: 9 20 45.0 %

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
20
Maybe<bool> NidKeyPairGenTraits::AdditionalConfig(
36
    CryptoJobMode mode,
37
    const FunctionCallbackInfo<Value>& args,
38
    unsigned int* offset,
39
    NidKeyPairGenConfig* params) {
40
60
  CHECK(args[*offset]->IsInt32());
41
80
  params->params.id = args[*offset].As<Int32>()->Value();
42
43
20
  *offset += 1;
44
45
20
  return Just(true);
46
}
47
48
20
EVPKeyCtxPointer NidKeyPairGenTraits::Setup(NidKeyPairGenConfig* params) {
49
  EVPKeyCtxPointer ctx =
50
40
      EVPKeyCtxPointer(EVP_PKEY_CTX_new_id(params->params.id, nullptr));
51

20
  if (!ctx || EVP_PKEY_keygen_init(ctx.get()) <= 0)
52
    return EVPKeyCtxPointer();
53
54
20
  return ctx;
55
}
56
57
void SecretKeyGenConfig::MemoryInfo(MemoryTracker* tracker) const {
58
  if (out != nullptr)
59
    tracker->TrackFieldWithSize("out", length);
60
}
61
62
82
Maybe<bool> SecretKeyGenTraits::AdditionalConfig(
63
    CryptoJobMode mode,
64
    const FunctionCallbackInfo<Value>& args,
65
    unsigned int* offset,
66
    SecretKeyGenConfig* params) {
67
82
  Environment* env = Environment::GetCurrent(args);
68
246
  CHECK(args[*offset]->IsUint32());
69
82
  params->length = static_cast<size_t>(
70
410
      std::trunc(args[*offset].As<Uint32>()->Value() / CHAR_BIT));
71
82
  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
82
  *offset += 1;
80
82
  return Just(true);
81
}
82
83
82
KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(
84
    Environment* env,
85
    SecretKeyGenConfig* params) {
86
82
  CHECK_LE(params->length, INT_MAX);
87
82
  params->out = MallocOpenSSL<char>(params->length);
88
82
  EntropySource(reinterpret_cast<unsigned char*>(params->out), params->length);
89
82
  return KeyGenJobStatus::OK;
90
}
91
92
82
Maybe<bool> SecretKeyGenTraits::EncodeKey(
93
    Environment* env,
94
    SecretKeyGenConfig* params,
95
    Local<Value>* result) {
96
164
  ByteSource out = ByteSource::Allocated(params->out, params->length);
97
  std::shared_ptr<KeyObjectData> data =
98
164
      KeyObjectData::CreateSecret(std::move(out));
99
246
  return Just(KeyObjectHandle::Create(env, data).ToLocal(result));
100
}
101
102
namespace Keygen {
103
4165
void Initialize(Environment* env, Local<Object> target) {
104
4165
  NidKeyPairGenJob::Initialize(env, target);
105
4165
  SecretKeyGenJob::Initialize(env, target);
106
4165
}
107
}  // namespace Keygen
108
}  // namespace crypto
109

14520
}  // namespace node