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 | * |
| 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
| 6 | * Copyright (C) 2009 Paul Bakker |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 22 | #ifndef POLARSSL_RSA_H |
| 23 | #define POLARSSL_RSA_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | |
Paul Bakker | 8e831ed | 2009-01-03 21:24:11 +0000 | [diff] [blame] | 25 | #include "polarssl/bignum.h" |
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 | #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x0400 |
| 28 | #define POLARSSL_ERR_RSA_INVALID_PADDING -0x0410 |
| 29 | #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x0420 |
| 30 | #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x0430 |
| 31 | #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x0440 |
| 32 | #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x0450 |
| 33 | #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x0460 |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 34 | #define POLARSSL_ERR_RSA_OUTPUT_TO_LARGE -0x0470 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * PKCS#1 constants |
| 38 | */ |
| 39 | #define RSA_RAW 0 |
| 40 | #define RSA_MD2 2 |
| 41 | #define RSA_MD4 3 |
| 42 | #define RSA_MD5 4 |
| 43 | #define RSA_SHA1 5 |
| 44 | #define RSA_SHA256 6 |
| 45 | |
| 46 | #define RSA_PUBLIC 0 |
| 47 | #define RSA_PRIVATE 1 |
| 48 | |
| 49 | #define RSA_PKCS_V15 0 |
| 50 | #define RSA_PKCS_V21 1 |
| 51 | |
| 52 | #define RSA_SIGN 1 |
| 53 | #define RSA_CRYPT 2 |
| 54 | |
| 55 | /* |
| 56 | * DigestInfo ::= SEQUENCE { |
| 57 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 58 | * digest Digest } |
| 59 | * |
| 60 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 61 | * |
| 62 | * Digest ::= OCTET STRING |
| 63 | */ |
| 64 | #define ASN1_HASH_MDX \ |
| 65 | "\x30\x20\x30\x0C\x06\x08\x2A\x86\x48" \ |
| 66 | "\x86\xF7\x0D\x02\x00\x05\x00\x04\x10" |
| 67 | |
| 68 | #define ASN1_HASH_SHA1 \ |
| 69 | "\x30\x21\x30\x09\x06\x05\x2B\x0E\x03" \ |
| 70 | "\x02\x1A\x05\x00\x04\x14" |
| 71 | |
| 72 | /** |
| 73 | * \brief RSA context structure |
| 74 | */ |
| 75 | typedef struct |
| 76 | { |
| 77 | int ver; /*!< always 0 */ |
| 78 | int len; /*!< size(N) in chars */ |
| 79 | |
| 80 | mpi N; /*!< public modulus */ |
| 81 | mpi E; /*!< public exponent */ |
| 82 | |
| 83 | mpi D; /*!< private exponent */ |
| 84 | mpi P; /*!< 1st prime factor */ |
| 85 | mpi Q; /*!< 2nd prime factor */ |
| 86 | mpi DP; /*!< D % (P - 1) */ |
| 87 | mpi DQ; /*!< D % (Q - 1) */ |
| 88 | mpi QP; /*!< 1 / (Q % P) */ |
| 89 | |
| 90 | mpi RN; /*!< cached R^2 mod N */ |
| 91 | mpi RP; /*!< cached R^2 mod P */ |
| 92 | mpi RQ; /*!< cached R^2 mod Q */ |
| 93 | |
| 94 | int padding; /*!< 1.5 or OAEP/PSS */ |
| 95 | int hash_id; /*!< hash identifier */ |
| 96 | int (*f_rng)(void *); /*!< RNG function */ |
| 97 | void *p_rng; /*!< RNG parameter */ |
| 98 | } |
| 99 | rsa_context; |
| 100 | |
| 101 | #ifdef __cplusplus |
| 102 | extern "C" { |
| 103 | #endif |
| 104 | |
| 105 | /** |
| 106 | * \brief Initialize an RSA context |
| 107 | * |
| 108 | * \param ctx RSA context to be initialized |
| 109 | * \param padding RSA_PKCS_V15 or RSA_PKCS_V21 |
| 110 | * \param hash_id RSA_PKCS_V21 hash identifier |
| 111 | * \param f_rng RNG function |
| 112 | * \param p_rng RNG parameter |
| 113 | * |
| 114 | * \note The hash_id parameter is actually ignored |
| 115 | * when using RSA_PKCS_V15 padding. |
| 116 | * |
| 117 | * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding |
| 118 | * is not supported. |
| 119 | */ |
| 120 | void rsa_init( rsa_context *ctx, |
| 121 | int padding, |
| 122 | int hash_id, |
| 123 | int (*f_rng)(void *), |
| 124 | void *p_rng ); |
| 125 | |
| 126 | /** |
| 127 | * \brief Generate an RSA keypair |
| 128 | * |
| 129 | * \param ctx RSA context that will hold the key |
| 130 | * \param nbits size of the public key in bits |
| 131 | * \param exponent public exponent (e.g., 65537) |
| 132 | * |
| 133 | * \note rsa_init() must be called beforehand to setup |
| 134 | * the RSA context (especially f_rng and p_rng). |
| 135 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 136 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | */ |
| 138 | int rsa_gen_key( rsa_context *ctx, int nbits, int exponent ); |
| 139 | |
| 140 | /** |
| 141 | * \brief Check a public RSA key |
| 142 | * |
| 143 | * \param ctx RSA context to be checked |
| 144 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 145 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | */ |
| 147 | int rsa_check_pubkey( rsa_context *ctx ); |
| 148 | |
| 149 | /** |
| 150 | * \brief Check a private RSA key |
| 151 | * |
| 152 | * \param ctx RSA context to be checked |
| 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 | int rsa_check_privkey( rsa_context *ctx ); |
| 157 | |
| 158 | /** |
| 159 | * \brief Do an RSA public key operation |
| 160 | * |
| 161 | * \param ctx RSA context |
| 162 | * \param input input buffer |
| 163 | * \param output output buffer |
| 164 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 165 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | * |
| 167 | * \note This function does NOT take care of message |
| 168 | * padding. Also, be sure to set input[0] = 0. |
| 169 | * |
| 170 | * \note The input and output buffers must be large |
| 171 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 172 | */ |
| 173 | int rsa_public( rsa_context *ctx, |
| 174 | unsigned char *input, |
| 175 | unsigned char *output ); |
| 176 | |
| 177 | /** |
| 178 | * \brief Do an RSA private key operation |
| 179 | * |
| 180 | * \param ctx RSA context |
| 181 | * \param input input buffer |
| 182 | * \param output output buffer |
| 183 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 184 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | * |
| 186 | * \note The input and output buffers must be large |
| 187 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 188 | */ |
| 189 | int rsa_private( rsa_context *ctx, |
| 190 | unsigned char *input, |
| 191 | unsigned char *output ); |
| 192 | |
| 193 | /** |
| 194 | * \brief Add the message padding, then do an RSA operation |
| 195 | * |
| 196 | * \param ctx RSA context |
| 197 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 198 | * \param ilen contains the the plaintext length |
| 199 | * \param input buffer holding the data to be encrypted |
| 200 | * \param output buffer that will hold the ciphertext |
| 201 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 202 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | * |
| 204 | * \note The output buffer must be as large as the size |
| 205 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 206 | */ |
| 207 | int rsa_pkcs1_encrypt( rsa_context *ctx, |
| 208 | int mode, int ilen, |
| 209 | unsigned char *input, |
| 210 | unsigned char *output ); |
| 211 | |
| 212 | /** |
| 213 | * \brief Do an RSA operation, then remove the message padding |
| 214 | * |
| 215 | * \param ctx RSA context |
| 216 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 217 | * \param input buffer holding the encrypted data |
| 218 | * \param output buffer that will hold the plaintext |
| 219 | * \param olen will contain the plaintext length |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 220 | * \param output_max_len maximum length of the output buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | * |
| 224 | * \note The output buffer must be as large as the size |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 225 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 226 | * an error is thrown. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | */ |
| 228 | int rsa_pkcs1_decrypt( rsa_context *ctx, |
| 229 | int mode, int *olen, |
| 230 | unsigned char *input, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 231 | unsigned char *output, |
| 232 | int output_max_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * \brief Do a private RSA to sign a message digest |
| 236 | * |
| 237 | * \param ctx RSA context |
| 238 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 239 | * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256} |
| 240 | * \param hashlen message digest length (for RSA_RAW only) |
| 241 | * \param hash buffer holding the message digest |
| 242 | * \param sig buffer that will hold the ciphertext |
| 243 | * |
| 244 | * \return 0 if the signing operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 245 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 246 | * |
| 247 | * \note The "sig" buffer must be as large as the size |
| 248 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 249 | */ |
| 250 | int rsa_pkcs1_sign( rsa_context *ctx, |
| 251 | int mode, |
| 252 | int hash_id, |
| 253 | int hashlen, |
| 254 | unsigned char *hash, |
| 255 | unsigned char *sig ); |
| 256 | |
| 257 | /** |
| 258 | * \brief Do a public RSA and check the message digest |
| 259 | * |
| 260 | * \param ctx points to an RSA public key |
| 261 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 262 | * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256} |
| 263 | * \param hashlen message digest length (for RSA_RAW only) |
| 264 | * \param hash buffer holding the message digest |
| 265 | * \param sig buffer holding the ciphertext |
| 266 | * |
| 267 | * \return 0 if the verify operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 268 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 269 | * |
| 270 | * \note The "sig" buffer must be as large as the size |
| 271 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 272 | */ |
| 273 | int rsa_pkcs1_verify( rsa_context *ctx, |
| 274 | int mode, |
| 275 | int hash_id, |
| 276 | int hashlen, |
| 277 | unsigned char *hash, |
| 278 | unsigned char *sig ); |
| 279 | |
| 280 | /** |
| 281 | * \brief Free the components of an RSA key |
| 282 | */ |
| 283 | void rsa_free( rsa_context *ctx ); |
| 284 | |
| 285 | /** |
| 286 | * \brief Checkup routine |
| 287 | * |
| 288 | * \return 0 if successful, or 1 if the test failed |
| 289 | */ |
| 290 | int rsa_self_test( int verbose ); |
| 291 | |
| 292 | #ifdef __cplusplus |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | #endif /* rsa.h */ |