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