image-sig-host.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = "rsa3072",
  57. .key_len = RSA3072_BYTES,
  58. .sign = rsa_sign,
  59. .add_verify_data = rsa_add_verify_data,
  60. .verify = rsa_verify,
  61. },
  62. {
  63. .name = "rsa4096",
  64. .key_len = RSA4096_BYTES,
  65. .sign = rsa_sign,
  66. .add_verify_data = rsa_add_verify_data,
  67. .verify = rsa_verify,
  68. },
  69. {
  70. .name = "ecdsa256",
  71. .key_len = ECDSA256_BYTES,
  72. .sign = ecdsa_sign,
  73. .add_verify_data = ecdsa_add_verify_data,
  74. .verify = ecdsa_verify,
  75. },
  76. };
  77. struct padding_algo padding_algos[] = {
  78. {
  79. .name = "pkcs-1.5",
  80. .verify = padding_pkcs_15_verify,
  81. },
  82. {
  83. .name = "pss",
  84. .verify = padding_pss_verify,
  85. }
  86. };
  87. struct checksum_algo *image_get_checksum_algo(const char *full_name)
  88. {
  89. int i;
  90. const char *name;
  91. for (i = 0; i < ARRAY_SIZE(checksum_algos); i++) {
  92. name = checksum_algos[i].name;
  93. /* Make sure names match and next char is a comma */
  94. if (!strncmp(name, full_name, strlen(name)) &&
  95. full_name[strlen(name)] == ',')
  96. return &checksum_algos[i];
  97. }
  98. return NULL;
  99. }
  100. struct crypto_algo *image_get_crypto_algo(const char *full_name)
  101. {
  102. int i;
  103. const char *name;
  104. /* Move name to after the comma */
  105. name = strchr(full_name, ',');
  106. if (!name)
  107. return NULL;
  108. name += 1;
  109. for (i = 0; i < ARRAY_SIZE(crypto_algos); i++) {
  110. if (!strcmp(crypto_algos[i].name, name))
  111. return &crypto_algos[i];
  112. }
  113. return NULL;
  114. }
  115. struct padding_algo *image_get_padding_algo(const char *name)
  116. {
  117. int i;
  118. if (!name)
  119. return NULL;
  120. for (i = 0; i < ARRAY_SIZE(padding_algos); i++) {
  121. if (!strcmp(padding_algos[i].name, name))
  122. return &padding_algos[i];
  123. }
  124. return NULL;
  125. }