trusted.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef __TRUSTED_KEY_H
  2. #define __TRUSTED_KEY_H
  3. /* implementation specific TPM constants */
  4. #define MAX_PCRINFO_SIZE 64
  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. struct trusted_key_options {
  33. uint16_t keytype;
  34. uint32_t keyhandle;
  35. unsigned char keyauth[SHA1_DIGEST_SIZE];
  36. unsigned char blobauth[SHA1_DIGEST_SIZE];
  37. uint32_t pcrinfo_len;
  38. unsigned char pcrinfo[MAX_PCRINFO_SIZE];
  39. int pcrlock;
  40. };
  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. static inline void store8(struct tpm_buf *buf, const unsigned char value)
  95. {
  96. buf->data[buf->len++] = value;
  97. }
  98. static inline void store16(struct tpm_buf *buf, const uint16_t value)
  99. {
  100. *(uint16_t *) & buf->data[buf->len] = htons(value);
  101. buf->len += sizeof value;
  102. }
  103. static inline void store32(struct tpm_buf *buf, const uint32_t value)
  104. {
  105. *(uint32_t *) & buf->data[buf->len] = htonl(value);
  106. buf->len += sizeof value;
  107. }
  108. static inline void storebytes(struct tpm_buf *buf, const unsigned char *in,
  109. const int len)
  110. {
  111. memcpy(buf->data + buf->len, in, len);
  112. buf->len += len;
  113. }
  114. #endif