i want to run this code.....related to 3G EPS AKA(Authentication Key Agreement) HOME ENVIRONMENT (H

20 views
Skip to first unread message

abhi malik

unread,
Apr 19, 2017, 10:09:29 AM4/19/17
to OMNeT++ Users
#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;
}
Reply all
Reply to author
Forward
0 new messages