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