Пример #1
0
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;
}
Пример #2
0
int gen_ukeys(FENC_SCHEME_TYPE scheme, char *g_params, char *public_params, char *uskfile ,char *upkfile) {
	FENC_ERROR result;
	fenc_context context;
	fenc_group_params group_params;
	fenc_global_params global_params;
	pairing_t pairing;
	FILE *fp;
	int c;
	size_t serialized_len = 0;
	size_t pub_len = 0;
	uint8 public_params_buf[SIZE];
	uint8 *uskBuffer = NULL;
	uint8 *upkBuffer = NULL;
	fenc_USK_KSFCP usk;
	fenc_UPK_KSFCP upk;
	size_t usk_len = 0, b64_usk_len = 0;
	size_t upk_len = 0, b64_upk_len = 0;

	/* 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(&public_params_buf, 0, SIZE);
	memset(&usk, 0, sizeof(fenc_USK_KSFCP));
	memset(&upk, 0, sizeof(fenc_UPK_KSFCP));

	/* Initialize the library. */
	result = libfenc_init();
	report_error("Initializing library", result);

	// insert code here...
	debug("Generating master ABE system parameters...\n");
	/* Create a Sahai-Waters context. */
	result = libfenc_create_context(&context, scheme);

	/* Load group parameters from a file. */
	fp = fopen(g_params, "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 parameters file.\n");
		goto cleanup;
	}
	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);

	debug("Reading the public parameters file = %s\n", public_params);
	/* read file */
	fp = fopen(public_params, "r");
	if(fp != NULL) {
		while (TRUE) {
			c = fgetc(fp);
			if(c != EOF) {
				public_params_buf[pub_len] = c;
				pub_len++;
			}
			else {
				break;
			}
		}
	}
	else {
		perror("File does not exist.\n");
		return -1;
	}
	fclose(fp);

	//debug("public params input = '%s'\n", public_params_buf);

	/* base-64 decode */
	uint8 *bin_public_buf = NewBase64Decode((const char *) public_params_buf, pub_len, &serialized_len);
	/* Import the parameters from binary buffer: */
	result = libfenc_import_public_params(&context, bin_public_buf, serialized_len);
	report_error("Importing public parameters", result);

	result = libfenc_gen_ukey_KSFCP(&context, &usk, &upk);

	uskBuffer = (uint8 *) malloc(KEYSIZE_MAX);
	memset(uskBuffer, 0, KEYSIZE_MAX);
	upkBuffer = (uint8 *) malloc(KEYSIZE_MAX);
	memset(upkBuffer, 0, KEYSIZE_MAX);

	debug("Generating USK...\n");

	result = libfenc_export_usk_KSFCP(&context, &usk, uskBuffer, KEYSIZE_MAX, &usk_len);
	if(result != FENC_ERROR_NONE) {
		report_error("Generating USK error!", result);
		return result;
	}

	/* base-64 encode the key and write to disk */
	char *b64_usk_buf = NewBase64Encode(uskBuffer, usk_len, FALSE, &b64_usk_len);
	fp = fopen(uskfile, "w");
	if(fp != NULL) {
		fprintf(fp, "%s", b64_usk_buf);
	}
	fclose(fp);

	debug("Generating UPK...\n");

	result = libfenc_export_upk_KSFCP(&context, &upk, upkBuffer, KEYSIZE_MAX, &upk_len);
	if(result != FENC_ERROR_NONE) {
		report_error("Generating UPK error!", result);
		return result;
	}

	/* base-64 encode the key and write to disk */
	char *b64_upk_buf = NewBase64Encode(upkBuffer, upk_len, FALSE, &b64_upk_len);
	fp = fopen(upkfile, "w");
	if(fp != NULL) {
		fprintf(fp, "%s", b64_upk_buf);
	}
	fclose(fp);

