Пример #1
0
int main (int argc, char *argv[])
{
	int c;
	int flag = FALSE;
	int yr;
	int mn;
	int dt;
	int hr;
	int min;
	int sec;

	char datestr[21] = {0};
	size_t len = 0;
	size_t str_len;
	ssize_t read;
	char *passwd = NULL;
	char *tweak_str = NULL;
	char *filename = NULL;

	clock_t start;
	clock_t end;
	double cpu_time;
	time_t t_of_day;
	time_t time_t_data;
	uint32_t raw_data;
	uint32_t encrypted_data;
	uint32_t time;
	struct tm t;
	FILE * stream = NULL;
	static unsigned char orig_key[32] = {0};

	fnr_expanded_key *key = NULL;
	fnr_expanded_tweak tweak;

	if (argc != ARGS_LIST_COUNT) {
		fprintf (stderr, "Usage: ./test/timestamptest -p <password> -t <tweak> "
				"-f <filename>\n");
		return FAILURE;
	}

	while ((c = getopt (argc, argv, "p:t:f:")) != -1) {
		switch (c) {
			case 'p':
				passwd = optarg;
				break;

			case 't':
				tweak_str = optarg;
				break;

			case 'f':
				filename = optarg;
				break;

			default:
				fprintf (stderr, "Usage: ./test/timestamptest -p <password> -t <tweak> "
						"-f <filename>");
				return FAILURE;
		}
	}

	stream = fopen (filename, "r");
	if (stream == NULL) {
		perror ("fopen");
		exit (EXIT_FAILURE);
	}

	/* Init */
	FNR_init ();	

	if (generate_master_key (passwd, (char *) orig_key) == 0) {
		fprintf (stderr, "Key derivation function failed\n");
		fclose (stream);
		return FAILURE;
	}

	key = FNR_expand_key (orig_key, AES_KEY_SIZE, 
			NUM_BITS * sizeof (t_of_day));
	if (key == NULL) {
		fprintf (stderr, "Error expanding key\n");
		fclose (stream);
		return FAILURE;
	}

	FNR_expand_tweak (&tweak, key, (void *) tweak_str, strlen (tweak_str));

	start = clock();
	if (start == -1) {
		flag = TRUE;
	}

	printf ("\n");
	while (fgets (datestr, sizeof (datestr), stream) != NULL) {
		len = strlen (datestr);
		if ((len != 0) && (datestr[len - 1] == '\n')) {
			datestr[len - 1] = '\0';
			if (sscanf (datestr, "%d-%d-%dT%d:%d:%d", 
						&yr, &mn, &dt, &hr, &min, &sec) != 6) {
				fprintf (stderr, "sscanf didn't read six elements\n");
				continue;
			}
			t.tm_year = yr - 1900;
			t.tm_mon = mn - 1;
			t.tm_mday = dt;
			t.tm_hour = hr;
			t.tm_min = min;
			t.tm_sec = sec;
			t.tm_isdst = 0;

			t_of_day = mktime (&t);
			if (t_of_day == -1) {
				perror ("mktime");
				fclose (stream);
				return FAILURE;
			}
			printf ("Date: %s", ctime (&t_of_day));
			printf ("Epoch value: %ld\n", t_of_day);

			time = htonl ((uint32_t) t_of_day);
			printf ("htonl (t_of_day) = %" PRIu32 "\n", time);

			raw_data = time;
			FNR_encrypt (key, &tweak, &raw_data, &encrypted_data);
			printf ("Encrypted data: %" PRIu32 "\n", encrypted_data);
			time_t_data = (time_t) encrypted_data;
			printf ("Encrypted date string: %s", ctime (&time_t_data));

			FNR_decrypt (key, &tweak, &encrypted_data, &raw_data);
			printf ("Decrypted data: %" PRIu32 "\n", raw_data);
			raw_data = ntohl (raw_data);
			printf ("htonl (raw_data)  = %" PRIu32 "\n", raw_data);
			time_t_data = (time_t) raw_data;
			printf ("Date: %s\n", ctime (&time_t_data));
		} else {
			break;
		}
	}
	
	end = clock();
	if ((end != -1) && (flag == FALSE)) {
		cpu_time = ((double) (end - start)) / CLOCKS_PER_SEC;
		printf ("cpu time used: %.3f seconds.\n\n", cpu_time);
	} else {
		printf ("Could not calculate processor time.\n\n");
	}
	
	fclose (stream);
	FNR_release_key (key);
	FNR_shut ();
	return SUCCESS;
} 
Пример #2
0
int main(int argc, char * argv[]) {
    static unsigned char orig_key[16] = {0};
    char *passwd = NULL , *tweak_str = NULL , *filename = NULL, ip_str[16] = {0};
    int c, ret ;
    FILE * f;
    unsigned int *p_raw_addr = NULL, raw_addr, encrypted_addr;
    unsigned int num_ip_addresses = 0, loop_count = 0;
    char *no_ip_str;
#ifdef DEBUG
    char *ip_address_str = NULL;
#endif

    if (argc != 7) {
        fprintf(stderr, "usage: ipv4test -p passwd -t tweak -f raw-trace-file\n");
        exit(-1);
    }

    while ((c = getopt (argc, argv, "p:t:f:")) != -1) {
        switch (c)
        {
        case 'p':
            passwd = optarg;
            break;
        case 't':
            tweak_str = optarg;
            break;
        case 'f':
            filename = optarg; /* Could be used when inputs are read from file */
            break;
        case '?':
            if (optopt == 'c')
                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
            else if (isprint (optopt))
                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
            else
                fprintf (stderr,
                         "Unknown option character `\\x%x'.\n",
                         optopt);
            return 1;
        default:
            abort ();
        }
    }

#ifdef DEBUG
    printf ("%s, %s, %s\n", passwd, tweak_str, filename);
    printf("-----------------------------------------------------------\n");
#endif
    if ((f = fopen(filename,"r")) == NULL) {
        fprintf(stderr,"Cannot open file %s\n", argv[1]);
        exit(-2);
    }

    /* Init */
    FNR_init();

    /* Initialize the keys and tweaks */
    generate_master_key(passwd,orig_key);
    fnr_expanded_key *key = FNR_expand_key(orig_key, 128, 32);
    if (!key) {
        fprintf( stderr, "Error expanding key\n" );
        return 0;
    }

    fnr_expanded_tweak tweak;
    FNR_expand_tweak(&tweak, key, (void*)tweak_str, strlen(tweak_str));

    fgets(no_ip_str, 10, f);
    num_ip_addresses = atoi(no_ip_str);
    p_raw_addr = (unsigned int *)malloc(sizeof(int)* num_ip_addresses);
    if(NULL == p_raw_addr) {
        fprintf(stderr,"Cannot allocate memory for %d IP addresses\n", num_ip_addresses);
        exit(-3);
    }

    while(fgets(ip_str, 16 , f) != NULL) {
        p_raw_addr[loop_count] = ipv4_rank(ip_str);
        memset(ip_str, 0, 16);
        ++loop_count;
    }

    loop_count = 0;

    clock_t start, end;
    double cpu_time_used;

    //Performance test start
    start = clock();
    while(loop_count < num_ip_addresses) {

        encrypted_addr = raw_addr = p_raw_addr[loop_count];

#ifdef DEBUG
        ip_address_str = ipv4_derank(raw_addr);
        printf("Input\t\t%s\n", ip_address_str);
        free(ip_address_str);
#endif

        FNR_encrypt(key, &tweak, &raw_addr, &encrypted_addr);

#ifdef DEBUG
        ip_address_str = ipv4_derank(encrypted_addr);
        printf("Ciphertext\t%s\n", ip_address_str);
        free(ip_address_str);
#endif

        FNR_decrypt(key, &tweak, &encrypted_addr, &raw_addr);

#ifdef DEBUG
        ip_address_str = ipv4_derank(raw_addr);
        printf("Plaintext\t%s\n", ip_address_str);
        free(ip_address_str);
        printf("-----------------------------------------------------------\n");

        if(raw_addr != p_raw_addr[loop_count]) {
            printf("ERROR: decrypt output does not match the user input\n");
            exit(-4);
        }
#endif

        ++loop_count;
    }

    end = clock();
    cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    //Performance test end

    //printf("CPU time used %f\n", cpu_time_used);
    printf("%d , %f\n",num_ip_addresses, cpu_time_used);
    free(p_raw_addr);
    FNR_release_key(key);
    FNR_shut();
    return 0;
}