@encharm/cws 4.5.2 → 4.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (284) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/CLAUDE.md +81 -0
  3. package/README.md +10 -5
  4. package/dist/bindings/cws_darwin_arm64_node115.node +0 -0
  5. package/dist/bindings/cws_darwin_arm64_node127.node +0 -0
  6. package/dist/bindings/cws_darwin_arm64_node137.node +0 -0
  7. package/dist/bindings/cws_darwin_arm64_node147.node +0 -0
  8. package/dist/bindings/cws_linux_arm64_node115.node +0 -0
  9. package/dist/bindings/cws_linux_arm64_node127.node +0 -0
  10. package/dist/bindings/cws_linux_arm64_node137.node +0 -0
  11. package/dist/bindings/cws_linux_arm64_node147.node +0 -0
  12. package/dist/bindings/cws_linux_x64_node115.node +0 -0
  13. package/dist/bindings/cws_linux_x64_node127.node +0 -0
  14. package/dist/bindings/cws_linux_x64_node137.node +0 -0
  15. package/dist/bindings/cws_linux_x64_node147.node +0 -0
  16. package/dist/bindings/cws_win32_x64_node115.node +0 -0
  17. package/dist/bindings/cws_win32_x64_node127.node +0 -0
  18. package/dist/bindings/cws_win32_x64_node137.node +0 -0
  19. package/dist/bindings/cws_win32_x64_node147.node +0 -0
  20. package/package.json +1 -1
  21. package/src/Addon.cpp +2 -1
  22. package/src/Addon.h +70 -14
  23. package/src/Hub.h +1 -0
  24. package/src/Networking.h +11 -0
  25. package/src/Socket.h +201 -6
  26. package/src/WebSocket.cpp +4 -1
  27. package/src/cSNode.cpp +2 -0
  28. package/src/cSNode.h +4 -0
  29. package/src/headers/24/tracing/agent.h +195 -0
  30. package/src/headers/24/tracing/trace_event.h +722 -0
  31. package/src/headers/26/acorn_version.h +6 -0
  32. package/src/headers/26/aliased_buffer-inl.h +264 -0
  33. package/src/headers/26/aliased_buffer.h +205 -0
  34. package/src/headers/26/aliased_struct-inl.h +143 -0
  35. package/src/headers/26/aliased_struct.h +248 -0
  36. package/src/headers/26/amaro_version.h +6 -0
  37. package/src/headers/26/async_context_frame.h +33 -0
  38. package/src/headers/26/async_wrap-inl.h +123 -0
  39. package/src/headers/26/async_wrap.h +254 -0
  40. package/src/headers/26/base_object-inl.h +333 -0
  41. package/src/headers/26/base_object.h +339 -0
  42. package/src/headers/26/base_object_types.h +76 -0
  43. package/src/headers/26/blob_serializer_deserializer-inl.h +385 -0
  44. package/src/headers/26/blob_serializer_deserializer.h +142 -0
  45. package/src/headers/26/builtin_info.h +57 -0
  46. package/src/headers/26/callback_queue-inl.h +97 -0
  47. package/src/headers/26/callback_queue.h +79 -0
  48. package/src/headers/26/cares_wrap.h +475 -0
  49. package/src/headers/26/cleanup_queue-inl.h +36 -0
  50. package/src/headers/26/cleanup_queue.h +79 -0
  51. package/src/headers/26/compile_cache.h +134 -0
  52. package/src/headers/26/connect_wrap.h +26 -0
  53. package/src/headers/26/connection_wrap.h +30 -0
  54. package/src/headers/26/cppgc_helpers-inl.h +69 -0
  55. package/src/headers/26/cppgc_helpers.h +162 -0
  56. package/src/headers/26/crypto/crypto_aes.h +107 -0
  57. package/src/headers/26/crypto/crypto_argon2.h +88 -0
  58. package/src/headers/26/crypto/crypto_bio.h +193 -0
  59. package/src/headers/26/crypto/crypto_chacha20_poly1305.h +62 -0
  60. package/src/headers/26/crypto/crypto_cipher.h +291 -0
  61. package/src/headers/26/crypto/crypto_common.h +48 -0
  62. package/src/headers/26/crypto/crypto_context.h +219 -0
  63. package/src/headers/26/crypto/crypto_dh.h +104 -0
  64. package/src/headers/26/crypto/crypto_dsa.h +50 -0
  65. package/src/headers/26/crypto/crypto_ec.h +102 -0
  66. package/src/headers/26/crypto/crypto_hash.h +157 -0
  67. package/src/headers/26/crypto/crypto_hkdf.h +62 -0
  68. package/src/headers/26/crypto/crypto_hmac.h +92 -0
  69. package/src/headers/26/crypto/crypto_kem.h +131 -0
  70. package/src/headers/26/crypto/crypto_keygen.h +382 -0
  71. package/src/headers/26/crypto/crypto_keys.h +366 -0
  72. package/src/headers/26/crypto/crypto_kmac.h +79 -0
  73. package/src/headers/26/crypto/crypto_pbkdf2.h +76 -0
  74. package/src/headers/26/crypto/crypto_pqc.h +39 -0
  75. package/src/headers/26/crypto/crypto_random.h +127 -0
  76. package/src/headers/26/crypto/crypto_rsa.h +108 -0
  77. package/src/headers/26/crypto/crypto_scrypt.h +85 -0
  78. package/src/headers/26/crypto/crypto_sig.h +159 -0
  79. package/src/headers/26/crypto/crypto_spkac.h +21 -0
  80. package/src/headers/26/crypto/crypto_timing.h +20 -0
  81. package/src/headers/26/crypto/crypto_tls.h +342 -0
  82. package/src/headers/26/crypto/crypto_turboshake.h +105 -0
  83. package/src/headers/26/crypto/crypto_util.h +775 -0
  84. package/src/headers/26/crypto/crypto_x509.h +140 -0
  85. package/src/headers/26/dataqueue/queue.h +311 -0
  86. package/src/headers/26/debug_utils-inl.h +291 -0
  87. package/src/headers/26/debug_utils.h +200 -0
  88. package/src/headers/26/diagnosticfilename-inl.h +33 -0
  89. package/src/headers/26/embedded_data.h +17 -0
  90. package/src/headers/26/encoding_binding.h +59 -0
  91. package/src/headers/26/env-inl.h +956 -0
  92. package/src/headers/26/env.h +1294 -0
  93. package/src/headers/26/env_properties.h +562 -0
  94. package/src/headers/26/ffi/data.h +30 -0
  95. package/src/headers/26/ffi/fast.h +96 -0
  96. package/src/headers/26/ffi/jit_memory.h +26 -0
  97. package/src/headers/26/ffi/types.h +99 -0
  98. package/src/headers/26/handle_wrap.h +123 -0
  99. package/src/headers/26/histogram-inl.h +211 -0
  100. package/src/headers/26/histogram.h +474 -0
  101. package/src/headers/26/inspector/dom_storage_agent.h +68 -0
  102. package/src/headers/26/inspector/inspector_object_utils.h +38 -0
  103. package/src/headers/26/inspector/io_agent.h +30 -0
  104. package/src/headers/26/inspector/main_thread_interface.h +116 -0
  105. package/src/headers/26/inspector/network_agent.h +110 -0
  106. package/src/headers/26/inspector/network_inspector.h +45 -0
  107. package/src/headers/26/inspector/network_requests_buffer.h +195 -0
  108. package/src/headers/26/inspector/network_resource_manager.h +29 -0
  109. package/src/headers/26/inspector/node_json.h +24 -0
  110. package/src/headers/26/inspector/node_string.h +101 -0
  111. package/src/headers/26/inspector/notification_emitter.h +38 -0
  112. package/src/headers/26/inspector/protocol_helper.h +27 -0
  113. package/src/headers/26/inspector/runtime_agent.h +41 -0
  114. package/src/headers/26/inspector/storage_agent.h +33 -0
  115. package/src/headers/26/inspector/target_agent.h +59 -0
  116. package/src/headers/26/inspector/target_manager.h +70 -0
  117. package/src/headers/26/inspector/tracing_agent.h +43 -0
  118. package/src/headers/26/inspector/worker_agent.h +40 -0
  119. package/src/headers/26/inspector/worker_inspector.h +124 -0
  120. package/src/headers/26/inspector_agent.h +168 -0
  121. package/src/headers/26/inspector_io.h +78 -0
  122. package/src/headers/26/inspector_profiler.h +149 -0
  123. package/src/headers/26/inspector_socket.h +60 -0
  124. package/src/headers/26/inspector_socket_server.h +110 -0
  125. package/src/headers/26/js_native_api.h +634 -0
  126. package/src/headers/26/js_native_api_types.h +244 -0
  127. package/src/headers/26/js_native_api_v8.h +485 -0
  128. package/src/headers/26/js_native_api_v8_internals.h +45 -0
  129. package/src/headers/26/js_stream.h +57 -0
  130. package/src/headers/26/json_utils.h +172 -0
  131. package/src/headers/26/large_pages/node_large_page.h +34 -0
  132. package/src/headers/26/lru_cache-inl.h +69 -0
  133. package/src/headers/26/memory_tracker-inl.h +461 -0
  134. package/src/headers/26/memory_tracker.h +341 -0
  135. package/src/headers/26/module_wrap.h +231 -0
  136. package/src/headers/26/ncrypto.h +2008 -0
  137. package/src/headers/26/node.h +1693 -0
  138. package/src/headers/26/node_api.h +265 -0
  139. package/src/headers/26/node_api_internals.h +48 -0
  140. package/src/headers/26/node_api_types.h +58 -0
  141. package/src/headers/26/node_binding.h +170 -0
  142. package/src/headers/26/node_blob.h +160 -0
  143. package/src/headers/26/node_bob-inl.h +36 -0
  144. package/src/headers/26/node_bob.h +107 -0
  145. package/src/headers/26/node_buffer.h +92 -0
  146. package/src/headers/26/node_builtins.h +239 -0
  147. package/src/headers/26/node_concepts.h +46 -0
  148. package/src/headers/26/node_config_file.h +77 -0
  149. package/src/headers/26/node_constants.h +82 -0
  150. package/src/headers/26/node_context_data.h +171 -0
  151. package/src/headers/26/node_contextify.h +261 -0
  152. package/src/headers/26/node_crypto.h +66 -0
  153. package/src/headers/26/node_debug.h +30 -0
  154. package/src/headers/26/node_diagnostics_channel.h +121 -0
  155. package/src/headers/26/node_dir.h +52 -0
  156. package/src/headers/26/node_dotenv.h +45 -0
  157. package/src/headers/26/node_errors.h +378 -0
  158. package/src/headers/26/node_exit_code.h +54 -0
  159. package/src/headers/26/node_external_reference.h +171 -0
  160. package/src/headers/26/node_ffi.h +212 -0
  161. package/src/headers/26/node_file-inl.h +421 -0
  162. package/src/headers/26/node_file.h +561 -0
  163. package/src/headers/26/node_file_utils.h +39 -0
  164. package/src/headers/26/node_hash.h +212 -0
  165. package/src/headers/26/node_http2.h +1202 -0
  166. package/src/headers/26/node_http2_state.h +159 -0
  167. package/src/headers/26/node_http_common-inl.h +199 -0
  168. package/src/headers/26/node_http_common.h +535 -0
  169. package/src/headers/26/node_i18n.h +132 -0
  170. package/src/headers/26/node_internals.h +485 -0
  171. package/src/headers/26/node_locks.h +184 -0
  172. package/src/headers/26/node_main_instance.h +67 -0
  173. package/src/headers/26/node_mem-inl.h +113 -0
  174. package/src/headers/26/node_mem.h +45 -0
  175. package/src/headers/26/node_messaging.h +389 -0
  176. package/src/headers/26/node_metadata.h +172 -0
  177. package/src/headers/26/node_modules.h +102 -0
  178. package/src/headers/26/node_mutex.h +323 -0
  179. package/src/headers/26/node_object_wrap.h +145 -0
  180. package/src/headers/26/node_options-inl.h +524 -0
  181. package/src/headers/26/node_options.h +710 -0
  182. package/src/headers/26/node_perf.h +167 -0
  183. package/src/headers/26/node_perf_common.h +103 -0
  184. package/src/headers/26/node_platform.h +290 -0
  185. package/src/headers/26/node_process-inl.h +26 -0
  186. package/src/headers/26/node_process.h +112 -0
  187. package/src/headers/26/node_profiling.h +38 -0
  188. package/src/headers/26/node_realm-inl.h +156 -0
  189. package/src/headers/26/node_realm.h +236 -0
  190. package/src/headers/26/node_report.h +43 -0
  191. package/src/headers/26/node_revert.h +82 -0
  192. package/src/headers/26/node_root_certs.h +2937 -0
  193. package/src/headers/26/node_sea.h +101 -0
  194. package/src/headers/26/node_shadow_realm.h +46 -0
  195. package/src/headers/26/node_snapshot_builder.h +57 -0
  196. package/src/headers/26/node_snapshotable.h +167 -0
  197. package/src/headers/26/node_sockaddr-inl.h +248 -0
  198. package/src/headers/26/node_sockaddr.h +459 -0
  199. package/src/headers/26/node_sqlite.h +607 -0
  200. package/src/headers/26/node_stat_watcher.h +73 -0
  201. package/src/headers/26/node_task_runner.h +92 -0
  202. package/src/headers/26/node_threadsafe_cow-inl.h +54 -0
  203. package/src/headers/26/node_threadsafe_cow.h +105 -0
  204. package/src/headers/26/node_union_bytes.h +81 -0
  205. package/src/headers/26/node_url.h +101 -0
  206. package/src/headers/26/node_url_pattern.h +118 -0
  207. package/src/headers/26/node_v8.h +78 -0
  208. package/src/headers/26/node_v8_embedder.h +26 -0
  209. package/src/headers/26/node_v8_platform-inl.h +96 -0
  210. package/src/headers/26/node_version.h +116 -0
  211. package/src/headers/26/node_wasi.h +184 -0
  212. package/src/headers/26/node_wasm_web_api.h +55 -0
  213. package/src/headers/26/node_watchdog.h +164 -0
  214. package/src/headers/26/node_webstorage.h +62 -0
  215. package/src/headers/26/node_worker.h +162 -0
  216. package/src/headers/26/path.h +36 -0
  217. package/src/headers/26/permission/boolean_permission.h +50 -0
  218. package/src/headers/26/permission/fs_permission.h +192 -0
  219. package/src/headers/26/permission/permission.h +155 -0
  220. package/src/headers/26/permission/permission_base.h +79 -0
  221. package/src/headers/26/pipe_wrap.h +80 -0
  222. package/src/headers/26/quic/application.h +279 -0
  223. package/src/headers/26/quic/arena.h +373 -0
  224. package/src/headers/26/quic/bindingdata.h +440 -0
  225. package/src/headers/26/quic/cid.h +127 -0
  226. package/src/headers/26/quic/data.h +358 -0
  227. package/src/headers/26/quic/defs.h +417 -0
  228. package/src/headers/26/quic/endpoint.h +519 -0
  229. package/src/headers/26/quic/guard.h +5 -0
  230. package/src/headers/26/quic/http3.h +25 -0
  231. package/src/headers/26/quic/packet.h +166 -0
  232. package/src/headers/26/quic/preferredaddress.h +72 -0
  233. package/src/headers/26/quic/session.h +770 -0
  234. package/src/headers/26/quic/session_manager.h +107 -0
  235. package/src/headers/26/quic/sessionticket.h +110 -0
  236. package/src/headers/26/quic/streams.h +518 -0
  237. package/src/headers/26/quic/tlscontext.h +353 -0
  238. package/src/headers/26/quic/tokens.h +263 -0
  239. package/src/headers/26/quic/transportparams.h +184 -0
  240. package/src/headers/26/req_wrap-inl.h +175 -0
  241. package/src/headers/26/req_wrap.h +79 -0
  242. package/src/headers/26/spawn_sync.h +243 -0
  243. package/src/headers/26/stream_base-inl.h +170 -0
  244. package/src/headers/26/stream_base.h +487 -0
  245. package/src/headers/26/stream_pipe.h +76 -0
  246. package/src/headers/26/stream_wrap.h +137 -0
  247. package/src/headers/26/string_bytes.h +114 -0
  248. package/src/headers/26/string_decoder-inl.h +31 -0
  249. package/src/headers/26/string_decoder.h +49 -0
  250. package/src/headers/26/tcp_wrap.h +138 -0
  251. package/src/headers/26/threadpoolwork-inl.h +70 -0
  252. package/src/headers/26/timer_wrap-inl.h +32 -0
  253. package/src/headers/26/timer_wrap.h +87 -0
  254. package/src/headers/26/timers.h +70 -0
  255. package/src/headers/26/tracing/agent.h +125 -0
  256. package/src/headers/26/tracing/agent_legacy.h +146 -0
  257. package/src/headers/26/tracing/agent_perfetto.h +145 -0
  258. package/src/headers/26/tracing/node_trace_buffer.h +91 -0
  259. package/src/headers/26/tracing/node_trace_writer.h +79 -0
  260. package/src/headers/26/tracing/trace_event.h +27 -0
  261. package/src/headers/26/tracing/trace_event_helper.h +40 -0
  262. package/src/headers/26/tracing/trace_event_legacy.h +1113 -0
  263. package/src/headers/26/tracing/trace_event_legacy_inl.h +711 -0
  264. package/src/headers/26/tracing/trace_event_perfetto.h +47 -0
  265. package/src/headers/26/tracing/traced_value.h +167 -0
  266. package/src/headers/26/tty_wrap.h +65 -0
  267. package/src/headers/26/udp_wrap.h +231 -0
  268. package/src/headers/26/undici_version.h +6 -0
  269. package/src/headers/26/util-inl.h +788 -0
  270. package/src/headers/26/util.h +1207 -0
  271. package/src/headers/26/v8-cpp-heap-external.h +56 -0
  272. package/src/headers/26/v8-external-memory-accounter.h +60 -0
  273. package/src/headers/26/v8-fast-api-calls.h +784 -0
  274. package/src/headers/26/v8-inspector-protocol.h +13 -0
  275. package/src/headers/26/v8-inspector.h +458 -0
  276. package/src/headers/26/v8-metrics.h +273 -0
  277. package/src/headers/26/v8-trace-categories.h +28 -0
  278. package/src/headers/26/v8-unwinder-state.h +30 -0
  279. package/src/headers/26/v8-util.h +549 -0
  280. package/src/headers/26/v8-value-serializer-version.h +24 -0
  281. package/src/headers/26/v8-version-string.h +38 -0
  282. package/src/headers/26/v8-wasm-trap-handler-posix.h +31 -0
  283. package/src/headers/26/v8-wasm-trap-handler-win.h +28 -0
  284. package/src/headers/26/zlib_version.h +6 -0
