static void fq_out_info(FILE *out, field_ptr f) { field_ptr fbase = f->data; element_fprintf(out, "extension x^2 + %B, base field: ", fq_nqr(f)); field_out_info(out, fbase); }
static void fp_out_info(FILE * out, field_ptr f) { element_fprintf(out, "GF(%Zd): Montgomery representation", f->order); }
void wSetup(char *string,int attrNo,pairing_t *pairing, MSP *msp){ int count = 0;//the index of the attribute array /* if(!strcmp(string,"ordinary")){ setupOrdinaryPairing(pairing);//setup pairing first printf("Use ordinary curve...\n"); }else if(!strcmp(string,"singular")){ setupSingularPairing(pairing);//setup pairing first printf("Use singular curve...\n"); }else{ fprintf(stderr,"Wrong input arguments!"); fprintf(stderr,"Please input <./wAbe><sinuglar> or <./wAbe><ordinary>\n"); } */ element_t g;//the generator of G element_init_G2(g,*pairing);//initial the generator g element_random(g); /* initial the random group elements h_1...h_attrNo which belog to G and are associated with the attrNo attributes in the system. */ element_t h; element_init_G2(h,*pairing); //initial the h element_t alpha; element_t a; //initial the alpha and a in Z_p element_init_Zr(alpha,*pairing); element_init_Zr(a,*pairing); element_random(alpha); element_random(a); //public key e(g,g)^alpha element_t pubKey; element_t gAlpha; element_t gA; element_init_GT(pubKey,*pairing);//initial the publicKey element_init_G2(gAlpha,*pairing);//initial the gAlpha element_init_G2(gA,*pairing);//initial the gA element_pow_zn(gAlpha,g,alpha);//gAlpha=g^alpha element_pow_zn(gA,g,a);//gA=g^a weilPairing(&pubKey,g,gAlpha,*pairing);//publicKey = e(g,g^alpha) = e(g,g)^alpha //Master secret key element_t msk; element_init_G2(msk,*pairing); element_set(msk,gAlpha);//msk = g^alpha //write the master key and public key to file FILE* fG = fopen("publicKey/g.key","w");//file pointer to the public key g FILE* fGA = fopen("publicKey/gA.key","w");//file pointer to the public key gA FILE* fPub = fopen("publicKey/eGG.key","w");//file pointer to the public key e(g,gALPHA) FILE* fH;//file pointer the the attribute key FILE* fMsk = fopen("MSK/msk.key","w");//file pointer to the master key element_fprintf(fG,"%B\n",g); element_fprintf(fPub,"%B\n",pubKey); element_fprintf(fGA,"%B\n",gA); count = 0; char hCmd[100];//the command line for the pointer of FILE* fH char attrName[2];//the name of attribute memset(hCmd,'\0',100); memset(attrName,'\0',2); strcpy(hCmd,"publicKey/h"); while(count!=attrNo){ sprintf(attrName,"%c",msp->label[count]); strcat(hCmd,attrName); strcat(hCmd,".key"); fH = fopen(hCmd,"w"); element_random(h); element_fprintf(fH,"%B",h); memset(hCmd,'\0',100); strcpy(hCmd,"publicKey/h"); memset(attrName,'\0',2); fclose(fH); count++; } element_clear(h); element_fprintf(fMsk,"%B\n",msk); //close the file pointer and clear all the element fclose(fG); fclose(fGA); fclose(fPub); fclose(fMsk); element_clear(g); element_clear(a); element_clear(alpha); element_clear(gAlpha); element_clear(gA); element_clear(pubKey); element_clear(msk); }//end of setup
static void zp_out_info(FILE * out, field_ptr f) { element_fprintf(out, "GF(%Zd), GMP wrapped", f->order); }
int main(int argc, char *argv[]) { DIR *d; FILE *fpub, *fciph, *fplain, *fprod, *fkey; paillier_pubkey_t *pub; paillier_plaintext_t *plain, *plain_sum, *plain_temp; paillier_prvkey_t *priv; paillier_get_rand_t get_rand; paillier_ciphertext_t *cipher, *cipher_prod, *cipher_total; int count = 0, count1=0, val = 163000, no_of_copies = 10; long int key_int, key_int1, key_rnd; struct dirent *dir; char* len; mpz_t rndKey, randNum, rnd,rnd_sum, rndno, sum, pl_sum; /* Hold our random numbers */ mpz_t rndBnd; /* Bound for mpz_urandomm */ mpz_t key, key1; //key for PRF mpz_t plain_total, plain_nomod; gmp_randstate_t gmpRandState, gmpRandState1, gmpRandState2; /* Random generator state object */ mpz_init(sum); mpz_init(pl_sum); mpz_init(rnd); mpz_init(rndno); mpz_init(rndBnd); mpz_init(randNum); mpz_init(key); mpz_init(key1); mpz_init(rnd_sum); mpz_init(plain_total); mpz_init(rnd); mpz_init(rndKey); mpz_init(plain_nomod); mpz_set_ui(plain_total, 0); plain = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); plain_sum = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); plain_temp = (paillier_plaintext_t*) malloc(sizeof(paillier_plaintext_t)); cipher_prod = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); cipher_total = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); cipher = (paillier_ciphertext_t*) malloc(sizeof(paillier_ciphertext_t)); len = (char *)malloc(2048*sizeof(char)); //mpz_init(cipher_prod->c); mpz_init(cipher_total->c); mpz_set_ui(rnd_sum, 0); mpz_init_set_str(cipher_prod->c, "1", 10); mpz_init_set_str(cipher_total->c, "1", 10); mpz_init_set_str(plain_sum->m, "0", 10); // printf("\nloading pailler keys"); if((fpub = fopen("pub.txt", "r"))) { 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); } gmp_randinit_default(gmpRandState); mpz_set_str(rndBnd, "163000", 10); mpz_set_ui(rndKey, time(NULL)); gmp_randseed(gmpRandState, rndKey); fkey = fopen("prf_key.txt", "w"); mpz_urandomm(rndno, gmpRandState, pub->n); mpz_set(key, rndno); element_fprintf(fkey, "%Zd\n", key); mpz_urandomm(rndno, gmpRandState, pub->n); mpz_set(key1, rndno); element_fprintf(fkey, "%Zd", key1); fclose(fkey); gmp_randseed(gmpRandState, key); //mpz_out_str(stdout, 10, rnd); //****Opening files to read files and encrypt***** int i, j; mpz_t rand_val[val], rand_init[no_of_copies], mpz_result[no_of_copies]; char fileName[1000], file[1000]; //strcpy(fileName, "./cipher/copy3/output"); mpz_set_str(sum, "0", 10); mpz_set_str(pl_sum, "0", 10); for(j =0; j < no_of_copies; j++) { mpz_init(rand_init[j]); mpz_urandomm(rand_init[j], gmpRandState, pub->n); mpz_add(sum, sum, rand_init[j]); // mpz_mod(sum, sum, pub->n); //printf("\n 1st rand \n"); //mpz_out_str(stdout, 10, rand_init[j]); } // printf("\nsum\n"); // mpz_out_str(stdout, 10, sum); mpz_set_str(pl_sum, "0", 10); gmp_randseed(gmpRandState, key1); for(j =0; j < no_of_copies; j++) { mpz_set_ui(cipher_prod->c, 1); for(i = 0; i < val; i++) { sprintf(fileName, "./cipher/copy%d/output", (j+1)); if(j == 0) { do mpz_urandomm(randNum, gmpRandState, rndBnd); while(mpz_cmp_ui(randNum,0) ==0); mpz_init(rand_val[i]); mpz_set(rand_val[i], randNum); //printf("\n2ndrand\n"); //mpz_out_str(stdout, 10, randNum); // printf("random\n"); // mpz_out_str(stdout, 10, rand_val[i]); } else { mpz_set(randNum, rand_val[i]); } // sprintf(num, "output%d.txt", (j+1)); mpz_get_str(file, 10, randNum); strcat(fileName, file); strcat(fileName,".txt"); count = 0; // printf("\n%s", fileName); //mpz_out_str(stdout, 10, randNum); if(!(fciph = fopen(fileName, "r"))) { printf("\n not able to read %s", fileName); // fputs("not possible to read file!\n", stderr); } else { fgets(len, 1000, fciph); mpz_init_set_str(cipher->c, len, 10); //if(strstr(dir->d_name, "output") != NULL) //{ // printf("\ncipher\n"); //mpz_out_str(stdout, 10, cipher->c); paillier_mul(pub, cipher_prod, cipher_prod, cipher); // printf("\ncipher after product\n"); //mpz_out_str(stdout, 10, cipher_prod->c); //} fclose(fciph); } if(j == 0) { strcpy(fileName, "./split/output"); strcat(fileName, file); strcat(fileName,".txt"); if(!(fplain = fopen(fileName, "r"))) { printf("\n not read %s", fileName); count1++; } else { fgets(len, 1000, fplain); // printf("\npleain sum\n"); // mpz_out_str(stdout, 10, plain_sum->m); mpz_init_set_str(plain->m, len, 10); // if(strstr(dir->d_name, "output") != NULL) //{ mpz_add(plain_sum->m, plain_sum->m, plain->m); //mpz_mod(pla in_sum->m, plain_sum->m, pub->n); //} fclose(fplain); } } } mpz_powm(cipher_prod->c, cipher_prod->c, rand_init[j], pub->n_squared); paillier_mul(pub, cipher_total, cipher_total, cipher_prod); mpz_mul(plain_temp->m, plain_sum->m, rand_init[j]); mpz_add(plain_total, plain_total, plain_temp->m); mpz_mod(plain_total, plain_total, pub->n); } plain = paillier_dec(plain, pub, priv, cipher_total); printf("\n decrypt \n"); mpz_out_str(stdout, 10, plain->m); // printf("\n plain total \n"); //mpz_out_str(stdout, 10, plain_total); printf("\n plain text total\n"); mpz_out_str(stdout, 10, plain_sum->m); mpz_mul(pl_sum, plain_sum->m, sum); mpz_mul(pl_sum, pl_sum, pub->n); mpz_mul(pl_sum, pl_sum, priv->lambda); // mpz_mod(pl_sum, pl_sum, pub->n); printf("\n plain text * rnd\n"); mpz_out_str(stdout, 10, pl_sum); if((fprod = fopen("result/cipher_result.txt", "w"))) { printf("\nWriting the result to file \n"); gmp_fprintf(fprod, "%Zd\n", cipher_total->c); fclose(fprod); } return 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; }
int main(int argc, char* argv[]) { QTextStream err(stderr, QIODevice::WriteOnly); if(argc != 2) { err << "Usage: " << argv[0] << " qbits\n"; return 1; } int qbits; QTextStream in(argv[1], QIODevice::ReadOnly); in >> qbits; if(qbits < 10) { err << "qbits must be greater than 10\n"; return 1; } QTextStream out(stdout, QIODevice::WriteOnly); out << "--- PBC Parameter Utility ---\n"; out << "r < q (for prime r and q)\n"; out << "Bits: " << qbits << "\n"; out << "\n\n"; out.flush(); pbc_param_t params; pairing_t pairing; const int rbits = qbits-8; pbc_param_init_a_gen(params, rbits, qbits); pbc_param_out_str(stdout, params); pairing_init_pbc_param(pairing, params); element_t gen1; element_t neg1; element_t gent; element_t tmp, tmp2; element_init_G1(tmp, pairing); element_init_G1(tmp2, pairing); element_init_Zr(neg1, pairing); element_init_G1(gen1, pairing); element_init_G1(gent, pairing); // neg1 = 1 element_set1(neg1); // neg1 = -1 mod r element_neg(neg1, neg1); do { element_random(gen1); // tmp = gen1^-1 element_pow_zn(tmp, gen1, neg1); // tmp = (gen1^-1)*gen1 == gen1^r element_mul(tmp2, tmp, gen1); } while (!element_is1(tmp2)); element_fprintf(stdout, "g1 = %B\n", gen1); do { element_random(gent); // tmp = gen1^-1 element_pow_zn(tmp, gent, neg1); // tmp = (gen1^-1)*gen1 == gen1^r element_mul(tmp2, tmp, gent); } while (!element_is1(tmp2)); element_fprintf(stdout, "gT = %B\n", gent); element_clear(gen1); element_clear(gent); element_clear(tmp); element_clear(tmp2); element_clear(neg1); pbc_param_clear(params); pairing_clear(pairing); return 0; }