Sunday 17 November 2013

3G EPS AKA(Authentication Key Agreement) HOME ENVIRONMENT (HE) / HLR Code


#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define PORT "3493"  // the port users will be connecting to
#define MAX_VEC 5
#define BACKLOG 10 // how many pending connections queue will hold
#define MAXDATASIZE 1000

struct authentication_vector
{
       int mac;
       int ak;
       int random_no;
       int xres;
       int ck;
       int ik;
       char autn[100];
};

typedef struct authentication_vector av;

static int counter_cs=0;
static int counter_ps=0;

// Generate MAC
int fk1(int sqn,int rand,int mode)
{
    int x=2;
    return (sqn*x*x+rand*x+mode);
}
//generate expected RES(result)(XRES)
int fk2(int rand)
{
    int x=3;
    return (rand*x);
}
//generate cipher key(CK)
int fk3(int rand)
{
    int x=4;
    return (rand*x);
}
//generate integrity key(IK)
int fk4(int rand)
{
    int x=5;
    return (rand*x);
}
//generate anonimity key(AK)
int fk5(int rand)
{
    int x=5;
    return (rand*x);
}


void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}

return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(void)
{
int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv;

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP

if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}

// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}

if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}

if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}

break;
}

if (p == NULL)  {
fprintf(stderr, "server: failed to bind\n");
return 2;
}

freeaddrinfo(servinfo); // all done with this structure

if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}

printf("server: waiting for connections...\n");
int numbytes;
char buf[MAXDATASIZE];
while(1) {  // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}

inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("server: got connection from %s\n", s);

if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
/*---Recieve IMUI and MODE from VLR---*/
if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1)
{
    perror("recv");
    exit(1);
}

buf[numbytes] = '\0';
printf("packet Recieved: %s\n",buf);

//***************************************************
      //*********************** AKA ***********************
        //***************************************************

//identify mode of user MS
       int i=0;
        int flag=0;
        char hlr_tmode[20];
        char hlr_timui[20];
        int k=0;
        while(buf[i]!='\0')
        {
              if(buf[i]=='#')
              {            flag++; k=0; i++; continue;}
              else if(flag==0)              
              {            hlr_timui[k]=buf[i]; k++;}
              else if(flag==1)
              {            hlr_tmode[k]=buf[i]; k++;}            
              else break;      
              i++;                                  
        }
        int mode=atoi(hlr_tmode);
        printf("MODE : %d \n",mode);
       
        //generate sqn sequence number according to mode
        int sqn;
        if(mode==0)
        { sqn=++counter_cs;}
        else
        { sqn=++counter_ps;}
              printf("SQN : %d \n")
        // create authentication vector
        i=0;
int sxa;
char temp[10],tmac[10],tmode[10],sqn_xor_ak[10];
        av avec[MAX_VEC];
        FILE *fp;
        fp=fopen("temp_auvec.hlr","w");
        srand(time(NULL));
       
bzero(buf,MAXDATASIZE);

while(i<MAX_VEC)
        {
                avec[i].random_no=rand()%1000;      
avec[i].mac=fk1(sqn,avec[i].random_no,mode);
                avec[i].xres=fk2(avec[i].random_no);
                avec[i].ck=fk3(avec[i].random_no);
                avec[i].ik=fk4(avec[i].random_no);
                avec[i].ak=fk5(avec[i].random_no);
           
            //generate autn
                sxa=sqn^avec[i].ak;
             
                sprintf(sqn_xor_ak,"%d",sxa);
                sprintf(tmode,"%d",mode);
                sprintf(tmac,"%d",avec[i].mac);

                memset(avec[i].autn,0,sizeof(avec[i].autn));
               
                strcat(avec[i].autn,sqn_xor_ak);
                strcat(avec[i].autn,"#");
                strcat(avec[i].autn,tmode);
                strcat(avec[i].autn,"#");
                strcat(avec[i].autn,tmac);
                strcat(avec[i].autn,"#");
                // write structure into file.
                fwrite(&avec[i],sizeof(avec[i]),1,fp);
                fputs("*",fp);
                i++;        
        }
     
        printf("\n\n Successfully created Authentication Vector....!!!!\n\n");
        printf("\n\nNo of elements in the vector : %d",MAX_VEC);
        int choice;
     
        printf("View Authentication Vectors :(Yes=1/No=0)");
        scanf("%d",&choice);
