psa_crypto_rsa.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 \emptydescription
  61. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  62. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  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 \emptydescription
  100. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  101. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  102. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  103. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  104. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  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 \emptydescription
  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 \emptydescription
  160. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  161. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  162. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  163. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  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 \emptydescription
  197. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  198. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  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. /**
  206. * \brief Encrypt a short message with a public key.
  207. *
  208. * \param attributes The attributes for the key to import.
  209. * \param key_buffer Buffer where the key data is to be written.
  210. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  211. * \param input_length Size of the \p input buffer in bytes.
  212. * \param[in] salt A salt or label, if supported by the
  213. * encryption algorithm.
  214. * If the algorithm does not support a
  215. * salt, pass \c NULL.
  216. * If the algorithm supports an optional
  217. * salt and you do not want to pass a salt,
  218. * pass \c NULL.
  219. *
  220. * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  221. * supported.
  222. * \param salt_length Size of the \p salt buffer in bytes.
  223. * If \p salt is \c NULL, pass 0.
  224. * \param[out] output Buffer where the encrypted message is to
  225. * be written.
  226. * \param output_size Size of the \p output buffer in bytes.
  227. * \param[out] output_length On success, the number of bytes
  228. * that make up the returned output.
  229. *
  230. * \retval #PSA_SUCCESS \emptydescription
  231. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  232. * The size of the \p output buffer is too small. You can
  233. * determine a sufficient buffer size by calling
  234. * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  235. * where \c key_type and \c key_bits are the type and bit-size
  236. * respectively of \p key.
  237. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  238. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  239. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  240. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  241. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  242. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  243. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  244. * \retval #PSA_ERROR_BAD_STATE
  245. * The library has not been previously initialized by psa_crypto_init().
  246. * It is implementation-dependent whether a failure to initialize
  247. * results in this error code.
  248. */
  249. psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
  250. const uint8_t *key_buffer,
  251. size_t key_buffer_size,
  252. psa_algorithm_t alg,
  253. const uint8_t *input,
  254. size_t input_length,
  255. const uint8_t *salt,
  256. size_t salt_length,
  257. uint8_t *output,
  258. size_t output_size,
  259. size_t *output_length);
  260. /**
  261. * \brief Decrypt a short message with a private key.
  262. *
  263. * \param attributes The attributes for the key to import.
  264. * \param key_buffer Buffer where the key data is to be written.
  265. * \param key_buffer_size Size of the \p key_buffer buffer in bytes.
  266. * \param[in] input The message to decrypt.
  267. * \param input_length Size of the \p input buffer in bytes.
  268. * \param[in] salt A salt or label, if supported by the
  269. * encryption algorithm.
  270. * If the algorithm does not support a
  271. * salt, pass \c NULL.
  272. * If the algorithm supports an optional
  273. * salt and you do not want to pass a salt,
  274. * pass \c NULL.
  275. *
  276. * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  277. * supported.
  278. * \param salt_length Size of the \p salt buffer in bytes.
  279. * If \p salt is \c NULL, pass 0.
  280. * \param[out] output Buffer where the decrypted message is to
  281. * be written.
  282. * \param output_size Size of the \c output buffer in bytes.
  283. * \param[out] output_length On success, the number of bytes
  284. * that make up the returned output.
  285. *
  286. * \retval #PSA_SUCCESS \emptydescription
  287. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  288. * The size of the \p output buffer is too small. You can
  289. * determine a sufficient buffer size by calling
  290. * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  291. * where \c key_type and \c key_bits are the type and bit-size
  292. * respectively of \p key.
  293. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  294. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  295. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  296. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  297. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  298. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  299. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  300. * \retval #PSA_ERROR_INVALID_PADDING \emptydescription
  301. * \retval #PSA_ERROR_BAD_STATE
  302. * The library has not been previously initialized by psa_crypto_init().
  303. * It is implementation-dependent whether a failure to initialize
  304. * results in this error code.
  305. */
  306. psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
  307. const uint8_t *key_buffer,
  308. size_t key_buffer_size,
  309. psa_algorithm_t alg,
  310. const uint8_t *input,
  311. size_t input_length,
  312. const uint8_t *salt,
  313. size_t salt_length,
  314. uint8_t *output,
  315. size_t output_size,
  316. size_t *output_length);
  317. #endif /* PSA_CRYPTO_RSA_H */