Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file rsa.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Rose Zadik | 21e2926 | 2018-04-17 14:08:56 +0100 | [diff] [blame] | 4 | * \brief This file provides an API for the RSA public-key cryptosystem. |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 5 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 6 | * The RSA public-key cryptosystem is defined in <em>Public-Key |
| 7 | * Cryptography Standards (PKCS) #1 v1.5: RSA Encryption</em> |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 8 | * and <em>Public-Key Cryptography Standards (PKCS) #1 v2.1: |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 9 | * RSA Cryptography Specifications</em>. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 10 | * |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 11 | */ |
| 12 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 13 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: Apache-2.0 |
| 15 | * |
| 16 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 17 | * not use this file except in compliance with the License. |
| 18 | * You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #ifndef MBEDTLS_RSA_H |
| 29 | #define MBEDTLS_RSA_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 30 | #include "mbedtls/private_access.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 32 | #include "mbedtls/build_info.h" |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 33 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 34 | #include "mbedtls/bignum.h" |
| 35 | #include "mbedtls/md.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_THREADING_C) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 38 | #include "mbedtls/threading.h" |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 39 | #endif |
| 40 | |
Przemyslaw Stekiel | b6a6650 | 2021-12-09 11:11:54 +0100 | [diff] [blame^] | 41 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 42 | #include <psa/crypto_driver_common.h> |
| 43 | #include <psa/crypto.h> |
| 44 | |
| 45 | typedef struct { |
| 46 | /* If non-null, on success, copy this to the output. */ |
| 47 | void *forced_output; |
| 48 | size_t forced_output_length; |
| 49 | /* If not PSA_SUCCESS, return this error code instead of processing the |
| 50 | * function call. */ |
| 51 | psa_status_t forced_status; |
| 52 | /* Count the amount of times one of the rsa driver functions is called. */ |
| 53 | unsigned long hits; |
| 54 | } mbedtls_test_driver_rsa_hooks_t; |
| 55 | |
| 56 | #define MBEDTLS_TEST_DRIVER_RSA_INIT { NULL, 0, PSA_SUCCESS, 0 } |
| 57 | |
| 58 | static inline mbedtls_test_driver_rsa_hooks_t |
| 59 | mbedtls_test_driver_rsa_hooks_init( void ) |
| 60 | { |
| 61 | const mbedtls_test_driver_rsa_hooks_t v = MBEDTLS_TEST_DRIVER_RSA_INIT; |
| 62 | return( v ); |
| 63 | } |
| 64 | |
| 65 | extern mbedtls_test_driver_rsa_hooks_t mbedtls_test_driver_rsa_hooks; |
| 66 | |
| 67 | psa_status_t mbedtls_test_transparent_asymmetric_encrypt( |
| 68 | const psa_key_attributes_t *attributes, |
| 69 | const uint8_t *key_buffer, |
| 70 | size_t key_buffer_size, |
| 71 | psa_algorithm_t alg, |
| 72 | const uint8_t *input, |
| 73 | size_t input_length, |
| 74 | const uint8_t *salt, |
| 75 | size_t salt_length, |
| 76 | uint8_t *output, |
| 77 | size_t output_size, |
| 78 | size_t *output_length ); |
| 79 | |
| 80 | psa_status_t mbedtls_test_opaque_asymmetric_encrypt( |
| 81 | const psa_key_attributes_t *attributes, |
| 82 | const uint8_t *key, |
| 83 | size_t key_length, |
| 84 | psa_algorithm_t alg, |
| 85 | const uint8_t *input, |
| 86 | size_t input_length, |
| 87 | const uint8_t *salt, |
| 88 | size_t salt_length, |
| 89 | uint8_t *output, |
| 90 | size_t output_size, |
| 91 | size_t *output_length); |
| 92 | |
| 93 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 94 | |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 95 | /* |
| 96 | * RSA Error codes |
| 97 | */ |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 98 | /** Bad input parameters to function. */ |
| 99 | #define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080 |
| 100 | /** Input data contains invalid padding and is rejected. */ |
| 101 | #define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100 |
| 102 | /** Something failed during generation of a key. */ |
| 103 | #define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180 |
| 104 | /** Key failed to pass the validity check of the library. */ |
| 105 | #define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200 |
| 106 | /** The public key operation failed. */ |
| 107 | #define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280 |
| 108 | /** The private key operation failed. */ |
| 109 | #define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300 |
| 110 | /** The PKCS#1 verification failed. */ |
| 111 | #define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380 |
| 112 | /** The output buffer for decryption is not large enough. */ |
| 113 | #define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 |
| 114 | /** The random generator failed to generate non-zeros. */ |
| 115 | #define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 116 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | /* |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 118 | * RSA constants |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 121 | #define MBEDTLS_RSA_PKCS_V15 0 /**< Use PKCS#1 v1.5 encoding. */ |
| 122 | #define MBEDTLS_RSA_PKCS_V21 1 /**< Use PKCS#1 v2.1 encoding. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 124 | #define MBEDTLS_RSA_SIGN 1 /**< Identifier for RSA signature operations. */ |
| 125 | #define MBEDTLS_RSA_CRYPT 2 /**< Identifier for RSA encryption and decryption operations. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | #define MBEDTLS_RSA_SALT_LEN_ANY -1 |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 128 | |
Manuel Pégourié-Gonnard | e511ffc | 2013-08-22 17:33:21 +0200 | [diff] [blame] | 129 | /* |
| 130 | * The above constants may be used even if the RSA module is compile out, |
| 131 | * eg for alternative (PKCS#11) RSA implemenations in the PK layers. |
| 132 | */ |
Hanno Becker | d22b78b | 2017-10-12 11:42:17 +0100 | [diff] [blame] | 133 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 134 | #ifdef __cplusplus |
| 135 | extern "C" { |
| 136 | #endif |
| 137 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame] | 138 | #if !defined(MBEDTLS_RSA_ALT) |
| 139 | // Regular implementation |
| 140 | // |
| 141 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 143 | * \brief The RSA context structure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 145 | typedef struct mbedtls_rsa_context |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 147 | int MBEDTLS_PRIVATE(ver); /*!< Reserved for internal purposes. |
Gilles Peskine | 4337a9c | 2021-02-09 18:59:42 +0100 | [diff] [blame] | 148 | * Do not set this field in application |
| 149 | * code. Its meaning might change without |
| 150 | * notice. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 151 | size_t MBEDTLS_PRIVATE(len); /*!< The size of \p N in Bytes. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 153 | mbedtls_mpi MBEDTLS_PRIVATE(N); /*!< The public modulus. */ |
| 154 | mbedtls_mpi MBEDTLS_PRIVATE(E); /*!< The public exponent. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 156 | mbedtls_mpi MBEDTLS_PRIVATE(D); /*!< The private exponent. */ |
| 157 | mbedtls_mpi MBEDTLS_PRIVATE(P); /*!< The first prime factor. */ |
| 158 | mbedtls_mpi MBEDTLS_PRIVATE(Q); /*!< The second prime factor. */ |
Hanno Becker | 1a59e79 | 2017-08-23 07:41:10 +0100 | [diff] [blame] | 159 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 160 | mbedtls_mpi MBEDTLS_PRIVATE(DP); /*!< <code>D % (P - 1)</code>. */ |
| 161 | mbedtls_mpi MBEDTLS_PRIVATE(DQ); /*!< <code>D % (Q - 1)</code>. */ |
| 162 | mbedtls_mpi MBEDTLS_PRIVATE(QP); /*!< <code>1 / (Q % P)</code>. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 164 | mbedtls_mpi MBEDTLS_PRIVATE(RN); /*!< cached <code>R^2 mod N</code>. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 166 | mbedtls_mpi MBEDTLS_PRIVATE(RP); /*!< cached <code>R^2 mod P</code>. */ |
| 167 | mbedtls_mpi MBEDTLS_PRIVATE(RQ); /*!< cached <code>R^2 mod Q</code>. */ |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 168 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 169 | mbedtls_mpi MBEDTLS_PRIVATE(Vi); /*!< The cached blinding value. */ |
| 170 | mbedtls_mpi MBEDTLS_PRIVATE(Vf); /*!< The cached un-blinding value. */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 171 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 172 | int MBEDTLS_PRIVATE(padding); /*!< Selects padding mode: |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 173 | #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and |
| 174 | #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 175 | int MBEDTLS_PRIVATE(hash_id); /*!< Hash identifier of mbedtls_md_type_t type, |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 176 | as specified in md.h for use in the MGF |
| 177 | mask generating function used in the |
| 178 | EME-OAEP and EMSA-PSS encodings. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 4337a9c | 2021-02-09 18:59:42 +0100 | [diff] [blame] | 180 | /* Invariant: the mutex is initialized iff ver != 0. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 181 | mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< Thread-safety mutex. */ |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 182 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | mbedtls_rsa_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | |
Ron Eldor | 4e6d55d | 2018-02-07 16:36:15 +0200 | [diff] [blame] | 186 | #else /* MBEDTLS_RSA_ALT */ |
| 187 | #include "rsa_alt.h" |
| 188 | #endif /* MBEDTLS_RSA_ALT */ |
| 189 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 191 | * \brief This function initializes an RSA context. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | * |
Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 193 | * \note This function initializes the padding and the hash |
Ronald Cron | d2cfa3e | 2021-06-08 09:09:04 +0200 | [diff] [blame] | 194 | * identifier to respectively #MBEDTLS_RSA_PKCS_V15 and |
| 195 | * #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more |
| 196 | * information about those parameters. |
Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 197 | * |
| 198 | * \param ctx The RSA context to initialize. This must not be \c NULL. |
| 199 | */ |
| 200 | void mbedtls_rsa_init( mbedtls_rsa_context *ctx ); |
| 201 | |
| 202 | /** |
| 203 | * \brief This function sets padding for an already initialized RSA |
| 204 | * context. |
| 205 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 206 | * \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 207 | * encryption scheme and the RSASSA-PSS signature scheme. |
| 208 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 209 | * \note The \p hash_id parameter is ignored when using |
| 210 | * #MBEDTLS_RSA_PKCS_V15 padding. |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 211 | * |
Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 212 | * \note The choice of padding mode is strictly enforced for private |
| 213 | * key operations, since there might be security concerns in |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 214 | * mixing padding modes. For public key operations it is |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 215 | * a default value, which can be overridden by calling specific |
Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 216 | * \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx |
| 217 | * functions. |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 218 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 219 | * \note The hash selected in \p hash_id is always used for OEAP |
| 220 | * encryption. For PSS signatures, it is always used for |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 221 | * making signatures, but can be overridden for verifying them. |
| 222 | * If set to #MBEDTLS_MD_NONE, it is always overridden. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 223 | * |
Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 224 | * \param ctx The initialized RSA context to be configured. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 225 | * \param padding The padding mode to use. This must be either |
| 226 | * #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21. |
Ronald Cron | d2cfa3e | 2021-06-08 09:09:04 +0200 | [diff] [blame] | 227 | * \param hash_id The hash identifier for PSS or OAEP, if \p padding is |
| 228 | * #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this |
| 229 | * function but may be not suitable for some operations. |
| 230 | * Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15. |
Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 231 | * |
| 232 | * \return \c 0 on success. |
| 233 | * \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure: |
| 234 | * \p padding or \p hash_id is invalid. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | */ |
Ronald Cron | c1905a1 | 2021-06-05 11:11:14 +0200 | [diff] [blame] | 236 | int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, |
| 237 | mbedtls_md_type_t hash_id ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | |
| 239 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 240 | * \brief This function imports a set of core parameters into an |
| 241 | * RSA context. |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 242 | * |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 243 | * \note This function can be called multiple times for successive |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 244 | * imports, if the parameters are not simultaneously present. |
| 245 | * |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 246 | * Any sequence of calls to this function should be followed |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 247 | * by a call to mbedtls_rsa_complete(), which checks and |
| 248 | * completes the provided information to a ready-for-use |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 249 | * public or private RSA key. |
| 250 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 251 | * \note See mbedtls_rsa_complete() for more information on which |
| 252 | * parameters are necessary to set up a private or public |
| 253 | * RSA key. |
Hanno Becker | 3319555 | 2017-10-25 17:04:10 +0100 | [diff] [blame] | 254 | * |
Hanno Becker | 5178dca | 2017-10-03 14:29:37 +0100 | [diff] [blame] | 255 | * \note The imported parameters are copied and need not be preserved |
| 256 | * for the lifetime of the RSA context being set up. |
| 257 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 258 | * \param ctx The initialized RSA context to store the parameters in. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 259 | * \param N The RSA modulus. This may be \c NULL. |
| 260 | * \param P The first prime factor of \p N. This may be \c NULL. |
| 261 | * \param Q The second prime factor of \p N. This may be \c NULL. |
| 262 | * \param D The private exponent. This may be \c NULL. |
| 263 | * \param E The public exponent. This may be \c NULL. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 264 | * |
| 265 | * \return \c 0 on success. |
| 266 | * \return A non-zero error code on failure. |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 267 | */ |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 268 | int mbedtls_rsa_import( mbedtls_rsa_context *ctx, |
| 269 | const mbedtls_mpi *N, |
| 270 | const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 271 | const mbedtls_mpi *D, const mbedtls_mpi *E ); |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 272 | |
| 273 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 274 | * \brief This function imports core RSA parameters, in raw big-endian |
| 275 | * binary format, into an RSA context. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 276 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 277 | * \note This function can be called multiple times for successive |
| 278 | * imports, if the parameters are not simultaneously present. |
| 279 | * |
| 280 | * Any sequence of calls to this function should be followed |
| 281 | * by a call to mbedtls_rsa_complete(), which checks and |
| 282 | * completes the provided information to a ready-for-use |
| 283 | * public or private RSA key. |
| 284 | * |
| 285 | * \note See mbedtls_rsa_complete() for more information on which |
| 286 | * parameters are necessary to set up a private or public |
| 287 | * RSA key. |
| 288 | * |
| 289 | * \note The imported parameters are copied and need not be preserved |
| 290 | * for the lifetime of the RSA context being set up. |
| 291 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 292 | * \param ctx The initialized RSA context to store the parameters in. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 293 | * \param N The RSA modulus. This may be \c NULL. |
| 294 | * \param N_len The Byte length of \p N; it is ignored if \p N == NULL. |
| 295 | * \param P The first prime factor of \p N. This may be \c NULL. |
| 296 | * \param P_len The Byte length of \p P; it ns ignored if \p P == NULL. |
| 297 | * \param Q The second prime factor of \p N. This may be \c NULL. |
| 298 | * \param Q_len The Byte length of \p Q; it is ignored if \p Q == NULL. |
| 299 | * \param D The private exponent. This may be \c NULL. |
| 300 | * \param D_len The Byte length of \p D; it is ignored if \p D == NULL. |
| 301 | * \param E The public exponent. This may be \c NULL. |
| 302 | * \param E_len The Byte length of \p E; it is ignored if \p E == NULL. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 303 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 304 | * \return \c 0 on success. |
| 305 | * \return A non-zero error code on failure. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 306 | */ |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 307 | int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx, |
Hanno Becker | 7471631 | 2017-10-02 10:00:37 +0100 | [diff] [blame] | 308 | unsigned char const *N, size_t N_len, |
| 309 | unsigned char const *P, size_t P_len, |
| 310 | unsigned char const *Q, size_t Q_len, |
| 311 | unsigned char const *D, size_t D_len, |
| 312 | unsigned char const *E, size_t E_len ); |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 313 | |
| 314 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 315 | * \brief This function completes an RSA context from |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 316 | * a set of imported core parameters. |
| 317 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 318 | * To setup an RSA public key, precisely \p N and \p E |
| 319 | * must have been imported. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 320 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 321 | * To setup an RSA private key, sufficient information must |
| 322 | * be present for the other parameters to be derivable. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 323 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 324 | * The default implementation supports the following: |
| 325 | * <ul><li>Derive \p P, \p Q from \p N, \p D, \p E.</li> |
| 326 | * <li>Derive \p N, \p D from \p P, \p Q, \p E.</li></ul> |
| 327 | * Alternative implementations need not support these. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 328 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 329 | * If this function runs successfully, it guarantees that |
| 330 | * the RSA context can be used for RSA operations without |
| 331 | * the risk of failure or crash. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 332 | * |
Hanno Becker | 1e801f5 | 2017-10-10 16:44:47 +0100 | [diff] [blame] | 333 | * \warning This function need not perform consistency checks |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 334 | * for the imported parameters. In particular, parameters that |
| 335 | * are not needed by the implementation might be silently |
| 336 | * discarded and left unchecked. To check the consistency |
| 337 | * of the key material, see mbedtls_rsa_check_privkey(). |
Hanno Becker | 43a08d0 | 2017-10-02 13:16:35 +0100 | [diff] [blame] | 338 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 339 | * \param ctx The initialized RSA context holding imported parameters. |
| 340 | * |
| 341 | * \return \c 0 on success. |
| 342 | * \return #MBEDTLS_ERR_RSA_BAD_INPUT_DATA if the attempted derivations |
| 343 | * failed. |
| 344 | * |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 345 | */ |
Hanno Becker | f9e184b | 2017-10-10 16:49:26 +0100 | [diff] [blame] | 346 | int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ); |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 347 | |
| 348 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 349 | * \brief This function exports the core parameters of an RSA key. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 350 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 351 | * If this function runs successfully, the non-NULL buffers |
| 352 | * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully |
| 353 | * written, with additional unused space filled leading by |
| 354 | * zero Bytes. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 355 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 356 | * Possible reasons for returning |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 357 | * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul> |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 358 | * <li>An alternative RSA implementation is in use, which |
| 359 | * stores the key externally, and either cannot or should |
| 360 | * not export it into RAM.</li> |
| 361 | * <li>A SW or HW implementation might not support a certain |
| 362 | * deduction. For example, \p P, \p Q from \p N, \p D, |
| 363 | * and \p E if the former are not part of the |
| 364 | * implementation.</li></ul> |
Hanno Becker | 91c194d | 2017-09-29 12:50:12 +0100 | [diff] [blame] | 365 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 366 | * If the function fails due to an unsupported operation, |
| 367 | * the RSA context stays intact and remains usable. |
| 368 | * |
| 369 | * \param ctx The initialized RSA context. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 370 | * \param N The MPI to hold the RSA modulus. |
| 371 | * This may be \c NULL if this field need not be exported. |
| 372 | * \param P The MPI to hold the first prime factor of \p N. |
| 373 | * This may be \c NULL if this field need not be exported. |
| 374 | * \param Q The MPI to hold the second prime factor of \p N. |
| 375 | * This may be \c NULL if this field need not be exported. |
| 376 | * \param D The MPI to hold the private exponent. |
| 377 | * This may be \c NULL if this field need not be exported. |
| 378 | * \param E The MPI to hold the public exponent. |
| 379 | * This may be \c NULL if this field need not be exported. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 380 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 381 | * \return \c 0 on success. |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 382 | * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 383 | * requested parameters cannot be done due to missing |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 384 | * functionality or because of security policies. |
| 385 | * \return A non-zero return code on any other failure. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 386 | * |
| 387 | */ |
| 388 | int mbedtls_rsa_export( const mbedtls_rsa_context *ctx, |
| 389 | mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, |
| 390 | mbedtls_mpi *D, mbedtls_mpi *E ); |
| 391 | |
| 392 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 393 | * \brief This function exports core parameters of an RSA key |
| 394 | * in raw big-endian binary format. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 395 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 396 | * If this function runs successfully, the non-NULL buffers |
| 397 | * pointed to by \p N, \p P, \p Q, \p D, and \p E are fully |
| 398 | * written, with additional unused space filled leading by |
| 399 | * zero Bytes. |
| 400 | * |
| 401 | * Possible reasons for returning |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 402 | * #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:<ul> |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 403 | * <li>An alternative RSA implementation is in use, which |
| 404 | * stores the key externally, and either cannot or should |
| 405 | * not export it into RAM.</li> |
| 406 | * <li>A SW or HW implementation might not support a certain |
| 407 | * deduction. For example, \p P, \p Q from \p N, \p D, |
| 408 | * and \p E if the former are not part of the |
| 409 | * implementation.</li></ul> |
| 410 | * If the function fails due to an unsupported operation, |
| 411 | * the RSA context stays intact and remains usable. |
| 412 | * |
Rose Zadik | f2ec288 | 2018-04-17 10:27:25 +0100 | [diff] [blame] | 413 | * \note The length parameters are ignored if the corresponding |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 414 | * buffer pointers are NULL. |
| 415 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 416 | * \param ctx The initialized RSA context. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 417 | * \param N The Byte array to store the RSA modulus, |
| 418 | * or \c NULL if this field need not be exported. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 419 | * \param N_len The size of the buffer for the modulus. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 420 | * \param P The Byte array to hold the first prime factor of \p N, |
| 421 | * or \c NULL if this field need not be exported. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 422 | * \param P_len The size of the buffer for the first prime factor. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 423 | * \param Q The Byte array to hold the second prime factor of \p N, |
| 424 | * or \c NULL if this field need not be exported. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 425 | * \param Q_len The size of the buffer for the second prime factor. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 426 | * \param D The Byte array to hold the private exponent, |
| 427 | * or \c NULL if this field need not be exported. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 428 | * \param D_len The size of the buffer for the private exponent. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 429 | * \param E The Byte array to hold the public exponent, |
| 430 | * or \c NULL if this field need not be exported. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 431 | * \param E_len The size of the buffer for the public exponent. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 432 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 433 | * \return \c 0 on success. |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 434 | * \return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 435 | * requested parameters cannot be done due to missing |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 436 | * functionality or because of security policies. |
| 437 | * \return A non-zero return code on any other failure. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 438 | */ |
| 439 | int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx, |
| 440 | unsigned char *N, size_t N_len, |
| 441 | unsigned char *P, size_t P_len, |
| 442 | unsigned char *Q, size_t Q_len, |
| 443 | unsigned char *D, size_t D_len, |
| 444 | unsigned char *E, size_t E_len ); |
| 445 | |
| 446 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 447 | * \brief This function exports CRT parameters of a private RSA key. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 448 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 449 | * \note Alternative RSA implementations not using CRT-parameters |
| 450 | * internally can implement this function based on |
| 451 | * mbedtls_rsa_deduce_opt(). |
| 452 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 453 | * \param ctx The initialized RSA context. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 454 | * \param DP The MPI to hold \c D modulo `P-1`, |
| 455 | * or \c NULL if it need not be exported. |
| 456 | * \param DQ The MPI to hold \c D modulo `Q-1`, |
| 457 | * or \c NULL if it need not be exported. |
| 458 | * \param QP The MPI to hold modular inverse of \c Q modulo \c P, |
| 459 | * or \c NULL if it need not be exported. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 460 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 461 | * \return \c 0 on success. |
| 462 | * \return A non-zero error code on failure. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 463 | * |
| 464 | */ |
| 465 | int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx, |
| 466 | mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP ); |
| 467 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 468 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 469 | * \brief This function retrieves the length of RSA modulus in Bytes. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 470 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 471 | * \param ctx The initialized RSA context. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 472 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 473 | * \return The length of the RSA modulus in Bytes. |
Hanno Becker | cbb59bc | 2017-08-23 14:11:08 +0100 | [diff] [blame] | 474 | * |
| 475 | */ |
| 476 | size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx ); |
| 477 | |
| 478 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 479 | * \brief This function generates an RSA keypair. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 480 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 481 | * \note mbedtls_rsa_init() must be called before this function, |
| 482 | * to set up the RSA context. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 483 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 484 | * \param ctx The initialized RSA context used to hold the key. |
| 485 | * \param f_rng The RNG function to be used for key generation. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 486 | * This is mandatory and must not be \c NULL. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 487 | * \param p_rng The RNG context to be passed to \p f_rng. |
| 488 | * This may be \c NULL if \p f_rng doesn't need a context. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 489 | * \param nbits The size of the public key in bits. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 490 | * \param exponent The public exponent to use. For example, \c 65537. |
Hanno Becker | f66f294 | 2018-12-18 13:30:08 +0000 | [diff] [blame] | 491 | * This must be odd and greater than \c 1. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 492 | * |
| 493 | * \return \c 0 on success. |
| 494 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 495 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 496 | int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 497 | int (*f_rng)(void *, unsigned char *, size_t), |
| 498 | void *p_rng, |
| 499 | unsigned int nbits, int exponent ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 500 | |
| 501 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 502 | * \brief This function checks if a context contains at least an RSA |
| 503 | * public key. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 504 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 505 | * If the function runs successfully, it is guaranteed that |
| 506 | * enough information is present to perform an RSA public key |
| 507 | * operation using mbedtls_rsa_public(). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 508 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 509 | * \param ctx The initialized RSA context to check. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 510 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 511 | * \return \c 0 on success. |
| 512 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Hanno Becker | 43a08d0 | 2017-10-02 13:16:35 +0100 | [diff] [blame] | 513 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 514 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 515 | int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 516 | |
| 517 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 518 | * \brief This function checks if a context contains an RSA private key |
Hanno Becker | 1e801f5 | 2017-10-10 16:44:47 +0100 | [diff] [blame] | 519 | * and perform basic consistency checks. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 520 | * |
Hanno Becker | 68767a6 | 2017-10-17 10:13:31 +0100 | [diff] [blame] | 521 | * \note The consistency checks performed by this function not only |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 522 | * ensure that mbedtls_rsa_private() can be called successfully |
Hanno Becker | 68767a6 | 2017-10-17 10:13:31 +0100 | [diff] [blame] | 523 | * on the given context, but that the various parameters are |
| 524 | * mutually consistent with high probability, in the sense that |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 525 | * mbedtls_rsa_public() and mbedtls_rsa_private() are inverses. |
Hanno Becker | 1e801f5 | 2017-10-10 16:44:47 +0100 | [diff] [blame] | 526 | * |
| 527 | * \warning This function should catch accidental misconfigurations |
| 528 | * like swapping of parameters, but it cannot establish full |
| 529 | * trust in neither the quality nor the consistency of the key |
| 530 | * material that was used to setup the given RSA context: |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 531 | * <ul><li>Consistency: Imported parameters that are irrelevant |
| 532 | * for the implementation might be silently dropped. If dropped, |
| 533 | * the current function does not have access to them, |
| 534 | * and therefore cannot check them. See mbedtls_rsa_complete(). |
| 535 | * If you want to check the consistency of the entire |
| 536 | * content of an PKCS1-encoded RSA private key, for example, you |
| 537 | * should use mbedtls_rsa_validate_params() before setting |
| 538 | * up the RSA context. |
| 539 | * Additionally, if the implementation performs empirical checks, |
| 540 | * these checks substantiate but do not guarantee consistency.</li> |
| 541 | * <li>Quality: This function is not expected to perform |
| 542 | * extended quality assessments like checking that the prime |
| 543 | * factors are safe. Additionally, it is the responsibility of the |
| 544 | * user to ensure the trustworthiness of the source of his RSA |
| 545 | * parameters, which goes beyond what is effectively checkable |
| 546 | * by the library.</li></ul> |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 547 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 548 | * \param ctx The initialized RSA context to check. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 549 | * |
| 550 | * \return \c 0 on success. |
| 551 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 552 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 554 | |
| 555 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 556 | * \brief This function checks a public-private RSA key pair. |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 557 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 558 | * It checks each of the contexts, and makes sure they match. |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 559 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 560 | * \param pub The initialized RSA context holding the public key. |
| 561 | * \param prv The initialized RSA context holding the private key. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 562 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 563 | * \return \c 0 on success. |
| 564 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 565 | */ |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 566 | int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, |
| 567 | const mbedtls_rsa_context *prv ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 568 | |
| 569 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 570 | * \brief This function performs an RSA public key operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 571 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 572 | * \param ctx The initialized RSA context to use. |
| 573 | * \param input The input buffer. This must be a readable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 574 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 575 | * for an 2048-bit RSA modulus. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 576 | * \param output The output buffer. This must be a writable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 577 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 578 | * for an 2048-bit RSA modulus. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 579 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 580 | * \note This function does not handle message padding. |
| 581 | * |
| 582 | * \note Make sure to set \p input[0] = 0 or ensure that |
| 583 | * input is smaller than \p N. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 584 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 585 | * \return \c 0 on success. |
| 586 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 587 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | int mbedtls_rsa_public( mbedtls_rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 589 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 590 | unsigned char *output ); |
| 591 | |
| 592 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 593 | * \brief This function performs an RSA private key operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 594 | * |
Hanno Becker | 2412061 | 2017-10-26 11:53:35 +0100 | [diff] [blame] | 595 | * \note Blinding is used if and only if a PRNG is provided. |
Hanno Becker | 88ec238 | 2017-05-03 13:51:16 +0100 | [diff] [blame] | 596 | * |
| 597 | * \note If blinding is used, both the base of exponentation |
Hanno Becker | 2412061 | 2017-10-26 11:53:35 +0100 | [diff] [blame] | 598 | * and the exponent are blinded, providing protection |
| 599 | * against some side-channel attacks. |
Hanno Becker | 88ec238 | 2017-05-03 13:51:16 +0100 | [diff] [blame] | 600 | * |
Hanno Becker | 4e1be39 | 2017-10-02 15:56:48 +0100 | [diff] [blame] | 601 | * \warning It is deprecated and a security risk to not provide |
| 602 | * a PRNG here and thereby prevent the use of blinding. |
| 603 | * Future versions of the library may enforce the presence |
| 604 | * of a PRNG. |
Hanno Becker | 88ec238 | 2017-05-03 13:51:16 +0100 | [diff] [blame] | 605 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 606 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 607 | * \param f_rng The RNG function, used for blinding. It is mandatory. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 608 | * \param p_rng The RNG context to pass to \p f_rng. This may be \c NULL |
Thomas Daubney | 0341278 | 2021-05-20 15:31:17 +0100 | [diff] [blame] | 609 | * if \p f_rng doesn't need a context. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 610 | * \param input The input buffer. This must be a readable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 611 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 612 | * for an 2048-bit RSA modulus. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 613 | * \param output The output buffer. This must be a writable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 614 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 615 | * for an 2048-bit RSA modulus. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 616 | * |
| 617 | * \return \c 0 on success. |
| 618 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
| 619 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 620 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | int mbedtls_rsa_private( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 622 | int (*f_rng)(void *, unsigned char *, size_t), |
| 623 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 624 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 625 | unsigned char *output ); |
| 626 | |
| 627 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 628 | * \brief This function adds the message padding, then performs an RSA |
| 629 | * operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 630 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 631 | * It is the generic wrapper for performing a PKCS#1 encryption |
Thomas Daubney | 2177277 | 2021-05-13 17:30:32 +0100 | [diff] [blame] | 632 | * operation. |
Rose Zadik | f2ec288 | 2018-04-17 10:27:25 +0100 | [diff] [blame] | 633 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 634 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | f54c5c5 | 2021-05-21 17:00:30 +0100 | [diff] [blame] | 635 | * \param f_rng The RNG to use. It is used for padding generation |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 636 | * and it is mandatory. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 637 | * \param p_rng The RNG context to be passed to \p f_rng. May be |
Thomas Daubney | 0341278 | 2021-05-20 15:31:17 +0100 | [diff] [blame] | 638 | * \c NULL if \p f_rng doesn't need a context argument. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 639 | * \param ilen The length of the plaintext in Bytes. |
| 640 | * \param input The input data to encrypt. This must be a readable |
Jaeden Amero | fb23673 | 2019-02-08 13:11:59 +0000 | [diff] [blame] | 641 | * buffer of size \p ilen Bytes. It may be \c NULL if |
| 642 | * `ilen == 0`. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 643 | * \param output The output buffer. This must be a writable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 644 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 645 | * for an 2048-bit RSA modulus. |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame] | 646 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 647 | * \return \c 0 on success. |
| 648 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 649 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 651 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 652 | void *p_rng, |
Thomas Daubney | 2177277 | 2021-05-13 17:30:32 +0100 | [diff] [blame] | 653 | size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 654 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 655 | unsigned char *output ); |
| 656 | |
| 657 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 658 | * \brief This function performs a PKCS#1 v1.5 encryption operation |
| 659 | * (RSAES-PKCS1-v1_5-ENCRYPT). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 660 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 661 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 662 | * \param f_rng The RNG function to use. It is mandatory and used for |
| 663 | * padding generation. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 664 | * \param p_rng The RNG context to be passed to \p f_rng. This may |
Thomas Daubney | 0341278 | 2021-05-20 15:31:17 +0100 | [diff] [blame] | 665 | * be \c NULL if \p f_rng doesn't need a context argument. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 666 | * \param ilen The length of the plaintext in Bytes. |
| 667 | * \param input The input data to encrypt. This must be a readable |
Jaeden Amero | fb23673 | 2019-02-08 13:11:59 +0000 | [diff] [blame] | 668 | * buffer of size \p ilen Bytes. It may be \c NULL if |
| 669 | * `ilen == 0`. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 670 | * \param output The output buffer. This must be a writable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 671 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 672 | * for an 2048-bit RSA modulus. |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame] | 673 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 674 | * \return \c 0 on success. |
| 675 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 676 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 678 | int (*f_rng)(void *, unsigned char *, size_t), |
| 679 | void *p_rng, |
Thomas Daubney | 53e4ac6 | 2021-05-13 18:26:49 +0100 | [diff] [blame] | 680 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 681 | const unsigned char *input, |
| 682 | unsigned char *output ); |
| 683 | |
| 684 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 685 | * \brief This function performs a PKCS#1 v2.1 OAEP encryption |
| 686 | * operation (RSAES-OAEP-ENCRYPT). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 687 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 688 | * \note The output buffer must be as large as the size |
| 689 | * of ctx->N. For example, 128 Bytes if RSA-1024 is used. |
| 690 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 691 | * \param ctx The initnialized RSA context to use. |
Hanno Becker | a9020f2 | 2018-12-18 14:45:45 +0000 | [diff] [blame] | 692 | * \param f_rng The RNG function to use. This is needed for padding |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 693 | * generation and is mandatory. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 694 | * \param p_rng The RNG context to be passed to \p f_rng. This may |
Hanno Becker | a9020f2 | 2018-12-18 14:45:45 +0000 | [diff] [blame] | 695 | * be \c NULL if \p f_rng doesn't need a context argument. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 696 | * \param label The buffer holding the custom label to use. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 697 | * This must be a readable buffer of length \p label_len |
| 698 | * Bytes. It may be \c NULL if \p label_len is \c 0. |
| 699 | * \param label_len The length of the label in Bytes. |
| 700 | * \param ilen The length of the plaintext buffer \p input in Bytes. |
| 701 | * \param input The input data to encrypt. This must be a readable |
Jaeden Amero | fb23673 | 2019-02-08 13:11:59 +0000 | [diff] [blame] | 702 | * buffer of size \p ilen Bytes. It may be \c NULL if |
| 703 | * `ilen == 0`. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 704 | * \param output The output buffer. This must be a writable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 705 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 706 | * for an 2048-bit RSA modulus. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 707 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 708 | * \return \c 0 on success. |
| 709 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 710 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 712 | int (*f_rng)(void *, unsigned char *, size_t), |
| 713 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 714 | const unsigned char *label, size_t label_len, |
| 715 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 716 | const unsigned char *input, |
| 717 | unsigned char *output ); |
| 718 | |
| 719 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 720 | * \brief This function performs an RSA operation, then removes the |
| 721 | * message padding. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 722 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 723 | * It is the generic wrapper for performing a PKCS#1 decryption |
Thomas Daubney | c7feaf3 | 2021-05-07 14:02:43 +0100 | [diff] [blame] | 724 | * operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 725 | * |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 726 | * \note The output buffer length \c output_max_len should be |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 727 | * as large as the size \p ctx->len of \p ctx->N (for example, |
| 728 | * 128 Bytes if RSA-1024 is used) to be able to hold an |
| 729 | * arbitrary decrypted message. If it is not large enough to |
| 730 | * hold the decryption of the particular ciphertext provided, |
| 731 | * the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 732 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 733 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 734 | * \param f_rng The RNG function. This is used for blinding and is |
| 735 | * mandatory; see mbedtls_rsa_private() for more. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 736 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
Thomas Daubney | 0341278 | 2021-05-20 15:31:17 +0100 | [diff] [blame] | 737 | * \c NULL if \p f_rng doesn't need a context. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 738 | * \param olen The address at which to store the length of |
| 739 | * the plaintext. This must not be \c NULL. |
| 740 | * \param input The ciphertext buffer. This must be a readable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 741 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 742 | * for an 2048-bit RSA modulus. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 743 | * \param output The buffer used to hold the plaintext. This must |
| 744 | * be a writable buffer of length \p output_max_len Bytes. |
Hanno Becker | f66f294 | 2018-12-18 13:30:08 +0000 | [diff] [blame] | 745 | * \param output_max_len The length in Bytes of the output buffer \p output. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 746 | * |
| 747 | * \return \c 0 on success. |
| 748 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 749 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 751 | int (*f_rng)(void *, unsigned char *, size_t), |
| 752 | void *p_rng, |
Thomas Daubney | c7feaf3 | 2021-05-07 14:02:43 +0100 | [diff] [blame] | 753 | size_t *olen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 754 | const unsigned char *input, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 755 | unsigned char *output, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 756 | size_t output_max_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 757 | |
| 758 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 759 | * \brief This function performs a PKCS#1 v1.5 decryption |
| 760 | * operation (RSAES-PKCS1-v1_5-DECRYPT). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 761 | * |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 762 | * \note The output buffer length \c output_max_len should be |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 763 | * as large as the size \p ctx->len of \p ctx->N, for example, |
| 764 | * 128 Bytes if RSA-1024 is used, to be able to hold an |
| 765 | * arbitrary decrypted message. If it is not large enough to |
| 766 | * hold the decryption of the particular ciphertext provided, |
| 767 | * the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
Hanno Becker | 248ae6d | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 768 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 769 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 770 | * \param f_rng The RNG function. This is used for blinding and is |
| 771 | * mandatory; see mbedtls_rsa_private() for more. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 772 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
Thomas Daubney | 0341278 | 2021-05-20 15:31:17 +0100 | [diff] [blame] | 773 | * \c NULL if \p f_rng doesn't need a context. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 774 | * \param olen The address at which to store the length of |
| 775 | * the plaintext. This must not be \c NULL. |
| 776 | * \param input The ciphertext buffer. This must be a readable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 777 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 778 | * for an 2048-bit RSA modulus. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 779 | * \param output The buffer used to hold the plaintext. This must |
| 780 | * be a writable buffer of length \p output_max_len Bytes. |
Hanno Becker | f66f294 | 2018-12-18 13:30:08 +0000 | [diff] [blame] | 781 | * \param output_max_len The length in Bytes of the output buffer \p output. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 782 | * |
| 783 | * \return \c 0 on success. |
| 784 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
| 785 | * |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 786 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 787 | int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 788 | int (*f_rng)(void *, unsigned char *, size_t), |
| 789 | void *p_rng, |
Thomas Daubney | 3473308 | 2021-05-12 09:24:29 +0100 | [diff] [blame] | 790 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 791 | const unsigned char *input, |
| 792 | unsigned char *output, |
| 793 | size_t output_max_len ); |
| 794 | |
| 795 | /** |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 796 | * \brief This function performs a PKCS#1 v2.1 OAEP decryption |
| 797 | * operation (RSAES-OAEP-DECRYPT). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 798 | * |
Rose Zadik | f2ec288 | 2018-04-17 10:27:25 +0100 | [diff] [blame] | 799 | * \note The output buffer length \c output_max_len should be |
| 800 | * as large as the size \p ctx->len of \p ctx->N, for |
| 801 | * example, 128 Bytes if RSA-1024 is used, to be able to |
| 802 | * hold an arbitrary decrypted message. If it is not |
| 803 | * large enough to hold the decryption of the particular |
| 804 | * ciphertext provided, the function returns |
| 805 | * #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 806 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 807 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 808 | * \param f_rng The RNG function. This is used for blinding and is |
| 809 | * mandatory. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 810 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
Thomas Daubney | 0341278 | 2021-05-20 15:31:17 +0100 | [diff] [blame] | 811 | * \c NULL if \p f_rng doesn't need a context. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 812 | * \param label The buffer holding the custom label to use. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 813 | * This must be a readable buffer of length \p label_len |
| 814 | * Bytes. It may be \c NULL if \p label_len is \c 0. |
| 815 | * \param label_len The length of the label in Bytes. |
| 816 | * \param olen The address at which to store the length of |
| 817 | * the plaintext. This must not be \c NULL. |
| 818 | * \param input The ciphertext buffer. This must be a readable buffer |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 819 | * of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 820 | * for an 2048-bit RSA modulus. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 821 | * \param output The buffer used to hold the plaintext. This must |
| 822 | * be a writable buffer of length \p output_max_len Bytes. |
Hanno Becker | f66f294 | 2018-12-18 13:30:08 +0000 | [diff] [blame] | 823 | * \param output_max_len The length in Bytes of the output buffer \p output. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 824 | * |
| 825 | * \return \c 0 on success. |
| 826 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 827 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 828 | int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 829 | int (*f_rng)(void *, unsigned char *, size_t), |
| 830 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 831 | const unsigned char *label, size_t label_len, |
| 832 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 833 | const unsigned char *input, |
| 834 | unsigned char *output, |
| 835 | size_t output_max_len ); |
| 836 | |
| 837 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 838 | * \brief This function performs a private RSA operation to sign |
| 839 | * a message digest using PKCS#1. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 840 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 841 | * It is the generic wrapper for performing a PKCS#1 |
Thomas Daubney | 140184d | 2021-05-18 16:04:07 +0100 | [diff] [blame] | 842 | * signature. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 843 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 844 | * \note The \p sig buffer must be as large as the size |
| 845 | * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 846 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 847 | * \note For PKCS#1 v2.1 encoding, see comments on |
| 848 | * mbedtls_rsa_rsassa_pss_sign() for details on |
| 849 | * \p md_alg and \p hash_id. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 850 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 851 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 852 | * \param f_rng The RNG function to use. This is mandatory and |
| 853 | * must not be \c NULL. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 854 | * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL |
Thomas Daubney | 0341278 | 2021-05-20 15:31:17 +0100 | [diff] [blame] | 855 | * if \p f_rng doesn't need a context argument. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 856 | * \param md_alg The message-digest algorithm used to hash the original data. |
| 857 | * Use #MBEDTLS_MD_NONE for signing raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 858 | * \param hashlen The length of the message digest or raw data in Bytes. |
| 859 | * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the |
| 860 | * output length of the corresponding hash algorithm. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 861 | * \param hash The buffer holding the message digest or raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 862 | * This must be a readable buffer of at least \p hashlen Bytes. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 863 | * \param sig The buffer to hold the signature. This must be a writable |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 864 | * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes |
Gilles Peskine | 73a1f37 | 2019-11-08 18:39:22 +0100 | [diff] [blame] | 865 | * for an 2048-bit RSA modulus. A buffer length of |
| 866 | * #MBEDTLS_MPI_MAX_SIZE is always safe. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 867 | * |
| 868 | * \return \c 0 if the signing operation was successful. |
| 869 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 870 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 871 | int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 872 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 873 | void *p_rng, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 874 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 875 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 876 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 877 | unsigned char *sig ); |
| 878 | |
| 879 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 880 | * \brief This function performs a PKCS#1 v1.5 signature |
| 881 | * operation (RSASSA-PKCS1-v1_5-SIGN). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 882 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 883 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 884 | * \param f_rng The RNG function. This is used for blinding and is |
| 885 | * mandatory; see mbedtls_rsa_private() for more. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 886 | * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 887 | * if \p f_rng doesn't need a context argument. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 888 | * \param md_alg The message-digest algorithm used to hash the original data. |
| 889 | * Use #MBEDTLS_MD_NONE for signing raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 890 | * \param hashlen The length of the message digest or raw data in Bytes. |
| 891 | * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the |
| 892 | * output length of the corresponding hash algorithm. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 893 | * \param hash The buffer holding the message digest or raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 894 | * This must be a readable buffer of at least \p hashlen Bytes. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 895 | * \param sig The buffer to hold the signature. This must be a writable |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 896 | * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes |
Gilles Peskine | 73a1f37 | 2019-11-08 18:39:22 +0100 | [diff] [blame] | 897 | * for an 2048-bit RSA modulus. A buffer length of |
| 898 | * #MBEDTLS_MPI_MAX_SIZE is always safe. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 899 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 900 | * \return \c 0 if the signing operation was successful. |
| 901 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 902 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 903 | int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 904 | int (*f_rng)(void *, unsigned char *, size_t), |
| 905 | void *p_rng, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 906 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 907 | unsigned int hashlen, |
| 908 | const unsigned char *hash, |
| 909 | unsigned char *sig ); |
| 910 | |
| 911 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 912 | * \brief This function performs a PKCS#1 v2.1 PSS signature |
| 913 | * operation (RSASSA-PSS-SIGN). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 914 | * |
Janos Follath | b795332 | 2021-04-01 14:44:17 +0100 | [diff] [blame] | 915 | * \note The \c hash_id set in \p ctx by calling |
| 916 | * mbedtls_rsa_set_padding() selects the hash used for the |
| 917 | * encoding operation and for the mask generation function |
| 918 | * (MGF1). For more details on the encoding operation and the |
| 919 | * mask generation function, consult <em>RFC-3447: Public-Key |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 920 | * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography |
Janos Follath | b795332 | 2021-04-01 14:44:17 +0100 | [diff] [blame] | 921 | * Specifications</em>. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 922 | * |
Cédric Meuter | 010ddc2 | 2020-04-25 09:24:11 +0200 | [diff] [blame] | 923 | * \note This function enforces that the provided salt length complies |
| 924 | * with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 v2.2) §9.1.1 |
| 925 | * step 3. The constraint is that the hash length plus the salt |
| 926 | * length plus 2 bytes must be at most the key length. If this |
| 927 | * constraint is not met, this function returns |
Jaeden Amero | 3725bb2 | 2018-09-07 19:12:36 +0100 | [diff] [blame] | 928 | * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA. |
| 929 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 930 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 931 | * \param f_rng The RNG function. It is mandatory and must not be \c NULL. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 932 | * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL |
| 933 | * if \p f_rng doesn't need a context argument. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 934 | * \param md_alg The message-digest algorithm used to hash the original data. |
| 935 | * Use #MBEDTLS_MD_NONE for signing raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 936 | * \param hashlen The length of the message digest or raw data in Bytes. |
| 937 | * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the |
| 938 | * output length of the corresponding hash algorithm. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 939 | * \param hash The buffer holding the message digest or raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 940 | * This must be a readable buffer of at least \p hashlen Bytes. |
Cedric Meuter | 8aa4d75 | 2020-04-21 12:49:11 +0200 | [diff] [blame] | 941 | * \param saltlen The length of the salt that should be used. |
Cédric Meuter | 010ddc2 | 2020-04-25 09:24:11 +0200 | [diff] [blame] | 942 | * If passed #MBEDTLS_RSA_SALT_LEN_ANY, the function will use |
| 943 | * the largest possible salt length up to the hash length, |
| 944 | * which is the largest permitted by some standards including |
| 945 | * FIPS 186-4 §5.5. |
Cedric Meuter | 8aa4d75 | 2020-04-21 12:49:11 +0200 | [diff] [blame] | 946 | * \param sig The buffer to hold the signature. This must be a writable |
| 947 | * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 948 | * for an 2048-bit RSA modulus. A buffer length of |
| 949 | * #MBEDTLS_MPI_MAX_SIZE is always safe. |
| 950 | * |
| 951 | * \return \c 0 if the signing operation was successful. |
| 952 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
| 953 | */ |
| 954 | int mbedtls_rsa_rsassa_pss_sign_ext( mbedtls_rsa_context *ctx, |
| 955 | int (*f_rng)(void *, unsigned char *, size_t), |
| 956 | void *p_rng, |
Cedric Meuter | 8aa4d75 | 2020-04-21 12:49:11 +0200 | [diff] [blame] | 957 | mbedtls_md_type_t md_alg, |
| 958 | unsigned int hashlen, |
| 959 | const unsigned char *hash, |
| 960 | int saltlen, |
| 961 | unsigned char *sig ); |
| 962 | |
| 963 | /** |
| 964 | * \brief This function performs a PKCS#1 v2.1 PSS signature |
| 965 | * operation (RSASSA-PSS-SIGN). |
| 966 | * |
Janos Follath | b795332 | 2021-04-01 14:44:17 +0100 | [diff] [blame] | 967 | * \note The \c hash_id set in \p ctx by calling |
| 968 | * mbedtls_rsa_set_padding() selects the hash used for the |
| 969 | * encoding operation and for the mask generation function |
| 970 | * (MGF1). For more details on the encoding operation and the |
| 971 | * mask generation function, consult <em>RFC-3447: Public-Key |
Cedric Meuter | 8aa4d75 | 2020-04-21 12:49:11 +0200 | [diff] [blame] | 972 | * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography |
Janos Follath | b795332 | 2021-04-01 14:44:17 +0100 | [diff] [blame] | 973 | * Specifications</em>. |
Cedric Meuter | 8aa4d75 | 2020-04-21 12:49:11 +0200 | [diff] [blame] | 974 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 975 | * \note This function always uses the maximum possible salt size, |
| 976 | * up to the length of the payload hash. This choice of salt |
| 977 | * size complies with FIPS 186-4 §5.5 (e) and RFC 8017 (PKCS#1 |
| 978 | * v2.2) §9.1.1 step 3. Furthermore this function enforces a |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 979 | * minimum salt size which is the hash size minus 2 bytes. If |
| 980 | * this minimum size is too large given the key size (the salt |
| 981 | * size, plus the hash size, plus 2 bytes must be no more than |
| 982 | * the key size in bytes), this function returns |
| 983 | * #MBEDTLS_ERR_RSA_BAD_INPUT_DATA. |
| 984 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 985 | * \param ctx The initialized RSA context to use. |
Thomas Daubney | 2c65db9 | 2021-05-21 10:58:28 +0100 | [diff] [blame] | 986 | * \param f_rng The RNG function. It is mandatory and must not be \c NULL. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 987 | * \param p_rng The RNG context to be passed to \p f_rng. This may be \c NULL |
| 988 | * if \p f_rng doesn't need a context argument. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 989 | * \param md_alg The message-digest algorithm used to hash the original data. |
| 990 | * Use #MBEDTLS_MD_NONE for signing raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 991 | * \param hashlen The length of the message digest or raw data in Bytes. |
| 992 | * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the |
| 993 | * output length of the corresponding hash algorithm. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 994 | * \param hash The buffer holding the message digest or raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 995 | * This must be a readable buffer of at least \p hashlen Bytes. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 996 | * \param sig The buffer to hold the signature. This must be a writable |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 997 | * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes |
Gilles Peskine | 73a1f37 | 2019-11-08 18:39:22 +0100 | [diff] [blame] | 998 | * for an 2048-bit RSA modulus. A buffer length of |
| 999 | * #MBEDTLS_MPI_MAX_SIZE is always safe. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1000 | * |
| 1001 | * \return \c 0 if the signing operation was successful. |
| 1002 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1003 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1004 | int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1005 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1006 | void *p_rng, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1007 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1008 | unsigned int hashlen, |
| 1009 | const unsigned char *hash, |
| 1010 | unsigned char *sig ); |
| 1011 | |
| 1012 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1013 | * \brief This function performs a public RSA operation and checks |
| 1014 | * the message digest. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1015 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1016 | * This is the generic wrapper for performing a PKCS#1 |
Thomas Daubney | 68d9cbc | 2021-05-18 18:45:09 +0100 | [diff] [blame] | 1017 | * verification. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1018 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1019 | * \note For PKCS#1 v2.1 encoding, see comments on |
| 1020 | * mbedtls_rsa_rsassa_pss_verify() about \p md_alg and |
| 1021 | * \p hash_id. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1022 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1023 | * \param ctx The initialized RSA public key context to use. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1024 | * \param md_alg The message-digest algorithm used to hash the original data. |
| 1025 | * Use #MBEDTLS_MD_NONE for signing raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 1026 | * \param hashlen The length of the message digest or raw data in Bytes. |
| 1027 | * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the |
| 1028 | * output length of the corresponding hash algorithm. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1029 | * \param hash The buffer holding the message digest or raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 1030 | * This must be a readable buffer of at least \p hashlen Bytes. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1031 | * \param sig The buffer holding the signature. This must be a readable |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 1032 | * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 1033 | * for an 2048-bit RSA modulus. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1034 | * |
| 1035 | * \return \c 0 if the verify operation was successful. |
| 1036 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1037 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1038 | int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1039 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1040 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1041 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 1042 | const unsigned char *sig ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1043 | |
| 1044 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1045 | * \brief This function performs a PKCS#1 v1.5 verification |
| 1046 | * operation (RSASSA-PKCS1-v1_5-VERIFY). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1047 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1048 | * \param ctx The initialized RSA public key context to use. |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1049 | * \param md_alg The message-digest algorithm used to hash the original data. |
| 1050 | * Use #MBEDTLS_MD_NONE for signing raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 1051 | * \param hashlen The length of the message digest or raw data in Bytes. |
| 1052 | * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the |
| 1053 | * output length of the corresponding hash algorithm. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1054 | * \param hash The buffer holding the message digest or raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 1055 | * This must be a readable buffer of at least \p hashlen Bytes. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1056 | * \param sig The buffer holding the signature. This must be a readable |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 1057 | * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 1058 | * for an 2048-bit RSA modulus. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1059 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1060 | * \return \c 0 if the verify operation was successful. |
| 1061 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1062 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1063 | int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1064 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1065 | unsigned int hashlen, |
| 1066 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 1067 | const unsigned char *sig ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1068 | |
| 1069 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1070 | * \brief This function performs a PKCS#1 v2.1 PSS verification |
| 1071 | * operation (RSASSA-PSS-VERIFY). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1072 | * |
Janos Follath | b795332 | 2021-04-01 14:44:17 +0100 | [diff] [blame] | 1073 | * \note The \c hash_id set in \p ctx by calling |
| 1074 | * mbedtls_rsa_set_padding() selects the hash used for the |
| 1075 | * encoding operation and for the mask generation function |
| 1076 | * (MGF1). For more details on the encoding operation and the |
| 1077 | * mask generation function, consult <em>RFC-3447: Public-Key |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1078 | * Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography |
Janos Follath | b795332 | 2021-04-01 14:44:17 +0100 | [diff] [blame] | 1079 | * Specifications</em>. If the \c hash_id set in \p ctx by |
| 1080 | * mbedtls_rsa_set_padding() is #MBEDTLS_MD_NONE, the \p md_alg |
| 1081 | * parameter is used. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1082 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1083 | * \param ctx The initialized RSA public key context to use. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1084 | * \param md_alg The message-digest algorithm used to hash the original data. |
| 1085 | * Use #MBEDTLS_MD_NONE for signing raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 1086 | * \param hashlen The length of the message digest or raw data in Bytes. |
| 1087 | * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the |
| 1088 | * output length of the corresponding hash algorithm. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1089 | * \param hash The buffer holding the message digest or raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 1090 | * This must be a readable buffer of at least \p hashlen Bytes. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1091 | * \param sig The buffer holding the signature. This must be a readable |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 1092 | * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 1093 | * for an 2048-bit RSA modulus. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1094 | * |
| 1095 | * \return \c 0 if the verify operation was successful. |
| 1096 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1097 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1099 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1100 | unsigned int hashlen, |
| 1101 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 1102 | const unsigned char *sig ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1103 | |
| 1104 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1105 | * \brief This function performs a PKCS#1 v2.1 PSS verification |
| 1106 | * operation (RSASSA-PSS-VERIFY). |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1107 | * |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1108 | * \note The \p sig buffer must be as large as the size |
| 1109 | * of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1110 | * |
Janos Follath | b795332 | 2021-04-01 14:44:17 +0100 | [diff] [blame] | 1111 | * \note The \c hash_id set in \p ctx by mbedtls_rsa_set_padding() is |
| 1112 | * ignored. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1113 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1114 | * \param ctx The initialized RSA public key context to use. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1115 | * \param md_alg The message-digest algorithm used to hash the original data. |
| 1116 | * Use #MBEDTLS_MD_NONE for signing raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 1117 | * \param hashlen The length of the message digest or raw data in Bytes. |
| 1118 | * If \p md_alg is not #MBEDTLS_MD_NONE, this must match the |
| 1119 | * output length of the corresponding hash algorithm. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1120 | * \param hash The buffer holding the message digest or raw data. |
Manuel Pégourié-Gonnard | e7885e5 | 2021-06-22 12:29:27 +0200 | [diff] [blame] | 1121 | * This must be a readable buffer of at least \p hashlen Bytes. |
Janos Follath | b795332 | 2021-04-01 14:44:17 +0100 | [diff] [blame] | 1122 | * \param mgf1_hash_id The message digest algorithm used for the |
| 1123 | * verification operation and the mask generation |
| 1124 | * function (MGF1). For more details on the encoding |
| 1125 | * operation and the mask generation function, consult |
| 1126 | * <em>RFC-3447: Public-Key Cryptography Standards |
| 1127 | * (PKCS) #1 v2.1: RSA Cryptography |
| 1128 | * Specifications</em>. |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1129 | * \param expected_salt_len The length of the salt used in padding. Use |
| 1130 | * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length. |
| 1131 | * \param sig The buffer holding the signature. This must be a readable |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 1132 | * buffer of length \c ctx->len Bytes. For example, \c 256 Bytes |
| 1133 | * for an 2048-bit RSA modulus. |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1134 | * |
| 1135 | * \return \c 0 if the verify operation was successful. |
| 1136 | * \return An \c MBEDTLS_ERR_RSA_XXX error code on failure. |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1137 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1140 | unsigned int hashlen, |
| 1141 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1142 | mbedtls_md_type_t mgf1_hash_id, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1143 | int expected_salt_len, |
| 1144 | const unsigned char *sig ); |
| 1145 | |
| 1146 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1147 | * \brief This function copies the components of an RSA context. |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1148 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1149 | * \param dst The destination context. This must be initialized. |
| 1150 | * \param src The source context. This must be initialized. |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1151 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1152 | * \return \c 0 on success. |
| 1153 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure. |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1154 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1155 | int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1156 | |
| 1157 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1158 | * \brief This function frees the components of an RSA key. |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 1159 | * |
Hanno Becker | 9a46777 | 2018-12-13 09:54:59 +0000 | [diff] [blame] | 1160 | * \param ctx The RSA context to free. May be \c NULL, in which case |
| 1161 | * this function is a no-op. If it is not \c NULL, it must |
Hanno Becker | 385ce91 | 2018-12-13 18:33:12 +0000 | [diff] [blame] | 1162 | * point to an initialized RSA context. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1163 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1164 | void mbedtls_rsa_free( mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1165 | |
Ron Eldor | fa8f635 | 2017-06-20 15:48:46 +0300 | [diff] [blame] | 1166 | #if defined(MBEDTLS_SELF_TEST) |
| 1167 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1168 | /** |
Rose Zadik | 042e97f | 2018-01-26 16:35:10 +0000 | [diff] [blame] | 1169 | * \brief The RSA checkup routine. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | * |
Rose Zadik | e8b5b99 | 2018-03-27 12:19:47 +0100 | [diff] [blame] | 1171 | * \return \c 0 on success. |
| 1172 | * \return \c 1 on failure. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1173 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1174 | int mbedtls_rsa_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1175 | |
Ron Eldor | fa8f635 | 2017-06-20 15:48:46 +0300 | [diff] [blame] | 1176 | #endif /* MBEDTLS_SELF_TEST */ |
| 1177 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1178 | #ifdef __cplusplus |
| 1179 | } |
| 1180 | #endif |
| 1181 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1182 | #endif /* rsa.h */ |