psa_crypto_ecp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * PSA ECP 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_ECP_H
  9. #define PSA_CRYPTO_ECP_H
  10. #include <psa/crypto.h>
  11. #include <mbedtls/ecp.h>
  12. /** Load the contents of a key buffer into an internal ECP representation
  13. *
  14. * \param[in] type The type of key contained in \p data.
  15. * \param[in] curve_bits The nominal bit-size of the curve.
  16. * It must be consistent with the representation
  17. * passed in \p data.
  18. * This can be 0, in which case the bit-size
  19. * is inferred from \p data_length (which is possible
  20. * for all key types and representation formats
  21. * formats that are currently supported or will
  22. * be in the foreseeable future).
  23. * \param[in] data The buffer from which to load the representation.
  24. * \param[in] data_length The size in bytes of \p data.
  25. * \param[out] p_ecp Returns a pointer to an ECP context on success.
  26. * The caller is responsible for freeing both the
  27. * contents of the context and the context itself
  28. * when done.
  29. */
  30. psa_status_t mbedtls_psa_ecp_load_representation(psa_key_type_t type,
  31. size_t curve_bits,
  32. const uint8_t *data,
  33. size_t data_length,
  34. mbedtls_ecp_keypair **p_ecp);
  35. /** Load the public part of an internal ECP, if required.
  36. *
  37. * \param ecp The ECP context to load the public part for.
  38. *
  39. * \return PSA_SUCCESS on success, otherwise an MPI error.
  40. */
  41. psa_status_t mbedtls_psa_ecp_load_public_part(mbedtls_ecp_keypair *ecp);
  42. /** Import an ECP key in binary format.
  43. *
  44. * \note The signature of this function is that of a PSA driver
  45. * import_key entry point. This function behaves as an import_key
  46. * entry point as defined in the PSA driver interface specification for
  47. * transparent drivers.
  48. *
  49. * \param[in] attributes The attributes for the key to import.
  50. * \param[in] data The buffer containing the key data in import
  51. * format.
  52. * \param[in] data_length Size of the \p data buffer in bytes.
  53. * \param[out] key_buffer The buffer containing the key data in output
  54. * format.
  55. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
  56. * size is greater or equal to \p data_length.
  57. * \param[out] key_buffer_length The length of the data written in \p
  58. * key_buffer in bytes.
  59. * \param[out] bits The key size in number of bits.
  60. *
  61. * \retval #PSA_SUCCESS The ECP key was imported successfully.
  62. * \retval #PSA_ERROR_INVALID_ARGUMENT
  63. * The key data is not correctly formatted.
  64. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  65. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  66. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  67. */
  68. psa_status_t mbedtls_psa_ecp_import_key(
  69. const psa_key_attributes_t *attributes,
  70. const uint8_t *data, size_t data_length,
  71. uint8_t *key_buffer, size_t key_buffer_size,
  72. size_t *key_buffer_length, size_t *bits);
  73. /** Export an ECP key to export representation
  74. *
  75. * \param[in] type The type of key (public/private) to export
  76. * \param[in] ecp The internal ECP representation from which to export
  77. * \param[out] data The buffer to export to
  78. * \param[in] data_size The length of the buffer to export to
  79. * \param[out] data_length The amount of bytes written to \p data
  80. */
  81. psa_status_t mbedtls_psa_ecp_export_key(psa_key_type_t type,
  82. mbedtls_ecp_keypair *ecp,
  83. uint8_t *data,
  84. size_t data_size,
  85. size_t *data_length);
  86. /** Export an ECP public key or the public part of an ECP key pair in binary
  87. * format.
  88. *
  89. * \note The signature of this function is that of a PSA driver
  90. * export_public_key entry point. This function behaves as an
  91. * export_public_key entry point as defined in the PSA driver interface
  92. * specification.
  93. *
  94. * \param[in] attributes The attributes for the key to export.
  95. * \param[in] key_buffer Material or context of the key to export.
  96. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  97. * \param[out] data Buffer where the key data is to be written.
  98. * \param[in] data_size Size of the \p data buffer in bytes.
  99. * \param[out] data_length On success, the number of bytes written in
  100. * \p data
  101. *
  102. * \retval #PSA_SUCCESS The ECP public key was exported successfully.
  103. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  104. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  105. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  106. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  107. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  108. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  109. */
  110. psa_status_t mbedtls_psa_ecp_export_public_key(
  111. const psa_key_attributes_t *attributes,
  112. const uint8_t *key_buffer, size_t key_buffer_size,
  113. uint8_t *data, size_t data_size, size_t *data_length);
  114. /**
  115. * \brief Generate an ECP key.
  116. *
  117. * \note The signature of the function is that of a PSA driver generate_key
  118. * entry point.
  119. *
  120. * \param[in] attributes The attributes for the ECP key to generate.
  121. * \param[out] key_buffer Buffer where the key data is to be written.
  122. * \param[in] key_buffer_size Size of \p key_buffer in bytes.
  123. * \param[out] key_buffer_length On success, the number of bytes written in
  124. * \p key_buffer.
  125. *
  126. * \retval #PSA_SUCCESS
  127. * The key was successfully generated.
  128. * \retval #PSA_ERROR_NOT_SUPPORTED
  129. * Key length or type not supported.
  130. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  131. * The size of \p key_buffer is too small.
  132. */
  133. psa_status_t mbedtls_psa_ecp_generate_key(
  134. const psa_key_attributes_t *attributes,
  135. uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length);
  136. /** Sign an already-calculated hash with ECDSA.
  137. *
  138. * \note The signature of this function is that of a PSA driver
  139. * sign_hash entry point. This function behaves as a sign_hash
  140. * entry point as defined in the PSA driver interface specification for
  141. * transparent drivers.
  142. *
  143. * \param[in] attributes The attributes of the ECC key to use for the
  144. * operation.
  145. * \param[in] key_buffer The buffer containing the ECC key context.
  146. * format.
  147. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  148. * \param[in] alg Randomized or deterministic ECDSA algorithm.
  149. * \param[in] hash The hash or message to sign.
  150. * \param[in] hash_length Size of the \p hash buffer in bytes.
  151. * \param[out] signature Buffer where the signature is to be written.
  152. * \param[in] signature_size Size of the \p signature buffer in bytes.
  153. * \param[out] signature_length On success, the number of bytes
  154. * that make up the returned signature value.
  155. *
  156. * \retval #PSA_SUCCESS \emptydescription
  157. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  158. * The size of the \p signature buffer is too small. You can
  159. * determine a sufficient buffer size by calling
  160. * #PSA_SIGN_OUTPUT_SIZE(\c PSA_KEY_TYPE_ECC_KEY_PAIR, \c key_bits,
  161. * \p alg) where \c key_bits is the bit-size of the ECC key.
  162. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  163. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  164. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  165. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  166. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  167. */
  168. psa_status_t mbedtls_psa_ecdsa_sign_hash(
  169. const psa_key_attributes_t *attributes,
  170. const uint8_t *key_buffer, size_t key_buffer_size,
  171. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  172. uint8_t *signature, size_t signature_size, size_t *signature_length);
  173. /**
  174. * \brief Verify an ECDSA hash or short message signature.
  175. *
  176. * \note The signature of this function is that of a PSA driver
  177. * verify_hash entry point. This function behaves as a verify_hash
  178. * entry point as defined in the PSA driver interface specification for
  179. * transparent drivers.
  180. *
  181. * \param[in] attributes The attributes of the ECC key to use for the
  182. * operation.
  183. * \param[in] key_buffer The buffer containing the ECC key context.
  184. * format.
  185. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  186. * \param[in] alg Randomized or deterministic ECDSA algorithm.
  187. * \param[in] hash The hash or message whose signature is to be
  188. * verified.
  189. * \param[in] hash_length Size of the \p hash buffer in bytes.
  190. * \param[in] signature Buffer containing the signature to verify.
  191. * \param[in] signature_length Size of the \p signature buffer in bytes.
  192. *
  193. * \retval #PSA_SUCCESS
  194. * The signature is valid.
  195. * \retval #PSA_ERROR_INVALID_SIGNATURE
  196. * The calculation was performed successfully, but the passed
  197. * signature is not a valid signature.
  198. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  199. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  200. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  201. */
  202. psa_status_t mbedtls_psa_ecdsa_verify_hash(
  203. const psa_key_attributes_t *attributes,
  204. const uint8_t *key_buffer, size_t key_buffer_size,
  205. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  206. const uint8_t *signature, size_t signature_length);
  207. /** Perform a key agreement and return the raw ECDH shared secret.
  208. *
  209. * \note The signature of this function is that of a PSA driver
  210. * key_agreement entry point. This function behaves as a key_agreement
  211. * entry point as defined in the PSA driver interface specification for
  212. * transparent drivers.
  213. *
  214. * \param[in] attributes The attributes of the key to use for the
  215. * operation.
  216. * \param[in] key_buffer The buffer containing the private key
  217. * context.
  218. * \param[in] key_buffer_size Size of the \p key_buffer buffer in
  219. * bytes.
  220. * \param[in] alg A key agreement algorithm that is
  221. * compatible with the type of the key.
  222. * \param[in] peer_key The buffer containing the key context
  223. * of the peer's public key.
  224. * \param[in] peer_key_length Size of the \p peer_key buffer in
  225. * bytes.
  226. * \param[out] shared_secret The buffer to which the shared secret
  227. * is to be written.
  228. * \param[in] shared_secret_size Size of the \p shared_secret buffer in
  229. * bytes.
  230. * \param[out] shared_secret_length On success, the number of bytes that make
  231. * up the returned shared secret.
  232. * \retval #PSA_SUCCESS
  233. * Success. Shared secret successfully calculated.
  234. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  235. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  236. * \retval #PSA_ERROR_INVALID_ARGUMENT
  237. * \p alg is not a key agreement algorithm, or
  238. * \p private_key is not compatible with \p alg,
  239. * or \p peer_key is not valid for \p alg or not compatible with
  240. * \p private_key.
  241. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  242. * \p shared_secret_size is too small
  243. * \retval #PSA_ERROR_NOT_SUPPORTED
  244. * \p alg is not a supported key agreement algorithm.
  245. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  246. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  247. */
  248. psa_status_t mbedtls_psa_key_agreement_ecdh(
  249. const psa_key_attributes_t *attributes,
  250. const uint8_t *key_buffer, size_t key_buffer_size,
  251. psa_algorithm_t alg, const uint8_t *peer_key, size_t peer_key_length,
  252. uint8_t *shared_secret, size_t shared_secret_size,
  253. size_t *shared_secret_length);
  254. #endif /* PSA_CRYPTO_ECP_H */