image-sig.c 3.1 KB

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