GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_keygen.cc Lines: 33 40 82.5 %
Date: 2022-11-08 04:21:23 Branches: 12 26 46.2 %

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) tracker->TrackFieldWithSize("out", length);
58
}
59
60
1299
Maybe<bool> SecretKeyGenTraits::AdditionalConfig(
61
    CryptoJobMode mode,
62
    const FunctionCallbackInfo<Value>& args,
63
    unsigned int* offset,
64
    SecretKeyGenConfig* params) {
65
1299
  Environment* env = Environment::GetCurrent(args);
66

2598
  CHECK(args[*offset]->IsUint32());
67
3897
  params->length = args[*offset].As<Uint32>()->Value() / CHAR_BIT;
68
1299
  if (params->length > INT_MAX) {
69
    THROW_ERR_OUT_OF_RANGE(env,
70
                           "length must be less than or equal to %u bits",
71
                           static_cast<uint64_t>(INT_MAX) * CHAR_BIT);
72
    return Nothing<bool>();
73
  }
74
1299
  *offset += 1;
75
1299
  return Just(true);
76
}
77
78
1299
KeyGenJobStatus SecretKeyGenTraits::DoKeyGen(
79
    Environment* env,
80
    SecretKeyGenConfig* params) {
81
1299
  CHECK_LE(params->length, INT_MAX);
82
2598
  ByteSource::Builder bytes(params->length);
83
1299
  if (CSPRNG(bytes.data<unsigned char>(), params->length).is_err())
84
    return KeyGenJobStatus::FAILED;
85
1299
  params->out = std::move(bytes).release();
86
1299
  return KeyGenJobStatus::OK;
87
}
88
89
1299
Maybe<bool> SecretKeyGenTraits::EncodeKey(Environment* env,
90
                                          SecretKeyGenConfig* params,
91
                                          Local<Value>* result) {
92
  std::shared_ptr<KeyObjectData> data =
93
1299
      KeyObjectData::CreateSecret(std::move(params->out));
94
2598
  return Just(KeyObjectHandle::Create(env, data).ToLocal(result));
95
}
96
97
namespace Keygen {
98
788
void Initialize(Environment* env, Local<Object> target) {
99
788
  NidKeyPairGenJob::Initialize(env, target);
100
788
  SecretKeyGenJob::Initialize(env, target);
101
788
}
102
103
5594
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
104
5594
  NidKeyPairGenJob::RegisterExternalReferences(registry);
105
5594
  SecretKeyGenJob::RegisterExternalReferences(registry);
106
5594
}
107
108
}  // namespace Keygen
109
}  // namespace crypto
110
}  // namespace node