trusted.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __TRUSTED_KEY_H
  3. #define __TRUSTED_KEY_H
  4. /* implementation specific TPM constants */
  5. #define MAX_BUF_SIZE 512
  6. #define TPM_GETRANDOM_SIZE 14
  7. #define TPM_OSAP_SIZE 36
  8. #define TPM_OIAP_SIZE 10
  9. #define TPM_SEAL_SIZE 87
  10. #define TPM_UNSEAL_SIZE 104
  11. #define TPM_SIZE_OFFSET 2
  12. #define TPM_RETURN_OFFSET 6
  13. #define TPM_DATA_OFFSET 10
  14. #define LOAD32(buffer, offset) (ntohl(*(uint32_t *)&buffer[offset]))
  15. #define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset])
  16. #define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset]))
  17. struct tpm_buf {
  18. int len;
  19. unsigned char data[MAX_BUF_SIZE];
  20. };
  21. #define INIT_BUF(tb) (tb->len = 0)
  22. struct osapsess {
  23. uint32_t handle;
  24. unsigned char secret[SHA1_DIGEST_SIZE];
  25. unsigned char enonce[TPM_NONCE_SIZE];
  26. };
  27. /* discrete values, but have to store in uint16_t for TPM use */
  28. enum {
  29. SEAL_keytype = 1,
  30. SRK_keytype = 4
  31. };
  32. #define TPM_DEBUG 0
  33. #if TPM_DEBUG
  34. static inline void dump_options(struct trusted_key_options *o)
  35. {
  36. pr_info("trusted_key: sealing key type %d\n", o->keytype);
  37. pr_info("trusted_key: sealing key handle %0X\n", o->keyhandle);
  38. pr_info("trusted_key: pcrlock %d\n", o->pcrlock);
  39. pr_info("trusted_key: pcrinfo %d\n", o->pcrinfo_len);
  40. print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE,
  41. 16, 1, o->pcrinfo, o->pcrinfo_len, 0);
  42. }
  43. static inline void dump_payload(struct trusted_key_payload *p)
  44. {
  45. pr_info("trusted_key: key_len %d\n", p->key_len);
  46. print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
  47. 16, 1, p->key, p->key_len, 0);
  48. pr_info("trusted_key: bloblen %d\n", p->blob_len);
  49. print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
  50. 16, 1, p->blob, p->blob_len, 0);
  51. pr_info("trusted_key: migratable %d\n", p->migratable);
  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("trusted-key: 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("\ntrusted-key: tpm 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_payload(struct trusted_key_payload *p)
  76. {
  77. }
  78. static inline void dump_sess(struct osapsess *s)
  79. {
  80. }
  81. static inline void dump_tpm_buf(unsigned char *buf)
  82. {
  83. }
  84. #endif
  85. static inline void store8(struct tpm_buf *buf, const unsigned char value)
  86. {
  87. buf->data[buf->len++] = value;
  88. }
  89. static inline void store16(struct tpm_buf *buf, const uint16_t value)
  90. {
  91. *(uint16_t *) & buf->data[buf->len] = htons(value);
  92. buf->len += sizeof value;
  93. }
  94. static inline void store32(struct tpm_buf *buf, const uint32_t value)
  95. {
  96. *(uint32_t *) & buf->data[buf->len] = htonl(value);
  97. buf->len += sizeof value;
  98. }
  99. static inline void storebytes(struct tpm_buf *buf, const unsigned char *in,
  100. const int len)
  101. {
  102. memcpy(buf->data + buf->len, in, len);
  103. buf->len += len;
  104. }
  105. #endif