image-cipher.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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->ivname = fdt_getprop(fit, cipher_noffset, "iv-name-hint", NULL);
  88. if (!info->ivname) {
  89. printf("Can't get IV name\n");
  90. return -1;
  91. }
  92. info->fit = fit;
  93. info->node_noffset = image_noffset;
  94. info->name = algo_name;
  95. info->cipher = image_get_cipher_algo(algo_name);
  96. if (!info->cipher) {
  97. printf("Can't get cipher\n");
  98. return -1;
  99. }
  100. ret = fit_image_get_data_size_unciphered(fit, image_noffset,
  101. &info->size_unciphered);
  102. if (ret) {
  103. printf("Can't get size of unciphered data\n");
  104. return -1;
  105. }
  106. /*
  107. * Search the cipher node in the u-boot fdt
  108. * the path should be: /cipher/key-<algo>-<key>-<iv>
  109. */
  110. snprintf(node_path, sizeof(node_path), "/%s/key-%s-%s-%s",
  111. FIT_CIPHER_NODENAME, algo_name, info->keyname, info->ivname);
  112. noffset = fdt_path_offset(fdt, node_path);
  113. if (noffset < 0) {
  114. printf("Can't found cipher node offset\n");
  115. return -1;
  116. }
  117. /* read key */
  118. info->key = fdt_getprop(fdt, noffset, "key", NULL);
  119. if (!info->key) {
  120. printf("Can't get key in cipher node '%s'\n", node_path);
  121. return -1;
  122. }
  123. /* read iv */
  124. info->iv = fdt_getprop(fdt, noffset, "iv", NULL);
  125. if (!info->iv) {
  126. printf("Can't get IV in cipher node '%s'\n", node_path);
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. int fit_image_decrypt_data(const void *fit,
  132. int image_noffset, int cipher_noffset,
  133. const void *data_ciphered, size_t size_ciphered,
  134. void **data_unciphered, size_t *size_unciphered)
  135. {
  136. struct image_cipher_info info;
  137. int ret;
  138. ret = fit_image_setup_decrypt(&info, fit, image_noffset,
  139. cipher_noffset);
  140. if (ret < 0)
  141. goto out;
  142. ret = info.cipher->decrypt(&info, data_ciphered, size_ciphered,
  143. data_unciphered, size_unciphered);
  144. out:
  145. return ret;
  146. }