io_encrypted.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2020, Linaro Limited. All rights reserved.
  3. * Author: Sumit Garg <sumit.garg@linaro.org>
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <errno.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <platform_def.h>
  12. #include <common/bl_common.h>
  13. #include <common/debug.h>
  14. #include <drivers/auth/crypto_mod.h>
  15. #include <drivers/io/io_driver.h>
  16. #include <drivers/io/io_encrypted.h>
  17. #include <drivers/io/io_storage.h>
  18. #include <lib/utils.h>
  19. #include <plat/common/platform.h>
  20. #include <tools_share/firmware_encrypted.h>
  21. #include <tools_share/uuid.h>
  22. static uintptr_t backend_dev_handle;
  23. static uintptr_t backend_dev_spec;
  24. static uintptr_t backend_handle;
  25. static uintptr_t backend_image_spec;
  26. static io_dev_info_t enc_dev_info;
  27. /* Encrypted firmware driver functions */
  28. static int enc_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
  29. static int enc_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
  30. io_entity_t *entity);
  31. static int enc_file_len(io_entity_t *entity, size_t *length);
  32. static int enc_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
  33. size_t *length_read);
  34. static int enc_file_close(io_entity_t *entity);
  35. static int enc_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
  36. static int enc_dev_close(io_dev_info_t *dev_info);
  37. static inline int is_valid_header(struct fw_enc_hdr *header)
  38. {
  39. if (header->magic == ENC_HEADER_MAGIC)
  40. return 1;
  41. else
  42. return 0;
  43. }
  44. static io_type_t device_type_enc(void)
  45. {
  46. return IO_TYPE_ENCRYPTED;
  47. }
  48. static const io_dev_connector_t enc_dev_connector = {
  49. .dev_open = enc_dev_open
  50. };
  51. static const io_dev_funcs_t enc_dev_funcs = {
  52. .type = device_type_enc,
  53. .open = enc_file_open,
  54. .seek = NULL,
  55. .size = enc_file_len,
  56. .read = enc_file_read,
  57. .write = NULL,
  58. .close = enc_file_close,
  59. .dev_init = enc_dev_init,
  60. .dev_close = enc_dev_close,
  61. };
  62. static int enc_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info)
  63. {
  64. assert(dev_info != NULL);
  65. enc_dev_info.funcs = &enc_dev_funcs;
  66. *dev_info = &enc_dev_info;
  67. return 0;
  68. }
  69. static int enc_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
  70. {
  71. int result;
  72. unsigned int image_id = (unsigned int)init_params;
  73. /* Obtain a reference to the image by querying the platform layer */
  74. result = plat_get_image_source(image_id, &backend_dev_handle,
  75. &backend_dev_spec);
  76. if (result != 0) {
  77. WARN("Failed to obtain reference to image id=%u (%i)\n",
  78. image_id, result);
  79. return -ENOENT;
  80. }
  81. return result;
  82. }
  83. static int enc_dev_close(io_dev_info_t *dev_info)
  84. {
  85. backend_dev_handle = (uintptr_t)NULL;
  86. backend_dev_spec = (uintptr_t)NULL;
  87. return 0;
  88. }
  89. static int enc_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
  90. io_entity_t *entity)
  91. {
  92. int result;
  93. assert(spec != 0);
  94. assert(entity != NULL);
  95. backend_image_spec = spec;
  96. result = io_open(backend_dev_handle, backend_image_spec,
  97. &backend_handle);
  98. if (result != 0) {
  99. WARN("Failed to open backend device (%i)\n", result);
  100. result = -ENOENT;
  101. }
  102. return result;
  103. }
  104. static int enc_file_len(io_entity_t *entity, size_t *length)
  105. {
  106. int result;
  107. assert(entity != NULL);
  108. assert(length != NULL);
  109. result = io_size(backend_handle, length);
  110. if (result != 0) {
  111. WARN("Failed to read blob length (%i)\n", result);
  112. return -ENOENT;
  113. }
  114. /*
  115. * Encryption header is attached at the beginning of the encrypted file
  116. * and is not considered a part of the payload.
  117. */
  118. if (*length < sizeof(struct fw_enc_hdr))
  119. return -EIO;
  120. *length -= sizeof(struct fw_enc_hdr);
  121. return result;
  122. }
  123. static int enc_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
  124. size_t *length_read)
  125. {
  126. int result;
  127. struct fw_enc_hdr header;
  128. enum fw_enc_status_t fw_enc_status;
  129. size_t bytes_read;
  130. uint8_t key[ENC_MAX_KEY_SIZE];
  131. size_t key_len = sizeof(key);
  132. unsigned int key_flags = 0;
  133. const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)backend_image_spec;
  134. assert(entity != NULL);
  135. assert(length_read != NULL);
  136. result = io_read(backend_handle, (uintptr_t)&header, sizeof(header),
  137. &bytes_read);
  138. if (result != 0) {
  139. WARN("Failed to read encryption header (%i)\n", result);
  140. return -ENOENT;
  141. }
  142. if (!is_valid_header(&header)) {
  143. WARN("Encryption header check failed.\n");
  144. return -ENOENT;
  145. }
  146. VERBOSE("Encryption header looks OK.\n");
  147. fw_enc_status = header.flags & FW_ENC_STATUS_FLAG_MASK;
  148. if ((header.iv_len > ENC_MAX_IV_SIZE) ||
  149. (header.tag_len > ENC_MAX_TAG_SIZE)) {
  150. WARN("Incorrect IV or tag length\n");
  151. return -ENOENT;
  152. }
  153. result = io_read(backend_handle, buffer, length, &bytes_read);
  154. if (result != 0) {
  155. WARN("Failed to read encrypted payload (%i)\n", result);
  156. return -ENOENT;
  157. }
  158. *length_read = bytes_read;
  159. result = plat_get_enc_key_info(fw_enc_status, key, &key_len, &key_flags,
  160. (uint8_t *)&uuid_spec->uuid,
  161. sizeof(uuid_t));
  162. if (result != 0) {
  163. WARN("Failed to obtain encryption key (%i)\n", result);
  164. return -ENOENT;
  165. }
  166. result = crypto_mod_auth_decrypt(header.dec_algo,
  167. (void *)buffer, *length_read, key,
  168. key_len, key_flags, header.iv,
  169. header.iv_len, header.tag,
  170. header.tag_len);
  171. memset(key, 0, key_len);
  172. if (result != 0) {
  173. ERROR("File decryption failed (%i)\n", result);
  174. return -ENOENT;
  175. }
  176. return result;
  177. }
  178. static int enc_file_close(io_entity_t *entity)
  179. {
  180. io_close(backend_handle);
  181. backend_image_spec = (uintptr_t)NULL;
  182. entity->info = 0;
  183. return 0;
  184. }
  185. /* Exported functions */
  186. /* Register the Encrypted Firmware driver with the IO abstraction */
  187. int register_io_dev_enc(const io_dev_connector_t **dev_con)
  188. {
  189. int result;
  190. assert(dev_con != NULL);
  191. result = io_register_device(&enc_dev_info);
  192. if (result == 0)
  193. *dev_con = &enc_dev_connector;
  194. return result;
  195. }