trusted.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 1024
  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. int TSS_authhmac(unsigned char *digest, const unsigned char *key,
  33. unsigned int keylen, unsigned char *h1,
  34. unsigned char *h2, unsigned char h3, ...);
  35. int TSS_checkhmac1(unsigned char *buffer,
  36. const uint32_t command,
  37. const unsigned char *ononce,
  38. const unsigned char *key,
  39. unsigned int keylen, ...);
  40. int trusted_tpm_send(unsigned char *cmd, size_t buflen);
  41. int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce);
  42. #define TPM_DEBUG 0
  43. #if TPM_DEBUG
  44. static inline void dump_options(struct trusted_key_options *o)
  45. {
  46. pr_info("trusted_key: sealing key type %d\n", o->keytype);
  47. pr_info("trusted_key: sealing key handle %0X\n", o->keyhandle);
  48. pr_info("trusted_key: pcrlock %d\n", o->pcrlock);
  49. pr_info("trusted_key: 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_payload(struct trusted_key_payload *p)
  54. {
  55. pr_info("trusted_key: key_len %d\n", p->key_len);
  56. print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE,
  57. 16, 1, p->key, p->key_len, 0);
  58. pr_info("trusted_key: bloblen %d\n", p->blob_len);
  59. print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE,
  60. 16, 1, p->blob, p->blob_len, 0);
  61. pr_info("trusted_key: migratable %d\n", p->migratable);
  62. }
  63. static inline void dump_sess(struct osapsess *s)
  64. {
  65. print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE,
  66. 16, 1, &s->handle, 4, 0);
  67. pr_info("trusted-key: secret:\n");
  68. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
  69. 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0);
  70. pr_info("trusted-key: enonce:\n");
  71. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE,
  72. 16, 1, &s->enonce, SHA1_DIGEST_SIZE, 0);
  73. }
  74. static inline void dump_tpm_buf(unsigned char *buf)
  75. {
  76. int len;
  77. pr_info("\ntrusted-key: tpm buffer\n");
  78. len = LOAD32(buf, TPM_SIZE_OFFSET);
  79. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0);
  80. }
  81. #else
  82. static inline void dump_options(struct trusted_key_options *o)
  83. {
  84. }
  85. static inline void dump_payload(struct trusted_key_payload *p)
  86. {
  87. }
  88. static inline void dump_sess(struct osapsess *s)
  89. {
  90. }
  91. static inline void dump_tpm_buf(unsigned char *buf)
  92. {
  93. }
  94. #endif
  95. static inline void store8(struct tpm_buf *buf, const unsigned char value)
  96. {
  97. buf->data[buf->len++] = value;
  98. }
  99. static inline void store16(struct tpm_buf *buf, const uint16_t value)
  100. {
  101. *(uint16_t *) & buf->data[buf->len] = htons(value);
  102. buf->len += sizeof value;
  103. }
  104. static inline void store32(struct tpm_buf *buf, const uint32_t value)
  105. {
  106. *(uint32_t *) & buf->data[buf->len] = htonl(value);
  107. buf->len += sizeof value;
  108. }
  109. static inline void storebytes(struct tpm_buf *buf, const unsigned char *in,
  110. const int len)
  111. {
  112. memcpy(buf->data + buf->len, in, len);
  113. buf->len += len;
  114. }
  115. #endif