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