Parameterize the number of random bytes

Allow to specify the number of random bytes to obtain from a single
parameter on the command line.
pull/1/head
Damien Goutte-Gattat 8 years ago
parent c0d2a6762d
commit 396d092668
  1. 38
      src/scdrand.c

@ -45,7 +45,15 @@
static void
usage(int status)
{
puts("Usage: scdrand\n");
printf("Usage: scdrand [options] [BYTES]\n\
Read the specified number of random bytes from a smartcard\n\
and feed them to the kernel entropy pool. If BYTES is not\n\
specified, the default is %d bytes.\n\n", DEFAULT_RANDOM_BYTES);
puts("Options:\n\
-h, --help Display this help message.\n\
-v, --version Display the version message.\n\
");
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
@ -278,10 +286,25 @@ start_privileged_process(void)
return 0; /* Not reached. */
}
static int
get_uinteger_or_die(const char *arg)
{
unsigned val;
char *endptr;
errno = 0;
val = strtoul(arg, &endptr, 10);
if ( errno != 0 || endptr == arg )
errx(EXIT_FAILURE, "Invalid argument, unsigned integer expected: %s", arg);
return val;
}
int
main(int argc, char **argv)
{
int c, fd;
unsigned nbytes;
char *scd_socket_name;
unsigned char random_buffer[MAX_RANDOM_BYTES];
@ -292,6 +315,7 @@ main(int argc, char **argv)
};
setprogname(argv[0]);
nbytes = DEFAULT_RANDOM_BYTES;
while ( (c = getopt_long(argc, argv, "hv", options, NULL)) != -1 ) {
switch ( c ) {
@ -311,6 +335,16 @@ main(int argc, char **argv)
fd = start_privileged_process();
if ( optind < argc ) {
nbytes = get_uinteger_or_die(argv[optind++]);
if ( nbytes == 0 ) /* Funny guy? */
exit(EXIT_FAILURE);
if ( nbytes > MAX_RANDOM_BYTES )
nbytes = MAX_RANDOM_BYTES;
}
#ifndef GPG_ERR_INITIALIZED
gpg_err_init();
#endif
@ -320,7 +354,7 @@ main(int argc, char **argv)
if ( scd_socket_name ) {
int n, i;
n = get_challenge(scd_socket_name, random_buffer, DEFAULT_RANDOM_BYTES);
n = get_challenge(scd_socket_name, random_buffer, nbytes);
write(fd, random_buffer, n);
free(scd_socket_name);

Loading…
Cancel
Save