Esempio n. 1
0
struct crypto* init_pbc()
{
	struct crypto *pbc = calloc(1, sizeof(*pbc));
	int paramLength;
	size_t count;
	char *param;
	FILE *fp;

	fp = fopen("param", "rb");
	if (!fp) {
		perror("Cannot load pbc params");
		exit(EXIT_FAILURE);
	}

	fseek(fp, 0, SEEK_END);
	paramLength = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	param = calloc(paramLength, sizeof(param[0]));
	count = fread(param, 1, paramLength, fp);
	pairing_init_set_buf(pbc->pairing, param, count);
	free(param);
	fclose(fp);
	return pbc;
}
Esempio n. 2
0
void pairing_from_file(pairing_t pairing, char* inputFile){

   char param[2048] = {0};
   FILE* fp_param = fopen(inputFile, "r");

   int int_read_bytes = fread(param, 1, 2048, fp_param);
   pairing_init_set_buf(pairing, param, int_read_bytes);

   fclose(fp_param);
}
Esempio n. 3
0
void set_up_comm(int dm, int usr) {
  printf("[ setup communication ] \n");
  DM_num = 0;
  dms = (domain_manager* ) malloc(sizeof(domain_manager)* dm);
  user_num = 0;
  users = (user* ) malloc(sizeof(user)* usr);
  char param[1024];
  size_t count = fread(param, 1, 1024, stdin);
  if (!count) pbc_die("input error");
  pairing_init_set_buf(pairing, param, count);
}
Esempio n. 4
0
static inline void pbc_single_pairing_init(pairing_t pairing, int argc, char *argv) {
  char s[16384];
  FILE *fp = stdin;

  if (argc > 1) {
    fp = fopen(argv, "r");
    if (!fp) pbc_die("error opening %s", argv);
  }
  size_t count = fread(s, 1, 16384, fp);
  if (!count) pbc_die("input error");
  fclose(fp);

  if (pairing_init_set_buf(pairing, s, count)) pbc_die("pairing init failed");
}
Esempio n. 5
0
void setup_global_broadcast_params(global_broadcast_params_t *sys, int num_users)
{
  global_broadcast_params_t gbs;

  gbs = pbc_malloc(sizeof(struct global_broadcast_params_s));

  // Setup curve in gbp
  size_t count = strlen(PBC_PAIRING_PARAMS);
  if (!count) pbc_die("input error");
  if (pairing_init_set_buf(gbs->pairing, PBC_PAIRING_PARAMS, count))
    pbc_die("pairing init failed");

  gbs->num_users = num_users;
  element_t *lgs;
  int i;

  lgs = pbc_malloc(2 * num_users * sizeof(element_t));
  if(!(lgs)) {
    printf("\nMalloc Failed\n");
    printf("Didn't finish system setup\n\n");
  }
  //Set g as a chosen public value
  element_init(gbs->g, gbs->pairing->G1);
  i=element_set_str(gbs->g, PUBLIC_G, PBC_CONVERT_BASE);

  //Get alpha from Zp as mentioned in the paper
  element_init_Zr(gbs->alpha, gbs->pairing);
  element_random(gbs->alpha);   //pick random alpha value and later delete from memory
  //i=element_set_str(gbs->alpha, PRIVATE_ALPHA, PBC_CONVERT_BASE); //alpha is initialised as secret and later removed from memory

  //Make the 0th element equal to g^alpha
  element_init(lgs[0], gbs->pairing->G1);
  element_pow_zn(lgs[0],gbs->g, gbs->alpha);

  //Fill in the gs and the hs arrays
  for(i = 1; i < 2*num_users; i++) {
    //raise alpha to one more power
    element_init(lgs[i], gbs->pairing->G1);
    element_pow_zn(lgs[i], lgs[i-1], gbs->alpha);
  }
  element_clear(lgs[num_users]);  //remove g^(alpha^(n+1)) as it can leak info about parameters

  //For simplicity & so code was easy to read
  gbs->gs = lgs;
  *sys = gbs;
}
Esempio n. 6
0
void consumerShares(signed long int *codeword){
    pairing_t pairing;
    element_t g, r, a, e_g_g, share;
    char *argv = "./param/a.param";
    char s[16384];
    signed long int temp_share;
    FILE *fp = stdin;

    fp = fopen(argv, "r");
    if (!fp) 
        pbc_die("error opening %s\n", argv);
    size_t count = fread(s, 1, 16384, fp);
    if(!count) 
        pbc_die("read parameter failure\n");
    fclose(fp);
    if(pairing_init_set_buf(pairing, s, count)) 
        pbc_die("pairing init failed\n");
    if(!pairing_is_symmetric(pairing)) pbc_die("pairing is not symmetric\n");
    
    element_init_G1(g, pairing);
    element_init_Zr(r, pairing);
    element_init_Zr(a, pairing);
    element_init_Zr(share, pairing);
    element_init_GT(e_g_g, pairing);
    
    //find the generator of the group
    element_set(g, ((curve_data_ptr)((a_pairing_data_ptr)
    pairing->data)->Eq->data)->gen);
    element_random(r);
    element_random(a);
    //compute e(g, g)
    element_pairing(e_g_g, g, g);
    //compute e(g, g)^r
    element_pow_zn(e_g_g, e_g_g, r);
    //compute e(g,g)^ra
    element_pow_zn(e_g_g, e_g_g, a);
    temp_share = codeword[0];
    //transfer signed long int type ecret shares to an element_t type before we do the power of
    //e_g_g
    element_set_si(share, temp_share);
    element_pow_zn(e_g_g, e_g_g, share);
    
}
/*
 * Constructor
 */
