• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   HMACCtxPointer ctx_;
40 };
41 
42 struct HmacConfig final : public MemoryRetainer {
43   CryptoJobMode job_mode;
44   SignConfiguration::Mode mode;
45   std::shared_ptr<KeyObjectData> key;
46   ByteSource data;
47   ByteSource signature;
48   const EVP_MD* digest;
49 
50   HmacConfig() = default;
51 
52   explicit HmacConfig(HmacConfig&& other) noexcept;
53 
54   HmacConfig& operator=(HmacConfig&& other) noexcept;
55 
56   void MemoryInfo(MemoryTracker* tracker) const override;
57   SET_MEMORY_INFO_NAME(HmacConfig)
58   SET_SELF_SIZE(HmacConfig)
59 };
60 
61 struct HmacTraits final {
62   using AdditionalParameters = HmacConfig;
63   static constexpr const char* JobName = "HmacJob";
64 
65 // TODO(@jasnell): Sign request vs. Verify request
66 
67   static constexpr AsyncWrap::ProviderType Provider =
68       AsyncWrap::PROVIDER_SIGNREQUEST;
69 
70   static v8::Maybe<bool> AdditionalConfig(
71       CryptoJobMode mode,
72       const v8::FunctionCallbackInfo<v8::Value>& args,
73       unsigned int offset,
74       HmacConfig* params);
75 
76   static bool DeriveBits(
77       Environment* env,
78       const HmacConfig& params,
79       ByteSource* out);
80 
81   static v8::Maybe<bool> EncodeOutput(
82       Environment* env,
83       const HmacConfig& params,
84       ByteSource* out,
85       v8::Local<v8::Value>* result);
86 };
87 
88 using HmacJob = DeriveBitsJob<HmacTraits>;
89 
90 }  // namespace crypto
91 }  // namespace node
92 
93 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
94 #endif  // SRC_CRYPTO_CRYPTO_HMAC_H_
95