Sunday 17 November 2013

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;
}

No comments:

Post a Comment

Have some problem with this code? or Request the code you want if you cant find it in Blog Archive.