2222#include <wolfssl/wolfcrypt/settings.h>
2323
2424#if !defined(NO_RSA ) && \
25- (defined(WOLFSSL_RENESAS_TSIP_TLS ) || \
26- defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY ))
25+ defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY )
2726
2827#include <string.h>
2928#include <stdio.h>
@@ -121,6 +120,7 @@ WOLFSSL_LOCAL int wc_tsip_MakeRsaKey(int size, void* ctx)
121120
122121 info -> keyflgs_crypt .bits .rsapri1024_key_set = 1 ;
123122 info -> keyflgs_crypt .bits .rsapub1024_key_set = 1 ;
123+ info -> wrappedKeyType = TSIP_KEY_TYPE_RSA1024 ;
124124 }
125125 else if (size == 2048 ) {
126126 XFREE (info -> rsa2048pri_keyIdx , NULL , DYNAMIC_TYPE_RSA_BUFFER );
@@ -158,6 +158,7 @@ WOLFSSL_LOCAL int wc_tsip_MakeRsaKey(int size, void* ctx)
158158
159159 info -> keyflgs_crypt .bits .rsapri2048_key_set = 1 ;
160160 info -> keyflgs_crypt .bits .rsapub2048_key_set = 1 ;
161+ info -> wrappedKeyType = TSIP_KEY_TYPE_RSA2048 ;
161162 }
162163 }
163164
@@ -167,42 +168,14 @@ WOLFSSL_LOCAL int wc_tsip_MakeRsaKey(int size, void* ctx)
167168
168169 return 0 ;
169170}
170-
171-
172- /* Perform Rsa verify by TSIP
173- * Assumes to be called by Crypt Callback
171+ /* Generate TSIP key index if needed
174172 *
175- * in Buffer to hold plaintext
176- * inLen Length of plaintext in bytes
177- * out Buffer to hold generated signature
178- * outLen Length of signature in bytes
179- * key rsa key object
180- * ctx The callback context
181- * return FSP_SUCCESS(0) on Success, otherwise negative value
173+ * tuc struct pointer of TsipUserCtx
174+ * return FSP_SUCCESS(0) on Success, otherwise CRYPTOCB_UNAVAILABLE
182175 */
183-
184- WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs (wc_CryptoInfo * info , TsipUserCtx * tuc )
176+ static int tsip_RsakeyImport (TsipUserCtx * tuc )
185177{
186178 int ret = 0 ;
187- e_tsip_err_t err = TSIP_SUCCESS ;
188- tsip_rsa_byte_data_t hashData , sigData ;
189- uint8_t tsip_hash_type ;
190-
191- /* sanity check */
192- if (info == NULL || tuc == NULL ){
193- return BAD_FUNC_ARG ;
194- }
195-
196- if (ret == 0 ) {
197- if (tuc -> sign_hash_type == md5_mac )
198- tsip_hash_type = R_TSIP_RSA_HASH_MD5 ;
199- else if (tuc -> sign_hash_type == sha_mac )
200- tsip_hash_type = R_TSIP_RSA_HASH_SHA1 ;
201- else if (tuc -> sign_hash_type == sha256_mac )
202- tsip_hash_type = R_TSIP_RSA_HASH_SHA256 ;
203- else
204- ret = CRYPTOCB_UNAVAILABLE ;
205- }
206179
207180 switch (tuc -> wrappedKeyType ) {
208181 case TSIP_KEY_TYPE_RSA1024 :
@@ -230,7 +203,110 @@ WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
230203 break ;
231204 }
232205
206+ return ret ;
207+ }
208+
209+ /* Perform rsa encryption/decryption by TSIP
210+ * Assumes to be called by Crypt Callback
211+ *
212+ * info struct pointer of wc_CryptoInfo including necessary info
213+ * tuc struct pointer of TsipUserCtx including TSIP key info
214+ * return FSP_SUCCESS(0) on Success, otherwise negative value
215+ */
216+ WOLFSSL_LOCAL int wc_tsip_RsaFunction (wc_CryptoInfo * info , TsipUserCtx * tuc )
217+ {
218+ int ret ;
219+ int keySize ;
220+ int type ;
221+ tsip_rsa_byte_data_t plain , cipher ;
222+
223+
224+ if (info == NULL || tuc == NULL ) {
225+ return BAD_FUNC_ARG ;
226+ }
227+
228+ if (tsip_RsakeyImport (tuc ) == 0 ) {
229+ type = info -> pk .rsa .type ;
230+ keySize = (int )tuc -> wrappedKeyType ;
231+
232+ if ((ret = tsip_hw_lock ()) == 0 ) {
233+ if (type == RSA_PUBLIC_ENCRYPT ) {
234+ plain .pdata = (uint8_t * )info -> pk .rsa .in ;
235+ plain .data_length = info -> pk .rsa .inLen ;
236+ cipher .pdata = (uint8_t * )info -> pk .rsa .out ;
237+ cipher .data_length = info -> pk .rsa .outLen ;
238+
239+ if (keySize == TSIP_KEY_TYPE_RSA1024 ) {
240+ ret = R_TSIP_RsaesPkcs1024Encrypt (& plain , & cipher ,
241+ tuc -> rsa1024pub_keyIdx );
242+ }
243+ else if (keySize == TSIP_KEY_TYPE_RSA2048 ) {
244+ ret = R_TSIP_RsaesPkcs2048Encrypt (& plain , & cipher ,
245+ tuc -> rsa2048pub_keyIdx );
246+ }
247+ else {
248+ WOLFSSL_MSG ("keySize is invalid, neither 128 or 256 bytes, "
249+ "1024 or 2048 bits." );
250+ return BAD_FUNC_ARG ;
251+ }
252+ }
253+ else if (type == RSA_PRIVATE_DECRYPT ) {
254+ plain .pdata = (uint8_t * )info -> pk .rsa .out ;
255+ plain .data_length = info -> pk .rsa .outLen ;
256+ cipher .pdata = (uint8_t * )info -> pk .rsa .in ;
257+ cipher .data_length = info -> pk .rsa .inLen ;
258+
259+ if (keySize == TSIP_KEY_TYPE_RSA1024 ) {
260+ ret = R_TSIP_RsaesPkcs1024Decrypt (& cipher , & plain ,
261+ tuc -> rsa1024pri_keyIdx );
262+ }
263+ else if (keySize == TSIP_KEY_TYPE_RSA2048 ) {
264+ ret = R_TSIP_RsaesPkcs2048Decrypt (& cipher , & plain ,
265+ tuc -> rsa2048pri_keyIdx );
266+ }
267+ else {
268+ WOLFSSL_MSG ("keySize is invalid, neither 128 or 256 bytes, "
269+ "1024 or 2048 bits." );
270+ return BAD_FUNC_ARG ;
271+ }
272+ }
273+ tsip_hw_unlock ();
274+ }
275+ }
276+ return ret ;
277+ }
278+ /* Perform Rsa verify by TSIP
279+ * Assumes to be called by Crypt Callback
280+ *
281+ * info struct pointer of wc_CryptoInfo including necessary info
282+ * tuc struct pointer of TsipUserCtx including TSIP key info
283+ * return FSP_SUCCESS(0) on Success, otherwise negative value
284+ */
285+
286+ WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs (wc_CryptoInfo * info , TsipUserCtx * tuc )
287+ {
288+ int ret = 0 ;
289+ e_tsip_err_t err = TSIP_SUCCESS ;
290+ tsip_rsa_byte_data_t hashData , sigData ;
291+ uint8_t tsip_hash_type ;
292+
293+ /* sanity check */
294+ if (info == NULL || tuc == NULL ){
295+ return BAD_FUNC_ARG ;
296+ }
297+
233298 if (ret == 0 ) {
299+ if (tuc -> sign_hash_type == md5_mac )
300+ tsip_hash_type = R_TSIP_RSA_HASH_MD5 ;
301+ else if (tuc -> sign_hash_type == sha_mac )
302+ tsip_hash_type = R_TSIP_RSA_HASH_SHA1 ;
303+ else if (tuc -> sign_hash_type == sha256_mac )
304+ tsip_hash_type = R_TSIP_RSA_HASH_SHA256 ;
305+ else
306+ ret = CRYPTOCB_UNAVAILABLE ;
307+ }
308+
309+ if (tsip_RsakeyImport (tuc ) == 0 ) {
234310 hashData .pdata = (uint8_t * )info -> pk .rsa .in ;
235311 hashData .data_length = info -> pk .rsa .inLen ;
236312 hashData .data_type =
0 commit comments