xref: /aosp_15_r20/external/mbedtls/3rdparty/p256-m/p256-m_driver_entrypoints.h (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1*62c56f98SSadaf Ebrahimi /*
2*62c56f98SSadaf Ebrahimi  *   Driver entry points for p256-m
3*62c56f98SSadaf Ebrahimi  */
4*62c56f98SSadaf Ebrahimi /*
5*62c56f98SSadaf Ebrahimi  *  Copyright The Mbed TLS Contributors
6*62c56f98SSadaf Ebrahimi  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7*62c56f98SSadaf Ebrahimi  */
8*62c56f98SSadaf Ebrahimi 
9*62c56f98SSadaf Ebrahimi #ifndef P256M_DRIVER_ENTRYPOINTS_H
10*62c56f98SSadaf Ebrahimi #define P256M_DRIVER_ENTRYPOINTS_H
11*62c56f98SSadaf Ebrahimi 
12*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_PSA_P256M_DRIVER_ENABLED)
13*62c56f98SSadaf Ebrahimi #ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
14*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
15*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
16*62c56f98SSadaf Ebrahimi #endif /* MBEDTLS_PSA_P256M_DRIVER_ENABLED */
17*62c56f98SSadaf Ebrahimi 
18*62c56f98SSadaf Ebrahimi #include "psa/crypto_types.h"
19*62c56f98SSadaf Ebrahimi 
20*62c56f98SSadaf Ebrahimi /** Import SECP256R1 key.
21*62c56f98SSadaf Ebrahimi  *
22*62c56f98SSadaf Ebrahimi  * \param[in]  attributes           The attributes of the key to use for the
23*62c56f98SSadaf Ebrahimi  *                                  operation.
24*62c56f98SSadaf Ebrahimi  * \param[in]  data                 The raw key material. For private keys
25*62c56f98SSadaf Ebrahimi  *                                  this must be a big-endian integer of 32
26*62c56f98SSadaf Ebrahimi  *                                  bytes; for public key this must be an
27*62c56f98SSadaf Ebrahimi  *                                  uncompressed ECPoint (65 bytes).
28*62c56f98SSadaf Ebrahimi  * \param[in]  data_length          The size of the raw key material.
29*62c56f98SSadaf Ebrahimi  * \param[out] key_buffer           The buffer to contain the key data in
30*62c56f98SSadaf Ebrahimi  *                                  output format upon successful return.
31*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer_size      Size of the \p key_buffer buffer in bytes.
32*62c56f98SSadaf Ebrahimi  * \param[out] key_buffer_length    The length of the data written in \p
33*62c56f98SSadaf Ebrahimi  *                                  key_buffer in bytes.
34*62c56f98SSadaf Ebrahimi  * \param[out] bits                 The bitsize of the key.
35*62c56f98SSadaf Ebrahimi  *
36*62c56f98SSadaf Ebrahimi  * \retval  #PSA_SUCCESS
37*62c56f98SSadaf Ebrahimi  *          Success. Keypair generated and stored in buffer.
38*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_NOT_SUPPORTED
39*62c56f98SSadaf Ebrahimi  *          The input is not supported by this driver (not SECP256R1).
40*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_INVALID_ARGUMENT
41*62c56f98SSadaf Ebrahimi  *          The input is invalid.
42*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_BUFFER_TOO_SMALL
43*62c56f98SSadaf Ebrahimi  *          \p key_buffer_size is too small.
44*62c56f98SSadaf Ebrahimi  */
45*62c56f98SSadaf Ebrahimi psa_status_t p256_transparent_import_key(const psa_key_attributes_t *attributes,
46*62c56f98SSadaf Ebrahimi                              const uint8_t *data,
47*62c56f98SSadaf Ebrahimi                              size_t data_length,
48*62c56f98SSadaf Ebrahimi                              uint8_t *key_buffer,
49*62c56f98SSadaf Ebrahimi                              size_t key_buffer_size,
50*62c56f98SSadaf Ebrahimi                              size_t *key_buffer_length,
51*62c56f98SSadaf Ebrahimi                              size_t *bits);
52*62c56f98SSadaf Ebrahimi 
53*62c56f98SSadaf Ebrahimi /** Export SECP256R1 public key, from the private key.
54*62c56f98SSadaf Ebrahimi  *
55*62c56f98SSadaf Ebrahimi  * \param[in]  attributes           The attributes of the key to use for the
56*62c56f98SSadaf Ebrahimi  *                                  operation.
57*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer           The private key in the export format.
58*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer_size      The size of the private key in bytes.
59*62c56f98SSadaf Ebrahimi  * \param[out] data                 The buffer to contain the public key in
60*62c56f98SSadaf Ebrahimi  *                                  the export format upon successful return.
61*62c56f98SSadaf Ebrahimi  * \param[in]  data_size            The size of the \p data buffer in bytes.
62*62c56f98SSadaf Ebrahimi  * \param[out] data_length          The length written to \p data in bytes.
63*62c56f98SSadaf Ebrahimi  *
64*62c56f98SSadaf Ebrahimi  * \retval  #PSA_SUCCESS
65*62c56f98SSadaf Ebrahimi  *          Success. Keypair generated and stored in buffer.
66*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_NOT_SUPPORTED
67*62c56f98SSadaf Ebrahimi  *          The input is not supported by this driver (not SECP256R1).
68*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_INVALID_ARGUMENT
69*62c56f98SSadaf Ebrahimi  *          The input is invalid.
70*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_BUFFER_TOO_SMALL
71*62c56f98SSadaf Ebrahimi  *          \p key_buffer_size is too small.
72*62c56f98SSadaf Ebrahimi  */
73*62c56f98SSadaf Ebrahimi psa_status_t p256_transparent_export_public_key(const psa_key_attributes_t *attributes,
74*62c56f98SSadaf Ebrahimi                                     const uint8_t *key_buffer,
75*62c56f98SSadaf Ebrahimi                                     size_t key_buffer_size,
76*62c56f98SSadaf Ebrahimi                                     uint8_t *data,
77*62c56f98SSadaf Ebrahimi                                     size_t data_size,
78*62c56f98SSadaf Ebrahimi                                     size_t *data_length);
79*62c56f98SSadaf Ebrahimi 
80*62c56f98SSadaf Ebrahimi /** Generate SECP256R1 ECC Key Pair.
81*62c56f98SSadaf Ebrahimi  *  Interface function which calls the p256-m key generation function and
82*62c56f98SSadaf Ebrahimi  *  places it in the key buffer provided by the caller (Mbed TLS) in the
83*62c56f98SSadaf Ebrahimi  *  correct format. For a SECP256R1 curve this is the 32 bit private key.
84*62c56f98SSadaf Ebrahimi  *
85*62c56f98SSadaf Ebrahimi  * \param[in]  attributes           The attributes of the key to use for the
86*62c56f98SSadaf Ebrahimi  *                                  operation.
87*62c56f98SSadaf Ebrahimi  * \param[out]  key_buffer          The buffer to contain the key data in
88*62c56f98SSadaf Ebrahimi  *                                  output format upon successful return.
89*62c56f98SSadaf Ebrahimi  * \param[in]   key_buffer_size     Size of the \p key_buffer buffer in bytes.
90*62c56f98SSadaf Ebrahimi  * \param[out]  key_buffer_length   The length of the data written in \p
91*62c56f98SSadaf Ebrahimi  *                                  key_buffer in bytes.
92*62c56f98SSadaf Ebrahimi  *
93*62c56f98SSadaf Ebrahimi  * \retval  #PSA_SUCCESS
94*62c56f98SSadaf Ebrahimi  *          Success. Keypair generated and stored in buffer.
95*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_BUFFER_TOO_SMALL
96*62c56f98SSadaf Ebrahimi  *          \p key_buffer_size is too small.
97*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_GENERIC_ERROR
98*62c56f98SSadaf Ebrahimi  *          The internal RNG failed.
99*62c56f98SSadaf Ebrahimi  */
100*62c56f98SSadaf Ebrahimi psa_status_t p256_transparent_generate_key(
101*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes,
102*62c56f98SSadaf Ebrahimi     uint8_t *key_buffer,
103*62c56f98SSadaf Ebrahimi     size_t key_buffer_size,
104*62c56f98SSadaf Ebrahimi     size_t *key_buffer_length);
105*62c56f98SSadaf Ebrahimi 
106*62c56f98SSadaf Ebrahimi /** Perform raw key agreement using p256-m's ECDH implementation
107*62c56f98SSadaf Ebrahimi  * \param[in]  attributes           The attributes of the key to use for the
108*62c56f98SSadaf Ebrahimi  *                                  operation.
109*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer           The buffer containing the private key
110*62c56f98SSadaf Ebrahimi  *                                  in the format specified by PSA.
111*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer_size      Size of the \p key_buffer buffer in bytes.
112*62c56f98SSadaf Ebrahimi  * \param[in]  alg                  A key agreement algorithm that is
113*62c56f98SSadaf Ebrahimi  *                                  compatible with the type of the key.
114*62c56f98SSadaf Ebrahimi  * \param[in]  peer_key             The buffer containing the peer's public
115*62c56f98SSadaf Ebrahimi  *                                  key in format specified by PSA.
116*62c56f98SSadaf Ebrahimi  * \param[in]  peer_key_length      Size of the \p peer_key buffer in
117*62c56f98SSadaf Ebrahimi  *                                  bytes.
118*62c56f98SSadaf Ebrahimi  * \param[out] shared_secret        The buffer to which the shared secret
119*62c56f98SSadaf Ebrahimi  *                                  is to be written.
120*62c56f98SSadaf Ebrahimi  * \param[in]  shared_secret_size   Size of the \p shared_secret buffer in
121*62c56f98SSadaf Ebrahimi  *                                  bytes.
122*62c56f98SSadaf Ebrahimi  * \param[out] shared_secret_length On success, the number of bytes that
123*62c56f98SSadaf Ebrahimi  *                                  make up the returned shared secret.
124*62c56f98SSadaf Ebrahimi  * \retval  #PSA_SUCCESS
125*62c56f98SSadaf Ebrahimi  *          Success. Shared secret successfully calculated.
126*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_INVALID_ARGUMENT
127*62c56f98SSadaf Ebrahimi  *          The input is invalid.
128*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_BUFFER_TOO_SMALL
129*62c56f98SSadaf Ebrahimi  *          \p shared_secret_size is too small.
130*62c56f98SSadaf Ebrahimi  */
131*62c56f98SSadaf Ebrahimi psa_status_t p256_transparent_key_agreement(
132*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes,
133*62c56f98SSadaf Ebrahimi     const uint8_t *key_buffer,
134*62c56f98SSadaf Ebrahimi     size_t key_buffer_size,
135*62c56f98SSadaf Ebrahimi     psa_algorithm_t alg,
136*62c56f98SSadaf Ebrahimi     const uint8_t *peer_key,
137*62c56f98SSadaf Ebrahimi     size_t peer_key_length,
138*62c56f98SSadaf Ebrahimi     uint8_t *shared_secret,
139*62c56f98SSadaf Ebrahimi     size_t shared_secret_size,
140*62c56f98SSadaf Ebrahimi     size_t *shared_secret_length);
141*62c56f98SSadaf Ebrahimi 
142*62c56f98SSadaf Ebrahimi /** Sign an already-calculated hash with a private key using p256-m's ECDSA
143*62c56f98SSadaf Ebrahimi  *  implementation
144*62c56f98SSadaf Ebrahimi  * \param[in]  attributes           The attributes of the key to use for the
145*62c56f98SSadaf Ebrahimi  *                                  operation.
146*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer           The buffer containing the private key
147*62c56f98SSadaf Ebrahimi  *                                  in the format specified by PSA.
148*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer_size      Size of the \p key_buffer buffer in bytes.
149*62c56f98SSadaf Ebrahimi  * \param[in]  alg                  A signature algorithm that is compatible
150*62c56f98SSadaf Ebrahimi  *                                  with the type of the key.
151*62c56f98SSadaf Ebrahimi  * \param[in]  hash                 The hash to sign.
152*62c56f98SSadaf Ebrahimi  * \param[in]  hash_length          Size of the \p hash buffer in bytes.
153*62c56f98SSadaf Ebrahimi  * \param[out] signature            Buffer where signature is to be written.
154*62c56f98SSadaf Ebrahimi  * \param[in]  signature_size       Size of the \p signature buffer in bytes.
155*62c56f98SSadaf Ebrahimi  * \param[out] signature_length     On success, the number of bytes
156*62c56f98SSadaf Ebrahimi  *                                  that make up the returned signature value.
157*62c56f98SSadaf Ebrahimi  *
158*62c56f98SSadaf Ebrahimi  * \retval  #PSA_SUCCESS
159*62c56f98SSadaf Ebrahimi  *          Success. Hash was signed successfully.
160*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_INVALID_ARGUMENT
161*62c56f98SSadaf Ebrahimi  *          The input is invalid.
162*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_BUFFER_TOO_SMALL
163*62c56f98SSadaf Ebrahimi  *          \p signature_size is too small.
164*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_GENERIC_ERROR
165*62c56f98SSadaf Ebrahimi  *          The internal RNG failed.
166*62c56f98SSadaf Ebrahimi  */
167*62c56f98SSadaf Ebrahimi psa_status_t p256_transparent_sign_hash(
168*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes,
169*62c56f98SSadaf Ebrahimi     const uint8_t *key_buffer,
170*62c56f98SSadaf Ebrahimi     size_t key_buffer_size,
171*62c56f98SSadaf Ebrahimi     psa_algorithm_t alg,
172*62c56f98SSadaf Ebrahimi     const uint8_t *hash,
173*62c56f98SSadaf Ebrahimi     size_t hash_length,
174*62c56f98SSadaf Ebrahimi     uint8_t *signature,
175*62c56f98SSadaf Ebrahimi     size_t signature_size,
176*62c56f98SSadaf Ebrahimi     size_t *signature_length);
177*62c56f98SSadaf Ebrahimi 
178*62c56f98SSadaf Ebrahimi /** Verify the signature of a hash using a SECP256R1 public key using p256-m's
179*62c56f98SSadaf Ebrahimi  *  ECDSA implementation.
180*62c56f98SSadaf Ebrahimi  *
181*62c56f98SSadaf Ebrahimi  * \note p256-m expects a 64 byte public key, but the contents of the key
182*62c56f98SSadaf Ebrahimi          buffer may be the 32 byte keypair representation or the 65 byte
183*62c56f98SSadaf Ebrahimi          public key representation. As a result, this function calls
184*62c56f98SSadaf Ebrahimi          psa_driver_wrapper_export_public_key() to ensure the public key
185*62c56f98SSadaf Ebrahimi          can be passed to p256-m.
186*62c56f98SSadaf Ebrahimi  *
187*62c56f98SSadaf Ebrahimi  * \param[in]  attributes       The attributes of the key to use for the
188*62c56f98SSadaf Ebrahimi  *                              operation.
189*62c56f98SSadaf Ebrahimi  *
190*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer       The buffer containing the key
191*62c56f98SSadaf Ebrahimi  *                              in the format specified by PSA.
192*62c56f98SSadaf Ebrahimi  * \param[in]  key_buffer_size  Size of the \p key_buffer buffer in bytes.
193*62c56f98SSadaf Ebrahimi  * \param[in]  alg              A signature algorithm that is compatible with
194*62c56f98SSadaf Ebrahimi  *                              the type of the key.
195*62c56f98SSadaf Ebrahimi  * \param[in]  hash             The hash whose signature is to be
196*62c56f98SSadaf Ebrahimi  *                              verified.
197*62c56f98SSadaf Ebrahimi  * \param[in]  hash_length      Size of the \p hash buffer in bytes.
198*62c56f98SSadaf Ebrahimi  * \param[in]  signature        Buffer containing the signature to verify.
199*62c56f98SSadaf Ebrahimi  * \param[in]  signature_length Size of the \p signature buffer in bytes.
200*62c56f98SSadaf Ebrahimi  *
201*62c56f98SSadaf Ebrahimi  * \retval  #PSA_SUCCESS
202*62c56f98SSadaf Ebrahimi  *          The signature is valid.
203*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_INVALID_SIGNATURE
204*62c56f98SSadaf Ebrahimi  *          The calculation was performed successfully, but the passed
205*62c56f98SSadaf Ebrahimi  *          signature is not a valid signature.
206*62c56f98SSadaf Ebrahimi  * \retval  #PSA_ERROR_INVALID_ARGUMENT
207*62c56f98SSadaf Ebrahimi  *          The input is invalid.
208*62c56f98SSadaf Ebrahimi  */
209*62c56f98SSadaf Ebrahimi psa_status_t p256_transparent_verify_hash(
210*62c56f98SSadaf Ebrahimi     const psa_key_attributes_t *attributes,
211*62c56f98SSadaf Ebrahimi     const uint8_t *key_buffer,
212*62c56f98SSadaf Ebrahimi     size_t key_buffer_size,
213*62c56f98SSadaf Ebrahimi     psa_algorithm_t alg,
214*62c56f98SSadaf Ebrahimi     const uint8_t *hash,
215*62c56f98SSadaf Ebrahimi     size_t hash_length,
216*62c56f98SSadaf Ebrahimi     const uint8_t *signature,
217*62c56f98SSadaf Ebrahimi     size_t signature_length);
218*62c56f98SSadaf Ebrahimi 
219*62c56f98SSadaf Ebrahimi #endif /* P256M_DRIVER_ENTRYPOINTS_H */
220