diff options
author | Kees Cook <keescook@chromium.org> | 2012-02-29 16:09:14 -0800 |
---|---|---|
committer | Gerrit <chrome-bot@google.com> | 2012-03-01 15:25:31 -0800 |
commit | f0605cbdc36f58829a908a3333e438c565c8c7af (patch) | |
tree | 3cbf146c627d95d2d01461ac0224d90234e802fe /utility | |
parent | 5ee257d94cb8aab2f3717c5cd4ceb37fbba3ec41 (diff) | |
download | vboot-f0605cbdc36f58829a908a3333e438c565c8c7af.tar.gz |
tpm_lite: implement TPM_GetRandom
Provide TPM_GetRandom function to library callers.
BUG=chromium-os:22172
TEST=lumpy build & manual testing
Change-Id: Id604fd92490ba697033158a580b0b4df1d975932
Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/17120
Reviewed-by: Luigi Semenzato <semenzato@chromium.org>
Diffstat (limited to 'utility')
-rw-r--r-- | utility/tlcl_generator.c | 11 | ||||
-rw-r--r-- | utility/tpmc.c | 33 |
2 files changed, 42 insertions, 2 deletions
diff --git a/utility/tlcl_generator.c b/utility/tlcl_generator.c index f905784f..bbc379c1 100644 --- a/utility/tlcl_generator.c +++ b/utility/tlcl_generator.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. +/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -362,6 +362,14 @@ Command* BuildGetPermissionsCommand(void) { return cmd; } +Command* BuildGetRandomCommand(void) { + int size = kTpmRequestHeaderLength + sizeof(uint32_t); + Command* cmd = newCommand(TPM_ORD_GetRandom, size); + cmd->name = "tpm_get_random_cmd"; + AddVisibleField(cmd, "bytesRequested", kTpmRequestHeaderLength); + return cmd; +} + /* Output the fields of a structure. */ void OutputFields(Field* fld) { @@ -480,6 +488,7 @@ Command* (*builders[])(void) = { BuildGetFlagsCommand, BuildGetSTClearFlagsCommand, BuildGetPermissionsCommand, + BuildGetRandomCommand, BuildExtendCommand, }; diff --git a/utility/tpmc.c b/utility/tpmc.c index d8fb07a5..68ce6d3f 100644 --- a/utility/tpmc.c +++ b/utility/tpmc.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. +/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * @@ -228,6 +228,35 @@ static uint32_t HandlerGetPermissions(void) { return result; } +static uint32_t HandlerGetRandom(void) { + uint32_t length, size; + uint8_t* bytes; + uint32_t result; + int i; + if (nargs != 3) { + fprintf(stderr, "usage: tpmc getrandom <size>\n"); + exit(OTHER_ERROR); + } + if (HexStringToUint32(args[2], &length) != 0) { + fprintf(stderr, "<size> must be 32-bit hex (0x[0-9a-f]+)\n"); + exit(OTHER_ERROR); + } + bytes = calloc(1, length); + if (bytes == NULL) { + perror("calloc"); + exit(OTHER_ERROR); + } + result = TlclGetRandom(bytes, length, &size); + if (result == 0 && size > 0) { + for (i = 0; i < size; i++) { + printf("%02x", bytes[i]); + } + printf("\n"); + } + free(bytes); + return result; +} + static uint32_t HandlerGetPermanentFlags(void) { TPM_PERMANENT_FLAGS pflags; uint32_t result = TlclGetPermanentFlags(&pflags); @@ -312,6 +341,8 @@ command_record command_table[] = { HandlerGetPermissions }, { "getpermanentflags", "getpf", "print all permanent flags", HandlerGetPermanentFlags }, + { "getrandom", "rand", "read bytes from RNG (rand <size>)", + HandlerGetRandom }, { "getstclearflags", "getvf", "print all volatile (ST_CLEAR) flags", HandlerGetSTClearFlags }, { "resume", "res", "execute TPM_Startup(ST_STATE)", TlclResume }, |