image-sig.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. */
  5. #ifdef USE_HOSTCC
  6. #include "mkimage.h"
  7. #include <fdt_support.h>
  8. #include <time.h>
  9. #include <linux/libfdt.h>
  10. #else
  11. #include <common.h>
  12. #include <log.h>
  13. #include <malloc.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. #endif /* !USE_HOSTCC*/
  16. #include <image.h>
  17. #include <u-boot/rsa.h>
  18. #include <u-boot/rsa-checksum.h>
  19. #define IMAGE_MAX_HASHED_NODES 100
  20. struct checksum_algo checksum_algos[] = {
  21. {
  22. .name = "sha1",
  23. .checksum_len = SHA1_SUM_LEN,
  24. .der_len = SHA1_DER_LEN,
  25. .der_prefix = sha1_der_prefix,
  26. #if IMAGE_ENABLE_SIGN
  27. .calculate_sign = EVP_sha1,
  28. #endif
  29. .calculate = hash_calculate,
  30. },
  31. {
  32. .name = "sha256",
  33. .checksum_len = SHA256_SUM_LEN,
  34. .der_len = SHA256_DER_LEN,
  35. .der_prefix = sha256_der_prefix,
  36. #if IMAGE_ENABLE_SIGN
  37. .calculate_sign = EVP_sha256,
  38. #endif
  39. .calculate = hash_calculate,
  40. },
  41. #ifdef CONFIG_SHA384
  42. {
  43. .name = "sha384",
  44. .checksum_len = SHA384_SUM_LEN,
  45. .der_len = SHA384_DER_LEN,
  46. .der_prefix = sha384_der_prefix,
  47. #if IMAGE_ENABLE_SIGN
  48. .calculate_sign = EVP_sha384,
  49. #endif
  50. .calculate = hash_calculate,
  51. },
  52. #endif
  53. #ifdef CONFIG_SHA512
  54. {
  55. .name = "sha512",
  56. .checksum_len = SHA512_SUM_LEN,
  57. .der_len = SHA512_DER_LEN,
  58. .der_prefix = sha512_der_prefix,
  59. #if IMAGE_ENABLE_SIGN
  60. .calculate_sign = EVP_sha512,
  61. #endif
  62. .calculate = hash_calculate,
  63. },
  64. #endif
  65. };
  66. struct crypto_algo crypto_algos[] = {
  67. {
  68. .name = "rsa2048",
  69. .key_len = RSA2048_BYTES,
  70. .sign = rsa_sign,
  71. .add_verify_data = rsa_add_verify_data,
  72. .verify = rsa_verify,
  73. },
  74. {
  75. .name = "rsa4096",
  76. .key_len = RSA4096_BYTES,
  77. .sign = rsa_sign,
  78. .add_verify_data = rsa_add_verify_data,
  79. .verify = rsa_verify,
  80. }
  81. };
  82. struct padding_algo padding_algos[] = {
  83. {
  84. .name = "pkcs-1.5",
  85. .verify = padding_pkcs_15_verify,
  86. },
  87. #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
  88. {
  89. .name = "pss",
  90. .verify = padding_pss_verify,
  91. }
  92. #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
  93. };
  94. struct checksum_algo *image_get_checksum_algo(const char *full_name)
  95. {
  96. int i;
  97. const char *name;
  98. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  99. static bool done;
  100. if (!done) {
  101. done = true;
  102. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  103. checksum_algos[i].name += gd->reloc_off;
  104. #if IMAGE_ENABLE_SIGN
  105. checksum_algos[i].calculate_sign += gd->reloc_off;
  106. #endif
  107. checksum_algos[i].calculate += gd->reloc_off;
  108. }
  109. }
  110. #endif
  111. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  112. name = checksum_algos[i].name;
  113. /* Make sure names match and next char is a comma */
  114. if (!strncmp(name, full_name, strlen(name)) &&
  115. full_name[strlen(name)] == ',')
  116. return &checksum_algos[i];
  117. }
  118. return NULL;
  119. }
  120. struct crypto_algo *image_get_crypto_algo(const char *full_name)
  121. {
  122. int i;
  123. const char *name;
  124. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  125. static bool done;
  126. if (!done) {
  127. done = true;
  128. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  129. crypto_algos[i].name += gd->reloc_off;
  130. crypto_algos[i].sign += gd->reloc_off;
  131. crypto_algos[i].add_verify_data += gd->reloc_off;
  132. crypto_algos[i].verify += gd->reloc_off;
  133. }
  134. }
  135. #endif
  136. /* Move name to after the comma */
  137. name = strchr(full_name, ',');
  138. if (!name)
  139. return NULL;
  140. name += 1;
  141. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  142. if (!strcmp(crypto_algos[i].name, name))
  143. return &crypto_algos[i];
  144. }
  145. return NULL;
  146. }
  147. struct padding_algo *image_get_padding_algo(const char *name)
  148. {
  149. int i;
  150. if (!name)
  151. return NULL;
  152. for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
  153. if (!strcmp(padding_algos[i].name, name))
  154. return &padding_algos[i];
  155. }
  156. return NULL;
  157. }