psa_crypto_rsa.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * PSA RSA layer on top of Mbed TLS crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef PSA_CRYPTO_RSA_H
  21. #define PSA_CRYPTO_RSA_H
  22. #include <psa/crypto.h>
  23. #include <mbedtls/rsa.h>
  24. /** Load the contents of a key buffer into an internal RSA representation
  25. *
  26. * \param[in] type The type of key contained in \p data.
  27. * \param[in] data The buffer from which to load the representation.
  28. * \param[in] data_length The size in bytes of \p data.
  29. * \param[out] p_rsa Returns a pointer to an RSA context on success.
  30. * The caller is responsible for freeing both the
  31. * contents of the context and the context itself
  32. * when done.
  33. */
  34. psa_status_t mbedtls_psa_rsa_load_representation( psa_key_type_t type,
  35. const uint8_t *data,
  36. size_t data_length,
  37. mbedtls_rsa_context **p_rsa );
  38. /** Import an RSA key in binary format.
  39. *
  40. * \note The signature of this function is that of a PSA driver
  41. * import_key entry point. This function behaves as an import_key
  42. * entry point as defined in the PSA driver interface specification for
  43. * transparent drivers.
  44. *
  45. * \param[in] attributes The attributes for the key to import.
  46. * \param[in] data The buffer containing the key data in import
  47. * format.
  48. * \param[in] data_length Size of the \p data buffer in bytes.
  49. * \param[out] key_buffer The buffer containing the key data in output
  50. * format.
  51. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
  52. * size is greater or equal to \p data_length.
  53. * \param[out] key_buffer_length The length of the data written in \p
  54. * key_buffer in bytes.
  55. * \param[out] bits The key size in number of bits.
  56. *
  57. * \retval #PSA_SUCCESS The RSA key was imported successfully.
  58. * \retval #PSA_ERROR_INVALID_ARGUMENT
  59. * The key data is not correctly formatted.
  60. * \retval #PSA_ERROR_NOT_SUPPORTED
  61. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  62. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  63. */
  64. psa_status_t mbedtls_psa_rsa_import_key(
  65. const psa_key_attributes_t *attributes,
  66. const uint8_t *data, size_t data_length,
  67. uint8_t *key_buffer, size_t key_buffer_size,
  68. size_t *key_buffer_length, size_t *bits );
  69. /** Export an RSA key to export representation
  70. *
  71. * \param[in] type The type of key (public/private) to export
  72. * \param[in] rsa The internal RSA representation from which to export
  73. * \param[out] data The buffer to export to
  74. * \param[in] data_size The length of the buffer to export to
  75. * \param[out] data_length The amount of bytes written to \p data
  76. */
  77. psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
  78. mbedtls_rsa_context *rsa,
  79. uint8_t *data,
  80. size_t data_size,
  81. size_t *data_length );
  82. /** Export a public RSA key or the public part of an RSA key pair in binary
  83. * format.
  84. *
  85. * \note The signature of this function is that of a PSA driver
  86. * export_public_key entry point. This function behaves as an
  87. * export_public_key entry point as defined in the PSA driver interface
  88. * specification.
  89. *
  90. * \param[in] attributes The attributes for the key to export.
  91. * \param[in] key_buffer Material or context of the key to export.
  92. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  93. * \param[out] data Buffer where the key data is to be written.
  94. * \param[in] data_size Size of the \p data buffer in bytes.
  95. * \param[out] data_length On success, the number of bytes written in
  96. * \p data.
  97. *
  98. * \retval #PSA_SUCCESS The RSA public key was exported successfully.
  99. * \retval #PSA_ERROR_NOT_SUPPORTED
  100. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  101. * \retval #PSA_ERROR_HARDWARE_FAILURE
  102. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  103. * \retval #PSA_ERROR_STORAGE_FAILURE
  104. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  105. */
  106. psa_status_t mbedtls_psa_rsa_export_public_key(
  107. const psa_key_attributes_t *attributes,
  108. const uint8_t *key_buffer, size_t key_buffer_size,
  109. uint8_t *data, size_t data_size, size_t *data_length );
  110. /**
  111. * \brief Generate an RSA key.
  112. *
  113. * \note The signature of the function is that of a PSA driver generate_key
  114. * entry point.
  115. *
  116. * \param[in] attributes The attributes for the RSA key to generate.
  117. * \param[out] key_buffer Buffer where the key data is to be written.
  118. * \param[in] key_buffer_size Size of \p key_buffer in bytes.
  119. * \param[out] key_buffer_length On success, the number of bytes written in
  120. * \p key_buffer.
  121. *
  122. * \retval #PSA_SUCCESS
  123. * The key was successfully generated.
  124. * \retval #PSA_ERROR_NOT_SUPPORTED
  125. * Key length or type not supported.
  126. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  127. * The size of \p key_buffer is too small.
  128. */
  129. psa_status_t mbedtls_psa_rsa_generate_key(
  130. const psa_key_attributes_t *attributes,
  131. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length );
  132. /** Sign an already-calculated hash with an RSA private key.
  133. *
  134. * \note The signature of this function is that of a PSA driver
  135. * sign_hash entry point. This function behaves as a sign_hash
  136. * entry point as defined in the PSA driver interface specification for
  137. * transparent drivers.
  138. *
  139. * \param[in] attributes The attributes of the RSA key to use for the
  140. * operation.
  141. * \param[in] key_buffer The buffer containing the RSA key context.
  142. * format.
  143. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  144. * \param[in] alg A signature algorithm that is compatible with
  145. * an RSA key.
  146. * \param[in] hash The hash or message to sign.
  147. * \param[in] hash_length Size of the \p hash buffer in bytes.
  148. * \param[out] signature Buffer where the signature is to be written.
  149. * \param[in] signature_size Size of the \p signature buffer in bytes.
  150. * \param[out] signature_length On success, the number of bytes
  151. * that make up the returned signature value.
  152. *
  153. * \retval #PSA_SUCCESS
  154. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  155. * The size of the \p signature buffer is too small. You can
  156. * determine a sufficient buffer size by calling
  157. * #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_RSA_KEY_PAIR, \c key_bits,
  158. * \p alg) where \c key_bits is the bit-size of the RSA key.
  159. * \retval #PSA_ERROR_NOT_SUPPORTED
  160. * \retval #PSA_ERROR_INVALID_ARGUMENT
  161. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  162. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  163. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  164. */
  165. psa_status_t mbedtls_psa_rsa_sign_hash(
  166. const psa_key_attributes_t *attributes,
  167. const uint8_t *key_buffer, size_t key_buffer_size,
  168. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  169. uint8_t *signature, size_t signature_size, size_t *signature_length );
  170. /**
  171. * \brief Verify the signature a hash or short message using a public RSA key.
  172. *
  173. * \note The signature of this function is that of a PSA driver
  174. * verify_hash entry point. This function behaves as a verify_hash
  175. * entry point as defined in the PSA driver interface specification for
  176. * transparent drivers.
  177. *
  178. * \param[in] attributes The attributes of the RSA key to use for the
  179. * operation.
  180. * \param[in] key_buffer The buffer containing the RSA key context.
  181. * format.
  182. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  183. * \param[in] alg A signature algorithm that is compatible with
  184. * an RSA key.
  185. * \param[in] hash The hash or message whose signature is to be
  186. * verified.
  187. * \param[in] hash_length Size of the \p hash buffer in bytes.
  188. * \param[in] signature Buffer containing the signature to verify.
  189. * \param[in] signature_length Size of the \p signature buffer in bytes.
  190. *
  191. * \retval #PSA_SUCCESS
  192. * The signature is valid.
  193. * \retval #PSA_ERROR_INVALID_SIGNATURE
  194. * The calculation was performed successfully, but the passed
  195. * signature is not a valid signature.
  196. * \retval #PSA_ERROR_NOT_SUPPORTED
  197. * \retval #PSA_ERROR_INVALID_ARGUMENT
  198. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  199. */
  200. psa_status_t mbedtls_psa_rsa_verify_hash(
  201. const psa_key_attributes_t *attributes,
  202. const uint8_t *key_buffer, size_t key_buffer_size,
  203. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  204. const uint8_t *signature, size_t signature_length );
  205. #endif /* PSA_CRYPTO_RSA_H */