Быстрая схема аутентификации и обмена ключами, устойчивая к DDoS-атаке
Дипломная работа - Компьютеры, программирование
Другие дипломы по предмету Компьютеры, программирование
p;
#define _CRT_SECURE_NO_WARNINGS
_SECTION cs1;_SECTION cs2;_SECTION cs3;_SECTION cs4;
num_connect=0;_short PORT=3425;
*pub_serv;*priv_serv;*log,*log1,*log2;tik_per_second;config_param[10];config_flags[10];counter[8],savg[8];
__int64 rdtsc() {__rdtsc();
}
servini(){* pub_key_file = NULL;* priv_key_file = NULL;* ini;i=0;tmp[10];__int64 t1, t2;EVP_CIPHER *cipher = NULL;
_add_all_ciphers();= EVP_get_cipherbyname("bf-ofb");(GetCurrentProcess(), HIGH_PRIORITY_CLASS);_s(&ini,"serv_ini","r+");(ini,"%d",&PORT);(ini,"%d",&config_param[0]);(ini,"%d",&config_param[1]);(ini,"%d",&config_param[2]);(ini,"%d",&config_param[3]);(ini,"%d",&config_param[4]);(ini,"%d",&config_param[5]);(ini,"%d",&config_flags[0]);(ini,"%d",&config_flags[1]);(ini,"%d",&config_flags[2]);(ini,"%d",&config_flags[3]);(ini,"%d",&config_flags[4]);
(i0){[i]=tik_per_second;++;
}
}_per_second=0;(int j=0;j<10;j++){_per_second+=tmp[j];
}_per_second/=10;("%f",tik_per_second);(int i=0; i<8; i++){[i]=0;[i]=0;
}((fopen_s(&log1,"log_error.txt", "w+"))!=0){(-1);
}((fopen_s(&log,"log.txt", "w+"))!=0){(log1,"error open log file");(-1);
}((fopen_s(&log2,"log_AVG.txt", "w+"))!=0){(log1,"error open AVG log file");(-1);
}((fopen_s(&pub_key_file,PUBLIC_SERVER_KEY, "rb"))!=0){(log1,"error open PUBLIC_SERVER_KEY");(-1);
}{_serv = PEM_read_RSAPublicKey(pub_key_file, NULL, NULL, NULL);
}((fopen_s(&priv_key_file,PRIVAT_SERVER_KEY, "rb"))!=0){(log1,"error open PRIVAT_SERVER_KEY");(-1);
}{_serv = PEM_read_RSAPrivateKey(priv_key_file, NULL, NULL, "hello");
}0;
}
md5(unsigned char *str, unsigned char *md_value, int strlen){_MD_CTX mdctx;EVP_MD * md;int md_len;_add_all_digests();= EVP_get_digestbyname("md5");_DigestInit(&mdctx, md);_DigestUpdate(&mdctx, str, strlen);_DigestFinal(&mdctx, md_value, &md_len);_MD_CTX_cleanup(&mdctx);1;
}
do_DECRYPT_AES(unsigned char *k, char *str, int strlen)
{outlen;char key[32]; /* 256- битный ключ */
unsigned char iv[8]; /* вектор инициализации */
unsigned char inbuf[BUFSIZE], outbuf[BUFSIZE];(int i=0; i<32; i++){[i]=k[i];
}(inbuf,str,strlen);_CIPHER_CTX ctx;EVP_CIPHER * cipher;_CIPHER_CTX_init(&ctx);= EVP_aes_256_cfb();_DecryptInit(&ctx, cipher, key, iv);_DecryptUpdate(&ctx, outbuf, &outlen,inbuf, strlen);(str,outbuf,strlen);_EncryptFinal(&ctx, outbuf, &outlen);_CIPHER_CTX_cleanup(&ctx);0;
}
do_AES(unsigned char *k, char *str, int strlen)
{outlen;char key[32]; /* 256- битный ключ */
unsigned char iv[8]; /* вектор инициализации */
unsigned char inbuf[BUFSIZE], outbuf[BUFSIZE];
(int i=0; i<32; i++){[i]=k[i];
}
(inbuf,str,strlen);_CIPHER_CTX ctx;EVP_CIPHER * cipher;_CIPHER_CTX_init(&ctx);= EVP_aes_256_cfb();_EncryptInit(&ctx, cipher, key, iv);_EncryptUpdate(&ctx, outbuf, &outlen, inbuf, strlen);(str,outbuf,strlen);_EncryptFinal(&ctx, outbuf, &outlen);_CIPHER_CTX_cleanup(&ctx);
1;
}
do_decrypt(unsigned char *ctext, unsigned char *ptext){outlen;key_size = RSA_size(priv_serv);= RSA_private_decrypt(key_size, ctext, ptext, priv_serv, RSA_PKCS1_PADDING);(outlen < 0){(log1,"error: do_decrypt failed;");
}0;
}
do_verify(unsigned char *m, unsigned int m_len, unsigned char *ctext, unsigned int key_size, RSA *pubKey){(RSA_verify(1,m,m_len,ctext,key_size,pubKey)==1){0;
}{1;
}
}
to_hex(unsigned char *str_dest, unsigned char *str_src, int sstr_len){tmp[4],buf1[RSA_key_size+1];
(int i=0; i<sstr_len; i+=2){(tmp,"x",str_src[i/2]);(&buf1[i],"%c",tmp[0]);(i+1<sstr_len)(&buf1[i+1],"%c",tmp[1]);
}(str_dest,buf1,sstr_len+1);0;
}
split_pakage(char *pakage, unsigned char *P1, unsigned char *P2, unsigned char *P3, unsigned char *P4){buf[RSA_key_size+1];buf2[KEYSIZE+1];count=0;(int i=0; i<RSA_key_size; i++){[i]=pakage[count];++;
}(P1,buf,RSA_key_size);(int i=0; i<RSA_key_size; i++){[i]=pakage[count];++;
}(P2,buf,RSA_key_size);(int i=0; i<KEYSIZE; i++){[i]=pakage[count];++;
}(P3,buf2,KEYSIZE);(int i=0; i<RSA_key_size; i++){[i]=pakage[count];++;
}(P4,buf,RSA_key_size);0;
}
make_replay(char *buff, int mode,unsigned char *k2){tmp[]="Hello,clien";tmp1[]="dialogstart";(mode==1){(buff,tmp,11);_AES(k2,tmp,11);(int i=0;i<11;i++){[i+11]=tmp[i];
}[23]=\0;1;
}(mode==0){="authentication failed";1;
}(mode==2){(buff,tmp1,11);_AES(k2,tmp1,11);(int i=0;i<11;i++){[i+11]=tmp1[i];
}[23]=\0;1;
}0;
}
split_P1(unsigned char *K1, unsigned char *K2, unsigned char *IDc, unsigned char *ptext){
(int i=0; i<KEYSIZE; i++){[i]=ptext[i];[i]=ptext[KEYSIZE+i];[i]=ptext[(KEYSIZE*2)+i];
}[KEYSIZE]=K2[KEYSIZE]=IDc[KEYSIZE]=\0;0;
}
get_authkey(unsigned char *IDauthkey, unsigned char *authkey){*f;buf[KEYSIZE+3]="1\\",tmp[33];(tmp,IDauthkey,32);[32]=\0;(int i=0; i<KEYSIZE;i++){[i+2]=tmp[i];
}((fopen_s(&f,buf,"r+"))==0){((fread(authkey,1,33,f))!=0){(f);::remove(buf);0;
}(f);1;
}1;
}
*get_PKC(unsigned char *IDc, RSA *pubKey){*pub_key_file;buf[33];(buf,IDc,32);[32]=\0;((fopen_s(&pub_key_file,buf, "rb"))==0){= PEM_read_RSAPublicKey(pub_key_file, NULL, NULL, NULL);(pub_key_file);pubKey;
}{(log1,"error: can not open PKC fail");
}
NULL;
}pre_auth(unsigned char *P1, unsigned char *P4, unsigned char *authkey){
buf[RSA_key_size+1];char tmp[RSA_key_size+1];(buf,P1,RSA_key_size);_AES(authkey,buf,RSA_key_size);(tmp,buf,RSA_key_size);[RSA_key_size]=\0;[RSA_key_size]=\0;((_mbscmp(P4,tmp))==0){0;
}1;
}
gen_authkey(unsigned char *k1, unsigned char *k2){
buf[KEYSIZE+1];tmp1[KEYSIZE+4]="1\\";char tmp[KEYSIZE+1];*f;(k1,tmp,KEYSIZE);_hex(tmp,tmp,KEYSIZE);(buf,tmp,KEYSIZE);[KEYSIZE]=\0;(int i=0; i<KEYSIZE;i++){[i+2]=buf[i];
}_s(&f,tmp1,"w+");(k2,tmp,KEYSIZE);_hex(tmp,tmp,KEYSIZE);(tmp,1,KEYSIZE+1,f);(f);0;
}
dialog(char *buf,int strlen, unsigned char *session_key){tmp[10];_DECRYPT_AES(session_key,buf,strlen);(int i=0; i<strlen; i++){
_itoa(i,tmp,10);[i]=tmp[0];
}_AES(session_key,buf,strlen);0;
}
__stdcall servprotocol( void* pArguments ){
*pubKey=NULL;buff[BUFSIZE],tmp1[10];char tmp[RSA_key_size+1];char authkey[KEYSIZE+1];char session_key1[KEYSIZE+1], session_key2[KEYSIZE+1], IDc[KEYSIZE+1], P1[RSA_key_size+1], P2[RSA_key_size+1], P3[KEYSIZE+1], P4[RSA_key_size+1];char ptext[RSA_key_size+1];byte_read, paflag=0,client_speak=0;;__int64 t1,t2;cur_tik,cur_time;
sock1=((SOCKET *) pArguments)[0];_PRIORITY_BELOW_NORMAL);((byte_read=recv(sock1, buff, 417,0))>0){(config_flags[0]==1)(config_param[1]);= rdtsc ();_pakage(buff,P1,P2,P3,P4);(config_flags[4]==1){(&cs3);((get_authkey(P3,authkey))==0){(&cs3);((pre_auth(P1,P4,authkey))==0){=0;
}
}{(&cs3);(GetCurrentThread(), THREAD_PRIORITY_LOWEST);=1;
}
}(config_flags[3]==1)(1);(&cs1);_decrypt(P1,ptext);(&cs1);_P1(session_key1, session_key2,IDc,ptext);(&cs2);=get_PKC(IDc,pubKey);(&cs2);(pubKey!=NULL){(P1,tmp,RSA_key_size);_hex(tmp,tmp,KEYSIZE);(buff,P2,RSA_key_size);_DECRYPT_AES(session_key1, buff, RSA_key_size);(P2, buff, RSA_key_size);[RSA_key_size+1]=\0;((do_verify(tmp,32,P2,RSA_key_size,pubKey))==0){(config_flags[1]==1 && config_flags[4]==1)_authkey(session_key1,session_key2);= rdtsc ();_tik=t2-t1;_time=cur_tik/tik_per_second*1000;(log,"\nsucceed %d %d %f ms\n",paflag,num_connect, cur_time);(config_flags[2]!=1){_replay(buff,1,session_key2);(sock1,buff,25,0);(config_flags[1]!=1 && config_flags[4]==1)_authkey(session_key1,session_key2);
}{_replay(buff,2,session_key2);(sock1,buff,25,0);(config_flags[1]!=1)_authkey(session_key1,session_key2);((byte_read=recv(sock1, buff, config_param[5],0))>0){(tmp1,buff,byte_read);(strcmp(tmp1,"ok")==0){(config_param[4],buff,10);_read=send(sock1,buff,config_param[4],0);_read=recv(sock1, buff, config_param[5],0);(config_param[5],buff,10);_read=send(sock1,buff,config_param[5],0);(client_speak0){(buff,config_param[5],session_key2);(sock1,buff,config_param[5],0);_speak++;
}{_speak+=config_param[4];
}
}
}
}
}(closesocket(sock1)!=0){(log1,"close sock1 eror (succeed auth)");
}( &cs4 );(paflag==0){(cur_time10){(log2,"\n\nlegal up client time AVG= %f\n\n",savg[4]/counter[4]);[4]=0;[4]=0;
}
}
}(paflag==1){(cur_time=config_param[2]){(log2,"\nlegal down client time avg=%f\n&qu