PolarSSL v1.3.7
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 /*
69  * The above constants may be used even if the RSA module is compile out,
70  * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
71  */
72 #if defined(POLARSSL_RSA_C)
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
81 typedef struct
82 {
83  int ver;
84  size_t len;
86  mpi N;
87  mpi E;
89  mpi D;
90  mpi P;
91  mpi Q;
92  mpi DP;
93  mpi DQ;
94  mpi QP;
96  mpi RN;
97  mpi RP;
98  mpi RQ;
100 #if !defined(POLARSSL_RSA_NO_CRT)
101  mpi Vi;
102  mpi Vf;
103 #endif
104 
105  int padding;
107  int hash_id;
111 #if defined(POLARSSL_THREADING_C)
112  threading_mutex_t mutex;
113 #endif
114 }
116 
130 void rsa_init( rsa_context *ctx,
131  int padding,
132  int hash_id);
133 
147 void rsa_set_padding( rsa_context *ctx, int padding, int hash_id);
148 
163 int rsa_gen_key( rsa_context *ctx,
164  int (*f_rng)(void *, unsigned char *, size_t),
165  void *p_rng,
166  unsigned int nbits, int exponent );
167 
175 int rsa_check_pubkey( const rsa_context *ctx );
176 
184 int rsa_check_privkey( const rsa_context *ctx );
185 
202 int rsa_public( rsa_context *ctx,
203  const unsigned char *input,
204  unsigned char *output );
205 
220 int rsa_private( rsa_context *ctx,
221  int (*f_rng)(void *, unsigned char *, size_t),
222  void *p_rng,
223  const unsigned char *input,
224  unsigned char *output );
225 
246  int (*f_rng)(void *, unsigned char *, size_t),
247  void *p_rng,
248  int mode, size_t ilen,
249  const unsigned char *input,
250  unsigned char *output );
251 
269  int (*f_rng)(void *, unsigned char *, size_t),
270  void *p_rng,
271  int mode, size_t ilen,
272  const unsigned char *input,
273  unsigned char *output );
274 
295  int (*f_rng)(void *, unsigned char *, size_t),
296  void *p_rng,
297  int mode,
298  const unsigned char *label, size_t label_len,
299  size_t ilen,
300  const unsigned char *input,
301  unsigned char *output );
302 
324  int (*f_rng)(void *, unsigned char *, size_t),
325  void *p_rng,
326  int mode, size_t *olen,
327  const unsigned char *input,
328  unsigned char *output,
329  size_t output_max_len );
330 
350  int (*f_rng)(void *, unsigned char *, size_t),
351  void *p_rng,
352  int mode, size_t *olen,
353  const unsigned char *input,
354  unsigned char *output,
355  size_t output_max_len );
356 
378  int (*f_rng)(void *, unsigned char *, size_t),
379  void *p_rng,
380  int mode,
381  const unsigned char *label, size_t label_len,
382  size_t *olen,
383  const unsigned char *input,
384  unsigned char *output,
385  size_t output_max_len );
386 
414 int rsa_pkcs1_sign( rsa_context *ctx,
415  int (*f_rng)(void *, unsigned char *, size_t),
416  void *p_rng,
417  int mode,
418  md_type_t md_alg,
419  unsigned int hashlen,
420  const unsigned char *hash,
421  unsigned char *sig );
422 
442  int (*f_rng)(void *, unsigned char *, size_t),
443  void *p_rng,
444  int mode,
445  md_type_t md_alg,
446  unsigned int hashlen,
447  const unsigned char *hash,
448  unsigned char *sig );
449 
476  int (*f_rng)(void *, unsigned char *, size_t),
477  void *p_rng,
478  int mode,
479  md_type_t md_alg,
480  unsigned int hashlen,
481  const unsigned char *hash,
482  unsigned char *sig );
483 
510 int rsa_pkcs1_verify( rsa_context *ctx,
511  int (*f_rng)(void *, unsigned char *, size_t),
512  void *p_rng,
513  int mode,
514  md_type_t md_alg,
515  unsigned int hashlen,
516  const unsigned char *hash,
517  const unsigned char *sig );
518 
538  int (*f_rng)(void *, unsigned char *, size_t),
539  void *p_rng,
540  int mode,
541  md_type_t md_alg,
542  unsigned int hashlen,
543  const unsigned char *hash,
544  const unsigned char *sig );
545 
571  int (*f_rng)(void *, unsigned char *, size_t),
572  void *p_rng,
573  int mode,
574  md_type_t md_alg,
575  unsigned int hashlen,
576  const unsigned char *hash,
577  const unsigned char *sig );
578 
588 int rsa_copy( rsa_context *dst, const rsa_context *src );
589 
595 void rsa_free( rsa_context *ctx );
596 
602 int rsa_self_test( int verbose );
603 
604 #ifdef __cplusplus
605 }
606 #endif
607 
608 #endif /* POLARSSL_RSA_C */
609 
610 #endif /* rsa.h */
int rsa_self_test(int verbose)
Checkup routine.
int rsa_copy(rsa_context *dst, const rsa_context *src)
Copy the components of an RSA context.
int rsa_rsaes_oaep_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t ilen, const unsigned char *input, unsigned char *output)
Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
int rsa_check_privkey(const rsa_context *ctx)
Check a private RSA key.
int padding
Definition: rsa.h:105
int rsa_rsassa_pkcs1_v15_verify(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
mpi Vf
Definition: rsa.h:102
int rsa_rsaes_pkcs1_v15_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
int rsa_rsaes_oaep_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
mpi DQ
Definition: rsa.h:93
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
mpi RP
Definition: rsa.h:97
int rsa_rsassa_pss_verify(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
int rsa_pkcs1_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Generic wrapper to perform a PKCS#1 decryption using the mode from the context.
MPI structure.
Definition: bignum.h:181
Multi-precision integer library.
size_t len
Definition: rsa.h:84
md_type_t
Definition: md.h:51
mpi P
Definition: rsa.h:90
mpi Vi
Definition: rsa.h:101
int rsa_rsaes_pkcs1_v15_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
mpi Q
Definition: rsa.h:91
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
int rsa_private(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, const unsigned char *input, unsigned char *output)
Do an RSA private key operation.
RSA context structure.
Definition: rsa.h:81
mpi D
Definition: rsa.h:89
int rsa_pkcs1_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Generic wrapper to perform a PKCS#1 encryption using the mode from the context.
Threading abstraction layer.
mpi QP
Definition: rsa.h:94
mpi N
Definition: rsa.h:86
mpi RQ
Definition: rsa.h:98
int rsa_rsassa_pss_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
mpi E
Definition: rsa.h:87
int rsa_pkcs1_verify(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
Generic wrapper to perform a PKCS#1 verification using the mode from the context. ...
mpi DP
Definition: rsa.h:92
int hash_id
Definition: rsa.h:107
int rsa_pkcs1_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Generic wrapper to perform a PKCS#1 signature using the mode from the context.
int rsa_rsassa_pkcs1_v15_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
Generic message digest wrapper.
void rsa_set_padding(rsa_context *ctx, int padding, int hash_id)
Set padding for an already initialized RSA context.
int rsa_gen_key(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, unsigned int nbits, int exponent)
Generate an RSA keypair.
void rsa_init(rsa_context *ctx, int padding, int hash_id)
Initialize an RSA context.
mpi RN
Definition: rsa.h:96
int ver
Definition: rsa.h:83
int rsa_public(rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA public key operation.