tss2_getrandom.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "tools/fapi/tss2_template.h"
  7. /* Context struct used to store passed commandline parameters */
  8. static struct cxt {
  9. size_t numBytes;
  10. char *filename;
  11. bool overwrite;
  12. bool hex;
  13. } ctx;
  14. /* Parse commandline parameters */
  15. static bool on_option(char key, char *value) {
  16. switch (key) {
  17. case 'n': {
  18. /*N.B. In theory size_t can be unsigned long, which is more than
  19. * uint32, in practice this will never happen */
  20. uint32_t i;
  21. if (!tpm2_util_string_to_uint32 (value, &i) || i == 0) {
  22. fprintf (stderr, "%s cannot be converted to a positive integer or "\
  23. "is larger than 2**32 - 1\n", value);
  24. return false;
  25. }
  26. ctx.numBytes = i; /* cast from uint32 to size_t */
  27. }
  28. break;
  29. case 'f':
  30. ctx.overwrite = true;
  31. break;
  32. case 'o':
  33. ctx.filename = value;
  34. break;
  35. case 0:
  36. ctx.hex = true;
  37. break;
  38. }
  39. return true;
  40. }
  41. /* Define possible commandline parameters */
  42. static bool tss2_tool_onstart(tpm2_options **opts) {
  43. struct option topts[] = {
  44. {"numBytes", required_argument, NULL, 'n'},
  45. {"force" , no_argument , NULL, 'f'},
  46. /* output file */
  47. {"data" , required_argument, NULL, 'o'},
  48. {"hex", no_argument, NULL, 0}
  49. };
  50. return (*opts = tpm2_options_new ("fn:o:", ARRAY_LEN(topts), topts,
  51. on_option, NULL, 0)) != NULL;
  52. }
  53. /* Execute specific tool */
  54. static int tss2_tool_onrun (FAPI_CONTEXT *fctx) {
  55. /* Check availability of required parameters */
  56. if (!ctx.filename) {
  57. fprintf (stderr, "No filename for data was provided, use --data\n");
  58. return -1;
  59. }
  60. if (!ctx.numBytes) {
  61. fprintf (stderr, "No amount of bytes was provided, use --numBytes\n");
  62. return -1;
  63. }
  64. /* Execute FAPI command with passed arguments */
  65. uint8_t *data;
  66. TSS2_RC r = Fapi_GetRandom (fctx, ctx.numBytes, &data);
  67. if (r != TSS2_RC_SUCCESS) {
  68. LOG_PERR ("Fapi_GetRandom", r);
  69. return 1;
  70. }
  71. if (ctx.hex) {
  72. char* str = malloc (ctx.numBytes*2 + 1);
  73. if (!str) {
  74. Fapi_Free (data);
  75. LOG_ERR ("malloc(2) failed: %m\n");
  76. return 1;
  77. }
  78. for (size_t i = 0; i<ctx.numBytes; i++) {
  79. sprintf(str+i*2,"%02x",data[i]);
  80. }
  81. /* Write returned data to file(s) */
  82. r = open_write_and_close (ctx.filename, ctx.overwrite, str, strlen(str));
  83. free(str);
  84. }
  85. else {
  86. /* Write returned data to file(s) */
  87. r = open_write_and_close (ctx.filename, ctx.overwrite, data,
  88. ctx.numBytes);
  89. }
  90. if (r) {
  91. Fapi_Free (data);
  92. return 1;
  93. }
  94. Fapi_Free (data);
  95. return 0;
  96. }
  97. TSS2_TOOL_REGISTER("getrandom", tss2_tool_onstart, tss2_tool_onrun, NULL)