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