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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 32 | /* |
| 33 | * RSA Error codes |
| 34 | */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 35 | #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */ |
| 36 | #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */ |
| 37 | #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */ |
| 38 | #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */ |
| 39 | #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */ |
| 40 | #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */ |
| 41 | #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */ |
| 42 | #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */ |
| 43 | #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] | 44 | |
| 45 | /* |
| 46 | * PKCS#1 constants |
| 47 | */ |
Paul Bakker | fc22c44 | 2009-07-19 20:36:27 +0000 | [diff] [blame] | 48 | #define SIG_RSA_RAW 0 |
| 49 | #define SIG_RSA_MD2 2 |
| 50 | #define SIG_RSA_MD4 3 |
| 51 | #define SIG_RSA_MD5 4 |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 52 | #define SIG_RSA_SHA1 5 |
| 53 | #define SIG_RSA_SHA224 14 |
| 54 | #define SIG_RSA_SHA256 11 |
| 55 | #define SIG_RSA_SHA384 12 |
| 56 | #define SIG_RSA_SHA512 13 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | |
| 58 | #define RSA_PUBLIC 0 |
| 59 | #define RSA_PRIVATE 1 |
| 60 | |
| 61 | #define RSA_PKCS_V15 0 |
| 62 | #define RSA_PKCS_V21 1 |
| 63 | |
| 64 | #define RSA_SIGN 1 |
| 65 | #define RSA_CRYPT 2 |
| 66 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 67 | #define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30" |
| 68 | #define ASN1_STR_NULL "\x05" |
| 69 | #define ASN1_STR_OID "\x06" |
| 70 | #define ASN1_STR_OCTET_STRING "\x04" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 71 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 72 | #define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00" |
| 73 | #define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a" |
| 74 | #define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 75 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 76 | #define OID_ISO_MEMBER_BODIES "\x2a" |
| 77 | #define OID_ISO_IDENTIFIED_ORG "\x2b" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * ISO Member bodies OID parts |
| 81 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 82 | #define OID_COUNTRY_US "\x86\x48" |
| 83 | #define OID_RSA_DATA_SECURITY "\x86\xf7\x0d" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * ISO Identified organization OID parts |
| 87 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 88 | #define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a" |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 89 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | /* |
| 91 | * DigestInfo ::= SEQUENCE { |
| 92 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 93 | * digest Digest } |
| 94 | * |
| 95 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 96 | * |
| 97 | * Digest ::= OCTET STRING |
| 98 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 99 | #define ASN1_HASH_MDX \ |
| 100 | ( \ |
| 101 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \ |
| 102 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \ |
| 103 | ASN1_STR_OID "\x08" \ |
| 104 | OID_DIGEST_ALG_MDX \ |
| 105 | ASN1_STR_NULL "\x00" \ |
| 106 | ASN1_STR_OCTET_STRING "\x10" \ |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 107 | ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 109 | #define ASN1_HASH_SHA1 \ |
| 110 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \ |
| 111 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \ |
| 112 | ASN1_STR_OID "\x05" \ |
| 113 | OID_HASH_ALG_SHA1 \ |
| 114 | ASN1_STR_NULL "\x00" \ |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 115 | ASN1_STR_OCTET_STRING "\x14" |
| 116 | |
Paul Bakker | 56a7684 | 2012-03-22 15:31:27 +0000 | [diff] [blame] | 117 | #define ASN1_HASH_SHA1_ALT \ |
| 118 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x1F" \ |
| 119 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x07" \ |
| 120 | ASN1_STR_OID "\x05" \ |
| 121 | OID_HASH_ALG_SHA1 \ |
| 122 | ASN1_STR_OCTET_STRING "\x14" |
| 123 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 124 | #define ASN1_HASH_SHA2X \ |
| 125 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \ |
| 126 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \ |
| 127 | ASN1_STR_OID "\x09" \ |
| 128 | OID_HASH_ALG_SHA2X \ |
| 129 | ASN1_STR_NULL "\x00" \ |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 130 | ASN1_STR_OCTET_STRING "\x00" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * \brief RSA context structure |
| 134 | */ |
| 135 | typedef struct |
| 136 | { |
| 137 | int ver; /*!< always 0 */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 138 | size_t len; /*!< size(N) in chars */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | |
| 140 | mpi N; /*!< public modulus */ |
| 141 | mpi E; /*!< public exponent */ |
| 142 | |
| 143 | mpi D; /*!< private exponent */ |
| 144 | mpi P; /*!< 1st prime factor */ |
| 145 | mpi Q; /*!< 2nd prime factor */ |
| 146 | mpi DP; /*!< D % (P - 1) */ |
| 147 | mpi DQ; /*!< D % (Q - 1) */ |
| 148 | mpi QP; /*!< 1 / (Q % P) */ |
| 149 | |
| 150 | mpi RN; /*!< cached R^2 mod N */ |
| 151 | mpi RP; /*!< cached R^2 mod P */ |
| 152 | mpi RQ; /*!< cached R^2 mod Q */ |
| 153 | |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 154 | #if !defined(POLARSSL_RSA_NO_CRT) |
| 155 | mpi Vi; /*!< cached blinding value */ |
| 156 | mpi Vf; /*!< cached un-blinding value */ |
| 157 | #endif |
| 158 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 159 | int padding; /*!< RSA_PKCS_V15 for 1.5 padding and |
| 160 | RSA_PKCS_v21 for OAEP/PSS */ |
| 161 | int hash_id; /*!< Hash identifier of md_type_t as |
| 162 | specified in the md.h header file |
| 163 | for the EME-OAEP and EMSA-PSS |
| 164 | encoding */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | } |
| 166 | rsa_context; |
| 167 | |
| 168 | #ifdef __cplusplus |
| 169 | extern "C" { |
| 170 | #endif |
| 171 | |
| 172 | /** |
| 173 | * \brief Initialize an RSA context |
| 174 | * |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 175 | * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP |
| 176 | * encryption scheme and the RSASSA-PSS signature scheme. |
| 177 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | * \param ctx RSA context to be initialized |
| 179 | * \param padding RSA_PKCS_V15 or RSA_PKCS_V21 |
| 180 | * \param hash_id RSA_PKCS_V21 hash identifier |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | * |
| 182 | * \note The hash_id parameter is actually ignored |
| 183 | * when using RSA_PKCS_V15 padding. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | */ |
| 185 | void rsa_init( rsa_context *ctx, |
| 186 | int padding, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 187 | int hash_id); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * \brief Generate an RSA keypair |
| 191 | * |
| 192 | * \param ctx RSA context that will hold the key |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 193 | * \param f_rng RNG function |
| 194 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 | * \param nbits size of the public key in bits |
| 196 | * \param exponent public exponent (e.g., 65537) |
| 197 | * |
| 198 | * \note rsa_init() must be called beforehand to setup |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 199 | * the RSA context. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | */ |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 203 | int rsa_gen_key( rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 204 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 205 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 206 | unsigned int nbits, int exponent ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 207 | |
| 208 | /** |
| 209 | * \brief Check a public RSA key |
| 210 | * |
| 211 | * \param ctx RSA context to be checked |
| 212 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 213 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 215 | int rsa_check_pubkey( const rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * \brief Check a private RSA key |
| 219 | * |
| 220 | * \param ctx RSA context to be checked |
| 221 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 222 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 223 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 224 | int rsa_check_privkey( const rsa_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * \brief Do an RSA public key operation |
| 228 | * |
| 229 | * \param ctx RSA context |
| 230 | * \param input input buffer |
| 231 | * \param output output buffer |
| 232 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 233 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 234 | * |
| 235 | * \note This function does NOT take care of message |
Paul Bakker | 619467a | 2009-03-28 23:26:51 +0000 | [diff] [blame] | 236 | * padding. Also, be sure to set input[0] = 0 or assure that |
| 237 | * input is smaller than N. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | * |
| 239 | * \note The input and output buffers must be large |
| 240 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 241 | */ |
| 242 | int rsa_public( rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 243 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | unsigned char *output ); |
| 245 | |
| 246 | /** |
| 247 | * \brief Do an RSA private key operation |
| 248 | * |
| 249 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 250 | * \param f_rng RNG function (Needed for blinding) |
| 251 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 | * \param input input buffer |
| 253 | * \param output output buffer |
| 254 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 255 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 256 | * |
| 257 | * \note The input and output buffers must be large |
| 258 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 259 | */ |
| 260 | int rsa_private( rsa_context *ctx, |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 261 | int (*f_rng)(void *, unsigned char *, size_t), |
| 262 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 263 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | unsigned char *output ); |
| 265 | |
| 266 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 267 | * \brief Generic wrapper to perform a PKCS#1 encryption using the |
| 268 | * mode from the context. Add the message padding, then do an |
| 269 | * RSA operation. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 | * |
| 271 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 272 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding |
| 273 | * and RSA_PRIVATE) |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 274 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | 592457c | 2009-04-01 19:01:43 +0000 | [diff] [blame] | 276 | * \param ilen contains the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 277 | * \param input buffer holding the data to be encrypted |
| 278 | * \param output buffer that will hold the ciphertext |
| 279 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 280 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 281 | * |
| 282 | * \note The output buffer must be as large as the size |
| 283 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 284 | */ |
| 285 | int rsa_pkcs1_encrypt( rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 286 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 287 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 288 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 289 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | unsigned char *output ); |
| 291 | |
| 292 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 293 | * \brief Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT) |
| 294 | * |
| 295 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 296 | * \param f_rng RNG function (Needed for padding and RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 297 | * \param p_rng RNG parameter |
| 298 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 299 | * \param ilen contains the plaintext length |
| 300 | * \param input buffer holding the data to be encrypted |
| 301 | * \param output buffer that will hold the ciphertext |
| 302 | * |
| 303 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 304 | * |
| 305 | * \note The output buffer must be as large as the size |
| 306 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 307 | */ |
| 308 | int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx, |
| 309 | int (*f_rng)(void *, unsigned char *, size_t), |
| 310 | void *p_rng, |
| 311 | int mode, size_t ilen, |
| 312 | const unsigned char *input, |
| 313 | unsigned char *output ); |
| 314 | |
| 315 | /** |
| 316 | * \brief Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT) |
| 317 | * |
| 318 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 319 | * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding |
| 320 | * and RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 321 | * \param p_rng RNG parameter |
| 322 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 323 | * \param label buffer holding the custom label to use |
| 324 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 325 | * \param ilen contains the plaintext length |
| 326 | * \param input buffer holding the data to be encrypted |
| 327 | * \param output buffer that will hold the ciphertext |
| 328 | * |
| 329 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 330 | * |
| 331 | * \note The output buffer must be as large as the size |
| 332 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 333 | */ |
| 334 | int rsa_rsaes_oaep_encrypt( rsa_context *ctx, |
| 335 | int (*f_rng)(void *, unsigned char *, size_t), |
| 336 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 337 | int mode, |
| 338 | const unsigned char *label, size_t label_len, |
| 339 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 340 | const unsigned char *input, |
| 341 | unsigned char *output ); |
| 342 | |
| 343 | /** |
| 344 | * \brief Generic wrapper to perform a PKCS#1 decryption using the |
| 345 | * mode from the context. Do an RSA operation, then remove |
| 346 | * the message padding |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 347 | * |
| 348 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 349 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
| 350 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 351 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | 4d8ca70 | 2011-08-09 10:31:05 +0000 | [diff] [blame] | 352 | * \param olen will contain the plaintext length |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 353 | * \param input buffer holding the encrypted data |
| 354 | * \param output buffer that will hold the plaintext |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 355 | * \param output_max_len maximum length of the output buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 357 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 358 | * |
| 359 | * \note The output buffer must be as large as the size |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 360 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 361 | * an error is thrown. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 362 | */ |
| 363 | int rsa_pkcs1_decrypt( rsa_context *ctx, |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 364 | int (*f_rng)(void *, unsigned char *, size_t), |
| 365 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 366 | int mode, size_t *olen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 367 | const unsigned char *input, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 368 | unsigned char *output, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 369 | size_t output_max_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 370 | |
| 371 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 372 | * \brief Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT) |
| 373 | * |
| 374 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 375 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
| 376 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 377 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 378 | * \param olen will contain the plaintext length |
| 379 | * \param input buffer holding the encrypted data |
| 380 | * \param output buffer that will hold the plaintext |
| 381 | * \param output_max_len maximum length of the output buffer |
| 382 | * |
| 383 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 384 | * |
| 385 | * \note The output buffer must be as large as the size |
| 386 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 387 | * an error is thrown. |
| 388 | */ |
| 389 | int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx, |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 390 | int (*f_rng)(void *, unsigned char *, size_t), |
| 391 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 392 | int mode, size_t *olen, |
| 393 | const unsigned char *input, |
| 394 | unsigned char *output, |
| 395 | size_t output_max_len ); |
| 396 | |
| 397 | /** |
| 398 | * \brief Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT) |
| 399 | * |
| 400 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 401 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
| 402 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 403 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 404 | * \param label buffer holding the custom label to use |
| 405 | * \param label_len contains the label length |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 406 | * \param olen will contain the plaintext length |
| 407 | * \param input buffer holding the encrypted data |
| 408 | * \param output buffer that will hold the plaintext |
| 409 | * \param output_max_len maximum length of the output buffer |
| 410 | * |
| 411 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
| 412 | * |
| 413 | * \note The output buffer must be as large as the size |
| 414 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 415 | * an error is thrown. |
| 416 | */ |
| 417 | int rsa_rsaes_oaep_decrypt( rsa_context *ctx, |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 418 | int (*f_rng)(void *, unsigned char *, size_t), |
| 419 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 420 | int mode, |
| 421 | const unsigned char *label, size_t label_len, |
| 422 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 423 | const unsigned char *input, |
| 424 | unsigned char *output, |
| 425 | size_t output_max_len ); |
| 426 | |
| 427 | /** |
| 428 | * \brief Generic wrapper to perform a PKCS#1 signature using the |
| 429 | * mode from the context. Do a private RSA operation to sign |
| 430 | * a message digest |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 431 | * |
| 432 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 433 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for |
| 434 | * RSA_PRIVATE) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 435 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 436 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | fc22c44 | 2009-07-19 20:36:27 +0000 | [diff] [blame] | 437 | * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512} |
| 438 | * \param hashlen message digest length (for SIG_RSA_RAW only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 439 | * \param hash buffer holding the message digest |
| 440 | * \param sig buffer that will hold the ciphertext |
| 441 | * |
| 442 | * \return 0 if the signing operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 443 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 444 | * |
| 445 | * \note The "sig" buffer must be as large as the size |
| 446 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 447 | * |
| 448 | * \note In case of PKCS#1 v2.1 encoding keep in mind that |
| 449 | * the hash_id in the RSA context is the one used for the |
| 450 | * encoding. hash_id in the function call is the type of hash |
| 451 | * that is encoded. According to RFC 3447 it is advised to |
| 452 | * keep both hashes the same. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 453 | */ |
| 454 | int rsa_pkcs1_sign( rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 455 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 456 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 457 | int mode, |
| 458 | int hash_id, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 459 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 460 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 461 | unsigned char *sig ); |
| 462 | |
| 463 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 464 | * \brief Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN) |
| 465 | * |
| 466 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 467 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
| 468 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 469 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 470 | * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512} |
| 471 | * \param hashlen message digest length (for SIG_RSA_RAW only) |
| 472 | * \param hash buffer holding the message digest |
| 473 | * \param sig buffer that will hold the ciphertext |
| 474 | * |
| 475 | * \return 0 if the signing operation was successful, |
| 476 | * or an POLARSSL_ERR_RSA_XXX error code |
| 477 | * |
| 478 | * \note The "sig" buffer must be as large as the size |
| 479 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 480 | */ |
| 481 | int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx, |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 482 | int (*f_rng)(void *, unsigned char *, size_t), |
| 483 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 484 | int mode, |
| 485 | int hash_id, |
| 486 | unsigned int hashlen, |
| 487 | const unsigned char *hash, |
| 488 | unsigned char *sig ); |
| 489 | |
| 490 | /** |
| 491 | * \brief Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN) |
| 492 | * |
| 493 | * \param ctx RSA context |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 494 | * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding and for |
| 495 | * RSA_PRIVATE) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 496 | * \param p_rng RNG parameter |
| 497 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 498 | * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512} |
| 499 | * \param hashlen message digest length (for SIG_RSA_RAW only) |
| 500 | * \param hash buffer holding the message digest |
| 501 | * \param sig buffer that will hold the ciphertext |
| 502 | * |
| 503 | * \return 0 if the signing operation was successful, |
| 504 | * or an POLARSSL_ERR_RSA_XXX error code |
| 505 | * |
| 506 | * \note The "sig" buffer must be as large as the size |
| 507 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 508 | * |
| 509 | * \note In case of PKCS#1 v2.1 encoding keep in mind that |
| 510 | * the hash_id in the RSA context is the one used for the |
| 511 | * encoding. hash_id in the function call is the type of hash |
| 512 | * that is encoded. According to RFC 3447 it is advised to |
| 513 | * keep both hashes the same. |
| 514 | */ |
| 515 | int rsa_rsassa_pss_sign( rsa_context *ctx, |
| 516 | int (*f_rng)(void *, unsigned char *, size_t), |
| 517 | void *p_rng, |
| 518 | int mode, |
| 519 | int hash_id, |
| 520 | unsigned int hashlen, |
| 521 | const unsigned char *hash, |
| 522 | unsigned char *sig ); |
| 523 | |
| 524 | /** |
| 525 | * \brief Generic wrapper to perform a PKCS#1 verification using the |
| 526 | * mode from the context. Do a public RSA operation and check |
| 527 | * the message digest |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 528 | * |
| 529 | * \param ctx points to an RSA public key |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 530 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
| 531 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 532 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | b924f04 | 2010-07-18 08:49:19 +0000 | [diff] [blame] | 533 | * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512} |
Paul Bakker | fc22c44 | 2009-07-19 20:36:27 +0000 | [diff] [blame] | 534 | * \param hashlen message digest length (for SIG_RSA_RAW only) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 535 | * \param hash buffer holding the message digest |
| 536 | * \param sig buffer holding the ciphertext |
| 537 | * |
| 538 | * \return 0 if the verify operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 539 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 540 | * |
| 541 | * \note The "sig" buffer must be as large as the size |
| 542 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 543 | * |
| 544 | * \note In case of PKCS#1 v2.1 encoding keep in mind that |
| 545 | * the hash_id in the RSA context is the one used for the |
| 546 | * verification. hash_id in the function call is the type of hash |
| 547 | * that is verified. According to RFC 3447 it is advised to |
| 548 | * keep both hashes the same. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 549 | */ |
| 550 | int rsa_pkcs1_verify( rsa_context *ctx, |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 551 | int (*f_rng)(void *, unsigned char *, size_t), |
| 552 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 553 | int mode, |
| 554 | int hash_id, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 555 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 556 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 557 | unsigned char *sig ); |
| 558 | |
| 559 | /** |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 560 | * \brief Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY) |
| 561 | * |
| 562 | * \param ctx points to an RSA public key |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 563 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
| 564 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 565 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 566 | * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512} |
| 567 | * \param hashlen message digest length (for SIG_RSA_RAW only) |
| 568 | * \param hash buffer holding the message digest |
| 569 | * \param sig buffer holding the ciphertext |
| 570 | * |
| 571 | * \return 0 if the verify operation was successful, |
| 572 | * or an POLARSSL_ERR_RSA_XXX error code |
| 573 | * |
| 574 | * \note The "sig" buffer must be as large as the size |
| 575 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 576 | */ |
| 577 | int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx, |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 578 | int (*f_rng)(void *, unsigned char *, size_t), |
| 579 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 580 | int mode, |
| 581 | int hash_id, |
| 582 | unsigned int hashlen, |
| 583 | const unsigned char *hash, |
| 584 | unsigned char *sig ); |
| 585 | |
| 586 | /** |
| 587 | * \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) |
| 588 | * \brief Do a public RSA and check the message digest |
| 589 | * |
| 590 | * \param ctx points to an RSA public key |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 591 | * \param f_rng RNG function (Only needed for RSA_PRIVATE) |
| 592 | * \param p_rng RNG parameter |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 593 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 594 | * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512} |
| 595 | * \param hashlen message digest length (for SIG_RSA_RAW only) |
| 596 | * \param hash buffer holding the message digest |
| 597 | * \param sig buffer holding the ciphertext |
| 598 | * |
| 599 | * \return 0 if the verify operation was successful, |
| 600 | * or an POLARSSL_ERR_RSA_XXX error code |
| 601 | * |
| 602 | * \note The "sig" buffer must be as large as the size |
| 603 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 604 | * |
| 605 | * \note In case of PKCS#1 v2.1 encoding keep in mind that |
| 606 | * the hash_id in the RSA context is the one used for the |
| 607 | * verification. hash_id in the function call is the type of hash |
| 608 | * that is verified. According to RFC 3447 it is advised to |
| 609 | * keep both hashes the same. |
| 610 | */ |
| 611 | int rsa_rsassa_pss_verify( rsa_context *ctx, |
Paul Bakker | 43f9799 | 2013-09-23 11:23:31 +0200 | [diff] [blame^] | 612 | int (*f_rng)(void *, unsigned char *, size_t), |
| 613 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 614 | int mode, |
| 615 | int hash_id, |
| 616 | unsigned int hashlen, |
| 617 | const unsigned char *hash, |
| 618 | unsigned char *sig ); |
| 619 | |
| 620 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 621 | * \brief Free the components of an RSA key |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 622 | * |
| 623 | * \param ctx RSA Context to free |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 624 | */ |
| 625 | void rsa_free( rsa_context *ctx ); |
| 626 | |
| 627 | /** |
| 628 | * \brief Checkup routine |
| 629 | * |
| 630 | * \return 0 if successful, or 1 if the test failed |
| 631 | */ |
| 632 | int rsa_self_test( int verbose ); |
| 633 | |
| 634 | #ifdef __cplusplus |
| 635 | } |
| 636 | #endif |
| 637 | |
| 638 | #endif /* rsa.h */ |