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