sync update from linux version

This commit is contained in:
wangyu- 2018-06-23 17:12:04 -05:00
parent d43b9ae98c
commit 62453eaa39
7 changed files with 163 additions and 44 deletions

View File

@ -793,8 +793,13 @@ char * ip_port_t::to_s()
void print_binary_chars(const char * a,int len)
{
for(int i=0;i<len;i++)
{
unsigned char b=a[i];
log_bare(log_debug,"<%02x>",(int)b);
}
log_bare(log_debug,"\n");
}

View File

@ -261,4 +261,6 @@ int hex_to_u32(const string & a,u32_t &output);
int create_fifo(char * file);
void print_binary_chars(const char * a,int len);
#endif /* COMMON_H_ */

View File

@ -504,7 +504,7 @@ int send_bare(raw_info_t &raw_info,const char* data,int len)//send function with
memcpy(send_data_buf+sizeof(iv)+sizeof(padding)+1,data,len);
int new_len=len+sizeof(iv)+sizeof(padding)+1;
if(my_encrypt(send_data_buf,send_data_buf2,new_len,key)!=0)
if(my_encrypt(send_data_buf,send_data_buf2,new_len)!=0)
{
return -1;
}
@ -520,7 +520,7 @@ int reserved_parse_bare(const char *input,int input_len,char* & data,int & len)
mylog(log_debug,"input_len <0\n");
return -1;
}
if(my_decrypt(input,recv_data_buf,input_len,key)!=0)
if(my_decrypt(input,recv_data_buf,input_len)!=0)
{
mylog(log_debug,"decrypt_fail in recv bare\n");
return -1;
@ -620,7 +620,7 @@ int send_safer(conn_info_t &conn_info,char type,const char* data,int len) //saf
int new_len=len+sizeof(n_seq)+sizeof(n_tmp_id)*2+2;
if(my_encrypt(send_data_buf,send_data_buf2,new_len,key)!=0)
if(my_encrypt(send_data_buf,send_data_buf2,new_len)!=0)
{
return -1;
}
@ -652,7 +652,7 @@ int reserved_parse_safer(conn_info_t &conn_info,const char * input,int input_len
static char recv_data_buf[buf_len];
// char *recv_data_buf=recv_data_buf0; //fix strict alias warning
if(my_decrypt(input,recv_data_buf,input_len,key)!=0)
if(my_decrypt(input,recv_data_buf,input_len)!=0)
{
//printf("decrypt fail\n");
return -1;

View File

@ -1,5 +1,7 @@
#include "lib/aes.h"
#include "lib/md5.h"
#include "lib/pbkdf2-sha1.h"
#include "lib/pbkdf2-sha256.h"
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
@ -16,19 +18,77 @@ 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
* https://crypto.stackexchange.com/questions/5421/using-cbc-with-a-fixed-iv-and-a-random-first-plaintext-block
****/
char key[16];//generated from key_string by md5.
//TODO key derive function
char normal_key[16 + 100];//generated from key_string by md5. reserved for compatiblity
const int hmac_key_len=32;
const int cipher_key_len=32;
unsigned char hmac_key_encrypt[hmac_key_len + 100]; //key for hmac
unsigned char hmac_key_decrypt[hmac_key_len + 100]; //key for hmac
unsigned char cipher_key_encrypt[cipher_key_len + 100]; //key for aes etc.
unsigned char cipher_key_decrypt[cipher_key_len + 100]; //key for aes etc.
unordered_map<int, const char *> auth_mode_tostring = {{auth_none, "none"}, {auth_md5, "md5"}, {auth_crc32, "crc32"},{auth_simple,"simple"}};
unordered_map<int, const char *> auth_mode_tostring = {{auth_none, "none"}, {auth_md5, "md5"}, {auth_crc32, "crc32"},{auth_simple,"simple"},{auth_hmac_sha1,"hmac_sha1"},};
//TODO HMAC-md5 ,HMAC-sha1
unordered_map<int, const char *> cipher_mode_tostring={{cipher_none,"none"},{cipher_aes128cbc,"aes128cbc"},{cipher_xor,"xor"}};
unordered_map<int, const char *> cipher_mode_tostring={{cipher_none,"none"},{cipher_aes128cbc,"aes128cbc"},{cipher_xor,"xor"},};
//TODO aes-gcm
auth_mode_t auth_mode=auth_md5;
cipher_mode_t cipher_mode=cipher_aes128cbc;
int is_hmac_used=0;
int my_init_keys(const char * user_passwd,int is_client)
{
char tmp[1000]="";
int len=strlen(user_passwd);
strcat(tmp,user_passwd);
strcat(tmp,"key1");
md5((uint8_t*)tmp,strlen(tmp),(uint8_t*)normal_key);
if(auth_mode==auth_hmac_sha1)
is_hmac_used=1;
if(is_hmac_used)
{
unsigned char salt[400]="";
char salt_text[400]="udp2raw_salt1";
md5((uint8_t*)(salt_text),strlen(salt_text),salt); //TODO different salt per session
unsigned char pbkdf2_output1[400]="";
PKCS5_PBKDF2_HMAC_SHA256((uint8_t*)user_passwd,len,salt,16,10000, 32,pbkdf2_output1); //TODO HKDF, argon2 ?
unsigned char pbkdf2_output2[400]="";
PKCS5_PBKDF2_HMAC_SHA256(pbkdf2_output1,32,0,0,1, hmac_key_len*2+cipher_key_len*2,pbkdf2_output2); //stretch it
if(is_client)
{
memcpy(cipher_key_encrypt,pbkdf2_output2,cipher_key_len);
memcpy(cipher_key_decrypt,pbkdf2_output2+cipher_key_len,cipher_key_len);
memcpy(hmac_key_encrypt,pbkdf2_output2+cipher_key_len*2,hmac_key_len);
memcpy(hmac_key_decrypt,pbkdf2_output2+cipher_key_len*2+hmac_key_len,hmac_key_len);
}
else
{
memcpy(cipher_key_decrypt,pbkdf2_output2,cipher_key_len);
memcpy(cipher_key_encrypt,pbkdf2_output2+cipher_key_len,cipher_key_len);
memcpy(hmac_key_decrypt,pbkdf2_output2+cipher_key_len*2,hmac_key_len);
memcpy(hmac_key_encrypt,pbkdf2_output2+cipher_key_len*2+hmac_key_len,hmac_key_len);
}
}
print_binary_chars(normal_key,16);
print_binary_chars((char *)hmac_key_encrypt,32);
print_binary_chars((char *)hmac_key_decrypt,32);
print_binary_chars((char *)cipher_key_encrypt,32);
print_binary_chars((char *)cipher_key_decrypt,32);
return 0;
}
/*
* this function comes from http://www.hackersdelight.org/hdcodetxt/crc.c.txt
*/
@ -93,6 +153,37 @@ int auth_md5_cal(const char *data,char * output,int &len)
return 0;
}
int auth_hmac_sha1_cal(const char *data,char * output,int &len)
{
mylog(log_trace,"auth_hmac_sha1_cal() is called\n");
memcpy(output,data,len);//TODO inefficient code
sha1_hmac(hmac_key_encrypt, hmac_key_len, (const unsigned char *)data, len,(unsigned char *)(output+len));
//md5((unsigned char *)output,len,(unsigned char *)(output+len));
len+=20;
return 0;
}
int auth_hmac_sha1_verify(const char *data,int &len)
{
mylog(log_trace,"auth_hmac_sha1_verify() is called\n");
if(len<20)
{
mylog(log_trace,"auth_hmac_sha1_verify len<20\n");
return -1;
}
char res[20];
sha1_hmac(hmac_key_decrypt, hmac_key_len, (const unsigned char *)data, len-20,(unsigned char *)(res));
if(memcmp(res,data+len-20,20)!=0)
{
mylog(log_trace,"auth_hmac_sha1 check failed\n");
return -2;
}
len-=20;
return 0;
}
int auth_crc32_cal(const char *data,char * output,int &len)
{
memcpy(output,data,len);//TODO inefficient code
@ -182,7 +273,8 @@ int padding(char *data ,int &data_len,int padding_num)
{
data_len= (data_len/padding_num)*padding_num+padding_num;
}
data[data_len-1]= (data_len-old_len);
unsigned char * p= (unsigned char *)&data[data_len-1];
*p= (data_len-old_len);
return 0;
}
@ -278,7 +370,9 @@ int auth_cal(const char *data,char * output,int &len)
case auth_md5:return auth_md5_cal(data, output, len);
case auth_simple:return auth_simple_cal(data, output, len);
case auth_none:return auth_none_cal(data, output, len);
default: return auth_md5_cal(data,output,len);//default
case auth_hmac_sha1:return auth_hmac_sha1_cal(data,output,len);
//default: return auth_md5_cal(data,output,len);//default;
default: assert(0==1);
}
}
@ -291,7 +385,9 @@ int auth_verify(const char *data,int &len)
case auth_md5:return auth_md5_verify(data, len);
case auth_simple:return auth_simple_verify(data, len);
case auth_none:return auth_none_verify(data, len);
default: return auth_md5_verify(data,len);//default
case auth_hmac_sha1:return auth_hmac_sha1_verify(data,len);
//default: return auth_md5_verify(data,len);//default
default: assert(0==1);
}
}
@ -320,44 +416,65 @@ int cipher_decrypt(const char *data,char *output,int &len,char * key)
}
int encrypt_AE(const char *data,char *output,int &len /*,char * key*/)
{
mylog(log_trace,"encrypt_AE is called\n");
char buf[buf_len];
char buf2[buf_len];
memcpy(buf,data,len);
if(cipher_encrypt(buf,buf2,len,(char *)cipher_key_encrypt) !=0) {mylog(log_debug,"cipher_encrypt failed ");return -1;}
if(auth_cal(buf2,output,len)!=0) {mylog(log_debug,"auth_cal failed ");return -1;}
int my_encrypt(const char *data,char *output,int &len,char * key)
//printf("%d %x %x\n",len,(int)(output[0]),(int)(output[1]));
//print_binary_chars(output,len);
//use encrypt-then-MAC scheme
return 0;
}
int decrypt_AE(const char *data,char *output,int &len /*,char * key*/)
{
mylog(log_trace,"decrypt_AE is called\n");
//printf("%d %x %x\n",len,(int)(data[0]),(int)(data[1]));
//print_binary_chars(data,len);
if(auth_verify(data,len)!=0) {mylog(log_debug,"auth_verify failed\n");return -1;}
if(cipher_decrypt(data,output,len,(char *)cipher_key_decrypt) !=0) {mylog(log_debug,"cipher_decrypt failed \n"); return -1;}
return 0;
}
int my_encrypt(const char *data,char *output,int &len /*,char * key*/)
{
if(len<0) {mylog(log_trace,"len<0");return -1;}
if(len>max_data_len) {mylog(log_warn,"len>max_data_len");return -1;}
if(is_hmac_used)
return encrypt_AE(data,output,len);
char buf[buf_len];
char buf2[buf_len];
memcpy(buf,data,len);
if(auth_cal(buf,buf2,len)!=0) {mylog(log_debug,"auth_cal failed ");return -1;}
if(cipher_encrypt(buf2,output,len,key) !=0) {mylog(log_debug,"cipher_encrypt failed ");return -1;}
if(cipher_encrypt(buf2,output,len,normal_key) !=0) {mylog(log_debug,"cipher_encrypt failed ");return -1;}
return 0;
}
int my_decrypt(const char *data,char *output,int &len,char * key)
int my_decrypt(const char *data,char *output,int &len /*,char * key*/)
{
if(len<0) return -1;
if(len>max_data_len) {mylog(log_warn,"len>max_data_len");return -1;}
if(cipher_decrypt(data,output,len,key) !=0) {mylog(log_debug,"cipher_decrypt failed \n"); return -1;}
if(is_hmac_used)
return decrypt_AE(data,output,len);
if(cipher_decrypt(data,output,len,normal_key) !=0) {mylog(log_debug,"cipher_decrypt failed \n"); return -1;}
if(auth_verify(output,len)!=0) {mylog(log_debug,"auth_verify failed\n");return -1;}
return 0;
}
int encrypt_AE(const char *data,char *output,int &len,char * key)
{
//TODO
//use encrypt-then-MAC scheme
return -1;
}
int decrypt_AE(const char *data,char *output,int &len,char * key)
{
//TODO
return -1;
}
int encrypt_AEAD(uint8_t *data,uint8_t *output,int &len,uint8_t * key,uint8_t *header,int hlen)
{

View File

@ -9,19 +9,20 @@
//using namespace std;
//extern char key[16];
const int aes_key_optimize=1; //if enabled,once you used a key for aes,you cant change it anymore
extern char key[16];
int my_encrypt(const char *data,char *output,int &len,char * key);
int my_decrypt(const char *data,char *output,int &len,char * key);
int my_init_keys(const char *,int);
int my_encrypt(const char *data,char *output,int &len);
int my_decrypt(const char *data,char *output,int &len);
unsigned short csum(const unsigned short *ptr,int nbytes) ;
enum auth_mode_t {auth_none=0,auth_md5,auth_crc32,auth_simple,auth_end};
enum auth_mode_t {auth_none=0,auth_md5,auth_crc32,auth_simple,auth_hmac_sha1,auth_end};
enum cipher_mode_t {cipher_none=0,cipher_aes128cbc,cipher_xor,cipher_end};

View File

@ -1110,13 +1110,7 @@ int main(int argc, char *argv[])
mylog(log_info,"const_id:%x\n",const_id);
char tmp[1000]="";
strcat(tmp,key_string);
strcat(tmp,"key1");
md5((uint8_t*)tmp,strlen(tmp),(uint8_t*)key);
my_init_keys(key_string,program_mode==client_mode?1:0);
iptables_rule();

View File

@ -17,7 +17,7 @@ PCAP="-lpcap"
LIBNET=-D_DEFAULT_SOURCE `libnet-config --defines` `libnet-config --libs`
SOURCES= $(COMMON) lib/aes_faster_c/aes.cpp lib/aes_faster_c/wrapper.cpp
SOURCES= $(COMMON) lib/aes_faster_c/aes.cpp lib/aes_faster_c/wrapper.cpp lib/pbkdf2-sha1.cpp lib/pbkdf2-sha256.cpp
SOURCES_TINY_AES= $(COMMON) lib/aes.cpp
SOURCES_AES_ACC=$(COMMON) $(wildcard lib/aes_acc/aes*.c)