PolarSSL v1.3.1
rsa.h
Go to the documentation of this file.
1 
27 #ifndef POLARSSL_RSA_H
28 #define POLARSSL_RSA_H
29 
30 #include "config.h"
31 
32 #include "bignum.h"
33 #include "md.h"
34 
35 #if defined(POLARSSL_THREADING_C)
36 #include "threading.h"
37 #endif
38 
39 /*
40  * RSA Error codes
41  */
42 #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080
43 #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100
44 #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180
45 #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200
46 #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280
47 #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300
48 #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380
49 #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
50 #define POLARSSL_ERR_RSA_RNG_FAILED -0x4480
52 /*
53  * RSA constants
54  */
55 #define RSA_PUBLIC 0
56 #define RSA_PRIVATE 1
57 
58 #define RSA_PKCS_V15 0
59 #define RSA_PKCS_V21 1
60 
61 #define RSA_SIGN 1
62 #define RSA_CRYPT 2
63 
64 /*
65  * The above constants may be used even if the RSA module is compile out,
66  * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
67  */
68 #if defined(POLARSSL_RSA_C)
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
77 typedef struct
78 {
79  int ver;
80  size_t len;
82  mpi N;
83  mpi E;
85  mpi D;
86  mpi P;
87  mpi Q;
88  mpi DP;
89  mpi DQ;
90  mpi QP;
92  mpi RN;
93  mpi RP;
94  mpi RQ;
96 #if !defined(POLARSSL_RSA_NO_CRT)
97  mpi Vi;
98  mpi Vf;
99 #endif
100 
101  int padding;
103  int hash_id;
107 #if defined(POLARSSL_THREADING_C)
108  threading_mutex_t mutex;
109 #endif
110 }
112 
126 void rsa_init( rsa_context *ctx,
127  int padding,
128  int hash_id);
129 
144 int rsa_gen_key( rsa_context *ctx,
145  int (*f_rng)(void *, unsigned char *, size_t),
146  void *p_rng,
147  unsigned int nbits, int exponent );
148 
156 int rsa_check_pubkey( const rsa_context *ctx );
157 
165 int rsa_check_privkey( const rsa_context *ctx );
166 
183 int rsa_public( rsa_context *ctx,
184  const unsigned char *input,
185  unsigned char *output );
186 
201 int rsa_private( rsa_context *ctx,
202  int (*f_rng)(void *, unsigned char *, size_t),
203  void *p_rng,
204  const unsigned char *input,
205  unsigned char *output );
206 
227  int (*f_rng)(void *, unsigned char *, size_t),
228  void *p_rng,
229  int mode, size_t ilen,
230  const unsigned char *input,
231  unsigned char *output );
232 
250  int (*f_rng)(void *, unsigned char *, size_t),
251  void *p_rng,
252  int mode, size_t ilen,
253  const unsigned char *input,
254  unsigned char *output );
255 
276  int (*f_rng)(void *, unsigned char *, size_t),
277  void *p_rng,
278  int mode,
279  const unsigned char *label, size_t label_len,
280  size_t ilen,
281  const unsigned char *input,
282  unsigned char *output );
283 
305  int (*f_rng)(void *, unsigned char *, size_t),
306  void *p_rng,
307  int mode, size_t *olen,
308  const unsigned char *input,
309  unsigned char *output,
310  size_t output_max_len );
311 
331  int (*f_rng)(void *, unsigned char *, size_t),
332  void *p_rng,
333  int mode, size_t *olen,
334  const unsigned char *input,
335  unsigned char *output,
336  size_t output_max_len );
337 
359  int (*f_rng)(void *, unsigned char *, size_t),
360  void *p_rng,
361  int mode,
362  const unsigned char *label, size_t label_len,
363  size_t *olen,
364  const unsigned char *input,
365  unsigned char *output,
366  size_t output_max_len );
367 
395 int rsa_pkcs1_sign( rsa_context *ctx,
396  int (*f_rng)(void *, unsigned char *, size_t),
397  void *p_rng,
398  int mode,
399  md_type_t md_alg,
400  unsigned int hashlen,
401  const unsigned char *hash,
402  unsigned char *sig );
403 
423  int (*f_rng)(void *, unsigned char *, size_t),
424  void *p_rng,
425  int mode,
426  md_type_t md_alg,
427  unsigned int hashlen,
428  const unsigned char *hash,
429  unsigned char *sig );
430 
457  int (*f_rng)(void *, unsigned char *, size_t),
458  void *p_rng,
459  int mode,
460  md_type_t md_alg,
461  unsigned int hashlen,
462  const unsigned char *hash,
463  unsigned char *sig );
464 
491 int rsa_pkcs1_verify( rsa_context *ctx,
492  int (*f_rng)(void *, unsigned char *, size_t),
493  void *p_rng,
494  int mode,
495  md_type_t md_alg,
496  unsigned int hashlen,
497  const unsigned char *hash,
498  const unsigned char *sig );
499 
519  int (*f_rng)(void *, unsigned char *, size_t),
520  void *p_rng,
521  int mode,
522  md_type_t md_alg,
523  unsigned int hashlen,
524  const unsigned char *hash,
525  const unsigned char *sig );
526 
553  int (*f_rng)(void *, unsigned char *, size_t),
554  void *p_rng,
555  int mode,
556  md_type_t md_alg,
557  unsigned int hashlen,
558  const unsigned char *hash,
559  const unsigned char *sig );
560 
570 int rsa_copy( rsa_context *dst, const rsa_context *src );
571 
577 void rsa_free( rsa_context *ctx );
578 
584 int rsa_self_test( int verbose );
585 
586 #ifdef __cplusplus
587 }
588 #endif
589 
590 #endif /* POLARSSL_RSA_C */
591 
592 #endif /* rsa.h */