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