@@ -3112,6 +3112,9 @@ static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
31123112 int ret = 0 ;
31133113 (void )rng ;
31143114 (void )checkSmallCt ;
3115+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3116+ RsaPadding padding ;
3117+ #endif
31153118
31163119 if (key == NULL || in == NULL || inLen == 0 || out == NULL ||
31173120 outLen == NULL || * outLen == 0 || type == RSA_TYPE_UNKNOWN ) {
@@ -3123,7 +3126,18 @@ static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
31233126 if (key -> devId != INVALID_DEVID )
31243127 #endif
31253128 {
3129+ #if defined(WOLF_CRYPTO_CB_RSA_PAD )
3130+ /* If we are here, either the RSA PAD callback was already called
3131+ * and returned that it could not implement for that padding scheme,
3132+ * or this is a public verify operation. Either way indicate to the
3133+ * callback that this should be a raw RSA operation with no padding.*/
3134+ XMEMSET (& padding , 0 , sizeof (RsaPadding ));
3135+ padding .pad_type = WC_RSA_NO_PAD ;
3136+ ret = wc_CryptoCb_RsaPad (in , inLen , out ,
3137+ outLen , type , key , rng , & padding );
3138+ #else
31263139 ret = wc_CryptoCb_Rsa (in , inLen , out , outLen , type , key , rng );
3140+ #endif
31273141 #ifndef WOLF_CRYPTO_CB_ONLY_RSA
31283142 if (ret != WC_NO_ERR_TRACE (CRYPTOCB_UNAVAILABLE ))
31293143 return ret ;
@@ -3231,6 +3245,9 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
32313245 int ret = 0 ;
32323246 int sz ;
32333247 int state ;
3248+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3249+ RsaPadding padding ;
3250+ #endif
32343251
32353252 if (in == NULL || inLen == 0 || out == NULL || key == NULL ) {
32363253 return BAD_FUNC_ARG ;
@@ -3327,6 +3344,29 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
33273344 #endif
33283345 #endif /* WOLFSSL_SE050 */
33293346
3347+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3348+ if (key -> devId != INVALID_DEVID ) {
3349+ XMEMSET (& padding , 0 , sizeof (RsaPadding ));
3350+ padding .pad_value = pad_value ;
3351+ padding .pad_type = pad_type ;
3352+ padding .hash = hash ;
3353+ padding .mgf = mgf ;
3354+ padding .label = label ;
3355+ padding .labelSz = labelSz ;
3356+ padding .saltLen = saltLen ;
3357+ ret = wc_CryptoCb_RsaPad (in , inLen , out , & outLen , rsa_type , key , rng ,
3358+ & padding );
3359+
3360+ if (ret != WC_NO_ERR_TRACE (CRYPTOCB_UNAVAILABLE )) {
3361+ if (ret < 0 ) {
3362+ break ;
3363+ }
3364+
3365+ ret = outLen ;
3366+ break ;
3367+ }
3368+ }
3369+ #endif
33303370 key -> state = RSA_STATE_ENCRYPT_PAD ;
33313371 ret = wc_RsaPad_ex (in , inLen , out , (word32 )sz , pad_value , rng , pad_type ,
33323372 hash , mgf , label , labelSz , saltLen ,
@@ -3406,6 +3446,9 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
34063446{
34073447 int ret = WC_NO_ERR_TRACE (RSA_WRONG_TYPE_E );
34083448 byte * pad = NULL ;
3449+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3450+ RsaPadding padding ;
3451+ #endif
34093452
34103453 if (in == NULL || inLen == 0 || out == NULL || key == NULL ) {
34113454 return BAD_FUNC_ARG ;
@@ -3516,6 +3559,25 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35163559 FALL_THROUGH ;
35173560
35183561 case RSA_STATE_DECRYPT_EXPTMOD :
3562+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3563+ if ((key -> devId != INVALID_DEVID ) && (rsa_type != RSA_PUBLIC_DECRYPT )) {
3564+ /* Everything except verify goes to crypto cb if
3565+ * WOLF_CRYPTO_CB_RSA_PAD defined */
3566+ XMEMSET (& padding , 0 , sizeof (RsaPadding ));
3567+ padding .pad_value = pad_value ;
3568+ padding .pad_type = pad_type ;
3569+ padding .hash = hash ;
3570+ padding .mgf = mgf ;
3571+ padding .label = label ;
3572+ padding .labelSz = labelSz ;
3573+ padding .saltLen = saltLen ;
3574+ ret = wc_CryptoCb_RsaPad (in , inLen , out ,
3575+ & outLen , rsa_type , key , rng , & padding );
3576+ if (ret != WC_NO_ERR_TRACE (CRYPTOCB_UNAVAILABLE )) {
3577+ break ;
3578+ }
3579+ }
3580+ #endif
35193581#if !defined(WOLFSSL_RSA_VERIFY_ONLY ) && !defined(WOLFSSL_RSA_VERIFY_INLINE ) && \
35203582 !defined(WOLFSSL_NO_MALLOC )
35213583 ret = wc_RsaFunction_ex (key -> data , inLen , key -> data , & key -> dataLen ,
0 commit comments