00001
00027 #ifndef POLARSSL_RSA_H
00028 #define POLARSSL_RSA_H
00029
00030 #include "config.h"
00031
00032 #include "bignum.h"
00033 #include "md.h"
00034
00035 #if defined(POLARSSL_THREADING_C)
00036 #include "threading.h"
00037 #endif
00038
00039
00040
00041
00042 #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080
00043 #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100
00044 #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180
00045 #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200
00046 #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280
00047 #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300
00048 #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380
00049 #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
00050 #define POLARSSL_ERR_RSA_RNG_FAILED -0x4480
00052
00053
00054
00055 #define RSA_PUBLIC 0
00056 #define RSA_PRIVATE 1
00057
00058 #define RSA_PKCS_V15 0
00059 #define RSA_PKCS_V21 1
00060
00061 #define RSA_SIGN 1
00062 #define RSA_CRYPT 2
00063
00064
00065
00066
00067
00068 #if defined(POLARSSL_RSA_C)
00069
00070 #ifdef __cplusplus
00071 extern "C" {
00072 #endif
00073
00077 typedef struct
00078 {
00079 int ver;
00080 size_t len;
00082 mpi N;
00083 mpi E;
00085 mpi D;
00086 mpi P;
00087 mpi Q;
00088 mpi DP;
00089 mpi DQ;
00090 mpi QP;
00092 mpi RN;
00093 mpi RP;
00094 mpi RQ;
00096 #if !defined(POLARSSL_RSA_NO_CRT)
00097 mpi Vi;
00098 mpi Vf;
00099 #endif
00100
00101 int padding;
00103 int hash_id;
00107 #if defined(POLARSSL_THREADING_C)
00108 threading_mutex_t mutex;
00109 #endif
00110 }
00111 rsa_context;
00112
00126 void rsa_init( rsa_context *ctx,
00127 int padding,
00128 int hash_id);
00129
00144 int rsa_gen_key( rsa_context *ctx,
00145 int (*f_rng)(void *, unsigned char *, size_t),
00146 void *p_rng,
00147 unsigned int nbits, int exponent );
00148
00156 int rsa_check_pubkey( const rsa_context *ctx );
00157
00165 int rsa_check_privkey( const rsa_context *ctx );
00166
00183 int rsa_public( rsa_context *ctx,
00184 const unsigned char *input,
00185 unsigned char *output );
00186
00201 int rsa_private( rsa_context *ctx,
00202 int (*f_rng)(void *, unsigned char *, size_t),
00203 void *p_rng,
00204 const unsigned char *input,
00205 unsigned char *output );
00206
00226 int rsa_pkcs1_encrypt( rsa_context *ctx,
00227 int (*f_rng)(void *, unsigned char *, size_t),
00228 void *p_rng,
00229 int mode, size_t ilen,
00230 const unsigned char *input,
00231 unsigned char *output );
00232
00249 int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
00250 int (*f_rng)(void *, unsigned char *, size_t),
00251 void *p_rng,
00252 int mode, size_t ilen,
00253 const unsigned char *input,
00254 unsigned char *output );
00255
00275 int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
00276 int (*f_rng)(void *, unsigned char *, size_t),
00277 void *p_rng,
00278 int mode,
00279 const unsigned char *label, size_t label_len,
00280 size_t ilen,
00281 const unsigned char *input,
00282 unsigned char *output );
00283
00304 int rsa_pkcs1_decrypt( rsa_context *ctx,
00305 int (*f_rng)(void *, unsigned char *, size_t),
00306 void *p_rng,
00307 int mode, size_t *olen,
00308 const unsigned char *input,
00309 unsigned char *output,
00310 size_t output_max_len );
00311
00330 int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
00331 int (*f_rng)(void *, unsigned char *, size_t),
00332 void *p_rng,
00333 int mode, size_t *olen,
00334 const unsigned char *input,
00335 unsigned char *output,
00336 size_t output_max_len );
00337
00358 int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
00359 int (*f_rng)(void *, unsigned char *, size_t),
00360 void *p_rng,
00361 int mode,
00362 const unsigned char *label, size_t label_len,
00363 size_t *olen,
00364 const unsigned char *input,
00365 unsigned char *output,
00366 size_t output_max_len );
00367
00395 int rsa_pkcs1_sign( rsa_context *ctx,
00396 int (*f_rng)(void *, unsigned char *, size_t),
00397 void *p_rng,
00398 int mode,
00399 md_type_t md_alg,
00400 unsigned int hashlen,
00401 const unsigned char *hash,
00402 unsigned char *sig );
00403
00422 int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
00423 int (*f_rng)(void *, unsigned char *, size_t),
00424 void *p_rng,
00425 int mode,
00426 md_type_t md_alg,
00427 unsigned int hashlen,
00428 const unsigned char *hash,
00429 unsigned char *sig );
00430
00456 int rsa_rsassa_pss_sign( rsa_context *ctx,
00457 int (*f_rng)(void *, unsigned char *, size_t),
00458 void *p_rng,
00459 int mode,
00460 md_type_t md_alg,
00461 unsigned int hashlen,
00462 const unsigned char *hash,
00463 unsigned char *sig );
00464
00491 int rsa_pkcs1_verify( rsa_context *ctx,
00492 int (*f_rng)(void *, unsigned char *, size_t),
00493 void *p_rng,
00494 int mode,
00495 md_type_t md_alg,
00496 unsigned int hashlen,
00497 const unsigned char *hash,
00498 const unsigned char *sig );
00499
00518 int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
00519 int (*f_rng)(void *, unsigned char *, size_t),
00520 void *p_rng,
00521 int mode,
00522 md_type_t md_alg,
00523 unsigned int hashlen,
00524 const unsigned char *hash,
00525 const unsigned char *sig );
00526
00552 int rsa_rsassa_pss_verify( rsa_context *ctx,
00553 int (*f_rng)(void *, unsigned char *, size_t),
00554 void *p_rng,
00555 int mode,
00556 md_type_t md_alg,
00557 unsigned int hashlen,
00558 const unsigned char *hash,
00559 const unsigned char *sig );
00560
00570 int rsa_copy( rsa_context *dst, const rsa_context *src );
00571
00577 void rsa_free( rsa_context *ctx );
00578
00584 int rsa_self_test( int verbose );
00585
00586 #ifdef __cplusplus
00587 }
00588 #endif
00589
00590 #endif
00591
00592 #endif