From 60848c9009ccfadc1622ff518cd2b5d3e8c8c533 Mon Sep 17 00:00:00 2001 From: Rupesh Chiluka Date: Wed, 30 Oct 2024 14:14:19 +0530 Subject: [PATCH 1/3] Added Recirculation for PNA_NIC Signed-off-by: Rupesh Chiluka --- targets/pna_nic/pna_nic.cpp | 15 +++++++++++++++ targets/pna_nic/pna_nic.h | 1 + 2 files changed, 16 insertions(+) diff --git a/targets/pna_nic/pna_nic.cpp b/targets/pna_nic/pna_nic.cpp index 76aaa396..75e8c651 100644 --- a/targets/pna_nic/pna_nic.cpp +++ b/targets/pna_nic/pna_nic.cpp @@ -204,6 +204,21 @@ PnaNic::main_thread() { Deparser *deparser = this->get_deparser("main_deparser"); deparser->deparse(packet.get()); + + // recirculation + if ((port_t) packet->get_egress_port() == PNA_PORT_RECIRCULATE) { + BMLOG_DEBUG_PKT(*packet, "Recirculating packet"); + + phv->reset(); + phv->reset_header_stacks(); + phv->reset_metadata(); + + phv->get_field("pna_main_parser_input_metadata.recirculated") + .set(1); + input_buffer.push_front(std::move(packet)); + continue; + } + output_buffer.push_front(std::move(packet)); } } diff --git a/targets/pna_nic/pna_nic.h b/targets/pna_nic/pna_nic.h index c929f638..e1e17a74 100644 --- a/targets/pna_nic/pna_nic.h +++ b/targets/pna_nic/pna_nic.h @@ -79,6 +79,7 @@ class PnaNic : public Switch { private: static packet_id_t packet_id; + static constexpr port_t PNA_PORT_RECIRCULATE = 0xfffffffa; enum PktInstanceType { FROM_NET_PORT, From 9501c45151c35863aaeb8ad22086029687394627 Mon Sep 17 00:00:00 2001 From: Rupesh Chiluka Date: Wed, 30 Oct 2024 15:00:55 +0530 Subject: [PATCH 2/3] Added skeleton IPSec Accelerator extern Signed-off-by: Rupesh Chiluka --- targets/pna_nic/Makefile.am | 5 +- .../pna_nic/externs/pna_ipsec_accelerator.cpp | 61 +++++++++++++++++++ .../pna_nic/externs/pna_ipsec_accelerator.h | 58 ++++++++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 targets/pna_nic/externs/pna_ipsec_accelerator.cpp create mode 100644 targets/pna_nic/externs/pna_ipsec_accelerator.h diff --git a/targets/pna_nic/Makefile.am b/targets/pna_nic/Makefile.am index 2ae34b70..2ede5f32 100644 --- a/targets/pna_nic/Makefile.am +++ b/targets/pna_nic/Makefile.am @@ -15,14 +15,15 @@ externs/pna_counter.h externs/pna_counter.cpp \ externs/pna_meter.h externs/pna_meter.cpp \ externs/pna_random.h externs/pna_random.cpp \ externs/pna_internet_checksum.h externs/pna_internet_checksum.cpp \ -externs/pna_hash.h externs/pna_hash.cpp +externs/pna_hash.h externs/pna_hash.cpp \ +externs/pna_ipsec_accelerator.h externs/pna_ipsec_accelerator.cpp libpnanic_la_LIBADD = \ $(top_builddir)/src/bm_sim/libbmsim.la \ $(top_builddir)/src/bf_lpm_trie/libbflpmtrie.la \ $(top_builddir)/src/BMI/libbmi.la \ $(top_builddir)/third_party/jsoncpp/libjson.la \ --lboost_system $(THRIFT_LIB) -lboost_program_options -lboost_filesystem +-lboost_system $(THRIFT_LIB) -lboost_program_options -lboost_filesystem -lcrypto if COND_THRIFT diff --git a/targets/pna_nic/externs/pna_ipsec_accelerator.cpp b/targets/pna_nic/externs/pna_ipsec_accelerator.cpp new file mode 100644 index 00000000..af4cad55 --- /dev/null +++ b/targets/pna_nic/externs/pna_ipsec_accelerator.cpp @@ -0,0 +1,61 @@ +/* Copyright 2024 Marvell Technology, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Rupesh Chiluka (rchiluka@marvell.com) + * + */ + +#include "pna_ipsec_accelerator.h" + +namespace bm { + +namespace pna { + +void PNA_IpsecAccelerator::init() { + _is_enabled = false; +} + +void PNA_IpsecAccelerator::decrypt() { +} + +void PNA_IpsecAccelerator::encrypt() { +} + +void PNA_IpsecAccelerator::set_sa_index(const Data &sa_index) { + _sa_index = sa_index.get(); +} + +void PNA_IpsecAccelerator::enable() { + _is_enabled = true; +} + +void PNA_IpsecAccelerator::disable() { + _is_enabled = false; +} + +BM_REGISTER_EXTERN_W_NAME(ipsec_accelerator, PNA_IpsecAccelerator); +BM_REGISTER_EXTERN_W_NAME_METHOD(ipsec_accelerator, PNA_IpsecAccelerator, set_sa_index, const Data + &); +BM_REGISTER_EXTERN_W_NAME_METHOD(ipsec_accelerator, PNA_IpsecAccelerator, enable); +BM_REGISTER_EXTERN_W_NAME_METHOD(ipsec_accelerator, PNA_IpsecAccelerator, disable); + +} // namespace bm::pna + +} // namespace bm + +int import_ipsec_accelerator() { + return 0; +} diff --git a/targets/pna_nic/externs/pna_ipsec_accelerator.h b/targets/pna_nic/externs/pna_ipsec_accelerator.h new file mode 100644 index 00000000..063c71c2 --- /dev/null +++ b/targets/pna_nic/externs/pna_ipsec_accelerator.h @@ -0,0 +1,58 @@ +/* Copyright 2024 Marvell Technology, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Rupesh Chiluka (rchiluka@marvell.com) + * + */ + + +#ifndef PNA_NIC_PNA_IPSECACCELERATOR_H_ +#define PNA_NIC_PNA_IPSECACCELERATOR_H_ + +#include + +namespace bm { + +namespace pna { + +class PNA_IpsecAccelerator : public bm::ExternType { + public: + + BM_EXTERN_ATTRIBUTES { + } + + void init() override; + + void set_sa_index(const Data &sa_index); + + void enable(); + + void disable(); + + void decrypt(); + + void encrypt(); + + private: + uint32_t _sa_index; + bool _is_enabled = false; +}; + +} // namespace bm::pna + +} // namespace bm + +#endif // PNA_NIC_PNA_IPSECACCELERATOR_H_ From 7c12000dc31009da08a454a6361cd10f9ff31107 Mon Sep 17 00:00:00 2001 From: Rupesh Chiluka Date: Wed, 30 Oct 2024 16:54:45 +0530 Subject: [PATCH 3/3] Added Decryption Skeleton Signed-off-by: Rupesh Chiluka --- .../pna_nic/externs/pna_ipsec_accelerator.cpp | 81 ++++++++++++++++++- .../pna_nic/externs/pna_ipsec_accelerator.h | 29 ++++--- targets/pna_nic/pna_nic.cpp | 2 + 3 files changed, 97 insertions(+), 15 deletions(-) diff --git a/targets/pna_nic/externs/pna_ipsec_accelerator.cpp b/targets/pna_nic/externs/pna_ipsec_accelerator.cpp index af4cad55..73a51130 100644 --- a/targets/pna_nic/externs/pna_ipsec_accelerator.cpp +++ b/targets/pna_nic/externs/pna_ipsec_accelerator.cpp @@ -29,6 +29,83 @@ void PNA_IpsecAccelerator::init() { } void PNA_IpsecAccelerator::decrypt() { + std::vector raw_packet_data; + raw_packet_data.resize(get_packet().get_data_size(), '\0'); + std::copy(get_packet().data(), + get_packet().data() + get_packet().get_data_size(), + raw_packet_data.begin()); + + // check the ICV + // compute HMAC + // drop the packet if ICV and the computed hmac are not the same + // decrypt + // create IV + // std::vector IV; + unsigned char iv[17] = {0}; + unsigned char key[] = "sixteenbytes key"; + + std::copy_n(raw_packet_data.begin(), 16, iv); + + EVP_CIPHER_CTX *ctx; + + ctx = EVP_CIPHER_CTX_new(); + if (! EVP_DecryptInit_ex2(ctx, EVP_aes_128_cbc(), key, iv, NULL)) { + // Error + EVP_CIPHER_CTX_free(ctx); + return; + } + + EVP_CIPHER_CTX_set_padding(ctx, 0); + + + std::vector encrypted; + encrypted.resize(raw_packet_data.size() - 16, '\0'); + std::copy(raw_packet_data.begin() + 16, raw_packet_data.end(), encrypted.begin()); + + std::vector decrypted; + decrypted.resize(encrypted.size() + 16, '\0'); + + int outlen = 0; + int tmplen = 0; + + if (!EVP_DecryptUpdate(ctx, decrypted.data(), &outlen, encrypted.data(), + encrypted.size())) { + // Error + EVP_CIPHER_CTX_free(ctx); + std::cout << "Error in DecryptUpdate" << std::endl; + return; + } + + if (!EVP_DecryptFinal_ex(ctx, decrypted.data() + outlen, &tmplen)) { + // Error + EVP_CIPHER_CTX_free(ctx); + ERR_print_errors_fp(stderr); + std::cout << "Error in DecryptFinal" << std::endl; + return; + } + + outlen += tmplen; + decrypted.resize(outlen); // resize vector to actual output length + EVP_CIPHER_CTX_free(ctx); + + // padding length + int padding_length = *(decrypted.data() + encrypted.size() - 2); + + // payload + std::vector payload; + payload.resize(decrypted.size() - 2 - padding_length, '\0'); + std::copy(decrypted.begin(), decrypted.end() - 2 - padding_length, payload.begin()); + + // replace payload + // first, remove all the data + get_packet().remove(get_packet().get_data_size()); + // make room for the ciphertext and write the ciphertext in it + char *payload_start = get_packet().prepend( + (size_t) decrypted.size() - 2 - padding_length); + // 2 = padding length + next header + for (uint i =0; i < (size_t) decrypted.size() - 2 - padding_length; i++) { + payload_start[i] = payload[i]; + } } void PNA_IpsecAccelerator::encrypt() { @@ -40,6 +117,7 @@ void PNA_IpsecAccelerator::set_sa_index(const Data &sa_index) { void PNA_IpsecAccelerator::enable() { _is_enabled = true; + decrypt(); } void PNA_IpsecAccelerator::disable() { @@ -47,8 +125,7 @@ void PNA_IpsecAccelerator::disable() { } BM_REGISTER_EXTERN_W_NAME(ipsec_accelerator, PNA_IpsecAccelerator); -BM_REGISTER_EXTERN_W_NAME_METHOD(ipsec_accelerator, PNA_IpsecAccelerator, set_sa_index, const Data - &); +BM_REGISTER_EXTERN_W_NAME_METHOD(ipsec_accelerator, PNA_IpsecAccelerator, set_sa_index, const Data &); BM_REGISTER_EXTERN_W_NAME_METHOD(ipsec_accelerator, PNA_IpsecAccelerator, enable); BM_REGISTER_EXTERN_W_NAME_METHOD(ipsec_accelerator, PNA_IpsecAccelerator, disable); diff --git a/targets/pna_nic/externs/pna_ipsec_accelerator.h b/targets/pna_nic/externs/pna_ipsec_accelerator.h index 063c71c2..c5ffc7ae 100644 --- a/targets/pna_nic/externs/pna_ipsec_accelerator.h +++ b/targets/pna_nic/externs/pna_ipsec_accelerator.h @@ -23,32 +23,35 @@ #define PNA_NIC_PNA_IPSECACCELERATOR_H_ #include +#include +#include +#include namespace bm { namespace pna { class PNA_IpsecAccelerator : public bm::ExternType { - public: + public: - BM_EXTERN_ATTRIBUTES { - } + BM_EXTERN_ATTRIBUTES { + } - void init() override; - - void set_sa_index(const Data &sa_index); + void init() override; + + void set_sa_index(const Data &sa_index); - void enable(); + void enable(); - void disable(); + void disable(); - void decrypt(); + void decrypt(); - void encrypt(); + void encrypt(); - private: - uint32_t _sa_index; - bool _is_enabled = false; + private: + uint32_t _sa_index; + bool _is_enabled = false; }; } // namespace bm::pna diff --git a/targets/pna_nic/pna_nic.cpp b/targets/pna_nic/pna_nic.cpp index 75e8c651..82d05c0f 100644 --- a/targets/pna_nic/pna_nic.cpp +++ b/targets/pna_nic/pna_nic.cpp @@ -36,6 +36,7 @@ extern int import_meters(); extern int import_random(); extern int import_internet_checksum(); extern int import_hash(); +extern int import_ipsec_accelerator(); namespace bm { @@ -78,6 +79,7 @@ PnaNic::PnaNic(bool enable_swap) import_random(); import_internet_checksum(); import_hash(); + import_ipsec_accelerator(); } #define PACKET_LENGTH_REG_IDX 0