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