psa_crypto_rsa.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 OR GPL-2.0-or-later
  7. */
  8. #ifndef PSA_CRYPTO_RSA_H
  9. #define PSA_CRYPTO_RSA_H
  10. #include <psa/crypto.h>
  11. #include <mbedtls/rsa.h>
  12. /** Load the contents of a key buffer into an internal RSA representation
  13. *
  14. * \param[in] type The type of key contained in \p data.
  15. * \param[in] data The buffer from which to load the representation.
  16. * \param[in] data_length The size in bytes of \p data.
  17. * \param[out] p_rsa Returns a pointer to an RSA context on success.
  18. * The caller is responsible for freeing both the
  19. * contents of the context and the context itself
  20. * when done.
  21. */
  22. psa_status_t mbedtls_psa_rsa_load_representation(psa_key_type_t type,
  23. const uint8_t *data,
  24. size_t data_length,
  25. mbedtls_rsa_context **p_rsa);
  26. /** Import an RSA key in binary format.
  27. *
  28. * \note The signature of this function is that of a PSA driver
  29. * import_key entry point. This function behaves as an import_key
  30. * entry point as defined in the PSA driver interface specification for
  31. * transparent drivers.
  32. *
  33. * \param[in] attributes The attributes for the key to import.
  34. * \param[in] data The buffer containing the key data in import
  35. * format.
  36. * \param[in] data_length Size of the \p data buffer in bytes.
  37. * \param[out] key_buffer The buffer containing the key data in output
  38. * format.
  39. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
  40. * size is greater or equal to \p data_length.
  41. * \param[out] key_buffer_length The length of the data written in \p
  42. * key_buffer in bytes.
  43. * \param[out] bits The key size in number of bits.
  44. *
  45. * \retval #PSA_SUCCESS The RSA key was imported successfully.
  46. * \retval #PSA_ERROR_INVALID_ARGUMENT
  47. * The key data is not correctly formatted.
  48. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  49. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  50. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  51. */
  52. psa_status_t mbedtls_psa_rsa_import_key(
  53. const psa_key_attributes_t *attributes,
  54. const uint8_t *data, size_t data_length,
  55. uint8_t *key_buffer, size_t key_buffer_size,
  56. size_t *key_buffer_length, size_t *bits);
  57. /** Export an RSA key to export representation
  58. *
  59. * \param[in] type The type of key (public/private) to export
  60. * \param[in] rsa The internal RSA representation from which to export
  61. * \param[out] data The buffer to export to
  62. * \param[in] data_size The length of the buffer to export to
  63. * \param[out] data_length The amount of bytes written to \p data
  64. */
  65. psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
  66. mbedtls_rsa_context *rsa,
  67. uint8_t *data,
  68. size_t data_size,
  69. size_t *data_length);
  70. /** Export a public RSA key or the public part of an RSA key pair in binary
  71. * format.
  72. *
  73. * \note The signature of this function is that of a PSA driver
  74. * export_public_key entry point. This function behaves as an
  75. * export_public_key entry point as defined in the PSA driver interface
  76. * specification.
  77. *
  78. * \param[in] attributes The attributes for the key to export.
  79. * \param[in] key_buffer Material or context of the key to export.
  80. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  81. * \param[out] data Buffer where the key data is to be written.
  82. * \param[in] data_size Size of the \p data buffer in bytes.
  83. * \param[out] data_length On success, the number of bytes written in
  84. * \p data.
  85. *
  86. * \retval #PSA_SUCCESS The RSA public key was exported successfully.
  87. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  88. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  89. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  90. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  91. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  92. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  93. */
  94. psa_status_t mbedtls_psa_rsa_export_public_key(
  95. const psa_key_attributes_t *attributes,
  96. const uint8_t *key_buffer, size_t key_buffer_size,
  97. uint8_t *data, size_t data_size, size_t *data_length);
  98. /**
  99. * \brief Generate an RSA key.
  100. *
  101. * \param[in] attributes The attributes for the RSA key to generate.
  102. * \param[in] custom_data The public exponent to use.
  103. * This can be a null pointer if
  104. * \c params_data_length is 0.
  105. * \param custom_data_length Length of \p custom_data in bytes.
  106. * This can be 0, in which case the
  107. * public exponent will be 65537.
  108. * \param[out] key_buffer Buffer where the key data is to be written.
  109. * \param[in] key_buffer_size Size of \p key_buffer in bytes.
  110. * \param[out] key_buffer_length On success, the number of bytes written in
  111. * \p key_buffer.
  112. *
  113. * \retval #PSA_SUCCESS
  114. * The key was successfully generated.
  115. * \retval #PSA_ERROR_NOT_SUPPORTED
  116. * Key length or type not supported.
  117. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  118. * The size of \p key_buffer is too small.
  119. */
  120. psa_status_t mbedtls_psa_rsa_generate_key(
  121. const psa_key_attributes_t *attributes,
  122. const uint8_t *custom_data, size_t custom_data_length,
  123. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length);
  124. /** Sign an already-calculated hash with an RSA private key.
  125. *
  126. * \note The signature of this function is that of a PSA driver
  127. * sign_hash entry point. This function behaves as a sign_hash
  128. * entry point as defined in the PSA driver interface specification for
  129. * transparent drivers.
  130. *
  131. * \param[in] attributes The attributes of the RSA key to use for the
  132. * operation.
  133. * \param[in] key_buffer The buffer containing the RSA key context.
  134. * format.
  135. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  136. * \param[in] alg A signature algorithm that is compatible with
  137. * an RSA key.
  138. * \param[in] hash The hash or message to sign.
  139. * \param[in] hash_length Size of the \p hash buffer in bytes.
  140. * \param[out] signature Buffer where the signature is to be written.
  141. * \param[in] signature_size Size of the \p signature buffer in bytes.
  142. * \param[out] signature_length On success, the number of bytes
  143. * that make up the returned signature value.
  144. *
  145. * \retval #PSA_SUCCESS \emptydescription
  146. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  147. * The size of the \p signature buffer is too small. You can
  148. * determine a sufficient buffer size by calling
  149. * #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_RSA_KEY_PAIR, \c key_bits,
  150. * \p alg) where \c key_bits is the bit-size of the RSA key.
  151. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  152. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  153. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  154. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  155. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  156. */
  157. psa_status_t mbedtls_psa_rsa_sign_hash(
  158. const psa_key_attributes_t *attributes,
  159. const uint8_t *key_buffer, size_t key_buffer_size,
  160. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  161. uint8_t *signature, size_t signature_size, size_t *signature_length);
  162. /**
  163. * \brief Verify the signature a hash or short message using a public RSA key.
  164. *
  165. * \note The signature of this function is that of a PSA driver
  166. * verify_hash entry point. This function behaves as a verify_hash
  167. * entry point as defined in the PSA driver interface specification for
  168. * transparent drivers.
  169. *
  170. * \param[in] attributes The attributes of the RSA key to use for the
  171. * operation.
  172. * \param[in] key_buffer The buffer containing the RSA key context.
  173. * format.
  174. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  175. * \param[in] alg A signature algorithm that is compatible with
  176. * an RSA key.
  177. * \param[in] hash The hash or message whose signature is to be
  178. * verified.
  179. * \param[in] hash_length Size of the \p hash buffer in bytes.
  180. * \param[in] signature Buffer containing the signature to verify.
  181. * \param[in] signature_length Size of the \p signature buffer in bytes.
  182. *
  183. * \retval #PSA_SUCCESS
  184. * The signature is valid.
  185. * \retval #PSA_ERROR_INVALID_SIGNATURE
  186. * The calculation was performed successfully, but the passed
  187. * signature is not a valid signature.
  188. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  189. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  190. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  191. */
  192. psa_status_t mbedtls_psa_rsa_verify_hash(
  193. const psa_key_attributes_t *attributes,
  194. const uint8_t *key_buffer, size_t key_buffer_size,
  195. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  196. const uint8_t *signature, size_t signature_length);
  197. /**
  198. * \brief Encrypt a short message with a public key.
  199. *
  200. * \param attributes The attributes for the key to import.
  201. * \param key_buffer Buffer where the key data is to be written.
  202. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  203. * \param input_length Size of the \p input buffer in bytes.
  204. * \param[in] salt A salt or label, if supported by the
  205. * encryption algorithm.
  206. * If the algorithm does not support a
  207. * salt, pass \c NULL.
  208. * If the algorithm supports an optional
  209. * salt and you do not want to pass a salt,
  210. * pass \c NULL.
  211. *
  212. * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  213. * supported.
  214. * \param salt_length Size of the \p salt buffer in bytes.
  215. * If \p salt is \c NULL, pass 0.
  216. * \param[out] output Buffer where the encrypted message is to
  217. * be written.
  218. * \param output_size Size of the \p output buffer in bytes.
  219. * \param[out] output_length On success, the number of bytes
  220. * that make up the returned output.
  221. *
  222. * \retval #PSA_SUCCESS \emptydescription
  223. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  224. * The size of the \p output buffer is too small. You can
  225. * determine a sufficient buffer size by calling
  226. * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  227. * where \c key_type and \c key_bits are the type and bit-size
  228. * respectively of \p key.
  229. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  230. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  231. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  232. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  233. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  234. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  235. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  236. * \retval #PSA_ERROR_BAD_STATE
  237. * The library has not been previously initialized by psa_crypto_init().
  238. * It is implementation-dependent whether a failure to initialize
  239. * results in this error code.
  240. */
  241. psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
  242. const uint8_t *key_buffer,
  243. size_t key_buffer_size,
  244. psa_algorithm_t alg,
  245. const uint8_t *input,
  246. size_t input_length,
  247. const uint8_t *salt,
  248. size_t salt_length,
  249. uint8_t *output,
  250. size_t output_size,
  251. size_t *output_length);
  252. /**
  253. * \brief Decrypt a short message with a private key.
  254. *
  255. * \param attributes The attributes for the key to import.
  256. * \param key_buffer Buffer where the key data is to be written.
  257. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  258. * \param[in] input The message to decrypt.
  259. * \param input_length Size of the \p input buffer in bytes.
  260. * \param[in] salt A salt or label, if supported by the
  261. * encryption algorithm.
  262. * If the algorithm does not support a
  263. * salt, pass \c NULL.
  264. * If the algorithm supports an optional
  265. * salt and you do not want to pass a salt,
  266. * pass \c NULL.
  267. *
  268. * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  269. * supported.
  270. * \param salt_length Size of the \p salt buffer in bytes.
  271. * If \p salt is \c NULL, pass 0.
  272. * \param[out] output Buffer where the decrypted message is to
  273. * be written.
  274. * \param output_size Size of the \c output buffer in bytes.
  275. * \param[out] output_length On success, the number of bytes
  276. * that make up the returned output.
  277. *
  278. * \retval #PSA_SUCCESS \emptydescription
  279. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  280. * The size of the \p output buffer is too small. You can
  281. * determine a sufficient buffer size by calling
  282. * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  283. * where \c key_type and \c key_bits are the type and bit-size
  284. * respectively of \p key.
  285. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  286. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  287. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  288. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  289. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  290. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  291. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  292. * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
  293. * \retval #PSA_ERROR_BAD_STATE
  294. * The library has not been previously initialized by psa_crypto_init().
  295. * It is implementation-dependent whether a failure to initialize
  296. * results in this error code.
  297. */
  298. psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
  299. const uint8_t *key_buffer,
  300. size_t key_buffer_size,
  301. psa_algorithm_t alg,
  302. const uint8_t *input,
  303. size_t input_length,
  304. const uint8_t *salt,
  305. size_t salt_length,
  306. uint8_t *output,
  307. size_t output_size,
  308. size_t *output_length);
  309. #endif /* PSA_CRYPTO_RSA_H */