image-sig.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. };
  42. struct crypto_algo crypto_algos[] = {
  43. {
  44. .name = "rsa2048",
  45. .key_len = RSA2048_BYTES,
  46. .sign = rsa_sign,
  47. .add_verify_data = rsa_add_verify_data,
  48. .verify = rsa_verify,
  49. },
  50. {
  51. .name = "rsa4096",
  52. .key_len = RSA4096_BYTES,
  53. .sign = rsa_sign,
  54. .add_verify_data = rsa_add_verify_data,
  55. .verify = rsa_verify,
  56. }
  57. };
  58. struct padding_algo padding_algos[] = {
  59. {
  60. .name = "pkcs-1.5",
  61. .verify = padding_pkcs_15_verify,
  62. },
  63. #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
  64. {
  65. .name = "pss",
  66. .verify = padding_pss_verify,
  67. }
  68. #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
  69. };
  70. struct checksum_algo *image_get_checksum_algo(const char *full_name)
  71. {
  72. int i;
  73. const char *name;
  74. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  75. static bool done;
  76. if (!done) {
  77. done = true;
  78. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  79. checksum_algos[i].name += gd->reloc_off;
  80. #if IMAGE_ENABLE_SIGN
  81. checksum_algos[i].calculate_sign += gd->reloc_off;
  82. #endif
  83. checksum_algos[i].calculate += gd->reloc_off;
  84. }
  85. }
  86. #endif
  87. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  88. name = checksum_algos[i].name;
  89. /* Make sure names match and next char is a comma */
  90. if (!strncmp(name, full_name, strlen(name)) &&
  91. full_name[strlen(name)] == ',')
  92. return &checksum_algos[i];
  93. }
  94. return NULL;
  95. }
  96. struct crypto_algo *image_get_crypto_algo(const char *full_name)
  97. {
  98. int i;
  99. const char *name;
  100. #if !defined(USE_HOSTCC) && defined(CONFIG_NEEDS_MANUAL_RELOC)
  101. static bool done;
  102. if (!done) {
  103. done = true;
  104. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  105. crypto_algos[i].name += gd->reloc_off;
  106. crypto_algos[i].sign += gd->reloc_off;
  107. crypto_algos[i].add_verify_data += gd->reloc_off;
  108. crypto_algos[i].verify += gd->reloc_off;
  109. }
  110. }
  111. #endif
  112. /* Move name to after the comma */
  113. name = strchr(full_name, ',');
  114. if (!name)
  115. return NULL;
  116. name += 1;
  117. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  118. if (!strcmp(crypto_algos[i].name, name))
  119. return &crypto_algos[i];
  120. }
  121. return NULL;
  122. }
  123. struct padding_algo *image_get_padding_algo(const char *name)
  124. {
  125. int i;
  126. if (!name)
  127. return NULL;
  128. for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
  129. if (!strcmp(padding_algos[i].name, name))
  130. return &padding_algos[i];
  131. }
  132. return NULL;
  133. }