客户端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include <rte_crypto.h>
#include <rte_cryptodev.h>
#include <rte_malloc.h>
#define PORT 8080
#define BUFFER_SIZE 1024
#define CRYPTODEV_NAME "crypto_aesni_mb_pmd"
struct rte_crypto_sym_xform cipher_xform = {
.next = NULL,
.type = RTE_CRYPTO_SYM_XFORM_CIPHER,
.cipher = {
.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT,
.algo = RTE_CRYPTO_CIPHER_AES_CBC,
.key = {
.data = (uint8_t *)"0123456789abcdef", // 示例密钥
.length = 16,
},
.iv = {
.offset = 0,
},
},
};
void encrypt_data(uint8_t *data, uint16_t length, uint8_t *iv) {
struct rte_mempool *crypto_pool;
struct rte_crypto_op *crypto_op;
uint8_t cryptodev_id;
crypto_pool = rte_crypto_op_pool_create("CRYPTO_OP_POOL", RTE_CRYPTO_OP_POOL_CACHE_SIZE,
8192, 0, sizeof(struct rte_crypto_op), rte_socket_id());
if (crypto_pool == NULL) {
rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
}
cryptodev_id = rte_cryptodev_get_dev_id(CRYPTODEV_NAME);
if (cryptodev_id < 0) {
rte_exit(EXIT_FAILURE, "Cannot find crypto device\n");
}
if (rte_cryptodev_sym_session_init(cryptodev_id, &cipher_xform, crypto_pool) < 0) {
rte_exit(EXIT_FAILURE, "Cannot init crypto session\n");
}
if (rte_crypto_op_alloc(crypto_pool, RTE_CRYPTO_OP_TYPE_SYMMETRIC, &crypto_op) == NULL) {
rte_exit(EXIT_FAILURE, "Cannot allocate crypto op\n");
}
crypto_op->sym->m_src = rte_pktmbuf_alloc(crypto_pool);
crypto_op->sym->m_dst = crypto_op->sym->m_src;
rte_memcpy(rte_pktmbuf_mtod(crypto_op->sym->m_src, uint8_t *), data, length);
crypto_op->sym->cipher.data.offset = 0;
crypto_op->sym->cipher.data.length = length;
crypto_op->sym->cipher.iv.data = iv;
if (rte_cryptodev_enqueue_burst(cryptodev_id, 0, &crypto_op, 1) != 1) {
rte_exit(EXIT_FAILURE, "Cannot enqueue crypto op\n");
}
if (rte_cryptodev_dequeue_burst(cryptodev_id, 0, &crypto_op, 1) != 1) {
rte_exit(EXIT_FAILURE, "Cannot dequeue crypto op\n");
}
rte_memcpy(data, rte_pktmbuf_mtod(crypto_op->sym->m_dst, uint8_t *), length);
rte_pktmbuf_free(crypto_op->sym->m_src);
rte_crypto_op_free(crypto_op);
}
int main(int argc, char **argv) {
int sock = 0;
struct sockaddr_in serv_addr;
uint8_t buffer[BUFFER_SIZE] = "Hello, this is a test message.";
uint8_t iv[16] = "0123456789abcdef"; // 示例 IV
if (rte_eal_init(argc, argv) < 0) {
rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
encrypt_data(buffer, strlen((char *)buffer), iv);
printf("Encrypted data: %s\n", buffer);
send(sock, buffer, strlen((char *)buffer), 0);
printf("Encrypted data sent\n");
int valread = read(sock, buffer, BUFFER_SIZE);
printf("Decrypted data received from server: %s\n", buffer);
close(sock);
return 0;
}
服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include <rte_crypto.h>
#include <rte_cryptodev.h>
#include <rte_malloc.h>
#define PORT 8080
#define BUFFER_SIZE 1024
#define CRYPTODEV_NAME "crypto_aesni_mb_pmd"
struct rte_crypto_sym_xform cipher_xform = {
.next = NULL,
.type = RTE_CRYPTO_SYM_XFORM_CIPHER,
.cipher = {
.op = RTE_CRYPTO_CIPHER_OP_DECRYPT,
.algo = RTE_CRYPTO_CIPHER_AES_CBC,
.key = {
.data = (uint8_t *) "0123456789abcdef", // 示例密钥
.length = 16,
},
.iv = {
.offset = 0,
},
},
};
void decrypt_data(uint8_t *data, uint16_t length, uint8_t *iv) {
struct rte_mempool *crypto_pool;
struct rte_crypto_op *crypto_op;
uint8_t cryptodev_id;
crypto_pool = rte_crypto_op_pool_create("CRYPTO_OP_POOL", RTE_CRYPTO_OP_POOL_CACHE_SIZE,
8192, 0, sizeof(struct rte_crypto_op), rte_socket_id());
if (crypto_pool == NULL) {
rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
}
cryptodev_id = rte_cryptodev_get_dev_id(CRYPTODEV_NAME);
if (cryptodev_id < 0) {
rte_exit(EXIT_FAILURE, "Cannot find crypto device\n");
}
if (rte_cryptodev_sym_session_init(cryptodev_id, &cipher_xform, crypto_pool) < 0) {
rte_exit(EXIT_FAILURE, "Cannot init crypto session\n");
}
if (rte_crypto_op_alloc(crypto_pool, RTE_CRYPTO_OP_TYPE_SYMMETRIC, &crypto_op) == NULL) {
rte_exit(EXIT_FAILURE, "Cannot allocate crypto op\n");
}
crypto_op->sym->m_src = rte_pktmbuf_alloc(crypto_pool);
crypto_op->sym->m_dst = crypto_op->sym->m_src;
rte_memcpy(rte_pktmbuf_mtod(crypto_op->sym->m_src, uint8_t * ), data, length);
crypto_op->sym->cipher.data.offset = 0;
crypto_op->sym->cipher.data.length = length;
crypto_op->sym->cipher.iv.data = iv;
if (rte_cryptodev_enqueue_burst(cryptodev_id, 0, &crypto_op, 1) != 1) {
rte_exit(EXIT_FAILURE, "Cannot enqueue crypto op\n");
}
if (rte_cryptodev_dequeue_burst(cryptodev_id, 0, &crypto_op, 1) != 1) {
rte_exit(EXIT_FAILURE, "Cannot dequeue crypto op\n");
}
rte_memcpy(data, rte_pktmbuf_mtod(crypto_op->sym->m_dst, uint8_t * ), length);
rte_pktmbuf_free(crypto_op->sym->m_src);
rte_crypto_op_free(crypto_op);
}
int main(int argc, char **argv) {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
uint8_t buffer[BUFFER_SIZE] = {0};
uint8_t iv[16] = "0123456789abcdef"; // 示例 IV
if (rte_eal_init(argc, argv) < 0) {
rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
}
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
close(server_fd);
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *) &address, sizeof(address)) < 0) {
perror("bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
close(server_fd);
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *) &address, (socklen_t * ) & addrlen)) < 0) {
perror("accept");
close(server_fd);
exit(EXIT_FAILURE);
}
int valread = read(new_socket, buffer, BUFFER_SIZE);
printf("Received encrypted data: %s\n", buffer);
decrypt_data(buffer, valread, iv);
printf("Decrypted data: %s\n", buffer);
send(new_socket, buffer, valread, 0);
printf("Decrypted data sent back to client\n");
close(new_socket);
close(server_fd);
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。