PolarSSL v1.3.9
rsa.h
Go to the documentation of this file.
1 
27 #ifndef POLARSSL_RSA_H
28 #define POLARSSL_RSA_H
29 
30 #if !defined(POLARSSL_CONFIG_FILE)
31 #include "config.h"
32 #else
33 #include POLARSSL_CONFIG_FILE
34 #endif
35 
36 #include "bignum.h"
37 #include "md.h"
38 
39 #if defined(POLARSSL_THREADING_C)
40 #include "threading.h"
41 #endif
42 
43 /*
44  * RSA Error codes
45  */
46 #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080
47 #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100
48 #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180
49 #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200
50 #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280
51 #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300
52 #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380
53 #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
54 #define POLARSSL_ERR_RSA_RNG_FAILED -0x4480
56 /*
57  * RSA constants
58  */
59 #define RSA_PUBLIC 0
60 #define RSA_PRIVATE 1
61 
62 #define RSA_PKCS_V15 0
63 #define RSA_PKCS_V21 1
64 
65 #define RSA_SIGN 1
66 #define RSA_CRYPT 2
67 
68 #define RSA_SALT_LEN_ANY -1
69 
70 /*
71  * The above constants may be used even if the RSA module is compile out,
72  * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
73  */
74 #if defined(POLARSSL_RSA_C)
75 
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 
83 typedef struct
84 {
85  int ver;
86  size_t len;
88  mpi N;
89  mpi E;
91  mpi D;
92  mpi P;
93  mpi Q;
94  mpi DP;
95  mpi DQ;
96  mpi QP;
98  mpi RN;
99  mpi RP;
100  mpi RQ;
102 #if !defined(POLARSSL_RSA_NO_CRT)
103  mpi Vi;
104  mpi Vf;
105 #endif
106 
107  int padding;
109  int hash_id;
113 #if defined(POLARSSL_THREADING_C)
114  threading_mutex_t mutex;
115 #endif
116 }
118 
143 void rsa_init( rsa_context *ctx,
144  int padding,
145  int hash_id);
146 
155 void rsa_set_padding( rsa_context *ctx, int padding, int hash_id);
156 
171 int rsa_gen_key( rsa_context *ctx,
172  int (*f_rng)(void *, unsigned char *, size_t),
173  void *p_rng,
174  unsigned int nbits, int exponent );
175 
183 int rsa_check_pubkey( const rsa_context *ctx );
184 
192 int rsa_check_privkey( const rsa_context *ctx );
193 
210 int rsa_public( rsa_context *ctx,
211  const unsigned char *input,
212  unsigned char *output );
213 
228 int rsa_private( rsa_context *ctx,
229  int (*f_rng)(void *, unsigned char *, size_t),
230  void *p_rng,
231  const unsigned char *input,
232  unsigned char *output );
233 
254  int (*f_rng)(void *, unsigned char *, size_t),
255  void *p_rng,
256  int mode, size_t ilen,
257  const unsigned char *input,
258  unsigned char *output );
259 
277  int (*f_rng)(void *, unsigned char *, size_t),
278  void *p_rng,
279  int mode, size_t ilen,
280  const unsigned char *input,
281  unsigned char *output );
282 
303  int (*f_rng)(void *, unsigned char *, size_t),
304  void *p_rng,
305  int mode,
306  const unsigned char *label, size_t label_len,
307  size_t ilen,
308  const unsigned char *input,
309  unsigned char *output );
310 
332  int (*f_rng)(void *, unsigned char *, size_t),
333  void *p_rng,
334  int mode, size_t *olen,
335  const unsigned char *input,
336  unsigned char *output,
337  size_t output_max_len );
338 
358  int (*f_rng)(void *, unsigned char *, size_t),
359  void *p_rng,
360  int mode, size_t *olen,
361  const unsigned char *input,
362  unsigned char *output,
363  size_t output_max_len );
364 
386  int (*f_rng)(void *, unsigned char *, size_t),
387  void *p_rng,
388  int mode,
389  const unsigned char *label, size_t label_len,
390  size_t *olen,
391  const unsigned char *input,
392  unsigned char *output,
393  size_t output_max_len );
394 
419 int rsa_pkcs1_sign( rsa_context *ctx,
420  int (*f_rng)(void *, unsigned char *, size_t),
421  void *p_rng,
422  int mode,
423  md_type_t md_alg,
424  unsigned int hashlen,
425  const unsigned char *hash,
426  unsigned char *sig );
427 
447  int (*f_rng)(void *, unsigned char *, size_t),
448  void *p_rng,
449  int mode,
450  md_type_t md_alg,
451  unsigned int hashlen,
452  const unsigned char *hash,
453  unsigned char *sig );
454 
480  int (*f_rng)(void *, unsigned char *, size_t),
481  void *p_rng,
482  int mode,
483  md_type_t md_alg,
484  unsigned int hashlen,
485  const unsigned char *hash,
486  unsigned char *sig );
487 
511 int rsa_pkcs1_verify( rsa_context *ctx,
512  int (*f_rng)(void *, unsigned char *, size_t),
513  void *p_rng,
514  int mode,
515  md_type_t md_alg,
516  unsigned int hashlen,
517  const unsigned char *hash,
518  const unsigned char *sig );
519 
539  int (*f_rng)(void *, unsigned char *, size_t),
540  void *p_rng,
541  int mode,
542  md_type_t md_alg,
543  unsigned int hashlen,
544  const unsigned char *hash,
545  const unsigned char *sig );
546 
573  int (*f_rng)(void *, unsigned char *, size_t),
574  void *p_rng,
575  int mode,
576  md_type_t md_alg,
577  unsigned int hashlen,
578  const unsigned char *hash,
579  const unsigned char *sig );
580 
606  int (*f_rng)(void *, unsigned char *, size_t),
607  void *p_rng,
608  int mode,
609  md_type_t md_alg,
610  unsigned int hashlen,
611  const unsigned char *hash,
612  md_type_t mgf1_hash_id,
613  int expected_salt_len,
614  const unsigned char *sig );
615 
625 int rsa_copy( rsa_context *dst, const rsa_context *src );
626 
632 void rsa_free( rsa_context *ctx );
633 
639 int rsa_self_test( int verbose );
640 
641 #ifdef __cplusplus
642 }
643 #endif
644 
645 #endif /* POLARSSL_RSA_C */
646 
647 #endif /* rsa.h */