measured_boot.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2022-2024, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <string.h>
  8. #include <common/debug.h>
  9. #include <drivers/measured_boot/metadata.h>
  10. #include <measured_boot.h>
  11. #include <psa/client.h>
  12. #include <psa_manifest/sid.h>
  13. #include "measured_boot_private.h"
  14. static void print_byte_array(const uint8_t *array __unused, size_t len __unused)
  15. {
  16. #if LOG_LEVEL >= LOG_LEVEL_INFO
  17. size_t i;
  18. if (array == NULL || len == 0U) {
  19. (void)printf("\n");
  20. } else {
  21. for (i = 0U; i < len; ++i) {
  22. (void)printf(" %02x", array[i]);
  23. if ((i & U(0xF)) == U(0xF)) {
  24. (void)printf("\n");
  25. if (i < (len - 1U)) {
  26. INFO("\t\t:");
  27. }
  28. }
  29. }
  30. }
  31. #endif
  32. }
  33. static void log_measurement(uint8_t index,
  34. const uint8_t *signer_id,
  35. size_t signer_id_size,
  36. const uint8_t *version, /* string */
  37. size_t version_size,
  38. const uint8_t *sw_type, /* string */
  39. size_t sw_type_size,
  40. uint32_t measurement_algo,
  41. const uint8_t *measurement_value,
  42. size_t measurement_value_size,
  43. bool lock_measurement)
  44. {
  45. INFO("Measured boot extend measurement:\n");
  46. INFO(" - slot : %u\n", index);
  47. INFO(" - signer_id :");
  48. print_byte_array(signer_id, signer_id_size);
  49. INFO(" - version : %s\n", version);
  50. INFO(" - version_size: %zu\n", version_size);
  51. INFO(" - sw_type : %s\n", sw_type);
  52. INFO(" - sw_type_size: %zu\n", sw_type_size);
  53. INFO(" - algorithm : %x\n", measurement_algo);
  54. INFO(" - measurement :");
  55. print_byte_array(measurement_value, measurement_value_size);
  56. INFO(" - locking : %s\n", lock_measurement ? "true" : "false");
  57. }
  58. psa_status_t
  59. rse_measured_boot_extend_measurement(uint8_t index,
  60. const uint8_t *signer_id,
  61. size_t signer_id_size,
  62. const uint8_t *version,
  63. size_t version_size,
  64. uint32_t measurement_algo,
  65. const uint8_t *sw_type,
  66. size_t sw_type_size,
  67. const uint8_t *measurement_value,
  68. size_t measurement_value_size,
  69. bool lock_measurement)
  70. {
  71. struct measured_boot_extend_iovec_t extend_iov = {
  72. .index = index,
  73. .lock_measurement = lock_measurement,
  74. .measurement_algo = measurement_algo,
  75. .sw_type = {0},
  76. .sw_type_size = sw_type_size,
  77. };
  78. if (version_size > VERSION_MAX_SIZE) {
  79. return PSA_ERROR_INVALID_ARGUMENT;
  80. }
  81. if (version_size > 0 && version[version_size - 1] == '\0') {
  82. version_size--;
  83. }
  84. psa_invec in_vec[] = {
  85. {.base = &extend_iov,
  86. .len = sizeof(struct measured_boot_extend_iovec_t)},
  87. {.base = signer_id, .len = signer_id_size},
  88. {.base = version, .len = version_size },
  89. {.base = measurement_value, .len = measurement_value_size}
  90. };
  91. if (sw_type != NULL) {
  92. if (extend_iov.sw_type_size > SW_TYPE_MAX_SIZE) {
  93. return PSA_ERROR_INVALID_ARGUMENT;
  94. }
  95. if (sw_type_size > 0 && sw_type[sw_type_size - 1] == '\0') {
  96. extend_iov.sw_type_size--;
  97. }
  98. memcpy(extend_iov.sw_type, sw_type, extend_iov.sw_type_size);
  99. }
  100. log_measurement(index, signer_id, signer_id_size,
  101. version, version_size, sw_type, sw_type_size,
  102. measurement_algo, measurement_value,
  103. measurement_value_size, lock_measurement);
  104. return psa_call(RSE_MEASURED_BOOT_HANDLE,
  105. RSE_MEASURED_BOOT_EXTEND,
  106. in_vec, IOVEC_LEN(in_vec),
  107. NULL, 0);
  108. }
  109. psa_status_t rse_measured_boot_read_measurement(uint8_t index,
  110. uint8_t *signer_id,
  111. size_t signer_id_size,
  112. size_t *signer_id_len,
  113. uint8_t *version,
  114. size_t version_size,
  115. size_t *version_len,
  116. uint32_t *measurement_algo,
  117. uint8_t *sw_type,
  118. size_t sw_type_size,
  119. size_t *sw_type_len,
  120. uint8_t *measurement_value,
  121. size_t measurement_value_size,
  122. size_t *measurement_value_len,
  123. bool *is_locked)
  124. {
  125. psa_status_t status;
  126. struct measured_boot_read_iovec_in_t read_iov_in = {
  127. .index = index,
  128. .sw_type_size = sw_type_size,
  129. .version_size = version_size,
  130. };
  131. struct measured_boot_read_iovec_out_t read_iov_out;
  132. psa_invec in_vec[] = {
  133. {.base = &read_iov_in,
  134. .len = sizeof(struct measured_boot_read_iovec_in_t)},
  135. };
  136. psa_outvec out_vec[] = {
  137. {.base = &read_iov_out,
  138. .len = sizeof(struct measured_boot_read_iovec_out_t)},
  139. {.base = signer_id, .len = signer_id_size},
  140. {.base = measurement_value, .len = measurement_value_size}
  141. };
  142. status = psa_call(RSE_MEASURED_BOOT_HANDLE, RSE_MEASURED_BOOT_READ,
  143. in_vec, IOVEC_LEN(in_vec),
  144. out_vec, IOVEC_LEN(out_vec));
  145. if (status == PSA_SUCCESS) {
  146. *is_locked = read_iov_out.is_locked;
  147. *measurement_algo = read_iov_out.measurement_algo;
  148. *sw_type_len = read_iov_out.sw_type_len;
  149. *version_len = read_iov_out.version_len;
  150. memcpy(sw_type, read_iov_out.sw_type, read_iov_out.sw_type_len);
  151. memcpy(version, read_iov_out.version, read_iov_out.version_len);
  152. *signer_id_len = out_vec[1].len;
  153. *measurement_value_len = out_vec[2].len;
  154. }
  155. return status;
  156. }