2323
2424namespace duckdb {
2525
26- AESStateSSL::AESStateSSL (EncryptionTypes::CipherType cipher_p, const std::string *key ) : EncryptionState(cipher_p), context(EVP_CIPHER_CTX_new()), cipher(cipher_p ) {
26+ AESStateSSL::AESStateSSL (EncryptionTypes::CipherType cipher_p, idx_t key_len ) : EncryptionState(cipher_p, key_len ), context(EVP_CIPHER_CTX_new()) {
2727 if (!(context)) {
28- throw InternalException (" AES GCM failed with initializing context" );
28+ throw InternalException (" OpenSSL AES failed with initializing context" );
2929 }
3030}
3131
@@ -46,7 +46,7 @@ const EVP_CIPHER *AESStateSSL::GetCipher(idx_t key_len) {
4646 case 32 :
4747 return EVP_aes_256_gcm ();
4848 default :
49- throw InternalException (" Invalid AES key length" );
49+ throw InternalException (" Invalid AES key length for GCM " );
5050 }
5151 }
5252 case EncryptionTypes::CTR: {
@@ -58,7 +58,7 @@ const EVP_CIPHER *AESStateSSL::GetCipher(idx_t key_len) {
5858 case 32 :
5959 return EVP_aes_256_ctr ();
6060 default :
61- throw InternalException (" Invalid AES key length" );
61+ throw InternalException (" Invalid AES key length for CTR " );
6262 }
6363 }
6464 case EncryptionTypes::CBC: {
@@ -70,11 +70,11 @@ const EVP_CIPHER *AESStateSSL::GetCipher(idx_t key_len) {
7070 case 32 :
7171 return EVP_aes_256_cbc ();
7272 default :
73- throw InternalException (" Invalid AES key length" );
73+ throw InternalException (" Invalid AES key length for CBC " );
7474 }
7575 }
7676 default :
77- throw duckdb:: InternalException (" Invalid Encryption/Decryption Cipher: %d" , static_cast <int >(cipher));
77+ throw InternalException (" Invalid Encryption/Decryption Cipher: %d" , static_cast <int >(cipher));
7878 }
7979}
8080
@@ -83,11 +83,21 @@ void AESStateSSL::GenerateRandomData(data_ptr_t data, idx_t len) {
8383 RAND_bytes (data, len);
8484}
8585
86- void AESStateSSL::InitializeEncryption (const_data_ptr_t iv, idx_t iv_len, const_data_ptr_t key, idx_t key_len , const_data_ptr_t aad, idx_t aad_len) {
86+ void AESStateSSL::InitializeEncryption (const_data_ptr_t iv, idx_t iv_len, const_data_ptr_t key, idx_t key_len_p , const_data_ptr_t aad, idx_t aad_len) {
8787 mode = EncryptionTypes::ENCRYPT;
8888
89- if (1 != EVP_EncryptInit_ex (context, GetCipher (key_len), NULL , key, iv)) {
90- throw InternalException (" EncryptInit failed" );
89+ if (key_len_p != key_len) {
90+ throw InternalException (" Invalid encryption key length, expected %llu, got %llu" , key_len, key_len_p);
91+ }
92+ if (1 != EVP_EncryptInit_ex (context, GetCipher (key_len), NULL , NULL , NULL )) {
93+ throw InternalException (" EncryptInit failed (attempt 1)" );
94+ }
95+ if (1 != EVP_CIPHER_CTX_ctrl (context, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL )) {
96+ throw InternalException (" EVP_CIPHER_CTX_ctrl failed (EVP_CTRL_GCM_SET_IVLEN)" );
97+ }
98+
99+ if (1 != EVP_EncryptInit_ex (context, NULL , NULL , key, iv)) {
100+ throw InternalException (" EncryptInit failed (attempt 2)" );
91101 }
92102
93103 int len;
@@ -98,13 +108,23 @@ void AESStateSSL::InitializeEncryption(const_data_ptr_t iv, idx_t iv_len, const_
98108 }
99109}
100110
101- void AESStateSSL::InitializeDecryption (const_data_ptr_t iv, idx_t iv_len, const_data_ptr_t key, idx_t key_len , const_data_ptr_t aad, idx_t aad_len) {
111+ void AESStateSSL::InitializeDecryption (const_data_ptr_t iv, idx_t iv_len, const_data_ptr_t key, idx_t key_len_p , const_data_ptr_t aad, idx_t aad_len) {
102112 mode = EncryptionTypes::DECRYPT;
103-
104- if (1 != EVP_DecryptInit_ex (context, GetCipher (key_len), NULL , key, iv)) {
105- throw InternalException (" DecryptInit failed" );
113+ if (key_len_p != key_len) {
114+ throw InternalException (" Invalid encryption key length, expected %llu, got %llu" , key_len, key_len_p);
115+ }
116+ if (1 != EVP_DecryptInit_ex (context, GetCipher (key_len), NULL , NULL , NULL )) {
117+ throw InternalException (" EVP_DecryptInit_ex failed to set cipher" );
118+ }
119+ // we use a bigger IV for GCM
120+ if (cipher == EncryptionTypes::GCM) {
121+ if (1 != EVP_CIPHER_CTX_ctrl (context, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL )) {
122+ throw InternalException (" EVP_CIPHER_CTX_ctrl failed to set GCM iv len" );
123+ }
124+ }
125+ if (1 != EVP_DecryptInit_ex (context, NULL , NULL , key, iv)) {
126+ throw InternalException (" EVP_DecryptInit_ex failed to set iv/key" );
106127 }
107-
108128 int len;
109129 if (aad_len > 0 ){
110130 if (!EVP_DecryptUpdate (context, NULL , &len, aad, aad_len)) {
@@ -114,7 +134,6 @@ void AESStateSSL::InitializeDecryption(const_data_ptr_t iv, idx_t iv_len, const_
114134}
115135
116136size_t AESStateSSL::Process (const_data_ptr_t in, idx_t in_len, data_ptr_t out, idx_t out_len) {
117-
118137 switch (mode) {
119138 case EncryptionTypes::ENCRYPT:
120139 if (1 != EVP_EncryptUpdate (context, data_ptr_cast (out), reinterpret_cast <int *>(&out_len),
@@ -135,7 +154,6 @@ size_t AESStateSSL::Process(const_data_ptr_t in, idx_t in_len, data_ptr_t out, i
135154 if (out_len != in_len) {
136155 throw InternalException (" AES GCM failed, in- and output lengths differ" );
137156 }
138-
139157 return out_len;
140158}
141159
@@ -189,15 +207,13 @@ size_t AESStateSSL::Finalize(data_ptr_t out, idx_t out_len, data_ptr_t tag, idx_
189207 if (1 != EVP_EncryptFinal_ex (context, data_ptr_cast (out) + out_len, reinterpret_cast <int *>(&out_len))) {
190208 throw InternalException (" EncryptFinal failed" );
191209 }
192-
193210 return text_len += out_len;
194211 }
195212
196213 case EncryptionTypes::DECRYPT: {
197214 // EVP_DecryptFinal() will return an error code if final block is not correctly formatted.
198215 int ret = EVP_DecryptFinal_ex (context, data_ptr_cast (out) + out_len, reinterpret_cast <int *>(&out_len));
199216 text_len += out_len;
200-
201217 if (ret > 0 ) {
202218 // success
203219 return text_len;
0 commit comments