image-sig-host.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. */
  5. #include "mkimage.h"
  6. #include <fdt_support.h>
  7. #include <time.h>
  8. #include <linux/libfdt.h>
  9. #include <image.h>
  10. #include <u-boot/ecdsa.h>
  11. #include <u-boot/rsa.h>
  12. #include <u-boot/hash-checksum.h>
  13. struct checksum_algo checksum_algos[] = {
  14. {
  15. .name = "sha1",
  16. .checksum_len = SHA1_SUM_LEN,
  17. .der_len = SHA1_DER_LEN,
  18. .der_prefix = sha1_der_prefix,
  19. .calculate_sign = EVP_sha1,
  20. .calculate = hash_calculate,
  21. },
  22. {
  23. .name = "sha256",
  24. .checksum_len = SHA256_SUM_LEN,
  25. .der_len = SHA256_DER_LEN,
  26. .der_prefix = sha256_der_prefix,
  27. .calculate_sign = EVP_sha256,
  28. .calculate = hash_calculate,
  29. },
  30. {
  31. .name = "sha384",
  32. .checksum_len = SHA384_SUM_LEN,
  33. .der_len = SHA384_DER_LEN,
  34. .der_prefix = sha384_der_prefix,
  35. .calculate_sign = EVP_sha384,
  36. .calculate = hash_calculate,
  37. },
  38. {
  39. .name = "sha512",
  40. .checksum_len = SHA512_SUM_LEN,
  41. .der_len = SHA512_DER_LEN,
  42. .der_prefix = sha512_der_prefix,
  43. .calculate_sign = EVP_sha512,
  44. .calculate = hash_calculate,
  45. },
  46. };
  47. struct crypto_algo crypto_algos[] = {
  48. {
  49. .name = "rsa2048",
  50. .key_len = RSA2048_BYTES,
  51. .sign = rsa_sign,
  52. .add_verify_data = rsa_add_verify_data,
  53. .verify = rsa_verify,
  54. },
  55. {
  56. .name = "rsa4096",
  57. .key_len = RSA4096_BYTES,
  58. .sign = rsa_sign,
  59. .add_verify_data = rsa_add_verify_data,
  60. .verify = rsa_verify,
  61. },
  62. {
  63. .name = "ecdsa256",
  64. .key_len = ECDSA256_BYTES,
  65. .sign = ecdsa_sign,
  66. .add_verify_data = ecdsa_add_verify_data,
  67. .verify = ecdsa_verify,
  68. },
  69. };
  70. struct padding_algo padding_algos[] = {
  71. {
  72. .name = "pkcs-1.5",
  73. .verify = padding_pkcs_15_verify,
  74. },
  75. {
  76. .name = "pss",
  77. .verify = padding_pss_verify,
  78. }
  79. };
  80. struct checksum_algo *image_get_checksum_algo(const char *full_name)
  81. {
  82. int i;
  83. const char *name;
  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. /* Move name to after the comma */
  98. name = strchr(full_name, ',');
  99. if (!name)
  100. return NULL;
  101. name += 1;
  102. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  103. if (!strcmp(crypto_algos[i].name, name))
  104. return &crypto_algos[i];
  105. }
  106. return NULL;
  107. }
  108. struct padding_algo *image_get_padding_algo(const char *name)
  109. {
  110. int i;
  111. if (!name)
  112. return NULL;
  113. for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
  114. if (!strcmp(padding_algos[i].name, name))
  115. return &padding_algos[i];
  116. }
  117. return NULL;
  118. }