mbed TLS v1.3.14
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 
217 int rsa_public( rsa_context *ctx,
218  const unsigned char *input,
219  unsigned char *output );
220 
236 int rsa_private( rsa_context *ctx,
237  int (*f_rng)(void *, unsigned char *, size_t),
238  void *p_rng,
239  const unsigned char *input,
240  unsigned char *output );
241 
263  int (*f_rng)(void *, unsigned char *, size_t),
264  void *p_rng,
265  int mode, size_t ilen,
266  const unsigned char *input,
267  unsigned char *output );
268 
287  int (*f_rng)(void *, unsigned char *, size_t),
288  void *p_rng,
289  int mode, size_t ilen,
290  const unsigned char *input,
291  unsigned char *output );
292 
314  int (*f_rng)(void *, unsigned char *, size_t),
315  void *p_rng,
316  int mode,
317  const unsigned char *label, size_t label_len,
318  size_t ilen,
319  const unsigned char *input,
320  unsigned char *output );
321 
344  int (*f_rng)(void *, unsigned char *, size_t),
345  void *p_rng,
346  int mode, size_t *olen,
347  const unsigned char *input,
348  unsigned char *output,
349  size_t output_max_len );
350 
371  int (*f_rng)(void *, unsigned char *, size_t),
372  void *p_rng,
373  int mode, size_t *olen,
374  const unsigned char *input,
375  unsigned char *output,
376  size_t output_max_len );
377 
400  int (*f_rng)(void *, unsigned char *, size_t),
401  void *p_rng,
402  int mode,
403  const unsigned char *label, size_t label_len,
404  size_t *olen,
405  const unsigned char *input,
406  unsigned char *output,
407  size_t output_max_len );
408 
434 int rsa_pkcs1_sign( rsa_context *ctx,
435  int (*f_rng)(void *, unsigned char *, size_t),
436  void *p_rng,
437  int mode,
438  md_type_t md_alg,
439  unsigned int hashlen,
440  const unsigned char *hash,
441  unsigned char *sig );
442 
462  int (*f_rng)(void *, unsigned char *, size_t),
463  void *p_rng,
464  int mode,
465  md_type_t md_alg,
466  unsigned int hashlen,
467  const unsigned char *hash,
468  unsigned char *sig );
469 
496  int (*f_rng)(void *, unsigned char *, size_t),
497  void *p_rng,
498  int mode,
499  md_type_t md_alg,
500  unsigned int hashlen,
501  const unsigned char *hash,
502  unsigned char *sig );
503 
528 int rsa_pkcs1_verify( rsa_context *ctx,
529  int (*f_rng)(void *, unsigned char *, size_t),
530  void *p_rng,
531  int mode,
532  md_type_t md_alg,
533  unsigned int hashlen,
534  const unsigned char *hash,
535  const unsigned char *sig );
536 
557  int (*f_rng)(void *, unsigned char *, size_t),
558  void *p_rng,
559  int mode,
560  md_type_t md_alg,
561  unsigned int hashlen,
562  const unsigned char *hash,
563  const unsigned char *sig );
564 
592  int (*f_rng)(void *, unsigned char *, size_t),
593  void *p_rng,
594  int mode,
595  md_type_t md_alg,
596  unsigned int hashlen,
597  const unsigned char *hash,
598  const unsigned char *sig );
599 
626  int (*f_rng)(void *, unsigned char *, size_t),
627  void *p_rng,
628  int mode,
629  md_type_t md_alg,
630  unsigned int hashlen,
631  const unsigned char *hash,
632  md_type_t mgf1_hash_id,
633  int expected_salt_len,
634  const unsigned char *sig );
635 
645 int rsa_copy( rsa_context *dst, const rsa_context *src );
646 
652 void rsa_free( rsa_context *ctx );
653 
659 int rsa_self_test( int verbose );
660 
661 #ifdef __cplusplus
662 }
663 #endif
664 
665 #endif /* POLARSSL_RSA_C */
666 
667 #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) (Thread-safe if POLARSSL_THREADING_C is en...
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) (Thread-safe if POLARSSL_THREADING_C is...
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) (Thread-safe if POLARSSL_THREADING_C is e...
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) (Thread-safe if POLARSSL_THREADING_C is en...
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.) (Thread-safe if POLARSSL_THREADING_C is enabled)
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:45
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) (Thread-safe if POLARSSL_THREADING_C is e...
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 (Thread-safe if POLARSSL_THREADING_C is enabled)
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) (Thread-safe if POLARSSL_THREADING_C is enabled...
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 (Thread-safe if POLARSSL_THREADING_C is enabled)