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