Skip to content

Commit

Permalink
tpm2_getrandom: update help and simplify interface
Browse files Browse the repository at this point in the history
Make argument options optional for tpm2_getrandom. Size is a required
argument, so make it a required positional arg. Now, the only option
is -o or --output, which optionally specifies an output file. If an
output file is specified, it writes all output to that file, else
it prints hex values of the bytes to the terminal.

Fixes 251

This patch requires a significant version bump as this version of
tpm2_getrandom is not "api" compatable with older versions.

Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Mar 29, 2017
1 parent 0b744d1 commit 6dba52d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 44 deletions.
16 changes: 7 additions & 9 deletions man/tpm2_getrandom.8.in
Expand Up @@ -28,20 +28,17 @@
.\" THE POSSIBILITY OF SUCH DAMAGE.
.TH tpm2_getrandom 8 "DECEMBER 2016" Intel "tpm2.0-tools"
.SH NAME
tpm2_getrandom\ - returns the next bytesRequested octets from the random number generator.
tpm2_getrandom\ - returns the next SIZE octets from the random number generator.
.SH SYNOPSIS
.B tpm2_getrandom[ COMMON OPTIONS ] [ TCTI OPTIONS ] [ \fB\-\-size\fR|\fB\-\-of\fR|\fB ]
.B tpm2_getrandom[ COMMON OPTIONS ] [ TCTI OPTIONS ] [ -\-output\fR|\fB ] SIZE
.PP
returns the next bytesRequested octets from the random number generator.
returns the next SIZE octets from the random number generator.
.SH DESCRIPTION
.B tpm2_getrandom
returns the next bytesRequested octets from the random number generator.
returns the next SIZE octets from the random number generator.
.SH OPTIONS
.TP
\fB\-s ,\-\-size\fR
specifies the size of the bytesRequested.
.TP
\fB\-o ,\-\-of\fR
\fB\-o ,\-\-output\fR
specifies the filename of output.
@COMMON_OPTIONS_INCLUDE@
@TCTI_OPTIONS_INCLUDE@
Expand All @@ -51,6 +48,7 @@ specifies the filename of output.
.PP
.nf
.RS
tpm2_getrandom -s 20 -o random.out
tpm2_getrandom -o random.out 20
tpm2_getrandom 8
.RE
.fi
2 changes: 1 addition & 1 deletion test/system/test_smoking.sh
Expand Up @@ -93,7 +93,7 @@ tpm2_akparse -f ak.pub1.out -k akparse.out
fi

##### getrandom & hash
tpm2_getrandom -s 20 -o random.out
tpm2_getrandom -o random.out 20
if [ $? != 0 ];then
fail getrandom
fi
Expand Down
2 changes: 1 addition & 1 deletion test/system/test_tpm2_getrandom.sh
Expand Up @@ -35,7 +35,7 @@ size=32

rm -f random.out

tpm2_getrandom -s 32 -o random.out
tpm2_getrandom -o random.out 32
if [ $? != 0 ];then
echo "getrandom test fail, please check the environment or parameters!"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion test/system/test_tpm2_getrandom_func.sh
Expand Up @@ -40,7 +40,7 @@ i=

#for((i=1;i<=10;i++)); do
for i in `seq 100`; do
tpm2_getrandom -s 32 -o random_"$i".out
tpm2_getrandom o random_"$i".out 32
if [ $? != 0 ];then
echo " create random_"$i".out fail, please check the environment or parameters!"
exit 2
Expand Down
51 changes: 19 additions & 32 deletions tools/tpm2_getrandom.c
Expand Up @@ -47,6 +47,7 @@

typedef struct tpm_random_ctx tpm_random_ctx;
struct tpm_random_ctx {
bool output_file_specified;
char output_file[PATH_MAX];
UINT16 num_of_bytes;
TSS2_SYS_CONTEXT *sapi_context;
Expand All @@ -63,11 +64,14 @@ static bool get_random_and_save(tpm_random_ctx *ctx) {
return false;
}

printf("byte size: %d\n", random_bytes.t.size);
UINT16 i;
for (i = 0; i < random_bytes.t.size; i++)
printf(" 0x%2.2X", random_bytes.t.buffer[i]);
printf("\n");
if (!ctx->output_file_specified) {
UINT16 i;
for (i = 0; i < random_bytes.t.size; i++) {
printf("%s0x%2.2X", i ? " " : "", random_bytes.t.buffer[i]);
}
printf("\n");
return true;
}

return files_save_bytes_to_file(ctx->output_file, (UINT8 *) random_bytes.t.buffer,
random_bytes.t.size);
Expand All @@ -77,45 +81,25 @@ static bool get_random_and_save(tpm_random_ctx *ctx) {

static bool init(int argc, char *argv[], tpm_random_ctx *ctx) {

static const char *short_options = "s:o:p:d:hv";
static const char *short_options = "o:";
static const struct option long_options[] = {
{ "size", required_argument, NULL, 's' },
{ "of", required_argument, NULL, 'o' },
{ "output", required_argument, NULL, 'o' },
{ NULL, no_argument, NULL, '\0' },
};

struct {
UINT8 s : 1;
UINT8 o : 1;
UINT8 unused : 6;
} flags = { 0 };

/*
* subtract 1 from argc to disregard argv[0]
* ALL options are required.
* */
if ((argc - 1) != ARG_CNT) {
if (argc !=2 && argc != 4) {
showArgMismatch(argv[0]);
return false;
}

int opt;
bool result;
optind = 0; /* force reset of getopt() since we used gnu extensionsin main, sic */
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL))
!= -1) {
switch (opt) {
case 's':
result = string_bytes_get_uint16(optarg, &ctx->num_of_bytes);
if (!result) {
LOG_ERR("Error converting size to a number, got: \"%s\".",
optarg);
return false;
}
flags.s = 1;
break;
case 'o':
ctx->output_file_specified = true;
snprintf(ctx->output_file, sizeof(ctx->output_file), "%s", optarg);
flags.o = 1;
break;
case ':':
LOG_ERR("Argument %c needs a value!\n", optopt);
Expand All @@ -129,8 +113,10 @@ static bool init(int argc, char *argv[], tpm_random_ctx *ctx) {
}
}

if (!(flags.s && flags.o)) {
LOG_ERR("Must specify size and output file");
bool result = string_bytes_get_uint16(argv[optind], &ctx->num_of_bytes);
if (!result) {
LOG_ERR("Error converting size to a number, got: \"%s\".",
argv[optind]);
return false;
}

Expand All @@ -144,6 +130,7 @@ int execute_tool(int argc, char *argv[], char *envp[], common_opts_t *opts,
(void)envp;

tpm_random_ctx ctx = {
.output_file_specified = false,
.num_of_bytes = 0,
.output_file = { 0 },
.sapi_context = sapi_context
Expand Down

0 comments on commit 6dba52d

Please sign in to comment.