Aditya Deshpande | 045b370 | 2023-02-20 17:08:30 +0000 | [diff] [blame] | 1 | /* |
| 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 Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 21 | #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é-Gonnard | 5424cf2 | 2023-08-07 10:56:12 +0200 | [diff] [blame] | 32 | /** 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 | */ |
| 57 | psa_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é-Gonnard | 18d7142 | 2023-08-07 11:18:05 +0200 | [diff] [blame] | 65 | /** 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 | */ |
| 85 | psa_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 Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 92 | /** 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. |
Manuel Pégourié-Gonnard | f251894 | 2023-09-20 09:42:55 +0200 | [diff] [blame^] | 107 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 108 | * \p key_buffer_size is too small. |
| 109 | * \retval #PSA_ERROR_GENERIC_ERROR |
| 110 | * The internal RNG failed. |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 111 | */ |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 112 | psa_status_t p256_transparent_generate_key( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 113 | const psa_key_attributes_t *attributes, |
| 114 | uint8_t *key_buffer, |
| 115 | size_t key_buffer_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 116 | size_t *key_buffer_length); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 117 | |
| 118 | /** Perform raw key agreement using p256-m's ECDH implementation |
| 119 | * \param[in] attributes The attributes of the key to use for the |
| 120 | * operation. |
| 121 | * \param[in] key_buffer The buffer containing the private key |
| 122 | * in the format specified by PSA. |
| 123 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 124 | * \param[in] alg A key agreement algorithm that is |
| 125 | * compatible with the type of the key. |
| 126 | * \param[in] peer_key The buffer containing the peer's public |
| 127 | * key in format specified by PSA. |
| 128 | * \param[in] peer_key_length Size of the \p peer_key buffer in |
| 129 | * bytes. |
| 130 | * \param[out] shared_secret The buffer to which the shared secret |
| 131 | * is to be written. |
| 132 | * \param[in] shared_secret_size Size of the \p shared_secret buffer in |
| 133 | * bytes. |
| 134 | * \param[out] shared_secret_length On success, the number of bytes that |
| 135 | * make up the returned shared secret. |
Manuel Pégourié-Gonnard | f251894 | 2023-09-20 09:42:55 +0200 | [diff] [blame^] | 136 | * \retval #PSA_SUCCESS |
| 137 | * Success. Shared secret successfully calculated. |
| 138 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 139 | * The input is invalid. |
| 140 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 141 | * \p shared_secret_size is too small. |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 142 | */ |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 143 | psa_status_t p256_transparent_key_agreement( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 144 | const psa_key_attributes_t *attributes, |
| 145 | const uint8_t *key_buffer, |
| 146 | size_t key_buffer_size, |
| 147 | psa_algorithm_t alg, |
| 148 | const uint8_t *peer_key, |
| 149 | size_t peer_key_length, |
| 150 | uint8_t *shared_secret, |
| 151 | size_t shared_secret_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 152 | size_t *shared_secret_length); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 153 | |
| 154 | /** Sign an already-calculated hash with a private key using p256-m's ECDSA |
| 155 | * implementation |
| 156 | * \param[in] attributes The attributes of the key to use for the |
| 157 | * operation. |
| 158 | * \param[in] key_buffer The buffer containing the private key |
| 159 | * in the format specified by PSA. |
| 160 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 161 | * \param[in] alg A signature algorithm that is compatible |
| 162 | * with the type of the key. |
| 163 | * \param[in] hash The hash to sign. |
| 164 | * \param[in] hash_length Size of the \p hash buffer in bytes. |
| 165 | * \param[out] signature Buffer where signature is to be written. |
| 166 | * \param[in] signature_size Size of the \p signature buffer in bytes. |
| 167 | * \param[out] signature_length On success, the number of bytes |
| 168 | * that make up the returned signature value. |
| 169 | * |
Manuel Pégourié-Gonnard | f251894 | 2023-09-20 09:42:55 +0200 | [diff] [blame^] | 170 | * \retval #PSA_SUCCESS |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 171 | * Success. Hash was signed successfully. |
Manuel Pégourié-Gonnard | f251894 | 2023-09-20 09:42:55 +0200 | [diff] [blame^] | 172 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 173 | * The input is invalid. |
| 174 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 175 | * \p signature_size is too small. |
| 176 | * \retval #PSA_ERROR_GENERIC_ERROR |
| 177 | * The internal RNG failed. |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 178 | */ |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 179 | psa_status_t p256_transparent_sign_hash( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 180 | const psa_key_attributes_t *attributes, |
| 181 | const uint8_t *key_buffer, |
| 182 | size_t key_buffer_size, |
| 183 | psa_algorithm_t alg, |
| 184 | const uint8_t *hash, |
| 185 | size_t hash_length, |
| 186 | uint8_t *signature, |
| 187 | size_t signature_size, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 188 | size_t *signature_length); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 189 | |
| 190 | /** Verify the signature of a hash using a SECP256R1 public key using p256-m's |
| 191 | * ECDSA implementation. |
| 192 | * |
| 193 | * \note p256-m expects a 64 byte public key, but the contents of the key |
| 194 | buffer may be the 32 byte keypair representation or the 65 byte |
| 195 | public key representation. As a result, this function calls |
| 196 | psa_driver_wrapper_export_public_key() to ensure the public key |
| 197 | can be passed to p256-m. |
| 198 | * |
| 199 | * \param[in] attributes The attributes of the key to use for the |
| 200 | * operation. |
| 201 | * |
| 202 | * \param[in] key_buffer The buffer containing the key |
| 203 | * in the format specified by PSA. |
| 204 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 205 | * \param[in] alg A signature algorithm that is compatible with |
| 206 | * the type of the key. |
| 207 | * \param[in] hash The hash whose signature is to be |
| 208 | * verified. |
| 209 | * \param[in] hash_length Size of the \p hash buffer in bytes. |
| 210 | * \param[in] signature Buffer containing the signature to verify. |
| 211 | * \param[in] signature_length Size of the \p signature buffer in bytes. |
| 212 | * |
Manuel Pégourié-Gonnard | f251894 | 2023-09-20 09:42:55 +0200 | [diff] [blame^] | 213 | * \retval #PSA_SUCCESS |
| 214 | * The signature is valid. |
| 215 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 216 | * The calculation was performed successfully, but the passed |
| 217 | * signature is not a valid signature. |
| 218 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 219 | * The input is invalid. |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 220 | */ |
Aditya Deshpande | 695e44b | 2023-01-23 14:59:29 +0000 | [diff] [blame] | 221 | psa_status_t p256_transparent_verify_hash( |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 222 | const psa_key_attributes_t *attributes, |
| 223 | const uint8_t *key_buffer, |
| 224 | size_t key_buffer_size, |
| 225 | psa_algorithm_t alg, |
| 226 | const uint8_t *hash, |
| 227 | size_t hash_length, |
| 228 | const uint8_t *signature, |
Aditya Deshpande | ac363d8 | 2023-03-21 18:56:31 +0000 | [diff] [blame] | 229 | size_t signature_length); |
Aditya Deshpande | e41f7e4 | 2023-01-12 16:29:02 +0000 | [diff] [blame] | 230 | |
| 231 | #endif /* P256M_DRIVER_ENTRYPOINTS_H */ |