Loading...
Searching...
No Matches
rsa.h
Go to the documentation of this file.
/*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
*/
#ifndef MBEDTLS_RSA_H
#define MBEDTLS_RSA_H
#include "mbedtls/private_access.h"
#include "mbedtls/build_info.h"
#include "mbedtls/bignum.h"
#include "mbedtls/md.h"
#if defined(MBEDTLS_THREADING_C)
#include "mbedtls/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_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 implementations in the PK layers.
*/
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(MBEDTLS_RSA_ALT)
// Regular implementation
//
#if !defined(MBEDTLS_RSA_GEN_KEY_MIN_BITS)
#define MBEDTLS_RSA_GEN_KEY_MIN_BITS 1024
#elif MBEDTLS_RSA_GEN_KEY_MIN_BITS < 128
#error "MBEDTLS_RSA_GEN_KEY_MIN_BITS must be at least 128 bits"
#endif
typedef struct mbedtls_rsa_context {
int MBEDTLS_PRIVATE(ver);
size_t MBEDTLS_PRIVATE(len);
mbedtls_mpi MBEDTLS_PRIVATE(N);
mbedtls_mpi MBEDTLS_PRIVATE(E);
mbedtls_mpi MBEDTLS_PRIVATE(D);
mbedtls_mpi MBEDTLS_PRIVATE(P);
mbedtls_mpi MBEDTLS_PRIVATE(Q);
mbedtls_mpi MBEDTLS_PRIVATE(DP);
mbedtls_mpi MBEDTLS_PRIVATE(DQ);
mbedtls_mpi MBEDTLS_PRIVATE(QP);
mbedtls_mpi MBEDTLS_PRIVATE(RN);
mbedtls_mpi MBEDTLS_PRIVATE(RP);
mbedtls_mpi MBEDTLS_PRIVATE(RQ);
mbedtls_mpi MBEDTLS_PRIVATE(Vi);
mbedtls_mpi MBEDTLS_PRIVATE(Vf);
int MBEDTLS_PRIVATE(padding);
int MBEDTLS_PRIVATE(hash_id);
#if defined(MBEDTLS_THREADING_C)
/* Invariant: the mutex is initialized iff ver != 0. */
mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex);
#endif
}
mbedtls_rsa_context;
#else /* MBEDTLS_RSA_ALT */
#include "rsa_alt.h"
#endif /* MBEDTLS_RSA_ALT */
void mbedtls_rsa_init(mbedtls_rsa_context *ctx);
int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding,
mbedtls_md_type_t hash_id);
int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx);
int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx);
int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
const mbedtls_mpi *N,
const mbedtls_mpi *P, const mbedtls_mpi *Q,
const mbedtls_mpi *D, const mbedtls_mpi *E);
int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
unsigned char const *N, size_t N_len,
unsigned char const *P, size_t P_len,
unsigned char const *Q, size_t Q_len,
unsigned char const *D, size_t D_len,
unsigned char const *E, size_t E_len);
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx);
int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
mbedtls_mpi *D, mbedtls_mpi *E);
int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
unsigned char *N, size_t N_len,
unsigned char *P, size_t P_len,
unsigned char *Q, size_t Q_len,
unsigned char *D, size_t D_len,
unsigned char *E, size_t E_len);
int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP);
size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx);
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx);
int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
mbedtls_f_rng_t *f_rng,
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,
mbedtls_f_rng_t *f_rng,
void *p_rng,
const unsigned char *input,
unsigned char *output);
int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
mbedtls_f_rng_t *f_rng,
void *p_rng,
size_t ilen,
const unsigned char *input,
unsigned char *output);
int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
mbedtls_f_rng_t *f_rng,
void *p_rng,
size_t ilen,
const unsigned char *input,
unsigned char *output);
int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
mbedtls_f_rng_t *f_rng,
void *p_rng,
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,
mbedtls_f_rng_t *f_rng,
void *p_rng,
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,
mbedtls_f_rng_t *f_rng,
void *p_rng,
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,
mbedtls_f_rng_t *f_rng,
void *p_rng,
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,
mbedtls_f_rng_t *f_rng,
void *p_rng,
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,
mbedtls_f_rng_t *f_rng,
void *p_rng,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig);
#if defined(MBEDTLS_PKCS1_V21)
int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx,
mbedtls_f_rng_t *f_rng,
void *p_rng,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
int saltlen,
unsigned char *sig);
int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
mbedtls_f_rng_t *f_rng,
void *p_rng,
mbedtls_md_type_t md_alg,
unsigned int hashlen,
const unsigned char *hash,
unsigned char *sig);
#endif /* MBEDTLS_PKCS1_V21 */
int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
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,
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,
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,
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);
#if defined(MBEDTLS_SELF_TEST)
int mbedtls_rsa_self_test(int verbose);
#endif /* MBEDTLS_SELF_TEST */
#ifdef __cplusplus
}
#endif
#endif /* rsa.h */
bignum.h
Multi-precision integer library.
build_info.h
Build-time configuration info.
md.h
This file contains the generic functions for message-digest (hashing) and HMAC.
mbedtls_md_type_t
mbedtls_md_type_t
Supported message digests.
Definition md.h:47
mbedtls_f_rng_t
int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size)
The type of custom random generator (RNG) callbacks.
Definition platform_util.h:209
private_access.h
Macro wrapper for struct's members.
MBEDTLS_PRIVATE
#define MBEDTLS_PRIVATE(member)
Definition private_access.h:15
mbedtls_rsa_complete
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
This function completes an RSA context from a set of imported core parameters.
mbedtls_rsa_pkcs1_encrypt
int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, size_t ilen, const unsigned char *input, unsigned char *output)
This function adds the message padding, then performs an RSA operation.
mbedtls_rsa_gen_key
int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, unsigned int nbits, int exponent)
This function generates an RSA keypair.
mbedtls_rsa_rsassa_pss_verify_ext
int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx, 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)
This function performs a PKCS#1 v2.1 PSS verification operation (RSASSA-PSS-VERIFY).
mbedtls_rsa_self_test
int mbedtls_rsa_self_test(int verbose)
The RSA checkup routine.
mbedtls_rsa_rsaes_oaep_encrypt
int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, const unsigned char *label, size_t label_len, size_t ilen, const unsigned char *input, unsigned char *output)
This function performs a PKCS#1 v2.1 OAEP encryption operation (RSAES-OAEP-ENCRYPT).
mbedtls_rsa_import
int mbedtls_rsa_import(mbedtls_rsa_context *ctx, const mbedtls_mpi *N, const mbedtls_mpi *P, const mbedtls_mpi *Q, const mbedtls_mpi *D, const mbedtls_mpi *E)
This function imports a set of core parameters into an RSA context.
mbedtls_rsa_check_pub_priv
int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv)
This function checks a public-private RSA key pair.
mbedtls_rsa_pkcs1_decrypt
int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs an RSA operation, then removes the message padding.
mbedtls_rsa_import_raw
int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx, unsigned char const *N, size_t N_len, unsigned char const *P, size_t P_len, unsigned char const *Q, size_t Q_len, unsigned char const *D, size_t D_len, unsigned char const *E, size_t E_len)
This function imports core RSA parameters, in raw big-endian binary format, into an RSA context.
mbedtls_rsa_rsaes_oaep_decrypt
int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, const unsigned char *label, size_t label_len, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs a PKCS#1 v2.1 OAEP decryption operation (RSAES-OAEP-DECRYPT).
mbedtls_rsa_get_padding_mode
int mbedtls_rsa_get_padding_mode(const mbedtls_rsa_context *ctx)
This function retrieves padding mode of initialized RSA context.
mbedtls_rsa_get_len
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
This function retrieves the length of RSA modulus in Bytes.
mbedtls_rsa_rsassa_pss_sign
int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS signature operation (RSASSA-PSS-SIGN).
mbedtls_rsa_set_padding
int mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, mbedtls_md_type_t hash_id)
This function sets padding for an already initialized RSA context.
mbedtls_rsa_get_bitlen
size_t mbedtls_rsa_get_bitlen(const mbedtls_rsa_context *ctx)
This function retrieves the length of the RSA modulus in bits.
mbedtls_rsa_get_md_alg
int mbedtls_rsa_get_md_alg(const mbedtls_rsa_context *ctx)
This function retrieves hash identifier of mbedtls_md_type_t type.
mbedtls_rsa_export
int mbedtls_rsa_export(const mbedtls_rsa_context *ctx, mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, mbedtls_mpi *D, mbedtls_mpi *E)
This function exports the core parameters of an RSA key.
mbedtls_rsa_export_raw
int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx, unsigned char *N, size_t N_len, unsigned char *P, size_t P_len, unsigned char *Q, size_t Q_len, unsigned char *D, size_t D_len, unsigned char *E, size_t E_len)
This function exports core parameters of an RSA key in raw big-endian binary format.
mbedtls_rsa_rsaes_pkcs1_v15_encrypt
int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, size_t ilen, const unsigned char *input, unsigned char *output)
This function performs a PKCS#1 v1.5 encryption operation (RSAES-PKCS1-v1_5-ENCRYPT).
mbedtls_rsa_rsassa_pkcs1_v15_sign
int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a PKCS#1 v1.5 signature operation (RSASSA-PKCS1-v1_5-SIGN).
mbedtls_rsa_copy
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
This function copies the components of an RSA context.
mbedtls_rsa_rsassa_pss_verify
int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS verification operation (RSASSA-PSS-VERIFY).
mbedtls_rsa_free
void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
This function frees the components of an RSA key.
mbedtls_rsa_rsassa_pkcs1_v15_verify
int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a PKCS#1 v1.5 verification operation (RSASSA-PKCS1-v1_5-VERIFY).
mbedtls_rsa_private
int mbedtls_rsa_private(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, const unsigned char *input, unsigned char *output)
This function performs an RSA private key operation.
mbedtls_rsa_pkcs1_sign
int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
This function performs a private RSA operation to sign a message digest using PKCS#1.
mbedtls_rsa_export_crt
int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx, mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
This function exports CRT parameters of a private RSA key.
mbedtls_rsa_public
int mbedtls_rsa_public(mbedtls_rsa_context *ctx, const unsigned char *input, unsigned char *output)
This function performs an RSA public key operation.
mbedtls_rsa_rsassa_pss_sign_ext
int mbedtls_rsa_rsassa_pss_sign_ext(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, int saltlen, unsigned char *sig)
This function performs a PKCS#1 v2.1 PSS signature operation (RSASSA-PSS-SIGN).
mbedtls_rsa_check_privkey
int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
This function checks if a context contains an RSA private key and perform basic consistency checks.
mbedtls_rsa_init
void mbedtls_rsa_init(mbedtls_rsa_context *ctx)
This function initializes an RSA context.
mbedtls_rsa_check_pubkey
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
This function checks if a context contains at least an RSA public key.
mbedtls_rsa_rsaes_pkcs1_v15_decrypt
int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx, mbedtls_f_rng_t *f_rng, void *p_rng, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
This function performs a PKCS#1 v1.5 decryption operation (RSAES-PKCS1-v1_5-DECRYPT).
mbedtls_rsa_pkcs1_verify
int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
This function performs a public RSA operation and checks the message digest.
rsa_alt.h
mbedtls_mpi
MPI structure.
Definition bignum.h:208
mbedtls_rsa_context
The RSA context structure.
Definition rsa.h:85
mbedtls_threading_mutex_t
Definition threading.h:29
threading.h
Threading abstraction layer.