HAL_RSA_ALT

MBEDTLS IMPLEMENTATION HAL RSA API

group MBEDTLS_IMPLEMENTATION_HAL_RSA_API

Defines

RSA_EXPONENT_BLINDING

Functions

int rsa_rsassa_pkcs1_v15_encode ( mbedtls_md_type_t md_alg , unsigned int hashlen , const unsigned char * hash , size_t dst_len , unsigned char * dst )

Encodes a message using RSASSA-PKCS1-v1.5.

Construct a PKCS v1.5 encoding of a hashed message.

This is used both for signature generation and verification.

Note

Assumptions:

  • hash has size hashlen.

  • dst points to a buffer of size at least dst_len.

Parameters :
  • md_alg – Identifies the hash algorithm used to generate the given hash; MBEDTLS_MD_NONE if raw data is signed.

  • hashlen – Length of hash. Must match md_alg if that’s not NONE.

  • hash – Buffer containing the hashed message or the raw data.

  • dst_len – Length of the encoded message.

  • dst – Buffer to hold the encoded message.

Returns :

0 on success, MBEDTLS_ERR_RSA_BAD_INPUT_DATA on error.

int mgf_mask ( unsigned char * dst , size_t dlen , unsigned char * src , size_t slen , mbedtls_md_type_t md_alg )

Mask generation function for RSA.

Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.

Parameters :
  • dst – buffer to mask

  • dlen – length of destination buffer

  • src – source of the mask generation

  • slen – length of the source buffer

  • md_alg – message digest to use

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 )

Initializes an RSA context and imports the specified key components.

Note

At a minimum, N and E must be provided for public key operations; N, P, Q, and D are required for private key operations.

Parameters :
  • ctx[in] Pointer to the RSA context to initialize. Must not be NULL.

  • N[in] Pointer to the modulus N. Must not be NULL.

  • P[in] Pointer to the prime factor P. might be NULL if only public key operations are required.

  • Q[in] Pointer to the prime factor Q. might be NULL if only public key operations are required.

  • D[in] Pointer to the private exponent D. might be NULL if only public key operations are required.

  • E[in] Pointer to the public exponent E. might be NULL if only private key operations are required.

Return values :
  • 0 – Operation completed successfully.

  • <0 – An error occurred during the import operation.

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 )

Imports raw RSA parameters into the given RSA context.

This function initializes the provided mbedtls_rsa_context structure with the raw RSA parameters supplied by the caller. It is typically used to set up the RSA context with key components such as modulus, public exponent, private exponent, and primes.

Parameters :
  • ctx – Pointer to the initialized mbedtls_rsa_context structure.

  • ... – Raw RSA parameters to be imported (modulus, exponents, primes, etc.).

Returns :

0 on success, or a specific error code on failure.

int mbedtls_rsa_complete ( mbedtls_rsa_context * ctx )

Validates and completes the RSA context.

