Exemplo n.º 1
0
int main(int argc, char **argv) {
  pairing_t pairing;
  pbc_demo_pairing_init(pairing, argc, argv);
  char m[80]={0};


  if (!pairing_is_symmetric(pairing)) pbc_die("pairing must be symmetric");

  printf("Enter the message to be encrypted : ");
  gets(m);
  size_t len_m = sizeof(m);

  unsigned char hash[30];
  SHA1(m, len_m, hash);
  printf("The hash is : %s", hash);

  element_t g, h;
  element_t public_key, secret_key;
  element_t sig;
  element_t temp1, temp2;

  element_init_G2(g, pairing);
  element_init_G2(public_key, pairing);
  element_init_G1(h, pairing);
  element_init_G1(sig, pairing);
  element_init_GT(temp1, pairing);
  element_init_GT(temp2, pairing);
  element_init_Zr(secret_key, pairing);

  element_random(g);
  element_random(secret_key);
  element_pow_zn(public_key, g, secret_key);

  element_printf("The public key is %B\n", public_key);

  element_from_hash(h, hash, 30);
  element_pow_zn(sig, h, secret_key);

  pairing_apply(temp1, sig, g, pairing);
  pairing_apply(temp2, h, public_key, pairing);
  if(!element_cmp(temp1, temp2)){
	  printf("\nVerified\n");}
  else{
	  printf("\nNot verified\n");
  }
}
Exemplo n.º 2
0
int main(int argc, char **argv) {
  pairing_t pairing;
  double time1, time2;
  element_t P, a, b, c, Ka, Kb, Kc, t1, t2, t3, t4, t5, t6;
  pbc_demo_pairing_init(pairing, argc, argv);
  if (!pairing_is_symmetric(pairing)) pbc_die("pairing must be symmetric");

  element_init_G1(P, pairing);
  element_init_G1(t1, pairing);
  element_init_G1(t2, pairing);
  element_init_G1(t3, pairing);

  element_init_Zr(a, pairing);
  element_init_Zr(b, pairing);
  element_init_Zr(c, pairing);

  element_init_GT(t4, pairing);
  element_init_GT(t5, pairing);
  element_init_GT(t6, pairing);
  element_init_GT(Ka, pairing);
  element_init_GT(Kb, pairing);
  element_init_GT(Kc, pairing);

  time1 = pbc_get_time();
  printf("Joux key agreement between A, B and C.\n");
  element_random(P);
  element_random(a);
  element_random(b);
  element_random(c);
  element_mul_zn(t1, P, a);
  printf("A sends B and C: aP\n");
  element_printf("aP = %B\n", t1);
  element_mul_zn(t2, P, b);
  printf("B sends A and C: bP\n");
  element_printf("bP = %B\n", t2);
  element_mul_zn(t3, P, c);
  printf("C sends A and B: cP\n");
  element_printf("cP = %B\n", t3);

  element_pairing(t4, t2, t3);
  element_pow_zn(Ka, t4, a);
  element_printf("Ka = %B\n", Ka);
  element_pairing(t5, t1, t3);
  element_pow_zn(Kb, t5, b);
  element_printf("Kb = %B\n", Kb);
  element_pairing(t6, t1, t2);
  element_pow_zn(Kc, t6, c);
  element_printf("Kc = %B\n", Kc);

  printf("Shared key K = Ka = Kb = Kc\n");
  time2 = pbc_get_time();
  printf("All time = %fs\n", time2 - time1);


  element_clear(P);
  element_clear(a);
  element_clear(b);
  element_clear(c);
  element_clear(Ka);
  element_clear(Kb);
  element_clear(Kc);
  element_clear(t1);
  element_clear(t2);
  element_clear(t3);
  element_clear(t4);
  element_clear(t5);
  element_clear(t6);
  pairing_clear(pairing);

  return 0;
}
Exemplo n.º 3
0
int main(int argc, char **argv) {
  pairing_t pairing;
  pbc_demo_pairing_init(pairing, argc, argv);
  if (!pairing_is_symmetric(pairing)) pbc_die("pairing must be symmetric");
  double time1, time2;
  element_t P, Ppub, x, S, H, t1, t2, t3, t4;
  element_init_Zr(x, pairing);
  element_init_Zr(H, pairing);
  element_init_Zr(t1, pairing);

  element_init_G1(S, pairing);
  element_init_G1(P, pairing);
  element_init_G1(Ppub, pairing);
  element_init_G1(t2, pairing);

  element_init_GT(t3, pairing);
  element_init_GT(t4, pairing);

  printf("ZSS short signature schema\n");
  printf("KEYGEN\n");
  time1 = pbc_get_time();
  element_random(x);
  element_random(P);
  element_mul_zn(Ppub, P, x);
  element_printf("P = %B\n", P);
  element_printf("x = %B\n", x);
  element_printf("Ppub = %B\n", Ppub);

  printf("SIGN\n");
  element_from_hash(H, "Message", 7);
  element_add(t1, H, x);
  element_invert(t1, t1);
  element_mul_zn(S, P, t1);
  printf("Signature of message \"Message\" is:\n");
  element_printf("S = %B\n", S);

  printf("VERIFY\n");
  element_from_hash(H, "Message", 7);
  element_mul_zn(t2, P, H);
  element_add(t2, t2, Ppub);
  element_pairing(t3, t2, S);
  element_pairing(t4, P, P);
  element_printf("e(H(m)P + Ppub, S) = %B\n", t3);
  element_printf("e(P, P) = %B\n", t4);
  if (!element_cmp(t3, t4)) printf("Signature is valid\n");
  else printf("Signature is invalid\n");
  time2 = pbc_get_time();
  printf("All time = %fs\n", time2 - time1);
  element_clear(P);
  element_clear(Ppub);
  element_clear(x);
  element_clear(S);
  element_clear(H);
  element_clear(t1);
  element_clear(t2);
  element_clear(t3);
  element_clear(t4);
  pairing_clear(pairing);

  return 0;
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{  
  ///list all the files in the directory///
   DIR *d;
   FILE  *fpub, *fpriv, *fciph, *fplain, *ftag, *fpairing, *ftemp, *frand;//, *fp6, *fp7;
   paillier_pubkey_t *pub;
   paillier_prvkey_t *priv;
   paillier_get_rand_t get_rand;
   paillier_plaintext_t *plain;
   paillier_ciphertext_t *cipher, *cipher_copy;
   paillier_tag* tag;
   mpz_t tag_sig, *rand_prf;
   gmp_randstate_t rand;
   char *len;
   struct stat st= {0};
   unsigned char *data;
   int count=0, count1=0, gbytes, n, no_copies=10;
   struct dirent *dir;
   ///pairing parameters
   pairing_t pairing;
   //pairing_t p;
    //printf("setting pairing parameters\n");
    //pairing_init_set_str(pairing, param_str);
  // printf("after pairing setup\n");
   element_t g, h, u, temp_pow, test1, test2;
   element_t public_key, sig;
   element_t secret_key;
   ///end of pairing parameters
   //initialize pairing parametrs
   pbc_demo_pairing_init(pairing, argc, argv);
   element_init_G2(g, pairing);
   element_init_G1(u, pairing);
   element_init_G1(test1, pairing);
   element_init_G2(test2, pairing);
   element_init_G1(temp_pow, pairing);
   element_init_G2(public_key, pairing);
  // element_from_hash(h, "hashofmessage", 13);
   element_init_G1(h, pairing);
   element_init_G1(sig, pairing);
   element_init_Zr(secret_key, pairing);
   //end of pairing parameters initialization
   //set up pairing parameters
   //generate system parameters
   element_random(g);
  // n = pairing_length_in_bytes_x_only_G1(pairing);
  // data = pbc_malloc(n);
  // gbytes = pairing_length_in_bytes_G2(pairing);
  // printf(" \n g in bytes %d \n", gbytes);
  // element_printf("system parameter g = %B\n", g);
   //generate private key
   element_random(secret_key);
   //generate u
   element_random(u);
   //calculating hash of a file name and mapping it to element in group G1
  // element_from_hash(h, "FileName", 8);	
   element_random(h);
   //element_printf("private key = %B\n", secret_key);
   //compute corresponding public key
   element_pow_zn(public_key, g, secret_key);
   //element_printf("public key = %B\n", public_key);
   //end of setup
   tag = (paillier_tag*) malloc(sizeof(paillier_tag));
   plain = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t));
   cipher = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t));
   mpz_init(plain->m);
   mpz_init(tag->t);	
   mpz_init(cipher->c);
   mpz_init(tag_sig);	
   rand_prf = (mpz_t*) malloc(n*sizeof(mpz_t));
   
   len = (char *)malloc(2048*sizeof(char));
  //****paillier key generation****
   if(!(fpub = fopen("pub.txt", "r")))
    {
       //fputs("Not able to read public key file!\n", stderr);
       paillier_keygen(&pub, &priv, get_rand,450);
       //fclose(fpub);	
       fpub = fopen("pub.txt", "w");
       gmp_fprintf(fpub, "%Zd\n", pub->p); 
       gmp_fprintf(fpub, "%Zd\n", pub->q);	
       gmp_fprintf(fpub, "%Zd\n", pub->n_plusone);
       //***Writing private keys into a file***
       fpriv = fopen("priv.txt", "w"); 	
       gmp_fprintf(fpriv, "%Zd\n", priv->lambda);  		
       gmp_fprintf(fpriv, "%Zd\n", priv->x);  		
       fclose(fpriv);
       //****End of writing private key in a file***	
    }
   else
    {
        printf("\n in else");
	pub = (paillier_pubkey_t*) malloc(sizeof(paillier_pubkey_t));
	priv = (paillier_prvkey_t*) malloc(sizeof(paillier_prvkey_t));	
	mpz_init(pub->n_squared);
	mpz_init(pub->n);
	fgets(len, 1000, fpub);
   	mpz_init_set_str(pub->p, len, 10);
	fgets(len, 1000, fpub);
   	mpz_init_set_str(pub->q, len, 10);
	fgets(len, 1000, fpub);
   	mpz_init_set_str(pub->n_plusone, len, 10);
	//printf("value of nplusone : \n");
	//mpz_out_str(stdout, 10, pub->n_plusone);
	paillier_keygen(&pub, &priv, get_rand, 0);
        pub->bits = mpz_sizeinbase(pub->n, 2);	
    }
   fclose(fpub);
  //****end of paillier key generation****
  //printf("writing pairing parameters to a file\n");
  //writing pairing keys to file
  fpairing = fopen("pairing.txt", "w"); 
  
 /* n = pairing_length_in_bytes_compressed_G2(pairing);
  data = pbc_malloc(n);

  element_to_bytes_compressed(data, g);	
  element_printf(" decomp g %B\n", g);
  element_from_bytes_compressed(test2, data);
  element_printf(" decomp g %B\n", test2); */
  //writing compressed g to file
  element_fprintf(fpairing, "%B\n", g); 
