Skip to content

Commit

Permalink
Add AES
Browse files Browse the repository at this point in the history
  • Loading branch information
Chion82 committed Feb 1, 2017
1 parent 623a45c commit d41b1ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 5,8 @@ default:

build:
make clean
$(CC) -std=gnu99 trans_packet.c ikcp.c common.c client.c -o client -lev -O2
$(CC) -std=gnu99 trans_packet.c ikcp.c common.c server.c -o server -lev -O2 -D SERVER
$(CC) -std=gnu99 trans_packet.c ikcp.c common.c client.c -o client -lev -lcrypto -O2
$(CC) -std=gnu99 trans_packet.c ikcp.c common.c server.c -o server -lev -lcrypto -O2 -D SERVER

clean:
rm -rf server client
20 changes: 17 additions & 3 deletions trans_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 12,7 @@
#include <fcntl.h>
#include <byteswap.h>
#include <ev.h>
#include <openssl/aes.h>

#include "trans_packet.h"

Expand All @@ -29,12 30,14 @@ struct pseudo_header {
u_int16_t tcp_length;
};

char* aes_key = "it is a secrect!";
char* aes_ckey = "it is a secrect!";
char* aes_vec = "1234567890123456";

unsigned short csum(unsigned short *ptr,int nbytes);

void init_packet(struct packet_info* packetinfo) {
AES_set_encrypt_key(aes_ckey, 128, &aes_key);

packet_send_sd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
packet_recv_sd = socket(AF_PACKET , SOCK_DGRAM , htons(ETH_P_IP));
// packet_recv_sd = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
Expand Down Expand Up @@ -168,10 171,16 @@ void check_packet_recv(struct packet_info* packetinfo) {
(packetinfo->state).ack = __bswap_32(tcph->seq) payloadlen;
}

char* data_payload_buf = buffer iphdrlen tcph->doff*4 4;
char* payload = buffer iphdrlen tcph->doff*4;
char* data_payload_buf = payload 4;
int data_payload_len = payloadlen - 4;

unsigned short data_payload_checksum = *((unsigned short*)(buffer iphdrlen tcph->doff*4));
int aes_num = 0;
char aes_tmp_vec[16];
memcpy(aes_tmp_vec, aes_vec, 16);
AES_cfb128_encrypt(payload, payload, payloadlen, &aes_key, aes_tmp_vec, &aes_num, AES_DECRYPT);

unsigned short data_payload_checksum = *((unsigned short*)payload);

if (csum((unsigned short*)data_payload_buf, data_payload_len) != data_payload_checksum) {
LOG("[trans_packet]Data checksum verification failed. Dropping.");
Expand Down Expand Up @@ -201,6 210,11 @@ int send_packet(struct packet_info* packetinfo, char* source_payload, int source
memset(payload 2, 0x00, 2); // 2 reserved bytes
memcpy(payload 4, source_payload, source_payloadlen);
payloadlen = source_payloadlen 4;

int aes_num = 0;
char aes_tmp_vec[16];
memcpy(aes_tmp_vec, aes_vec, 16);
AES_cfb128_encrypt(payload, payload, payloadlen, &aes_key, aes_tmp_vec, &aes_num, AES_ENCRYPT);
}

//zero out the packet buffer
Expand Down
5 changes: 4 additions & 1 deletion trans_packet.h
Original file line number Diff line number Diff line change
@@ -1,3 1,5 @@
#include <openssl/aes.h>

#define MTU 1440

#define FIRST_SYN UINT_MAX - 2
Expand All @@ -23,8 25,9 @@ struct packet_info {
int packet_send_sd;
int packet_recv_sd;

char* aes_key;
char* aes_ckey;
char* aes_vec;
AES_KEY aes_key;

void init_packet(struct packet_info* packetinfo);
int send_packet(struct packet_info* packetinfo, char* source_payload, int payloadlen, unsigned int flag);
Expand Down

0 comments on commit d41b1ac

Please sign in to comment.