image-sig.c 3.6 KB

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