mbed TLS v1.3.11
rsa.h
Go to the documentation of this file.
1 
24 #ifndef POLARSSL_RSA_H
25 #define POLARSSL_RSA_H
26 
27 #if !defined(POLARSSL_CONFIG_FILE)
28 #include "config.h"
29 #else
30 #include POLARSSL_CONFIG_FILE
31 #endif
32 
33 #include "bignum.h"
34 #include "md.h"
35 
36 #if defined(POLARSSL_THREADING_C)
37 #include "threading.h"
38 #endif
39 
40 /*
41  * RSA Error codes
42  */
43 #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080
44 #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100
45 #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180
46 #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200
47 #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280
48 #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300
49 #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380
50 #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
51 #define POLARSSL_ERR_RSA_RNG_FAILED -0x4480
53 /*
54  * RSA constants
55  */
56 #define RSA_PUBLIC 0
57 #define RSA_PRIVATE 1
58 
59 #define RSA_PKCS_V15 0
60 #define RSA_PKCS_V21 1
61 
62 #define RSA_SIGN 1
63 #define RSA_CRYPT 2
64 
65 #define RSA_SALT_LEN_ANY -1
66 
67 /*
68  * The above constants may be used even if the RSA module is compile out,
69  * eg for alternative (PKCS#11) RSA implemenations in the PK layers.
70  */
71 #if defined(POLARSSL_RSA_C)
72 
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76 
80 typedef struct
81 {
82  int ver;
83  size_t len;
85  mpi N;
86  mpi E;
88  mpi D;
89  mpi P;
90  mpi Q;
91  mpi DP;
92  mpi DQ;
93  mpi QP;
95  mpi RN;
96  mpi RP;
97  mpi RQ;
99  mpi Vi;
100  mpi Vf;
102  int padding;
104  int hash_id;
108 #if defined(POLARSSL_THREADING_C)
109  threading_mutex_t mutex;
110 #endif
111 }
113 
138 void rsa_init( rsa_context *ctx,
139  int padding,
140  int hash_id);
141 
150 void rsa_set_padding( rsa_context *ctx, int padding, int hash_id);
151 
166 int rsa_gen_key( rsa_context *ctx,
167  int (*f_rng)(void *, unsigned char *, size_t),
168  void *p_rng,
169  unsigned int nbits, int exponent );
170 
178 int rsa_check_pubkey( const rsa_context *ctx );
179 
187 int rsa_check_privkey( const rsa_context *ctx );
188 
198 int rsa_check_pub_priv( const rsa_context *pub, const rsa_context *prv );
199 
216 int rsa_public( rsa_context *ctx,
217  const unsigned char *input,
218  unsigned char *output );
219 
234 int rsa_private( rsa_context *ctx,
235  int (*f_rng)(void *, unsigned char *, size_t),
236  void *p_rng,
237  const unsigned char *input,
238  unsigned char *output );
239 
260  int (*f_rng)(void *, unsigned char *, size_t),
261  void *p_rng,
262  int mode, size_t ilen,
263  const unsigned char *input,
264  unsigned char *output );
265 
283  int (*f_rng)(void *, unsigned char *, size_t),
284  void *p_rng,
285  int mode, size_t ilen,
286  const unsigned char *input,
287  unsigned char *output );
288 
309  int (*f_rng)(void *, unsigned char *, size_t),
310  void *p_rng,
311  int mode,
312  const unsigned char *label, size_t label_len,
313  size_t ilen,
314  const unsigned char *input,
315  unsigned char *output );
316 
338  int (*f_rng)(void *, unsigned char *, size_t),
339  void *p_rng,
340  int mode, size_t *olen,
341  const unsigned char *input,
342  unsigned char *output,
343  size_t output_max_len );
344 
364  int (*f_rng)(void *, unsigned char *, size_t),
365  void *p_rng,
366  int mode, size_t *olen,
367  const unsigned char *input,
368  unsigned char *output,
369  size_t output_max_len );
370 
392  int (*f_rng)(void *, unsigned char *, size_t),
393  void *p_rng,
394  int mode,
395  const unsigned char *label, size_t label_len,
396  size_t *olen,
397  const unsigned char *input,
398  unsigned char *output,
399  size_t output_max_len );
400 
425 int rsa_pkcs1_sign( rsa_context *ctx,
426  int (*f_rng)(void *, unsigned char *, size_t),
427  void *p_rng,
428  int mode,
429  md_type_t md_alg,
430  unsigned int hashlen,
431  const unsigned char *hash,
432  unsigned char *sig );
433 
453  int (*f_rng)(void *, unsigned char *, size_t),
454  void *p_rng,
455  int mode,
456  md_type_t md_alg,
457  unsigned int hashlen,
458  const unsigned char *hash,
459  unsigned char *sig );
460 
486  int (*f_rng)(void *, unsigned char *, size_t),
487  void *p_rng,
488  int mode,
489  md_type_t md_alg,
490  unsigned int hashlen,
491  const unsigned char *hash,
492  unsigned char *sig );
493 
517 int rsa_pkcs1_verify( rsa_context *ctx,
518  int (*f_rng)(void *, unsigned char *, size_t),
519  void *p_rng,
520  int mode,
521  md_type_t md_alg,
522  unsigned int hashlen,
523  const unsigned char *hash,
524  const unsigned char *sig );
525 
545  int (*f_rng)(void *, unsigned char *, size_t),
546  void *p_rng,
547  int mode,
548  md_type_t md_alg,
549  unsigned int hashlen,
550  const unsigned char *hash,
551  const unsigned char *sig );
552 
579  int (*f_rng)(void *, unsigned char *, size_t),
580  void *p_rng,
581  int mode,
582  md_type_t md_alg,
583  unsigned int hashlen,
584  const unsigned char *hash,
585  const unsigned char *sig );
586 
612  int (*f_rng)(void *, unsigned char *, size_t),
613  void *p_rng,
614  int mode,
615  md_type_t md_alg,
616  unsigned int hashlen,
617  const unsigned char *hash,
618  md_type_t mgf1_hash_id,
619  int expected_salt_len,
620  const unsigned char *sig );
621 
631 int rsa_copy( rsa_context *dst, const rsa_context *src );
632 
638 void rsa_free( rsa_context *ctx );
639 
645 int rsa_self_test( int verbose );
646 
647 #ifdef __cplusplus
648 }
649 #endif
650 
651 #endif /* POLARSSL_RSA_C */
652 
653 #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:102
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:100
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:92
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
mpi RP
Definition: rsa.h:96
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) (This is the "simple" version.)
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:182
Multi-precision integer library.
int rsa_check_pub_priv(const rsa_context *pub, const rsa_context *prv)
Check a public-private RSA key pair.
size_t len
Definition: rsa.h:83
md_type_t
Definition: md.h:48
mpi P
Definition: rsa.h:89
mpi Vi
Definition: rsa.h:99
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:90
int rsa_rsassa_pss_verify_ext(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, md_type_t mgf1_hash_id, int expected_salt_len, const unsigned char *sig)
Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) (This is the version with "full" options...
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:80
mpi D
Definition: rsa.h:88
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:93
mpi N
Definition: rsa.h:85
mpi RQ
Definition: rsa.h:97
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:86
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:91
int hash_id
Definition: rsa.h:104
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 See rsa_init() for details.
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:95
int ver
Definition: rsa.h:82
int rsa_public(rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA public key operation.