trusted_tpm.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __TRUSTED_TPM_H
  3. #define __TRUSTED_TPM_H
  4. #include <keys/trusted-type.h>
  5. #include <linux/tpm_command.h>
  6. /* implementation specific TPM constants */
  7. #define MAX_BUF_SIZE 1024
  8. #define TPM_GETRANDOM_SIZE 14
  9. #define TPM_SIZE_OFFSET 2
  10. #define TPM_RETURN_OFFSET 6
  11. #define TPM_DATA_OFFSET 10
  12. #define LOAD32(buffer, offset) (ntohl(*(uint32_t *)&buffer[offset]))
  13. #define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset])
  14. #define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset]))
  15. extern struct trusted_key_ops trusted_key_tpm_ops;
  16. struct osapsess {
  17. uint32_t handle;
  18. unsigned char secret[SHA1_DIGEST_SIZE];
  19. unsigned char enonce[TPM_NONCE_SIZE];
  20. };
  21. /* discrete values, but have to store in uint16_t for TPM use */
  22. enum {
  23. SEAL_keytype = 1,
  24. SRK_keytype = 4
  25. };
  26. int TSS_authhmac(unsigned char *digest, const unsigned char *key,
  27. unsigned int keylen, unsigned char *h1,
  28. unsigned char *h2, unsigned int h3, ...);
  29. int TSS_checkhmac1(unsigned char *buffer,
  30. const uint32_t command,
  31. const unsigned char *ononce,
  32. const unsigned char *key,
  33. unsigned int keylen, ...);
  34. int trusted_tpm_send(unsigned char *cmd, size_t buflen);
  35. int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
  36. int tpm2_seal_trusted(struct tpm_chip *chip,
  37. struct trusted_key_payload *payload,
  38. struct trusted_key_options *options);
  39. int tpm2_unseal_trusted(struct tpm_chip *chip,
  40. struct trusted_key_payload *payload,
  41. struct trusted_key_options *options);
  42. #define TPM_DEBUG 0
  43. #if TPM_DEBUG
  44. static inline void dump_options(struct trusted_key_options *o)
  45. {
  46. pr_info("sealing key type %d\n", o->keytype);
  47. pr_info("sealing key handle %0X\n", o->keyhandle);
  48. pr_info("pcrlock %d\n", o->pcrlock);
  49. pr_info("pcrinfo %d\n", o->pcrinfo_len);
  50. print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE,
  51. 16, 1, o->pcrinfo, o->pcrinfo_len, 0);
  52. }
  53. static inline void dump_sess(struct osapsess *s)
  54. {
  55. print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
  56. 16, 1, &s->handle, 4, 0);
  57. pr_info("secret:\n");
  58. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
  59. 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
  60. pr_info("trusted-key: enonce:\n");
  61. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
  62. 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
  63. }
  64. static inline void dump_tpm_buf(unsigned char *buf)
  65. {
  66. int len;
  67. pr_info("\ntpm buffer\n");
  68. len = LOAD32(buf, TPM_SIZE_OFFSET);
  69. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
  70. }
  71. #else
  72. static inline void dump_options(struct trusted_key_options *o)
  73. {
  74. }
  75. static inline void dump_sess(struct osapsess *s)
  76. {
  77. }
  78. static inline void dump_tpm_buf(unsigned char *buf)
  79. {
  80. }
  81. #endif
  82. #endif