mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-01-19 06:19:31 +08:00
a bunch of commit together,i lost some commit log while upgrading git
This commit is contained in:
parent
c6371c0ef5
commit
93c0d789c4
166
encrypt.cpp
166
encrypt.cpp
@ -12,13 +12,11 @@
|
||||
|
||||
static int8_t zero_iv[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0};//this prog use zero iv,you should make sure first block of data contains a random/nonce data
|
||||
|
||||
static const int disable_all=0;
|
||||
extern const int max_data_len;
|
||||
extern const int buf_len;
|
||||
|
||||
static const int disable_aes=0;
|
||||
|
||||
|
||||
int auth_mode=auth_none;
|
||||
int cipher_mode=cipher_none;
|
||||
auth_mode_t auth_mode=auth_sum;
|
||||
cipher_mode_t cipher_mode=cipher_xor;
|
||||
|
||||
|
||||
unsigned int crc32h(unsigned char *message,int len) {
|
||||
@ -43,6 +41,16 @@ unsigned int crc32h(unsigned char *message,int len) {
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
void sum(const unsigned char *data,int len,unsigned char* res) {
|
||||
memset(res,0,sizeof(int));
|
||||
for(int i=0,j=0;i<len;i++,j++)
|
||||
{
|
||||
if(j==4) j=0;
|
||||
res[j]+=data[i];
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
int auth_md5_cal(const char *data,char * output,int &len)
|
||||
{
|
||||
memcpy(output,data,len);//TODO inefficient code
|
||||
@ -51,6 +59,35 @@ int auth_md5_cal(const char *data,char * output,int &len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int auth_crc32_cal(const char *data,char * output,int &len)
|
||||
{
|
||||
memcpy(output,data,len);//TODO inefficient code
|
||||
unsigned int ret=crc32h((unsigned char *)output,len);
|
||||
unsigned int ret_n=htonl(ret);
|
||||
memcpy(output+len,&ret_n,sizeof(unsigned int));
|
||||
len+=sizeof(unsigned int);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int auth_sum_cal(const char *data,char * output,int &len)
|
||||
{
|
||||
//char res[4];
|
||||
memcpy(output,data,len);//TODO inefficient code
|
||||
sum((unsigned char *)output,len,(unsigned char *)(output+len));
|
||||
len+=4;
|
||||
return 0;
|
||||
}
|
||||
int auth_sum_verify(const char *data,int &len)
|
||||
{
|
||||
unsigned char res[4];
|
||||
len-=4;
|
||||
sum((unsigned char *)data,len,res);
|
||||
if(memcmp(res,data+len,sizeof(int))!=0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int auth_none_cal(const char *data,char * output,int &len)
|
||||
{
|
||||
memcpy(output,data,len);
|
||||
@ -60,7 +97,7 @@ int auth_md5_verify(const char *data,int &len)
|
||||
{
|
||||
if(len<16)
|
||||
{
|
||||
log(log_trace,"auth_md5_verify len<16\n");
|
||||
mylog(log_trace,"auth_md5_verify len<16\n");
|
||||
return -1;
|
||||
}
|
||||
char md5_res[16];
|
||||
@ -69,7 +106,7 @@ int auth_md5_verify(const char *data,int &len)
|
||||
|
||||
if(memcmp(md5_res,data+len-16,16)!=0)
|
||||
{
|
||||
log(log_trace,"auth_md5_verify md5 check failed\n");
|
||||
mylog(log_trace,"auth_md5_verify md5 check failed\n");
|
||||
return -2;
|
||||
}
|
||||
len-=16;
|
||||
@ -79,9 +116,33 @@ int auth_none_verify(const char *data,int &len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cipher_xor_encrypt(const char * data, char *output,int &len, char *key) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < len; i++, j++) {
|
||||
if(j==16) j=0;
|
||||
output[i] = data[i]^key[j];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int cipher_xor_decrypt(const char * data, char *output,int &len, char *key) {
|
||||
int i, j;
|
||||
//char tmp[buf_len];
|
||||
//len=len/16*16+1;
|
||||
//AES128_CBC_decrypt_buffer((uint8_t *)tmp, (uint8_t *)input, len, (uint8_t *)key, (uint8_t *)iv);
|
||||
//for(i=0;i<len;i++)
|
||||
//input[i]=tmp[i];
|
||||
for (i = 0, j = 0; i < len; i++, j++) {
|
||||
if(j==16) j=0;
|
||||
output[i] = data[i]^key[j];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int cipher_aes128cbc_encrypt(const char *data,char *output,int &len,char * key)
|
||||
{
|
||||
char buf[65535+100];
|
||||
char buf[buf_len];
|
||||
memcpy(buf,data,len);//TODO inefficient code
|
||||
|
||||
int ori_len=len;
|
||||
@ -90,7 +151,7 @@ int cipher_aes128cbc_encrypt(const char *data,char *output,int &len,char * key)
|
||||
{
|
||||
len= (len/16)*16+16;
|
||||
}
|
||||
if(len>65535) return -1;
|
||||
if(len>max_data_len) return -1;
|
||||
|
||||
buf[len-2]= (unsigned char)( (uint16_t(ori_len))>>8);
|
||||
buf[len-1]=(unsigned char)( ((uint16_t(ori_len))<<8)>>8) ;
|
||||
@ -98,6 +159,24 @@ int cipher_aes128cbc_encrypt(const char *data,char *output,int &len,char * key)
|
||||
AES_CBC_encrypt_buffer((unsigned char *)output,(unsigned char *)buf,len,(unsigned char *)key,(unsigned char *)zero_iv);
|
||||
return 0;
|
||||
}
|
||||
int auth_crc32_verify(const char *data,int &len)
|
||||
{
|
||||
if(len<(sizeof(unsigned int)))
|
||||
{
|
||||
mylog(log_debug,"auth_crc32_verify len<16\n");
|
||||
return -1;
|
||||
}
|
||||
unsigned int ret=crc32h((unsigned char *)data,len-sizeof(unsigned int));
|
||||
unsigned int ret_n=htonl(ret);
|
||||
|
||||
if(memcmp(data+len-sizeof(unsigned int),&ret_n,sizeof(unsigned int))!=0)
|
||||
{
|
||||
mylog(log_debug,"auth_crc32_verify memcmp fail\n");
|
||||
return -1;
|
||||
}
|
||||
len-=sizeof(unsigned int);
|
||||
return 0;
|
||||
}
|
||||
int cipher_none_encrypt(const char *data,char *output,int &len,char * key)
|
||||
{
|
||||
memcpy(output,data,len);
|
||||
@ -105,8 +184,8 @@ int cipher_none_encrypt(const char *data,char *output,int &len,char * key)
|
||||
}
|
||||
int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key)
|
||||
{
|
||||
if(len%16 !=0) {log(log_trace,"len%16!=0");return -1;}
|
||||
if(len<2) {log(log_trace,"len <2 ");return -1;}return -1;
|
||||
if(len%16 !=0) {mylog(log_trace,"len%16!=0");return -1;}
|
||||
if(len<2) {mylog(log_trace,"len <2 ");return -1;}return -1;
|
||||
AES_CBC_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv);
|
||||
len=((unsigned char)output[len-2])*256u+((unsigned char)output[len-1]);
|
||||
return 0;
|
||||
@ -120,59 +199,77 @@ int cipher_none_decrypt(const char *data,char *output,int &len,char * key)
|
||||
|
||||
int auth_cal(const char *data,char * output,int &len)
|
||||
{
|
||||
if(auth_mode==auth_md5)return auth_md5_cal(data,output,len);
|
||||
else if(auth_mode==auth_none ) return auth_none_cal(data,output,len);
|
||||
|
||||
switch(auth_mode)
|
||||
{
|
||||
case auth_crc32:return auth_crc32_cal(data, output, len);
|
||||
case auth_md5:return auth_md5_cal(data, output, len);
|
||||
case auth_sum:return auth_sum_cal(data, output, len);
|
||||
case auth_none:return auth_none_cal(data, output, len);
|
||||
}
|
||||
return auth_md5_cal(data,output,len);//default
|
||||
}
|
||||
int auth_verify(const char *data,int &len)
|
||||
{
|
||||
if(auth_mode==auth_md5)return auth_md5_verify(data,len);
|
||||
else if(auth_mode==auth_none )return auth_none_verify(data,len);
|
||||
|
||||
return auth_md5_verify(data,len);
|
||||
switch(auth_mode)
|
||||
{
|
||||
case auth_crc32:return auth_crc32_verify(data, len);
|
||||
case auth_md5:return auth_md5_verify(data, len);
|
||||
case auth_sum:return auth_sum_verify(data, len);
|
||||
case auth_none:return auth_none_verify(data, len);
|
||||
}
|
||||
return auth_md5_verify(data,len);//default
|
||||
}
|
||||
int cipher_encrypt(const char *data,char *output,int &len,char * key)
|
||||
{
|
||||
if(cipher_mode==cipher_aes128cbc)return cipher_aes128cbc_encrypt(data,output,len, key);
|
||||
if(cipher_mode==cipher_none)return cipher_none_encrypt(data,output,len, key);
|
||||
|
||||
switch(cipher_mode)
|
||||
{
|
||||
case cipher_aes128cbc:return cipher_aes128cbc_encrypt(data,output,len, key);
|
||||
case cipher_xor:return cipher_xor_encrypt(data,output,len, key);
|
||||
case cipher_none:return cipher_none_encrypt(data,output,len, key);
|
||||
}
|
||||
return cipher_aes128cbc_encrypt(data,output,len, key);
|
||||
}
|
||||
int cipher_decrypt(const char *data,char *output,int &len,char * key)
|
||||
{
|
||||
if(cipher_mode==cipher_aes128cbc)return cipher_aes128cbc_decrypt(data,output,len, key);
|
||||
if(cipher_mode==cipher_none)return cipher_none_decrypt(data,output,len, key);
|
||||
switch(cipher_mode)
|
||||
{
|
||||
case cipher_aes128cbc:return cipher_aes128cbc_decrypt(data,output,len, key);
|
||||
case cipher_xor:return cipher_xor_decrypt(data,output,len, key);
|
||||
case cipher_none:return cipher_none_decrypt(data,output,len, key);
|
||||
}
|
||||
return cipher_aes128cbc_decrypt(data,output,len,key);
|
||||
}
|
||||
|
||||
|
||||
int my_encrypt(const char *data,char *output,int &len,char * key)
|
||||
{
|
||||
if(len<0) {log(log_trace,"len<0");return -1;}
|
||||
if(len>65535) {log(log_trace,"len>65535");return -1;}
|
||||
if(len<0) {mylog(log_trace,"len<0");return -1;}
|
||||
if(len>max_data_len) {mylog(log_trace,"len>max_data_len");return -1;}
|
||||
|
||||
char buf[65535+100];
|
||||
char buf2[65535+100];
|
||||
char buf[buf_len];
|
||||
char buf2[buf_len];
|
||||
memcpy(buf,data,len);
|
||||
if(auth_cal(buf,buf2,len)!=0) {log(log_trace,"auth_cal failed ");return -1;}
|
||||
if(cipher_encrypt(buf2,output,len,key) !=0) {log(log_trace,"auth_cal failed ");return -1;}
|
||||
if(auth_cal(buf,buf2,len)!=0) {mylog(log_trace,"auth_cal failed ");return -1;}
|
||||
if(cipher_encrypt(buf2,output,len,key) !=0) {mylog(log_trace,"auth_cal failed ");return -1;}
|
||||
return 0;
|
||||
|
||||
}
|
||||
int my_decrypt(const char *data,char *output,int &len,char * key)
|
||||
{
|
||||
if(len<0) return -1;
|
||||
if(len>65535) return -1;
|
||||
if(len>max_data_len) return -1;
|
||||
|
||||
if(cipher_decrypt(data,output,len,key) !=0) {log(log_trace,"cipher_decrypt failed "); return -1;}
|
||||
if(auth_verify(output,len)!=0) {log(log_trace,"auth_verify failed ");return -1;}
|
||||
if(cipher_decrypt(data,output,len,key) !=0) {mylog(log_trace,"cipher_decrypt failed \n"); return -1;}
|
||||
if(auth_verify(output,len)!=0) {mylog(log_trace,"auth_verify failed ");return -1;}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int my_encrypt_old(const char *data0,char *output,int &len,char * key)
|
||||
{
|
||||
static const int disable_all=0;
|
||||
static const int disable_aes=0;
|
||||
|
||||
char data[65535+100];
|
||||
memcpy(data,data0,len);
|
||||
|
||||
@ -219,6 +316,9 @@ int my_encrypt_old(const char *data0,char *output,int &len,char * key)
|
||||
}
|
||||
int my_decrypt_old(const char *data0,char *output,int &len,char * key)
|
||||
{
|
||||
static const int disable_all=0;
|
||||
static const int disable_aes=0;
|
||||
|
||||
char data[65535+100];
|
||||
memcpy(data,data0,len);
|
||||
|
||||
|
@ -16,10 +16,9 @@ int my_decrypt_pesudo_header(uint8_t *data,uint8_t *output,int &len,uint8_t * ke
|
||||
unsigned short csum(const unsigned short *ptr,int nbytes) ;
|
||||
|
||||
|
||||
const int auth_none=0;
|
||||
const int auth_md5=1;
|
||||
enum auth_mode_t {auth_none=0,auth_md5,auth_crc32,auth_sum};
|
||||
|
||||
enum cipher_mode_t {cipher_none=0,cipher_aes128cbc,cipher_xor};
|
||||
|
||||
const int cipher_none=0;
|
||||
const int cipher_aes128cbc=1;
|
||||
|
||||
#endif
|
||||
|
26
log.cpp
26
log.cpp
@ -1,10 +1,10 @@
|
||||
#include <log.h>
|
||||
|
||||
int log_level=log_impossible;
|
||||
int log_level=log_info;
|
||||
|
||||
int enable_log_position=1;
|
||||
int enable_log_color=1;
|
||||
char log_text[][20]={"IMPOSSIBLE","FATAL","ERROR","WARN","INFO","DEBUG","TRACE"};
|
||||
char log_text[][20]={"NEVER","FATAL","ERROR","WARN","INFO","DEBUG","TRACE",""};
|
||||
char log_color[][20]={RED,RED,RED,YEL,GRN,BLU,""};
|
||||
void log0(const char * file,const char * function,int line,int level,const char* str, ...) {
|
||||
|
||||
@ -20,7 +20,7 @@ void log0(const char * file,const char * function,int line,int level,const char*
|
||||
tm_info = localtime(&timer);
|
||||
|
||||
if(enable_log_color)
|
||||
printf(log_color[level]);
|
||||
printf(log_color[level]);
|
||||
|
||||
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
|
||||
printf("[%s][%s]",buffer,log_text[level],file,line);
|
||||
@ -33,6 +33,26 @@ void log0(const char * file,const char * function,int line,int level,const char*
|
||||
va_end(vlist);
|
||||
if(enable_log_color)
|
||||
printf(RESET);
|
||||
|
||||
//printf("\n");
|
||||
if(enable_log_color)
|
||||
printf(log_color[level]);
|
||||
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void log_bare(int level,const char* str, ...)
|
||||
{
|
||||
if(level>log_level) return ;
|
||||
if(level>log_trace||level<0) return ;
|
||||
if(enable_log_color)
|
||||
printf(log_color[level]);
|
||||
va_list vlist;
|
||||
va_start(vlist, str);
|
||||
vfprintf(stdout, str, vlist);
|
||||
va_end(vlist);
|
||||
if(enable_log_color)
|
||||
printf(RESET);
|
||||
fflush(stdout);
|
||||
|
||||
}
|
||||
|
8
log.h
8
log.h
@ -66,7 +66,7 @@ using namespace std;
|
||||
#define RESET "\x1B[0m"
|
||||
|
||||
|
||||
const int log_impossible=0;
|
||||
const int log_never=0;
|
||||
const int log_fatal=1;
|
||||
const int log_error=2;
|
||||
const int log_warn=3;
|
||||
@ -75,11 +75,15 @@ const int log_debug=5;
|
||||
const int log_trace=6;
|
||||
|
||||
extern int log_level;
|
||||
extern int enable_log_position;
|
||||
extern int enable_log_color;
|
||||
|
||||
|
||||
#define log(...) log0(__FILE__,__FUNCTION__,__LINE__,__VA_ARGS__)
|
||||
#define mylog(...) log0(__FILE__,__FUNCTION__,__LINE__,__VA_ARGS__)
|
||||
|
||||
void log0(const char * file,const char * function,int line,int level,const char* str, ...);
|
||||
|
||||
void log_bare(int level,const char* str, ...);
|
||||
|
||||
|
||||
#endif
|
||||
|
4
makefile
4
makefile
@ -2,7 +2,7 @@ ccmips=mips-openwrt-linux-g++
|
||||
all:
|
||||
killall raw||true
|
||||
sleep 1
|
||||
g++ main.cpp -o raw -static -lrt -ggdb -I. aes.c md5.c encrypt.cpp log.cpp -O3
|
||||
${ccmips} -O3 main.cpp -o rawmips -lrt -I. aes.c md5.c encrypt.cpp log.cpp
|
||||
g++ main.cpp -o raw -static -lrt -ggdb -I. aes.c md5.c encrypt.cpp log.cpp -O3 -std=c++11
|
||||
${ccmips} -O3 main.cpp -o rawmips -lrt -I. aes.c md5.c encrypt.cpp log.cpp -std=c++11
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user