rsa.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2013, Google Inc.
  3. *
  4. * (C) Copyright 2008 Semihalf
  5. *
  6. * (C) Copyright 2000-2006
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #ifndef _RSA_H
  12. #define _RSA_H
  13. #include <errno.h>
  14. #include <image.h>
  15. /**
  16. * struct rsa_public_key - holder for a public key
  17. *
  18. * An RSA public key consists of a modulus (typically called N), the inverse
  19. * and R^2, where R is 2^(# key bits).
  20. */
  21. struct rsa_public_key {
  22. uint len; /* len of modulus[] in number of uint32_t */
  23. uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */
  24. uint32_t *modulus; /* modulus as little endian array */
  25. uint32_t *rr; /* R^2 as little endian array */
  26. uint64_t exponent; /* public exponent */
  27. };
  28. struct image_sign_info;
  29. #if IMAGE_ENABLE_SIGN
  30. /**
  31. * sign() - calculate and return signature for given input data
  32. *
  33. * @info: Specifies key and FIT information
  34. * @data: Pointer to the input data
  35. * @data_len: Data length
  36. * @sigp: Set to an allocated buffer holding the signature
  37. * @sig_len: Set to length of the calculated hash
  38. *
  39. * This computes input data signature according to selected algorithm.
  40. * Resulting signature value is placed in an allocated buffer, the
  41. * pointer is returned as *sigp. The length of the calculated
  42. * signature is returned via the sig_len pointer argument. The caller
  43. * should free *sigp.
  44. *
  45. * @return: 0, on success, -ve on error
  46. */
  47. int rsa_sign(struct image_sign_info *info,
  48. const struct image_region region[],
  49. int region_count, uint8_t **sigp, uint *sig_len);
  50. /**
  51. * add_verify_data() - Add verification information to FDT
  52. *
  53. * Add public key information to the FDT node, suitable for
  54. * verification at run-time. The information added depends on the
  55. * algorithm being used.
  56. *
  57. * @info: Specifies key and FIT information
  58. * @keydest: Destination FDT blob for public key data
  59. * @return: 0, on success, -ENOSPC if the keydest FDT blob ran out of space,
  60. other -ve value on error
  61. */
  62. int rsa_add_verify_data(struct image_sign_info *info, void *keydest);
  63. #else
  64. static inline int rsa_sign(struct image_sign_info *info,
  65. const struct image_region region[], int region_count,
  66. uint8_t **sigp, uint *sig_len)
  67. {
  68. return -ENXIO;
  69. }
  70. static inline int rsa_add_verify_data(struct image_sign_info *info,
  71. void *keydest)
  72. {
  73. return -ENXIO;
  74. }
  75. #endif
  76. #if IMAGE_ENABLE_VERIFY
  77. /**
  78. * rsa_verify() - Verify a signature against some data
  79. *
  80. * Verify a RSA PKCS1.5 signature against an expected hash.
  81. *
  82. * @info: Specifies key and FIT information
  83. * @data: Pointer to the input data
  84. * @data_len: Data length
  85. * @sig: Signature
  86. * @sig_len: Number of bytes in signature
  87. * @return 0 if verified, -ve on error
  88. */
  89. int rsa_verify(struct image_sign_info *info,
  90. const struct image_region region[], int region_count,
  91. uint8_t *sig, uint sig_len);
  92. #else
  93. static inline int rsa_verify(struct image_sign_info *info,
  94. const struct image_region region[], int region_count,
  95. uint8_t *sig, uint sig_len)
  96. {
  97. return -ENXIO;
  98. }
  99. #endif
  100. #define RSA2048_BYTES (2048 / 8)
  101. #define RSA4096_BYTES (4096 / 8)
  102. /* This is the minimum/maximum key size we support, in bits */
  103. #define RSA_MIN_KEY_BITS 2048
  104. #define RSA_MAX_KEY_BITS 4096
  105. /* This is the maximum signature length that we support, in bits */
  106. #define RSA_MAX_SIG_BITS 4096
  107. #endif