1
- /* renesas_sce_rsa .c
1
+ /* renesas_tsip_rsa .c
2
2
*
3
3
* Copyright (C) 2006-2025 wolfSSL Inc.
4
4
*
38
38
#include <wolfssl/wolfcrypt/rsa.h>
39
39
#include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
40
40
41
- #ifdef WOLFSSL_RENESAS_TSIP_CRYPTONLY
41
+ /* Make sure at least RSA 1024 or RSA 2048 is enabled */
42
+ #if (defined(TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 0 ) && \
43
+ (defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 0 )
44
+ #error Please enable TSIP RSA 1024 or 2048. \
45
+ This code assumes at least one is enabled
46
+ #endif
47
+
42
48
/* Make RSA key for TSIP and set it to callback ctx
43
49
* Assumes to be called by Crypt Callback
44
50
*
@@ -50,63 +56,84 @@ int wc_tsip_MakeRsaKey(int size, void* ctx)
50
56
{
51
57
e_tsip_err_t ret ;
52
58
TsipUserCtx * info = (TsipUserCtx * )ctx ;
53
-
59
+ #if defined( TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 1
54
60
tsip_rsa1024_key_pair_index_t * tsip_pair1024_key = NULL ;
61
+ #endif
62
+ #if defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 1
55
63
tsip_rsa2048_key_pair_index_t * tsip_pair2048_key = NULL ;
64
+ #endif
56
65
57
66
/* sanity check */
58
67
if (ctx == NULL )
59
68
return BAD_FUNC_ARG ;
60
69
70
+
61
71
if (size != 1024 && size != 2048 ) {
62
- WOLFSSL_MSG ("Failed to generate key pair by TSIP " );
72
+ WOLFSSL_MSG ("TSIP RSA KeyGen bit size not supported " );
63
73
return CRYPTOCB_UNAVAILABLE ;
64
74
}
75
+ #if defined(TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 0
76
+ if (size == 1024 )
77
+ return CRYPTOCB_UNAVAILABLE ;
78
+ #endif
79
+ #if defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 0
80
+ if (size == 2048 )
81
+ return CRYPTOCB_UNAVAILABLE ;
82
+ #endif
65
83
66
84
if ((ret = tsip_hw_lock ()) == 0 ) {
67
85
if (size == 1024 ) {
86
+ #if defined(TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 1
68
87
tsip_pair1024_key =
69
- (tsip_rsa1024_key_pair_index_t * )XMALLOC (
70
- sizeof (tsip_rsa1024_key_pair_index_t ), NULL ,
71
- DYNAMIC_TYPE_RSA_BUFFER );
72
- if (tsip_pair1024_key == NULL )
88
+ (tsip_rsa1024_key_pair_index_t * )XMALLOC (
89
+ sizeof (tsip_rsa1024_key_pair_index_t ), NULL ,
90
+ DYNAMIC_TYPE_RSA_BUFFER );
91
+ if (tsip_pair1024_key == NULL ) {
92
+ tsip_hw_unlock ();
73
93
return MEMORY_E ;
74
-
94
+ }
75
95
ret = R_TSIP_GenerateRsa1024RandomKeyIndex (tsip_pair1024_key );
96
+ #endif
76
97
}
77
98
else if (size == 2048 ) {
99
+ #if defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 1
78
100
tsip_pair2048_key =
79
- (tsip_rsa2048_key_pair_index_t * )XMALLOC (
80
- sizeof (tsip_rsa2048_key_pair_index_t ), NULL ,
81
- DYNAMIC_TYPE_RSA_BUFFER );
82
- if (tsip_pair2048_key == NULL )
101
+ (tsip_rsa2048_key_pair_index_t * )XMALLOC (
102
+ sizeof (tsip_rsa2048_key_pair_index_t ), NULL ,
103
+ DYNAMIC_TYPE_RSA_BUFFER );
104
+ if (tsip_pair2048_key == NULL ) {
105
+ tsip_hw_unlock ();
83
106
return MEMORY_E ;
84
-
107
+ }
85
108
ret = R_TSIP_GenerateRsa2048RandomKeyIndex (tsip_pair2048_key );
109
+ #endif
86
110
}
87
-
88
111
if (ret == TSIP_SUCCESS ) {
89
112
if (size == 1024 ) {
113
+ #if defined(TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 1
90
114
XFREE (info -> rsa1024pri_keyIdx , NULL , DYNAMIC_TYPE_RSA_BUFFER );
91
115
XFREE (info -> rsa1024pub_keyIdx , NULL , DYNAMIC_TYPE_RSA_BUFFER );
116
+
92
117
info -> rsa1024pri_keyIdx =
93
- (tsip_rsa1024_private_key_index_t * )XMALLOC (
94
- sizeof (tsip_rsa1024_private_key_index_t ), NULL ,
95
- DYNAMIC_TYPE_RSA_BUFFER );
118
+ (tsip_rsa1024_private_key_index_t * )XMALLOC (
119
+ sizeof (tsip_rsa1024_private_key_index_t ), NULL ,
120
+ DYNAMIC_TYPE_RSA_BUFFER );
96
121
97
122
if (info -> rsa1024pri_keyIdx == NULL ) {
98
123
XFREE (tsip_pair1024_key , NULL , DYNAMIC_TYPE_RSA_BUFFER );
124
+ tsip_hw_unlock ();
99
125
return MEMORY_E ;
100
126
}
101
127
102
128
info -> rsa1024pub_keyIdx =
103
- (tsip_rsa1024_public_key_index_t * )XMALLOC (
104
- sizeof (tsip_rsa1024_public_key_index_t ), NULL ,
105
- DYNAMIC_TYPE_RSA_BUFFER );
129
+ (tsip_rsa1024_public_key_index_t * )XMALLOC (
130
+ sizeof (tsip_rsa1024_public_key_index_t ), NULL ,
131
+ DYNAMIC_TYPE_RSA_BUFFER );
106
132
107
133
if (info -> rsa1024pub_keyIdx == NULL ) {
108
134
XFREE (tsip_pair1024_key , NULL , DYNAMIC_TYPE_RSA_BUFFER );
109
135
XFREE (info -> rsa1024pri_keyIdx , NULL , DYNAMIC_TYPE_RSA_BUFFER );
136
+ tsip_hw_unlock ();
110
137
return MEMORY_E ;
111
138
}
112
139
/* copy generated key pair and free malloced key */
@@ -121,17 +148,21 @@ int wc_tsip_MakeRsaKey(int size, void* ctx)
121
148
info -> keyflgs_crypt .bits .rsapri1024_key_set = 1 ;
122
149
info -> keyflgs_crypt .bits .rsapub1024_key_set = 1 ;
123
150
info -> wrappedKeyType = TSIP_KEY_TYPE_RSA1024 ;
151
+ #endif
124
152
}
125
153
else if (size == 2048 ) {
154
+ #if defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 1
126
155
XFREE (info -> rsa2048pri_keyIdx , NULL , DYNAMIC_TYPE_RSA_BUFFER );
127
156
XFREE (info -> rsa2048pub_keyIdx , NULL , DYNAMIC_TYPE_RSA_BUFFER );
157
+
128
158
info -> rsa2048pri_keyIdx =
129
- (tsip_rsa2048_private_key_index_t * )XMALLOC (
130
- sizeof (tsip_rsa2048_private_key_index_t ), NULL ,
131
- DYNAMIC_TYPE_RSA_BUFFER );
159
+ (tsip_rsa2048_private_key_index_t * )XMALLOC (
160
+ sizeof (tsip_rsa2048_private_key_index_t ), NULL ,
161
+ DYNAMIC_TYPE_RSA_BUFFER );
132
162
133
163
if (info -> rsa2048pri_keyIdx == NULL ) {
134
164
XFREE (tsip_pair2048_key , NULL , DYNAMIC_TYPE_RSA_BUFFER );
165
+ tsip_hw_unlock ();
135
166
return MEMORY_E ;
136
167
}
137
168
@@ -144,6 +175,7 @@ int wc_tsip_MakeRsaKey(int size, void* ctx)
144
175
XFREE (tsip_pair2048_key , NULL , DYNAMIC_TYPE_RSA_BUFFER );
145
176
XFREE (info -> rsa2048pri_keyIdx , NULL ,
146
177
DYNAMIC_TYPE_RSA_BUFFER );
178
+ tsip_hw_unlock ();
147
179
return MEMORY_E ;
148
180
}
149
181
@@ -159,15 +191,15 @@ int wc_tsip_MakeRsaKey(int size, void* ctx)
159
191
info -> keyflgs_crypt .bits .rsapri2048_key_set = 1 ;
160
192
info -> keyflgs_crypt .bits .rsapub2048_key_set = 1 ;
161
193
info -> wrappedKeyType = TSIP_KEY_TYPE_RSA2048 ;
194
+ #endif
162
195
}
163
196
}
164
-
165
197
tsip_hw_unlock ();
166
198
}
167
199
168
-
169
200
return 0 ;
170
201
}
202
+
171
203
/* Generate TSIP key index if needed
172
204
*
173
205
* tuc struct pointer of TsipUserCtx
@@ -178,6 +210,7 @@ static int tsip_RsakeyImport(TsipUserCtx* tuc)
178
210
int ret = 0 ;
179
211
180
212
switch (tuc -> wrappedKeyType ) {
213
+ #if defined(TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 1
181
214
case TSIP_KEY_TYPE_RSA1024 :
182
215
if (tuc -> keyflgs_crypt .bits .rsapub1024_key_set != 1 ) {
183
216
ret = tsip_ImportPublicKey (tuc , tuc -> wrappedKeyType );
@@ -188,6 +221,8 @@ static int tsip_RsakeyImport(TsipUserCtx* tuc)
188
221
189
222
}
190
223
break ;
224
+ #endif
225
+ #if defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 1
191
226
case TSIP_KEY_TYPE_RSA2048 :
192
227
if (tuc -> keyflgs_crypt .bits .rsapub2048_key_set != 1 ) {
193
228
ret = tsip_ImportPublicKey (tuc , tuc -> wrappedKeyType );
@@ -197,6 +232,7 @@ static int tsip_RsakeyImport(TsipUserCtx* tuc)
197
232
ret = CRYPTOCB_UNAVAILABLE ;
198
233
}
199
234
break ;
235
+ #endif
200
236
default :
201
237
WOLFSSL_MSG ("wrapped private key is not supported" );
202
238
ret = CRYPTOCB_UNAVAILABLE ;
@@ -220,7 +256,6 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
220
256
int type ;
221
257
tsip_rsa_byte_data_t plain , cipher ;
222
258
223
-
224
259
if (info == NULL || tuc == NULL ) {
225
260
return BAD_FUNC_ARG ;
226
261
}
@@ -230,48 +265,57 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
230
265
keySize = (int )tuc -> wrappedKeyType ;
231
266
232
267
if ((ret = tsip_hw_lock ()) == 0 ) {
233
- if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT ) {
268
+ if (type == RSA_PUBLIC_ENCRYPT ||
269
+ type == RSA_PUBLIC_DECRYPT )
270
+ {
234
271
plain .pdata = (uint8_t * )info -> pk .rsa .in ;
235
272
plain .data_length = info -> pk .rsa .inLen ;
236
273
cipher .pdata = (uint8_t * )info -> pk .rsa .out ;
237
274
cipher .data_length = * (info -> pk .rsa .outLen );
238
275
239
- if (keySize == TSIP_KEY_TYPE_RSA1024 ) {
276
+ switch (keySize ) {
277
+ #if defined(TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 1
278
+ case TSIP_KEY_TYPE_RSA1024 :
240
279
ret = R_TSIP_RsaesPkcs1024Encrypt (& plain , & cipher ,
241
280
tuc -> rsa1024pub_keyIdx );
242
- }
243
- else if (keySize == TSIP_KEY_TYPE_RSA2048 ) {
281
+ break ;
282
+ #endif
283
+ #if defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 1
284
+ case TSIP_KEY_TYPE_RSA2048 :
244
285
ret = R_TSIP_RsaesPkcs2048Encrypt (& plain , & cipher ,
245
286
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 ;
287
+ break ;
288
+ #endif
289
+ default :
290
+ ret = CRYPTOCB_UNAVAILABLE ;
251
291
}
252
292
if (ret == 0 ) {
253
293
* (info -> pk .rsa .outLen ) = cipher .data_length ;
254
294
}
255
295
}
256
- else if (type == RSA_PRIVATE_DECRYPT || type == RSA_PRIVATE_ENCRYPT )
296
+ else if (type == RSA_PRIVATE_DECRYPT ||
297
+ type == RSA_PRIVATE_ENCRYPT )
257
298
{
258
299
plain .pdata = (uint8_t * )info -> pk .rsa .out ;
259
300
plain .data_length = * (info -> pk .rsa .outLen );
260
301
cipher .pdata = (uint8_t * )info -> pk .rsa .in ;
261
302
cipher .data_length = info -> pk .rsa .inLen ;
262
303
263
- if (keySize == TSIP_KEY_TYPE_RSA1024 ) {
304
+ switch (keySize ) {
305
+ #if defined(TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 1
306
+ case TSIP_KEY_TYPE_RSA1024 :
264
307
ret = R_TSIP_RsaesPkcs1024Decrypt (& cipher , & plain ,
265
308
tuc -> rsa1024pri_keyIdx );
266
- }
267
- else if (keySize == TSIP_KEY_TYPE_RSA2048 ) {
309
+ break ;
310
+ #endif
311
+ #if defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 1
312
+ case TSIP_KEY_TYPE_RSA2048 :
268
313
ret = R_TSIP_RsaesPkcs2048Decrypt (& cipher , & plain ,
269
314
tuc -> rsa2048pri_keyIdx );
270
- }
271
- else {
272
- WOLFSSL_MSG ("keySize is invalid, neither 128 or 256 bytes, "
273
- "1024 or 2048 bits." );
274
- return BAD_FUNC_ARG ;
315
+ break ;
316
+ #endif
317
+ default :
318
+ ret = CRYPTOCB_UNAVAILABLE ;
275
319
}
276
320
if (ret == 0 ) {
277
321
* (info -> pk .rsa .outLen ) = plain .data_length ;
@@ -280,6 +324,10 @@ int wc_tsip_RsaFunction(wc_CryptoInfo* info, TsipUserCtx* tuc)
280
324
tsip_hw_unlock ();
281
325
}
282
326
}
327
+
328
+ if (ret != 0 ) {
329
+ WOLFSSL_MSG ("RSA key size is not supported (only 1024 or 2048 bits)" );
330
+ }
283
331
return ret ;
284
332
}
285
333
/* Perform Rsa verify by TSIP
@@ -324,6 +372,7 @@ int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
324
372
325
373
if ((ret = tsip_hw_lock ()) == 0 ) {
326
374
switch (tuc -> wrappedKeyType ) {
375
+ #if defined(TSIP_RSAES_1024 ) && TSIP_RSAES_1024 == 1
327
376
case TSIP_KEY_TYPE_RSA1024 :
328
377
err = R_TSIP_RsassaPkcs1024SignatureVerification (& sigData ,
329
378
& hashData ,
@@ -340,6 +389,8 @@ int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
340
389
ret = WC_HW_E ;
341
390
}
342
391
break ;
392
+ #endif
393
+ #if defined(TSIP_RSAES_2048 ) && TSIP_RSAES_2048 == 1
343
394
case TSIP_KEY_TYPE_RSA2048 :
344
395
err = R_TSIP_RsassaPkcs2048SignatureVerification (& sigData ,
345
396
& hashData ,
@@ -356,13 +407,14 @@ int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info, TsipUserCtx* tuc)
356
407
ret = WC_HW_E ;
357
408
}
358
409
break ;
410
+ #endif
411
+ default :
412
+ ret = CRYPTOCB_UNAVAILABLE ;
359
413
}
360
414
tsip_hw_unlock ();
361
415
}
362
416
}
363
417
364
418
return ret ;
365
419
}
366
- #endif /* WOLFSSL_RENESAS_TSIP_CRYPTONLY */
367
- #endif /* WOLFSSL_RENESAS_TSIP_TLS || \
368
- WOLFSSL_RENESAS_TSIP_CRYPTONLY */
420
+ #endif /* !NO_RSA && WOLFSSL_RENESAS_TSIP_CRYPTONLY */
0 commit comments