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