rse_measured_boot.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2022-2023, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <common/debug.h>
  10. #include <drivers/auth/crypto_mod.h>
  11. #include <drivers/measured_boot/rse/rse_measured_boot.h>
  12. #include <lib/psa/measured_boot.h>
  13. #include <psa/crypto_types.h>
  14. #include <psa/crypto_values.h>
  15. #include <psa/error.h>
  16. #define MBOOT_ALG_SHA512 0
  17. #define MBOOT_ALG_SHA384 1
  18. #define MBOOT_ALG_SHA256 2
  19. #if MBOOT_ALG_ID == MBOOT_ALG_SHA512
  20. #define CRYPTO_MD_ID CRYPTO_MD_SHA512
  21. #define PSA_CRYPTO_MD_ID PSA_ALG_SHA_512
  22. #elif MBOOT_ALG_ID == MBOOT_ALG_SHA384
  23. #define CRYPTO_MD_ID CRYPTO_MD_SHA384
  24. #define PSA_CRYPTO_MD_ID PSA_ALG_SHA_384
  25. #elif MBOOT_ALG_ID == MBOOT_ALG_SHA256
  26. #define CRYPTO_MD_ID CRYPTO_MD_SHA256
  27. #define PSA_CRYPTO_MD_ID PSA_ALG_SHA_256
  28. #else
  29. # error Invalid Measured Boot algorithm.
  30. #endif /* MBOOT_ALG_ID */
  31. #if ENABLE_ASSERTIONS
  32. static bool null_arr(const uint8_t *signer_id, size_t signer_id_size)
  33. {
  34. for (size_t i = 0U; i < signer_id_size; i++) {
  35. if (signer_id[i] != 0U) {
  36. return false;
  37. }
  38. }
  39. return true;
  40. }
  41. #endif /* ENABLE_ASSERTIONS */
  42. /* Functions' declarations */
  43. void rse_measured_boot_init(struct rse_mboot_metadata *metadata_ptr)
  44. {
  45. assert(metadata_ptr != NULL);
  46. /* Init the non-const members of the metadata structure */
  47. while (metadata_ptr->id != RSE_MBOOT_INVALID_ID) {
  48. assert(null_arr(metadata_ptr->signer_id, MBOOT_DIGEST_SIZE));
  49. metadata_ptr->sw_type_size =
  50. strlen((const char *)&metadata_ptr->sw_type) + 1;
  51. metadata_ptr++;
  52. }
  53. }
  54. int rse_mboot_measure_and_record(struct rse_mboot_metadata *metadata_ptr,
  55. uintptr_t data_base, uint32_t data_size,
  56. uint32_t data_id)
  57. {
  58. unsigned char hash_data[CRYPTO_MD_MAX_SIZE];
  59. int rc;
  60. psa_status_t ret;
  61. assert(metadata_ptr != NULL);
  62. /* Get the metadata associated with this image. */
  63. while ((metadata_ptr->id != RSE_MBOOT_INVALID_ID) &&
  64. (metadata_ptr->id != data_id)) {
  65. metadata_ptr++;
  66. }
  67. /* If image is not present in metadata array then skip */
  68. if (metadata_ptr->id == RSE_MBOOT_INVALID_ID) {
  69. return 0;
  70. }
  71. /* Calculate hash */
  72. rc = crypto_mod_calc_hash(CRYPTO_MD_ID,
  73. (void *)data_base, data_size, hash_data);
  74. if (rc != 0) {
  75. return rc;
  76. }
  77. ret = rse_measured_boot_extend_measurement(
  78. metadata_ptr->slot,
  79. metadata_ptr->signer_id,
  80. metadata_ptr->signer_id_size,
  81. metadata_ptr->version,
  82. metadata_ptr->version_size,
  83. PSA_CRYPTO_MD_ID,
  84. metadata_ptr->sw_type,
  85. metadata_ptr->sw_type_size,
  86. hash_data,
  87. MBOOT_DIGEST_SIZE,
  88. metadata_ptr->lock_measurement);
  89. if (ret != PSA_SUCCESS) {
  90. return ret;
  91. }
  92. return 0;
  93. }
  94. int rse_mboot_set_signer_id(struct rse_mboot_metadata *metadata_ptr,
  95. const void *pk_oid,
  96. const void *pk_ptr,
  97. size_t pk_len)
  98. {
  99. unsigned char hash_data[CRYPTO_MD_MAX_SIZE];
  100. int rc;
  101. bool hash_calc_done = false;
  102. assert(metadata_ptr != NULL);
  103. /*
  104. * Do an exhaustive search over the platform metadata to find
  105. * all images whose key OID matches the one passed in argument.
  106. *
  107. * Note that it is not an error if do not get any matches.
  108. * The platform may decide not to measure all of the images
  109. * in the system.
  110. */
  111. while (metadata_ptr->id != RSE_MBOOT_INVALID_ID) {
  112. /* Get the metadata associated with this key-oid */
  113. if (metadata_ptr->pk_oid == pk_oid) {
  114. if (hash_calc_done == false) {
  115. /* Calculate public key hash */
  116. rc = crypto_mod_calc_hash(CRYPTO_MD_ID,
  117. (void *)pk_ptr,
  118. pk_len, hash_data);
  119. if (rc != 0) {
  120. return rc;
  121. }
  122. hash_calc_done = true;
  123. }
  124. /*
  125. * Fill the signer-ID field with the newly/already
  126. * computed hash of the public key and update its
  127. * signer ID size field with compile-time decided
  128. * digest size.
  129. */
  130. (void)memcpy(metadata_ptr->signer_id,
  131. hash_data,
  132. MBOOT_DIGEST_SIZE);
  133. metadata_ptr->signer_id_size = MBOOT_DIGEST_SIZE;
  134. }
  135. metadata_ptr++;
  136. }
  137. return 0;
  138. }