rsa.h
Go to the documentation of this file.
#ifndef MBEDTLS_RSA_H
#define MBEDTLS_RSA_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "bignum.h"
#include "md.h"
#if defined(MBEDTLS_THREADING_C)
#include "threading.h"
#endif
/*
* RSA Error codes
*/
#define MBEDTLS_ERR_RSA_BAD_INPUT_DATA -0x4080
#define MBEDTLS_ERR_RSA_INVALID_PADDING -0x4100
#define MBEDTLS_ERR_RSA_KEY_GEN_FAILED -0x4180
#define MBEDTLS_ERR_RSA_KEY_CHECK_FAILED -0x4200
#define MBEDTLS_ERR_RSA_PUBLIC_FAILED -0x4280
#define MBEDTLS_ERR_RSA_PRIVATE_FAILED -0x4300
#define MBEDTLS_ERR_RSA_VERIFY_FAILED -0x4380
#define MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
#define MBEDTLS_ERR_RSA_RNG_FAILED -0x4480
/*
* RSA constants
*/
#define MBEDTLS_RSA_PUBLIC 0
#define MBEDTLS_RSA_PRIVATE 1
#define MBEDTLS_RSA_PKCS_V15 0
#define MBEDTLS_RSA_PKCS_V21 1
#define MBEDTLS_RSA_SIGN 1
#define MBEDTLS_RSA_CRYPT 2
#define MBEDTLS_RSA_SALT_LEN_ANY -1
/*
* The above constants may be used even if the RSA module is compile out,
* eg for alternative (PKCS#11) RSA implemenations in the PK layers.
*/
#if defined(MBEDTLS_RSA_C)
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
int ver;
size_t len;
mbedtls_mpi N;
mbedtls_mpi E;
mbedtls_mpi D;
mbedtls_mpi P;
mbedtls_mpi Q;
mbedtls_mpi DP;
mbedtls_mpi DQ;
mbedtls_mpi QP;
mbedtls_mpi RN;
mbedtls_mpi RP;
mbedtls_mpi RQ;
mbedtls_mpi Vi;
mbedtls_mpi Vf;
int padding;
int hash_id;
#if defined(MBEDTLS_THREADING_C)
mbedtls_threading_mutex_t mutex;
#endif
}
mbedtls_rsa_context;
void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
int padding,
int hash_id);
void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id);
int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
unsigned int nbits, int exponent );
int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx );
int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx );
int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv );
int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
const unsigned char *input,
unsigned char *output );
int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
const unsigned char *input,
unsigned char *output );
int mbedtls_rsa_pkcs1_encrypt( mbedtls_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 );
int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_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 );
int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_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 );
int mbedtls_rsa_pkcs1_decrypt( mbedtls_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 );
int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_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 );
int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_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 );
int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig );
int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig );
int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig );
int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
const unsigned char *sig );
int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
const unsigned char *sig );
int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
const unsigned char *sig );
int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
mbedtls_md_type_t mgf1_hash_id,
int expected_salt_len,
const unsigned char *sig );
int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src );
void mbedtls_rsa_free( mbedtls_rsa_context *ctx );
int mbedtls_rsa_self_test( int verbose );
#ifdef __cplusplus
}
#endif
#endif /* MBEDTLS_RSA_C */
#endif /* rsa.h */
mbedtls_rsa_self_test
int mbedtls_rsa_self_test(int verbose)
Checkup routine.
mbedtls_rsa_context::DP
mbedtls_mpi DP
Definition: rsa.h:90
mbedtls_rsa_rsassa_pkcs1_v15_sign
int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_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)
mbedtls_rsa_public
int mbedtls_rsa_public(mbedtls_rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA public key operation.
mbedtls_rsa_rsaes_oaep_decrypt
int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_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)
mbedtls_rsa_context::hash_id
int hash_id
Definition: rsa.h:103
mbedtls_rsa_pkcs1_verify
int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_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. ...
config.h
Configuration options (set of defines)
mbedtls_rsa_context::DQ
mbedtls_mpi DQ
Definition: rsa.h:91
mbedtls_rsa_pkcs1_sign
int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_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.
mbedtls_rsa_set_padding
void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id)
Set padding for an already initialized RSA context See mbedtls_rsa_init() for details.
bignum.h
Multi-precision integer library.
mbedtls_rsa_rsassa_pss_verify_ext
int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, mbedtls_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...
mbedtls_rsa_context::mutex
mbedtls_threading_mutex_t mutex
Definition: rsa.h:108
mbedtls_rsa_pkcs1_decrypt
int mbedtls_rsa_pkcs1_decrypt(mbedtls_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.
mbedtls_rsa_context::ver
int ver
Definition: rsa.h:81
mbedtls_rsa_rsassa_pss_sign
int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_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)
mbedtls_rsa_context::RP
mbedtls_mpi RP
Definition: rsa.h:95
mbedtls_rsa_check_pub_priv
int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv)
Check a public-private RSA key pair.
threading.h
Threading abstraction layer.
mbedtls_rsa_context::P
mbedtls_mpi P
Definition: rsa.h:88
mbedtls_rsa_private
int mbedtls_rsa_private(mbedtls_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.
mbedtls_rsa_rsaes_pkcs1_v15_decrypt
int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_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)
mbedtls_threading_mutex_t
Definition: threading.h:44
mbedtls_rsa_rsassa_pkcs1_v15_verify
int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_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)
mbedtls_rsa_context::E
mbedtls_mpi E
Definition: rsa.h:85
mbedtls_rsa_context::len
size_t len
Definition: rsa.h:82
mbedtls_rsa_context::Vf
mbedtls_mpi Vf
Definition: rsa.h:99
mbedtls_rsa_pkcs1_encrypt
int mbedtls_rsa_pkcs1_encrypt(mbedtls_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.
mbedtls_rsa_context::QP
mbedtls_mpi QP
Definition: rsa.h:92
mbedtls_rsa_context::D
mbedtls_mpi D
Definition: rsa.h:87
mbedtls_rsa_context::padding
int padding
Definition: rsa.h:101
md.h
Generic message digest wrapper.
mbedtls_rsa_rsassa_pss_verify
int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_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.)
mbedtls_rsa_rsaes_pkcs1_v15_encrypt
int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_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)
mbedtls_mpi
MPI structure.
Definition: bignum.h:144
mbedtls_rsa_context::N
mbedtls_mpi N
Definition: rsa.h:84
mbedtls_rsa_check_pubkey
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
Check a public RSA key.
mbedtls_rsa_context::RN
mbedtls_mpi RN
Definition: rsa.h:94
mbedtls_rsa_free
void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
Free the components of an RSA key.
mbedtls_rsa_context::RQ
mbedtls_mpi RQ
Definition: rsa.h:96
mbedtls_rsa_gen_key
int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, unsigned int nbits, int exponent)
Generate an RSA keypair.
mbedtls_rsa_copy
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
Copy the components of an RSA context.
mbedtls_rsa_check_privkey
int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
Check a private RSA key.
mbedtls_rsa_context::Vi
mbedtls_mpi Vi
Definition: rsa.h:98
mbedtls_md_type_t
mbedtls_md_type_t
Definition: md.h:39
mbedtls_rsa_context::Q
mbedtls_mpi Q
Definition: rsa.h:89
mbedtls_rsa_context
RSA context structure.
Definition: rsa.h:79
mbedtls_rsa_rsaes_oaep_encrypt
int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_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)
mbedtls_rsa_init
void mbedtls_rsa_init(mbedtls_rsa_context *ctx, int padding, int hash_id)
Initialize an RSA context.