Skip to content

Commit

Permalink
fixup! Augment rand argument parsing to allow scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
nhorman committed Nov 7, 2023
1 parent d761c55 commit bd3b659
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
91 changes: 51 additions & 40 deletions apps/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,67 +99,78 @@ int rand_main(int argc, char **argv)
if (argc == 1) {
int factoridx = 0;
int shift = 0;

/*
* iterate over the value and check to see if there are
* any non-numerical chars
* A non digit suffix indicates we need to shift the
* number of requested bytes by a factor of:
* k = 1024^1 (1 << (10 * 1))
* m = 1024^2 (1 << (10 * 2))
* g = 1024^3 (1 << (10 * 3))
* t = 1024^4 (1 << (10 * 4))
* which can be achieved by bit-shifting the number
* special case for requesting the max allowed
* number of random bytes to be generated
*/
while (argv[0][factoridx]) {
if (!isdigit((int)(argv[0][factoridx]))) {
switch(argv[0][factoridx]) {
case 'k':
case 'K':
shift = 10;
break;
case 'm':
case 'M':
shift = 20;
break;
case 'g':
case 'G':
shift = 30;
break;
case 't':
case 'T':
shift = 40;
if (!strcmp(argv[0], "max")) {
scaled_num = SIZE_MAX;
} else {
/*
* iterate over the value and check to see if there are
* any non-numerical chars
* A non digit suffix indicates we need to shift the
* number of requested bytes by a factor of:
* k = 1024^1 (1 << (10 * 1))
* m = 1024^2 (1 << (10 * 2))
* g = 1024^3 (1 << (10 * 3))
* t = 1024^4 (1 << (10 * 4))
* which can be achieved by bit-shifting the number
*/
while (argv[0][factoridx]) {
if (!isdigit((int)(argv[0][factoridx]))) {
switch(argv[0][factoridx]) {
case 'k':
case 'K':
shift = 10;
break;
case 'm':
case 'M':
shift = 20;
break;
case 'g':
case 'G':
shift = 30;
break;
case 't':
case 'T':
shift = 40;
break;
default:
BIO_printf(bio_err, "Invalid size suffix %s\n",
&argv[0][factoridx]);
goto opthelp;
}
break;
default:
BIO_printf(bio_err, "Invalid size suffix %s\n",
&argv[0][factoridx]);
goto opthelp;
}
break;
factoridx++;
}
factoridx++;
}

if (factoridx && strlen(&argv[0][factoridx]) != 1) {
BIO_printf(bio_err, "Invalid size suffix %s\n", &argv[0][factoridx]);
goto opthelp;
if (shift != 0 && strlen(&argv[0][factoridx]) != 1) {
BIO_printf(bio_err, "Invalid size suffix %s\n",
&argv[0][factoridx]);
goto opthelp;
}
}
/* Remove the suffix from the arg so that opt_long works */
if (shift != 0)
argv[0][factoridx] = '\0';

if (!opt_long(argv[0], &num) || num <= 0)
if ((scaled_num != SIZE_MAX) && (!opt_long(argv[0], &num) || num <= 0))
goto opthelp;

if (shift != 0) {
/* check for overflow */
if ((SIZE_MAX >> shift) < (size_t)num) {
BIO_printf(bio_err, "%lu bytes with provided suffix overflows\n",
BIO_printf(bio_err, "%lu bytes with suffix overflows\n",
num);
goto opthelp;
}
scaled_num = num << shift;
} else {
scaled_num = num;
if (scaled_num == 0)
scaled_num = num;
}
} else if (!opt_check_rest_arg(NULL)) {
goto opthelp;
Expand Down
12 changes: 9 additions & 3 deletions doc/man1/openssl-rand.pod.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ I<num>[k|m|g|t]
=head1 DESCRIPTION

This command generates I<num> random bytes using a cryptographically
secure pseudo random number generator (CSPRNG). the suffix k, m, g or t may be
added to the num value to indicate the request value be scaled as a multiple of
kiB/MiB/GiB/TiB respectively. Note that suffixes are case insensitive.
secure pseudo random number generator (CSPRNG). A suffix [K|M|G|T] may be
appended to the num value to indicate the requested value be scaled as a
multiple of KiB/MiB/GiB/TiB respectively. Note that suffixes are case
insensitive, and that the suffixes represent binary multiples
(KiB = 1024 bytes, MiB = 1024*1024 bytes, etc).

The string 'max' may be substituted for a numercial value in num, to request the
maximum number of bytes the CSPRNG can produce per instantiation. Currently,
this is restricted to 2^64 bytes as per NIST SP 800-90C.

The random bytes are generated using the L<RAND_bytes(3)> function,
which provides a security level of 256 bits, provided it managed to
Expand Down

0 comments on commit bd3b659

Please sign in to comment.