@@ -3127,6 +3127,9 @@ static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
31273127 int ret = 0 ;
31283128 (void )rng ;
31293129 (void )checkSmallCt ;
3130+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3131+ RsaPadding padding ;
3132+ #endif
31303133
31313134 if (key == NULL || in == NULL || inLen == 0 || out == NULL ||
31323135 outLen == NULL || * outLen == 0 || type == RSA_TYPE_UNKNOWN ) {
@@ -3138,7 +3141,18 @@ static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
31383141 if (key -> devId != INVALID_DEVID )
31393142 #endif
31403143 {
3144+ #if defined(WOLF_CRYPTO_CB_RSA_PAD )
3145+ /* If we are here, either the RSA PAD callback was already called
3146+ * and returned that it could not implement for that padding scheme,
3147+ * or this is a public verify operation. Either way indicate to the
3148+ * callback that this should be a raw RSA operation with no padding.*/
3149+ XMEMSET (& padding , 0 , sizeof (RsaPadding ));
3150+ padding .pad_type = WC_RSA_NO_PAD ;
3151+ ret = wc_CryptoCb_RsaPad (in , inLen , out ,
3152+ outLen , type , key , rng , & padding );
3153+ #else
31413154 ret = wc_CryptoCb_Rsa (in , inLen , out , outLen , type , key , rng );
3155+ #endif
31423156 #ifndef WOLF_CRYPTO_CB_ONLY_RSA
31433157 if (ret != WC_NO_ERR_TRACE (CRYPTOCB_UNAVAILABLE ))
31443158 return ret ;
@@ -3246,6 +3260,9 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
32463260 int ret = 0 ;
32473261 int sz ;
32483262 int state ;
3263+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3264+ RsaPadding padding ;
3265+ #endif
32493266
32503267 if (in == NULL || inLen == 0 || out == NULL || key == NULL ) {
32513268 return BAD_FUNC_ARG ;
@@ -3342,6 +3359,29 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
33423359 #endif
33433360 #endif /* WOLFSSL_SE050 */
33443361
3362+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3363+ if (key -> devId != INVALID_DEVID ) {
3364+ XMEMSET (& padding , 0 , sizeof (RsaPadding ));
3365+ padding .pad_value = pad_value ;
3366+ padding .pad_type = pad_type ;
3367+ padding .hash = hash ;
3368+ padding .mgf = mgf ;
3369+ padding .label = label ;
3370+ padding .labelSz = labelSz ;
3371+ padding .saltLen = saltLen ;
3372+ ret = wc_CryptoCb_RsaPad (in , inLen , out , & outLen , rsa_type , key , rng ,
3373+ & padding );
3374+
3375+ if (ret != WC_NO_ERR_TRACE (CRYPTOCB_UNAVAILABLE )) {
3376+ if (ret < 0 ) {
3377+ break ;
3378+ }
3379+
3380+ ret = outLen ;
3381+ break ;
3382+ }
3383+ }
3384+ #endif
33453385 key -> state = RSA_STATE_ENCRYPT_PAD ;
33463386 ret = wc_RsaPad_ex (in , inLen , out , (word32 )sz , pad_value , rng , pad_type ,
33473387 hash , mgf , label , labelSz , saltLen ,
@@ -3421,6 +3461,9 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
34213461{
34223462 int ret = WC_NO_ERR_TRACE (RSA_WRONG_TYPE_E );
34233463 byte * pad = NULL ;
3464+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3465+ RsaPadding padding ;
3466+ #endif
34243467
34253468 if (in == NULL || inLen == 0 || out == NULL || key == NULL ) {
34263469 return BAD_FUNC_ARG ;
@@ -3531,6 +3574,25 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35313574 FALL_THROUGH ;
35323575
35333576 case RSA_STATE_DECRYPT_EXPTMOD :
3577+ #if defined(WOLF_CRYPTO_CB ) && defined(WOLF_CRYPTO_CB_RSA_PAD )
3578+ if ((key -> devId != INVALID_DEVID ) && (rsa_type != RSA_PUBLIC_DECRYPT )) {
3579+ /* Everything except verify goes to crypto cb if
3580+ * WOLF_CRYPTO_CB_RSA_PAD defined */
3581+ XMEMSET (& padding , 0 , sizeof (RsaPadding ));
3582+ padding .pad_value = pad_value ;
3583+ padding .pad_type = pad_type ;
3584+ padding .hash = hash ;
3585+ padding .mgf = mgf ;
3586+ padding .label = label ;
3587+ padding .labelSz = labelSz ;
3588+ padding .saltLen = saltLen ;
3589+ ret = wc_CryptoCb_RsaPad (in , inLen , out ,
3590+ & outLen , rsa_type , key , rng , & padding );
3591+ if (ret != WC_NO_ERR_TRACE (CRYPTOCB_UNAVAILABLE )) {
3592+ break ;
3593+ }
3594+ }
3595+ #endif
35343596#if !defined(WOLFSSL_RSA_VERIFY_ONLY ) && !defined(WOLFSSL_RSA_VERIFY_INLINE ) && \
35353597 !defined(WOLFSSL_NO_MALLOC )
35363598 ret = wc_RsaFunction_ex (key -> data , inLen , key -> data , & key -> dataLen ,
0 commit comments