trusted.h 3.2 KB

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