@@ -0,0 +1,92 @@
1
+ #ifndef SRC_CRYPTO_CRYPTO_HMAC_H_
2
+ #define SRC_CRYPTO_CRYPTO_HMAC_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "base_object.h"
7
+ #include "crypto/crypto_keys.h"
8
+ #include "crypto/crypto_sig.h"
9
+ #include "crypto/crypto_util.h"
10
+ #include "env.h"
11
+ #include "memory_tracker.h"
12
+ #include "v8.h"
13
+
14
+ namespace node {
15
+ namespace crypto {
16
+ class Hmac : public BaseObject {
17
+ public:
18
+ static void Initialize(Environment* env, v8::Local<v8::Object> target);
19
+ static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
20
+
21
+ void MemoryInfo(MemoryTracker* tracker) const override;
22
+ SET_MEMORY_INFO_NAME(Hmac)
23
+ SET_SELF_SIZE(Hmac)
24
+
25
+ protected:
26
+ void HmacInit(const char* hash_type, const char* key, int key_len);
27
+ bool HmacUpdate(const char* data, size_t len);
28
+
29
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
30
+ static void HmacInit(const v8::FunctionCallbackInfo<v8::Value>& args);
31
+ static void HmacUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
32
+ static void HmacDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
33
+
34
+ Hmac(Environment* env, v8::Local<v8::Object> wrap);
35
+
36
+ static void Sign(const v8::FunctionCallbackInfo<v8::Value>& args);
37
+
38
+ private:
39
+ ncrypto::HMACCtxPointer ctx_;
40
+ };
41
+
42
+ struct HmacConfig final : public MemoryRetainer {
43
+ SignConfiguration::Mode mode;
44
+ KeyObjectData key;
45
+ ByteSource data;
46
+ ByteSource signature;
47
+ ncrypto::Digest digest;
48
+
49
+ HmacConfig() = default;
50
+
51
+ explicit HmacConfig(HmacConfig&& other) noexcept;
52
+
53
+ HmacConfig& operator=(HmacConfig&& other) noexcept;
54
+
55
+ void MemoryInfo(MemoryTracker* tracker) const override;
56
+ SET_MEMORY_INFO_NAME(HmacConfig)
57
+ SET_SELF_SIZE(HmacConfig)
58
+ };
59
+
60
+ struct HmacTraits final {
61
+ using AdditionalParameters = HmacConfig;
62
+ static constexpr const char* JobName = "HmacJob";
63
+
64
+ // TODO(@jasnell): Sign request vs. Verify request
65
+
66
+ static constexpr AsyncWrap::ProviderType Provider =
67
+ AsyncWrap::PROVIDER_SIGNREQUEST;
68
+
69
+ static v8::Maybe<void> AdditionalConfig(
70
+ CryptoJobMode mode,
71
+ const v8::FunctionCallbackInfo<v8::Value>& args,
72
+ unsigned int offset,
73
+ HmacConfig* params);
74
+
75
+ static bool DeriveBits(Environment* env,
76
+ const HmacConfig& params,
77
+ ByteSource* out,
78
+ CryptoJobMode mode,
79
+ CryptoErrorStore* errors);
80
+
81
+ static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
82
+ const HmacConfig& params,
83
+ ByteSource* out);
84
+ };
85
+
86
+ using HmacJob = DeriveBitsJob<HmacTraits>;
87
+
88
+ } // namespace crypto
89
+ } // namespace node
90
+
91
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
92
+ #endif // SRC_CRYPTO_CRYPTO_HMAC_H_
@@ -0,0 +1,131 @@
1
+ #ifndef SRC_CRYPTO_CRYPTO_KEM_H_
2
+ #define SRC_CRYPTO_CRYPTO_KEM_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "base_object.h"
7
+ #include "crypto/crypto_keys.h"
8
+ #include "crypto/crypto_util.h"
9
+ #include "env.h"
10
+ #include "memory_tracker.h"
11
+ #include "node_external_reference.h"
12
+
13
+ #if OPENSSL_WITH_KEM
14
+
15
+ namespace node {
16
+ namespace crypto {
17
+
18
+ enum class KEMMode { Encapsulate, Decapsulate };
19
+
20
+ struct KEMConfiguration final : public MemoryRetainer {
21
+ KeyObjectData key;
22
+ ByteSource ciphertext;
23
+
24
+ KEMConfiguration() = default;
25
+ explicit KEMConfiguration(KEMConfiguration&& other) noexcept;
26
+ KEMConfiguration& operator=(KEMConfiguration&& other) noexcept;
27
+
28
+ void MemoryInfo(MemoryTracker* tracker) const override;
29
+ SET_MEMORY_INFO_NAME(KEMConfiguration)
30
+ SET_SELF_SIZE(KEMConfiguration)
31
+ };
32
+
33
+ struct KEMEncapsulateTraits final {
34
+ using AdditionalParameters = KEMConfiguration;
35
+ static constexpr const char* JobName = "KEMEncapsulateJob";
36
+
37
+ static constexpr AsyncWrap::ProviderType Provider =
38
+ AsyncWrap::PROVIDER_DERIVEBITSREQUEST;
39
+
40
+ static v8::Maybe<void> AdditionalConfig(
41
+ CryptoJobMode mode,
42
+ const v8::FunctionCallbackInfo<v8::Value>& args,
43
+ unsigned int offset,
44
+ KEMConfiguration* params);
45
+ };
46
+
47
+ class KEMEncapsulateJob final : public CryptoJob<KEMEncapsulateTraits> {
48
+ public:
49
+ using AdditionalParams = KEMEncapsulateTraits::AdditionalParameters;
50
+
51
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
52
+ static void Initialize(Environment* env, v8::Local<v8::Object> target);
53
+ static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
54
+
55
+ KEMEncapsulateJob(Environment* env,
56
+ v8::Local<v8::Object> object,
57
+ CryptoJobMode mode,
58
+ AdditionalParams&& params);
59
+
60
+ void DoThreadPoolWork() override;
61
+ v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
62
+ v8::Local<v8::Value>* result) override;
63
+
64
+ SET_SELF_SIZE(KEMEncapsulateJob)
65
+ void MemoryInfo(MemoryTracker* tracker) const override;
66
+
67
+ private:
68
+ struct Output {
69
+ ByteSource ciphertext;
70
+ ByteSource shared_key;
71
+ };
72
+
73
+ std::optional<Output> out_;
74
+ };
75
+
76
+ struct KEMDecapsulateTraits final {
77
+ using AdditionalParameters = KEMConfiguration;
78
+ static constexpr const char* JobName = "KEMDecapsulateJob";
79
+
80
+ static constexpr AsyncWrap::ProviderType Provider =
81
+ AsyncWrap::PROVIDER_DERIVEBITSREQUEST;
82
+
83
+ static v8::Maybe<void> AdditionalConfig(
84
+ CryptoJobMode mode,
85
+ const v8::FunctionCallbackInfo<v8::Value>& args,
86
+ unsigned int offset,
87
+ KEMConfiguration* params);
88
+
89
+ static bool DeriveBits(Environment* env,
90
+ const KEMConfiguration& params,
91
+ ByteSource* out,
92
+ CryptoJobMode mode,
93
+ CryptoErrorStore* errors);
94
+
95
+ static v8::MaybeLocal<v8::Value> EncodeOutput(Environment* env,
96
+ const KEMConfiguration& params,
97
+ ByteSource* out);
98
+ };
99
+
100
+ using KEMDecapsulateJob = DeriveBitsJob<KEMDecapsulateTraits>;
101
+
102
+ void InitializeKEM(Environment* env, v8::Local<v8::Object> target);
103
+ void RegisterKEMExternalReferences(ExternalReferenceRegistry* registry);
104
+
105
+ namespace KEM {
106
+ void Initialize(Environment* env, v8::Local<v8::Object> target);
107
+ void RegisterExternalReferences(ExternalReferenceRegistry* registry);
108
+ } // namespace KEM
109
+
110
+ } // namespace crypto
111
+ } // namespace node
112
+
113
+ #else
114
+
115
+ // Provide stub implementations when OpenSSL < 3.0
116
+ namespace node {
117
+ namespace crypto {
118
+ namespace KEM {
119
+ inline void Initialize(Environment* env, v8::Local<v8::Object> target) {
120
+ // No-op when OpenSSL < 3.0
121
+ }
122
+ inline void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
123
+ // No-op when OpenSSL < 3.0
124
+ }
125
+ } // namespace KEM
126
+ } // namespace crypto
127
+ } // namespace node
128
+
129
+ #endif
130
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
131
+ #endif // SRC_CRYPTO_CRYPTO_KEM_H_
@@ -0,0 +1,382 @@
1
+ #ifndef SRC_CRYPTO_CRYPTO_KEYGEN_H_
2
+ #define SRC_CRYPTO_CRYPTO_KEYGEN_H_
3
+
4
+ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
+
6
+ #include "async_wrap.h"
7
+ #include "base_object.h"
8
+ #include "crypto/crypto_keys.h"
9
+ #include "crypto/crypto_util.h"
10
+ #include "env.h"
11
+ #include "memory_tracker.h"
12
+ #include "v8.h"
13
+
14
+ namespace node::crypto {
15
+ namespace Keygen {
16
+ void Initialize(Environment* env, v8::Local<v8::Object> target);
17
+ void RegisterExternalReferences(ExternalReferenceRegistry* registry);
18
+ } // namespace Keygen
19
+
20
+ enum class KeyGenJobStatus {
21
+ OK,
22
+ FAILED
23
+ };
24
+
25
+ struct WebCryptoKeyGenConfig final {
26
+ v8::Global<v8::Value> algorithm;
27
+ uint32_t usages_mask = 0;
28
+ uint32_t public_usages_mask = 0;
29
+ uint32_t private_usages_mask = 0;
30
+ bool extractable = false;
31
+
32
+ WebCryptoKeyGenConfig() = default;
33
+ WebCryptoKeyGenConfig(WebCryptoKeyGenConfig&&) = default;
34
+ WebCryptoKeyGenConfig& operator=(WebCryptoKeyGenConfig&&) = default;
35
+ WebCryptoKeyGenConfig(const WebCryptoKeyGenConfig&) = delete;
36
+ WebCryptoKeyGenConfig& operator=(const WebCryptoKeyGenConfig&) = delete;
37
+ };
38
+
39
+ // A Base CryptoJob for generating secret keys or key pairs.
40
+ // The KeyGenTraits is largely responsible for the details of
41
+ // the implementation, while KeyGenJob handles the common
42
+ // mechanisms.
43
+ template <typename KeyGenTraits>
44
+ class KeyGenJob final : public CryptoJob<KeyGenTraits> {
45
+ public:
46
+ using AdditionalParams = typename KeyGenTraits::AdditionalParameters;
47
+
48
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args) {
49
+ Environment* env = Environment::GetCurrent(args);
50
+ CHECK(args.IsConstructCall());
51
+
52
+ CryptoJobMode mode = GetCryptoJobMode(args[0]);
53
+
54
+ unsigned int offset = 1;
55
+
56
+ AdditionalParams params;
57
+ if (KeyGenTraits::AdditionalConfig(mode, args, &offset, &params)
58
+ .IsNothing()) {
59
+ // The KeyGenTraits::AdditionalConfig is responsible for
60
+ // calling an appropriate THROW_CRYPTO_* variant reporting
61
+ // whatever error caused initialization to fail.
62
+ return;
63
+ }
64
+
65
+ WebCryptoKeyGenConfig config;
66
+ if (mode == kCryptoJobWebCrypto) {
67
+ if constexpr (KeyGenTraits::kWebCryptoKeyPair) {
68
+ CHECK(args[offset]->IsObject());
69
+ CHECK(args[offset + 1]->IsUint32());
70
+ CHECK(args[offset + 2]->IsUint32());
71
+ CHECK(args[offset + 3]->IsBoolean());
72
+ config.algorithm.Reset(env->isolate(), args[offset]);
73
+ config.public_usages_mask = args[offset + 1].As<v8::Uint32>()->Value();
74
+ config.private_usages_mask = args[offset + 2].As<v8::Uint32>()->Value();
75
+ config.extractable = args[offset + 3]->IsTrue();
76
+ } else {
77
+ CHECK(args[offset]->IsObject());
78
+ CHECK(args[offset + 1]->IsUint32());
79
+ CHECK(args[offset + 2]->IsBoolean());
80
+ config.algorithm.Reset(env->isolate(), args[offset]);
81
+ config.usages_mask = args[offset + 1].As<v8::Uint32>()->Value();
82
+ config.extractable = args[offset + 2]->IsTrue();
83
+ }
84
+ }
85
+
86
+ new KeyGenJob<KeyGenTraits>(
87
+ env, args.This(), mode, std::move(params), std::move(config));
88
+ }
89
+
90
+ static void Initialize(
91
+ Environment* env,
92
+ v8::Local<v8::Object> target) {
93
+ CryptoJob<KeyGenTraits>::Initialize(New, env, target);
94
+ }
95
+
96
+ static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
97
+ CryptoJob<KeyGenTraits>::RegisterExternalReferences(New, registry);
98
+ }
99
+
100
+ KeyGenJob(Environment* env,
101
+ v8::Local<v8::Object> object,
102
+ CryptoJobMode mode,
103
+ AdditionalParams&& params,
104
+ WebCryptoKeyGenConfig&& config)
105
+ : CryptoJob<KeyGenTraits>(
106
+ env, object, KeyGenTraits::Provider, mode, std::move(params)),
107
+ webcrypto_config_(std::move(config)) {}
108
+
109
+ void DoThreadPoolWork() override {
110
+ AdditionalParams* params = CryptoJob<KeyGenTraits>::params();
111
+
112
+ switch (KeyGenTraits::DoKeyGen(AsyncWrap::env(), params)) {
113
+ case KeyGenJobStatus::OK:
114
+ status_ = KeyGenJobStatus::OK;
115
+ // Success!
116
+ break;
117
+ case KeyGenJobStatus::FAILED: {
118
+ CryptoErrorStore* errors = CryptoJob<KeyGenTraits>::errors();
119
+ errors->Capture();
120
+ if (errors->Empty())
121
+ errors->Insert(NodeCryptoError::KEY_GENERATION_JOB_FAILED);
122
+ }
123
+ }
124
+ }
125
+
126
+ v8::Maybe<void> ToResult(v8::Local<v8::Value>* err,
127
+ v8::Local<v8::Value>* result) override {
128
+ Environment* env = AsyncWrap::env();
129
+ CryptoErrorStore* errors = CryptoJob<KeyGenTraits>::errors();
130
+ AdditionalParams* params = CryptoJob<KeyGenTraits>::params();
131
+
132
+ if (status_ == KeyGenJobStatus::OK) {
133
+ v8::TryCatch try_catch(env->isolate());
134
+ v8::MaybeLocal<v8::Value> encoded =
135
+ CryptoJob<KeyGenTraits>::mode() == kCryptoJobWebCrypto
136
+ ? EncodeWebCryptoKey(env, params)
137
+ : KeyGenTraits::EncodeKey(env, params);
138
+ if (encoded.ToLocal(result)) {
139
+ *err = Undefined(env->isolate());
140
+ } else {
141
+ CHECK(try_catch.HasCaught());
142
+ CHECK(try_catch.CanContinue());
143
+ *result = Undefined(env->isolate());
144
+ *err = try_catch.Exception();
145
+ }
146
+ } else {
147
+ if (errors->Empty()) errors->Capture();
148
+ CHECK(!errors->Empty());
149
+ *result = Undefined(env->isolate());
150
+ if (!errors->ToException(env).ToLocal(err)) {
151
+ return v8::Nothing<void>();
152
+ }
153
+ }
154
+ CHECK(!result->IsEmpty());
155
+ CHECK(!err->IsEmpty());
156
+ return v8::JustVoid();
157
+ }
158
+
159
+ SET_SELF_SIZE(KeyGenJob)
160
+
161
+ private:
162
+ v8::MaybeLocal<v8::Value> EncodeWebCryptoKey(Environment* env,
163
+ AdditionalParams* params) {
164
+ v8::Isolate* isolate = env->isolate();
165
+ v8::Local<v8::Value> algorithm =
166
+ v8::Local<v8::Value>::New(isolate, webcrypto_config_.algorithm);
167
+
168
+ if constexpr (KeyGenTraits::kWebCryptoKeyPair) {
169
+ v8::Local<v8::Value> public_key;
170
+ v8::Local<v8::Value> private_key;
171
+ if (!NativeCryptoKey::Create(env,
172
+ params->key.addRefWithType(kKeyTypePublic),
173
+ algorithm,
174
+ webcrypto_config_.public_usages_mask,
175
+ true)
176
+ .ToLocal(&public_key) ||
177
+ !NativeCryptoKey::Create(env,
178
+ params->key.addRefWithType(kKeyTypePrivate),
179
+ algorithm,
180
+ webcrypto_config_.private_usages_mask,
181
+ webcrypto_config_.extractable)
182
+ .ToLocal(&private_key)) {
183
+ return {};
184
+ }
185
+
186
+ v8::Local<v8::Object> ret = v8::Object::New(isolate);
187
+ if (!ret->DefineOwnProperty(env->context(),
188
+ OneByteString(isolate, "publicKey"),
189
+ public_key)
190
+ .FromMaybe(false) ||
191
+ !ret->DefineOwnProperty(env->context(),
192
+ OneByteString(isolate, "privateKey"),
193
+ private_key)
194
+ .FromMaybe(false)) {
195
+ return {};
196
+ }
197
+ return ret;
198
+ } else {
199
+ auto data = KeyObjectData::CreateSecret(std::move(params->out));
200
+ return NativeCryptoKey::Create(env,
201
+ data,
202
+ algorithm,
203
+ webcrypto_config_.usages_mask,
204
+ webcrypto_config_.extractable);
205
+ }
206
+ }
207
+
208
+ WebCryptoKeyGenConfig webcrypto_config_;
209
+ KeyGenJobStatus status_ = KeyGenJobStatus::FAILED;
210
+ };
211
+
212
+ // A Base KeyGenTraits for Key Pair generation algorithms.
213
+ template <typename KeyPairAlgorithmTraits>
214
+ struct KeyPairGenTraits final {
215
+ using AdditionalParameters =
216
+ typename KeyPairAlgorithmTraits::AdditionalParameters;
217
+ static constexpr bool kWebCryptoKeyPair = true;
218
+
219
+ static const AsyncWrap::ProviderType Provider =
220
+ AsyncWrap::PROVIDER_KEYPAIRGENREQUEST;
221
+ static constexpr const char* JobName = KeyPairAlgorithmTraits::JobName;
222
+
223
+ static v8::Maybe<void> AdditionalConfig(
224
+ CryptoJobMode mode,
225
+ const v8::FunctionCallbackInfo<v8::Value>& args,
226
+ unsigned int* offset,
227
+ AdditionalParameters* params) {
228
+ // Notice that offset is a pointer. Each of the AdditionalConfig,
229
+ // GetPublicKeyEncodingFromJs, and GetPrivateKeyEncodingFromJs
230
+ // functions will update the value of the offset as they successfully
231
+ // process input parameters. This allows each job to have a variable
232
+ // number of input parameters specific to each job type.
233
+ if (KeyPairAlgorithmTraits::AdditionalConfig(mode, args, offset, params)
234
+ .IsNothing()) {
235
+ return v8::Nothing<void>();
236
+ }
237
+
238
+ if (mode == kCryptoJobWebCrypto) return v8::JustVoid();
239
+
240
+ if (!KeyObjectData::GetPublicKeyEncodingFromJs(
241
+ args, offset, kKeyContextGenerate)
242
+ .To(&params->public_key_encoding) ||
243
+ !KeyObjectData::GetPrivateKeyEncodingFromJs(
244
+ args, offset, kKeyContextGenerate)
245
+ .To(&params->private_key_encoding)) {
246
+ return v8::Nothing<void>();
247
+ }
248
+
249
+ return v8::JustVoid();
250
+ }
251
+
252
+ static KeyGenJobStatus DoKeyGen(
253
+ Environment* env,
254
+ AdditionalParameters* params) {
255
+ ncrypto::EVPKeyCtxPointer ctx = KeyPairAlgorithmTraits::Setup(params);
256
+
257
+ if (!ctx)
258
+ return KeyGenJobStatus::FAILED;
259
+
260
+ // Generate the key
261
+ EVP_PKEY* pkey = nullptr;
262
+ if (!EVP_PKEY_keygen(ctx.get(), &pkey))
263
+ return KeyGenJobStatus::FAILED;
264
+
265
+ auto data = KeyObjectData::CreateAsymmetric(KeyType::kKeyTypePrivate,
266
+ ncrypto::EVPKeyPointer(pkey));
267
+ if (!data) [[unlikely]]
268
+ return KeyGenJobStatus::FAILED;
269
+ params->key = std::move(data);
270
+ return KeyGenJobStatus::OK;
271
+ }
272
+
273
+ static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
274
+ AdditionalParameters* params) {
275
+ v8::Local<v8::Value> keys[2];
276
+ if (!params->key.ToEncodedPublicKey(
277
+ env, params->public_key_encoding, &keys[0]) ||
278
+ !params->key.ToEncodedPrivateKey(
279
+ env, params->private_key_encoding, &keys[1])) {
280
+ return {};
281
+ }
282
+ return v8::Array::New(env->isolate(), keys, arraysize(keys));
283
+ }
284
+ };
285
+
286
+ struct SecretKeyGenConfig final : public MemoryRetainer {
287
+ size_t length = 0; // In bytes.
288
+ size_t length_bits = 0;
289
+ bool truncate_to_bit_length = false;
290
+ ByteSource out; // Placeholder for the generated key bytes.
291
+
292
+ void MemoryInfo(MemoryTracker* tracker) const override;
293
+ SET_MEMORY_INFO_NAME(SecretKeyGenConfig)
294
+ SET_SELF_SIZE(SecretKeyGenConfig)
295
+ };
296
+
297
+ struct SecretKeyGenTraits final {
298
+ using AdditionalParameters = SecretKeyGenConfig;
299
+ static constexpr bool kWebCryptoKeyPair = false;
300
+ static const AsyncWrap::ProviderType Provider =
301
+ AsyncWrap::PROVIDER_KEYGENREQUEST;
302
+ static constexpr const char* JobName = "SecretKeyGenJob";
303
+
304
+ static v8::Maybe<void> AdditionalConfig(
305
+ CryptoJobMode mode,
306
+ const v8::FunctionCallbackInfo<v8::Value>& args,
307
+ unsigned int* offset,
308
+ SecretKeyGenConfig* params);
309
+
310
+ static KeyGenJobStatus DoKeyGen(
311
+ Environment* env,
312
+ SecretKeyGenConfig* params);
313
+
314
+ static v8::MaybeLocal<v8::Value> EncodeKey(Environment* env,
315
+ SecretKeyGenConfig* params);
316
+ };
317
+
318
+ template <typename AlgorithmParams>
319
+ struct KeyPairGenConfig final : public MemoryRetainer {
320
+ ncrypto::EVPKeyPointer::PublicKeyEncodingConfig public_key_encoding;
321
+ ncrypto::EVPKeyPointer::PrivateKeyEncodingConfig private_key_encoding;
322
+ KeyObjectData key;
323
+ AlgorithmParams params;
324
+
325
+ KeyPairGenConfig() = default;
326
+
327
+ explicit KeyPairGenConfig(KeyPairGenConfig&& other) noexcept
328
+ : public_key_encoding(other.public_key_encoding),
329
+ private_key_encoding(
330
+ std::forward<ncrypto::EVPKeyPointer::PrivateKeyEncodingConfig>(
331
+ other.private_key_encoding)),
332
+ key(std::move(other.key)),
333
+ params(std::move(other.params)) {}
334
+
335
+ KeyPairGenConfig& operator=(KeyPairGenConfig&& other) noexcept {
336
+ if (&other == this) return *this;
337
+ this->~KeyPairGenConfig();
338
+ return *new (this) KeyPairGenConfig(std::move(other));
339
+ }
340
+
341
+ void MemoryInfo(MemoryTracker* tracker) const override {
342
+ tracker->TrackField("key", key);
343
+ if (private_key_encoding.passphrase.has_value()) {
344
+ auto& passphrase = private_key_encoding.passphrase.value();
345
+ tracker->TrackFieldWithSize("private_key_encoding.passphrase",
346
+ passphrase.size());
347
+ }
348
+ tracker->TrackField("params", params);
349
+ }
350
+
351
+ SET_MEMORY_INFO_NAME(KeyPairGenConfig)
352
+ SET_SELF_SIZE(KeyPairGenConfig)
353
+ };
354
+
355
+ struct NidKeyPairParams final : public MemoryRetainer {
356
+ int id;
357
+ SET_NO_MEMORY_INFO()
358
+ SET_MEMORY_INFO_NAME(NidKeyPairParams)
359
+ SET_SELF_SIZE(NidKeyPairParams)
360
+ };
361
+
362
+ using NidKeyPairGenConfig = KeyPairGenConfig<NidKeyPairParams>;
363
+
364
+ struct NidKeyPairGenTraits final {
365
+ using AdditionalParameters = NidKeyPairGenConfig;
366
+ static constexpr const char* JobName = "NidKeyPairGenJob";
367
+
368
+ static ncrypto::EVPKeyCtxPointer Setup(NidKeyPairGenConfig* params);
369
+
370
+ static v8::Maybe<void> AdditionalConfig(
371
+ CryptoJobMode mode,
372
+ const v8::FunctionCallbackInfo<v8::Value>& args,
373
+ unsigned int* offset,
374
+ NidKeyPairGenConfig* params);
375
+ };
376
+
377
+ using NidKeyPairGenJob = KeyGenJob<KeyPairGenTraits<NidKeyPairGenTraits>>;
378
+ using SecretKeyGenJob = KeyGenJob<SecretKeyGenTraits>;
379
+ } // namespace node::crypto
380
+
381
+ #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
382
+ #endif // SRC_CRYPTO_CRYPTO_KEYGEN_H_