image-sig.c 3.8 KB

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