image-sig.c 3.0 KB

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