ecdsa-verify.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ECDSA signature verification for u-boot
  4. *
  5. * This implements the firmware-side wrapper for ECDSA verification. It bridges
  6. * the struct crypto_algo API to the ECDSA uclass implementations.
  7. *
  8. * Copyright (c) 2020, Alexandru Gagniuc <mr.nuke.me@gmail.com>
  9. */
  10. #include <crypto/ecdsa-uclass.h>
  11. #include <dm/uclass.h>
  12. #include <u-boot/ecdsa.h>
  13. /*
  14. * Derive size of an ECDSA key from the curve name
  15. *
  16. * While it's possible to extract the key size by using string manipulation,
  17. * use a list of known curves for the time being.
  18. */
  19. static int ecdsa_key_size(const char *curve_name)
  20. {
  21. if (!strcmp(curve_name, "prime256v1"))
  22. return 256;
  23. else
  24. return 0;
  25. }
  26. static int fdt_get_key(struct ecdsa_public_key *key, const void *fdt, int node)
  27. {
  28. int x_len, y_len;
  29. key->curve_name = fdt_getprop(fdt, node, "ecdsa,curve", NULL);
  30. key->size_bits = ecdsa_key_size(key->curve_name);
  31. if (key->size_bits == 0) {
  32. debug("Unknown ECDSA curve '%s'", key->curve_name);
  33. return -EINVAL;
  34. }
  35. key->x = fdt_getprop(fdt, node, "ecdsa,x-point", &x_len);
  36. key->y = fdt_getprop(fdt, node, "ecdsa,y-point", &y_len);
  37. if (!key->x || !key->y)
  38. return -EINVAL;
  39. if (x_len != (key->size_bits / 8) || y_len != (key->size_bits / 8)) {
  40. printf("%s: node=%d, curve@%p x@%p+%i y@%p+%i\n", __func__,
  41. node, key->curve_name, key->x, x_len, key->y, y_len);
  42. return -EINVAL;
  43. }
  44. return 0;
  45. }
  46. static int ecdsa_verify_hash(struct udevice *dev,
  47. const struct image_sign_info *info,
  48. const void *hash, const void *sig, uint sig_len)
  49. {
  50. const struct ecdsa_ops *ops = device_get_ops(dev);
  51. const struct checksum_algo *algo = info->checksum;
  52. struct ecdsa_public_key key;
  53. int sig_node, key_node, ret;
  54. if (!ops || !ops->verify)
  55. return -ENODEV;
  56. if (info->required_keynode > 0) {
  57. ret = fdt_get_key(&key, info->fdt_blob, info->required_keynode);
  58. if (ret < 0)
  59. return ret;
  60. return ops->verify(dev, &key, hash, algo->checksum_len,
  61. sig, sig_len);
  62. }
  63. sig_node = fdt_subnode_offset(info->fdt_blob, 0, FIT_SIG_NODENAME);
  64. if (sig_node < 0)
  65. return -ENOENT;
  66. /* Try all possible keys under the "/signature" node */
  67. fdt_for_each_subnode(key_node, info->fdt_blob, sig_node) {
  68. ret = fdt_get_key(&key, info->fdt_blob, key_node);
  69. if (ret < 0)
  70. continue;
  71. ret = ops->verify(dev, &key, hash, algo->checksum_len,
  72. sig, sig_len);
  73. /* On success, don't worry about remaining keys */
  74. if (!ret)
  75. return 0;
  76. }
  77. return -EPERM;
  78. }
  79. int ecdsa_verify(struct image_sign_info *info,
  80. const struct image_region region[], int region_count,
  81. uint8_t *sig, uint sig_len)
  82. {
  83. const struct checksum_algo *algo = info->checksum;
  84. uint8_t hash[algo->checksum_len];
  85. struct udevice *dev;
  86. int ret;
  87. ret = uclass_first_device_err(UCLASS_ECDSA, &dev);
  88. if (ret) {
  89. debug("ECDSA: Could not find ECDSA implementation: %d\n", ret);
  90. return ret;
  91. }
  92. ret = algo->calculate(algo->name, region, region_count, hash);
  93. if (ret < 0)
  94. return -EINVAL;
  95. return ecdsa_verify_hash(dev, info, hash, sig, sig_len);
  96. }
  97. U_BOOT_CRYPTO_ALGO(ecdsa) = {
  98. .name = "ecdsa256",
  99. .key_len = ECDSA256_BYTES,
  100. .verify = ecdsa_verify,
  101. };
  102. /*
  103. * uclass definition for ECDSA API
  104. *
  105. * We don't implement any wrappers around ecdsa_ops->verify() because it's
  106. * trivial to call ops->verify().
  107. */
  108. UCLASS_DRIVER(ecdsa) = {
  109. .id = UCLASS_ECDSA,
  110. .name = "ecdsa_verifier",
  111. };