#ifndef SRC_CRYPTO_CRYPTO_EC_H_ #define SRC_CRYPTO_CRYPTO_EC_H_ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #include "crypto/crypto_keys.h" #include "crypto/crypto_keygen.h" #include "crypto/crypto_util.h" #include "allocated_buffer.h" #include "async_wrap.h" #include "base_object.h" #include "env.h" #include "memory_tracker.h" #include "node_internals.h" #include "v8.h" namespace node { namespace crypto { int GetCurveFromName(const char* name); int GetOKPCurveFromName(const char* name); class ECDH final : public BaseObject { public: ~ECDH() override; static void Initialize(Environment* env, v8::Local target); static void RegisterExternalReferences(ExternalReferenceRegistry* registry); static ECPointPointer BufferToPoint(Environment* env, const EC_GROUP* group, v8::Local buf); void MemoryInfo(MemoryTracker* tracker) const override; SET_MEMORY_INFO_NAME(ECDH) SET_SELF_SIZE(ECDH) static void ConvertKey(const v8::FunctionCallbackInfo& args); static void GetCurves(const v8::FunctionCallbackInfo& args); protected: ECDH(Environment* env, v8::Local wrap, ECKeyPointer&& key); static void New(const v8::FunctionCallbackInfo& args); static void GenerateKeys(const v8::FunctionCallbackInfo& args); static void ComputeSecret(const v8::FunctionCallbackInfo& args); static void GetPrivateKey(const v8::FunctionCallbackInfo& args); static void SetPrivateKey(const v8::FunctionCallbackInfo& args); static void GetPublicKey(const v8::FunctionCallbackInfo& args); static void SetPublicKey(const v8::FunctionCallbackInfo& args); bool IsKeyPairValid(); bool IsKeyValidForCurve(const BignumPointer& private_key); ECKeyPointer key_; const EC_GROUP* group_; }; struct ECDHBitsConfig final : public MemoryRetainer { int id_; std::shared_ptr private_; std::shared_ptr public_; void MemoryInfo(MemoryTracker* tracker) const override; SET_MEMORY_INFO_NAME(ECDHBitsConfig) SET_SELF_SIZE(ECDHBitsConfig) }; struct ECDHBitsTraits final { using AdditionalParameters = ECDHBitsConfig; static constexpr const char* JobName = "ECDHBitsJob"; static constexpr AsyncWrap::ProviderType Provider = AsyncWrap::PROVIDER_DERIVEBITSREQUEST; static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int offset, ECDHBitsConfig* params); static bool DeriveBits( Environment* env, const ECDHBitsConfig& params, ByteSource* out_); static v8::Maybe EncodeOutput( Environment* env, const ECDHBitsConfig& params, ByteSource* out, v8::Local* result); }; using ECDHBitsJob = DeriveBitsJob; struct EcKeyPairParams final : public MemoryRetainer { int curve_nid; int param_encoding; SET_NO_MEMORY_INFO() SET_MEMORY_INFO_NAME(EcKeyPairParams) SET_SELF_SIZE(EcKeyPairParams) }; using EcKeyPairGenConfig = KeyPairGenConfig; struct EcKeyGenTraits final { using AdditionalParameters = EcKeyPairGenConfig; static constexpr const char* JobName = "EcKeyPairGenJob"; static EVPKeyCtxPointer Setup(EcKeyPairGenConfig* params); static v8::Maybe AdditionalConfig( CryptoJobMode mode, const v8::FunctionCallbackInfo& args, unsigned int* offset, EcKeyPairGenConfig* params); }; using ECKeyPairGenJob = KeyGenJob>; // There is currently no additional information that the // ECKeyExport needs to collect, but we need to provide // the base struct anyway. struct ECKeyExportConfig final : public MemoryRetainer { SET_NO_MEMORY_INFO() SET_MEMORY_INFO_NAME(ECKeyExportConfig) SET_SELF_SIZE(ECKeyExportConfig) }; struct ECKeyExportTraits final { static constexpr const char* JobName = "ECKeyExportJob"; using AdditionalParameters = ECKeyExportConfig; static v8::Maybe AdditionalConfig( const v8::FunctionCallbackInfo& args, unsigned int offset, ECKeyExportConfig* config); static WebCryptoKeyExportStatus DoExport( std::shared_ptr key_data, WebCryptoKeyFormat format, const ECKeyExportConfig& params, ByteSource* out); }; using ECKeyExportJob = KeyExportJob; v8::Maybe ExportJWKEcKey( Environment* env, std::shared_ptr key, v8::Local target); v8::Maybe ExportJWKEdKey( Environment* env, std::shared_ptr key, v8::Local target); std::shared_ptr ImportJWKEcKey( Environment* env, v8::Local jwk, const v8::FunctionCallbackInfo& args, unsigned int offset); v8::Maybe GetEcKeyDetail( Environment* env, std::shared_ptr key, v8::Local target); } // namespace crypto } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #endif // SRC_CRYPTO_CRYPTO_EC_H_