| 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 | #if !defined(POLARSSL_RSA_NO_CRT) | 
|  | 103 | mpi Vi;                     /*!<  cached blinding value     */ | 
|  | 104 | mpi Vf;                     /*!<  cached un-blinding value  */ | 
|  | 105 | #endif | 
|  | 106 |  | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 107 | int padding;                /*!<  RSA_PKCS_V15 for 1.5 padding and | 
|  | 108 | RSA_PKCS_v21 for OAEP/PSS         */ | 
|  | 109 | int hash_id;                /*!<  Hash identifier of md_type_t as | 
|  | 110 | specified in the md.h header file | 
|  | 111 | for the EME-OAEP and EMSA-PSS | 
|  | 112 | encoding                          */ | 
| Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 113 | #if defined(POLARSSL_THREADING_C) | 
|  | 114 | threading_mutex_t mutex;    /*!<  Thread-safety mutex       */ | 
|  | 115 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | } | 
|  | 117 | rsa_context; | 
|  | 118 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | /** | 
|  | 120 | * \brief          Initialize an RSA context | 
|  | 121 | * | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 122 | *                 Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP | 
|  | 123 | *                 encryption scheme and the RSASSA-PSS signature scheme. | 
|  | 124 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | * \param ctx      RSA context to be initialized | 
|  | 126 | * \param padding  RSA_PKCS_V15 or RSA_PKCS_V21 | 
|  | 127 | * \param hash_id  RSA_PKCS_V21 hash identifier | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | * | 
|  | 129 | * \note           The hash_id parameter is actually ignored | 
|  | 130 | *                 when using RSA_PKCS_V15 padding. | 
| Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 131 | * | 
|  | 132 | * \note           Choice of padding mode is strictly enforced for private key | 
|  | 133 | *                 operations, since there might be security concerns in | 
|  | 134 | *                 mixing padding modes. For public key operations it's merely | 
|  | 135 | *                 a default value, which can be overriden by calling specific | 
|  | 136 | *                 rsa_rsaes_xxx or rsa_rsassa_xxx functions. | 
|  | 137 | * | 
|  | 138 | * \note           The chosen hash is always used for OEAP encryption. | 
|  | 139 | *                 For PSS signatures, it's always used for making signatures, | 
|  | 140 | *                 but can be overriden (and always is, if set to | 
|  | 141 | *                 POLARSSL_MD_NONE) for verifying them. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | */ | 
|  | 143 | void rsa_init( rsa_context *ctx, | 
|  | 144 | int padding, | 
| Paul Bakker | 42099c3 | 2014-01-27 11:45:49 +0100 | [diff] [blame] | 145 | int hash_id); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 |  | 
|  | 147 | /** | 
| Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 148 | * \brief          Set padding for an already initialized RSA context | 
| Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 149 | *                 See \c rsa_init() for details. | 
| Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 150 | * | 
|  | 151 | * \param ctx      RSA context to be set | 
|  | 152 | * \param padding  RSA_PKCS_V15 or RSA_PKCS_V21 | 
|  | 153 | * \param hash_id  RSA_PKCS_V21 hash identifier | 
| Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 154 | */ | 
|  | 155 | void rsa_set_padding( rsa_context *ctx, int padding, int hash_id); | 
|  | 156 |  | 
|  | 157 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | * \brief          Generate an RSA keypair | 
|  | 159 | * | 
|  | 160 | * \param ctx      RSA context that will hold the key | 
| Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 161 | * \param f_rng    RNG function | 
|  | 162 | * \param p_rng    RNG parameter | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | * \param nbits    size of the public key in bits | 
|  | 164 | * \param exponent public exponent (e.g., 65537) | 
|  | 165 | * | 
|  | 166 | * \note           rsa_init() must be called beforehand to setup | 
| Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 167 | *                 the RSA context. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | * | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 169 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 170 | */ | 
| Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 171 | int rsa_gen_key( rsa_context *ctx, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 172 | int (*f_rng)(void *, unsigned char *, size_t), | 
| Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 173 | void *p_rng, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 174 | unsigned int nbits, int exponent ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 175 |  | 
|  | 176 | /** | 
|  | 177 | * \brief          Check a public RSA key | 
|  | 178 | * | 
|  | 179 | * \param ctx      RSA context to be checked | 
|  | 180 | * | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 181 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 183 | int rsa_check_pubkey( const rsa_context *ctx ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 |  | 
|  | 185 | /** | 
|  | 186 | * \brief          Check a private RSA key | 
|  | 187 | * | 
|  | 188 | * \param ctx      RSA context to be checked | 
|  | 189 | * | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 190 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 192 | int rsa_check_privkey( const rsa_context *ctx ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 |  | 
|  | 194 | /** | 
|  | 195 | * \brief          Do an RSA public key operation | 
|  | 196 | * | 
|  | 197 | * \param ctx      RSA context | 
|  | 198 | * \param input    input buffer | 
|  | 199 | * \param output   output buffer | 
|  | 200 | * | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 201 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | * | 
|  | 203 | * \note           This function does NOT take care of message | 
| Paul Bakker | 619467a | 2009-03-28 23:26:51 +0000 | [diff] [blame] | 204 | *                 padding. Also, be sure to set input[0] = 0 or assure that | 
|  | 205 | *                 input is smaller than N. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | * | 
|  | 207 | * \note           The input and output buffers must be large | 
|  | 208 | *                 enough (eg. 128 bytes if RSA-1024 is used). | 
|  | 209 | */ | 
|  | 210 | int rsa_public( rsa_context *ctx, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 211 | const unsigned char *input, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | unsigned char *output ); | 
|  | 213 |  | 
|  | 214 | /** | 
|  | 215 | * \brief          Do an RSA private key operation | 
|  | 216 | * | 
|  | 217 | * \param ctx      RSA context | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 218 | * \param f_rng    RNG function (Needed for blinding) | 
|  | 219 | * \param p_rng    RNG parameter | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | * \param input    input buffer | 
|  | 221 | * \param output   output buffer | 
|  | 222 | * | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 223 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 | * | 
|  | 225 | * \note           The input and output buffers must be large | 
|  | 226 | *                 enough (eg. 128 bytes if RSA-1024 is used). | 
|  | 227 | */ | 
|  | 228 | int rsa_private( rsa_context *ctx, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 229 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 230 | void *p_rng, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 231 | const unsigned char *input, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 232 | unsigned char *output ); | 
|  | 233 |  | 
|  | 234 | /** | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 235 | * \brief          Generic wrapper to perform a PKCS#1 encryption using the | 
|  | 236 | *                 mode from the context. Add the message padding, then do an | 
|  | 237 | *                 RSA operation. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | * | 
|  | 239 | * \param ctx      RSA context | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 240 | * \param f_rng    RNG function (Needed for padding and PKCS#1 v2.1 encoding | 
|  | 241 | *                               and RSA_PRIVATE) | 
| Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 242 | * \param p_rng    RNG parameter | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | 592457c | 2009-04-01 19:01:43 +0000 | [diff] [blame] | 244 | * \param ilen     contains the plaintext length | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | * \param input    buffer holding the data to be encrypted | 
|  | 246 | * \param output   buffer that will hold the ciphertext | 
|  | 247 | * | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 248 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 249 | * | 
|  | 250 | * \note           The output buffer must be as large as the size | 
|  | 251 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
|  | 252 | */ | 
|  | 253 | int rsa_pkcs1_encrypt( rsa_context *ctx, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 254 | int (*f_rng)(void *, unsigned char *, size_t), | 
| Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 255 | void *p_rng, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 256 | int mode, size_t ilen, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 257 | const unsigned char *input, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 258 | unsigned char *output ); | 
|  | 259 |  | 
|  | 260 | /** | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 261 | * \brief          Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) | 
|  | 262 | * | 
|  | 263 | * \param ctx      RSA context | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 264 | * \param f_rng    RNG function (Needed for padding and RSA_PRIVATE) | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 265 | * \param p_rng    RNG parameter | 
|  | 266 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
|  | 267 | * \param ilen     contains the plaintext length | 
|  | 268 | * \param input    buffer holding the data to be encrypted | 
|  | 269 | * \param output   buffer that will hold the ciphertext | 
|  | 270 | * | 
|  | 271 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
|  | 272 | * | 
|  | 273 | * \note           The output buffer must be as large as the size | 
|  | 274 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
|  | 275 | */ | 
|  | 276 | int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx, | 
|  | 277 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 278 | void *p_rng, | 
|  | 279 | int mode, size_t ilen, | 
|  | 280 | const unsigned char *input, | 
|  | 281 | unsigned char *output ); | 
|  | 282 |  | 
|  | 283 | /** | 
|  | 284 | * \brief          Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) | 
|  | 285 | * | 
|  | 286 | * \param ctx      RSA context | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 287 | * \param f_rng    RNG function (Needed for padding and PKCS#1 v2.1 encoding | 
|  | 288 | *                               and RSA_PRIVATE) | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 289 | * \param p_rng    RNG parameter | 
|  | 290 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 291 | * \param label    buffer holding the custom label to use | 
|  | 292 | * \param label_len contains the label length | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 293 | * \param ilen     contains the plaintext length | 
|  | 294 | * \param input    buffer holding the data to be encrypted | 
|  | 295 | * \param output   buffer that will hold the ciphertext | 
|  | 296 | * | 
|  | 297 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
|  | 298 | * | 
|  | 299 | * \note           The output buffer must be as large as the size | 
|  | 300 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
|  | 301 | */ | 
|  | 302 | int rsa_rsaes_oaep_encrypt( rsa_context *ctx, | 
|  | 303 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 304 | void *p_rng, | 
| Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 305 | int mode, | 
|  | 306 | const unsigned char *label, size_t label_len, | 
|  | 307 | size_t ilen, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 308 | const unsigned char *input, | 
|  | 309 | unsigned char *output ); | 
|  | 310 |  | 
|  | 311 | /** | 
|  | 312 | * \brief          Generic wrapper to perform a PKCS#1 decryption using the | 
|  | 313 | *                 mode from the context. Do an RSA operation, then remove | 
|  | 314 | *                 the message padding | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 315 | * | 
|  | 316 | * \param ctx      RSA context | 
| Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 317 | * \param f_rng    RNG function (Only needed for RSA_PRIVATE) | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 318 | * \param p_rng    RNG parameter | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | 4d8ca70 | 2011-08-09 10:31:05 +0000 | [diff] [blame] | 320 | * \param olen     will contain the plaintext length | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 | * \param input    buffer holding the encrypted data | 
|  | 322 | * \param output   buffer that will hold the plaintext | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 323 | * \param output_max_len    maximum length of the output buffer | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 324 | * | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 325 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 326 | * | 
|  | 327 | * \note           The output buffer must be as large as the size | 
| Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 328 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise | 
|  | 329 | *                 an error is thrown. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | */ | 
|  | 331 | int rsa_pkcs1_decrypt( rsa_context *ctx, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 332 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 333 | void *p_rng, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 334 | int mode, size_t *olen, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 335 | const unsigned char *input, | 
| Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 336 | unsigned char *output, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 337 | size_t output_max_len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 |  | 
|  | 339 | /** | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 340 | * \brief          Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) | 
|  | 341 | * | 
|  | 342 | * \param ctx      RSA context | 
| Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 343 | * \param f_rng    RNG function (Only needed for RSA_PRIVATE) | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 344 | * \param p_rng    RNG parameter | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 345 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
|  | 346 | * \param olen     will contain the plaintext length | 
|  | 347 | * \param input    buffer holding the encrypted data | 
|  | 348 | * \param output   buffer that will hold the plaintext | 
|  | 349 | * \param output_max_len    maximum length of the output buffer | 
|  | 350 | * | 
|  | 351 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
|  | 352 | * | 
|  | 353 | * \note           The output buffer must be as large as the size | 
|  | 354 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise | 
|  | 355 | *                 an error is thrown. | 
|  | 356 | */ | 
|  | 357 | int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 358 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 359 | void *p_rng, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 360 | int mode, size_t *olen, | 
|  | 361 | const unsigned char *input, | 
|  | 362 | unsigned char *output, | 
|  | 363 | size_t output_max_len ); | 
|  | 364 |  | 
|  | 365 | /** | 
|  | 366 | * \brief          Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT) | 
|  | 367 | * | 
|  | 368 | * \param ctx      RSA context | 
| Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 369 | * \param f_rng    RNG function (Only needed for RSA_PRIVATE) | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 370 | * \param p_rng    RNG parameter | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 371 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 372 | * \param label    buffer holding the custom label to use | 
|  | 373 | * \param label_len contains the label length | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 374 | * \param olen     will contain the plaintext length | 
|  | 375 | * \param input    buffer holding the encrypted data | 
|  | 376 | * \param output   buffer that will hold the plaintext | 
|  | 377 | * \param output_max_len    maximum length of the output buffer | 
|  | 378 | * | 
|  | 379 | * \return         0 if successful, or an POLARSSL_ERR_RSA_XXX error code | 
|  | 380 | * | 
|  | 381 | * \note           The output buffer must be as large as the size | 
|  | 382 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise | 
|  | 383 | *                 an error is thrown. | 
|  | 384 | */ | 
|  | 385 | int rsa_rsaes_oaep_decrypt( rsa_context *ctx, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 386 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 387 | void *p_rng, | 
| Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 388 | int mode, | 
|  | 389 | const unsigned char *label, size_t label_len, | 
|  | 390 | size_t *olen, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 391 | const unsigned char *input, | 
|  | 392 | unsigned char *output, | 
|  | 393 | size_t output_max_len ); | 
|  | 394 |  | 
|  | 395 | /** | 
|  | 396 | * \brief          Generic wrapper to perform a PKCS#1 signature using the | 
|  | 397 | *                 mode from the context. Do a private RSA operation to sign | 
|  | 398 | *                 a message digest | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 399 | * | 
|  | 400 | * \param ctx      RSA context | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 401 | * \param f_rng    RNG function (Needed for PKCS#1 v2.1 encoding and for | 
|  | 402 | *                               RSA_PRIVATE) | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 403 | * \param p_rng    RNG parameter | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 404 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 405 | * \param md_alg   a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) | 
|  | 406 | * \param hashlen  message digest length (for POLARSSL_MD_NONE only) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 407 | * \param hash     buffer holding the message digest | 
|  | 408 | * \param sig      buffer that will hold the ciphertext | 
|  | 409 | * | 
|  | 410 | * \return         0 if the signing operation was successful, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 411 | *                 or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 412 | * | 
|  | 413 | * \note           The "sig" buffer must be as large as the size | 
|  | 414 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 415 | * | 
| Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 416 | * \note           In case of PKCS#1 v2.1 encoding, see comments on | 
|  | 417 | * \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] | 418 | */ | 
|  | 419 | int rsa_pkcs1_sign( rsa_context *ctx, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 420 | int (*f_rng)(void *, unsigned char *, size_t), | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 421 | void *p_rng, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 422 | int mode, | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 423 | md_type_t md_alg, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 424 | unsigned int hashlen, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 425 | const unsigned char *hash, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 426 | unsigned char *sig ); | 
|  | 427 |  | 
|  | 428 | /** | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 429 | * \brief          Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN) | 
|  | 430 | * | 
|  | 431 | * \param ctx      RSA context | 
| Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 432 | * \param f_rng    RNG function (Only needed for RSA_PRIVATE) | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 433 | * \param p_rng    RNG parameter | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 434 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 435 | * \param md_alg   a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) | 
|  | 436 | * \param hashlen  message digest length (for POLARSSL_MD_NONE only) | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 437 | * \param hash     buffer holding the message digest | 
|  | 438 | * \param sig      buffer that will hold the ciphertext | 
|  | 439 | * | 
|  | 440 | * \return         0 if the signing operation was successful, | 
|  | 441 | *                 or an POLARSSL_ERR_RSA_XXX error code | 
|  | 442 | * | 
|  | 443 | * \note           The "sig" buffer must be as large as the size | 
|  | 444 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
|  | 445 | */ | 
|  | 446 | int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 447 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 448 | void *p_rng, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 449 | int mode, | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 450 | md_type_t md_alg, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 451 | unsigned int hashlen, | 
|  | 452 | const unsigned char *hash, | 
|  | 453 | unsigned char *sig ); | 
|  | 454 |  | 
|  | 455 | /** | 
|  | 456 | * \brief          Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) | 
|  | 457 | * | 
|  | 458 | * \param ctx      RSA context | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 459 | * \param f_rng    RNG function (Needed for PKCS#1 v2.1 encoding and for | 
|  | 460 | *                               RSA_PRIVATE) | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 461 | * \param p_rng    RNG parameter | 
|  | 462 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 463 | * \param md_alg   a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) | 
|  | 464 | * \param hashlen  message digest length (for POLARSSL_MD_NONE only) | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 465 | * \param hash     buffer holding the message digest | 
|  | 466 | * \param sig      buffer that will hold the ciphertext | 
|  | 467 | * | 
|  | 468 | * \return         0 if the signing operation was successful, | 
|  | 469 | *                 or an POLARSSL_ERR_RSA_XXX error code | 
|  | 470 | * | 
|  | 471 | * \note           The "sig" buffer must be as large as the size | 
|  | 472 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
|  | 473 | * | 
| Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 474 | * \note           The hash_id in the RSA context is the one used for the | 
|  | 475 | *                 encoding. md_alg in the function call is the type of hash | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 476 | *                 that is encoded. According to RFC 3447 it is advised to | 
|  | 477 | *                 keep both hashes the same. | 
|  | 478 | */ | 
|  | 479 | int rsa_rsassa_pss_sign( rsa_context *ctx, | 
|  | 480 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 481 | void *p_rng, | 
|  | 482 | int mode, | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 483 | md_type_t md_alg, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 484 | unsigned int hashlen, | 
|  | 485 | const unsigned char *hash, | 
|  | 486 | unsigned char *sig ); | 
|  | 487 |  | 
|  | 488 | /** | 
|  | 489 | * \brief          Generic wrapper to perform a PKCS#1 verification using the | 
|  | 490 | *                 mode from the context. Do a public RSA operation and check | 
|  | 491 | *                 the message digest | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 492 | * | 
|  | 493 | * \param ctx      points to an RSA public key | 
| Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 494 | * \param f_rng    RNG function (Only needed for RSA_PRIVATE) | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 495 | * \param p_rng    RNG parameter | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 496 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 497 | * \param md_alg   a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) | 
|  | 498 | * \param hashlen  message digest length (for POLARSSL_MD_NONE only) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 499 | * \param hash     buffer holding the message digest | 
|  | 500 | * \param sig      buffer holding the ciphertext | 
|  | 501 | * | 
|  | 502 | * \return         0 if the verify operation was successful, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 503 | *                 or an POLARSSL_ERR_RSA_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 504 | * | 
|  | 505 | * \note           The "sig" buffer must be as large as the size | 
|  | 506 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
| Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 507 | * | 
| Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 508 | * \note           In case of PKCS#1 v2.1 encoding, see comments on | 
|  | 509 | *                 \c rsa_rsassa_pss_verify() about md_alg and hash_id. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 510 | */ | 
|  | 511 | int rsa_pkcs1_verify( rsa_context *ctx, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 512 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 513 | void *p_rng, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 514 | int mode, | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 515 | md_type_t md_alg, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 516 | unsigned int hashlen, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 517 | const unsigned char *hash, | 
| Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 518 | const unsigned char *sig ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 519 |  | 
|  | 520 | /** | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 521 | * \brief          Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) | 
|  | 522 | * | 
|  | 523 | * \param ctx      points to an RSA public key | 
| Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 524 | * \param f_rng    RNG function (Only needed for RSA_PRIVATE) | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 525 | * \param p_rng    RNG parameter | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 526 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 527 | * \param md_alg   a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) | 
|  | 528 | * \param hashlen  message digest length (for POLARSSL_MD_NONE only) | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 529 | * \param hash     buffer holding the message digest | 
|  | 530 | * \param sig      buffer holding the ciphertext | 
|  | 531 | * | 
|  | 532 | * \return         0 if the verify operation was successful, | 
|  | 533 | *                 or an POLARSSL_ERR_RSA_XXX error code | 
|  | 534 | * | 
|  | 535 | * \note           The "sig" buffer must be as large as the size | 
|  | 536 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
|  | 537 | */ | 
|  | 538 | int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 539 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 540 | void *p_rng, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 541 | int mode, | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 542 | md_type_t md_alg, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 543 | unsigned int hashlen, | 
|  | 544 | const unsigned char *hash, | 
| Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 545 | const unsigned char *sig ); | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 546 |  | 
|  | 547 | /** | 
|  | 548 | * \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] | 549 | *                 (This is the "simple" version.) | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 550 | * | 
|  | 551 | * \param ctx      points to an RSA public key | 
| Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 552 | * \param f_rng    RNG function (Only needed for RSA_PRIVATE) | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 553 | * \param p_rng    RNG parameter | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 554 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 555 | * \param md_alg   a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) | 
|  | 556 | * \param hashlen  message digest length (for POLARSSL_MD_NONE only) | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 557 | * \param hash     buffer holding the message digest | 
|  | 558 | * \param sig      buffer holding the ciphertext | 
|  | 559 | * | 
|  | 560 | * \return         0 if the verify operation was successful, | 
|  | 561 | *                 or an POLARSSL_ERR_RSA_XXX error code | 
|  | 562 | * | 
|  | 563 | * \note           The "sig" buffer must be as large as the size | 
|  | 564 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
|  | 565 | * | 
| Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 566 | * \note           The hash_id in the RSA context is the one used for the | 
|  | 567 | *                 verification. md_alg in the function call is the type of | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 568 | *                 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] | 569 | *                 keep both hashes the same. If hash_id in the RSA context is | 
|  | 570 | *                 unset, the md_alg from the function call is used. | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 571 | */ | 
|  | 572 | int rsa_rsassa_pss_verify( rsa_context *ctx, | 
| Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 573 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 574 | void *p_rng, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 575 | int mode, | 
| Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 576 | md_type_t md_alg, | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 577 | unsigned int hashlen, | 
|  | 578 | const unsigned char *hash, | 
| Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 579 | const unsigned char *sig ); | 
| Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 580 |  | 
|  | 581 | /** | 
| Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 582 | * \brief          Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) | 
|  | 583 | *                 (This is the version with "full" options.) | 
|  | 584 | * | 
|  | 585 | * \param ctx      points to an RSA public key | 
|  | 586 | * \param f_rng    RNG function (Only needed for RSA_PRIVATE) | 
|  | 587 | * \param p_rng    RNG parameter | 
|  | 588 | * \param mode     RSA_PUBLIC or RSA_PRIVATE | 
|  | 589 | * \param md_alg   a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) | 
|  | 590 | * \param hashlen  message digest length (for POLARSSL_MD_NONE only) | 
|  | 591 | * \param hash     buffer holding the message digest | 
|  | 592 | * \param mgf1_hash_id message digest used for mask generation | 
|  | 593 | * \param expected_salt_len Length of the salt used in padding, use | 
|  | 594 | *                 RSA_SALT_LEN_ANY to accept any salt length | 
|  | 595 | * \param sig      buffer holding the ciphertext | 
|  | 596 | * | 
|  | 597 | * \return         0 if the verify operation was successful, | 
|  | 598 | *                 or an POLARSSL_ERR_RSA_XXX error code | 
|  | 599 | * | 
|  | 600 | * \note           The "sig" buffer must be as large as the size | 
|  | 601 | *                 of ctx->N (eg. 128 bytes if RSA-1024 is used). | 
|  | 602 | * | 
|  | 603 | * \note           The hash_id in the RSA context is ignored. | 
|  | 604 | */ | 
|  | 605 | int rsa_rsassa_pss_verify_ext( rsa_context *ctx, | 
|  | 606 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 607 | void *p_rng, | 
|  | 608 | int mode, | 
|  | 609 | md_type_t md_alg, | 
|  | 610 | unsigned int hashlen, | 
|  | 611 | const unsigned char *hash, | 
|  | 612 | md_type_t mgf1_hash_id, | 
|  | 613 | int expected_salt_len, | 
|  | 614 | const unsigned char *sig ); | 
|  | 615 |  | 
|  | 616 | /** | 
| Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 617 | * \brief          Copy the components of an RSA context | 
|  | 618 | * | 
|  | 619 | * \param dst      Destination context | 
|  | 620 | * \param src      Source context | 
|  | 621 | * | 
|  | 622 | * \return         O on success, | 
|  | 623 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED on memory allocation failure | 
|  | 624 | */ | 
|  | 625 | int rsa_copy( rsa_context *dst, const rsa_context *src ); | 
|  | 626 |  | 
|  | 627 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 628 | * \brief          Free the components of an RSA key | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 629 | * | 
|  | 630 | * \param ctx      RSA Context to free | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 631 | */ | 
|  | 632 | void rsa_free( rsa_context *ctx ); | 
|  | 633 |  | 
|  | 634 | /** | 
|  | 635 | * \brief          Checkup routine | 
|  | 636 | * | 
|  | 637 | * \return         0 if successful, or 1 if the test failed | 
|  | 638 | */ | 
|  | 639 | int rsa_self_test( int verbose ); | 
|  | 640 |  | 
|  | 641 | #ifdef __cplusplus | 
|  | 642 | } | 
|  | 643 | #endif | 
|  | 644 |  | 
| Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 645 | #endif /* POLARSSL_RSA_C */ | 
|  | 646 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 647 | #endif /* rsa.h */ |