//  element_printf(" g = %B\n", g);
  /*n = pairing_length_in_bytes_compressed_G1(pairing);
  data = pbc_malloc(n);
  element_to_bytes_compressed(data, u);	
  element_printf(" decomp g %B\n", u);
  element_from_bytes_compressed(test1, data);
  element_printf(" decomp g %B\n", test1);  
  //writing compressed u to file */
  element_fprintf(fpairing, "%B\n", u);
  //element_printf(" u = %B\n", u);
  //writing secret key to file
  element_fprintf(fpairing, "%B\n", secret_key); 
  //element_printf(" sk = %B\n", secret_key);
//  printf("secret key = %s\n",secret_key);	
 /* n = pairing_length_in_bytes_compressed_G2(pairing);
  data = pbc_malloc(n);
  element_to_bytes_compressed(data, public_key); 
  //writing compressed public key to file	*/ 
  element_fprintf(fpairing, "%B\n", public_key); 
  //element_printf("pk = %B\n", public_key);	
 /* n = pairing_length_in_bytes_compressed_G1(pairing);
  data = pbc_malloc(n);
  element_to_bytes_compressed(data, h);	
  element_printf(" decomp g %B\n", h);
  element_from_bytes_compressed(test1, data);
  element_printf(" decomp g %B\n", test1);  
  //writing compressed h to file */
  element_fprintf(fpairing, "%B\n", h);
  //element_printf("h = %B\n", h);
  //writing n to file
  gmp_fprintf(fpairing, "%Zd\n", pub->n);  		
  fclose(fpairing);
  //end of writing pairing keys to file  
  cipher_copy = (paillier_ciphertext_t*)malloc(no_copies*sizeof(paillier_ciphertext_t));
  frand = fopen("rand.txt","w");
  int i;
   init_rand(rand, get_rand, pub->bits / 8 + 1);
   for(i = 0; i< no_copies; i++)
   {
	mpz_init(rand_prf[i]);
	do
		mpz_urandomb(rand_prf[i], rand, pub->bits);
	while( mpz_cmp(rand_prf[i], pub->n) >= 0 );
	gmp_fprintf(frand, "%Zd\n", rand_prf[i]); 
	//printf("\nrandom : \n");
        //mpz_out_str(stdout, 10, rand_prf[i]);
   }
  fclose(frand);
  //****Opening files to read files and encrypt***** 
  d = opendir("./split");
   if (d)
   {
    while ((dir = readdir(d)) != NULL)
    {
     //printf("%s\n", dir->d_name);
     char fileName[1000], copy[1000];
     strcpy(fileName, "./split/");
     strcat(fileName,dir->d_name);	
     //printf("\nfile name %s", fileName);
     if(!(fplain = fopen(fileName, "r")))
      {
        printf("\n not able to read %s", fileName);
      //  fputs("not possible to read  file!\n", stderr);
	 count1++;
      }
      else
      {
	//printf("\n able to read %s", fileName);
	fgets(len, 2048, fplain);
        mpz_init_set_str(plain->m, len, 10);	
       // mpz_out_str(stdout, 10, plain->m);
	fclose(fplain);	
	//Writing cipher text to files
	strcpy(fileName, "./cipher/");
        //strcat(fileName,dir->d_name);	
        //printf("\nfilename %s",fileName);
        
         paillier_enc(tag, cipher_copy, pub,plain, get_rand, no_copies, rand_prf);
	// mpz_out_str(stdout, 10, tag->t);
	 int j;
         for(j=0;j < no_copies; j++)
         {
	    char num[20];
	    strcpy(copy, fileName);

	    sprintf(num, "copy%d/", (j+1));
	   // strcat(copy, );
	    strcat(copy, num);
	   if(stat(copy, &st) == -1)
	      mkdir(copy,0777);

            strcat(copy,dir->d_name);
            if(!(fciph = fopen(copy, "w")))
            {
	         printf("\nnot able to open file for writing cipher text %s", copy);
	    }
            else
            {
		// printf("\nbefore enc");
		
	        gmp_fprintf(fciph, "%Zd\n", cipher_copy[j].c); 	
                fclose(fciph); 	
	    }
         }	
	//writing tags to files
	strcpy(fileName, "./tag/");
        strcat(fileName,dir->d_name);	
        //printf("\nfilename %s",fileName);
        if(!(ftag = fopen(fileName, "w")))
        {
         printf("not able to open file for writing tag  %s", fileName);
        }
        else
        {
	
	 element_pow_mpz(temp_pow,u, tag->t);
	 element_mul(temp_pow, temp_pow, h);
	 element_pow_zn(sig, temp_pow, secret_key);
	 element_fprintf(ftag, "%B", sig);
	 fclose(ftag); 
        } 	
      }	
	count++;
    }	
   
    closedir(d);
   }
   
   printf("\nTotal number of files : %d, unreadable files %d", count, count1);
  
   return 0;
}
Exemplo n.º 5
0
int main(int argc, char **argv) {
  pairing_t pairing;
  element_t g1, u1, up1, g2, u2, up2, r;
  mpz_t r_mpz;
  element_pp_t g1_pp, g2_pp;
  double t0, t1;
  int i, n;

  printf("reading pairing from stdin...\n");
  pbc_demo_pairing_init(pairing, argc, argv);

  element_init(r, pairing->Zr);
  element_init(g1, pairing->G1);
  element_init(u1, pairing->G1);
  element_init(up1, pairing->G1);
  element_init(g2, pairing->G2);
  element_init(u2, pairing->G2);
  element_init(up2, pairing->G2);

  element_random(r);
  element_random(g1);
  element_random(g2);

  mpz_init(r_mpz);
  element_to_mpz(r_mpz, r);

  element_pp_init(g1_pp, g1);
  element_pp_init(g2_pp, g2);

  n = 100;
  t0 = pbc_get_time();
  for (i=0; i<n; i++) {
    element_pow_mpz(u1, g1, r_mpz);
  }
  t1 = pbc_get_time();
  printf("G1 exp:\t\t%fs\n", t1 - t0);

  n = 100;
  t0 = pbc_get_time();
  for (i=0; i<n; i++) {
    element_pow_mpz(u2, g2, r_mpz);
  }
  t1 = pbc_get_time();
  printf("G2 exp:\t\t%fs\n", t1 - t0);

  n = 100;
  t0 = pbc_get_time();
  for (i=0; i<n; i++) {
    element_pp_pow(up1, r_mpz, g1_pp);
  }
  t1 = pbc_get_time();
  printf("G1 pp exp:\t%fs\n", t1 - t0);

  n = 100;
  t0 = pbc_get_time();
  for (i=0; i<n; i++) {
    element_pp_pow(up2, r_mpz, g2_pp);
  }
  t1 = pbc_get_time();
  printf("G2 pp exp:\t%fs\n", t1 - t0);

  if (element_cmp(u1, up1)) {
    printf("Oops 1!\n");
  }
  if (element_cmp(u2, up2)) {
    printf("Oops 2!\n");
  }

  mpz_clear(r_mpz);
  element_clear(g1);
  element_clear(u1);
  element_clear(up1);
  element_clear(g2);
  element_clear(u2);
  element_clear(up2);
  element_clear(r);
  element_pp_clear(g1_pp);
  element_pp_clear(g2_pp);
  pairing_clear(pairing);

  return 0;
}
Exemplo n.º 6
0
int main(int argc, char **argv) {
	pairing_t pairing;
	element_t g1;
	element_t s, r;
	element_t id;
	element_t pri_id;
	element_t gID;
	element_t Ppub;
	element_t C1;
	element_t gIDr;
	
	pbc_demo_pairing_init(pairing, argc, argv);
	element_init_G1(g1, pairing);
	element_init_Zr(s, pairing);
	element_init_Zr(r, pairing);
	element_init_G1(id, pairing);
	element_init_G1(pri_id, pairing);
	element_init_GT(gID, pairing);
	element_init_G1(Ppub, pairing);
	element_init_G1(C1, pairing);
	element_init_GT(gIDr, pairing);
	
	double t0, t1, ttotal, ttotalpp;
	element_random(g1);
	element_random(s);
	element_random(r);
	element_random(id);
	element_pow_zn(Ppub, g1, s);
	ttotal = 0.0;
	t0 = pbc_get_time();
	element_pow_zn(pri_id, id, s);
	element_pairing(gID, id, Ppub);
	element_pow_zn(gIDr, gID, r);
	t1 = pbc_get_time();
	
	int i = 0;
	int n = 1000;
	
	for(i; i<n; i++)
	{	
		t0 = pbc_get_time();
		element_pow_zn(C1, g1, r);
		t1 = pbc_get_time();
		ttotal += t1 - t0;
		
	}
	printf("if we only have one user and the user is cached, average enc time = %f\n", ttotal / n);

	i = 0;
	ttotal = 0.0;
	for(i; i<n; i++)
	{	
		element_random(id);
		t0 = pbc_get_time();
		element_pow_zn(pri_id, id, s);
		element_pairing(gID, id, Ppub);
		element_pow_zn(gIDr, gID, r);
		element_pow_zn(C1, g1, r);
		t1 = pbc_get_time();
		ttotal += t1 - t0;
		
	}
	printf("100 diff users, average enc time = %f\n", ttotal / n);
	
	

	//dec
	ttotal = 0.0; 
	element_t gIDr_prime;
	element_init_GT(gIDr_prime, pairing);
		i = 0;
	
	for(i; i<n; i++)
	{	
		element_random(pri_id);
		element_random(C1);
		t0 = pbc_get_time();
		element_pairing(gIDr_prime, pri_id,  C1);
		t1 = pbc_get_time();
		ttotal += t1 - t0;
		
	}
	printf("dec random messages, average dec time = %f\n", ttotal / n);
	

	/*if (!element_cmp(gIDr_prime, gIDr)) {
    	printf("----------Confirm----------------\n");}
  	else{
    	printf("----------damn--------\n");
   	}*/
	
}