image-sig.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <relocate.h>
  12. #include <u-boot/ecdsa.h>
  13. #include <u-boot/rsa.h>
  14. #include <u-boot/hash-checksum.h>
  15. #define IMAGE_MAX_HASHED_NODES 100
  16. struct checksum_algo checksum_algos[] = {
  17. {
  18. .name = "sha1",
  19. .checksum_len = SHA1_SUM_LEN,
  20. .der_len = SHA1_DER_LEN,
  21. .der_prefix = sha1_der_prefix,
  22. .calculate = hash_calculate,
  23. },
  24. {
  25. .name = "sha256",
  26. .checksum_len = SHA256_SUM_LEN,
  27. .der_len = SHA256_DER_LEN,
  28. .der_prefix = sha256_der_prefix,
  29. .calculate = hash_calculate,
  30. },
  31. #ifdef CONFIG_SHA384
  32. {
  33. .name = "sha384",
  34. .checksum_len = SHA384_SUM_LEN,
  35. .der_len = SHA384_DER_LEN,
  36. .der_prefix = sha384_der_prefix,
  37. .calculate = hash_calculate,
  38. },
  39. #endif
  40. #ifdef CONFIG_SHA512
  41. {
  42. .name = "sha512",
  43. .checksum_len = SHA512_SUM_LEN,
  44. .der_len = SHA512_DER_LEN,
  45. .der_prefix = sha512_der_prefix,
  46. .calculate = hash_calculate,
  47. },
  48. #endif
  49. };
  50. struct checksum_algo *image_get_checksum_algo(const char *full_name)
  51. {
  52. int i;
  53. const char *name;
  54. if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
  55. static bool done;
  56. if (!done) {
  57. done = true;
  58. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  59. struct checksum_algo *algo = &checksum_algos[i];
  60. MANUAL_RELOC(algo->name);
  61. MANUAL_RELOC(algo->calculate);
  62. }
  63. }
  64. }
  65. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  66. name = checksum_algos[i].name;
  67. /* Make sure names match and next char is a comma */
  68. if (!strncmp(name, full_name, strlen(name)) &&
  69. full_name[strlen(name)] == ',')
  70. return &checksum_algos[i];
  71. }
  72. return NULL;
  73. }
  74. struct crypto_algo *image_get_crypto_algo(const char *full_name)
  75. {
  76. struct crypto_algo *crypto, *end;
  77. const char *name;
  78. if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
  79. static bool done;
  80. if (!done) {
  81. done = true;
  82. crypto = ll_entry_start(struct crypto_algo, cryptos);
  83. end = ll_entry_end(struct crypto_algo, cryptos);
  84. for (; crypto < end; crypto++) {
  85. MANUAL_RELOC(crypto->name);
  86. MANUAL_RELOC(crypto->verify);
  87. }
  88. }
  89. }
  90. /* Move name to after the comma */
  91. name = strchr(full_name, ',');
  92. if (!name)
  93. return NULL;
  94. name += 1;
  95. crypto = ll_entry_start(struct crypto_algo, cryptos);
  96. end = ll_entry_end(struct crypto_algo, cryptos);
  97. for (; crypto < end; crypto++) {
  98. if (!strcmp(crypto->name, name))
  99. return crypto;
  100. }
  101. /* Not found */
  102. return NULL;
  103. }
  104. struct padding_algo *image_get_padding_algo(const char *name)
  105. {
  106. struct padding_algo *padding, *end;
  107. if (!name)
  108. return NULL;
  109. padding = ll_entry_start(struct padding_algo, paddings);
  110. end = ll_entry_end(struct padding_algo, paddings);
  111. for (; padding < end; padding++) {
  112. if (!strcmp(padding->name, name))
  113. return padding;
  114. }
  115. return NULL;
  116. }