• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <variant>
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(BignumPointer&& bn_p, int g);
24   bool Init(const char* p, int p_len, int g);
25   bool Init(const char* p, int p_len, const char* g, int g_len);
26 
27   static void Stateless(const v8::FunctionCallbackInfo<v8::Value>& args);
28 
29  protected:
30   static void DiffieHellmanGroup(
31       const v8::FunctionCallbackInfo<v8::Value>& args);
32   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
33   static void GenerateKeys(const v8::FunctionCallbackInfo<v8::Value>& args);
34   static void GetPrime(const v8::FunctionCallbackInfo<v8::Value>& args);
35   static void GetGenerator(const v8::FunctionCallbackInfo<v8::Value>& args);
36   static void GetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
37   static void GetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
38   static void ComputeSecret(const v8::FunctionCallbackInfo<v8::Value>& args);
39   static void SetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
40   static void SetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
41   static void VerifyErrorGetter(
42       const v8::FunctionCallbackInfo<v8::Value>& args);
43 
44   DiffieHellman(Environment* env, v8::Local<v8::Object> wrap);
45 
46   void MemoryInfo(MemoryTracker* tracker) const override;
47   SET_MEMORY_INFO_NAME(DiffieHellman)
48   SET_SELF_SIZE(DiffieHellman)
49 
50  private:
51   static void GetField(const v8::FunctionCallbackInfo<v8::Value>& args,
52                        const BIGNUM* (*get_field)(const DH*),
53                        const char* err_if_null);
54   static void SetKey(const v8::FunctionCallbackInfo<v8::Value>& args,
55                      int (*set_field)(DH*, BIGNUM*), const char* what);
56   bool VerifyContext();
57 
58   int verifyError_;
59   DHPointer dh_;
60 };
61 
62 struct DhKeyPairParams final : public MemoryRetainer {
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   std::variant<BignumPointer, int> prime;
67   unsigned int generator;
68   SET_NO_MEMORY_INFO()
69   SET_MEMORY_INFO_NAME(DhKeyPairParams)
70   SET_SELF_SIZE(DhKeyPairParams)
71 };
72 
73 using DhKeyPairGenConfig = KeyPairGenConfig<DhKeyPairParams>;
74 
75 struct DhKeyGenTraits final {
76   using AdditionalParameters = DhKeyPairGenConfig;
77   static constexpr const char* JobName = "DhKeyPairGenJob";
78 
79   static EVPKeyCtxPointer Setup(DhKeyPairGenConfig* params);
80 
81   static v8::Maybe<bool> AdditionalConfig(
82       CryptoJobMode mode,
83       const v8::FunctionCallbackInfo<v8::Value>& args,
84       unsigned int* offset,
85       DhKeyPairGenConfig* params);
86 };
87 
88 using DHKeyPairGenJob = KeyGenJob<KeyPairGenTraits<DhKeyGenTraits>>;
89 
90 struct DHKeyExportConfig final : public MemoryRetainer {
91   SET_NO_MEMORY_INFO()
92   SET_MEMORY_INFO_NAME(DHKeyExportConfig)
93   SET_SELF_SIZE(DHKeyExportConfig)
94 };
95 
96 struct DHKeyExportTraits final {
97   static constexpr const char* JobName = "DHKeyExportJob";
98   using AdditionalParameters = DHKeyExportConfig;
99 
100   static v8::Maybe<bool> AdditionalConfig(
101       const v8::FunctionCallbackInfo<v8::Value>& args,
102       unsigned int offset,
103       DHKeyExportConfig* config);
104 
105   static WebCryptoKeyExportStatus DoExport(
106       std::shared_ptr<KeyObjectData> key_data,
107       WebCryptoKeyFormat format,
108       const DHKeyExportConfig& params,
109       ByteSource* out);
110 };
111 
112 using DHKeyExportJob = KeyExportJob<DHKeyExportTraits>;
113 
114 struct DHBitsConfig final : public MemoryRetainer {
115   std::shared_ptr<KeyObjectData> private_key;
116   std::shared_ptr<KeyObjectData> public_key;
117   SET_NO_MEMORY_INFO()
118   SET_MEMORY_INFO_NAME(DHBitsConfig)
119   SET_SELF_SIZE(DHBitsConfig)
120 };
121 
122 struct DHBitsTraits final {
123   using AdditionalParameters = DHBitsConfig;
124   static constexpr const char* JobName = "DHBitsJob";
125   static constexpr AsyncWrap::ProviderType Provider =
126       AsyncWrap::PROVIDER_DERIVEBITSREQUEST;
127 
128   static v8::Maybe<bool> AdditionalConfig(
129       CryptoJobMode mode,
130       const v8::FunctionCallbackInfo<v8::Value>& args,
131       unsigned int offset,
132       DHBitsConfig* params);
133 
134   static bool DeriveBits(
135       Environment* env,
136       const DHBitsConfig& params,
137       ByteSource* out_);
138 
139   static v8::Maybe<bool> EncodeOutput(
140       Environment* env,
141       const DHBitsConfig& params,
142       ByteSource* out,
143       v8::Local<v8::Value>* result);
144 };
145 
146 using DHBitsJob = DeriveBitsJob<DHBitsTraits>;
147 
148 v8::Maybe<bool> GetDhKeyDetail(
149     Environment* env,
150     std::shared_ptr<KeyObjectData> key,
151     v8::Local<v8::Object> target);
152 
153 }  // namespace crypto
154 }  // namespace node
155 
156 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
157 #endif  // SRC_CRYPTO_CRYPTO_DH_H_
158