dice_prot_env.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2024, 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 <psa/crypto_types.h>
  10. #include <psa/crypto_values.h>
  11. #include <common/debug.h>
  12. #include <drivers/auth/crypto_mod.h>
  13. #include <drivers/measured_boot/rse/dice_prot_env.h>
  14. #include <lib/cassert.h>
  15. #include <lib/psa/dice_protection_environment.h>
  16. #include <platform_def.h>
  17. #define DPE_ALG_SHA512 0
  18. #define DPE_ALG_SHA384 1
  19. #define DPE_ALG_SHA256 2
  20. #if DPE_ALG_ID == DPE_ALG_SHA512
  21. #define CRYPTO_MD_ID CRYPTO_MD_SHA512
  22. #define PSA_CRYPTO_MD_ID PSA_ALG_SHA_512
  23. #elif DPE_ALG_ID == DPE_ALG_SHA384
  24. #define CRYPTO_MD_ID CRYPTO_MD_SHA384
  25. #define PSA_CRYPTO_MD_ID PSA_ALG_SHA_384
  26. #elif DPE_ALG_ID == DPE_ALG_SHA256
  27. #define CRYPTO_MD_ID CRYPTO_MD_SHA256
  28. #define PSA_CRYPTO_MD_ID PSA_ALG_SHA_256
  29. #else
  30. # error Invalid DPE hash algorithm.
  31. #endif /* DPE_ALG_ID */
  32. /* Ensure that computed hash values fits into the DiceInputValues structure */
  33. CASSERT(DICE_HASH_SIZE >= DPE_DIGEST_SIZE,
  34. assert_digest_size_bigger_than_allocated_buffer);
  35. static int initial_context_handle;
  36. static void map_metadata_to_dice_inputs(struct dpe_metadata *metadata,
  37. DiceInputValues *dice_inputs)
  38. {
  39. /* Hash of the content certificate signing key (public part) */
  40. memcpy(dice_inputs->authority_hash, metadata->signer_id,
  41. DPE_DIGEST_SIZE);
  42. /* SW type string identifier */
  43. assert(metadata->sw_type_size < DICE_CODE_DESCRIPTOR_MAX_SIZE);
  44. dice_inputs->code_descriptor = metadata->sw_type;
  45. dice_inputs->code_descriptor_size = metadata->sw_type_size;
  46. }
  47. void dpe_init(struct dpe_metadata *metadata)
  48. {
  49. assert(metadata != NULL);
  50. /* Init the non-const members of the metadata structure */
  51. while (metadata->id != DPE_INVALID_ID) {
  52. /* Terminating 0 character is not needed due to CBOR encoding */
  53. metadata->sw_type_size =
  54. strlen((const char *)&metadata->sw_type);
  55. metadata++;
  56. }
  57. plat_dpe_get_context_handle(&initial_context_handle);
  58. }
  59. int dpe_measure_and_record(struct dpe_metadata *metadata,
  60. uintptr_t data_base, uint32_t data_size,
  61. uint32_t data_id)
  62. {
  63. static int current_context_handle;
  64. DiceInputValues dice_inputs = { 0 };
  65. int new_parent_context_handle;
  66. int new_context_handle;
  67. dpe_error_t ret;
  68. int rc;
  69. assert(metadata != NULL);
  70. /* Get the metadata associated with this image. */
  71. while ((metadata->id != DPE_INVALID_ID) && (metadata->id != data_id)) {
  72. metadata++;
  73. }
  74. /* If image is not present in metadata array then skip */
  75. if (metadata->id == DPE_INVALID_ID) {
  76. return 0;
  77. }
  78. /* Calculate hash */
  79. rc = crypto_mod_calc_hash(CRYPTO_MD_ID,
  80. (void *)data_base, data_size,
  81. dice_inputs.code_hash);
  82. if (rc != 0) {
  83. return rc;
  84. }
  85. map_metadata_to_dice_inputs(metadata, &dice_inputs);
  86. /* Only at the first call */
  87. if (current_context_handle == 0) {
  88. current_context_handle = initial_context_handle;
  89. }
  90. VERBOSE("Calling dpe_derive_context, image_id: %d\n", metadata->id);
  91. ret = dpe_derive_context(current_context_handle,
  92. metadata->cert_id,
  93. metadata->retain_parent_context,
  94. metadata->allow_new_context_to_derive,
  95. metadata->create_certificate,
  96. &dice_inputs,
  97. metadata->target_locality,
  98. false, /* return_certificate */
  99. true, /* allow_new_context_to_export */
  100. false, /* export_cdi */
  101. &new_context_handle,
  102. &new_parent_context_handle,
  103. NULL, 0, NULL, /* new_certificate_* */
  104. NULL, 0, NULL); /* exported_cdi_* */
  105. if (ret == DPE_NO_ERROR) {
  106. current_context_handle = new_parent_context_handle;
  107. if (metadata->allow_new_context_to_derive == true) {
  108. /* Share new_context_handle with child component:
  109. * e.g: BL2, BL33.
  110. */
  111. VERBOSE("Share new_context_handle with child: 0x%x\n",
  112. new_context_handle);
  113. plat_dpe_share_context_handle(&new_context_handle,
  114. &new_parent_context_handle);
  115. }
  116. } else {
  117. ERROR("dpe_derive_context failed: %d\n", ret);
  118. }
  119. return (ret == DPE_NO_ERROR) ? 0 : -1;
  120. }
  121. int dpe_set_signer_id(struct dpe_metadata *metadata,
  122. const void *pk_oid,
  123. const void *pk_ptr,
  124. size_t pk_len)
  125. {
  126. unsigned char hash_data[CRYPTO_MD_MAX_SIZE];
  127. int rc;
  128. bool hash_calc_done = false;
  129. assert(metadata != NULL);
  130. /*
  131. * Do an exhaustive search over the platform metadata to find
  132. * all images whose key OID matches the one passed in argument.
  133. *
  134. * Note that it is not an error if do not get any matches.
  135. * The platform may decide not to measure all of the images
  136. * in the system.
  137. */
  138. while (metadata->id != DPE_INVALID_ID) {
  139. /* Get the metadata associated with this key-oid */
  140. if (metadata->pk_oid == pk_oid) {
  141. if (hash_calc_done == false) {
  142. /* Calculate public key hash */
  143. rc = crypto_mod_calc_hash(CRYPTO_MD_ID,
  144. (void *)pk_ptr,
  145. pk_len, hash_data);
  146. if (rc != 0) {
  147. return rc;
  148. }
  149. hash_calc_done = true;
  150. }
  151. /*
  152. * Fill the signer-ID field with the newly/already
  153. * computed hash of the public key and update its
  154. * signer ID size field with compile-time decided
  155. * digest size.
  156. */
  157. (void)memcpy(metadata->signer_id,
  158. hash_data,
  159. DPE_DIGEST_SIZE);
  160. metadata->signer_id_size = DPE_DIGEST_SIZE;
  161. }
  162. metadata++;
  163. }
  164. return 0;
  165. }