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