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 | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 24 | #ifndef POLARSSL_RSA_H |
| 25 | #define POLARSSL_RSA_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 32 | |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 33 | #include "bignum.h" |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 34 | #include "md.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 36 | #if defined(POLARSSL_THREADING_C) |
| 37 | #include "threading.h" |
| 38 | #endif |
| 39 | |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 40 | /* |
| 41 | * RSA Error codes |
| 42 | */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 43 | #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */ |
| 44 | #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */ |
| 45 | #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */ |
Nicholas Wilson | d0fa5cc | 2015-05-11 10:39:49 +0100 | [diff] [blame] | 46 | #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the library's validity check. */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 47 | #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */ |
| 48 | #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */ |
| 49 | #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */ |
| 50 | #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */ |
| 51 | #define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */ |
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 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | #define RSA_PUBLIC 0 |
| 57 | #define RSA_PRIVATE 1 |
| 58 | |
| 59 | #define RSA_PKCS_V15 0 |
| 60 | #define RSA_PKCS_V21 1 |
| 61 | |
| 62 | #define RSA_SIGN 1 |
| 63 | #define RSA_CRYPT 2 |
| 64 | |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 65 | #define RSA_SALT_LEN_ANY -1 |
| 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 | */ |
| 71 | #if defined(POLARSSL_RSA_C) |
| 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 | |
| 85 | mpi N; /*!< public modulus */ |
| 86 | mpi E; /*!< public exponent */ |
| 87 | |
| 88 | mpi D; /*!< private exponent */ |
| 89 | mpi P; /*!< 1st prime factor */ |
| 90 | mpi Q; /*!< 2nd prime factor */ |
| 91 | mpi DP; /*!< D % (P - 1) */ |
| 92 | mpi DQ; /*!< D % (Q - 1) */ |
| 93 | mpi QP; /*!< 1 / (Q % P) */ |
| 94 | |
| 95 | mpi RN; /*!< cached R^2 mod N */ |
| 96 | mpi RP; /*!< cached R^2 mod P */ |
| 97 | mpi RQ; /*!< cached R^2 mod Q */ |
| 98 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 99 | mpi Vi; /*!< cached blinding value */ |
| 100 | mpi Vf; /*!< cached un-blinding value */ |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 101 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 102 | int padding; /*!< RSA_PKCS_V15 for 1.5 padding and |
| 103 | RSA_PKCS_v21 for OAEP/PSS */ |
| 104 | int hash_id; /*!< Hash identifier of md_type_t as |
| 105 | specified in the md.h header file |
| 106 | for the EME-OAEP and EMSA-PSS |
| 107 | encoding */ |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 108 | #if defined(POLARSSL_THREADING_C) |
| 109 | threading_mutex_t mutex; /*!< Thread-safety mutex */ |
| 110 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | } |
| 112 | rsa_context; |
| 113 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | /** |
| 115 | * \brief Initialize an RSA context |
| 116 | * |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 117 | * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP |
| 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 |
| 121 | * \param padding RSA_PKCS_V15 or RSA_PKCS_V21 |
| 122 | * \param hash_id 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 |
| 125 | * when using 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 |
| 136 | * POLARSSL_MD_NONE) for verifying them. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | */ |
| 138 | void rsa_init( rsa_context *ctx, |
| 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 | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 144 | * See \c 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 |
| 147 | * \param padding RSA_PKCS_V15 or RSA_PKCS_V21 |
| 148 | * \param hash_id RSA_PKCS_V21 hash identifier |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 149 | */ |
| 150 | void rsa_set_padding( rsa_context *ctx, int padding, int hash_id); |
| 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 | * |
| 161 | * \note 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 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 164 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | */ |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 166 | int rsa_gen_key( 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 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 176 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 178 | int rsa_check_pubkey( const 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 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 185 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 187 | int rsa_check_privkey( const 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 | * |
| 196 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 197 | */ |
| 198 | int rsa_check_pub_priv( const rsa_context *pub, const rsa_context *prv ); |
| 199 | |
| 200 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | * \brief Do an RSA public key operation |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 202 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | * |
| 204 | * \param ctx RSA context |
| 205 | * \param input input buffer |
| 206 | * \param output output buffer |
| 207 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 208 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | * |
| 210 | * \note This function does NOT take care of message |
Paul Bakker | 619467a | 2009-03-28 23:26:51 +0000 | [diff] [blame] | 211 | * padding. Also, be sure to set input[0] = 0 or assure that |
| 212 | * input is smaller than N. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | * |
| 214 | * \note The input and output buffers must be large |
| 215 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 216 | */ |
| 217 | int rsa_public( rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 218 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | unsigned char *output ); |
| 220 | |
| 221 | /** |
| 222 | * \brief Do an RSA private key operation |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 223 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 | * |
| 225 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 226 | * \param f_rng RNG function (Needed for blinding) |
| 227 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 228 | * \param input input buffer |
| 229 | * \param output output buffer |
| 230 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 231 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 232 | * |
| 233 | * \note The input and output buffers must be large |
| 234 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 235 | */ |
| 236 | int rsa_private( rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 237 | int (*f_rng)(void *, unsigned char *, size_t), |
| 238 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 239 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 240 | unsigned char *output ); |
| 241 | |
| 242 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 243 | * \brief Generic wrapper to perform a PKCS#1 encryption using the |
| 244 | * mode from the context. Add the message padding, then do an |
| 245 | * RSA operation. |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 246 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 247 | * |
| 248 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 249 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding |
| 250 | * and RSA_PRIVATE) |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 251 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | 592457c | 2009-04-01 19:01:43 +0000 | [diff] [blame] | 253 | * \param ilen contains the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 254 | * \param input buffer holding the data to be encrypted |
| 255 | * \param output buffer that will hold the ciphertext |
| 256 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 257 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 258 | * |
| 259 | * \note The output buffer must be as large as the size |
| 260 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 261 | */ |
| 262 | int rsa_pkcs1_encrypt( rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 263 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 264 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 265 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 266 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 267 | unsigned char *output ); |
| 268 | |
| 269 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 270 | * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 271 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 272 | * |
| 273 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 274 | * \param f_rng RNG function (Needed for padding and RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 275 | * \param p_rng RNG parameter |
| 276 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 277 | * \param ilen contains the plaintext length |
| 278 | * \param input buffer holding the data to be encrypted |
| 279 | * \param output buffer that will hold the ciphertext |
| 280 | * |
| 281 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 282 | * |
| 283 | * \note The output buffer must be as large as the size |
| 284 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 285 | */ |
| 286 | int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx, |
| 287 | int (*f_rng)(void *, unsigned char *, size_t), |
| 288 | void *p_rng, |
| 289 | int mode, size_t ilen, |
| 290 | const unsigned char *input, |
| 291 | unsigned char *output ); |
| 292 | |
| 293 | /** |
| 294 | * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 295 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 296 | * |
| 297 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 298 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding |
| 299 | * and RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 300 | * \param p_rng RNG parameter |
| 301 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 302 | * \param label buffer holding the custom label to use |
| 303 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 304 | * \param ilen contains the plaintext length |
| 305 | * \param input buffer holding the data to be encrypted |
| 306 | * \param output buffer that will hold the ciphertext |
| 307 | * |
| 308 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 309 | * |
| 310 | * \note The output buffer must be as large as the size |
| 311 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 312 | */ |
| 313 | int rsa_rsaes_oaep_encrypt( rsa_context *ctx, |
| 314 | int (*f_rng)(void *, unsigned char *, size_t), |
| 315 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 316 | int mode, |
| 317 | const unsigned char *label, size_t label_len, |
| 318 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 319 | const unsigned char *input, |
| 320 | unsigned char *output ); |
| 321 | |
| 322 | /** |
| 323 | * \brief Generic wrapper to perform a PKCS#1 decryption using the |
| 324 | * mode from the context. Do an RSA operation, then remove |
| 325 | * the message padding |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 326 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 327 | * |
| 328 | * \param ctx RSA context |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 329 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 330 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 331 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | 4d8ca70 | 2011-08-09 10:31:05 +0000 | [diff] [blame] | 332 | * \param olen will contain the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 333 | * \param input buffer holding the encrypted data |
| 334 | * \param output buffer that will hold the plaintext |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 335 | * \param output_max_len maximum length of the output buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 337 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | * |
Simon Butcher | 573bb96 | 2017-07-19 01:58:47 +0100 | [diff] [blame] | 339 | * \note The output buffer length \c output_max_len should be |
| 340 | * as large as the size ctx->len of ctx->N (eg. 128 bytes |
| 341 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 342 | * decrypted message. If it is not large enough to hold |
| 343 | * the decryption of the particular ciphertext provided, |
| 344 | * the function will return POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE. |
| 345 | * |
| 346 | * \note The input buffer must be as large as the size |
| 347 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 348 | */ |
| 349 | int rsa_pkcs1_decrypt( rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 350 | int (*f_rng)(void *, unsigned char *, size_t), |
| 351 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 352 | int mode, size_t *olen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 353 | const unsigned char *input, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 354 | unsigned char *output, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 355 | size_t output_max_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 | |
| 357 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 358 | * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 359 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 360 | * |
| 361 | * \param ctx RSA context |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 362 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 363 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 364 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 365 | * \param olen will contain the plaintext length |
| 366 | * \param input buffer holding the encrypted data |
| 367 | * \param output buffer that will hold the plaintext |
| 368 | * \param output_max_len maximum length of the output buffer |
| 369 | * |
| 370 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 371 | * |
Simon Butcher | 573bb96 | 2017-07-19 01:58:47 +0100 | [diff] [blame] | 372 | * \note The output buffer length \c output_max_len should be |
| 373 | * as large as the size ctx->len of ctx->N (eg. 128 bytes |
| 374 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 375 | * decrypted message. If it is not large enough to hold |
| 376 | * the decryption of the particular ciphertext provided, |
| 377 | * the function will return POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE. |
| 378 | * |
| 379 | * \note The input buffer must be as large as the size |
| 380 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Hanno Becker | 1af21bf | 2017-05-11 16:33:02 +0100 | [diff] [blame^] | 381 | * |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 382 | */ |
| 383 | int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 384 | int (*f_rng)(void *, unsigned char *, size_t), |
| 385 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 386 | int mode, size_t *olen, |
| 387 | const unsigned char *input, |
| 388 | unsigned char *output, |
| 389 | size_t output_max_len ); |
| 390 | |
| 391 | /** |
| 392 | * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT) |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 393 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 394 | * |
| 395 | * \param ctx RSA context |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 396 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 397 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 398 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 399 | * \param label buffer holding the custom label to use |
| 400 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 401 | * \param olen will contain the plaintext length |
| 402 | * \param input buffer holding the encrypted data |
| 403 | * \param output buffer that will hold the plaintext |
| 404 | * \param output_max_len maximum length of the output buffer |
| 405 | * |
| 406 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 407 | * |
Simon Butcher | 573bb96 | 2017-07-19 01:58:47 +0100 | [diff] [blame] | 408 | * \note The output buffer length \c output_max_len should be |
| 409 | * as large as the size ctx->len of ctx->N (eg. 128 bytes |
| 410 | * if RSA-1024 is used) to be able to hold an arbitrary |
| 411 | * decrypted message. If it is not large enough to hold |
| 412 | * the decryption of the particular ciphertext provided, |
| 413 | * the function will return POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE. |
| 414 | * |
Hanno Becker | 1af21bf | 2017-05-11 16:33:02 +0100 | [diff] [blame^] | 415 | * \note The input buffer must be as large as the size |
Simon Butcher | 573bb96 | 2017-07-19 01:58:47 +0100 | [diff] [blame] | 416 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Hanno Becker | 1af21bf | 2017-05-11 16:33:02 +0100 | [diff] [blame^] | 417 | * |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 418 | */ |
| 419 | int rsa_rsaes_oaep_decrypt( rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 420 | int (*f_rng)(void *, unsigned char *, size_t), |
| 421 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 422 | int mode, |
| 423 | const unsigned char *label, size_t label_len, |
| 424 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 425 | const unsigned char *input, |
| 426 | unsigned char *output, |
| 427 | size_t output_max_len ); |
| 428 | |
| 429 | /** |
| 430 | * \brief Generic wrapper to perform a PKCS#1 signature using the |
| 431 | * mode from the context. Do a private RSA operation to sign |
| 432 | * a message digest |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 433 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 434 | * |
| 435 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 436 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for |
| 437 | * RSA_PRIVATE) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 438 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 439 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 440 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 441 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 442 | * \param hash buffer holding the message digest |
| 443 | * \param sig buffer that will hold the ciphertext |
| 444 | * |
| 445 | * \return 0 if the signing operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 446 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 447 | * |
| 448 | * \note The "sig" buffer must be as large as the size |
| 449 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 450 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 451 | * \note In case of PKCS#1 v2.1 encoding, see comments on |
| 452 | * \note \c rsa_rsassa_pss_sign() for details on md_alg and hash_id. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 453 | */ |
| 454 | int rsa_pkcs1_sign( rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 455 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 456 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 457 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 458 | md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 459 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 460 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 461 | unsigned char *sig ); |
| 462 | |
| 463 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 464 | * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN) |
| 465 | * |
| 466 | * \param ctx RSA context |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 467 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 468 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 469 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 470 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 471 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 472 | * \param hash buffer holding the message digest |
| 473 | * \param sig buffer that will hold the ciphertext |
| 474 | * |
| 475 | * \return 0 if the signing operation was successful, |
| 476 | * or an POLARSSL_ERR_RSA_XXX error code |
| 477 | * |
| 478 | * \note The "sig" buffer must be as large as the size |
| 479 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 480 | */ |
| 481 | int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 482 | int (*f_rng)(void *, unsigned char *, size_t), |
| 483 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 484 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 485 | md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 486 | unsigned int hashlen, |
| 487 | const unsigned char *hash, |
| 488 | unsigned char *sig ); |
| 489 | |
| 490 | /** |
| 491 | * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 492 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 493 | * |
| 494 | * \param ctx RSA context |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 495 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for |
| 496 | * RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 497 | * \param p_rng RNG parameter |
| 498 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 499 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 500 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 501 | * \param hash buffer holding the message digest |
| 502 | * \param sig buffer that will hold the ciphertext |
| 503 | * |
| 504 | * \return 0 if the signing operation was successful, |
| 505 | * or an POLARSSL_ERR_RSA_XXX error code |
| 506 | * |
| 507 | * \note The "sig" buffer must be as large as the size |
| 508 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 509 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 510 | * \note The hash_id in the RSA context is the one used for the |
| 511 | * encoding. md_alg in the function call is the type of hash |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 512 | * that is encoded. According to RFC 3447 it is advised to |
| 513 | * keep both hashes the same. |
| 514 | */ |
| 515 | int rsa_rsassa_pss_sign( rsa_context *ctx, |
| 516 | int (*f_rng)(void *, unsigned char *, size_t), |
| 517 | void *p_rng, |
| 518 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 519 | md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 520 | unsigned int hashlen, |
| 521 | const unsigned char *hash, |
| 522 | unsigned char *sig ); |
| 523 | |
| 524 | /** |
| 525 | * \brief Generic wrapper to perform a PKCS#1 verification using the |
| 526 | * mode from the context. Do a public RSA operation and check |
| 527 | * the message digest |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 528 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 529 | * |
| 530 | * \param ctx points to an RSA public key |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 531 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 532 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 533 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 534 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 535 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 536 | * \param hash buffer holding the message digest |
| 537 | * \param sig buffer holding the ciphertext |
| 538 | * |
| 539 | * \return 0 if the verify operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 540 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 541 | * |
| 542 | * \note The "sig" buffer must be as large as the size |
| 543 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 544 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 545 | * \note In case of PKCS#1 v2.1 encoding, see comments on |
| 546 | * \c rsa_rsassa_pss_verify() about md_alg and hash_id. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 547 | */ |
| 548 | int rsa_pkcs1_verify( rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 549 | int (*f_rng)(void *, unsigned char *, size_t), |
| 550 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 551 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 552 | md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 553 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 554 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 555 | const unsigned char *sig ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 556 | |
| 557 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 558 | * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 559 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 560 | * |
| 561 | * \param ctx points to an RSA public key |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 562 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 563 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 564 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 565 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 566 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 567 | * \param hash buffer holding the message digest |
| 568 | * \param sig buffer holding the ciphertext |
| 569 | * |
| 570 | * \return 0 if the verify operation was successful, |
| 571 | * or an POLARSSL_ERR_RSA_XXX error code |
| 572 | * |
| 573 | * \note The "sig" buffer must be as large as the size |
| 574 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 575 | */ |
| 576 | int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 577 | int (*f_rng)(void *, unsigned char *, size_t), |
| 578 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 579 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 580 | md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 581 | unsigned int hashlen, |
| 582 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 583 | const unsigned char *sig ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 584 | |
| 585 | /** |
| 586 | * \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] | 587 | * (This is the "simple" version.) |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 588 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 589 | * |
| 590 | * \param ctx points to an RSA public key |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 591 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 592 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 593 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 594 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 595 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 596 | * \param hash buffer holding the message digest |
| 597 | * \param sig buffer holding the ciphertext |
| 598 | * |
| 599 | * \return 0 if the verify operation was successful, |
| 600 | * or an POLARSSL_ERR_RSA_XXX error code |
| 601 | * |
| 602 | * \note The "sig" buffer must be as large as the size |
| 603 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 604 | * |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 605 | * \note The hash_id in the RSA context is the one used for the |
| 606 | * verification. md_alg in the function call is the type of |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 607 | * 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] | 608 | * keep both hashes the same. If hash_id in the RSA context is |
| 609 | * unset, the md_alg from the function call is used. |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 610 | */ |
| 611 | int rsa_rsassa_pss_verify( rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 612 | int (*f_rng)(void *, unsigned char *, size_t), |
| 613 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 614 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 615 | md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 616 | unsigned int hashlen, |
| 617 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 618 | const unsigned char *sig ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 619 | |
| 620 | /** |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 621 | * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) |
| 622 | * (This is the version with "full" options.) |
Manuel Pégourié-Gonnard | 5574546 | 2015-07-03 17:51:10 +0200 | [diff] [blame] | 623 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 624 | * |
| 625 | * \param ctx points to an RSA public key |
| 626 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
| 627 | * \param p_rng RNG parameter |
| 628 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 629 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 630 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
| 631 | * \param hash buffer holding the message digest |
| 632 | * \param mgf1_hash_id message digest used for mask generation |
| 633 | * \param expected_salt_len Length of the salt used in padding, use |
| 634 | * RSA_SALT_LEN_ANY to accept any salt length |
| 635 | * \param sig buffer holding the ciphertext |
| 636 | * |
| 637 | * \return 0 if the verify operation was successful, |
| 638 | * or an POLARSSL_ERR_RSA_XXX error code |
| 639 | * |
| 640 | * \note The "sig" buffer must be as large as the size |
| 641 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 642 | * |
| 643 | * \note The hash_id in the RSA context is ignored. |
| 644 | */ |
| 645 | int rsa_rsassa_pss_verify_ext( rsa_context *ctx, |
| 646 | int (*f_rng)(void *, unsigned char *, size_t), |
| 647 | void *p_rng, |
| 648 | int mode, |
| 649 | md_type_t md_alg, |
| 650 | unsigned int hashlen, |
| 651 | const unsigned char *hash, |
| 652 | md_type_t mgf1_hash_id, |
| 653 | int expected_salt_len, |
| 654 | const unsigned char *sig ); |
| 655 | |
| 656 | /** |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 657 | * \brief Copy the components of an RSA context |
| 658 | * |
| 659 | * \param dst Destination context |
| 660 | * \param src Source context |
| 661 | * |
| 662 | * \return O on success, |
| 663 | * POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure |
| 664 | */ |
| 665 | int rsa_copy( rsa_context *dst, const rsa_context *src ); |
| 666 | |
| 667 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 668 | * \brief Free the components of an RSA key |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 669 | * |
| 670 | * \param ctx RSA Context to free |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 671 | */ |
| 672 | void rsa_free( rsa_context *ctx ); |
| 673 | |
| 674 | /** |
| 675 | * \brief Checkup routine |
| 676 | * |
| 677 | * \return 0 if successful, or 1 if the test failed |
| 678 | */ |
| 679 | int rsa_self_test( int verbose ); |
| 680 | |
| 681 | #ifdef __cplusplus |
| 682 | } |
| 683 | #endif |
| 684 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 685 | #endif /* POLARSSL_RSA_C */ |
| 686 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 687 | #endif /* rsa.h */ |