image-sig.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. */
  5. #include <common.h>
  6. #include <log.h>
  7. #include <malloc.h>
  8. #include <asm/global_data.h>
  9. DECLARE_GLOBAL_DATA_PTR;
  10. #include <image.h>
  11. #include <u-boot/ecdsa.h>
  12. #include <u-boot/rsa.h>
  13. #include <u-boot/hash-checksum.h>
  14. #define IMAGE_MAX_HASHED_NODES 100
  15. struct checksum_algo checksum_algos[] = {
  16. {
  17. .name = "sha1",
  18. .checksum_len = SHA1_SUM_LEN,
  19. .der_len = SHA1_DER_LEN,
  20. .der_prefix = sha1_der_prefix,
  21. .calculate = hash_calculate,
  22. },
  23. {
  24. .name = "sha256",
  25. .checksum_len = SHA256_SUM_LEN,
  26. .der_len = SHA256_DER_LEN,
  27. .der_prefix = sha256_der_prefix,
  28. .calculate = hash_calculate,
  29. },
  30. #ifdef CONFIG_SHA384
  31. {
  32. .name = "sha384",
  33. .checksum_len = SHA384_SUM_LEN,
  34. .der_len = SHA384_DER_LEN,
  35. .der_prefix = sha384_der_prefix,
  36. .calculate = hash_calculate,
  37. },
  38. #endif
  39. #ifdef CONFIG_SHA512
  40. {
  41. .name = "sha512",
  42. .checksum_len = SHA512_SUM_LEN,
  43. .der_len = SHA512_DER_LEN,
  44. .der_prefix = sha512_der_prefix,
  45. .calculate = hash_calculate,
  46. },
  47. #endif
  48. };
  49. struct padding_algo padding_algos[] = {
  50. {
  51. .name = "pkcs-1.5",
  52. .verify = padding_pkcs_15_verify,
  53. },
  54. #ifdef CONFIG_FIT_RSASSA_PSS
  55. {
  56. .name = "pss",
  57. .verify = padding_pss_verify,
  58. }
  59. #endif /* CONFIG_FIT_RSASSA_PSS */
  60. };
  61. struct checksum_algo *image_get_checksum_algo(const char *full_name)
  62. {
  63. int i;
  64. const char *name;
  65. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  66. static bool done;
  67. if (!done) {
  68. done = true;
  69. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  70. checksum_algos[i].name += gd->reloc_off;
  71. checksum_algos[i].calculate += gd->reloc_off;
  72. }
  73. }
  74. #endif
  75. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  76. name = checksum_algos[i].name;
  77. /* Make sure names match and next char is a comma */
  78. if (!strncmp(name, full_name, strlen(name)) &&
  79. full_name[strlen(name)] == ',')
  80. return &checksum_algos[i];
  81. }
  82. return NULL;
  83. }
  84. struct crypto_algo *image_get_crypto_algo(const char *full_name)
  85. {
  86. struct crypto_algo *crypto, *end;
  87. const char *name;
  88. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  89. static bool done;
  90. if (!done) {
  91. crypto = ll_entry_start(struct crypto_algo, cryptos);
  92. end = ll_entry_end(struct crypto_algo, cryptos);
  93. for (; crypto < end; crypto++) {
  94. crypto->name += gd->reloc_off;
  95. crypto->verify += gd->reloc_off;
  96. }
  97. }
  98. #endif
  99. /* Move name to after the comma */
  100. name = strchr(full_name, ',');
  101. if (!name)
  102. return NULL;
  103. name += 1;
  104. crypto = ll_entry_start(struct crypto_algo, cryptos);
  105. end = ll_entry_end(struct crypto_algo, cryptos);
  106. for (; crypto < end; crypto++) {
  107. if (!strcmp(crypto->name, name))
  108. return crypto;
  109. }
  110. /* Not found */
  111. return NULL;
  112. }
  113. struct padding_algo *image_get_padding_algo(const char *name)
  114. {
  115. int i;
  116. if (!name)
  117. return NULL;
  118. for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
  119. if (!strcmp(padding_algos[i].name, name))
  120. return &padding_algos[i];
  121. }
  122. return NULL;
  123. }