if(choice)
        {
                int i=0;
                while(i<MAX_VEC)
                {

printf(" \nMAC %d : %d",i+1,avec[i].mac);
bzero(temp,10);
sprintf(temp,"%d",avec[i].mac);
printf("\n%s",temp);              
strcat(buf,temp);
strcat(buf,"*");

                     
printf(" \nAnonymity Key %d : %d",i+1,avec[i].ak);
                      bzero(temp,10);
sprintf(temp,"%d",avec[i].ak);
printf("\n%s",temp);              
strcat(buf,temp);
strcat(buf,"*");

printf(" \nRAND %d : %d",i+1,avec[i].random_no);
bzero(temp,10);
sprintf(temp,"%d",avec[i].random_no);
printf("\n%s",temp);              
strcat(buf,temp);
strcat(buf,"*");

                      printf(" \nXRES %d : %d",i+1,avec[i].xres);
bzero(temp,10);
sprintf(temp,"%d",avec[i].xres);
printf("\n%s",temp);              
              strcat(buf,temp);
strcat(buf,"*");
                     
printf(" \nCipher Key %d : %d",i+1,avec[i].ck);
bzero(temp,10);
sprintf(temp,"%d",avec[i].ck);
              strcat(buf,temp);
strcat(buf,"*");

                      printf(" \nIntegrity Key %d : %d",i+1,avec[i].ik);
bzero(temp,10);
sprintf(temp,"%d",avec[i].ik);
              strcat(buf,temp);
strcat(buf,"*");

                      printf(" \nAuthentication Token %d : %s",i+1,avec[i].autn);
bzero(temp,10);
strcat(buf,avec[i].autn);
strcat(buf,"*");

                      //printf("\n buffer : %s\n",buf);        
                      i++;
                }
               
        }

fseek(fp,0,SEEK_SET);
        while(fscanf(fp,"%s",buf)!=EOF);
fclose(fp);

printf("\n\n Packet Created\n\n %s",buf);
if (send(new_fd, buf, sizeof(buf), 0) == -1)
perror("send");

close(new_fd);
exit(0);
}
close(new_fd);  // parent doesn't need this
}

return 0;
}

3G EPS AKA(Authentication Key Agreement) SERVING NETWORK / VLR Code


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MAXDATASIZE 1000 // max number of bytes we can get at once
#define MAXBUF          1000
#define MAX_VEC         5
#define PORT "3492"  // the port users will be connecting to
#define PORT_HLR "3493"               // Port of HLR Server
#define HLR_ADDR "127.0.0.1"            //  IP Address of HLR
#define LOCAL_ID 1234
#define BACKLOG 10 // how many pending connections queue will hold


struct authentication_vector
{
       int mac;
       int ak;
       int random_no;
       int xres;
       int ck;
       int ik;
       char autn[100];
};

typedef struct authentication_vector av;


//*********************
//  VLR Table and identities
int vlr_table[MAXBUF][2]; //IMUI and TMUI of MS
int lai=LOCAL_ID;
av au_vector[MAX_VEC];
int current=-1;
int current_ms=0;
//**********************



void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
{
return &(((struct sockaddr_in*)sa)->sin_addr);
}

return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(void)
{
int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
char buf[MAXDATASIZE];
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv;

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP

if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}

// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("server: socket");
continue;
}

if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
}

if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL)
{
fprintf(stderr, "server: failed to bind\n");
return 2;
}

freeaddrinfo(servinfo); // all done with this structure

if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
perror("sigaction");
exit(1);
}

printf("server: waiting for connections...\n");
int numbytes;
while(1)
{  // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1)
{
perror("accept");
continue;
}

inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr),s, sizeof s);
printf("\nserver: got connection from %s\n", s);

