image-sig.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 checksum_algo *image_get_checksum_algo(const char *full_name)
  50. {
  51. int i;
  52. const char *name;
  53. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  54. static bool done;
  55. if (!done) {
  56. done = true;
  57. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  58. checksum_algos[i].name += gd->reloc_off;
  59. checksum_algos[i].calculate += gd->reloc_off;
  60. }
  61. }
  62. #endif
  63. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  64. name = checksum_algos[i].name;
  65. /* Make sure names match and next char is a comma */
  66. if (!strncmp(name, full_name, strlen(name)) &&
  67. full_name[strlen(name)] == ',')
  68. return &checksum_algos[i];
  69. }
  70. return NULL;
  71. }
  72. struct crypto_algo *image_get_crypto_algo(const char *full_name)
  73. {
  74. struct crypto_algo *crypto, *end;
  75. const char *name;
  76. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  77. static bool done;
  78. if (!done) {
  79. crypto = ll_entry_start(struct crypto_algo, cryptos);
  80. end = ll_entry_end(struct crypto_algo, cryptos);
  81. for (; crypto < end; crypto++) {
  82. crypto->name += gd->reloc_off;
  83. crypto->verify += gd->reloc_off;
  84. }
  85. }
  86. #endif
  87. /* Move name to after the comma */
  88. name = strchr(full_name, ',');
  89. if (!name)
  90. return NULL;
  91. name += 1;
  92. crypto = ll_entry_start(struct crypto_algo, cryptos);
  93. end = ll_entry_end(struct crypto_algo, cryptos);
  94. for (; crypto < end; crypto++) {
  95. if (!strcmp(crypto->name, name))
  96. return crypto;
  97. }
  98. /* Not found */
  99. return NULL;
  100. }
  101. struct padding_algo *image_get_padding_algo(const char *name)
  102. {
  103. struct padding_algo *padding, *end;
  104. if (!name)
  105. return NULL;
  106. padding = ll_entry_start(struct padding_algo, paddings);
  107. end = ll_entry_end(struct padding_algo, paddings);
  108. for (; padding < end; padding++) {
  109. if (!strcmp(padding->name, name))
  110. return padding;
  111. }
  112. return NULL;
  113. }