This function checks whether the provided RSA context contains all the necessary parameters for cryptographic operations. If the context is incomplete but contains enough information to deduce the missing parameters, it attempts to compute and fill in those parameters (such as modulus N, private exponent D, or CRT parameters). If the context cannot be completed due to insufficient or inconsistent input, an error is returned.

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 ( const mbedtls_rsa_context * ctx , mbedtls_mpi * N , mbedtls_mpi * P , mbedtls_mpi * Q , mbedtls_mpi * D , mbedtls_mpi * E )
int mbedtls_rsa_export_crt ( const mbedtls_rsa_context * ctx , mbedtls_mpi * DP , mbedtls_mpi * DQ , mbedtls_mpi * QP )
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 )
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 , 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_rsaes_oaep_encrypt ( mbedtls_rsa_context * ctx , int ( * f_rng ) ( void * , unsigned char * , size_t ) , void * p_rng , const unsigned char * label , size_t label_len , 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 , size_t ilen , 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 , size_t ilen , const unsigned char * input , unsigned char * output )
int mbedtls_rsa_rsaes_oaep_decrypt ( mbedtls_rsa_context * ctx , int ( * f_rng ) ( void * , unsigned char * , size_t ) , 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_rsaes_pkcs1_v15_decrypt ( mbedtls_rsa_context * ctx , int ( * f_rng ) ( void * , unsigned char * , size_t ) , void * p_rng , size_t * olen , const unsigned char * input , unsigned char * output , size_t output_max_len )
int mbedtls_rsa_pkcs1_decrypt ( mbedtls_rsa_context * ctx , int ( * f_rng ) ( void * , unsigned char * , size_t ) , void * p_rng , size_t * olen , const unsigned char * input , unsigned char * output , size_t output_max_len )
int mbedtls_rsa_rsassa_pss_sign_no_mode_check ( mbedtls_rsa_context * ctx , int ( * f_rng ) ( void * , unsigned char * , size_t ) , void * p_rng , mbedtls_md_type_t md_alg , unsigned int hashlen , const unsigned char * hash , unsigned char * sig )
int mbedtls_rsa_rsassa_pss_sign_ext ( mbedtls_rsa_context * ctx , int ( * f_rng ) ( void * , unsigned char * , size_t ) , 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 , int ( * f_rng ) ( void * , unsigned char * , size_t ) , 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 , int ( * f_rng ) ( void * , unsigned char * , size_t ) , void * p_rng , mbedtls_md_type_t md_alg , unsigned int hashlen , const unsigned char * hash , unsigned char * sig )
int mbedtls_rsa_pkcs1_sign ( mbedtls_rsa_context * ctx , int ( * f_rng ) ( void * , unsigned char * , size_t ) , void * p_rng , mbedtls_md_type_t md_alg , unsigned int hashlen , const unsigned char * hash , 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_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_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_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_copy ( mbedtls_rsa_context * dst , const mbedtls_rsa_context * src )
void mbedtls_rsa_free ( mbedtls_rsa_context * ctx )

MBEDTLS IMPLEMENTATION HAL RSA TYPES

group MBEDTLS_IMPLEMENTATION_HAL_RSA_TYPES

This file provides an API for the RSA public-key cryptosystem based on STM32 PKA hardware crypto accelerator.

The RSA public-key cryptosystem is defined in Public-Key Cryptography Standards (PKCS) #1 v1.5: RSA Encryption and Public-Key Cryptography Standards (PKCS) #1 v2.1: RSA Cryptography Specifications .

The RSA context structure.

Note

Direct manipulation of the members of this structure is deprecated. All manipulation must instead be done through the public interface functions.

struct mbedtls_rsa_context
#include <hal_rsa_alt.h>

Public Members

int ver

Always 0.

size_t len

The size of N in Bytes.

mbedtls_mpi N

The public modulus.

mbedtls_mpi E

The public exponent.

mbedtls_mpi D

The private exponent.

mbedtls_mpi P

The first prime factor.

mbedtls_mpi Q

The second prime factor.

mbedtls_mpi Phi

The Euler tolient function.

mbedtls_mpi DP

D % (P - 1).

mbedtls_mpi DQ

D % (Q - 1).

mbedtls_mpi QP

1 / (Q % P).

mbedtls_mpi RN

cached R^2 mod N.

mbedtls_mpi RP

cached R^2 mod P.

mbedtls_mpi RQ

cached R^2 mod Q.

mbedtls_mpi Vi

The cached blinding value.

mbedtls_mpi Vf

The cached un-blinding value.

int padding

Selects padding mode: #MBEDTLS_RSA_PKCS_V15 for 1.5 padding and #MBEDTLS_RSA_PKCS_V21 for OAEP or PSS.

int hash_id

Hash identifier of mbedtls_md_type_t type, as specified in md.h for use in the MGF mask generating function used in the EME-OAEP and EMSA-PSS encodings.

mbedtls_threading_mutex_t mutex

Thread-safety mutex.

cmox_rsa_handle_t rsa_ctx

ptr on rsa generic ctx