drtm_measurements.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2022 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * DRTM measurements into TPM PCRs.
  7. *
  8. * Authors:
  9. * Lucian Paul-Trifu <lucian.paultrifu@gmail.com>
  10. *
  11. */
  12. #include <assert.h>
  13. #include <common/debug.h>
  14. #include <drivers/auth/crypto_mod.h>
  15. #include <drivers/measured_boot/event_log/event_log.h>
  16. #include "drtm_main.h"
  17. #include "drtm_measurements.h"
  18. #include <lib/xlat_tables/xlat_tables_v2.h>
  19. /* Event Log buffer */
  20. static uint8_t drtm_event_log[PLAT_DRTM_EVENT_LOG_MAX_SIZE];
  21. /*
  22. * Calculate and write hash of various payloads as per DRTM specification
  23. * to Event Log.
  24. *
  25. * @param[in] data_base Address of data
  26. * @param[in] data_size Size of data
  27. * @param[in] event_type Type of Event
  28. * @param[in] event_name Name of the Event
  29. * @return:
  30. * 0 = success
  31. * < 0 = error
  32. */
  33. static int drtm_event_log_measure_and_record(uintptr_t data_base,
  34. uint32_t data_size,
  35. uint32_t event_type,
  36. const char *event_name,
  37. unsigned int pcr)
  38. {
  39. int rc;
  40. unsigned char hash_data[CRYPTO_MD_MAX_SIZE];
  41. event_log_metadata_t metadata = {0};
  42. metadata.name = event_name;
  43. metadata.pcr = pcr;
  44. /*
  45. * Measure the payloads requested by D-CRTM and DCE components
  46. * Hash algorithm decided by the Event Log driver at build-time
  47. */
  48. rc = event_log_measure(data_base, data_size, hash_data);
  49. if (rc != 0) {
  50. return rc;
  51. }
  52. /* Record the mesasurement in the EventLog buffer */
  53. event_log_record(hash_data, event_type, &metadata);
  54. return 0;
  55. }
  56. /*
  57. * Initialise Event Log global variables, used during the recording
  58. * of various payload measurements into the Event Log buffer
  59. *
  60. * @param[in] event_log_start Base address of Event Log buffer
  61. * @param[in] event_log_finish End address of Event Log buffer,
  62. * it is a first byte past end of the
  63. * buffer
  64. */
  65. static void drtm_event_log_init(uint8_t *event_log_start,
  66. uint8_t *event_log_finish)
  67. {
  68. event_log_buf_init(event_log_start, event_log_finish);
  69. event_log_write_specid_event();
  70. }
  71. enum drtm_retc drtm_take_measurements(const struct_drtm_dl_args *a)
  72. {
  73. int rc;
  74. uintptr_t dlme_img_mapping;
  75. uint64_t dlme_img_ep;
  76. size_t dlme_img_mapping_bytes;
  77. uint8_t drtm_null_data = 0U;
  78. uint8_t pcr_schema = DL_ARGS_GET_PCR_SCHEMA(a);
  79. const char *drtm_event_arm_sep_data = "ARM_DRTM";
  80. /* Initialise the EventLog driver */
  81. drtm_event_log_init(drtm_event_log, drtm_event_log +
  82. sizeof(drtm_event_log));
  83. /**
  84. * Measurements extended into PCR-17.
  85. *
  86. * PCR-17: Measure the DCE image. Extend digest of (char)0 into PCR-17
  87. * since the D-CRTM and the DCE are not separate.
  88. */
  89. rc = drtm_event_log_measure_and_record((uintptr_t)&drtm_null_data,
  90. sizeof(drtm_null_data),
  91. DRTM_EVENT_ARM_DCE, NULL,
  92. PCR_17);
  93. CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DCE));
  94. /* PCR-17: Measure the PCR schema DRTM launch argument. */
  95. rc = drtm_event_log_measure_and_record((uintptr_t)&pcr_schema,
  96. sizeof(pcr_schema),
  97. DRTM_EVENT_ARM_PCR_SCHEMA,
  98. NULL, PCR_17);
  99. CHECK_RC(rc,
  100. drtm_event_log_measure_and_record(DRTM_EVENT_ARM_PCR_SCHEMA));
  101. /* PCR-17: Measure the enable state of external-debug, and trace. */
  102. /*
  103. * TODO: Measure the enable state of external-debug and trace. This should
  104. * be returned through a platform-specific hook.
  105. */
  106. /* PCR-17: Measure the security lifecycle state. */
  107. /*
  108. * TODO: Measure the security lifecycle state. This is an implementation-
  109. * defined value, retrieved through an implementation-defined mechanisms.
  110. */
  111. /*
  112. * PCR-17: Optionally measure the NWd DCE.
  113. * It is expected that such subsequent DCE stages are signed and verified.
  114. * Whether they are measured in addition to signing is implementation
  115. * -defined.
  116. * Here the choice is to not measure any NWd DCE, in favour of PCR value
  117. * resilience to any NWd DCE updates.
  118. */
  119. /* PCR-17: End of DCE measurements. */
  120. rc = drtm_event_log_measure_and_record((uintptr_t)drtm_event_arm_sep_data,
  121. strlen(drtm_event_arm_sep_data),
  122. DRTM_EVENT_ARM_SEPARATOR, NULL,
  123. PCR_17);
  124. CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_SEPARATOR));
  125. /**
  126. * Measurements extended into PCR-18.
  127. *
  128. * PCR-18: Measure the PCR schema DRTM launch argument.
  129. */
  130. rc = drtm_event_log_measure_and_record((uintptr_t)&pcr_schema,
  131. sizeof(pcr_schema),
  132. DRTM_EVENT_ARM_PCR_SCHEMA,
  133. NULL, PCR_18);
  134. CHECK_RC(rc,
  135. drtm_event_log_measure_and_record(DRTM_EVENT_ARM_PCR_SCHEMA));
  136. /*
  137. * PCR-18: Measure the public key used to verify DCE image(s) signatures.
  138. * Extend digest of (char)0, since we do not expect the NWd DCE to be
  139. * present.
  140. */
  141. assert(a->dce_nwd_size == 0);
  142. rc = drtm_event_log_measure_and_record((uintptr_t)&drtm_null_data,
  143. sizeof(drtm_null_data),
  144. DRTM_EVENT_ARM_DCE_PUBKEY,
  145. NULL, PCR_18);
  146. CHECK_RC(rc,
  147. drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DCE_PUBKEY));
  148. /* PCR-18: Measure the DLME image. */
  149. dlme_img_mapping_bytes = page_align(a->dlme_img_size, UP);
  150. rc = mmap_add_dynamic_region_alloc_va(a->dlme_paddr + a->dlme_img_off,
  151. &dlme_img_mapping,
  152. dlme_img_mapping_bytes, MT_RO_DATA | MT_NS);
  153. if (rc) {
  154. WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n",
  155. __func__, rc);
  156. return INTERNAL_ERROR;
  157. }
  158. rc = drtm_event_log_measure_and_record(dlme_img_mapping, a->dlme_img_size,
  159. DRTM_EVENT_ARM_DLME, NULL,
  160. PCR_18);
  161. CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DLME));
  162. rc = mmap_remove_dynamic_region(dlme_img_mapping, dlme_img_mapping_bytes);
  163. CHECK_RC(rc, mmap_remove_dynamic_region);
  164. /* PCR-18: Measure the DLME image entry point. */
  165. dlme_img_ep = DL_ARGS_GET_DLME_ENTRY_POINT(a);
  166. drtm_event_log_measure_and_record((uintptr_t)&dlme_img_ep,
  167. sizeof(dlme_img_ep),
  168. DRTM_EVENT_ARM_DLME_EP, NULL,
  169. PCR_18);
  170. CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DLME_EP));
  171. /* PCR-18: End of DCE measurements. */
  172. rc = drtm_event_log_measure_and_record((uintptr_t)drtm_event_arm_sep_data,
  173. strlen(drtm_event_arm_sep_data),
  174. DRTM_EVENT_ARM_SEPARATOR, NULL,
  175. PCR_18);
  176. CHECK_RC(rc,
  177. drtm_event_log_measure_and_record(DRTM_EVENT_ARM_SEPARATOR));
  178. /*
  179. * If the DCE is unable to log a measurement because there is no available
  180. * space in the event log region, the DCE must extend a hash of the value
  181. * 0xFF (1 byte in size) into PCR[17] and PCR[18] and enter remediation.
  182. */
  183. return SUCCESS;
  184. }
  185. void drtm_serialise_event_log(uint8_t *dst, size_t *event_log_size_out)
  186. {
  187. *event_log_size_out = event_log_get_cur_size(drtm_event_log);
  188. memcpy(dst, drtm_event_log, *event_log_size_out);
  189. }