blob: f7534ca3022cb45ee7eaecce9e679ce48464b097 [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
32/** Convert an internal p256-m error code to a PSA error code
33 *
34 * \param ret An error code thrown by p256-m
35 *
36 * \return The corresponding PSA error code
37 */
Aditya Deshpandeac363d82023-03-21 18:56:31 +000038psa_status_t p256_to_psa_error(int ret);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000039
40
41/** Generate SECP256R1 ECC Key Pair.
42 * Interface function which calls the p256-m key generation function and
43 * places it in the key buffer provided by the caller (mbed TLS) in the
44 * correct format. For a SECP256R1 curve this is the 32 bit private key.
45 *
46 * \param[in] attributes The attributes of the key to use for the
47 * operation.
48 * \param[out] key_buffer The buffer to contain the key data in
49 * output format upon successful return.
50 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
51 * \param[out] key_buffer_length The length of the data written in \p
52 * key_buffer in bytes.
53 *
54 * \retval #PSA_SUCCESS
55 * Success. Keypair generated and stored in buffer.
56 * \retval #PSA_ERROR_NOT_SUPPORTED
57 * \retval #PSA_ERROR_GENERIC_ERROR
58 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
59 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +000060psa_status_t p256_transparent_generate_key(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000061 const psa_key_attributes_t *attributes,
62 uint8_t *key_buffer,
63 size_t key_buffer_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +000064 size_t *key_buffer_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000065
66/** Perform raw key agreement using p256-m's ECDH implementation
67 * \param[in] attributes The attributes of the key to use for the
68 * operation.
69 * \param[in] key_buffer The buffer containing the private key
70 * in the format specified by PSA.
71 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
72 * \param[in] alg A key agreement algorithm that is
73 * compatible with the type of the key.
74 * \param[in] peer_key The buffer containing the peer's public
75 * key in format specified by PSA.
76 * \param[in] peer_key_length Size of the \p peer_key buffer in
77 * bytes.
78 * \param[out] shared_secret The buffer to which the shared secret
79 * is to be written.
80 * \param[in] shared_secret_size Size of the \p shared_secret buffer in
81 * bytes.
82 * \param[out] shared_secret_length On success, the number of bytes that
83 * make up the returned shared secret.
84 * \retval #PSA_SUCCESS
85 * Success. Shared secret successfully calculated.
86 * \retval #PSA_ERROR_NOT_SUPPORTED
87 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +000088psa_status_t p256_transparent_key_agreement(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000089 const psa_key_attributes_t *attributes,
90 const uint8_t *key_buffer,
91 size_t key_buffer_size,
92 psa_algorithm_t alg,
93 const uint8_t *peer_key,
94 size_t peer_key_length,
95 uint8_t *shared_secret,
96 size_t shared_secret_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +000097 size_t *shared_secret_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +000098
99/** Sign an already-calculated hash with a private key using p256-m's ECDSA
100 * implementation
101 * \param[in] attributes The attributes of the key to use for the
102 * operation.
103 * \param[in] key_buffer The buffer containing the private key
104 * in the format specified by PSA.
105 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
106 * \param[in] alg A signature algorithm that is compatible
107 * with the type of the key.
108 * \param[in] hash The hash to sign.
109 * \param[in] hash_length Size of the \p hash buffer in bytes.
110 * \param[out] signature Buffer where signature is to be written.
111 * \param[in] signature_size Size of the \p signature buffer in bytes.
112 * \param[out] signature_length On success, the number of bytes
113 * that make up the returned signature value.
114 *
115 * \retval #PSA_SUCCESS
116 * Success. Hash was signed successfully.
117 * respectively of the key.
118 * \retval #PSA_ERROR_NOT_SUPPORTED
119 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000120psa_status_t p256_transparent_sign_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000121 const psa_key_attributes_t *attributes,
122 const uint8_t *key_buffer,
123 size_t key_buffer_size,
124 psa_algorithm_t alg,
125 const uint8_t *hash,
126 size_t hash_length,
127 uint8_t *signature,
128 size_t signature_size,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000129 size_t *signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000130
131/** Verify the signature of a hash using a SECP256R1 public key using p256-m's
132 * ECDSA implementation.
133 *
134 * \note p256-m expects a 64 byte public key, but the contents of the key
135 buffer may be the 32 byte keypair representation or the 65 byte
136 public key representation. As a result, this function calls
137 psa_driver_wrapper_export_public_key() to ensure the public key
138 can be passed to p256-m.
139 *
140 * \param[in] attributes The attributes of the key to use for the
141 * operation.
142 *
143 * \param[in] key_buffer The buffer containing the key
144 * in the format specified by PSA.
145 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
146 * \param[in] alg A signature algorithm that is compatible with
147 * the type of the key.
148 * \param[in] hash The hash whose signature is to be
149 * verified.
150 * \param[in] hash_length Size of the \p hash buffer in bytes.
151 * \param[in] signature Buffer containing the signature to verify.
152 * \param[in] signature_length Size of the \p signature buffer in bytes.
153 *
154 * \retval #PSA_SUCCESS
155 * The signature is valid.
156 * \retval #PSA_ERROR_INVALID_SIGNATURE
157 * The calculation was performed successfully, but the passed
158 * signature is not a valid signature.
159 * \retval #PSA_ERROR_NOT_SUPPORTED
160 */
Aditya Deshpande695e44b2023-01-23 14:59:29 +0000161psa_status_t p256_transparent_verify_hash(
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000162 const psa_key_attributes_t *attributes,
163 const uint8_t *key_buffer,
164 size_t key_buffer_size,
165 psa_algorithm_t alg,
166 const uint8_t *hash,
167 size_t hash_length,
168 const uint8_t *signature,
Aditya Deshpandeac363d82023-03-21 18:56:31 +0000169 size_t signature_length);
Aditya Deshpandee41f7e42023-01-12 16:29:02 +0000170
171#endif /* P256M_DRIVER_ENTRYPOINTS_H */