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 | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2010, 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 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 30 | #include "config.h" |
| 31 | |
| 32 | #if defined(POLARSSL_RSA_C) |
| 33 | |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 34 | #include "bignum.h" |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 35 | #include "md.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 37 | /* |
| 38 | * RSA Error codes |
| 39 | */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 40 | #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */ |
| 41 | #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */ |
| 42 | #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */ |
| 43 | #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */ |
| 44 | #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */ |
| 45 | #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */ |
| 46 | #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */ |
| 47 | #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */ |
| 48 | #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] | 49 | |
| 50 | /* |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 51 | * RSA constants |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | #define RSA_PUBLIC 0 |
| 54 | #define RSA_PRIVATE 1 |
| 55 | |
| 56 | #define RSA_PKCS_V15 0 |
| 57 | #define RSA_PKCS_V21 1 |
| 58 | |
| 59 | #define RSA_SIGN 1 |
| 60 | #define RSA_CRYPT 2 |
| 61 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | /** |
| 63 | * \brief RSA context structure |
| 64 | */ |
| 65 | typedef struct |
| 66 | { |
| 67 | int ver; /*!< always 0 */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 68 | size_t len; /*!< size(N) in chars */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
| 70 | mpi N; /*!< public modulus */ |
| 71 | mpi E; /*!< public exponent */ |
| 72 | |
| 73 | mpi D; /*!< private exponent */ |
| 74 | mpi P; /*!< 1st prime factor */ |
| 75 | mpi Q; /*!< 2nd prime factor */ |
| 76 | mpi DP; /*!< D % (P - 1) */ |
| 77 | mpi DQ; /*!< D % (Q - 1) */ |
| 78 | mpi QP; /*!< 1 / (Q % P) */ |
| 79 | |
| 80 | mpi RN; /*!< cached R^2 mod N */ |
| 81 | mpi RP; /*!< cached R^2 mod P */ |
| 82 | mpi RQ; /*!< cached R^2 mod Q */ |
| 83 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 84 | int padding; /*!< RSA_PKCS_V15 for 1.5 padding and |
| 85 | RSA_PKCS_v21 for OAEP/PSS */ |
| 86 | int hash_id; /*!< Hash identifier of md_type_t as |
| 87 | specified in the md.h header file |
| 88 | for the EME-OAEP and EMSA-PSS |
| 89 | encoding */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | } |
| 91 | rsa_context; |
| 92 | |
| 93 | #ifdef __cplusplus |
| 94 | extern "C" { |
| 95 | #endif |
| 96 | |
| 97 | /** |
| 98 | * \brief Initialize an RSA context |
| 99 | * |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 100 | * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP |
| 101 | * encryption scheme and the RSASSA-PSS signature scheme. |
| 102 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | * \param ctx RSA context to be initialized |
| 104 | * \param padding RSA_PKCS_V15 or RSA_PKCS_V21 |
| 105 | * \param hash_id RSA_PKCS_V21 hash identifier |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | * |
| 107 | * \note The hash_id parameter is actually ignored |
| 108 | * when using RSA_PKCS_V15 padding. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | */ |
| 110 | void rsa_init( rsa_context *ctx, |
| 111 | int padding, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 112 | int hash_id); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * \brief Generate an RSA keypair |
| 116 | * |
| 117 | * \param ctx RSA context that will hold the key |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 118 | * \param f_rng RNG function |
| 119 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | * \param nbits size of the public key in bits |
| 121 | * \param exponent public exponent (e.g., 65537) |
| 122 | * |
| 123 | * \note rsa_init() must be called beforehand to setup |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 124 | * the RSA context. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 126 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | */ |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 128 | int rsa_gen_key( rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 129 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 130 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 131 | unsigned int nbits, int exponent ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * \brief Check a public RSA key |
| 135 | * |
| 136 | * \param ctx RSA context to be checked |
| 137 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 138 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 140 | int rsa_check_pubkey( const rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * \brief Check a private RSA key |
| 144 | * |
| 145 | * \param ctx RSA context to be checked |
| 146 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 147 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 149 | int rsa_check_privkey( const rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * \brief Do an RSA public key operation |
| 153 | * |
| 154 | * \param ctx RSA context |
| 155 | * \param input input buffer |
| 156 | * \param output output buffer |
| 157 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 158 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | * |
| 160 | * \note This function does NOT take care of message |
Paul Bakker | 619467a | 2009-03-28 23:26:51 +0000 | [diff] [blame] | 161 | * padding. Also, be sure to set input[0] = 0 or assure that |
| 162 | * input is smaller than N. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | * |
| 164 | * \note The input and output buffers must be large |
| 165 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 166 | */ |
| 167 | int rsa_public( rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 168 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | unsigned char *output ); |
| 170 | |
| 171 | /** |
| 172 | * \brief Do an RSA private key operation |
| 173 | * |
| 174 | * \param ctx RSA context |
| 175 | * \param input input buffer |
| 176 | * \param output output buffer |
| 177 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 178 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | * |
| 180 | * \note The input and output buffers must be large |
| 181 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 182 | */ |
| 183 | int rsa_private( rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 184 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | unsigned char *output ); |
| 186 | |
| 187 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 188 | * \brief Generic wrapper to perform a PKCS#1 encryption using the |
| 189 | * mode from the context. Add the message padding, then do an |
| 190 | * RSA operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | * |
| 192 | * \param ctx RSA context |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 193 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding) |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 194 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | 592457c | 2009-04-01 19:01:43 +0000 | [diff] [blame] | 196 | * \param ilen contains the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 197 | * \param input buffer holding the data to be encrypted |
| 198 | * \param output buffer that will hold the ciphertext |
| 199 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 200 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | * |
| 202 | * \note The output buffer must be as large as the size |
| 203 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 204 | */ |
| 205 | int rsa_pkcs1_encrypt( rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 206 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 207 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 208 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 209 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | unsigned char *output ); |
| 211 | |
| 212 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 213 | * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) |
| 214 | * |
| 215 | * \param ctx RSA context |
| 216 | * \param f_rng RNG function (Needed for padding) |
| 217 | * \param p_rng RNG parameter |
| 218 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 219 | * \param ilen contains the plaintext length |
| 220 | * \param input buffer holding the data to be encrypted |
| 221 | * \param output buffer that will hold the ciphertext |
| 222 | * |
| 223 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 224 | * |
| 225 | * \note The output buffer must be as large as the size |
| 226 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 227 | */ |
| 228 | int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx, |
| 229 | int (*f_rng)(void *, unsigned char *, size_t), |
| 230 | void *p_rng, |
| 231 | int mode, size_t ilen, |
| 232 | const unsigned char *input, |
| 233 | unsigned char *output ); |
| 234 | |
| 235 | /** |
| 236 | * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) |
| 237 | * |
| 238 | * \param ctx RSA context |
| 239 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding) |
| 240 | * \param p_rng RNG parameter |
| 241 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 242 | * \param label buffer holding the custom label to use |
| 243 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 244 | * \param ilen contains the plaintext length |
| 245 | * \param input buffer holding the data to be encrypted |
| 246 | * \param output buffer that will hold the ciphertext |
| 247 | * |
| 248 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 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_rsaes_oaep_encrypt( rsa_context *ctx, |
| 254 | int (*f_rng)(void *, unsigned char *, size_t), |
| 255 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 256 | int mode, |
| 257 | const unsigned char *label, size_t label_len, |
| 258 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 259 | const unsigned char *input, |
| 260 | unsigned char *output ); |
| 261 | |
| 262 | /** |
| 263 | * \brief Generic wrapper to perform a PKCS#1 decryption using the |
| 264 | * mode from the context. Do an RSA operation, then remove |
| 265 | * the message padding |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 266 | * |
| 267 | * \param ctx RSA context |
| 268 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | 4d8ca70 | 2011-08-09 10:31:05 +0000 | [diff] [blame] | 269 | * \param olen will contain the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 | * \param input buffer holding the encrypted data |
| 271 | * \param output buffer that will hold the plaintext |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 272 | * \param output_max_len maximum length of the output buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 274 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 | * |
| 276 | * \note The output buffer must be as large as the size |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 277 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 278 | * an error is thrown. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | */ |
| 280 | int rsa_pkcs1_decrypt( rsa_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 281 | int mode, size_t *olen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 282 | const unsigned char *input, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 283 | unsigned char *output, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 284 | size_t output_max_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 285 | |
| 286 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 287 | * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) |
| 288 | * |
| 289 | * \param ctx RSA context |
| 290 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 291 | * \param olen will contain the plaintext length |
| 292 | * \param input buffer holding the encrypted data |
| 293 | * \param output buffer that will hold the plaintext |
| 294 | * \param output_max_len maximum length of the output buffer |
| 295 | * |
| 296 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 297 | * |
| 298 | * \note The output buffer must be as large as the size |
| 299 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 300 | * an error is thrown. |
| 301 | */ |
| 302 | int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx, |
| 303 | int mode, size_t *olen, |
| 304 | const unsigned char *input, |
| 305 | unsigned char *output, |
| 306 | size_t output_max_len ); |
| 307 | |
| 308 | /** |
| 309 | * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT) |
| 310 | * |
| 311 | * \param ctx RSA context |
| 312 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 313 | * \param label buffer holding the custom label to use |
| 314 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 315 | * \param olen will contain the plaintext length |
| 316 | * \param input buffer holding the encrypted data |
| 317 | * \param output buffer that will hold the plaintext |
| 318 | * \param output_max_len maximum length of the output buffer |
| 319 | * |
| 320 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 321 | * |
| 322 | * \note The output buffer must be as large as the size |
| 323 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 324 | * an error is thrown. |
| 325 | */ |
| 326 | int rsa_rsaes_oaep_decrypt( rsa_context *ctx, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 327 | int mode, |
| 328 | const unsigned char *label, size_t label_len, |
| 329 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 330 | const unsigned char *input, |
| 331 | unsigned char *output, |
| 332 | size_t output_max_len ); |
| 333 | |
| 334 | /** |
| 335 | * \brief Generic wrapper to perform a PKCS#1 signature using the |
| 336 | * mode from the context. Do a private RSA operation to sign |
| 337 | * a message digest |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | * |
| 339 | * \param ctx RSA context |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 340 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding) |
| 341 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 342 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 343 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 344 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 345 | * \param hash buffer holding the message digest |
| 346 | * \param sig buffer that will hold the ciphertext |
| 347 | * |
| 348 | * \return 0 if the signing operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 349 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 350 | * |
| 351 | * \note The "sig" buffer must be as large as the size |
| 352 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 353 | * |
| 354 | * \note In case of PKCS#1 v2.1 encoding keep in mind that |
| 355 | * the hash_id in the RSA context is the one used for the |
| 356 | * encoding. hash_id in the function call is the type of hash |
| 357 | * that is encoded. According to RFC 3447 it is advised to |
| 358 | * keep both hashes the same. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 359 | */ |
| 360 | int rsa_pkcs1_sign( rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 361 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 362 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 364 | md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 365 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 366 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 367 | unsigned char *sig ); |
| 368 | |
| 369 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 370 | * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN) |
| 371 | * |
| 372 | * \param ctx RSA context |
| 373 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 374 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 375 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 376 | * \param hash buffer holding the message digest |
| 377 | * \param sig buffer that will hold the ciphertext |
| 378 | * |
| 379 | * \return 0 if the signing operation was successful, |
| 380 | * or an POLARSSL_ERR_RSA_XXX error code |
| 381 | * |
| 382 | * \note The "sig" buffer must be as large as the size |
| 383 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 384 | */ |
| 385 | int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx, |
| 386 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 387 | md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 388 | unsigned int hashlen, |
| 389 | const unsigned char *hash, |
| 390 | unsigned char *sig ); |
| 391 | |
| 392 | /** |
| 393 | * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) |
| 394 | * |
| 395 | * \param ctx RSA context |
| 396 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding) |
| 397 | * \param p_rng RNG parameter |
| 398 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 399 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 400 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 401 | * \param hash buffer holding the message digest |
| 402 | * \param sig buffer that will hold the ciphertext |
| 403 | * |
| 404 | * \return 0 if the signing operation was successful, |
| 405 | * or an POLARSSL_ERR_RSA_XXX error code |
| 406 | * |
| 407 | * \note The "sig" buffer must be as large as the size |
| 408 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 409 | * |
| 410 | * \note In case of PKCS#1 v2.1 encoding keep in mind that |
| 411 | * the hash_id in the RSA context is the one used for the |
| 412 | * encoding. hash_id in the function call is the type of hash |
| 413 | * that is encoded. According to RFC 3447 it is advised to |
| 414 | * keep both hashes the same. |
| 415 | */ |
| 416 | int rsa_rsassa_pss_sign( rsa_context *ctx, |
| 417 | int (*f_rng)(void *, unsigned char *, size_t), |
| 418 | void *p_rng, |
| 419 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 420 | md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 421 | unsigned int hashlen, |
| 422 | const unsigned char *hash, |
| 423 | unsigned char *sig ); |
| 424 | |
| 425 | /** |
| 426 | * \brief Generic wrapper to perform a PKCS#1 verification using the |
| 427 | * mode from the context. Do a public RSA operation and check |
| 428 | * the message digest |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 429 | * |
| 430 | * \param ctx points to an RSA public key |
| 431 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 432 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 433 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 434 | * \param hash buffer holding the message digest |
| 435 | * \param sig buffer holding the ciphertext |
| 436 | * |
| 437 | * \return 0 if the verify operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 438 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 439 | * |
| 440 | * \note The "sig" buffer must be as large as the size |
| 441 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 442 | * |
| 443 | * \note In case of PKCS#1 v2.1 encoding keep in mind that |
| 444 | * the hash_id in the RSA context is the one used for the |
| 445 | * verification. hash_id in the function call is the type of hash |
| 446 | * that is verified. According to RFC 3447 it is advised to |
| 447 | * keep both hashes the same. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 448 | */ |
| 449 | int rsa_pkcs1_verify( rsa_context *ctx, |
| 450 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 451 | md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 452 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 453 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 454 | unsigned char *sig ); |
| 455 | |
| 456 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 457 | * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) |
| 458 | * |
| 459 | * \param ctx points to an RSA public key |
| 460 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 461 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 462 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 463 | * \param hash buffer holding the message digest |
| 464 | * \param sig buffer holding the ciphertext |
| 465 | * |
| 466 | * \return 0 if the verify operation was successful, |
| 467 | * or an POLARSSL_ERR_RSA_XXX error code |
| 468 | * |
| 469 | * \note The "sig" buffer must be as large as the size |
| 470 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 471 | */ |
| 472 | int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx, |
| 473 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 474 | md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 475 | unsigned int hashlen, |
| 476 | const unsigned char *hash, |
| 477 | unsigned char *sig ); |
| 478 | |
| 479 | /** |
| 480 | * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) |
| 481 | * \brief Do a public RSA and check the message digest |
| 482 | * |
| 483 | * \param ctx points to an RSA public key |
| 484 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 485 | * \param md_alg a POLARSSL_MD_* (use POLARSSL_MD_NONE for signing raw data) |
| 486 | * \param hashlen message digest length (for POLARSSL_MD_NONE only) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 487 | * \param hash buffer holding the message digest |
| 488 | * \param sig buffer holding the ciphertext |
| 489 | * |
| 490 | * \return 0 if the verify operation was successful, |
| 491 | * or an POLARSSL_ERR_RSA_XXX error code |
| 492 | * |
| 493 | * \note The "sig" buffer must be as large as the size |
| 494 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 495 | * |
| 496 | * \note In case of PKCS#1 v2.1 encoding keep in mind that |
| 497 | * the hash_id in the RSA context is the one used for the |
| 498 | * verification. hash_id in the function call is the type of hash |
| 499 | * that is verified. According to RFC 3447 it is advised to |
| 500 | * keep both hashes the same. |
| 501 | */ |
| 502 | int rsa_rsassa_pss_verify( rsa_context *ctx, |
| 503 | int mode, |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 504 | md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 505 | unsigned int hashlen, |
| 506 | const unsigned char *hash, |
| 507 | unsigned char *sig ); |
| 508 | |
| 509 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 510 | * \brief Free the components of an RSA key |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 511 | * |
| 512 | * \param ctx RSA Context to free |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 513 | */ |
| 514 | void rsa_free( rsa_context *ctx ); |
| 515 | |
| 516 | /** |
| 517 | * \brief Checkup routine |
| 518 | * |
| 519 | * \return 0 if successful, or 1 if the test failed |
| 520 | */ |
| 521 | int rsa_self_test( int verbose ); |
| 522 | |
| 523 | #ifdef __cplusplus |
| 524 | } |
| 525 | #endif |
| 526 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 527 | #endif /* POLARSSL_RSA_C */ |
| 528 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 529 | #endif /* rsa.h */ |