123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "tools/fapi/tss2_template.h"
- static struct cxt {
- size_t numBytes;
- char *filename;
- bool overwrite;
- bool hex;
- } ctx;
- static bool on_option(char key, char *value) {
- switch (key) {
- case 'n': {
-
- uint32_t i;
- if (!tpm2_util_string_to_uint32 (value, &i) || i == 0) {
- fprintf (stderr, "%s cannot be converted to a positive integer or "\
- "is larger than 2**32 - 1\n", value);
- return false;
- }
- ctx.numBytes = i;
- }
- break;
- case 'f':
- ctx.overwrite = true;
- break;
- case 'o':
- ctx.filename = value;
- break;
- case 0:
- ctx.hex = true;
- break;
- }
- return true;
- }
- static bool tss2_tool_onstart(tpm2_options **opts) {
- struct option topts[] = {
- {"numBytes", required_argument, NULL, 'n'},
- {"force" , no_argument , NULL, 'f'},
-
- {"data" , required_argument, NULL, 'o'},
- {"hex", no_argument, NULL, 0}
- };
- return (*opts = tpm2_options_new ("fn:o:", ARRAY_LEN(topts), topts,
- on_option, NULL, 0)) != NULL;
- }
- static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
-
- if (!ctx.filename) {
- fprintf (stderr, "No filename for data was provided, use --data\n");
- return -1;
- }
- if (!ctx.numBytes) {
- fprintf (stderr, "No amount of bytes was provided, use --numBytes\n");
- return -1;
- }
-
- uint8_t *data;
- TSS2_RC r = Fapi_GetRandom (fctx, ctx.numBytes, &data);
- if (r != TSS2_RC_SUCCESS) {
- LOG_PERR ("Fapi_GetRandom", r);
- return 1;
- }
- if (ctx.hex) {
- char* str = malloc (ctx.numBytes*2 + 1);
- if (!str) {
- Fapi_Free (data);
- LOG_ERR ("malloc(2) failed: %m\n");
- return 1;
- }
- for (size_t i = 0; i<ctx.numBytes; i++) {
- sprintf(str+i*2,"%02x",data[i]);
- }
-
- r = open_write_and_close (ctx.filename, ctx.overwrite, str, strlen(str));
- free(str);
- }
- else {
-
- r = open_write_and_close (ctx.filename, ctx.overwrite, data,
- ctx.numBytes);
- }
- if (r) {
- Fapi_Free (data);
- return 1;
- }
- Fapi_Free (data);
- return 0;
- }
- TSS2_TOOL_REGISTER("getrandom", tss2_tool_onstart, tss2_tool_onrun, NULL)
|