rsa.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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() - Verify a signature against some data
  78. *
  79. * Verify a RSA PKCS1.5 signature against an expected hash.
  80. *
  81. * @info: Specifies key and FIT information
  82. * @data: Pointer to the input data
  83. * @data_len: Data length
  84. * @sig: Signature
  85. * @sig_len: Number of bytes in signature
  86. * @return 0 if verified, -ve on error
  87. */
  88. int rsa_verify(struct image_sign_info *info,
  89. const struct image_region region[], int region_count,
  90. uint8_t *sig, uint sig_len);
  91. int padding_pkcs_15_verify(struct image_sign_info *info,
  92. uint8_t *msg, int msg_len,
  93. const uint8_t *hash, int hash_len);
  94. #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
  95. int padding_pss_verify(struct image_sign_info *info,
  96. uint8_t *msg, int msg_len,
  97. const uint8_t *hash, int hash_len);
  98. #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
  99. #else
  100. static inline int rsa_verify(struct image_sign_info *info,
  101. const struct image_region region[], int region_count,
  102. uint8_t *sig, uint sig_len)
  103. {
  104. return -ENXIO;
  105. }
  106. static inline 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. {
  110. return -ENXIO;
  111. }
  112. #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
  113. static inline int padding_pss_verify(struct image_sign_info *info,
  114. uint8_t *msg, int msg_len,
  115. const uint8_t *hash, int hash_len)
  116. {
  117. return -ENXIO;
  118. }
  119. #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
  120. #endif
  121. #define RSA_DEFAULT_PADDING_NAME "pkcs-1.5"
  122. #define RSA2048_BYTES (2048 / 8)
  123. #define RSA4096_BYTES (4096 / 8)
  124. /* This is the minimum/maximum key size we support, in bits */
  125. #define RSA_MIN_KEY_BITS 2048
  126. #define RSA_MAX_KEY_BITS 4096
  127. /* This is the maximum signature length that we support, in bits */
  128. #define RSA_MAX_SIG_BITS 4096
  129. #endif