@@ -2545,6 +2545,12 @@ int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) {
2545
2545
return 1 ;
2546
2546
}
2547
2547
2548
+ static bool IsSupportedAuthenticatedMode (int mode) {
2549
+ return mode == EVP_CIPH_CCM_MODE ||
2550
+ mode == EVP_CIPH_GCM_MODE ||
2551
+ mode == EVP_CIPH_OCB_MODE;
2552
+ }
2553
+
2548
2554
void CipherBase::Initialize (Environment* env, Local<Object> target) {
2549
2555
Local<FunctionTemplate> t = env->NewFunctionTemplate (New);
2550
2556
@@ -2571,6 +2577,43 @@ void CipherBase::New(const FunctionCallbackInfo<Value>& args) {
2571
2577
new CipherBase (env, args.This (), kind);
2572
2578
}
2573
2579
2580
+ void CipherBase::CommonInit (const char * cipher_type,
2581
+ const EVP_CIPHER* cipher,
2582
+ const unsigned char * key,
2583
+ int key_len,
2584
+ const unsigned char * iv,
2585
+ int iv_len,
2586
+ unsigned int auth_tag_len) {
2587
+ CHECK (!ctx_);
2588
+ ctx_.reset (EVP_CIPHER_CTX_new ());
2589
+
2590
+ const int mode = EVP_CIPHER_mode (cipher);
2591
+ if (mode == EVP_CIPH_WRAP_MODE)
2592
+ EVP_CIPHER_CTX_set_flags (ctx_.get (), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2593
+
2594
+ const bool encrypt = (kind_ == kCipher );
2595
+ if (1 != EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2596
+ nullptr , nullptr , encrypt)) {
2597
+ return ThrowCryptoError (env (), ERR_get_error (),
2598
+ " Failed to initialize cipher" );
2599
+ }
2600
+
2601
+ if (IsSupportedAuthenticatedMode (mode)) {
2602
+ CHECK_GE (iv_len, 0 );
2603
+ if (!InitAuthenticated (cipher_type, iv_len, auth_tag_len))
2604
+ return ;
2605
+ }
2606
+
2607
+ if (!EVP_CIPHER_CTX_set_key_length (ctx_.get (), key_len)) {
2608
+ ctx_.reset ();
2609
+ return env ()->ThrowError (" Invalid key length" );
2610
+ }
2611
+
2612
+ if (1 != EVP_CipherInit_ex (ctx_.get (), nullptr , nullptr , key, iv, encrypt)) {
2613
+ return ThrowCryptoError (env (), ERR_get_error (),
2614
+ " Failed to initialize cipher" );
2615
+ }
2616
+ }
2574
2617
2575
2618
void CipherBase::Init (const char * cipher_type,
2576
2619
const char * key_buf,
@@ -2586,7 +2629,6 @@ void CipherBase::Init(const char* cipher_type,
2586
2629
}
2587
2630
#endif // NODE_FIPS_MODE
2588
2631
2589
- CHECK (!ctx_);
2590
2632
const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
2591
2633
if (cipher == nullptr )
2592
2634
return env ()->ThrowError (" Unknown cipher" );
@@ -2604,45 +2646,19 @@ void CipherBase::Init(const char* cipher_type,
2604
2646
iv);
2605
2647
CHECK_NE (key_len, 0 );
2606
2648
2607
- ctx_.reset (EVP_CIPHER_CTX_new ());
2608
-
2609
2649
const int mode = EVP_CIPHER_mode (cipher);
2610
- if (mode == EVP_CIPH_WRAP_MODE)
2611
- EVP_CIPHER_CTX_set_flags (ctx_.get (), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2612
-
2613
- const bool encrypt = (kind_ == kCipher );
2614
- if (1 != EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2615
- nullptr , nullptr , encrypt)) {
2616
- return ThrowCryptoError (env (), ERR_get_error (),
2617
- " Failed to initialize cipher" );
2618
- }
2619
-
2620
- if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE ||
2621
- mode == EVP_CIPH_CCM_MODE)) {
2650
+ if (kind_ == kCipher && (mode == EVP_CIPH_CTR_MODE ||
2651
+ mode == EVP_CIPH_GCM_MODE ||
2652
+ mode == EVP_CIPH_CCM_MODE)) {
2622
2653
// Ignore the return value (i.e. possible exception) because we are
2623
2654
// not calling back into JS anyway.
2624
2655
ProcessEmitWarning (env (),
2625
2656
" Use Cipheriv for counter mode of %s" ,
2626
2657
cipher_type);
2627
2658
}
2628
2659
2629
- if (IsAuthenticatedMode ()) {
2630
- if (!InitAuthenticated (cipher_type, EVP_CIPHER_iv_length (cipher),
2631
- auth_tag_len))
2632
- return ;
2633
- }
2634
-
2635
- CHECK_EQ (1 , EVP_CIPHER_CTX_set_key_length (ctx_.get (), key_len));
2636
-
2637
- if (1 != EVP_CipherInit_ex (ctx_.get (),
2638
- nullptr ,
2639
- nullptr ,
2640
- reinterpret_cast <unsigned char *>(key),
2641
- reinterpret_cast <unsigned char *>(iv),
2642
- encrypt)) {
2643
- return ThrowCryptoError (env (), ERR_get_error (),
2644
- " Failed to initialize cipher" );
2645
- }
2660
+ CommonInit (cipher_type, cipher, key, key_len, iv,
2661
+ EVP_CIPHER_iv_length (cipher), auth_tag_len);
2646
2662
}
2647
2663
2648
2664
@@ -2669,16 +2685,10 @@ void CipherBase::Init(const FunctionCallbackInfo<Value>& args) {
2669
2685
cipher->Init (*cipher_type, key_buf, key_buf_len, auth_tag_len);
2670
2686
}
2671
2687
2672
- static bool IsSupportedAuthenticatedMode (int mode) {
2673
- return mode == EVP_CIPH_CCM_MODE ||
2674
- mode == EVP_CIPH_GCM_MODE ||
2675
- mode == EVP_CIPH_OCB_MODE;
2676
- }
2677
-
2678
2688
void CipherBase::InitIv (const char * cipher_type,
2679
- const char * key,
2689
+ const unsigned char * key,
2680
2690
int key_len,
2681
- const char * iv,
2691
+ const unsigned char * iv,
2682
2692
int iv_len,
2683
2693
unsigned int auth_tag_len) {
2684
2694
HandleScope scope (env ()->isolate ());
@@ -2706,38 +2716,7 @@ void CipherBase::InitIv(const char* cipher_type,
2706
2716
return env ()->ThrowError (" Invalid IV length" );
2707
2717
}
2708
2718
2709
- ctx_.reset (EVP_CIPHER_CTX_new ());
2710
-
2711
- if (mode == EVP_CIPH_WRAP_MODE)
2712
- EVP_CIPHER_CTX_set_flags (ctx_.get (), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2713
-
2714
- const bool encrypt = (kind_ == kCipher );
2715
- if (1 != EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2716
- nullptr , nullptr , encrypt)) {
2717
- return ThrowCryptoError (env (), ERR_get_error (),
2718
- " Failed to initialize cipher" );
2719
- }
2720
-
2721
- if (is_authenticated_mode) {
2722
- CHECK (has_iv);
2723
- if (!InitAuthenticated (cipher_type, iv_len, auth_tag_len))
2724
- return ;
2725
- }
2726
-
2727
- if (!EVP_CIPHER_CTX_set_key_length (ctx_.get (), key_len)) {
2728
- ctx_.reset ();
2729
- return env ()->ThrowError (" Invalid key length" );
2730
- }
2731
-
2732
- if (1 != EVP_CipherInit_ex (ctx_.get (),
2733
- nullptr ,
2734
- nullptr ,
2735
- reinterpret_cast <const unsigned char *>(key),
2736
- reinterpret_cast <const unsigned char *>(iv),
2737
- encrypt)) {
2738
- return ThrowCryptoError (env (), ERR_get_error (),
2739
- " Failed to initialize cipher" );
2740
- }
2719
+ CommonInit (cipher_type, cipher, key, key_len, iv, iv_len, auth_tag_len);
2741
2720
}
2742
2721
2743
2722
@@ -2750,14 +2729,15 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
2750
2729
2751
2730
const node::Utf8Value cipher_type (env->isolate (), args[0 ]);
2752
2731
ssize_t key_len = Buffer::Length (args[1 ]);
2753
- const char * key_buf = Buffer::Data (args[1 ]);
2732
+ const unsigned char * key_buf = reinterpret_cast <unsigned char *>(
2733
+ Buffer::Data (args[1 ]));
2754
2734
ssize_t iv_len;
2755
- const char * iv_buf;
2735
+ const unsigned char * iv_buf;
2756
2736
if (args[2 ]->IsNull ()) {
2757
2737
iv_buf = nullptr ;
2758
2738
iv_len = -1 ;
2759
2739
} else {
2760
- iv_buf = Buffer::Data (args[2 ]);
2740
+ iv_buf = reinterpret_cast < unsigned char *>( Buffer::Data (args[2 ]) );
2761
2741
iv_len = Buffer::Length (args[2 ]);
2762
2742
}
2763
2743
0 commit comments