• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_CRYPTO_CRYPTO_AES_H_
2 #define SRC_CRYPTO_CRYPTO_AES_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "crypto/crypto_cipher.h"
7 #include "crypto/crypto_keys.h"
8 #include "crypto/crypto_util.h"
9 #include "env.h"
10 #include "v8.h"
11 
12 namespace node {
13 namespace crypto {
14 constexpr size_t kAesBlockSize = 16;
15 constexpr unsigned kNoAuthTagLength = static_cast<unsigned>(-1);
16 constexpr const char* kDefaultWrapIV = "\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6";
17 
18 #define VARIANTS(V)                                                           \
19   V(CTR_128, AES_CTR_Cipher)                                                  \
20   V(CTR_192, AES_CTR_Cipher)                                                  \
21   V(CTR_256, AES_CTR_Cipher)                                                  \
22   V(CBC_128, AES_Cipher)                                                      \
23   V(CBC_192, AES_Cipher)                                                      \
24   V(CBC_256, AES_Cipher)                                                      \
25   V(GCM_128, AES_Cipher)                                                      \
26   V(GCM_192, AES_Cipher)                                                      \
27   V(GCM_256, AES_Cipher)                                                      \
28   V(KW_128, AES_Cipher)                                                       \
29   V(KW_192, AES_Cipher)                                                       \
30   V(KW_256, AES_Cipher)
31 
32 enum AESKeyVariant {
33 #define V(name, _) kKeyVariantAES_ ## name,
34   VARIANTS(V)
35 #undef V
36 };
37 
38 struct AESCipherConfig final : public MemoryRetainer {
39   CryptoJobMode mode;
40   AESKeyVariant variant;
41   const EVP_CIPHER* cipher;
42   size_t length;
43   ByteSource iv;  // Used for both iv or counter
44   ByteSource additional_data;
45   ByteSource tag;  // Used only for authenticated modes (GCM)
46 
47   AESCipherConfig() = default;
48 
49   AESCipherConfig(AESCipherConfig&& other) noexcept;
50 
51   AESCipherConfig& operator=(AESCipherConfig&& other) noexcept;
52 
53   void MemoryInfo(MemoryTracker* tracker) const override;
54   SET_MEMORY_INFO_NAME(AESCipherConfig)
55   SET_SELF_SIZE(AESCipherConfig)
56 };
57 
58 struct AESCipherTraits final {
59   static constexpr const char* JobName = "AESCipherJob";
60 
61   using AdditionalParameters = AESCipherConfig;
62 
63   static v8::Maybe<bool> AdditionalConfig(
64       CryptoJobMode mode,
65       const v8::FunctionCallbackInfo<v8::Value>& args,
66       unsigned int offset,
67       WebCryptoCipherMode cipher_mode,
68       AESCipherConfig* config);
69 
70   static WebCryptoCipherStatus DoCipher(
71       Environment* env,
72       std::shared_ptr<KeyObjectData> key_data,
73       WebCryptoCipherMode cipher_mode,
74       const AESCipherConfig& params,
75       const ByteSource& in,
76       ByteSource* out);
77 };
78 
79 using AESCryptoJob = CipherJob<AESCipherTraits>;
80 
81 namespace AES {
82 void Initialize(Environment* env, v8::Local<v8::Object> target);
83 void RegisterExternalReferences(ExternalReferenceRegistry* registry);
84 }  // namespace AES
85 }  // namespace crypto
86 }  // namespace node
87 
88 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
89 #endif  // SRC_CRYPTO_CRYPTO_AES_H_
90