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 | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 51 | #define MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION -0x4500 /**< The implementation doesn't provide the requested operation. */ |
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 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | e511ffc | 2013-08-22 17:33:21 +0200 | [diff] [blame] | 72 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 73 | #ifdef __cplusplus |
| 74 | extern "C" { |
| 75 | #endif |
| 76 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | /** |
| 78 | * \brief RSA context structure |
| 79 | */ |
| 80 | typedef struct |
| 81 | { |
| 82 | int ver; /*!< always 0 */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 83 | size_t len; /*!< size(N) in chars */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | mbedtls_mpi N; /*!< public modulus */ |
| 86 | mbedtls_mpi E; /*!< public exponent */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | mbedtls_mpi D; /*!< private exponent */ |
| 89 | mbedtls_mpi P; /*!< 1st prime factor */ |
| 90 | mbedtls_mpi Q; /*!< 2nd prime factor */ |
| 91 | mbedtls_mpi DP; /*!< D % (P - 1) */ |
| 92 | mbedtls_mpi DQ; /*!< D % (Q - 1) */ |
| 93 | mbedtls_mpi QP; /*!< 1 / (Q % P) */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 95 | mbedtls_mpi RN; /*!< cached R^2 mod N */ |
| 96 | mbedtls_mpi RP; /*!< cached R^2 mod P */ |
| 97 | mbedtls_mpi RQ; /*!< cached R^2 mod Q */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | mbedtls_mpi Vi; /*!< cached blinding value */ |
| 100 | mbedtls_mpi Vf; /*!< cached un-blinding value */ |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | int padding; /*!< MBEDTLS_RSA_PKCS_V15 for 1.5 padding and |
Brian J Murray | 98844ff | 2016-08-30 01:50:12 -0700 | [diff] [blame] | 103 | MBEDTLS_RSA_PKCS_v21 for OAEP/PSS */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | int hash_id; /*!< Hash identifier of mbedtls_md_type_t as |
| 105 | specified in the mbedtls_md.h header file |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 106 | for the EME-OAEP and EMSA-PSS |
| 107 | encoding */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | #if defined(MBEDTLS_THREADING_C) |
| 109 | mbedtls_threading_mutex_t mutex; /*!< Thread-safety mutex */ |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 110 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_rsa_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | /** |
| 115 | * \brief Initialize an RSA context |
| 116 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | * Note: Set padding to MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 118 | * encryption scheme and the RSASSA-PSS signature scheme. |
| 119 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | * \param ctx RSA context to be initialized |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21 |
| 122 | * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | * |
| 124 | * \note The hash_id parameter is actually ignored |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | * when using MBEDTLS_RSA_PKCS_V15 padding. |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 126 | * |
| 127 | * \note Choice of padding mode is strictly enforced for private key |
| 128 | * operations, since there might be security concerns in |
| 129 | * mixing padding modes. For public key operations it's merely |
| 130 | * a default value, which can be overriden by calling specific |
| 131 | * rsa_rsaes_xxx or rsa_rsassa_xxx functions. |
| 132 | * |
| 133 | * \note The chosen hash is always used for OEAP encryption. |
| 134 | * For PSS signatures, it's always used for making signatures, |
| 135 | * but can be overriden (and always is, if set to |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | * MBEDTLS_MD_NONE) for verifying them. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | void mbedtls_rsa_init( mbedtls_rsa_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | int padding, |
Paul Bakker | 42099c3 | 2014-01-27 11:45:49 +0100 | [diff] [blame] | 140 | int hash_id); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 143 | * \brief Set padding for an already initialized RSA context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | * See \c mbedtls_rsa_init() for details. |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 145 | * |
| 146 | * \param ctx RSA context to be set |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | * \param padding MBEDTLS_RSA_PKCS_V15 or MBEDTLS_RSA_PKCS_V21 |
| 148 | * \param hash_id MBEDTLS_RSA_PKCS_V21 hash identifier |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id); |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 151 | |
| 152 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | * \brief Generate an RSA keypair |
| 154 | * |
| 155 | * \param ctx RSA context that will hold the key |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 156 | * \param f_rng RNG function |
| 157 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | * \param nbits size of the public key in bits |
| 159 | * \param exponent public exponent (e.g., 65537) |
| 160 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | * \note mbedtls_rsa_init() must be called beforehand to setup |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 162 | * the RSA context. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 167 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 168 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 169 | unsigned int nbits, int exponent ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * \brief Check a public RSA key |
| 173 | * |
| 174 | * \param ctx RSA context to be checked |
| 175 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * \brief Check a private RSA key |
| 182 | * |
| 183 | * \param ctx RSA context to be checked |
| 184 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | |
| 189 | /** |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 190 | * \brief Check a public-private RSA key pair. |
| 191 | * Check each of the contexts, and make sure they match. |
| 192 | * |
| 193 | * \param pub RSA context holding the public key |
| 194 | * \param prv RSA context holding the private key |
| 195 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 197 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 199 | |
| 200 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | * \brief Do an RSA public key operation |
| 202 | * |
| 203 | * \param ctx RSA context |
| 204 | * \param input input buffer |
| 205 | * \param output output buffer |
| 206 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 208 | * |
| 209 | * \note This function does NOT take care of message |
Brian J Murray | 2adecba | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 210 | * padding. Also, be sure to set input[0] = 0 or ensure that |
Paul Bakker | 619467a | 2009-03-28 23:26:51 +0000 | [diff] [blame] | 211 | * input is smaller than N. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | * |
| 213 | * \note The input and output buffers must be large |
| 214 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 215 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | int mbedtls_rsa_public( mbedtls_rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 217 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 218 | unsigned char *output ); |
| 219 | |
| 220 | /** |
| 221 | * \brief Do an RSA private key operation |
| 222 | * |
| 223 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 224 | * \param f_rng RNG function (Needed for blinding) |
| 225 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | * \param input input buffer |
| 227 | * \param output output buffer |
| 228 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | * |
| 231 | * \note The input and output buffers must be large |
| 232 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 233 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | int mbedtls_rsa_private( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 235 | int (*f_rng)(void *, unsigned char *, size_t), |
| 236 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 237 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | unsigned char *output ); |
| 239 | |
| 240 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 241 | * \brief Generic wrapper to perform a PKCS#1 encryption using the |
| 242 | * mode from the context. Add the message padding, then do an |
| 243 | * RSA operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | * |
| 245 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 246 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | * and MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 248 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 249 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
Paul Bakker | 592457c | 2009-04-01 19:01:43 +0000 | [diff] [blame] | 250 | * \param ilen contains the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | * \param input buffer holding the data to be encrypted |
| 252 | * \param output buffer that will hold the ciphertext |
| 253 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 254 | * \deprecated It is deprecated and discouraged to call this function |
| 255 | * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary |
| 256 | * are likely to remove the mode argument and have it implicitly |
| 257 | * set to MBEDTLS_RSA_PUBLIC. |
| 258 | * |
| 259 | * \note Alternative implementations of RSA need not support |
| 260 | * mode being set to MBEDTLS_RSA_PRIVATE and may instead |
| 261 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 262 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | * |
| 265 | * \note The output buffer must be as large as the size |
| 266 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 267 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 269 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 270 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 271 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 272 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | unsigned char *output ); |
| 274 | |
| 275 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 276 | * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) |
| 277 | * |
| 278 | * \param ctx RSA context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | * \param f_rng RNG function (Needed for padding and MBEDTLS_RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 280 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 282 | * \param ilen contains the plaintext length |
| 283 | * \param input buffer holding the data to be encrypted |
| 284 | * \param output buffer that will hold the ciphertext |
| 285 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 286 | * \deprecated It is deprecated and discouraged to call this function |
| 287 | * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary |
| 288 | * are likely to remove the mode argument and have it implicitly |
| 289 | * set to MBEDTLS_RSA_PUBLIC. |
| 290 | * |
| 291 | * \note Alternative implementations of RSA need not support |
| 292 | * mode being set to MBEDTLS_RSA_PRIVATE and may instead |
| 293 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 294 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 296 | * |
| 297 | * \note The output buffer must be as large as the size |
| 298 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 299 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 301 | int (*f_rng)(void *, unsigned char *, size_t), |
| 302 | void *p_rng, |
| 303 | int mode, size_t ilen, |
| 304 | const unsigned char *input, |
| 305 | unsigned char *output ); |
| 306 | |
| 307 | /** |
| 308 | * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) |
| 309 | * |
| 310 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 311 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | * and MBEDTLS_RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 313 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 315 | * \param label buffer holding the custom label to use |
| 316 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 317 | * \param ilen contains the plaintext length |
| 318 | * \param input buffer holding the data to be encrypted |
| 319 | * \param output buffer that will hold the ciphertext |
| 320 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 321 | * \deprecated It is deprecated and discouraged to call this function |
| 322 | * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary |
| 323 | * are likely to remove the mode argument and have it implicitly |
| 324 | * set to MBEDTLS_RSA_PUBLIC. |
| 325 | * |
| 326 | * \note Alternative implementations of RSA need not support |
| 327 | * mode being set to MBEDTLS_RSA_PRIVATE and may instead |
| 328 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 329 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 331 | * |
| 332 | * \note The output buffer must be as large as the size |
| 333 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 334 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 336 | int (*f_rng)(void *, unsigned char *, size_t), |
| 337 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 338 | int mode, |
| 339 | const unsigned char *label, size_t label_len, |
| 340 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 341 | const unsigned char *input, |
| 342 | unsigned char *output ); |
| 343 | |
| 344 | /** |
| 345 | * \brief Generic wrapper to perform a PKCS#1 decryption using the |
| 346 | * mode from the context. Do an RSA operation, then remove |
| 347 | * the message padding |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 348 | * |
| 349 | * \param ctx RSA context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 351 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
Paul Bakker | 4d8ca70 | 2011-08-09 10:31:05 +0000 | [diff] [blame] | 353 | * \param olen will contain the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 354 | * \param input buffer holding the encrypted data |
| 355 | * \param output buffer that will hold the plaintext |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 356 | * \param output_max_len maximum length of the output buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 357 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 358 | * \deprecated It is deprecated and discouraged to call this function |
| 359 | * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary |
| 360 | * are likely to remove the mode argument and have it implicitly |
| 361 | * set to MBEDTLS_RSA_PRIVATE. |
| 362 | * |
| 363 | * \note Alternative implementations of RSA need not support |
| 364 | * mode being set to MBEDTLS_RSA_PUBLIC and may instead |
| 365 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 366 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 367 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 368 | * |
Hanno Becker | b647919 | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 369 | * \note The output buffer length \c output_max_len should be |
| 370 | * as large as the size ctx->len of ctx->N (eg. 128 bytes |
| 371 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 372 | * decrypted message. If it is not large enough to hold |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 373 | * the decryption of the particular ciphertext provided, |
Hanno Becker | b647919 | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 374 | * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
| 375 | * |
| 376 | * \note The input buffer must be as large as the size |
| 377 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 378 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 380 | int (*f_rng)(void *, unsigned char *, size_t), |
| 381 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 382 | int mode, size_t *olen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 383 | const unsigned char *input, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 384 | unsigned char *output, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 385 | size_t output_max_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 386 | |
| 387 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 388 | * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) |
| 389 | * |
| 390 | * \param ctx RSA context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 392 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 394 | * \param olen will contain the plaintext length |
| 395 | * \param input buffer holding the encrypted data |
| 396 | * \param output buffer that will hold the plaintext |
| 397 | * \param output_max_len maximum length of the output buffer |
| 398 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 399 | * \deprecated It is deprecated and discouraged to call this function |
| 400 | * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary |
| 401 | * are likely to remove the mode argument and have it implicitly |
| 402 | * set to MBEDTLS_RSA_PRIVATE. |
| 403 | * |
| 404 | * \note Alternative implementations of RSA need not support |
| 405 | * mode being set to MBEDTLS_RSA_PUBLIC and may instead |
| 406 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 407 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 408 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 409 | * |
Hanno Becker | b647919 | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 410 | * \note The output buffer length \c output_max_len should be |
| 411 | * as large as the size ctx->len of ctx->N (eg. 128 bytes |
| 412 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 413 | * decrypted message. If it is not large enough to hold |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 414 | * the decryption of the particular ciphertext provided, |
Hanno Becker | b647919 | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 415 | * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
| 416 | * |
| 417 | * \note The input buffer must be as large as the size |
| 418 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 419 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 421 | int (*f_rng)(void *, unsigned char *, size_t), |
| 422 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 423 | int mode, size_t *olen, |
| 424 | const unsigned char *input, |
| 425 | unsigned char *output, |
| 426 | size_t output_max_len ); |
| 427 | |
| 428 | /** |
| 429 | * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT) |
| 430 | * |
| 431 | * \param ctx RSA context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 433 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 435 | * \param label buffer holding the custom label to use |
| 436 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 437 | * \param olen will contain the plaintext length |
| 438 | * \param input buffer holding the encrypted data |
| 439 | * \param output buffer that will hold the plaintext |
| 440 | * \param output_max_len maximum length of the output buffer |
| 441 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 442 | * \deprecated It is deprecated and discouraged to call this function |
| 443 | * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary |
| 444 | * are likely to remove the mode argument and have it implicitly |
| 445 | * set to MBEDTLS_RSA_PRIVATE. |
| 446 | * |
| 447 | * \note Alternative implementations of RSA need not support |
| 448 | * mode being set to MBEDTLS_RSA_PUBLIC and may instead |
| 449 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 450 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 451 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 452 | * |
Hanno Becker | b647919 | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 453 | * \note The output buffer length \c output_max_len should be |
| 454 | * as large as the size ctx->len of ctx->N (eg. 128 bytes |
| 455 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 456 | * decrypted message. If it is not large enough to hold |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 457 | * the decryption of the particular ciphertext provided, |
Hanno Becker | b647919 | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 458 | * the function will return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. |
| 459 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 460 | * \note The input buffer must be as large as the size |
Hanno Becker | b647919 | 2017-05-04 11:27:39 +0100 | [diff] [blame] | 461 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 462 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 464 | int (*f_rng)(void *, unsigned char *, size_t), |
| 465 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 466 | int mode, |
| 467 | const unsigned char *label, size_t label_len, |
| 468 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 469 | const unsigned char *input, |
| 470 | unsigned char *output, |
| 471 | size_t output_max_len ); |
| 472 | |
| 473 | /** |
| 474 | * \brief Generic wrapper to perform a PKCS#1 signature using the |
| 475 | * mode from the context. Do a private RSA operation to sign |
| 476 | * a message digest |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 477 | * |
| 478 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 479 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | * MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 481 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
| 483 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 484 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 485 | * \param hash buffer holding the message digest |
| 486 | * \param sig buffer that will hold the ciphertext |
| 487 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 488 | * \deprecated It is deprecated and discouraged to call this function |
| 489 | * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary |
| 490 | * are likely to remove the mode argument and have it implicitly |
| 491 | * set to MBEDTLS_RSA_PRIVATE. |
| 492 | * |
| 493 | * \note Alternative implementations of RSA need not support |
| 494 | * mode being set to MBEDTLS_RSA_PUBLIC and may instead |
| 495 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 496 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 | * \return 0 if the signing operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 499 | * |
| 500 | * \note The "sig" buffer must be as large as the size |
| 501 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 502 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 503 | * \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] | 504 | * \note \c mbedtls_rsa_rsassa_pss_sign() for details on md_alg and hash_id. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 505 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 506 | int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 507 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 508 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 511 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 512 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 513 | unsigned char *sig ); |
| 514 | |
| 515 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 516 | * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN) |
| 517 | * |
| 518 | * \param ctx RSA context |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 519 | * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 520 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 521 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
| 522 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 523 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 524 | * \param hash buffer holding the message digest |
| 525 | * \param sig buffer that will hold the ciphertext |
| 526 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 527 | * \deprecated It is deprecated and discouraged to call this function |
| 528 | * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary |
| 529 | * are likely to remove the mode argument and have it implicitly |
| 530 | * set to MBEDTLS_RSA_PRIVATE. |
| 531 | * |
| 532 | * \note Alternative implementations of RSA need not support |
| 533 | * mode being set to MBEDTLS_RSA_PUBLIC and may instead |
| 534 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 535 | * |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 536 | * \return 0 if the signing operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 537 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 538 | * |
| 539 | * \note The "sig" buffer must be as large as the size |
| 540 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 541 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 543 | int (*f_rng)(void *, unsigned char *, size_t), |
| 544 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 545 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 547 | unsigned int hashlen, |
| 548 | const unsigned char *hash, |
| 549 | unsigned char *sig ); |
| 550 | |
| 551 | /** |
| 552 | * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) |
| 553 | * |
| 554 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 555 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | * MBEDTLS_RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 557 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
| 559 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 560 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 561 | * \param hash buffer holding the message digest |
| 562 | * \param sig buffer that will hold the ciphertext |
| 563 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 564 | * \deprecated It is deprecated and discouraged to call this function |
| 565 | * in mode MBEDTLS_RSA_PUBLIC. Future versions of the libary |
| 566 | * are likely to remove the mode argument and have it implicitly |
| 567 | * set to MBEDTLS_RSA_PRIVATE. |
| 568 | * |
| 569 | * \note Alternative implementations of RSA need not support |
| 570 | * mode being set to MBEDTLS_RSA_PUBLIC and may instead |
| 571 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 572 | * |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 573 | * \return 0 if the signing operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 575 | * |
| 576 | * \note The "sig" buffer must be as large as the size |
| 577 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 578 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 579 | * \note The hash_id in the RSA context is the one used for the |
| 580 | * encoding. md_alg in the function call is the type of hash |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 581 | * that is encoded. According to RFC 3447 it is advised to |
| 582 | * keep both hashes the same. |
| 583 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 585 | int (*f_rng)(void *, unsigned char *, size_t), |
| 586 | void *p_rng, |
| 587 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 589 | unsigned int hashlen, |
| 590 | const unsigned char *hash, |
| 591 | unsigned char *sig ); |
| 592 | |
| 593 | /** |
| 594 | * \brief Generic wrapper to perform a PKCS#1 verification using the |
| 595 | * mode from the context. Do a public RSA operation and check |
| 596 | * the message digest |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 597 | * |
| 598 | * \param ctx points to an RSA public key |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 600 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 601 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
| 602 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 603 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 604 | * \param hash buffer holding the message digest |
| 605 | * \param sig buffer holding the ciphertext |
| 606 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 607 | * \deprecated It is deprecated and discouraged to call this function |
| 608 | * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary |
| 609 | * are likely to remove the mode argument and have it implicitly |
| 610 | * set to MBEDTLS_RSA_PUBLIC. |
| 611 | * |
| 612 | * \note Alternative implementations of RSA need not support |
| 613 | * mode being set to MBEDTLS_RSA_PRIVATE and may instead |
| 614 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 615 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 616 | * \return 0 if the verify operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 618 | * |
| 619 | * \note The "sig" buffer must be as large as the size |
| 620 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 621 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 622 | * \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] | 623 | * \c mbedtls_rsa_rsassa_pss_verify() about md_alg and hash_id. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 624 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 626 | int (*f_rng)(void *, unsigned char *, size_t), |
| 627 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 628 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 630 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 631 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 632 | const unsigned char *sig ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 633 | |
| 634 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 635 | * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) |
| 636 | * |
| 637 | * \param ctx points to an RSA public key |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 639 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 640 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
| 641 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 642 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 643 | * \param hash buffer holding the message digest |
| 644 | * \param sig buffer holding the ciphertext |
| 645 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 646 | * \deprecated It is deprecated and discouraged to call this function |
| 647 | * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary |
| 648 | * are likely to remove the mode argument and have it implicitly |
| 649 | * set to MBEDTLS_RSA_PUBLIC. |
| 650 | * |
| 651 | * \note Alternative implementations of RSA need not support |
| 652 | * mode being set to MBEDTLS_RSA_PRIVATE and may instead |
| 653 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 654 | * |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 655 | * \return 0 if the verify operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 656 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 657 | * |
| 658 | * \note The "sig" buffer must be as large as the size |
| 659 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 660 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 662 | int (*f_rng)(void *, unsigned char *, size_t), |
| 663 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 664 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 666 | unsigned int hashlen, |
| 667 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 668 | const unsigned char *sig ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 669 | |
| 670 | /** |
| 671 | * \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] | 672 | * (This is the "simple" version.) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 673 | * |
| 674 | * \param ctx points to an RSA public key |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 676 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
| 678 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 679 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 680 | * \param hash buffer holding the message digest |
| 681 | * \param sig buffer holding the ciphertext |
| 682 | * |
Hanno Becker | 3cdc711 | 2017-10-05 10:09:31 +0100 | [diff] [blame^] | 683 | * \deprecated It is deprecated and discouraged to call this function |
| 684 | * in mode MBEDTLS_RSA_PRIVATE. Future versions of the libary |
| 685 | * are likely to remove the mode argument and have it implicitly |
| 686 | * set to MBEDTLS_RSA_PUBLIC. |
| 687 | * |
| 688 | * \note Alternative implementations of RSA need not support |
| 689 | * mode being set to MBEDTLS_RSA_PRIVATE and may instead |
| 690 | * return MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION. |
| 691 | * |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 692 | * \return 0 if the verify operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | * or an MBEDTLS_ERR_RSA_XXX error code |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 694 | * |
| 695 | * \note The "sig" buffer must be as large as the size |
| 696 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 697 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 698 | * \note The hash_id in the RSA context is the one used for the |
| 699 | * verification. md_alg in the function call is the type of |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 700 | * hash that is verified. According to RFC 3447 it is advised to |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 701 | * keep both hashes the same. If hash_id in the RSA context is |
| 702 | * unset, the md_alg from the function call is used. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 703 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 705 | int (*f_rng)(void *, unsigned char *, size_t), |
| 706 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 707 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 709 | unsigned int hashlen, |
| 710 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 711 | const unsigned char *sig ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 712 | |
| 713 | /** |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 714 | * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) |
| 715 | * (This is the version with "full" options.) |
| 716 | * |
| 717 | * \param ctx points to an RSA public key |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 718 | * \param f_rng RNG function (Only needed for MBEDTLS_RSA_PRIVATE) |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 719 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | * \param mode MBEDTLS_RSA_PUBLIC or MBEDTLS_RSA_PRIVATE |
| 721 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) |
| 722 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 723 | * \param hash buffer holding the message digest |
| 724 | * \param mgf1_hash_id message digest used for mask generation |
| 725 | * \param expected_salt_len Length of the salt used in padding, use |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | * MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 727 | * \param sig buffer holding the ciphertext |
| 728 | * |
| 729 | * \return 0 if the verify operation was successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | * or an MBEDTLS_ERR_RSA_XXX error code |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 731 | * |
| 732 | * \note The "sig" buffer must be as large as the size |
| 733 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 734 | * |
| 735 | * \note The hash_id in the RSA context is ignored. |
| 736 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 737 | 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] | 738 | int (*f_rng)(void *, unsigned char *, size_t), |
| 739 | void *p_rng, |
| 740 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 742 | unsigned int hashlen, |
| 743 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | mbedtls_md_type_t mgf1_hash_id, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 745 | int expected_salt_len, |
| 746 | const unsigned char *sig ); |
| 747 | |
| 748 | /** |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 749 | * \brief Copy the components of an RSA context |
| 750 | * |
| 751 | * \param dst Destination context |
| 752 | * \param src Source context |
| 753 | * |
Manuel Pégourié-Gonnard | 81abefd | 2015-05-29 12:53:47 +0200 | [diff] [blame] | 754 | * \return 0 on success, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 755 | * MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 756 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | 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] | 758 | |
| 759 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 760 | * \brief Free the components of an RSA key |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 761 | * |
| 762 | * \param ctx RSA Context to free |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 763 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 764 | void mbedtls_rsa_free( mbedtls_rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 765 | |
| 766 | /** |
| 767 | * \brief Checkup routine |
| 768 | * |
| 769 | * \return 0 if successful, or 1 if the test failed |
| 770 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 771 | int mbedtls_rsa_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 772 | |
| 773 | #ifdef __cplusplus |
| 774 | } |
| 775 | #endif |
| 776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 778 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 779 | #endif /* rsa.h */ |