tpm2_hmac.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "files.h"
  9. #include "log.h"
  10. #include "tpm2.h"
  11. #include "tpm2_alg_util.h"
  12. #include "tpm2_tool.h"
  13. typedef struct tpm_hmac_ctx tpm_hmac_ctx;
  14. struct tpm_hmac_ctx {
  15. struct {
  16. char *ctx_path;
  17. char *auth_str;
  18. tpm2_loaded_object object;
  19. } hmac_key;
  20. FILE *input;
  21. char *hmac_output_file_path;
  22. char *ticket_path;
  23. TPMI_ALG_HASH halg;
  24. bool hex;
  25. char *cp_hash_path;
  26. };
  27. static tpm_hmac_ctx ctx;
  28. static tool_rc tpm_hmac_file(ESYS_CONTEXT *ectx, TPM2B_DIGEST **result,
  29. TPMT_TK_HASHCHECK **validation) {
  30. unsigned long file_size = 0;
  31. FILE *input = ctx.input;
  32. tool_rc rc;
  33. /* Suppress error reporting with NULL path */
  34. bool res = files_get_file_size(input, &file_size, NULL);
  35. /*
  36. * If we can get the file size and its less than 1024, just do it in one hash invocation.
  37. * We can't use the one-shot command if we require ticket, as it doesn't provide it in
  38. * the response from the TPM.
  39. */
  40. if (!ctx.ticket_path && res && file_size <= TPM2_MAX_DIGEST_BUFFER) {
  41. TPM2B_MAX_BUFFER buffer = { .size = file_size };
  42. res = files_read_bytes(ctx.input, buffer.buffer, buffer.size);
  43. if (!res) {
  44. LOG_ERR("Error reading input file!");
  45. return tool_rc_general_error;
  46. }
  47. if (ctx.cp_hash_path) {
  48. LOG_WARN("Exiting without performing HMAC when calculating cpHash");
  49. TPM2B_DIGEST cp_hash = { .size = 0 };
  50. tool_rc rc = tpm2_hmac(ectx, &ctx.hmac_key.object, ctx.halg,
  51. &buffer, result, &cp_hash);
  52. if (rc != tool_rc_success) {
  53. return rc;
  54. }
  55. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  56. if (!result) {
  57. rc = tool_rc_general_error;
  58. }
  59. return rc;
  60. }
  61. /*
  62. * hash algorithm specified in the key's scheme is used as the
  63. * hash algorithm for the HMAC
  64. */
  65. return tpm2_hmac(ectx, &ctx.hmac_key.object, ctx.halg, &buffer, result,
  66. NULL);
  67. }
  68. if (ctx.cp_hash_path) {
  69. LOG_ERR("Cannot calculate cpHash for buffers requiring HMAC sequence.");
  70. return tool_rc_general_error;
  71. }
  72. ESYS_TR sequence_handle;
  73. /*
  74. * Size is either unknown because the FILE * is a fifo, or it's too big
  75. * to do in a single hash call. Based on the size figure out the chunks
  76. * to loop over, if possible. This way we can call Complete with data.
  77. */
  78. rc = tpm2_hmac_start(ectx, &ctx.hmac_key.object, ctx.halg,
  79. &sequence_handle);
  80. if (rc != tool_rc_success) {
  81. return rc;
  82. }
  83. /* If we know the file size, we decrement the amount read and terminate the
  84. * loop when 1 block is left, else we go till feof.
  85. */
  86. size_t left = file_size;
  87. bool use_left = !!res;
  88. TPM2B_MAX_BUFFER data;
  89. bool done = false;
  90. while (!done) {
  91. size_t bytes_read = fread(data.buffer, 1,
  92. BUFFER_SIZE(typeof(data), buffer), input);
  93. if (ferror(input)) {
  94. LOG_ERR("Error reading from input file");
  95. return tool_rc_general_error;
  96. }
  97. data.size = bytes_read;
  98. /* if data was read, update the sequence */
  99. rc = tpm2_hmac_sequenceupdate(ectx, sequence_handle,
  100. &ctx.hmac_key.object, &data);
  101. if (rc != tool_rc_success) {
  102. return rc;
  103. }
  104. if (use_left) {
  105. left -= bytes_read;
  106. if (left <= TPM2_MAX_DIGEST_BUFFER) {
  107. done = true;
  108. continue;
  109. }
  110. } else if (feof(input)) {
  111. done = true;
  112. }
  113. } /* end file read/hash update loop */
  114. if (use_left) {
  115. data.size = left;
  116. bool res = files_read_bytes(input, data.buffer, left);
  117. if (!res) {
  118. LOG_ERR("Error reading from input file.");
  119. return tool_rc_general_error;
  120. }
  121. } else {
  122. data.size = 0;
  123. }
  124. rc = tpm2_hmac_sequencecomplete(ectx, sequence_handle, &ctx.hmac_key.object,
  125. &data, result, validation);
  126. if (rc != tool_rc_success) {
  127. return rc;
  128. }
  129. return tool_rc_success;
  130. }
  131. static tool_rc do_hmac_and_output(ESYS_CONTEXT *ectx) {
  132. TPM2B_DIGEST *hmac_out = NULL;
  133. TPMT_TK_HASHCHECK *validation = NULL;
  134. FILE *out = stdout;
  135. tool_rc rc = tpm_hmac_file(ectx, &hmac_out, &validation);
  136. if (rc != tool_rc_success || ctx.cp_hash_path) {
  137. goto out;
  138. }
  139. assert(hmac_out);
  140. if (ctx.ticket_path) {
  141. bool res = files_save_validation(validation, ctx.ticket_path);
  142. if (!res) {
  143. rc = tool_rc_general_error;
  144. goto out;
  145. }
  146. }
  147. rc = tool_rc_general_error;
  148. if (ctx.hmac_output_file_path) {
  149. out = fopen(ctx.hmac_output_file_path, "wb+");
  150. if (!out) {
  151. LOG_ERR("Could not open output file \"%s\", error: %s",
  152. ctx.hmac_output_file_path, strerror(errno));
  153. goto out;
  154. }
  155. } else if (!output_enabled) {
  156. rc = tool_rc_success;
  157. goto out;
  158. }
  159. if (ctx.hex) {
  160. tpm2_util_print_tpm2b2(out, hmac_out);
  161. } else {
  162. bool res = files_write_bytes(out, hmac_out->buffer, hmac_out->size);
  163. if (!res) {
  164. goto out;
  165. }
  166. }
  167. rc = tool_rc_success;
  168. out:
  169. if (out && out != stdout) {
  170. fclose(out);
  171. }
  172. free(hmac_out);
  173. free(validation);
  174. return rc;
  175. }
  176. static bool on_option(char key, char *value) {
  177. switch (key) {
  178. case 'c':
  179. ctx.hmac_key.ctx_path = value;
  180. break;
  181. case 'p':
  182. ctx.hmac_key.auth_str = value;
  183. break;
  184. case 'o':
  185. ctx.hmac_output_file_path = value;
  186. break;
  187. case 'g':
  188. ctx.halg = tpm2_alg_util_from_optarg(value, tpm2_alg_util_flags_hash);
  189. if (ctx.halg == TPM2_ALG_ERROR) {
  190. return false;
  191. }
  192. break;
  193. case 't':
  194. ctx.ticket_path = value;
  195. break;
  196. case 0:
  197. ctx.hex = true;
  198. break;
  199. case 1:
  200. ctx.cp_hash_path = value;
  201. break;
  202. /* no default */
  203. }
  204. return true;
  205. }
  206. static bool on_args(int argc, char **argv) {
  207. if (argc > 1) {
  208. LOG_ERR("Expected 1 hmac input file, got: %d", argc);
  209. return false;
  210. }
  211. ctx.input = fopen(argv[0], "rb");
  212. if (!ctx.input) {
  213. LOG_ERR("Error opening file \"%s\", error: %s", argv[0],
  214. strerror(errno));
  215. return false;
  216. }
  217. return true;
  218. }
  219. static bool tpm2_tool_onstart(tpm2_options **opts) {
  220. const struct option topts[] = {
  221. { "key-context", required_argument, NULL, 'c' },
  222. { "auth", required_argument, NULL, 'p' },
  223. { "output", required_argument, NULL, 'o' },
  224. { "hash-algorithm", required_argument, NULL, 'g' },
  225. { "ticket", required_argument, NULL, 't' },
  226. { "hex", no_argument, NULL, 0 },
  227. { "cphash", required_argument, NULL, 1 },
  228. };
  229. ctx.input = stdin;
  230. *opts = tpm2_options_new("c:p:o:g:t:", ARRAY_LEN(topts), topts, on_option,
  231. on_args, 0);
  232. return *opts != NULL;
  233. }
  234. static tool_rc readpub(ESYS_CONTEXT *ectx, ESYS_TR handle,
  235. TPM2B_PUBLIC **public) {
  236. return tpm2_readpublic(ectx, handle, public, NULL, NULL);
  237. }
  238. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  239. UNUSED(flags);
  240. /*
  241. * Option C must be specified.
  242. */
  243. if (!ctx.hmac_key.ctx_path) {
  244. LOG_ERR("Must specify options C.");
  245. return tool_rc_option_error;
  246. }
  247. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.hmac_key.ctx_path,
  248. ctx.hmac_key.auth_str, &ctx.hmac_key.object, false,
  249. TPM2_HANDLE_ALL_W_NV);
  250. if (rc != tool_rc_success) {
  251. LOG_ERR("Invalid key handle authorization");
  252. return rc;
  253. }
  254. /*
  255. * if no halg was specified, read the public portion of the key and use it's
  256. * scheme
  257. */
  258. if (!ctx.halg) {
  259. TPM2B_PUBLIC *pub = NULL;
  260. rc = readpub(ectx, ctx.hmac_key.object.tr_handle, &pub);
  261. if (rc != tool_rc_success) {
  262. return rc;
  263. }
  264. /*
  265. * if we're attempting to figure out a hashing scheme, and the scheme is NULL
  266. * we default to sha256.
  267. */
  268. ctx.halg =
  269. pub->publicArea.parameters.keyedHashDetail.scheme.details.hmac.hashAlg;
  270. if (ctx.halg == TPM2_ALG_NULL) {
  271. ctx.halg = TPM2_ALG_SHA256;
  272. }
  273. free(pub);
  274. }
  275. return do_hmac_and_output(ectx);
  276. }
  277. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  278. UNUSED(ectx);
  279. if (ctx.input && ctx.input != stdin) {
  280. fclose(ctx.input);
  281. }
  282. return tpm2_session_close(&ctx.hmac_key.object.session);
  283. }
  284. // Register this tool with tpm2_tool.c
  285. TPM2_TOOL_REGISTER("hmac", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)