Bool abe_decrypt(FENC_SCHEME_TYPE scheme, char *public_params, char *inputfile, char *keyfile) { FENC_ERROR result; fenc_context context; fenc_group_params group_params; fenc_global_params global_params; fenc_ciphertext ciphertext; fenc_plaintext aes_session_key; pairing_t pairing; fenc_key secret_key; FILE *fp; char c; int pub_len = 0; size_t serialized_len = 0; char public_params_buf[SIZE]; int magic_failed; /* Clear data structures. */ memset(&context, 0, sizeof(fenc_context)); memset(&group_params, 0, sizeof(fenc_group_params)); memset(&global_params, 0, sizeof(fenc_global_params)); memset(&ciphertext, 0, sizeof(fenc_ciphertext)); memset(&aes_session_key, 0, sizeof(fenc_plaintext)); memset(&secret_key, 0, sizeof(fenc_key)); // all this memory must be free'd char *input_buf = NULL,*keyfile_buf = NULL; char *aes_blob64 = NULL, *abe_blob64 = NULL, *iv_blob64 = NULL; ssize_t input_len, key_len; /* Load user's input file */ fp = fopen(inputfile, "r"); if(fp != NULL) { if((input_len = read_file(fp, &input_buf)) > 0) { // printf("Input file: %s\n", input_buf); tokenize_inputfile(input_buf, &abe_blob64, &aes_blob64, &iv_blob64); debug("abe ciphertext = '%s'\n", abe_blob64); debug("init vector = '%s'\n", iv_blob64); debug("aes ciphertext = '%s'\n", aes_blob64); free(input_buf); } } else { fprintf(stderr, "Could not load input file: %s\n", inputfile); return FALSE; } fclose(fp); /* make sure the abe and aes ptrs are set */ if(aes_blob64 == NULL || abe_blob64 == NULL || iv_blob64 == NULL) { fprintf(stderr, "Input file either not well-formed or not encrypted.\n"); return FALSE; } /* Initialize the library. */ result = libfenc_init(); /* Create a Sahai-Waters context. */ result = libfenc_create_context(&context, scheme); /* Load group parameters from a file. */ fp = fopen(PARAM, "r"); if (fp != NULL) { libfenc_load_group_params_from_file(&group_params, fp); libfenc_get_pbc_pairing(&group_params, pairing); } else { perror("Could not open "PARAM" parameters file"); return FALSE; } fclose(fp); /* Set up the global parameters. */ result = context.generate_global_params(&global_params, &group_params); report_error("Loading global parameters", result); result = libfenc_gen_params(&context, &global_params); report_error("Generating scheme parameters and secret key", result); /* read public parameters file */ fp = fopen(public_params, "r"); if(fp != NULL) { while (TRUE) { c = fgetc(fp); if(c != EOF) { // statically allocated to prevent memory leaks public_params_buf[pub_len] = c; pub_len++; } else { break; } } } else { fprintf(stderr, "Could not load input file: %s\n", public_params); return FALSE; } fclose(fp); debug("public params input = '%s'\n", public_params_buf); /* base-64 decode public parameters */ uint8 *bin_public_buf = NewBase64Decode((const char *) public_params_buf, pub_len, &serialized_len); // printf("public params binary = '%s'\n", bin_public_buf); /* Import the parameters from binary buffer: */ result = libfenc_import_public_params(&context, bin_public_buf, serialized_len); report_error("Importing public parameters", result); /* read input key file */ // (PRIVATE KEY) debug("keyfile => '%s'\n", keyfile); fp = fopen(keyfile, "r"); if(fp != NULL) { if((key_len = read_file(fp, &keyfile_buf)) > 0) { // printf("\nYour private-key:\t'%s'\n", keyfile_buf); size_t keyLength; uint8 *bin_keyfile_buf = NewBase64Decode((const char *) keyfile_buf, key_len, &keyLength); /* base-64 decode user's private key */ debug("Base-64 decoded buffer:\t"); print_buffer_as_hex(bin_keyfile_buf, keyLength); result = libfenc_import_secret_key(&context, &secret_key, bin_keyfile_buf, keyLength); report_error("Importing secret key", result); free(keyfile_buf); } } else { fprintf(stderr, "Could not load input file: %s\n", keyfile); /* clear allocated possibly allocated memory */ return FALSE; } fclose(fp); size_t abeLength; uint8 *data = NewBase64Decode((const char *) abe_blob64, strlen(abe_blob64), &abeLength); ciphertext.data = data; ciphertext.data_len = abeLength; ciphertext.max_len = abeLength; /* Descrypt the resulting ciphertext. */ result = libfenc_decrypt(&context, &ciphertext, &secret_key, &aes_session_key); report_error("Decrypting the ciphertext", result); debug("Decrypted session key is: "); print_buffer_as_hex(aes_session_key.data, aes_session_key.data_len); /* decode the iv_blob64 */ size_t ivLength; char *ivec = NewBase64Decode((const char *) iv_blob64, strlen(iv_blob64), &ivLength); debug("IV: "); print_buffer_as_hex((uint8 *) ivec, AES_BLOCK_SIZE); /* decode the aesblob64 */ size_t aesLength; char *aesblob = NewBase64Decode((const char *) aes_blob64, strlen(aes_blob64), &aesLength); /* use the PSK to encrypt using openssl functions here */ AES_KEY sk; char aes_result[aesLength+1]; AES_set_decrypt_key((uint8 *) aes_session_key.data, 8*SESSION_KEY_LEN, &sk); memset(aes_result, 0, aesLength+1); AES_cbc_encrypt((uint8 *) aesblob, (uint8 *) aes_result, aesLength, &sk, (uint8 *) ivec, AES_DECRYPT); /* base-64 both ciphertext and write to the stdout -- in XML? */ char magic[strlen(MAGIC)+1]; memset(magic, 0, strlen(MAGIC)+1); strncpy(magic, aes_result, strlen(MAGIC)); if(strcmp(magic, MAGIC) == 0) { debug("Recovered magic: '%s'\n", magic); debug("Plaintext: %s\n", (char *) (aes_result + strlen(MAGIC))); magic_failed = FALSE; } else { fprintf(stderr, "ERROR: ABE decryption unsuccessful!!\n"); magic_failed = TRUE; } /* free allocated memory */ free(aesblob); free(aes_blob64); free(ivec); free(iv_blob64); free(data); free(abe_blob64); /* Destroy the context. */ result = libfenc_destroy_context(&context); report_error("Destroying the encryption context", result); /* Shutdown the library. */ result = libfenc_shutdown(); report_error("Shutting down library", result); return magic_failed; }
int tfel_decrypt_cp(char *inputfile, char *keyfile, char *output) { int i; FILE *fp; //pubkey input //共通 tfel_pubkey *pubkey = NULL; pubkey = (tfel_pubkey*)malloc(sizeof(tfel_pubkey)); if (pubkey == NULL) return -1; tfel_param_G *param_G; param_G = (tfel_param_G*)malloc(sizeof(tfel_param_G)); if (param_G == NULL) { tfel_clear_pk(pubkey->num_att, pubkey); free(pubkey); return -1; } memset(param_G, 0, sizeof(tfel_param_G)); pairing_init(param_G->p, CURVE); importpub(pubkey, *param_G); //pubkey input //sk_Gamma input //CP secretkey sk_Gamma; sk_Gamma.sk_Delta = (attribute_set*)malloc(sizeof(attribute_set)); sk_Gamma.sk_AS = NULL; sk_Gamma.sk_kStar = (basis*)malloc(sizeof(basis)); import_sk(sk_Gamma.sk_Delta, sk_Gamma.sk_kStar, *param_G, keyfile); //sk_Gamma input //ciphertext input //共通 char *input_buf = NULL;//,*keyfile_buf = NULL; char *aes_blob64 = NULL, *abe_blob64 = NULL, *iv_blob64 = NULL; ssize_t input_len; size_t key_len; fp = fopen(inputfile, "r"); if(fp != NULL) { if((input_len = read_file(fp, &input_buf)) > 0) { tokenize_inputfile(input_buf, &abe_blob64, &aes_blob64, &iv_blob64); if(aes_blob64 == NULL || abe_blob64 == NULL || iv_blob64 == NULL) { fprintf(stderr, "Input file either not well-formed or not encrypted.\n"); fclose(fp); return -1; } free(input_buf); } } else { fprintf(stderr, "Could not load input file: %s\n", inputfile); return FALSE; } fclose(fp); //ciphertext input //decode ciphertext //共通 tfel_ciphertext ct; memset(&ct, 0, sizeof(tfel_ciphertext)); size_t abeLength; unsigned char *data = NewBase64Decode((const char *) abe_blob64, strlen(abe_blob64), &abeLength); ct.data = data; ct.data_len = abeLength; ct.max_len = abeLength; //decode ciphertext //deserialize ciphertext /*printf("abe_blob64 = %zd\n", strlen(abe_blob64)); printf("abeLength = %zd\n", abeLength); printf("ct.data = %s\n", ct.data); printf("ct.data_len = %zd\n", ct.data_len); printf("ct.max_len = %zd\n", ct.max_len);*/ basis *c_i = NULL; c_i = (basis*)malloc(sizeof(basis)); if (c_i == NULL){ //error処理 } AccessStructure *AS = NULL; AS = (AccessStructure*)malloc(sizeof(AccessStructure)); tfel_deserialize_ciphertext_cp(AS, c_i, ct.data, ct.max_len, param_G->p); //deserialize ciphertext //decode iv size_t ivLength; char *ivec = NewBase64Decode((const char *) iv_blob64, strlen(iv_blob64), &ivLength); //debug("IV: "); //print_buffer_as_hex((uint8 *) ivec, AES_BLOCK_SIZE); //decode iv //decode aes /* decode the aesblob64 */ size_t aesLength; char *aesblob = NewBase64Decode((const char *) aes_blob64, strlen(aes_blob64), &aesLength); //printf("sizeof(aesblob) = %zd\n", aesLength); //decode aes //拡大係数行列のチェック printf("check\n"); AccessStructure *aAS; aAS = check_attribute_to_matrix(sk_Gamma.sk_Delta, AS); //errorならNULLを返す //alpha_iを生成 mpz_t order; mpz_init(order); mpz_set(order, *pairing_get_order(param_G->p)); mpz_t *alpha_i; mpz_t temp; mpz_init(temp); Element *K; Element *temp_E; Element temp_0; element_init(temp_0, param_G->p->g3); alpha_i = calc_alpha_i(aAS, order); rho_i *r_ptr; r_ptr = aAS->rho; int t; //search_tで使用 for (i = 0; i < aAS->num_policy+1; i++) { //generate K if (i == 0) { K = pairing_c_k(param_G->p, NULL, c_i->M[0], sk_Gamma.sk_kStar->M[0], NULL); } else { t = search_t(r_ptr, sk_Gamma.sk_Delta); //r_ptrからtを持ってくる関数 temp_E = pairing_c_k(param_G->p, r_ptr, c_i->M[i], sk_Gamma.sk_kStar->M[t], &alpha_i[i-1]); //temp_E = pairing_c_k(param_G->p, r_ptr, c_i->M[t], sk_Gamma.sk_kStar->M[i], &alpha_i[i-1]); element_mul(temp_0, *temp_E, *K); element_set(*K, temp_0); element_clear(*temp_E); free(temp_E); r_ptr = r_ptr->next; } } //printf("K generate\n"); //Kのバイト列を16ビットのハッシュに key_len = element_get_oct_length(*K); // size of K unsigned char *K_oct; K_oct = (unsigned char*)malloc(sizeof(unsigned char)*key_len); element_to_oct(K_oct, &key_len, *K); // bytes of K unsigned char *session_key; session_key = (unsigned char *)malloc(sizeof(unsigned char)*16); //int d_len; hash_to_bytes(K_oct, key_len, SESSION_KEY_LEN, session_key, 2); //Kのバイト列を16ビットのハッシュに //復号 AES_KEY sk; //char aes_result[aesLength+1]; char *aes_result; aes_result = (char*)malloc(sizeof(char)*aesLength+1); AES_set_decrypt_key((uint8 *) session_key, 8*SESSION_KEY_LEN, &sk); memset(aes_result, 0, aesLength+1); AES_cbc_encrypt((uint8 *) aesblob, (uint8 *) aes_result, aesLength, &sk, (uint8 *) ivec, AES_DECRYPT); char magic[strlen(MAGIC)+1]; memset(magic, 0, strlen(MAGIC)+1); strncpy(magic, aes_result, strlen(MAGIC)); if(strcmp(magic, MAGIC) == 0) { //printf("Recovered magic: '%s'\n", magic); //printf("Plaintext: %s\n", (char *) (aes_result + strlen(MAGIC))); if ((fp = fopen(output, "w")) == NULL) { fprintf(stderr, "output open error\n"); } else { fprintf(fp, "%s\n", (char*)(aes_result + strlen(MAGIC))); fclose(fp); } //magic_failed = FALSE; } else { printf("error decryption\n"); //printf(stderr, "ERROR: ABE decryption unsuccessful!!\n"); //magic_failed = TRUE; } //復号 free(aesblob); free(aes_blob64); free(ivec); free(iv_blob64); free(data); free(abe_blob64); free(aes_result); //free(&input_buf); free(session_key); free(K_oct); element_clear(*K); free(K); if (aAS != NULL) { for (i = 0; i < aAS->S->row; i++) { mpz_clear(alpha_i[i]); } free(alpha_i); AccessStructure_clear(aAS);//error } element_clear(temp_0); mpz_clear(order); mpz_clear(temp); for (i = 0; i < c_i->dim; i++) { //memsetとか追加? free(c_i->M[i]); } free(c_i->M); free(c_i); Spanprogram_clear(AS->S); free(AS); //AccessStructure_clear(AS);//すでにAS->rhoがAccessStructure_clear(aAS)により解放されているため return 0; }