Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA RSA layer on top of Mbed TLS crypto |
| 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 | |
| 21 | #ifndef PSA_CRYPTO_RSA_H |
| 22 | #define PSA_CRYPTO_RSA_H |
| 23 | |
| 24 | #include <psa/crypto.h> |
| 25 | #include <mbedtls/rsa.h> |
| 26 | |
| 27 | /** Load the contents of a key buffer into an internal RSA representation |
| 28 | * |
| 29 | * \param[in] type The type of key contained in \p data. |
| 30 | * \param[in] data The buffer from which to load the representation. |
| 31 | * \param[in] data_length The size in bytes of \p data. |
| 32 | * \param[out] p_rsa Returns a pointer to an RSA context on success. |
| 33 | * The caller is responsible for freeing both the |
| 34 | * contents of the context and the context itself |
| 35 | * when done. |
| 36 | */ |
| 37 | psa_status_t mbedtls_psa_rsa_load_representation( psa_key_type_t type, |
| 38 | const uint8_t *data, |
| 39 | size_t data_length, |
| 40 | mbedtls_rsa_context **p_rsa ); |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 41 | |
Ronald Cron | abf2aef | 2020-11-27 18:13:44 +0100 | [diff] [blame] | 42 | /** Import an RSA key in binary format. |
| 43 | * |
| 44 | * \note The signature of this function is that of a PSA driver |
| 45 | * import_key entry point. This function behaves as an import_key |
| 46 | * entry point as defined in the PSA driver interface specification for |
| 47 | * transparent drivers. |
| 48 | * |
| 49 | * \param[in] attributes The attributes for the key to import. |
| 50 | * \param[in] data The buffer containing the key data in import |
| 51 | * format. |
| 52 | * \param[in] data_length Size of the \p data buffer in bytes. |
| 53 | * \param[out] key_buffer The buffer containing the key data in output |
| 54 | * format. |
| 55 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This |
| 56 | * size is greater or equal to \p data_length. |
| 57 | * \param[out] key_buffer_length The length of the data written in \p |
| 58 | * key_buffer in bytes. |
| 59 | * \param[out] bits The key size in number of bits. |
| 60 | * |
| 61 | * \retval #PSA_SUCCESS The RSA key was imported successfully. |
| 62 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 63 | * The key data is not correctly formatted. |
| 64 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 65 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 66 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 67 | */ |
| 68 | psa_status_t mbedtls_psa_rsa_import_key( |
| 69 | const psa_key_attributes_t *attributes, |
| 70 | const uint8_t *data, size_t data_length, |
| 71 | uint8_t *key_buffer, size_t key_buffer_size, |
| 72 | size_t *key_buffer_length, size_t *bits ); |
| 73 | |
Ronald Cron | e5ca3d8 | 2020-11-26 16:36:16 +0100 | [diff] [blame] | 74 | /** Export an RSA key to export representation |
| 75 | * |
| 76 | * \param[in] type The type of key (public/private) to export |
| 77 | * \param[in] rsa The internal RSA representation from which to export |
| 78 | * \param[out] data The buffer to export to |
| 79 | * \param[in] data_size The length of the buffer to export to |
| 80 | * \param[out] data_length The amount of bytes written to \p data |
| 81 | */ |
| 82 | psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type, |
| 83 | mbedtls_rsa_context *rsa, |
| 84 | uint8_t *data, |
| 85 | size_t data_size, |
| 86 | size_t *data_length ); |
| 87 | |
| 88 | /** Export a public RSA key or the public part of an RSA key pair in binary |
| 89 | * format. |
| 90 | * |
| 91 | * \note The signature of this function is that of a PSA driver |
| 92 | * export_public_key entry point. This function behaves as an |
| 93 | * export_public_key entry point as defined in the PSA driver interface |
| 94 | * specification. |
| 95 | * |
| 96 | * \param[in] attributes The attributes for the key to export. |
| 97 | * \param[in] key_buffer Material or context of the key to export. |
| 98 | * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 99 | * \param[out] data Buffer where the key data is to be written. |
| 100 | * \param[in] data_size Size of the \p data buffer in bytes. |
| 101 | * \param[out] data_length On success, the number of bytes written in |
| 102 | * \p data. |
| 103 | * |
| 104 | * \retval #PSA_SUCCESS The RSA public key was exported successfully. |
| 105 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 106 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 107 | * \retval #PSA_ERROR_HARDWARE_FAILURE |
| 108 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 109 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 110 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 111 | */ |
| 112 | psa_status_t mbedtls_psa_rsa_export_public_key( |
| 113 | const psa_key_attributes_t *attributes, |
| 114 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 115 | uint8_t *data, size_t data_size, size_t *data_length ); |
| 116 | |
Ronald Cron | 9e18fc1 | 2020-11-05 17:36:40 +0100 | [diff] [blame] | 117 | /** |
| 118 | * \brief Generate an RSA key. |
| 119 | * |
| 120 | * \note The signature of the function is that of a PSA driver generate_key |
| 121 | * entry point. |
| 122 | * |
| 123 | * \param[in] attributes The attributes for the RSA key to generate. |
| 124 | * \param[out] key_buffer Buffer where the key data is to be written. |
| 125 | * \param[in] key_buffer_size Size of \p key_buffer in bytes. |
| 126 | * \param[out] key_buffer_length On success, the number of bytes written in |
| 127 | * \p key_buffer. |
| 128 | * |
| 129 | * \retval #PSA_SUCCESS |
| 130 | * The key was successfully generated. |
| 131 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 132 | * Key length or type not supported. |
| 133 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 134 | * The size of \p key_buffer is too small. |
| 135 | */ |
| 136 | psa_status_t mbedtls_psa_rsa_generate_key( |
| 137 | const psa_key_attributes_t *attributes, |
| 138 | uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length ); |
| 139 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 140 | /* |
| 141 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 142 | */ |
| 143 | |
| 144 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 145 | |
Ronald Cron | 784fb32 | 2020-11-30 13:55:05 +0100 | [diff] [blame] | 146 | psa_status_t mbedtls_transparent_test_driver_rsa_import_key( |
| 147 | const psa_key_attributes_t *attributes, |
| 148 | const uint8_t *data, size_t data_length, |
| 149 | uint8_t *key_buffer, size_t key_buffer_size, |
| 150 | size_t *key_buffer_length, size_t *bits ); |
| 151 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 152 | psa_status_t mbedtls_transparent_test_driver_rsa_export_public_key( |
| 153 | const psa_key_attributes_t *attributes, |
| 154 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 155 | uint8_t *data, size_t data_size, size_t *data_length ); |
| 156 | |
Ronald Cron | 3a9c46b | 2020-11-06 09:38:35 +0100 | [diff] [blame^] | 157 | psa_status_t mbedtls_transparent_test_driver_rsa_generate_key( |
| 158 | const psa_key_attributes_t *attributes, |
| 159 | uint8_t *key, size_t key_size, size_t *key_length ); |
| 160 | |
Ronald Cron | f1057d3 | 2020-11-26 19:19:10 +0100 | [diff] [blame] | 161 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 162 | |
Ronald Cron | 00b7bfc | 2020-11-25 15:25:26 +0100 | [diff] [blame] | 163 | #endif /* PSA_CRYPTO_RSA_H */ |