cleanup:
	/* Destroy the context. */
	result = libfenc_destroy_context(&context);
	report_error("Destroying context", result);

	/* Shutdown the library. */
	result = libfenc_shutdown();
	report_error("Shutting down library", result);

	free(bin_public_buf);
	free(uskBuffer);
	free(upkBuffer);
	free(b64_usk_buf);
	free(b64_upk_buf);
	return 0;
}
Пример #3
0
void generate_keys(char *outfile, FENC_SCHEME_TYPE scheme, char *secret_params, char *public_params)
{
	FENC_ERROR result;
	fenc_context context;
	fenc_group_params group_params;
	fenc_global_params global_params;
	fenc_function_input func_object_input; // could be policy or list
	pairing_t pairing;
	fenc_key key;
	fenc_key key2;
	FILE *fp;
	char c;
	size_t pub_len = 0, sec_len = 0;
	size_t serialized_len = 0;
	uint8 public_params_buf[SIZE];
	uint8 secret_params_buf[SIZE];
	// char session_key[SESSION_KEY_LEN];
	uint8 output_str[SIZE];
	size_t output_str_len = 0;
	// size_t session_key_len;
	
	/* 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(&public_params_buf, 0, SIZE);
	memset(&secret_params_buf, 0, SIZE);
	memset(output_str, 0, SIZE);
	/* stores user's authorized attributes */
	memset(&func_object_input, 0, sizeof(fenc_function_input));
	/* stores the user's private key */
	memset(&key, 0, sizeof(fenc_key)); 
	memset(&key2, 0, sizeof(fenc_key));

	/* 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 parameters file.\n");
		return;
	}
	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);
		
	printf("Reading the public parameters file = %s\n", public_params);	
	/* read file */
	fp = fopen(public_params, "r");
	if(fp != NULL) {
		while (TRUE) {
			c = fgetc(fp);
			if(c != EOF) {
				public_params_buf[pub_len] = c;
				pub_len++;
			}
			else {
				break;
			}
		}
	}
	else {
		perror("File does not exist.\n");
		return;
	}
	fclose(fp);

	printf("Reading the secret parameters file = %s\n", secret_params);	
	/* read file */
	fp = fopen(secret_params, "r");
	if(fp != NULL) {
		while (TRUE) {
			c = fgetc(fp);
			if(c != EOF) {
				secret_params_buf[sec_len] = c;
				sec_len++;
			}
			else {
				break;
			}
		}
	}
	else {
		perror("File does not exist.\n");
		return;
	}	
	fclose(fp);
	
	printf("public params input = '%s'\n", public_params_buf);
	printf("secret params input = '%s'\n", secret_params_buf);
	
	/* base-64 decode */
	uint8 *bin_public_buf = NewBase64Decode((const char *) public_params_buf, pub_len, &serialized_len);
	/* Import the parameters from binary buffer: */
	result = libfenc_import_public_params(&context, bin_public_buf, serialized_len);
	report_error("Importing public parameters", result);

	uint8 *bin_secret_buf = NewBase64Decode((const char *) secret_params_buf, sec_len, &serialized_len);
	result = libfenc_import_secret_params(&context, bin_secret_buf, serialized_len, NULL, 0);
	report_error("Importing secret parameters", result);
	
	if(scheme == FENC_SCHEME_LSW) {
		fenc_create_func_input_for_policy(policy_string, &func_object_input);
		debug_print_policy((fenc_attribute_policy *)(func_object_input.scheme_input));
		free(policy_string);
	}
	else if(scheme == FENC_SCHEME_WATERSCP || scheme == FENC_SCHEME_WATERSSIMPLECP) {
		fenc_create_func_input_for_attributes(attribute_string, &func_object_input);
		debug_print_attribute_list((fenc_attribute_list*)(func_object_input.scheme_input));
		free(attribute_string);
	}
		
	result = libfenc_extract_key(&context, &func_object_input, &key);
	report_error("Extracting a decryption key", result);

	uint8 *buffer = malloc(KEYSIZE_MAX);
	memset(buffer, 0, KEYSIZE_MAX);	
	result = libfenc_export_secret_key(&context, &key, buffer, KEYSIZE_MAX, &serialized_len);
	report_error("Exporting key", result);
	
	size_t keyLength;
	char *secret_key_buf = NewBase64Encode(buffer, serialized_len, FALSE, &keyLength);
	// printf("Your secret-key:\t'%s'\nKey-len:\t'%zd'\n", secret_key_buf, serialized_len);	
	
	fp = fopen(outfile, "w");
	if(fp != NULL) {
		fprintf(fp, "%s", secret_key_buf);
	}
	else {
		perror("Error writing private key.");
	}
	fclose(fp);

	/*
	printf("........Confirming contents of exported key......\n");
	printf("Buffer contents:\n");
	print_buffer_as_hex(buffer, serialized_len);

	if(scheme == FENC_SCHEME_LSW) {
		result = libfenc_import_secret_key(&context, &key2, buffer, serialized_len);
		report_error("Import secret key", result);
	
		fenc_key_LSW *myKey2 = (fenc_key_LSW *) key2.scheme_key;
	
		size_t serialized_len2;
		uint8 *buffer2 = malloc(KEYSIZE_MAX);
		memset(buffer2, 0, KEYSIZE_MAX);
		result = libfenc_serialize_key_LSW(myKey2, buffer2, KEYSIZE_MAX, &serialized_len2);		
		report_error("Serialize user's key", result);
	
		printf("Key-len2: '%zu'\n", serialized_len2);
		printf("Buffer contents 2:\n");
		print_buffer_as_hex(buffer2, serialized_len2);
	} */
cleanup:
	fenc_func_input_clear(&func_object_input);
	
	/* Destroy the context. */
	result = libfenc_destroy_context(&context);
	report_error("Destroying context", result);

	/* Shutdown the library. */
	result = libfenc_shutdown();
	report_error("Shutting down library", result);	
		
	/* free buffer */
	free(buffer);
	return;
}
Пример #4
0
void apply_scheme(FENC_SCHEME_TYPE scheme, char *public_params, char *data, char *outfile) 
{
	FENC_ERROR result;
	fenc_context context;
	fenc_group_params group_params;
	fenc_global_params global_params;
	fenc_function_input func_input;
	fenc_ciphertext ciphertext;
	fenc_key master_key;
	pairing_t pairing;
	FILE *fp;
	char *public_params_buf = NULL, *scheme_text = NULL;
	char session_key[SESSION_KEY_LEN];
	fenc_plaintext rec_session_key;
	size_t serialized_len;
	clock_t start, stop;
	uint32 num_leaves;
	fenc_attribute_policy *parsed_policy = NULL;
	fenc_attribute_list *parsed_attributes = NULL;
	
	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(&master_key, 0, sizeof(fenc_key));
	
	/* Initialize the library. */
	result = libfenc_init();
	report_error("Initializing library", result);
	
	/* Create a Sahai-Waters context. */
	result = libfenc_create_context(&context, scheme);
	report_error("Creating context for Waters CP scheme", result);
	
	/* 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 type-d parameters file.\n");
		return;
	}
	fclose(fp);
	
	/* Set up the global parameters. */
	result = context.generate_global_params(&global_params, &group_params);	
	result = libfenc_gen_params(&context, &global_params);
	
	/* Set up the publci parameters */
	fp = fopen(public_params, "r");
	if(fp != NULL) {
		size_t pub_len = read_file(fp, &public_params_buf);
		/* base-64 decode */
		uint8 *bin_public_buf = NewBase64Decode((const char *) public_params_buf, pub_len, &serialized_len);
		/* Import the parameters from binary buffer: */
		result = libfenc_import_public_params(&context, bin_public_buf, serialized_len);
		report_error("Importing public parameters", result);
		free(public_params_buf);
		free(bin_public_buf);
	}
	else {
		perror("Could not open public parameters\n");
		return;
	}
	fclose(fp);
	
	if(scheme == FENC_SCHEME_LSW) {
		/* convert the list of attributes into a fenc_attribute_list structure */
		parsed_attributes = (fenc_attribute_list *) malloc(sizeof(fenc_attribute_list));		
		fenc_buffer_to_attribute_list(&data, parsed_attributes);
						
		func_input.input_type = FENC_INPUT_ATTRIBUTE_LIST;
		func_input.scheme_input = (void *) parsed_attributes;
		
		/* store attribute list for future reference */
		char attr_str[SIZE];
		memset(attr_str, 0, SIZE);
		size_t attr_str_len;
		fenc_attribute_list_to_buffer((fenc_attribute_list*)(func_input.scheme_input), (uint8 *)attr_str, SIZE, &attr_str_len);
		printf("Attribute list: %s\n", attr_str);
		
		/* test */
		num_leaves = 0;
		
	}
	else {
	//else if(scheme == FENC_SCHEME_WATERSCP) {
		/* encrypt under given policy */ 
	// fenc_attribute_policy *parsed_policy = construct_test_policy2();
		parsed_policy = (fenc_attribute_policy *) malloc(sizeof(fenc_attribute_policy));
		memset(parsed_policy, 0, sizeof(fenc_attribute_policy));

		fenc_policy_from_string(parsed_policy, data); 
		func_input.input_type = FENC_INPUT_NM_ATTRIBUTE_POLICY;
		func_input.scheme_input = (void *) parsed_policy;
		
		/* store the policy for future reference */
		char policy_str[SIZE];
		memset(policy_str, 0, SIZE);
		fenc_attribute_policy_to_string(parsed_policy->root, policy_str, SIZE);	
		printf("POLICY => '%s'\n", policy_str);	
	}
		
	/* perform encryption */
	result = libfenc_kem_encrypt(&context, &func_input, SESSION_KEY_LEN, (uint8 *) session_key, &ciphertext);	
	
	printf("Decryption key:\t");
	print_buffer_as_hex((uint8 *)session_key, SESSION_KEY_LEN);
	
	/* now perform decryption with session key */		
	
	if(scheme == FENC_SCHEME_LSW) {
		printf("Successful import => '%d'\n", get_key(kp_abe_priv_keyfile, &context, &master_key));
		fenc_key_LSW *key_LSW = (fenc_key_LSW *) master_key.scheme_key;
		num_leaves = prune_tree(key_LSW->policy->root, parsed_attributes);
		scheme_text = "KP";
	}
	else if(scheme == FENC_SCHEME_WATERSCP) {
		printf("Successful import => '%d'\n", get_key(cp_abe_priv_keyfile, &context, &master_key));
		fenc_key_WatersCP *key_WatersCP = (fenc_key_WatersCP *) master_key.scheme_key;	
		num_leaves = prune_tree(parsed_policy->root, &(key_WatersCP->attribute_list));
		scheme_text = "CP";
	}
	else {
		printf("Successful import => '%d'\n", get_key(scp_abe_priv_keyfile, &context, &master_key));
		fenc_key_WatersSimpleCP *key_WatersSimpleCP = (fenc_key_WatersSimpleCP *) master_key.scheme_key;	
		num_leaves = prune_tree(parsed_policy->root, &(key_WatersSimpleCP->attribute_list));	
		scheme_text = "SCP";
	}
	
	printf("Start timer.\n");
	/* start timer */
	start = clock();
	/* Descrypt the resulting ciphertext. */
	result = libfenc_decrypt(&context, &ciphertext, &master_key, &rec_session_key);	
	/* stop timer */
	stop = clock();
	printf("Stop timer.\n");
	double diff = ((double)(stop - start))/CLOCKS_PER_SEC;

	printf("Recovered session key:\t");
	print_buffer_as_hex(rec_session_key.data, rec_session_key.data_len);		
	
	if(memcmp(rec_session_key.data, session_key, rec_session_key.data_len) == 0) {
		printf("\nDECRYPTION TIME => %f secs.\n", diff);
		printf("NUMBER OF LEAVES => %d\n", num_leaves);		
		fp = fopen(outfile, "a");
		fprintf(fp, "%s:%d:%f\n", scheme_text, num_leaves, diff);
		fclose(fp);
	}
	
	if(parsed_attributes != NULL)
		free(parsed_attributes);
	if(parsed_policy != NULL)
		free(parsed_policy);
	/* Shutdown the library. */
	result = libfenc_shutdown();
	report_error("Shutting down library", result);		
}
Пример #5
0
void test_libfenc(char *policy)
{
	FENC_ERROR result;
	fenc_context context;
	fenc_group_params group_params;
	fenc_global_params global_params;
	fenc_function_input policy_input;
	pairing_t pairing;
	FILE *fp;
	char *public_params_buf = NULL;
	size_t serialized_len;
	
	memset(&context, 0, sizeof(fenc_context)); 
	memset(&group_params, 0, sizeof(fenc_group_params));
	memset(&global_params, 0, sizeof(fenc_global_params));	
	
	/* Initialize the library. */
	result = libfenc_init();
	report_error("Initializing library", result);
	
	/* Create a Sahai-Waters context. */
	result = libfenc_create_context(&context, FENC_SCHEME_WATERSCP);
	report_error("Creating context for Waters CP scheme", result);
	
	/* 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 type-d parameters file.\n");
		return;
	}
	fclose(fp);
	
	/* Set up the global parameters. */
	result = context.generate_global_params(&global_params, &group_params);	
	result = libfenc_gen_params(&context, &global_params);
	
	/* Set up the publci parameters */
	fp = fopen(PUBLIC_FILE".cp", "r");
	if(fp != NULL) {
		size_t pub_len = read_file(fp, &public_params_buf);
		/* base-64 decode */
		uint8 *bin_public_buf = NewBase64Decode((const char *) public_params_buf, pub_len, &serialized_len);
		/* Import the parameters from binary buffer: */
		result = libfenc_import_public_params(&context, bin_public_buf, serialized_len);
		report_error("Importing public parameters", result);
		free(public_params_buf);
		free(bin_public_buf);
	}
	else {
		perror("Could not open public parameters\n");
		return;
	}
	fclose(fp);
	
	/* encrypt under given policy */
	// fenc_attribute_policy *parsed_policy = construct_test_policy();
	fenc_attribute_policy *parsed_policy = (fenc_attribute_policy *) malloc(sizeof(fenc_attribute_policy));
	memset(parsed_policy, 0, sizeof(fenc_attribute_policy)); 
	
	fenc_policy_from_string(parsed_policy, policy);
	
	policy_input.input_type = FENC_INPUT_NM_ATTRIBUTE_POLICY;
	policy_input.scheme_input = (void *) parsed_policy;
	
	printf("START: test_secret_sharing\n");
	test_secret_sharing(parsed_policy, pairing);
	printf("END: test_secret_sharing\n");

	/* simple test */
	element_t ONE, TWO, THREE, ONEG2, TWOG2, THREEG2, ONEGT, TWOGT, FinalGT;
	element_init_G1(ONE, pairing);
	element_init_G1(TWO, pairing);
	element_init_G1(THREE, pairing);
	element_init_G2(ONEG2, pairing);
	element_init_G2(TWOG2, pairing);
	element_init_G2(THREEG2, pairing);
	element_init_GT(ONEGT, pairing);
	element_init_GT(TWOGT, pairing);
	element_init_GT(FinalGT, pairing);

	clock_t start, stop;
	double timeG1, timeG2, timePairing;
	element_random(ONE);
	element_random(TWO);
	element_random(ONEG2);
	element_random(TWOG2);
	// element_random(ONEGT);
	// element_random(TWOGT);
	
	/* time G1 */
	start = clock();
	element_mul(THREE, ONE, TWO);	
	stop = clock();
	timeG1 = ((double)(stop - start))/CLOCKS_PER_SEC;

	element_printf("THREEG1: %B, ", THREE);
	printf("G1 mul time: %f secs\n", timeG1);
	
	/* time G2 */
	start = clock();
	element_mul(THREEG2, ONEG2, TWOG2);	
	stop = clock();
	timeG2 = ((double)(stop - start))/CLOCKS_PER_SEC;
		
	element_printf("THREEG2: %B, ", THREEG2);
	printf("G2 mul time: %f secs\n", timeG2);
	
	/* time GT 
	start = clock();
	element_mul(FinalGT, ONEGT, TWOGT);	
	stop = clock();
	timeGT = ((double)(stop - start))/CLOCKS_PER_SEC;	

	element_printf("FinalGT: %B, ", FinalGT);
	printf("GT mul time: %f secs\n", timeGT); */
	
	/* time pairings */
	start = clock();
	pairing_apply(FinalGT, THREE, THREEG2, pairing);
	stop = clock();
	timePairing = ((double)(stop - start))/CLOCKS_PER_SEC;

	element_printf("Pairing: %B, ", FinalGT);
	printf("GT pairing time: %f secs\n", timePairing);
	
	free(parsed_policy);
	result = libfenc_shutdown();
	report_error("Shutting down library", result);	
}
Пример #6
0
void abe_encrypt(FENC_SCHEME_TYPE scheme, char *public_params, char *data, char *enc_file, int isXML, char *ext)
{
	FENC_ERROR result;
	fenc_context context;
	fenc_group_params group_params;
	fenc_global_params global_params;
	fenc_ciphertext ciphertext;
	fenc_function_input func_object_input;
	pairing_t pairing;
	FILE *fp;
	char c;
	size_t pub_len = 0;
	size_t serialized_len = 0;
	uint8 public_params_buf[SIZE];
	char session_key[SESSION_KEY_LEN];
	// size_t session_key_len;
	char pol_str[MAX_POLICY_STR];
	int pol_str_len = MAX_POLICY_STR;
	/* 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(&public_params_buf, 0, SIZE);
	memset(&ciphertext, 0, sizeof(fenc_ciphertext));
	memset(pol_str, 0, pol_str_len);
	
	/* 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.\n");
		return;
	}
	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);
	
	debug("Reading the public parameters file = %s\n", public_params);
	/* read file */
	fp = fopen(public_params, "r");
	if(fp != NULL) {
		while (TRUE) {
			c = fgetc(fp);
			if(c != EOF) {
				public_params_buf[pub_len] = c;
				pub_len++;
			}
			else {
				break;
			}
		}
	}
	else {
		perror("File does not exist.\n");
		return;
	}
	fclose(fp);
		
	if(scheme == FENC_SCHEME_LSW) {
		fenc_create_func_input_for_attributes(attribute_string, &func_object_input);
		debug_print_attribute_list((fenc_attribute_list*)(func_object_input.scheme_input));
		free(attribute_string);
	}
	else if(scheme == FENC_SCHEME_WATERSCP) {
		fenc_create_func_input_for_policy(policy_string, &func_object_input);
		debug_print_policy((fenc_attribute_policy *)(func_object_input.scheme_input));
		free(policy_string);
	}
	
	/* base-64 decode */
	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);
	
	/* key encapsulation to obtain session key from policy */
	result = libfenc_kem_encrypt(&context, &func_object_input, SESSION_KEY_LEN, (uint8 *)session_key, &ciphertext);	
	
	/* generated PSK from policy string */
	debug("Generated session key: ");
	print_buffer_as_hex((uint8 *) session_key, SESSION_KEY_LEN);

	/* encrypted blob that belongs in the <ABED></ABE> tags */
	// print_buffer_as_hex(ciphertext.data, ciphertext.data_len);
		
	/* use the PSK to encrypt using openssl functions here */
	AES_KEY key;
	size_t iv_length;
	uint8 iv[AES_BLOCK_SIZE+1];
	int data_len = (int) ceil((strlen(data) + strlen(MAGIC))/(double)(AES_BLOCK_SIZE)) * AES_BLOCK_SIZE; // round to nearest multiple of 16-bytes
	char aes_ciphertext[data_len], data_magic[data_len];
	
	/* generate a random IV */
	memset(iv, 0, AES_BLOCK_SIZE);
	RAND_bytes((uint8 *) iv, AES_BLOCK_SIZE);
	debug("IV: ");
	print_buffer_as_hex((uint8 *) iv, AES_BLOCK_SIZE);
	char *iv_base64 = NewBase64Encode(iv, AES_BLOCK_SIZE, FALSE, &iv_length);

	memset(aes_ciphertext, 0, data_len);
	AES_set_encrypt_key((uint8 *) session_key, 8*SESSION_KEY_LEN, &key);
	sprintf(data_magic, MAGIC"%s", data);
	debug("\nEncrypting data...\n");
	debug("\tPlaintext is => '%s'.\n", data);
	
	AES_cbc_encrypt((uint8 *)data_magic, (uint8 *) aes_ciphertext, data_len, &key, (uint8 *) iv, AES_ENCRYPT);
	// printf("\tAES Ciphertext base 64: ");
	// print_buffer_as_hex((uint8 *) aes_ciphertext, data_len);
	
	char filename[strlen(enc_file)+1];
	memset(filename, 0, strlen(enc_file));
	uint8 *rand_id[BYTES+1];
	if(isXML) {
		sprintf(filename, "%s.%s.xml", enc_file, ext);
		fp = fopen(filename, "w");
		/* generate the random unique id */
		RAND_bytes((uint8 *) rand_id, BYTES);
		debug("Generated random id: %08x\n", (unsigned int) rand_id[0]);
	}
	else {
		sprintf(filename, "%s.%s", enc_file, ext);
		fp = fopen(filename, "w");
	}
	debug("\tCiphertext stored in '%s'.\n", filename);
	debug("\tABE Ciphertex size is: '%zd'.\n", ciphertext.data_len);
	debug("\tAES Ciphertext size is: '%d'.\n", data_len);

	/* base-64 both ciphertexts and write to the stdout -- in XML? */
	size_t abe_length, aes_length;
	char *ABE_cipher_base64 = NewBase64Encode(ciphertext.data, ciphertext.data_len, FALSE, &abe_length);
	char *AES_cipher_base64 = NewBase64Encode(aes_ciphertext, data_len, FALSE, &aes_length);
	
	/* output ciphertext to disk: either xml or custom format */
	if(isXML) {
		fprintf(fp,"<Encrypted id='");
		fprintf(fp, "%08x", (unsigned int) rand_id[0]);
		fprintf(fp,"'><ABE type='CP'>%s</ABE>", ABE_cipher_base64);
		fprintf(fp,"<IV>%s</IV>", iv_base64);
		fprintf(fp,"<EncryptedData>%s</EncryptedData></Encrypted>", AES_cipher_base64);
		fclose(fp);
	}
	else {
		fprintf(fp, ABE_TOKEN":%s:"ABE_TOKEN_END":", ABE_cipher_base64);
		fprintf(fp, IV_TOKEN":%s:"IV_TOKEN_END":", iv_base64);
		fprintf(fp, AES_TOKEN":%s:"AES_TOKEN_END, AES_cipher_base64);
		fclose(fp);
	}
		
	if(ABE_cipher_base64 != NULL)
		free(ABE_cipher_base64);
	if(ABE_cipher_base64 != NULL)
		free(AES_cipher_base64);
	if(iv_base64 != NULL)
		free(iv_base64);
	
	fenc_func_input_clear(&func_object_input);
	
	/* Shutdown the library. */
	result = libfenc_shutdown();
	report_error("Shutting down library", result);
	return;
}