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