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 | * |
Paul Bakker | 785a9ee | 2009-01-25 14:15:10 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 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 |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 40 | |
| 41 | #define SIG_RSA_MD2 2 |
| 42 | #define SIG_RSA_MD4 3 |
| 43 | #define SIG_RSA_MD5 4 |
| 44 | #define SIG_RSA_SHA1 5 |
| 45 | #define SIG_RSA_SHA224 14 |
| 46 | #define SIG_RSA_SHA256 11 |
| 47 | #define SIG_RSA_SHA384 12 |
| 48 | #define SIG_RSA_SHA512 13 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | |
| 50 | #define RSA_PUBLIC 0 |
| 51 | #define RSA_PRIVATE 1 |
| 52 | |
| 53 | #define RSA_PKCS_V15 0 |
| 54 | #define RSA_PKCS_V21 1 |
| 55 | |
| 56 | #define RSA_SIGN 1 |
| 57 | #define RSA_CRYPT 2 |
| 58 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 59 | #define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30" |
| 60 | #define ASN1_STR_NULL "\x05" |
| 61 | #define ASN1_STR_OID "\x06" |
| 62 | #define ASN1_STR_OCTET_STRING "\x04" |
| 63 | |
| 64 | #define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00" |
| 65 | #define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a" |
| 66 | #define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00" |
| 67 | |
| 68 | #define OID_ISO_MEMBER_BODIES "\x2a" |
| 69 | #define OID_ISO_IDENTIFIED_ORG "\x2b" |
| 70 | |
| 71 | /* |
| 72 | * ISO Member bodies OID parts |
| 73 | */ |
| 74 | #define OID_COUNTRY_US "\x86\x48" |
| 75 | #define OID_RSA_DATA_SECURITY "\x86\xf7\x0d" |
| 76 | |
| 77 | /* |
| 78 | * ISO Identified organization OID parts |
| 79 | */ |
| 80 | #define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a" |
| 81 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | /* |
| 83 | * DigestInfo ::= SEQUENCE { |
| 84 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 85 | * digest Digest } |
| 86 | * |
| 87 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 88 | * |
| 89 | * Digest ::= OCTET STRING |
| 90 | */ |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 91 | #define ASN1_HASH_MDX \ |
| 92 | ( \ |
| 93 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \ |
| 94 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \ |
| 95 | ASN1_STR_OID "\x08" \ |
| 96 | OID_DIGEST_ALG_MDX \ |
| 97 | ASN1_STR_NULL "\x00" \ |
| 98 | ASN1_STR_OCTET_STRING "\x10" \ |
| 99 | ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 101 | #define ASN1_HASH_SHA1 \ |
| 102 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \ |
| 103 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \ |
| 104 | ASN1_STR_OID "\x05" \ |
| 105 | OID_HASH_ALG_SHA1 \ |
| 106 | ASN1_STR_NULL "\x00" \ |
| 107 | ASN1_STR_OCTET_STRING "\x14" |
| 108 | |
| 109 | #define ASN1_HASH_SHA2X \ |
| 110 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \ |
| 111 | ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \ |
| 112 | ASN1_STR_OID "\x09" \ |
| 113 | OID_HASH_ALG_SHA2X \ |
| 114 | ASN1_STR_NULL "\x00" \ |
| 115 | ASN1_STR_OCTET_STRING "\x00" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * \brief RSA context structure |
| 119 | */ |
| 120 | typedef struct |
| 121 | { |
| 122 | int ver; /*!< always 0 */ |
| 123 | int len; /*!< size(N) in chars */ |
| 124 | |
| 125 | mpi N; /*!< public modulus */ |
| 126 | mpi E; /*!< public exponent */ |
| 127 | |
| 128 | mpi D; /*!< private exponent */ |
| 129 | mpi P; /*!< 1st prime factor */ |
| 130 | mpi Q; /*!< 2nd prime factor */ |
| 131 | mpi DP; /*!< D % (P - 1) */ |
| 132 | mpi DQ; /*!< D % (Q - 1) */ |
| 133 | mpi QP; /*!< 1 / (Q % P) */ |
| 134 | |
| 135 | mpi RN; /*!< cached R^2 mod N */ |
| 136 | mpi RP; /*!< cached R^2 mod P */ |
| 137 | mpi RQ; /*!< cached R^2 mod Q */ |
| 138 | |
| 139 | int padding; /*!< 1.5 or OAEP/PSS */ |
| 140 | int hash_id; /*!< hash identifier */ |
| 141 | int (*f_rng)(void *); /*!< RNG function */ |
| 142 | void *p_rng; /*!< RNG parameter */ |
| 143 | } |
| 144 | rsa_context; |
| 145 | |
| 146 | #ifdef __cplusplus |
| 147 | extern "C" { |
| 148 | #endif |
| 149 | |
| 150 | /** |
| 151 | * \brief Initialize an RSA context |
| 152 | * |
| 153 | * \param ctx RSA context to be initialized |
| 154 | * \param padding RSA_PKCS_V15 or RSA_PKCS_V21 |
| 155 | * \param hash_id RSA_PKCS_V21 hash identifier |
| 156 | * \param f_rng RNG function |
| 157 | * \param p_rng RNG parameter |
| 158 | * |
| 159 | * \note The hash_id parameter is actually ignored |
| 160 | * when using RSA_PKCS_V15 padding. |
| 161 | * |
| 162 | * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding |
| 163 | * is not supported. |
| 164 | */ |
| 165 | void rsa_init( rsa_context *ctx, |
| 166 | int padding, |
| 167 | int hash_id, |
| 168 | int (*f_rng)(void *), |
| 169 | void *p_rng ); |
| 170 | |
| 171 | /** |
| 172 | * \brief Generate an RSA keypair |
| 173 | * |
| 174 | * \param ctx RSA context that will hold the key |
| 175 | * \param nbits size of the public key in bits |
| 176 | * \param exponent public exponent (e.g., 65537) |
| 177 | * |
| 178 | * \note rsa_init() must be called beforehand to setup |
| 179 | * the RSA context (especially f_rng and p_rng). |
| 180 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 181 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | */ |
| 183 | int rsa_gen_key( rsa_context *ctx, int nbits, int exponent ); |
| 184 | |
| 185 | /** |
| 186 | * \brief Check a public RSA key |
| 187 | * |
| 188 | * \param ctx RSA context to be checked |
| 189 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 190 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | */ |
| 192 | int rsa_check_pubkey( rsa_context *ctx ); |
| 193 | |
| 194 | /** |
| 195 | * \brief Check a private RSA key |
| 196 | * |
| 197 | * \param ctx RSA context to be checked |
| 198 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 199 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | */ |
| 201 | int rsa_check_privkey( rsa_context *ctx ); |
| 202 | |
| 203 | /** |
| 204 | * \brief Do an RSA public key operation |
| 205 | * |
| 206 | * \param ctx RSA context |
| 207 | * \param input input buffer |
| 208 | * \param output output buffer |
| 209 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 210 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | * |
| 212 | * \note This function does NOT take care of message |
| 213 | * padding. Also, be sure to set input[0] = 0. |
| 214 | * |
| 215 | * \note The input and output buffers must be large |
| 216 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 217 | */ |
| 218 | int rsa_public( rsa_context *ctx, |
| 219 | unsigned char *input, |
| 220 | unsigned char *output ); |
| 221 | |
| 222 | /** |
| 223 | * \brief Do an RSA private key operation |
| 224 | * |
| 225 | * \param ctx RSA context |
| 226 | * \param input input buffer |
| 227 | * \param output output buffer |
| 228 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 229 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | * |
| 231 | * \note The input and output buffers must be large |
| 232 | * enough (eg. 128 bytes if RSA-1024 is used). |
| 233 | */ |
| 234 | int rsa_private( rsa_context *ctx, |
| 235 | unsigned char *input, |
| 236 | unsigned char *output ); |
| 237 | |
| 238 | /** |
| 239 | * \brief Add the message padding, then do an RSA operation |
| 240 | * |
| 241 | * \param ctx RSA context |
| 242 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 243 | * \param ilen contains the the plaintext length |
| 244 | * \param input buffer holding the data to be encrypted |
| 245 | * \param output buffer that will hold the ciphertext |
| 246 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 247 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 248 | * |
| 249 | * \note The output buffer must be as large as the size |
| 250 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 251 | */ |
| 252 | int rsa_pkcs1_encrypt( rsa_context *ctx, |
| 253 | int mode, int ilen, |
| 254 | unsigned char *input, |
| 255 | unsigned char *output ); |
| 256 | |
| 257 | /** |
| 258 | * \brief Do an RSA operation, then remove the message padding |
| 259 | * |
| 260 | * \param ctx RSA context |
| 261 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 262 | * \param input buffer holding the encrypted data |
| 263 | * \param output buffer that will hold the plaintext |
| 264 | * \param olen will contain the plaintext length |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 265 | * \param output_max_len maximum length of the output buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 266 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 267 | * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | * |
| 269 | * \note The output buffer must be as large as the size |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 270 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise |
| 271 | * an error is thrown. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 272 | */ |
| 273 | int rsa_pkcs1_decrypt( rsa_context *ctx, |
| 274 | int mode, int *olen, |
| 275 | unsigned char *input, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 276 | unsigned char *output, |
| 277 | int output_max_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * \brief Do a private RSA to sign a message digest |
| 281 | * |
| 282 | * \param ctx RSA context |
| 283 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
Paul Bakker | 4593aea | 2009-02-09 22:32:35 +0000 | [diff] [blame] | 284 | * \param hash_id RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512} |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 285 | * \param hashlen message digest length (for RSA_RAW only) |
| 286 | * \param hash buffer holding the message digest |
| 287 | * \param sig buffer that will hold the ciphertext |
| 288 | * |
| 289 | * \return 0 if the signing operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 290 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 291 | * |
| 292 | * \note The "sig" buffer must be as large as the size |
| 293 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 294 | */ |
| 295 | int rsa_pkcs1_sign( rsa_context *ctx, |
| 296 | int mode, |
| 297 | int hash_id, |
| 298 | int hashlen, |
| 299 | unsigned char *hash, |
| 300 | unsigned char *sig ); |
| 301 | |
| 302 | /** |
| 303 | * \brief Do a public RSA and check the message digest |
| 304 | * |
| 305 | * \param ctx points to an RSA public key |
| 306 | * \param mode RSA_PUBLIC or RSA_PRIVATE |
| 307 | * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256} |
| 308 | * \param hashlen message digest length (for RSA_RAW only) |
| 309 | * \param hash buffer holding the message digest |
| 310 | * \param sig buffer holding the ciphertext |
| 311 | * |
| 312 | * \return 0 if the verify operation was successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 313 | * or an POLARSSL_ERR_RSA_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 314 | * |
| 315 | * \note The "sig" buffer must be as large as the size |
| 316 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). |
| 317 | */ |
| 318 | int rsa_pkcs1_verify( rsa_context *ctx, |
| 319 | int mode, |
| 320 | int hash_id, |
| 321 | int hashlen, |
| 322 | unsigned char *hash, |
| 323 | unsigned char *sig ); |
| 324 | |
| 325 | /** |
| 326 | * \brief Free the components of an RSA key |
| 327 | */ |
| 328 | void rsa_free( rsa_context *ctx ); |
| 329 | |
| 330 | /** |
| 331 | * \brief Checkup routine |
| 332 | * |
| 333 | * \return 0 if successful, or 1 if the test failed |
| 334 | */ |
| 335 | int rsa_self_test( int verbose ); |
| 336 | |
| 337 | #ifdef __cplusplus |
| 338 | } |
| 339 | #endif |
| 340 | |
| 341 | #endif /* rsa.h */ |