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