Example #1
0
CK_RV init_token_data(CK_SLOT_ID slot_id)
{
	CK_RV rc;

	memset((char *)nv_token_data, 0, sizeof(nv_token_data));

	// the normal USER pin is not set when the token is initialized
	//
	memcpy(nv_token_data->user_pin_sha, "00000000000000000000",
	       SHA1_HASH_SIZE);
	memcpy(nv_token_data->so_pin_sha, default_so_pin_sha, SHA1_HASH_SIZE);

	memset(user_pin_md5, 0x0, MD5_HASH_SIZE);
	memcpy(so_pin_md5, default_so_pin_md5, MD5_HASH_SIZE);

	memcpy(nv_token_data->next_token_object_name, "00000000", 8);

	// generate the master key used for signing the Operation State information
	//                          `
	memset(nv_token_data->token_info.label, ' ',
	       sizeof(nv_token_data->token_info.label));
	memcpy(nv_token_data->token_info.label, label, strlen((char *)label));

	nv_token_data->tweak_vector.allow_weak_des = TRUE;
	nv_token_data->tweak_vector.check_des_parity = FALSE;
	nv_token_data->tweak_vector.allow_key_mods = TRUE;
	nv_token_data->tweak_vector.netscape_mods = TRUE;

	init_tokenInfo();

	if (token_specific.t_init_token_data) {
		rc = token_specific.t_init_token_data(slot_id);
		if (rc != CKR_OK)
			return rc;
	} else {
		//
		// FIXME: erase the token object index file (and all token objects)
		//
		rc = generate_master_key(master_key);
		if (rc != CKR_OK) {
			TRACE_DEVEL("generate_master_key failed.\n");
			return CKR_FUNCTION_FAILED;
		}

		rc = save_masterkey_so();
		if (rc != CKR_OK) {
			TRACE_DEVEL("save_masterkey_so failed.\n");
			return rc;
		}
	}

	rc = save_token_data(slot_id);

	return rc;
}
Example #2
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;
} 
Example #3
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;
}