if (!fork())
{ // this is the child process
close(sockfd); // child doesn't need the listener
/*---Recieve IMUI and MODE from MS---*/
if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1)
{
    perror("recv");
    exit(1);
}

buf[numbytes] = '\0';
printf("packet Recieved: %s\n",buf);
/*----Extract IMUI and Mode from Packet---------*/
        int i=0;
        int flag=0;
        char vlr_tmode[5];
        char vlr_timui[10];
        int k=0;
        while(buf[i]!='\0')
        {
              if(buf[i]=='#')
              {            flag++; k=0; i++; continue;}
              else if(flag==0)              
              {            vlr_timui[k]=buf[i]; k++;}
              else if(flag==1)
              {            vlr_tmode[k]=buf[i]; k++;}            
              else break;      
              i++;                                  
        }
        int vlr_mode=atoi(vlr_tmode);
        int vlr_imui=atoi(vlr_timui);
printf("\n IMUI: %d",vlr_imui);
printf("\n MODE: %d",vlr_mode);

/************************ Send MS the Latest Unused Authentication Vector*********/
if(current<MAX_VEC && current!=-1)
       {
char trand[10];
bzero(buf,MAXDATASIZE);
bzero(trand,10);
sprintf(trand,"%d",au_vector[current].random_no);
strcat(buf,trand);
strcat(buf,"*");
strcat(buf,au_vector[current].autn);
if (send(new_fd, buf, sizeof(buf), 0) == -1)
perror("send");
        }
else
        {
/************************Connect to HLR for New Authentication Token *****************/
                         
int sockfd, numbytes;
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

if ((rv = getaddrinfo(HLR_ADDR, PORT_HLR, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}

// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("\nclient HLR: socket");
continue;
}

if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("\nclient HLR: connect");
continue;
}

break;
}

if (p == NULL)
{
fprintf(stderr, "client HLR: failed to connect\n");
return 2;
}

inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
printf("\nclient HLR: connecting to %s\n", s);

freeaddrinfo(servinfo); // all done with this structure

/******************* Send Request to HLR *****************/
                       
send(sockfd, buf, sizeof(buf), 0);
                          printf("\n\n Request Sent to HLR");        
                          //memset(buf,0,sizeof(buf));
                          //recv(sockfd,buf,sizeof(buf),0);

/***************** Recieve Authentication Vectors *********/
memset(buf,0,sizeof(buf));
if ((numbytes = recv(sockfd, buf,MAXDATASIZE, 0)) == -1)
{
   perror("recv");
   exit(1);
}

buf[numbytes] = '\0';

printf("\nAuthentication Vectors received '%s'\n",buf);

close(sockfd);

                         
                                /************** Store New authentication Vectors ***********/
                          int i=0,flag=0,k=0,x=0;
                          char ch,tmac[10],tak[10],trand[10],txres[10],tck[10],tik[10],tautn[10];
                          while((ch=buf[x++])!='\0')
                          {
                                                    if(ch=='*'){ flag++; if(flag!=7)i=0;}
   else if(flag==0){ tmac[i]=ch; i++;}
   else if(flag==1){ tak[i]=ch; i++;}
   else if(flag==2){ trand[i]=ch; i++;}
   else if(flag==3){ txres[i]=ch; i++;}
   else if(flag==4){ tck[i]=ch; i++;}
   else if(flag==5){ tik[i]=ch; i++;}
   else if(flag==6){ au_vector[k].autn[i]=ch; i++;}
                 else if(flag==7)
   {                
       au_vector[k].mac=atoi(tmac);
               au_vector[k].ak=atoi(tak);
       au_vector[k].random_no=atoi(trand);
               au_vector[k].xres=atoi(txres);
       au_vector[k].ck=atoi(tck);
                               au_vector[k].ik=atoi(tik);
bzero(tmac,10);
bzero(tak,10);
                                          bzero(trand,10);
bzero(txres,10);
                                          bzero(tck,10);
bzero(tik,10);
                                          k++;
i=0;
tmac[i]=ch; i++;
flag=0;
}
      }
