blob: 6eacbc3e65cb50d1d0caef3c34d5475b6c7833d9 [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
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000065/** Generate SECP256R1 ECC Key Pair.
66 * Interface function which calls the p256-m key generation function and
67 * places it in the key buffer provided by the caller (mbed TLS) in the
68 * correct format. For a SECP256R1 curve this is the 32 bit private key.
69 *
70 * \param[in] attributes The attributes of the key to use for the
71 * operation.
72 * \param[out] key_buffer The buffer to contain the key data in
73 * output format upon successful return.
74 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
75 * \param[out] key_buffer_length The length of the data written in \p
76 * key_buffer in bytes.
77 *
78 * \retval #PSA_SUCCESS
79 * Success. Keypair generated and stored in buffer.
80 * \retval #PSA_ERROR_NOT_SUPPORTED
81 * \retval #PSA_ERROR_GENERIC_ERROR
82 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
83 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +000084psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000085 const psa_key_attributes_t *attributes,
86 uint8_t *key_buffer,
87 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +000088 size_t *key_buffer_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000089
90/** Perform raw key agreement using p256-m's ECDH implementation
91 * \param[in] attributes The attributes of the key to use for the
92 * operation.
93 * \param[in] key_buffer The buffer containing the private key
94 * in the format specified by PSA.
95 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
96 * \param[in] alg A key agreement algorithm that is
97 * compatible with the type of the key.
98 * \param[in] peer_key The buffer containing the peer's public
99 * key in format specified by PSA.
100 * \param[in] peer_key_length Size of the \p peer_key buffer in
101 * bytes.
102 * \param[out] shared_secret The buffer to which the shared secret
103 * is to be written.
104 * \param[in] shared_secret_size Size of the \p shared_secret buffer in
105 * bytes.
106 * \param[out] shared_secret_length On success, the number of bytes that
107 * make up the returned shared secret.
108 * \retval #PSA_SUCCESS
109 * Success. Shared secret successfully calculated.
110 * \retval #PSA_ERROR_NOT_SUPPORTED
111 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000112psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000113 const psa_key_attributes_t *attributes,
114 const uint8_t *key_buffer,
115 size_t key_buffer_size,
116 psa_algorithm_t alg,
117 const uint8_t *peer_key,
118 size_t peer_key_length,
119 uint8_t *shared_secret,
120 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000121 size_t *shared_secret_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000122
123/** Sign an already-calculated hash with a private key using p256-m's ECDSA
124 * implementation
125 * \param[in] attributes The attributes of the key to use for the
126 * operation.
127 * \param[in] key_buffer The buffer containing the private key
128 * in the format specified by PSA.
129 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
130 * \param[in] alg A signature algorithm that is compatible
131 * with the type of the key.
132 * \param[in] hash The hash to sign.
133 * \param[in] hash_length Size of the \p hash buffer in bytes.
134 * \param[out] signature Buffer where signature is to be written.
135 * \param[in] signature_size Size of the \p signature buffer in bytes.
136 * \param[out] signature_length On success, the number of bytes
137 * that make up the returned signature value.
138 *
139 * \retval #PSA_SUCCESS
140 * Success. Hash was signed successfully.
141 * respectively of the key.
142 * \retval #PSA_ERROR_NOT_SUPPORTED
143 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000144psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000145 const psa_key_attributes_t *attributes,
146 const uint8_t *key_buffer,
147 size_t key_buffer_size,
148 psa_algorithm_t alg,
149 const uint8_t *hash,
150 size_t hash_length,
151 uint8_t *signature,
152 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000153 size_t *signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000154
155/** Verify the signature of a hash using a SECP256R1 public key using p256-m's
156 * ECDSA implementation.
157 *
158 * \note p256-m expects a 64 byte public key, but the contents of the key
159 buffer may be the 32 byte keypair representation or the 65 byte
160 public key representation. As a result, this function calls
161 psa_driver_wrapper_export_public_key() to ensure the public key
162 can be passed to p256-m.
163 *
164 * \param[in] attributes The attributes of the key to use for the
165 * operation.
166 *
167 * \param[in] key_buffer The buffer containing the key
168 * in the format specified by PSA.
169 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
170 * \param[in] alg A signature algorithm that is compatible with
171 * the type of the key.
172 * \param[in] hash The hash whose signature is to be
173 * verified.
174 * \param[in] hash_length Size of the \p hash buffer in bytes.
175 * \param[in] signature Buffer containing the signature to verify.
176 * \param[in] signature_length Size of the \p signature buffer in bytes.
177 *
178 * \retval #PSA_SUCCESS
179 * The signature is valid.
180 * \retval #PSA_ERROR_INVALID_SIGNATURE
181 * The calculation was performed successfully, but the passed
182 * signature is not a valid signature.
183 * \retval #PSA_ERROR_NOT_SUPPORTED
184 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000185psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000186 const psa_key_attributes_t *attributes,
187 const uint8_t *key_buffer,
188 size_t key_buffer_size,
189 psa_algorithm_t alg,
190 const uint8_t *hash,
191 size_t hash_length,
192 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000193 size_t signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000194
195#endif /* P256M_DRIVER_ENTRYPOINTS_H */