trusted_tpm.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. struct osapsess {
  16. uint32_t handle;
  17. unsigned char secret[SHA1_DIGEST_SIZE];
  18. unsigned char enonce[TPM_NONCE_SIZE];
  19. };
  20. /* discrete values, but have to store in uint16_t for TPM use */
  21. enum {
  22. SEAL_keytype = 1,
  23. SRK_keytype = 4
  24. };
  25. int TSS_authhmac(unsigned char *digest, const unsigned char *key,
  26. unsigned int keylen, unsigned char *h1,
  27. unsigned char *h2, unsigned int h3, ...);
  28. int TSS_checkhmac1(unsigned char *buffer,
  29. const uint32_t command,
  30. const unsigned char *ononce,
  31. const unsigned char *key,
  32. unsigned int keylen, ...);
  33. int trusted_tpm_send(unsigned char *cmd, size_t buflen);
  34. int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
  35. int tpm2_seal_trusted(struct tpm_chip *chip,
  36. struct trusted_key_payload *payload,
  37. struct trusted_key_options *options);
  38. int tpm2_unseal_trusted(struct tpm_chip *chip,
  39. struct trusted_key_payload *payload,
  40. struct trusted_key_options *options);
  41. #define TPM_DEBUG 0
  42. #if TPM_DEBUG
  43. static inline void dump_options(struct trusted_key_options *o)
  44. {
  45. pr_info("trusted_key: sealing key type %d\n", o->keytype);
  46. pr_info("trusted_key: sealing key handle %0X\n", o->keyhandle);
  47. pr_info("trusted_key: pcrlock %d\n", o->pcrlock);
  48. pr_info("trusted_key: pcrinfo %d\n", o->pcrinfo_len);
  49. print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE,
  50. 16, 1, o->pcrinfo, o->pcrinfo_len, 0);
  51. }
  52. static inline void dump_payload(struct trusted_key_payload *p)
  53. {
  54. pr_info("trusted_key: key_len %d\n", p->key_len);
  55. print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
  56. 16, 1, p->key, p->key_len, 0);
  57. pr_info("trusted_key: bloblen %d\n", p->blob_len);
  58. print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
  59. 16, 1, p->blob, p->blob_len, 0);
  60. pr_info("trusted_key: migratable %d\n", p->migratable);
  61. }
  62. static inline void dump_sess(struct osapsess *s)
  63. {
  64. print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
  65. 16, 1, &s->handle, 4, 0);
  66. pr_info("trusted-key: secret:\n");
  67. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
  68. 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
  69. pr_info("trusted-key: enonce:\n");
  70. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
  71. 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
  72. }
  73. static inline void dump_tpm_buf(unsigned char *buf)
  74. {
  75. int len;
  76. pr_info("\ntrusted-key: tpm buffer\n");
  77. len = LOAD32(buf, TPM_SIZE_OFFSET);
  78. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
  79. }
  80. #else
  81. static inline void dump_options(struct trusted_key_options *o)
  82. {
  83. }
  84. static inline void dump_payload(struct trusted_key_payload *p)
  85. {
  86. }
  87. static inline void dump_sess(struct osapsess *s)
  88. {
  89. }
  90. static inline void dump_tpm_buf(unsigned char *buf)
  91. {
  92. }
  93. #endif
  94. #endif