au_vector[k].mac=atoi(tmac);
au_vector[k].ak=atoi(tak);
au_vector[k].random_no=atoi(trand);
au_vector[k].xres=atoi(txres);
au_vector[k].ck=atoi(tck);
                au_vector[k].ik=atoi(tik);

printf("\n\nNo of elements in the vector : %d",MAX_VEC);
       
int choice;    
        printf("View Authentication Vectors :(Yes=1/No=0)");
        scanf("%d",&choice);
if(choice)
        {
                int i=0;
                while(i<MAX_VEC)
                {

printf(" \nMAC %d : %d",i+1,au_vector[i].mac);

printf(" \nAnonymity Key %d : %d",i+1,au_vector[i].ak);
                     
printf(" \nRAND %d : %d",i+1,au_vector[i].random_no);

                      printf(" \nXRES %d : %d",i+1,au_vector[i].xres);

printf(" \nCipher Key %d : %d",i+1,au_vector[i].ck);

                      printf(" \nIntegrity Key %d : %d",i+1,au_vector[i].ik);

                      printf(" \nAuthentication Token %d : %s\n\n",i+1,au_vector[i].autn);
     
                      i++;
                }
             
      }
                          current=0;
/************* Send latest Vector **********/
bzero(buf,MAXDATASIZE);
bzero(trand,10);
sprintf(trand,"%d",au_vector[current].random_no);
strcat(buf,trand);
strcat(buf,"#");
strcat(buf,au_vector[current].autn);
if (send(new_fd, buf, sizeof(buf), 0) == -1)
perror("send");


          }

/************* Recieve RES From MS ***************/
numbytes=0;
bzero(buf,MAXDATASIZE);
if ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) == -1)
{
    perror("recv");
    exit(1);
}
printf("\n\n Packet recieved :\n\n %s",buf);
int vlr_xres=atoi(buf);
if(vlr_xres==au_vector[current].xres)
{  printf("\n\nMOBLE STATION VERIFIED SUCCESSFULLY.....!!!!");}
current++;
close(new_fd);

exit(0);
}
close(new_fd);  // parent doesn't need this
}

return 0;
}

3G EPS AKA(Authentication Key Agreement) MOBILE STATION CODE



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include <arpa/inet.h>

#define PORT "3492"                     // the port client will be connecting to
#define ms_ADDR "127.0.0.1"            //  IP Address of VLR
#define MAXDATASIZE 100                 // max number of bytes we can get at once



//********************************
// MS identity numbers
int sqn_HE=-1;
int imui;
int TMUI;
int LAI;
int mode;
int latest_ck;
int latest_ik;
//********************************



// Generate MAC
int fk1(int sqn,int rand,int mode)
{
    int x=2;
    return (sqn*x*x+rand*x+mode);
}
//generate expected RES(result)(XRES)
int fk2(int rand)
{
    int x=3;
    return (rand*x);
}
//generate cipher key(CK)
int fk3(int rand)
{
    int x=4;
    return (rand*x);
}
//generate integrity key(IK)
int fk4(int rand)
{
    int x=5;
    return (rand*x);
}
//generate anonimity key(AK)
int fk5(int rand)
{
    int x=5;
    return (rand*x);
}



// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}

