GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_dh.h Lines: 0 11 0.0 %
Date: 2021-10-21 04:13:57 Branches: 0 0 - %

Line Branch Exec Source
1
#ifndef SRC_CRYPTO_CRYPTO_DH_H_
2
#define SRC_CRYPTO_CRYPTO_DH_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "crypto/crypto_keys.h"
7
#include "crypto/crypto_keygen.h"
8
#include "crypto/crypto_util.h"
9
#include "env.h"
10
#include "memory_tracker.h"
11
#include "v8.h"
12
13
#include <vector>
14
15
namespace node {
16
namespace crypto {
17
class DiffieHellman : public BaseObject {
18
 public:
19
  static void Initialize(Environment* env, v8::Local<v8::Object> target);
20
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
21
22
  bool Init(int primeLength, int g);
23
  bool Init(const char* p, int p_len, int g);
24
  bool Init(const char* p, int p_len, const char* g, int g_len);
25
26
  static void Stateless(const v8::FunctionCallbackInfo<v8::Value>& args);
27
28
 protected:
29
  static void DiffieHellmanGroup(
30
      const v8::FunctionCallbackInfo<v8::Value>& args);
31
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
32
  static void GenerateKeys(const v8::FunctionCallbackInfo<v8::Value>& args);
33
  static void GetPrime(const v8::FunctionCallbackInfo<v8::Value>& args);
34
  static void GetGenerator(const v8::FunctionCallbackInfo<v8::Value>& args);
35
  static void GetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
36
  static void GetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
37
  static void ComputeSecret(const v8::FunctionCallbackInfo<v8::Value>& args);
38
  static void SetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
39
  static void SetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
40
  static void VerifyErrorGetter(
41
      const v8::FunctionCallbackInfo<v8::Value>& args);
42
43
  DiffieHellman(Environment* env, v8::Local<v8::Object> wrap);
44
45
  void MemoryInfo(MemoryTracker* tracker) const override;
46
  SET_MEMORY_INFO_NAME(DiffieHellman)
47
  SET_SELF_SIZE(DiffieHellman)
48
49
 private:
50
  static void GetField(const v8::FunctionCallbackInfo<v8::Value>& args,
51
                       const BIGNUM* (*get_field)(const DH*),
52
                       const char* err_if_null);
53
  static void SetKey(const v8::FunctionCallbackInfo<v8::Value>& args,
54
                     int (*set_field)(DH*, BIGNUM*), const char* what);
55
  bool VerifyContext();
56
57
  int verifyError_;
58
  DHPointer dh_;
59
};
60
61
struct DhKeyPairParams final : public MemoryRetainer {
62
  // TODO(tniessen): Use std::variant instead.
63
  // Diffie-Hellman can either generate keys using a fixed prime, or by first
64
  // generating a random prime of a given size (in bits). Only one of both
65
  // options may be specified.
66
  BignumPointer prime_fixed_value;
67
  unsigned int prime_size;
68
  unsigned int generator;
69
  SET_NO_MEMORY_INFO()
70
  SET_MEMORY_INFO_NAME(DhKeyPairParams)
71
  SET_SELF_SIZE(DhKeyPairParams)
72
};
73
74
using DhKeyPairGenConfig = KeyPairGenConfig<DhKeyPairParams>;
75
76
struct DhKeyGenTraits final {
77
  using AdditionalParameters = DhKeyPairGenConfig;
78
  static constexpr const char* JobName = "DhKeyPairGenJob";
79
80
  static EVPKeyCtxPointer Setup(DhKeyPairGenConfig* params);
81
82
  static v8::Maybe<bool> AdditionalConfig(
83
      CryptoJobMode mode,
84
      const v8::FunctionCallbackInfo<v8::Value>& args,
85
      unsigned int* offset,
86
      DhKeyPairGenConfig* params);
87
};
88
89
using DHKeyPairGenJob = KeyGenJob<KeyPairGenTraits<DhKeyGenTraits>>;
90
91
struct DHKeyExportConfig final : public MemoryRetainer {
92
  SET_NO_MEMORY_INFO()
93
  SET_MEMORY_INFO_NAME(DHKeyExportConfig)
94
  SET_SELF_SIZE(DHKeyExportConfig)
95
};
96
97
struct DHKeyExportTraits final {
98
  static constexpr const char* JobName = "DHKeyExportJob";
99
  using AdditionalParameters = DHKeyExportConfig;
100
101
  static v8::Maybe<bool> AdditionalConfig(
102
      const v8::FunctionCallbackInfo<v8::Value>& args,
103
      unsigned int offset,
104
      DHKeyExportConfig* config);
105
106
  static WebCryptoKeyExportStatus DoExport(
107
      std::shared_ptr<KeyObjectData> key_data,
108
      WebCryptoKeyFormat format,
109
      const DHKeyExportConfig& params,
110
      ByteSource* out);
111
};
112
113
using DHKeyExportJob = KeyExportJob<DHKeyExportTraits>;
114
115
struct DHBitsConfig final : public MemoryRetainer {
116
  std::shared_ptr<KeyObjectData> private_key;
117
  std::shared_ptr<KeyObjectData> public_key;
118
  SET_NO_MEMORY_INFO()
119
  SET_MEMORY_INFO_NAME(DHBitsConfig)
120
  SET_SELF_SIZE(DHBitsConfig)
121
};
122
123
struct DHBitsTraits final {
124
  using AdditionalParameters = DHBitsConfig;
125
  static constexpr const char* JobName = "DHBitsJob";
126
  static constexpr AsyncWrap::ProviderType Provider =
127
      AsyncWrap::PROVIDER_DERIVEBITSREQUEST;
128
129
  static v8::Maybe<bool> AdditionalConfig(
130
      CryptoJobMode mode,
131
      const v8::FunctionCallbackInfo<v8::Value>& args,
132
      unsigned int offset,
133
      DHBitsConfig* params);
134
135
  static bool DeriveBits(
136
      Environment* env,
137
      const DHBitsConfig& params,
138
      ByteSource* out_);
139
140
  static v8::Maybe<bool> EncodeOutput(
141
      Environment* env,
142
      const DHBitsConfig& params,
143
      ByteSource* out,
144
      v8::Local<v8::Value>* result);
145
};
146
147
using DHBitsJob = DeriveBitsJob<DHBitsTraits>;
148
149
v8::Maybe<bool> GetDhKeyDetail(
150
    Environment* env,
151
    std::shared_ptr<KeyObjectData> key,
152
    v8::Local<v8::Object> target);
153
154
}  // namespace crypto
155
}  // namespace node
156
157
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
158
#endif  // SRC_CRYPTO_CRYPTO_DH_H_