blob: 42e51d975a09c57dd0a47badc33efd9478afae7c [file] [log] [blame]
Aditya Deshpande045b3702023-02-20 17:08:30 +00001/*
2 * Driver entry points for p256-m
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
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000021#ifndef P256M_DRIVER_ENTRYPOINTS_H
22#define P256M_DRIVER_ENTRYPOINTS_H
23
24#if defined(MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED)
25#ifndef PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
26#define PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT
27#endif /* PSA_CRYPTO_ACCELERATOR_DRIVER_PRESENT */
28#endif /* MBEDTLS_P256M_EXAMPLE_DRIVER_ENABLED */
29
30#include "psa/crypto_types.h"
31
Manuel Pégourié-Gonnard5424cf22023-08-07 10:56:12 +020032/** Import SECP256R1 key.
33 *
34 * \param[in] attributes The attributes of the key to use for the
35 * operation.
36 * \param[in] data The raw key material. For private keys
37 * this must be a big-endian integer of 32
38 * bytes; for public key this must be an
39 * uncompressed ECPoint (65 bytes).
40 * \param[in] data_length The size of the raw key material.
41 * \param[out] key_buffer The buffer to contain the key data in
42 * output format upon successful return.
43 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
44 * \param[out] key_buffer_length The length of the data written in \p
45 * key_buffer in bytes.
46 * \param[out] bits The bitsize of the key.
47 *
48 * \retval #PSA_SUCCESS
49 * Success. Keypair generated and stored in buffer.
50 * \retval #PSA_ERROR_NOT_SUPPORTED
51 * The input is not supported by this driver (not SECP256R1).
52 * \retval #PSA_ERROR_INVALID_ARGUMENT
53 * The input is invalid.
54 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
55 * \p key_buffer_size is too small.
56 */
57psa_status_t p256_transparent_import_key(const psa_key_attributes_t *attributes,
58 const uint8_t *data,
59 size_t data_length,
60 uint8_t *key_buffer,
61 size_t key_buffer_size,
62 size_t *key_buffer_length,
63 size_t *bits);
64
Manuel Pégourié-Gonnard18d71422023-08-07 11:18:05 +020065/** Export SECP256R1 public key, from the private key.
66 *
67 * \param[in] attributes The attributes of the key to use for the
68 * operation.
69 * \param[in] key_buffer The private key in the export format.
70 * \param[in] key_buffer_size The size of the private key in bytes.
71 * \param[out] data The buffer to contain the public key in
72 * the export format upon successful return.
73 * \param[in] data_size The size of the \p data buffer in bytes.
74 * \param[out] data_length The length written to \p data in bytes.
75 *
76 * \retval #PSA_SUCCESS
77 * Success. Keypair generated and stored in buffer.
78 * \retval #PSA_ERROR_NOT_SUPPORTED
79 * The input is not supported by this driver (not SECP256R1).
80 * \retval #PSA_ERROR_INVALID_ARGUMENT
81 * The input is invalid.
82 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
83 * \p key_buffer_size is too small.
84 */
85psa_status_t p256_transparent_export_public_key(const psa_key_attributes_t *attributes,
86 const uint8_t *key_buffer,
87 size_t key_buffer_size,
88 uint8_t *data,
89 size_t data_size,
90 size_t *data_length);
91
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000092/** Generate SECP256R1 ECC Key Pair.
93 * Interface function which calls the p256-m key generation function and
94 * places it in the key buffer provided by the caller (mbed TLS) in the
95 * correct format. For a SECP256R1 curve this is the 32 bit private key.
96 *
97 * \param[in] attributes The attributes of the key to use for the
98 * operation.
99 * \param[out] key_buffer The buffer to contain the key data in
100 * output format upon successful return.
101 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
102 * \param[out] key_buffer_length The length of the data written in \p
103 * key_buffer in bytes.
104 *
105 * \retval #PSA_SUCCESS
106 * Success. Keypair generated and stored in buffer.
107 * \retval #PSA_ERROR_NOT_SUPPORTED
108 * \retval #PSA_ERROR_GENERIC_ERROR
109 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
110 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000111psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000112 const psa_key_attributes_t *attributes,
113 uint8_t *key_buffer,
114 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000115 size_t *key_buffer_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000116
117/** Perform raw key agreement using p256-m's ECDH implementation
118 * \param[in] attributes The attributes of the key to use for the
119 * operation.
120 * \param[in] key_buffer The buffer containing the private key
121 * in the format specified by PSA.
122 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
123 * \param[in] alg A key agreement algorithm that is
124 * compatible with the type of the key.
125 * \param[in] peer_key The buffer containing the peer's public
126 * key in format specified by PSA.
127 * \param[in] peer_key_length Size of the \p peer_key buffer in
128 * bytes.
129 * \param[out] shared_secret The buffer to which the shared secret
130 * is to be written.
131 * \param[in] shared_secret_size Size of the \p shared_secret buffer in
132 * bytes.
133 * \param[out] shared_secret_length On success, the number of bytes that
134 * make up the returned shared secret.
135 * \retval #PSA_SUCCESS
136 * Success. Shared secret successfully calculated.
137 * \retval #PSA_ERROR_NOT_SUPPORTED
138 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000139psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000140 const psa_key_attributes_t *attributes,
141 const uint8_t *key_buffer,
142 size_t key_buffer_size,
143 psa_algorithm_t alg,
144 const uint8_t *peer_key,
145 size_t peer_key_length,
146 uint8_t *shared_secret,
147 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000148 size_t *shared_secret_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000149
150/** Sign an already-calculated hash with a private key using p256-m's ECDSA
151 * implementation
152 * \param[in] attributes The attributes of the key to use for the
153 * operation.
154 * \param[in] key_buffer The buffer containing the private key
155 * in the format specified by PSA.
156 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
157 * \param[in] alg A signature algorithm that is compatible
158 * with the type of the key.
159 * \param[in] hash The hash to sign.
160 * \param[in] hash_length Size of the \p hash buffer in bytes.
161 * \param[out] signature Buffer where signature is to be written.
162 * \param[in] signature_size Size of the \p signature buffer in bytes.
163 * \param[out] signature_length On success, the number of bytes
164 * that make up the returned signature value.
165 *
166 * \retval #PSA_SUCCESS
167 * Success. Hash was signed successfully.
168 * respectively of the key.
169 * \retval #PSA_ERROR_NOT_SUPPORTED
170 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000171psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000172 const psa_key_attributes_t *attributes,
173 const uint8_t *key_buffer,
174 size_t key_buffer_size,
175 psa_algorithm_t alg,
176 const uint8_t *hash,
177 size_t hash_length,
178 uint8_t *signature,
179 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000180 size_t *signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000181
182/** Verify the signature of a hash using a SECP256R1 public key using p256-m's
183 * ECDSA implementation.
184 *
185 * \note p256-m expects a 64 byte public key, but the contents of the key
186 buffer may be the 32 byte keypair representation or the 65 byte
187 public key representation. As a result, this function calls
188 psa_driver_wrapper_export_public_key() to ensure the public key
189 can be passed to p256-m.
190 *
191 * \param[in] attributes The attributes of the key to use for the
192 * operation.
193 *
194 * \param[in] key_buffer The buffer containing the key
195 * in the format specified by PSA.
196 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
197 * \param[in] alg A signature algorithm that is compatible with
198 * the type of the key.
199 * \param[in] hash The hash 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
211 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000212psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000213 const psa_key_attributes_t *attributes,
214 const uint8_t *key_buffer,
215 size_t key_buffer_size,
216 psa_algorithm_t alg,
217 const uint8_t *hash,
218 size_t hash_length,
219 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000220 size_t signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000221
222#endif /* P256M_DRIVER_ENTRYPOINTS_H */