BilinearMappingHandler::BilinearMappingHandler(char* ParamFilePath)
{
	char param[1024]; //the parameter file will be read here
	FILE* paramFile;

	//open the group-param file:
	paramFile = fopen ( ParamFilePath , "rb" );
	if (paramFile==NULL)
	{
		Quit(1);
	}

	//read the file:
	size_t count = fread(param, 1, 1024, paramFile);

	fclose(paramFile);  //close the file stream

	//use the file to initialize the pairing
	pairing_init_set_buf(pairing, param, count); //init the pairing (which also defines the groups)
}//end of constructor
Esempio n. 8
0
//This function sets the global broadcast parameters downloaded as a file from the server
//Sets up the gamma value, dont forget to randomize gamma and store locally later please
void setup_global_broadcast_params(global_broadcast_params_t *sys,
           unsigned char* gbs_header)
{
  global_broadcast_params_t gbs;
  gbs = pbc_malloc(sizeof(struct global_broadcast_params_s));
   // Setup curve in gbp
  size_t count = strlen(PBC_PAIRING_PARAMS);
  if (pairing_init_set_buf(gbs->pairing, PBC_PAIRING_PARAMS, count))
    pbc_die("pairing init failed");

  int num_users;
  memcpy(&num_users, gbs_header, 4);
  
  gbs->num_users = num_users;
  gbs_header= gbs_header+4;
  
  element_t *lgs;
  int i;
  lgs = pbc_malloc(2 * num_users * sizeof(element_t));

  //generate g from the file contents
  element_init(gbs->g, gbs->pairing->G1);
  gbs_header += in(gbs->g, gbs_header);

  //Fill in the gi values in lgs[]
  for(i = 0; i < 2*num_users; i++) {
    element_init(lgs[i], gbs->pairing->G1);
    if(i == num_users)
      continue;
    gbs_header += in(lgs[i], gbs_header);
  }

  element_init_Zr(gbs->gamma, gbs->pairing);  //initialise gamma
  element_random(gbs->gamma); //pick random value of gamma

  //For simplicity & so code was easy to read
  gbs->gs = lgs;
  *sys = gbs;

  return;
}
Esempio n. 9
0
int main(void)
{

	pairing_t pairing;
    char param[50000];
    size_t count = fread(param, 1, 50000, stdin);
    if (!count) pbc_die("input error");
    pairing_init_set_buf(pairing, param, count);

//    int cont = 0;

    struct timeval tvBegin, tvEnd;

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

    // Generating key
    element_random(g);
    element_random(secret_key);
    element_pow_zn(public_key, g, secret_key);

    // Generating message
    element_from_hash(h, "ABCDEF", 6);

    element_pow_zn(sig, h, secret_key);


    // RANDOM TESTS
    /*

    // Fp

    element_t p1, p2;
    element_init(p1, element_x(h)->field);
    element_init(p2, p1->field);
    element_random(p1);
    element_random(p2);

    // multiplication

    element_t puntos[2000];
    for(cont = 0; cont < 1000; cont++){
        element_init(puntos[cont], element_x(h)->field);
        element_init(puntos[2*cont], element_x(h)->field);
        element_random(puntos[cont]);
        element_random(puntos[2*cont]);
    }

    gettimeofday(&tvBegin, NULL);

    for(cont = 0; cont < 1000; cont++){
        element_mul(puntos[cont], puntos[cont], puntos[2*cont]);
    }

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);

    //square

    gettimeofday(&tvBegin, NULL);

    for(cont = 0; cont < 1000; cont++) element_square(puntos[cont], puntos[2*cont]);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);

    // add

    gettimeofday(&tvBegin, NULL);

    for(cont = 0; cont < 1000; cont++) element_add(puntos[cont], puntos[cont], puntos[2*cont]);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);

    // invers

    gettimeofday(&tvBegin, NULL);

    for(cont = 0; cont < 1000; cont++) element_invert(puntos[cont], puntos[2*cont]);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);






    // Fpk

    element_t q1, q2;
    element_init_GT(q1, pairing);
    element_init_GT(q2, pairing);
    element_random(q1);
    element_random(q2);

    // multiplication

    for(cont = 0; cont < 1000; cont++){
        element_init_GT(puntos[cont], pairing);
        element_init_GT(puntos[2*cont], pairing);
        element_random(puntos[cont]);
        element_random(puntos[2*cont]);
    }

    gettimeofday(&tvBegin, NULL);

    for(cont = 0; cont < 1000; cont++) {
        element_mul(puntos[cont], puntos[cont], puntos[2*cont]);
    }

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);

    //square

    gettimeofday(&tvBegin, NULL);

    for(cont = 0; cont < 1000; cont++) element_square(puntos[cont], puntos[cont]);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);

    // add

    gettimeofday(&tvBegin, NULL);

    for(cont = 0; cont < 1000; cont++){
        element_add(element_x(puntos[cont]), element_x(puntos[cont]), element_x(puntos[2*cont]));
        element_add(element_y(puntos[cont]), element_y(puntos[cont]), element_y(puntos[2*cont]));
    }

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);

    // invers

    gettimeofday(&tvBegin, NULL);

    for(cont = 0; cont < 1000; cont++) element_invert(puntos[cont], puntos[2*cont]);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);






    // CURVE OPERATIONS

    element_t punto, punto2;
    element_init(punto, h->field); element_random(punto);
    element_init(punto2, h->field); element_random(punto2);

    // add

    gettimeofday(&tvBegin, NULL);

    element_mul(punto, punto, punto2);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);

    // double

    gettimeofday(&tvBegin, NULL);

    element_double(punto, punto2);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1);










   // SIZE GROUP
    int m = mpz_sizeinbase(pairing->r, 2) - 2;
    printf("%i\n",  m);
    int contador = 0;
    for(;;){
        if(!m) break;
        if(mpz_tstbit(pairing->r,m)) contador++;
        m--;
    }
    printf("%i\n", contador);




*/


    // One pairing
    gettimeofday(&tvBegin, NULL);

    eval_miller(temp1, sig, g, pairing);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1000);

    //print_contador();



    // One pairing (with precomputed values)

    // Original method


    pairing_pp_t pp;
    // Precomp
    gettimeofday(&tvBegin, NULL);

    pairing_pp_init(pp, sig, pairing);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1000);

    // Eval
    gettimeofday(&tvBegin, NULL);

    pairing_pp_apply(temp1, g, pp);

    gettimeofday(&tvEnd, NULL);
    timeval_subtract(&tvEnd, &tvBegin, 1000);


    pairing_pp_clear(pp);



    void do_precomp(){
        lpoly *list;

        // precomputation
        gettimeofday(&tvBegin, NULL);

        list = lpoly_init();
        precompute(list, pairing->r, sig, g);

        gettimeofday(&tvEnd, NULL);
        timeval_subtract(&tvEnd, &tvBegin, 1000);

        // DMAX
        printf("%i\n", list->MAXD);

        // eval
        gettimeofday(&tvBegin, NULL);

        compute_miller(temp2, list, g, pairing);

        gettimeofday(&tvEnd, NULL);
        timeval_subtract(&tvEnd, &tvBegin, 1000);

        lpoly_free(list);
    }
