tpm2_alg_util.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #ifndef LIB_TPM2_ALG_UTIL_H_
  3. #define LIB_TPM2_ALG_UTIL_H_
  4. #include <stdbool.h>
  5. #include <tss2/tss2_esys.h>
  6. #include "tool_rc.h"
  7. typedef enum tpm2_alg_util_flags tpm2_alg_util_flags;
  8. enum tpm2_alg_util_flags {
  9. tpm2_alg_util_flags_none = 0,
  10. tpm2_alg_util_flags_hash = 1 << 0,
  11. tpm2_alg_util_flags_keyedhash = 1 << 0,
  12. tpm2_alg_util_flags_symmetric = 1 << 2,
  13. tpm2_alg_util_flags_asymmetric = 1 << 3,
  14. tpm2_alg_util_flags_kdf = 1 << 4,
  15. tpm2_alg_util_flags_mgf = 1 << 5,
  16. tpm2_alg_util_flags_sig = 1 << 6,
  17. tpm2_alg_util_flags_mode = 1 << 7,
  18. tpm2_alg_util_flags_base = 1 << 8,
  19. tpm2_alg_util_flags_misc = 1 << 9,
  20. tpm2_alg_util_flags_enc_scheme = 1 << 10,
  21. tpm2_alg_util_flags_rsa_scheme = 1 << 11,
  22. tpm2_alg_util_flags_any = ~0
  23. };
  24. /**
  25. * Convert a "nice-name" string to an algorithm id.
  26. * @param name
  27. * The "nice-name" to convert.
  28. * @return
  29. * TPM2_ALG_ERROR on error, or a valid algorithm identifier.
  30. */
  31. TPM2_ALG_ID tpm2_alg_util_strtoalg(const char *name, tpm2_alg_util_flags flags);
  32. /**
  33. * Convert an id to a nice-name.
  34. * @param id
  35. * The id to convert.
  36. * @return
  37. * The nice-name.
  38. */
  39. const char *tpm2_alg_util_algtostr(TPM2_ALG_ID id, tpm2_alg_util_flags flags);
  40. /**
  41. * XXX DOC AND TESTME
  42. * @param id
  43. * @return
  44. */
  45. tpm2_alg_util_flags tpm2_alg_util_algtoflags(TPM2_ALG_ID id);
  46. /**
  47. * Converts either a string from algorithm number or algorithm nice-name to
  48. * an algorithm id.
  49. * @param optarg
  50. * The string to convert from an algorithm number or nice name.
  51. * @return
  52. * TPM2_ALG_ERROR on error or the algorithm id.
  53. */
  54. TPM2_ALG_ID tpm2_alg_util_from_optarg(const char *optarg,
  55. tpm2_alg_util_flags flags);
  56. /**
  57. * Contains the information from parsing an argv style vector of strings for
  58. * pcr digest language specifications.
  59. */
  60. typedef struct tpm2_pcr_digest_spec tpm2_pcr_digest_spec;
  61. struct tpm2_pcr_digest_spec {
  62. TPML_DIGEST_VALUES digests;
  63. TPMI_DH_PCR pcr_index;
  64. };
  65. /**
  66. * Parses an argv array that contains a digest specification at each location
  67. * within argv.
  68. *
  69. * The digest specification is as follows:
  70. * - A pcr identifier as understood by strtoul with 0 as the base.
  71. * - A colon followed by the algorithm hash specification.
  72. * - The algorithm hash specification is as follows:
  73. * - The algorithm friendly name or raw numerical as understood by
  74. * strtoul with a base of 0.
  75. * - An equals sign
  76. * - The hex hash value,
  77. *
  78. * This all distills to a string that looks like this:
  79. * <pcr index>:<hash alg id>=<hash value>
  80. *
  81. * Example:
  82. * "4:sha1=f1d2d2f924e986ac86fdf7b36c94bcdf32beec15"
  83. *
  84. * Note:
  85. * Multiple specifications of PCR and hash are OK. Multiple hashes
  86. * cause the pcr to be extended with both hashes. Multiple same PCR
  87. * values cause the PCR to be extended multiple times. Extension
  88. * is done in order from left to right as specified.
  89. *
  90. * At most 5 hash extensions per PCR entry are supported. This
  91. * is to keep the parser simple.
  92. *
  93. * @param argv
  94. * The argv of digest specifications to parse.
  95. * @param len
  96. * The number of digest specifications to parse.
  97. * @param digests
  98. * An array of tpm2_pcr_digest_spec big enough to hold len items.
  99. * @return
  100. * True if parsing was successful, False otherwise.
  101. * @note
  102. * This function logs errors via LOG_ERR.
  103. */
  104. bool pcr_parse_digest_list(char **argv, int len,
  105. tpm2_pcr_digest_spec *digest_spec);
  106. /**
  107. * Retrieves the size of a hash in bytes for a given hash
  108. * algorithm or 0 if unknown/not found.
  109. * @param id
  110. * The HASH algorithm identifier.
  111. * @return
  112. * 0 on failure or the size of the hash bytes.
  113. */
  114. UINT16 tpm2_alg_util_get_hash_size(TPMI_ALG_HASH id);
  115. /**
  116. * Retrieves an appropriate signature scheme (scheme) signable by
  117. * specified key (keyHandle) and hash algorithm (halg).
  118. * @param context
  119. * Enhanced System API (ESAPI) context for tpm
  120. * @param keyHandle
  121. * Handle to key used in signing operation
  122. * @param halg
  123. * The requested hash algorithm for message, the contents are checked
  124. * against what the object will support. If TPM2_ALG_NULL is specified, then the
  125. * hash algorithm is updated to what the object will support.
  126. * @param sig_scheme
  127. * Signature scheme (optional, use TPM2_ALG_NULL for default)
  128. * @param scheme
  129. * Signature scheme output
  130. * @param default_hash_sha1
  131. * Set to true to make the default hash sha1 over sha256
  132. * @return
  133. * tool_rc indicating status.
  134. * On error scheme is left unmodified.
  135. */
  136. tool_rc tpm2_alg_util_get_signature_scheme(ESYS_CONTEXT *context,
  137. ESYS_TR key_handle, TPMI_ALG_HASH *halg, TPMI_ALG_SIG_SCHEME sig_scheme,
  138. TPMT_SIG_SCHEME *scheme);
  139. /**
  140. *
  141. * @param alg_spec
  142. * Friendly specification of public algorithm set (algname:...:....)
  143. * @param public
  144. * Public structure which will contain relevant information about
  145. * specified algorithm
  146. * @pre public is caller allocated and must not be NULL
  147. * @return
  148. */
  149. bool tpm2_alg_util_handle_ext_alg(const char *alg_spec, TPM2B_PUBLIC *public);
  150. /**
  151. *
  152. * @param alg_details
  153. * @param name_halg
  154. * @param attrs
  155. * @param auth_policy
  156. * @param def_attrs
  157. * @param is_sealing
  158. * @param public
  159. * @return
  160. */
  161. tool_rc tpm2_alg_util_public_init(char *alg_details, char *name_halg, char *attrs,
  162. char *auth_policy, TPMA_OBJECT def_attrs, TPM2B_PUBLIC *public);
  163. /**
  164. * Returns an ECC curve as a friendly name.
  165. * @param curve_id
  166. * The curve to look up a friendly string for.
  167. * @return
  168. * The friendly string or NULL if not found.
  169. */
  170. const char *tpm2_alg_util_ecc_to_str(TPM2_ECC_CURVE curve_id);
  171. /**
  172. * Determines if a size is a valid AES key size.
  173. * @param size_in_bytes
  174. * The size of a potential AES key in bytes.
  175. * @return
  176. * true if valid, false otherwise.
  177. */
  178. bool tpm2_alg_util_is_aes_size_valid(UINT16 size_in_bytes);
  179. #endif /* LIB_TPM2_ALG_UTIL_H_ */