return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main()
{  
     printf("\n\n ######################################################\n");
     printf(" ######################################################");
     printf("\n\n              MOBILE STATION TERMINAL\n\n")
     printf("\n\n ######################################################\n");
     printf(" ######################################################");
   
/************************** Get MS Details*******************/
    printf("\n Enter IMUI no:");
    scanf("%d",&imui);
    printf("Enter Mode:");
    scanf("%d",&mode);

/************************************************************/      
   

/******************* REQUEST FOR AUTHENTICATION TO VLR ***********/
/****************************************************************/
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

if ((rv = getaddrinfo(ms_ADDR, PORT, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}

// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1)
{
perror("client: socket");
continue;
}

if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("client: connect");
continue;
}

break;
}

if (p == NULL)
{
fprintf(stderr, "client: failed to connect\n");
return 2;
}

inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
printf("client: connecting to %s\n", s);

freeaddrinfo(servinfo); // all done with this structure

/*---Send IMUI and MODE to VLR---*/
    bzero(buf, MAXDATASIZE);
    char timui[20],tmode[20];
//sprintf doesnt work in ubuntu
    sprintf(timui,"%d",imui);
    sprintf(tmode,"%d",mode);  
/*itoa doesnot work in gcc
itoa(imui,timui,10);
itoa(mode,tmode,10);
*/
strcat(buf,timui);
strcat(buf,"#");
    strcat(buf,tmode);
printf("\n\n Sending Packet to VLR.\nPlease Wait");
      while(i<5)
      {
                printf(". ");
                sleep(1000);
                i++;
      }
if (send(sockfd, buf, sizeof(buf), 0) == -1)
perror("send");

/**************************** Recieve Random_no and AUTN FROM VLR**************************************************/
numbytes=0;
bzero(buf,MAXDATASIZE);
if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
{
    perror("recv");
    exit(1);
}
printf("\n\n Packet recieved :\n\n %s",buf);
/**************************** Compute RES From Random_no and AUTN ***********************************************/
/******* retreive Random_no and authentication Token(sqn_xor_ak , mode , MAC) ******/
int i=0;
        int flag=0;
        char ms_trand[10];
        char ms_sqn_xor_ak[10];
char ms_tmac[10];
        char ms_tmode[5];        
int k=0;
        while(buf[i]!='\0')
        {
              if(buf[i]=='#')
              {            flag++; k=0; i++; continue;}
              else if(flag==0)              
              {            ms_trand[k]=buf[i]; k++;}
              else if(flag==1)
              {            ms_sqn_xor_ak[k]=buf[i]; k++;}            
else if(flag==2)
              {            ms_tmode[k]=buf[i]; k++;}
else if(flag==3)
              {            ms_tmac[k]=buf[i]; k++;}
          else break;      
              i++;                                  
        }
        int ms_rand=atoi(ms_trand);
        int ms_sxa=atoi(ms_sqn_xor_ak);
int ms_mac=atoi(ms_tmac);
printf("\n RAND: %d",ms_rand);
printf("\n SXA: %d",ms_sxa);
printf("\n MAC: %d",ms_mac);

      //retreive anonymity key
      int ms_ak=fk5(ms_rand);
      //retreive sqn number
      int ms_sqn=ms_sxa^ms_ak;
      //compute XMAC
      int ms_xmac=fk1(ms_sqn,ms_rand,mode);
      // verify MAC
      printf("\n\n Verifying MAC");
               while(i<5)
               {
                          printf(". ");
                          sleep(1000);
                          i++;
               }
      if(ms_xmac==ms_mac)
      {
                          printf("\n\nMAC Verified...!!!");
      }
      //check freshness of sqn
           printf("\n\n Verifying Sequence Number with Home Sequence no");
               while(i<5)
               {
                         printf(". ");
                         sleep(1000);
                         i++;
                }
      if(ms_sqn>sqn_HE)
      {
                        printf("\n\n Verified Fresh Vector...!!!");
                        sqn_HE++;
      }
      //compute RES
      int ms_res=fk2(ms_rand);
         printf("\nComputed RESULT : %d ",ms_res);
bzero(buf,MAXDATASIZE);
char tres[10];
sprintf(tres,"%d",ms_res);
strcat(buf,tres);
/********** Sending Result back to VLR ************/
printf("\n\n Sending Result to VLR");
      while(i<5)
      {
                printf(". ");
                sleep(1000);
                i++;
      }
if (send(sockfd, buf, sizeof(buf), 0) == -1)
perror("send");
close(sockfd);

printf("\nCipher Key : %d",latest_ck=fk3(ms_rand));
printf("\nIntegrity Key : %d",latest_ik=fk4(ms_rand));

return 0;
}