image-cipher.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2019, Softathome
  4. */
  5. #ifdef USE_HOSTCC
  6. #include "mkimage.h"
  7. #include <time.h>
  8. #else
  9. #include <common.h>
  10. #include <malloc.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. #endif /* !USE_HOSdTCC*/
  13. #include <image.h>
  14. #include <uboot_aes.h>
  15. #include <u-boot/aes.h>
  16. struct cipher_algo cipher_algos[] = {
  17. {
  18. .name = "aes128",
  19. .key_len = AES128_KEY_LENGTH,
  20. .iv_len = AES_BLOCK_LENGTH,
  21. #if IMAGE_ENABLE_ENCRYPT
  22. .calculate_type = EVP_aes_128_cbc,
  23. #endif
  24. .encrypt = image_aes_encrypt,
  25. .decrypt = image_aes_decrypt,
  26. .add_cipher_data = image_aes_add_cipher_data
  27. },
  28. {
  29. .name = "aes192",
  30. .key_len = AES192_KEY_LENGTH,
  31. .iv_len = AES_BLOCK_LENGTH,
  32. #if IMAGE_ENABLE_ENCRYPT
  33. .calculate_type = EVP_aes_192_cbc,
  34. #endif
  35. .encrypt = image_aes_encrypt,
  36. .decrypt = image_aes_decrypt,
  37. .add_cipher_data = image_aes_add_cipher_data
  38. },
  39. {
  40. .name = "aes256",
  41. .key_len = AES256_KEY_LENGTH,
  42. .iv_len = AES_BLOCK_LENGTH,
  43. #if IMAGE_ENABLE_ENCRYPT
  44. .calculate_type = EVP_aes_256_cbc,
  45. #endif
  46. .encrypt = image_aes_encrypt,
  47. .decrypt = image_aes_decrypt,
  48. .add_cipher_data = image_aes_add_cipher_data
  49. }
  50. };
  51. struct cipher_algo *image_get_cipher_algo(const char *full_name)
  52. {
  53. int i;
  54. const char *name;
  55. for (i = 0; i < ARRAY_SIZE(cipher_algos); i++) {
  56. name = cipher_algos[i].name;
  57. if (!strncmp(name, full_name, strlen(name)))
  58. return &cipher_algos[i];
  59. }
  60. return NULL;
  61. }
  62. static int fit_image_setup_decrypt(struct image_cipher_info *info,
  63. const void *fit, int image_noffset,
  64. int cipher_noffset)
  65. {
  66. const void *fdt = gd_fdt_blob();
  67. const char *node_name;
  68. char node_path[128];
  69. int noffset;
  70. char *algo_name;
  71. int ret;
  72. node_name = fit_get_name(fit, image_noffset, NULL);
  73. if (!node_name) {
  74. printf("Can't get node name\n");
  75. return -1;
  76. }
  77. if (fit_image_cipher_get_algo(fit, cipher_noffset, &algo_name)) {
  78. printf("Can't get algo name for cipher '%s' in image '%s'\n",
  79. node_name, node_name);
  80. return -1;
  81. }
  82. info->keyname = fdt_getprop(fit, cipher_noffset, FIT_KEY_HINT, NULL);
  83. if (!info->keyname) {
  84. printf("Can't get key name\n");
  85. return -1;
  86. }
  87. info->iv = fdt_getprop(fit, cipher_noffset, "iv", NULL);
  88. info->ivname = fdt_getprop(fit, cipher_noffset, "iv-name-hint", NULL);
  89. if (!info->iv && !info->ivname) {
  90. printf("Can't get IV or IV name\n");
  91. return -1;
  92. }
  93. info->fit = fit;
  94. info->node_noffset = image_noffset;
  95. info->name = algo_name;
  96. info->cipher = image_get_cipher_algo(algo_name);
  97. if (!info->cipher) {
  98. printf("Can't get cipher\n");
  99. return -1;
  100. }
  101. ret = fit_image_get_data_size_unciphered(fit, image_noffset,
  102. &info->size_unciphered);
  103. if (ret) {
  104. printf("Can't get size of unciphered data\n");
  105. return -1;
  106. }
  107. /*
  108. * Search the cipher node in the u-boot fdt
  109. * the path should be: /cipher/key-<algo>-<key>-<iv>
  110. */
  111. if (info->ivname)
  112. snprintf(node_path, sizeof(node_path), "/%s/key-%s-%s-%s",
  113. FIT_CIPHER_NODENAME, algo_name, info->keyname, info->ivname);
  114. else
  115. snprintf(node_path, sizeof(node_path), "/%s/key-%s-%s",
  116. FIT_CIPHER_NODENAME, algo_name, info->keyname);
  117. noffset = fdt_path_offset(fdt, node_path);
  118. if (noffset < 0) {
  119. printf("Can't found cipher node offset\n");
  120. return -1;
  121. }
  122. /* read key */
  123. info->key = fdt_getprop(fdt, noffset, "key", NULL);
  124. if (!info->key) {
  125. printf("Can't get key in cipher node '%s'\n", node_path);
  126. return -1;
  127. }
  128. /* read iv */
  129. if (!info->iv) {
  130. info->iv = fdt_getprop(fdt, noffset, "iv", NULL);
  131. if (!info->iv) {
  132. printf("Can't get IV in cipher node '%s'\n", node_path);
  133. return -1;
  134. }
  135. }
  136. return 0;
  137. }
  138. int fit_image_decrypt_data(const void *fit,
  139. int image_noffset, int cipher_noffset,
  140. const void *data_ciphered, size_t size_ciphered,
  141. void **data_unciphered, size_t *size_unciphered)
  142. {
  143. struct image_cipher_info info;
  144. int ret;
  145. ret = fit_image_setup_decrypt(&info, fit, image_noffset,
  146. cipher_noffset);
  147. if (ret < 0)
  148. goto out;
  149. ret = info.cipher->decrypt(&info, data_ciphered, size_ciphered,
  150. data_unciphered, size_unciphered);
  151. out:
  152. return ret;
  153. }