psa_crypto_ecp.h 13 KB

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