juno_trusted_boot.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2019-2020, 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 <plat/arm/common/plat_arm.h>
  10. #include <plat/common/common_def.h>
  11. #include <plat/common/platform.h>
  12. #if (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_REGS_ID)
  13. static unsigned char rotpk_hash_der[ARM_ROTPK_HEADER_LEN + ARM_ROTPK_HASH_LEN];
  14. extern unsigned char arm_rotpk_header[];
  15. /*
  16. * Return the ROTPK hash stored in the registers of Juno board.
  17. */
  18. static int juno_get_rotpk_info_regs(void **key_ptr, unsigned int *key_len,
  19. unsigned int *flags)
  20. {
  21. uint8_t *dst;
  22. uint32_t *src, tmp;
  23. unsigned int words, i;
  24. assert(key_ptr != NULL);
  25. assert(key_len != NULL);
  26. assert(flags != NULL);
  27. /* Copy the DER header */
  28. memcpy(rotpk_hash_der, arm_rotpk_header, ARM_ROTPK_HEADER_LEN);
  29. dst = (uint8_t *)&rotpk_hash_der[ARM_ROTPK_HEADER_LEN];
  30. /*
  31. * Append the hash from Trusted Root-Key Storage registers. The hash has
  32. * not been written linearly into the registers, so we have to do a bit
  33. * of byte swapping:
  34. *
  35. * 0x00 0x04 0x08 0x0C 0x10 0x14 0x18 0x1C
  36. * +---------------------------------------------------------------+
  37. * | Reg0 | Reg1 | Reg2 | Reg3 | Reg4 | Reg5 | Reg6 | Reg7 |
  38. * +---------------------------------------------------------------+
  39. * | ... ... | | ... ... |
  40. * | +--------------------+ | +-------+
  41. * | | | |
  42. * +----------------------------+ +----------------------------+
  43. * | | | |
  44. * +-------+ | +--------------------+ |
  45. * | | | |
  46. * v v v v
  47. * +---------------------------------------------------------------+
  48. * | | |
  49. * +---------------------------------------------------------------+
  50. * 0 15 16 31
  51. *
  52. * Additionally, we have to access the registers in 32-bit words
  53. */
  54. words = ARM_ROTPK_HASH_LEN >> 3;
  55. /* Swap bytes 0-15 (first four registers) */
  56. src = (uint32_t *)TZ_PUB_KEY_HASH_BASE;
  57. for (i = 0 ; i < words ; i++) {
  58. tmp = src[words - 1 - i];
  59. /* Words are read in little endian */
  60. *dst++ = (uint8_t)((tmp >> 24) & 0xFF);
  61. *dst++ = (uint8_t)((tmp >> 16) & 0xFF);
  62. *dst++ = (uint8_t)((tmp >> 8) & 0xFF);
  63. *dst++ = (uint8_t)(tmp & 0xFF);
  64. }
  65. /* Swap bytes 16-31 (last four registers) */
  66. src = (uint32_t *)(TZ_PUB_KEY_HASH_BASE + ARM_ROTPK_HASH_LEN / 2);
  67. for (i = 0 ; i < words ; i++) {
  68. tmp = src[words - 1 - i];
  69. *dst++ = (uint8_t)((tmp >> 24) & 0xFF);
  70. *dst++ = (uint8_t)((tmp >> 16) & 0xFF);
  71. *dst++ = (uint8_t)((tmp >> 8) & 0xFF);
  72. *dst++ = (uint8_t)(tmp & 0xFF);
  73. }
  74. *key_ptr = (void *)rotpk_hash_der;
  75. *key_len = (unsigned int)sizeof(rotpk_hash_der);
  76. *flags = ROTPK_IS_HASH;
  77. return 0;
  78. }
  79. #endif
  80. /*
  81. * Return the ROTPK hash in the following ASN.1 structure in DER format:
  82. *
  83. * AlgorithmIdentifier ::= SEQUENCE {
  84. * algorithm OBJECT IDENTIFIER,
  85. * parameters ANY DEFINED BY algorithm OPTIONAL
  86. * }
  87. *
  88. * DigestInfo ::= SEQUENCE {
  89. * digestAlgorithm AlgorithmIdentifier,
  90. * digest OCTET STRING
  91. * }
  92. */
  93. int plat_get_rotpk_info(void *cookie, void **key_ptr, unsigned int *key_len,
  94. unsigned int *flags)
  95. {
  96. #if (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_RSA_ID) || \
  97. (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_DEVEL_ECDSA_ID)
  98. return arm_get_rotpk_info_dev(key_ptr, key_len, flags);
  99. #elif (ARM_ROTPK_LOCATION_ID == ARM_ROTPK_REGS_ID)
  100. return juno_get_rotpk_info_regs(key_ptr, key_len, flags);
  101. #else
  102. return 1;
  103. #endif
  104. }