@@ -87,6 +87,33 @@ void PNA_IpsecAccelerator::disable() {
8787 _is_enabled = false ;
8888}
8989
90+ void PNA_IpsecAccelerator::apply () {
91+
92+ if (!_is_enabled) {
93+ return ;
94+ }
95+
96+ MatchTable::Entry entry;
97+ MatchErrorCode rc = sad_table->get_entry (_sa_index, &entry);
98+ if (rc != MatchErrorCode::SUCCESS) {
99+ BMLOG_DEBUG (" Entry in SAD Table NOT Found" );
100+ return ;
101+ }
102+
103+ // action_data variable
104+ bool is_encrypt = entry.action_data .action_data [0 ].get <bool >();
105+ std::string key = entry.action_data .action_data [1 ].get_string ();
106+ std::string iv = entry.action_data .action_data [2 ].get_string ();
107+
108+ if (is_encrypt) {
109+ this ->encrypt (key, iv);
110+ } else {
111+ this ->decrypt (key);
112+ }
113+
114+ this ->reset (); // needed ???
115+ }
116+
90117void PNA_IpsecAccelerator::cipher (std::vector<unsigned char > input, std::vector<unsigned char > &output,
91118 unsigned char key[16 ], unsigned char iv[16 ], int encrypt) {
92119 EVP_CIPHER_CTX *ctx;
@@ -138,7 +165,14 @@ void PNA_IpsecAccelerator::decrypt(std::string string_key) {
138165 // check the ICV
139166 // compute HMAC
140167 // drop the packet if ICV and the computed hmac are not the same
141- unsigned char iv[block_size + 1 ] = {0 };
168+
169+ unsigned char *iv = (unsigned char *) malloc (block_size + 1 );
170+ if (iv == NULL ) {
171+ BMLOG_DEBUG (" IV: Memory allocation failed\n " );
172+ return ;
173+ }
174+ memset (iv, 0 , block_size + 1 );
175+
142176 unsigned char key[string_key.length ()];
143177 std::copy (string_key.begin (), string_key.end (), key);
144178
@@ -176,6 +210,8 @@ void PNA_IpsecAccelerator::decrypt(std::string string_key) {
176210 std::copy (decrypted.begin (),
177211 decrypted.end () - NEXT_HEADER_LENGTH - padding_length,
178212 payload_start + ETH_HEADER_LENGTH);
213+
214+ free (iv);
179215}
180216
181217void PNA_IpsecAccelerator::encrypt (std::string string_key, std::string string_iv) {
@@ -189,8 +225,21 @@ void PNA_IpsecAccelerator::encrypt(std::string string_key, std::string string_iv
189225
190226 unsigned int block_size = EVP_CIPHER_block_size (EVP_aes_128_cbc ());
191227
192- unsigned char iv[block_size + 1 ] = {0 };
193- unsigned char key[block_size + 1 ] = {0 };
228+ unsigned char *iv = (unsigned char *) malloc (block_size + 1 );
229+ if (iv == NULL ) {
230+ BMLOG_DEBUG (" IV: Memory allocation failed\n " );
231+ return ;
232+ }
233+ memset (iv, 0 , block_size + 1 );
234+
235+ unsigned char *key = (unsigned char *) malloc (block_size + 1 );
236+
237+ if (key == NULL ) {
238+ BMLOG_DEBUG (" Key: Memory allocation failed\n " );
239+ return ;
240+ }
241+ memset (key, 0 , block_size + 1 );
242+
194243 std::copy (string_iv.begin (), string_iv.end (), iv);
195244 std::copy (string_key.begin (), string_key.end (), key);
196245
@@ -263,6 +312,9 @@ void PNA_IpsecAccelerator::encrypt(std::string string_key, std::string string_iv
263312
264313 std::copy (esp.begin (), esp.end (), payload_start
265314 + ETH_HEADER_LENGTH + IP_HEADER_LENGTH);
315+
316+ free (iv);
317+ free (key);
266318}
267319
268320BM_REGISTER_EXTERN_W_NAME (ipsec_accelerator, PNA_IpsecAccelerator);
0 commit comments