Esempio n. 10
0
int pairing_init_set_str(pairing_t pairing, const char *s) {
  return pairing_init_set_buf(pairing, s, 0);
}
Esempio n. 11
0
File: bce.c Progetto: loiluu/weshare
void LoadParams(char *systemFileName,
		global_broadcast_params_t *gbp,
		broadcast_system_t *sys)
{

  if(!gbp) {
    printf("ACK!  You gave me no broadcast params!  I die.\n");
    return;
  }
  if(!gbp) {
    printf("ACK!  You gave me no broadcast system!  I die.\n");
    return;
  }
  if(!systemFileName) {
    printf("ACK!  You gave me no system filename!  I die.\n");
    return;
  }

  global_broadcast_params_t p;
  broadcast_system_t s;

  p = pbc_malloc(sizeof(struct global_broadcast_params_s));
  s = pbc_malloc(sizeof(struct broadcast_system_s));

  FILE *sysp = fopen(systemFileName, "r");
  if(!sysp) {
    printf("ACK! couldn't open %s  I die\n", systemFileName);
    return;
  }

  int leng;
  fread(&leng, 4, 1, sysp);
  p->pairFileName = (char *) pbc_malloc(leng);
  fread(p->pairFileName, 1, leng, sysp);
  FILE *params = fopen(p->pairFileName, "r");
  if(!params) {
    printf("ACK! couldn't open %s  I die\n", p->pairFileName);
    return;    
  }
  // pairing_init_inp_str(p->pairing, params);
  char _s[2048];
  size_t count = fread(_s, 1, 2048, params);
  if (!count) pbc_die("input error");
  if (pairing_init_set_buf(p->pairing, _s, count)) pbc_die("pairing init failed");

  fclose(params);

  //restore num_users
  fread(&(p->num_users),4,1, sysp); 

  //restore encr_prod
  element_init(s->encr_prod, p->pairing->G1);
  in(s->encr_prod, sysp);
  //element_out_str(stdout, 0, s->encr_prod);

  //restore pub_key
  element_init(s->pub_key, p->pairing->G1);
  in(s->pub_key, sysp);
  //element_out_str(stdout, 0, s->pub_key);

  //restore g 
  element_init(p->g, p->pairing->G1);
  in(p->g, sysp);


  p->gs = pbc_malloc(2 * p->num_users * sizeof(element_t));

  //restore gs
  int i;
  for(i = 0; i < 2*p->num_users; i++) {
    if(i == p->num_users) continue;
    element_init(p->gs[i], p->pairing->G1);
    in(p->gs[i], sysp);
  }  
    
  fclose(sysp);

  //now insert a dummy private key
  element_init_Zr(s->priv_key, p->pairing);
  
  *gbp = p;
  *sys = s;

  return;


}
int main(void){
    pairing_t pairing;
    element_t g, h, f, beta, beta_inverse;

    char s[16384];
    signed long int temp_share;
    FILE *fp = stdin;

    fp = fopen("../public/a.param", "r");
    if (!fp) 
        pbc_die("error opening parameter file", "r");
    size_t count = fread(s, 1, 16384, fp);
    if(!count) 
        pbc_die("read parameter failure\n");
    fclose(fp);
    if(pairing_init_set_buf(pairing, s, count)) 
        pbc_die("pairing init failed\n");
    if(!pairing_is_symmetric(pairing)) pbc_die("pairing is not symmetric\n");
        
    element_init_G1(g, pairing);
    element_init_G1(h, pairing);
    element_init_G1(f, pairing);
    element_init_Zr(beta, pairing);
    element_init_Zr(beta_inverse, pairing);

    //(G1, g, h, f) is the public key of authorizer
    //find the generator of the group
    element_set(g, ((curve_data_ptr)((a_pairing_data_ptr)\
    pairing->data)->Eq->data)->gen);
    element_random(beta);
    element_invert(beta_inverse, beta);
    //h = g^beta
    element_pow_zn(h, g, beta);
    //f = g^(1/beta)
    element_pow_zn(f, g, beta_inverse);
    fp = NULL;
    fp = fopen("./authorizer_public_keys.txt", "w+");
    if(!fp)
        pbc_die("error creating public key files");
    else{
        fprintf(fp, "g:");
        element_out_str(fp, 10, g);
        fprintf(fp, "\n\nh:");
        element_out_str(fp, 10, h);
        fprintf(fp, "\n\nf:");
        element_out_str(fp, 10, f);
        fclose(fp);
    }
    fp = fopen("./authorizer_secret_key.txt", "w+");
    if(!fp)
        pbc_die("error creating secret key files");
    else{
        fprintf(fp, "beta:");
        element_out_str(fp, 10, beta);
    }
    element_clear(g);
    element_clear(h);
    element_clear(f);
    element_clear(beta);
    element_clear(beta_inverse);
    return 1;
}