gpt4 book ai didi

c - 对套接字使用 AES_ctr128_encrypt 时出现 SIGSEGV 错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:32:49 24 4
gpt4 key购买 nike

当我尝试使用 AES_ctr128_encrypt 加密文件数据时,出现 SIGBUS 错误。在这里,我使用 read() 函数每次以 32k block 将数据从文件读取到 bufptr[32k]。对于每个 block ,我只想使用 AES_ctr128_encrypt(bufptr+200, outdata, bytes_encrypt, &key, state.ivec, state.ecount, &state.num); 只加密所需的数据然后再次将其发送到服务器,然后再次解密并添加到缓冲区。有人可以帮我解决这个问题吗?提前致谢。

第一个客户端能够将文件大小发送到服务器,当它到达 AES_ctr128_encrypt() 时,我得到了段错误(核心已转储) gdb 说它是一个 SIGSEGV。

我的头文件

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <sys/time.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/modes.h>
#include <openssl/crypto.h>
#define FAIL -1
#define SEGMENT_SIZE 32768
#define j 10
#define OUT1 AES_BLOCK_SIZE
#define BYTES_ENCRYPT AES_BLOCK_SIZE

我的客户端程序

#include "headers.h"
#define FILE "/tmp/book"

struct ctr_state {
unsigned char ivec[AES_BLOCK_SIZE];
unsigned int num;
unsigned char ecount[AES_BLOCK_SIZE];
};

int init_ctr(struct ctr_state *state, const unsigned char iv[8])
{
/* aes_ctr128_encrypt requires 'num' and 'ecount' set to zero on the
* first call. */
state->num = 0;
memset(state->ecount, 0, 16);

/* Initialise counter in 'ivec' to 0 */
memset(state->ivec + 16, 0, 16);

/* Copy IV into 'ivec' */
memcpy(state->ivec, iv, 16);
}

int main(int argc, char* argv[])
{
size_t count;
AES_KEY key;
unsigned char ckey[] = "/home/nandan/aes.key";
unsigned char iv[AES_BLOCK_SIZE];
struct ctr_state state;

// validate proper usage
if (argc != 3)
{
fprintf(stderr, "Usage: %s <serverIP> <serverPort>\n", argv[0]);
exit(-1);
}

// store the command line arguments
char *server_ip = argv[1];
int server_port = htons(atoi(argv[2]));
// stores address of remote server to connect
struct sockaddr_in server_addr;
int fd, option;

fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
fprintf(stderr, "Error creating socket\n");
exit(-1);
}

memset(&server_addr, 0, sizeof(server_addr));

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(server_ip);
server_addr.sin_port = server_port;

if (connect(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{
fprintf(stderr, "Error connecting to server\n");
exit(-1);
}

struct stat stats;
stat("/tmp/book", &stats);
int size = stats.st_size;
// send size of file to the client
send(fd, &size, sizeof(int),0);
printf("Written \n");
unsigned char outdata[OUT1];
in = open (FILE, O_RDONLY);
to_read = size;
char *bufptr = malloc(SEGMENT_SIZE);
do
{
n = read (in, bufptr, SEGMENT_SIZE);
if(n < 0)
{
perror("Read Error:");
}
init_ctr(&state, iv);
AES_ctr128_encrypt(bufptr+200, outdata, BYTES_ENCRYPT, &key, state.ivec, state.ecount, &state.num);
memcpy(bufptr+200,outdata,BYTES_ENCRYPT);
sending = send(fd, bufptr, n,0);
to_read = to_read-n;
} while (to_read != 0);
close(fd);
}

我的服务器程序

#include "headers.h"
struct ctr_state {
unsigned char ivec[16];
unsigned int num;
unsigned char ecount[16];
};
int init_ctr(struct ctr_state *state, const unsigned char iv[8])
{
/* aes_ctr128_encrypt requires 'num' and 'ecount' set to zero on the
* first call. */
state->num = 0;
memset(state->ecount, 0, 16);

/* Initialise counter in 'ivec' to 0 */
memset(state->ivec + 8, 0, 8);

/* Copy IV into 'ivec' */
memcpy(state->ivec, iv, 8);
}

int main(int argc, char* argv[])
{

size_t count;
AES_KEY key;
unsigned char ckey[] = "/home/nandan/aes.key";
unsigned char iv[AES_BLOCK_SIZE];
struct ctr_state state;
// validate proper usage
if (argc != 4)
{
fprintf(stderr, "Usage %s <serverBindIP> <serverBindPort> <CredentialsFilePath>\n", argv[0]);
exit(-1);
}

int server;
char* ip = argv[1];
int port = htons(atoi(argv[2]));
char* passwd_file = argv[3];
struct sockaddr_in server_addr, client_addr;
clock_t start, end;
double cpu_time_used, timer = 0;
int server_fd, client_fd, result;
socklen_t length;

// Create an internet domain TCP socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1)
{
fprintf(stderr, "Unable to create socket\n");
exit(-1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);

// bind socket to an network interface
result = bind(server_fd, (struct sockaddr*) &server_addr, sizeof(server_addr));
if (result == -1)
{
fprintf(stderr, "Unable to bind socket\n");
exit(-1);
}

// mark the socket used for incoming requests
listen(server_fd, 5);

// accept an incoming connection
printf("Waiting for incoming connection\n");
length = sizeof(client_addr);
client_fd = accept(server_fd, (struct sockaddr*) &client_addr, &length);/* accept connection as usual */
if (client_fd == -1)
{
fprintf(stderr, "Unable to accept peer connection\n");
exit(-1);
}
printf("Connection: %s:%d\n",inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
int tmp = 0,i,size = 0; // read file size first
unsigned char outdata[OUT1];
recv(client_fd, &size, sizeof(int),0);
printf("%d \n", size);
int bytes_read = 0;
int to_read = size;
printf("%d \n", size);
FILE* out = fopen("/tmp/x1.pdf", "wb");
char *buffer = malloc(SEGMENT_SIZE);
do
{
bytes_read = recv(client_fd, buffer, sizeof(buffer),0);
init_ctr(&state, iv);
AES_ctr128_encrypt(buffer+200, outdata, BYTES_ENCRYPT, &key, state.ivec, state.ecount, &state.num);
memcpy(buffer+200, outdata, BYTES_ENCRYPT);
tmp = tmp + bytes_read;
// save content to disk
fwrite(buffer, 1, bytes_read, out);
} while (tmp != size);
close(client_fd); /* close socket */
return 0;
}

edit1:我不想加密所有数据,我只想加密每个 block 中的部分数据,看看它有多大用处。

最佳答案

您的代码因不同原因而出错:

  • 不要在函数内部定义结构
  • 不要使用局部函数(标准禁止),init_ctr
  • 在客户端中,您发送了 size 变量但尚未声明
  • 在您使用 sizeof(buffer) 代替 SEGMENT_SIZE 的服务器中
  • 为#defined 值使用大写标识符,bytes-encrypt
  • 定义你的 ivec 和 ecount 的长度为 AES_BLOCK_SIZE,不要使用 16
  • 直接发送outdata,不要复制回读入的buffer
  • 为什么只加密从 bufptr+200 开始的 16 个字节?
  • ...

我可以建议你以更干净的方式重写你的代码,这样会更容易找到你的问题吗?

关于c - 对套接字使用 AES_ctr128_encrypt 时出现 SIGSEGV 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34942106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com