rsa.h 4.7 KB

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