Пример #1
0
int main ()
{
    init ();
    fips_test ();

    ecb_test();
    cbc_test();
    cts_test();

    return 0;
}
Пример #2
0
int main ()
{
#ifdef CAMELLIA
    init ();
    fips_test ();

    ecb_test();
    cbc_test();
    cts_test();
#endif

    return 0;
}
Пример #3
0
/* The beginning of everything */
int main(int argc, char **argv)
{
	struct user_options user_ops;		/* holds user configuration data     */
	struct rand_pool_info *rand = NULL;	/* structure to pass entropy (IOCTL) */
	int random_fd = 0;			/* output file descriptor            */
	int random_hw_fd = 0;			/* input file descriptor             */
	int ent_count;				/* current system entropy            */
	int write_size;				/* max entropy data to pass          */
	struct pollfd fds[1];			/* used for polling file descriptor  */
	int ret;
	int exitval = 0;

	/*Hold minimal permissions, just enough to get IOCTL working*/
	if(0 != qrng_update_cap()){
		log_print(ERROR, "qrngd permission reset failed, exiting\n");
		exitval = 1;
		goto exit;
	}

	/* set default parameters */
	user_ops.run_as_daemon = TRUE;
	strcpy(user_ops.input_device_name, RANDOM_DEVICE_HW);
	strcpy(user_ops.output_device_name, RANDOM_DEVICE);

	/* display application header */
	title();

	/* get user preferences */
	ret = get_user_options(&user_ops, argc, argv);
	if (ret < 0) {
		usage();
		exitval = 1;
		goto exit;
	}

	/* open hardware random device */
	random_hw_fd = open(user_ops.input_device_name, O_RDONLY);
	if (random_hw_fd < 0) {
		fprintf(stderr, "Can't open hardware random device file %s\n", user_ops.input_device_name);
		exitval = 1;
		goto exit;
	}

	/* open random device */
	random_fd = open(user_ops.output_device_name, O_RDWR);
	if (random_fd < 0) {
		fprintf(stderr, "Can't open random device file %s\n", user_ops.output_device_name);
		exitval = 1;
		goto exit;
	}

	/* allocate memory for ioctl data struct and buffer */
	rand = malloc(sizeof(struct rand_pool_info) + MAX_ENT_POOL_WRITES);
	if (!rand) {
		fprintf(stderr, "Can't allocate memory\n");
		exitval = 1;
		goto exit;
	}

	/* setup poll() data */
	memset(fds, 0 , sizeof(fds));
	fds[0].fd = random_fd;
	fds[0].events = POLLOUT;

	/* run as daemon if requested to do so */
	if (user_ops.run_as_daemon) {
		fprintf(stderr, "Starting daemon.\n");
		if (daemon(0, 0) < 0) {
			fprintf(stderr, "can't daemonize: %s\n", strerror(errno));
			exitval = 1;
			goto exit;
		}
#ifndef ANDROID_CHANGES
		openlog(APP_NAME, 0, LOG_DAEMON);
#endif
	}

	/* log message */
	log_print(INFO, APP_NAME " has started:\n" "Reading device:'%s' updating entropy for device:'%s'",
		  user_ops.input_device_name,
		  user_ops.output_device_name);

	/* main loop to get data from hardware and feed RNG entropy pool */
	while (1) {

		/* Check for empty buffer and fill with hardware random generated numbers */
		if (buffsize == 0) {
			/* fill buffer with random data from hardware */
			ret = read_src(random_hw_fd, databuf, MAX_BUFFER);
			if (ret < 0) {
				log_print(ERROR, "ERROR: Can't read from hardware source.");
				exitval = 1;
				goto exit;
			}
			/* run FIPS test on buffer, if buffer fails then ditch it and get new data */
			ret = fips_test(databuf, MAX_BUFFER);
			if (ret < 0) {
				buffsize = 0;
				log_print(INFO, "ERROR: Failed FIPS test.");
			}
			/* everything good, reset buffer variables to indicate full buffer */
			else {
				buffsize = MAX_BUFFER;
				curridx  = 0;
			}
		}
		/* We should have data here, if not then something bad happened above and we should wait and try again */
		if (buffsize == 0) {
			log_print(ERROR, "ERROR: Timeout getting valid random data from hardware.");
			usleep(100000);	/* 100ms */
			continue;
		}

		/* Get current entropy pool size in bits and convert to bytes */
		if (ioctl(random_fd, RNDGETENTCNT, &ent_count) != 0) {
			log_print(ERROR, "ERROR: Can't read entropy count.");
			exitval = 1;
			goto exit;
		}
		/* convert entropy bits to bytes */
		ent_count >>= 3;

		/* fill entropy pool */
		write_size = min(buffsize, MAX_ENT_POOL_WRITES);

		/* Write some data to the device */
		rand->entropy_count = write_size * 8;
		rand->buf_size      = write_size;
		memcpy(rand->buf, &databuf[curridx], write_size);
		curridx  += write_size;
		buffsize -= write_size;

		/* Issue the ioctl to increase the entropy count */
		if (ioctl(random_fd, RNDADDENTROPY, rand) < 0) {
			log_print(ERROR,"ERROR: RNDADDENTROPY ioctl() failed.");
			exitval = 1;
			goto exit;
		}

		/* Wait if entropy pool is full */
		ret = poll(fds, 1, -1);
		if (ret < 0) {
			log_print(ERROR,"ERROR: poll call failed.");
			/* wait if error */
			usleep(100000);
		}
	}

exit:
	/* free other resources */
	if (rand)
		free(rand);
	if (random_fd)
		close(random_fd);
	if (random_hw_fd)
		close(random_hw_fd);
	return exitval;
}