Advertisement
jenswikl

Untitled

Nov 18th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. struct buf_state {
  2. u8 *buf;
  3. size_t blen;
  4. size_t pos;
  5. bool output;
  6. bool out_of_memory;
  7. };
  8.  
  9. struct tpml_digest_values {
  10. u32 count;
  11. /* can be up to TPM2_NUM_PCR_BANKS */
  12. struct tpmt_ha *digests;
  13. };
  14.  
  15. struct tcg_pcr_event2 {
  16. u32 pcr_index;
  17. u32 event_type;
  18. struct tpml_digest_values digests;
  19. u32 event_size;
  20. u8 *event;
  21. }
  22.  
  23. static bool check_space(struct buf_state *bs, size_t size)
  24. {
  25. return (bs->pos + size <= bs->blen);
  26. }
  27.  
  28. static bool do_u32(struct buf_state *bs, u32 *v)
  29. {
  30. if (!check_space(bs, sizeof(*v))
  31. return false;
  32. if (bs->output)
  33. put_unaligned_le32(*v, bs->buf + bs->pos);
  34. else
  35. *v = get_unaligned_le32(bs->buf + bs->pos);
  36. return true;
  37. }
  38.  
  39. static bool do_u8_array(struct buf_state *bs, u32 *count, u8 **array)
  40. {
  41. if (!do_u32(bs, count))
  42. return false;
  43. if (!check_space(bs, *count)
  44. return false;
  45. if (bs->output) {
  46. memcpy(bs->buf * bs->pos, *array, *count);
  47. } else {
  48. *array = malloc(*count);
  49. if (!*array) {
  50. bs->out_of_memory = true;
  51. return false;
  52. }
  53. memcpy(*array, bs->buf * bs->pos, *count);
  54. }
  55.  
  56. return true;
  57. }
  58.  
  59. static bool do_tpml_digest_values(struct buf_state *bs,
  60. struct tpml_digest_values *v)
  61. {
  62. return do_tpmt_ha_array(bs, &v->count, &v->digests);
  63. }
  64.  
  65. static bool do_tcg_pcr_event2(struct buf_state *bs, struct tcg_pcr_event2 *v)
  66. {
  67. return do_u32(bs, &v->pcr_index) && do_u32(bs, &v->event_type) &&
  68. do_tpml_digest_values(bs, &v->digests) &&
  69. do_u8_array(bs, &v->event_size, &v->event);
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement