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