1 /* renesas_common.c
2 *
3 * Copyright (C) 2006-2021 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL.
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20 */
21
22 #include <wolfssl/wolfcrypt/settings.h>
23
24 #if defined(WOLFSSL_RENESAS_SCEPROTECT) || defined(WOLFSSL_RENESAS_TSIP_TLS)
25
26 #if defined(WOLFSSL_RENESAS_SCEPROTECT)
27 #include <wolfssl/wolfcrypt/port/Renesas/renesas-sce-crypt.h>
28 #elif defined(WOLFSSL_RENESAS_TSIP_TLS)
29 #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
30 #endif
31
32 #include <wolfssl/wolfcrypt/wc_port.h>
33 #include <wolfssl/wolfcrypt/types.h>
34 #include <wolfssl/wolfcrypt/asn.h>
35 #include <wolfssl/internal.h>
36 #include <wolfssl/error-ssl.h>
37 #include <wolfssl/wolfcrypt/error-crypt.h>
38 #include <wolfssl/wolfcrypt/logging.h>
39
40 uint32_t g_CAscm_Idx = (uint32_t)-1; /* index of CM table */
41 static int devId = 7890; /* dev Id for Crypt Callback */
42
43 #ifdef WOLF_CRYPTO_CB
44
45 #include <wolfssl/wolfcrypt/cryptocb.h>
46
47 /* Renesas Security Library Common Callback
48 * For Crypto Call back
49 *
50 * devIdArg device Id
51 * info pointer to wc_CryptInfo
52 * ctx Crypto Callback context
53 * return 0 on success, otherwise MEMORY_E or BAD_FUNC_ARG on failure
54 */
Renesas_cmn_CryptoDevCb(int devIdArg,wc_CryptoInfo * info,void * ctx)55 static int Renesas_cmn_CryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
56 {
57 int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
58
59 WOLFSSL_ENTER("Renesas_cmn_CryptoDevCb");
60
61 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
62 TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
63 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
64 User_SCEPKCbInfo* cbInfo = (User_SCEPKCbInfo*) ctx;
65 #endif
66
67 if (info == NULL || ctx == NULL)
68 return BAD_FUNC_ARG;
69
70 #ifdef DEBUG_WOLFSSL
71 printf("CryptoDevCb: Algo Type %d session key set: %d\n",
72 info->algo_type, cbInfo->session_key_set);
73 #endif
74
75 #if defined(WOLFSSL_RENESAS_TSIP)
76
77 if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
78
79 #if !defined(NO_AES) || !defined(NO_DES3)
80 #ifdef HAVE_AESGCM
81 if (info->cipher.type == WC_CIPHER_AES_GCM &&
82 cbInfo->session_key_set == 1) {
83
84 if (info->cipher.enc) {
85
86 ret = wc_tsip_AesGcmEncrypt(
87 info->cipher.aesgcm_enc.aes,
88 (byte*)info->cipher.aesgcm_enc.out,
89 (byte*)info->cipher.aesgcm_enc.in,
90 info->cipher.aesgcm_enc.sz,
91 (byte*)info->cipher.aesgcm_enc.iv,
92 info->cipher.aesgcm_enc.ivSz,
93 (byte*)info->cipher.aesgcm_enc.authTag,
94 info->cipher.aesgcm_enc.authTagSz,
95 (byte*)info->cipher.aesgcm_enc.authIn,
96 info->cipher.aesgcm_enc.authInSz,
97 (void*)ctx);
98
99 }
100 else {
101
102 ret = wc_tsip_AesGcmDecrypt(
103 info->cipher.aesgcm_dec.aes,
104 (byte*)info->cipher.aesgcm_dec.out,
105 (byte*)info->cipher.aesgcm_dec.in,
106 info->cipher.aesgcm_dec.sz,
107 (byte*)info->cipher.aesgcm_dec.iv,
108 info->cipher.aesgcm_dec.ivSz,
109 (byte*)info->cipher.aesgcm_dec.authTag,
110 info->cipher.aesgcm_dec.authTagSz,
111 (byte*)info->cipher.aesgcm_dec.authIn,
112 info->cipher.aesgcm_dec.authInSz,
113 (void*)ctx);
114 }
115 }
116 #endif /* HAVE_AESGCM */
117 #ifdef HAVE_AES_CBC
118 if (info->cipher.type == WC_CIPHER_AES_CBC &&
119 cbInfo->session_key_set == 1) {
120
121 if (info->cipher.enc) {
122
123 ret = wc_tsip_AesCbcEncrypt(
124 info->cipher.aescbc.aes,
125 (byte*)info->cipher.aescbc.out,
126 (byte*)info->cipher.aescbc.in,
127 info->cipher.aescbc.sz);
128
129 }
130 else {
131
132 ret = wc_tsip_AesCbcDecrypt(
133 info->cipher.aescbc.aes,
134 (byte*)info->cipher.aescbc.out,
135 (byte*)info->cipher.aescbc.in,
136 info->cipher.aescbc.sz);
137 }
138 }
139 #endif /* HAVE_AES_CBC */
140 #endif /* !NO_AES || !NO_DES3 */
141 }
142 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
143
144 if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
145
146 #if !defined(NO_AES) || !defined(NO_DES3)
147 #ifdef HAVE_AESGCM
148 if (info->cipher.type == WC_CIPHER_AES_GCM) {
149
150 if (info->cipher.enc &&
151 ( cbInfo->session_key_set == 1 ||
152 (cbInfo->aes256_installedkey_set == 1 &&
153 info->cipher.aesgcm_enc.aes->keylen == 32) ||
154 (cbInfo->aes128_installedkey_set == 1 &&
155 info->cipher.aesgcm_enc.aes->keylen == 16))) {
156
157 if(cbInfo->aes256_installedkey_set == 1 &&
158 info->cipher.aesgcm_enc.aes->keylen == 32) {
159
160 XMEMCPY(&info->cipher.aesgcm_enc.aes->ctx.sce_wrapped_key,
161 &cbInfo->sce_wrapped_key_aes256,
162 sizeof(sce_aes_wrapped_key_t));
163 info->cipher.aesgcm_enc.aes->ctx.keySize = 32;
164
165 }
166 else if (cbInfo->aes128_installedkey_set == 1 &&
167 info->cipher.aesgcm_enc.aes->keylen == 16) {
168
169 XMEMCPY(&info->cipher.aesgcm_enc.aes->ctx.sce_wrapped_key,
170 &cbInfo->sce_wrapped_key_aes128,
171 sizeof(sce_aes_wrapped_key_t));
172 info->cipher.aesgcm_enc.aes->ctx.keySize = 16;
173 }
174
175 ret = wc_sce_AesGcmEncrypt(
176 info->cipher.aesgcm_enc.aes,
177 (byte*)info->cipher.aesgcm_enc.out,
178 (byte*)info->cipher.aesgcm_enc.in,
179 info->cipher.aesgcm_enc.sz,
180 (byte*)info->cipher.aesgcm_enc.iv,
181 info->cipher.aesgcm_enc.ivSz,
182 (byte*)info->cipher.aesgcm_enc.authTag,
183 info->cipher.aesgcm_enc.authTagSz,
184 (byte*)info->cipher.aesgcm_enc.authIn,
185 info->cipher.aesgcm_enc.authInSz,
186 (void*)ctx);
187
188 }
189 else if (cbInfo->session_key_set == 1 ||
190 (cbInfo->aes256_installedkey_set == 1 &&
191 info->cipher.aesgcm_dec.aes->keylen == 32) ||
192 (cbInfo->aes128_installedkey_set == 1 &&
193 info->cipher.aesgcm_dec.aes->keylen == 16)) {
194
195 if(cbInfo->aes256_installedkey_set == 1 &&
196 info->cipher.aesgcm_dec.aes->keylen == 32) {
197
198 XMEMCPY(&info->cipher.aesgcm_dec.aes->ctx.sce_wrapped_key,
199 &cbInfo->sce_wrapped_key_aes256,
200 sizeof(sce_aes_wrapped_key_t));
201 info->cipher.aesgcm_dec.aes->ctx.keySize = 32;
202
203 }
204 else if (cbInfo->aes128_installedkey_set == 1 &&
205 info->cipher.aesgcm_dec.aes->keylen == 16) {
206
207 XMEMCPY(&info->cipher.aesgcm_dec.aes->ctx.sce_wrapped_key,
208 &cbInfo->sce_wrapped_key_aes128,
209 sizeof(sce_aes_wrapped_key_t));
210 info->cipher.aesgcm_dec.aes->ctx.keySize = 16;
211 }
212
213 ret = wc_sce_AesGcmDecrypt(
214 info->cipher.aesgcm_dec.aes,
215 (byte*)info->cipher.aesgcm_dec.out,
216 (byte*)info->cipher.aesgcm_dec.in,
217 info->cipher.aesgcm_dec.sz,
218 (byte*)info->cipher.aesgcm_dec.iv,
219 info->cipher.aesgcm_dec.ivSz,
220 (byte*)info->cipher.aesgcm_dec.authTag,
221 info->cipher.aesgcm_dec.authTagSz,
222 (byte*)info->cipher.aesgcm_dec.authIn,
223 info->cipher.aesgcm_dec.authInSz,
224 (void*)ctx);
225 }
226 }
227 #endif /* HAVE_AESGCM */
228 #ifdef HAVE_AES_CBC
229 if ((info->cipher.type == WC_CIPHER_AES_CBC) &&
230 (cbInfo->session_key_set == 1 ||
231 (cbInfo->aes256_installedkey_set == 1 &&
232 info->cipher.aescbc.aes->keylen == 32) ||
233 (cbInfo->aes128_installedkey_set == 1 &&
234 info->cipher.aescbc.aes->keylen == 16))) {
235
236 if (info->cipher.enc) {
237 if(cbInfo->aes256_installedkey_set == 1 &&
238 info->cipher.aescbc.aes->keylen == 32) {
239 XMEMCPY(&info->cipher.aescbc.aes->ctx.sce_wrapped_key,
240 &cbInfo->sce_wrapped_key_aes256,
241 sizeof(sce_aes_wrapped_key_t));
242 info->cipher.aescbc.aes->ctx.keySize = 32;
243
244 }
245 else if (cbInfo->aes128_installedkey_set == 1 &&
246 info->cipher.aescbc.aes->keylen == 16) {
247 XMEMCPY(&info->cipher.aescbc.aes->ctx.sce_wrapped_key,
248 &cbInfo->sce_wrapped_key_aes128,
249 sizeof(sce_aes_wrapped_key_t));
250 info->cipher.aescbc.aes->ctx.keySize = 16;
251 }
252
253 ret = wc_sce_AesCbcEncrypt(
254 info->cipher.aescbc.aes,
255 (byte*)info->cipher.aescbc.out,
256 (byte*)info->cipher.aescbc.in,
257 info->cipher.aescbc.sz);
258 }
259 else {
260 if(cbInfo->aes256_installedkey_set == 1 &&
261 info->cipher.aescbc.aes->keylen == 32) {
262 XMEMCPY(&info->cipher.aescbc.aes->ctx.sce_wrapped_key,
263 &cbInfo->sce_wrapped_key_aes256,
264 sizeof(sce_aes_wrapped_key_t));
265 info->cipher.aescbc.aes->ctx.keySize = 32;
266
267 } else if (cbInfo->aes128_installedkey_set == 1 &&
268 info->cipher.aescbc.aes->keylen == 16) {
269 XMEMCPY(&info->cipher.aescbc.aes->ctx.sce_wrapped_key,
270 &cbInfo->sce_wrapped_key_aes128,
271 sizeof(sce_aes_wrapped_key_t));
272 info->cipher.aescbc.aes->ctx.keySize = 16;
273 }
274
275 ret = wc_sce_AesCbcDecrypt(
276 info->cipher.aescbc.aes,
277 (byte*)info->cipher.aescbc.out,
278 (byte*)info->cipher.aescbc.in,
279 info->cipher.aescbc.sz);
280 }
281 }
282 #endif /* HAVE_AES_CBC */
283 #endif /* !NO_AES || !NO_DES3 */
284 }
285 #endif /* TSIP or SCE */
286
287 (void)devIdArg;
288 (void)ctx;
289
290 return ret;
291 }
292
293 /* Renesas Security Library Common Entry Point
294 * For usable method
295 *
296 * ssl : a pointer to WOLFSSL object
297 * session_key_generated : if session key has been generated
298 * return 1 for usable, 0 for unusable
299 */
Renesas_cmn_usable(const WOLFSSL * ssl,byte session_key_generated)300 int Renesas_cmn_usable(const WOLFSSL* ssl, byte session_key_generated)
301 {
302 int ret;
303
304 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
305 ret = tsip_usable(ssl, session_key_generated);
306 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
307 ret = wc_sce_usable(ssl, session_key_generated);
308 #endif
309
310 return ret;
311 }
312
313 /* Renesas Security Library Common Method
314 * Crypt Callback initialization
315 *
316 * ssl : a pointer to WOLFSSL object
317 * ctx : callback context
318 * return valid device Id on success, otherwise INVALID_DEVIID
319 */
wc_CryptoCb_CryptInitRenesasCmn(WOLFSSL * ssl,void * ctx)320 int wc_CryptoCb_CryptInitRenesasCmn(WOLFSSL* ssl, void* ctx)
321 {
322 (void)ssl;
323 (void)ctx;
324
325 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
326 TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
327 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
328 User_SCEPKCbInfo* cbInfo = (User_SCEPKCbInfo*)ctx;
329 #endif
330
331 if (wc_CryptoCb_RegisterDevice(devId, Renesas_cmn_CryptoDevCb, cbInfo) < 0) {
332 return INVALID_DEVID;
333 }
334
335 if(ssl)
336 wolfSSL_SetDevId(ssl, devId);
337
338 return devId;
339 }
340
341 /* Renesas Security Library Common Method
342 * Clean up CryptCb
343 *
344 * id : a pointer to device id to unregister
345 * no return value
346 */
wc_CryptoCb_CleanupRenesasCmn(int * id)347 void wc_CryptoCb_CleanupRenesasCmn(int* id)
348 {
349 wc_CryptoCb_UnRegisterDevice(*id);
350 *id = INVALID_DEVID;
351 }
352
353 #endif /* WOLF_CRYPTO_CB */
354
355 /* Renesas Security Library Common Method
356 * Check CA index if CA can be used for SCE/TSIP because
357 * the CA has been verified by SCE/TSIP
358 *
359 * cmdIdx : ca index
360 * return 1 can be used, otherwise 0
361 */
Renesas_cmn_checkCA(word32 cmIdx)362 WOLFSSL_LOCAL byte Renesas_cmn_checkCA(word32 cmIdx)
363 {
364 WOLFSSL_ENTER("Renesas_cmn_checkCA");
365 return (cmIdx == g_CAscm_Idx? 1:0);
366 }
367
368 /* check if the root CA has been verified by TSIP/SCE,
369 * and it exists in the CM table.
370 */
sce_tsip_rootCAverified()371 static byte sce_tsip_rootCAverified( )
372 {
373 WOLFSSL_ENTER("sce_tsip_rootCAverified");
374 return (g_CAscm_Idx != (uint32_t)-1 ? 1:0);
375 }
376 /* Renesas Security Library Common Callback
377 * Callback for Rsa verify
378 *
379 * ssl the WOLFSSL object
380 * sig Buffer holding signature
381 * sigSz Length of signature in bytes
382 * out Buffer to hold hash
383 * key Buffer to hold ecc key
384 * keySz Length of key in bytes
385 * return FSP_SUCCESS(0) on sucess, otherwise FSP/TSIP error code
386 */
Renesas_cmn_RsaVerify(WOLFSSL * ssl,byte * sig,word32 sigSz,byte ** out,const byte * key,word32 keySz,void * ctx)387 WOLFSSL_LOCAL int Renesas_cmn_RsaVerify(WOLFSSL* ssl, byte* sig, word32 sigSz,
388 byte** out, const byte* key, word32 keySz, void* ctx)
389 {
390 int ret = 0;
391
392 WOLFSSL_ENTER("Renesas_cmn_RsaVerify");
393
394 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
395 ret = wc_tsip_RsaVerify(ssl, sig, sigSz, out, key, keySz, ctx);
396
397 if (ret == 0) {
398 /* Set Call back for SharedScret when being successful */
399 wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, wc_tsip_EccSharedSecret);
400 wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
401 }
402 else {
403 WOLFSSL_MSG("failed wc_tsip_RsaVerify");
404 wolfSSL_CTX_SetEccSharedSecretCb(ctx, NULL);
405 wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
406 }
407
408 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
409 ret = wc_SCE_RsaVerify(ssl, sig, sigSz, out,key, keySz, ctx);
410
411 if (ret == 0 && ret != CRYPTOCB_UNAVAILABLE) {
412 /* Set Call back for SharedScret when being successful */
413 wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, SCE_EccSharedSecret);
414 wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
415 }
416 else {
417 WOLFSSL_MSG("failed R_SCE_TLS_ServerKeyExchangeVerify");
418 wolfSSL_CTX_SetEccSharedSecretCb(ctx, NULL);
419 wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
420 }
421 #endif
422
423 return ret;
424 }
425 /* Renesas Security Library Common Callback
426 * Callback for Ecc verify
427 *
428 * ssl the WOLFSSL object
429 * sig Buffer holding signature
430 * sigSz Length of signature in bytes
431 * hash Buffer to hold hash
432 * hashSz Length of hash
433 * key Buffer to hold ecc key
434 * keySz Length of key in bytes
435 * result a pointer to int indicates if the verify is ok
436 * return FSP_SUCCESS(0) on sucess, otherwise FSP/TSIP error code
437 */
Renesas_cmn_EccVerify(WOLFSSL * ssl,const uint8_t * sig,uint32_t sigSz,const uint8_t * hash,uint32_t hashSz,const uint8_t * key,uint32_t keySz,int * result,void * ctx)438 WOLFSSL_LOCAL int Renesas_cmn_EccVerify(WOLFSSL* ssl, const uint8_t* sig,
439 uint32_t sigSz, const uint8_t* hash, uint32_t hashSz,
440 const uint8_t* key, uint32_t keySz, int* result, void* ctx)
441 {
442 int ret = 0;
443
444 WOLFSSL_ENTER("Renesas_cmn_EccVerify");
445
446 #if defined(WOLFSSL_RENESAS_TSIP)
447 ret = wc_tsip_EccVerify(ssl, sig, sigSz, hash, hashSz, key, keySz,
448 result, ctx);
449 if (ret == 0 && *result == 1) {
450 /* Set callback for SharedScret when being successful */
451 wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, wc_tsip_EccSharedSecret);
452 wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
453 }
454 else {
455 WOLFSSL_MSG("failed wc_tsip_EccVerify");
456 wolfSSL_CTX_SetEccSharedSecretCb(ctx, NULL);
457 wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
458 }
459 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
460 ret = wc_SCE_EccVerify(ssl, sig, sigSz, hash, hashSz, key, keySz,
461 result, ctx);
462 if (ret == 0 && *result == 1 && ret != CRYPTOCB_UNAVAILABLE) {
463 /* Set callback for SharedScret when being successful */
464 wolfSSL_CTX_SetEccSharedSecretCb(ssl->ctx, SCE_EccSharedSecret);
465 wolfSSL_SetEccSharedSecretCtx(ssl, ctx);
466 }
467 else {
468 WOLFSSL_MSG("failed R_SCE_TLS_ServerKeyExchangeVerify");
469 wolfSSL_CTX_SetEccSharedSecretCb(ctx, NULL);
470 wolfSSL_SetEccSharedSecretCtx(ssl, NULL);
471 }
472 #endif
473
474 return ret;
475 }
476 /* Renesas Security Library Common Entry Point
477 * For ROOT CA verifycation
478 *
479 * cert Buffer to hold cert
480 * cert_len Length of cert
481 * key_n_start Byte position of public key in cert
482 * key_n_len Length of public key in bytes
483 * key_e_start Byte position of public key exponent in cert
484 * key_e_len Length of public key exponent
485 * cm_row CA index
486 * return FSP_SUCCESS(0) on sucess, otherwise FSP/TSIP error code
487 */
wc_Renesas_cmn_RootCertVerify(const byte * cert,word32 cert_len,word32 key_n_start,word32 key_n_len,word32 key_e_start,word32 key_e_len,word32 cm_row)488 int wc_Renesas_cmn_RootCertVerify(const byte* cert, word32 cert_len, word32 key_n_start,
489 word32 key_n_len, word32 key_e_start, word32 key_e_len, word32 cm_row)
490 {
491 int ret;
492
493 WOLFSSL_ENTER("wc_Renesas_cmn_RootCertVerify");
494
495 if (sce_tsip_rootCAverified() == 0) {
496
497 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
498 ret = wc_tsip_tls_RootCertVerify(cert, cert_len, key_n_start,
499 key_n_len, key_e_start, key_e_len, cm_row);
500
501 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
502
503 ret = wc_sce_tls_RootCertVerify(cert, cert_len, key_n_start,
504 key_n_len, key_e_start, key_e_len, cm_row);
505 #endif
506 }
507 else {
508 /* already verified. skipped */
509 ret = 0;
510 }
511 return ret;
512 }
513
514 /* Renesas Security Library Common Callback
515 * Callback for tls finished
516 *
517 * ssl the WOLFSSL object
518 * side CLIENT or SERVER
519 * handshake_hash hash while doing handshake
520 * hashes calculated data by SCE/TSIP pseduo random function
521 * return FSP_SUCCESS(0) on sucess, otherwise FSP/TSIP error code
522 */
Renesas_cmn_TlsFinished(WOLFSSL * ssl,const byte * side,const byte * handshake_hash,byte * hashes,void * ctx)523 WOLFSSL_LOCAL int Renesas_cmn_TlsFinished(WOLFSSL* ssl, const byte *side,
524 const byte *handshake_hash,
525 byte *hashes, void* ctx)
526 {
527 int ret = -1;
528
529 (void)ctx;
530
531 WOLFSSL_ENTER("Renesas_cmn_TlsFinished");
532
533 /* sanity check */
534 if (ssl == NULL || ctx == NULL || side == NULL || handshake_hash == NULL ||
535 hashes == NULL )
536 return BAD_FUNC_ARG;
537 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
538 ret = wc_tsip_generateVerifyData(ssl->arrays->tsip_masterSecret,
539 side, handshake_hash, hashes);
540 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
541 ret = wc_sce_generateVerifyData(ssl->arrays->sce_masterSecret, /* master secret */
542 side, handshake_hash, hashes);
543 #endif
544
545 return ret;
546 }
547
548 /* Renesas Security Library Common Callback
549 * Callback for setting Encrypt Keys.
550 * Register callback for setting Encrypt Keys when keys are generated by SCE/TSIP
551 *
552 * ssl the WOLFSSL object
553 * ctx Callback context
554 * return 0 on sucess, -1 when keys are not generated by SCE/TSIP
555 */
Renesas_cmn_EncryptKeys(WOLFSSL * ssl,void * ctx)556 static int Renesas_cmn_EncryptKeys(WOLFSSL* ssl, void* ctx)
557 {
558 int ret;
559
560 WOLFSSL_ENTER("Renesas_cmn_EncryptKeys");
561
562 /* sanity check */
563 if (ssl == NULL || ctx == NULL)
564 return BAD_FUNC_ARG;
565
566 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
567 TsipUserCtx* cbInfo = (TsipUserCtx*)ctx;
568 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
569 User_SCEPKCbInfo* cbInfo = (User_SCEPKCbInfo*)ctx;
570 #endif
571
572 if (cbInfo->session_key_set == 1) {
573 ret = 0;
574
575 wolfSSL_CTX_SetTlsFinishedCb(ssl->ctx, Renesas_cmn_TlsFinished);
576 wolfSSL_SetTlsFinishedCtx(ssl, cbInfo);
577 }
578 else {
579 wolfSSL_CTX_SetTlsFinishedCb(ssl->ctx, NULL);
580 wolfSSL_SetTlsFinishedCtx(ssl, NULL);
581 ret = -1;
582 }
583
584 return ret;
585 }
586
587 /* Renesas Security Library Common Callback
588 * Callback for Sesssion Key generation
589 * Register callback for Set Keys when keys are successfully
590 * generated by SCE/TSIP
591 *
592 * ssl the WOLFSSL object
593 * ctx Callback context
594 * return FSP_SUCCESS(0) on sucess, otherwise SCE/TSIP error code
595 */
Renesas_cmn_generateSessionKey(WOLFSSL * ssl,void * ctx)596 WOLFSSL_LOCAL int Renesas_cmn_generateSessionKey(WOLFSSL* ssl, void* ctx)
597 {
598 int ret = -1;
599
600 (void)ctx;
601
602 WOLFSSL_ENTER("Renesas_cmn_generateSessionKey");
603
604 /* sanity check */
605 if (ssl == NULL || ctx == NULL)
606 return BAD_FUNC_ARG;
607 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
608 ret = wc_tsip_generateSessionKey(ssl, (TsipUserCtx*)ctx, devId);
609 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
610 ret = wc_sce_generateSessionKey(ssl, ctx, devId);
611 #endif
612 if (ret == 0) {
613 wolfSSL_CTX_SetEncryptKeysCb(ssl->ctx, Renesas_cmn_EncryptKeys);
614 wolfSSL_SetEncryptKeysCtx(ssl, ctx);
615 }
616 else {
617 wolfSSL_CTX_SetEncryptKeysCb(ssl->ctx, NULL);
618 wolfSSL_SetEncryptKeysCtx(ssl, NULL);
619 }
620
621 return ret;
622 }
623
624 /* Renesas Security Library Common Callback
625 * Callback for Premaster Secret generation
626 * Register callback for Set Keys when keys are successfully
627 * generated by SCE/TSIP
628 *
629 * ssl the WOLFSSL object
630 * premaster Buffer to hold pre master
631 * preSz Length of pre-master
632 * ctx Callback context
633 * return FSP_SUCCESS(0) on sucess,
634 * otherwise PROTOCOLCB_UNAVAILABLE
635 * so that caller could continue to process if want
636 */
Renesas_cmn_generatePremasterSecret(WOLFSSL * ssl,byte * premaster,word32 preSz,void * ctx)637 WOLFSSL_LOCAL int Renesas_cmn_generatePremasterSecret(WOLFSSL* ssl,
638 byte *premaster, word32 preSz, void* ctx)
639 {
640 int ret;
641
642 (void) ctx;
643 (void) ssl;
644
645 WOLFSSL_ENTER("Renesas_cmn_generatePremasterSecret");
646
647 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
648 if (Renesas_cmn_usable(ssl, 0)) {
649 ret = wc_tsip_generatePremasterSecret(premaster, preSz);
650 ssl->arrays->preMasterSz = preSz;
651 }
652 else
653 ret = PROTOCOLCB_UNAVAILABLE;
654
655 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
656 if (Renesas_cmn_usable(ssl, 0)) {
657 ret = wc_sce_generatePremasterSecret(premaster, preSz);
658 ssl->arrays->preMasterSz = preSz;
659 }
660 else
661 ret = PROTOCOLCB_UNAVAILABLE;
662 #endif
663
664 return ret;
665 }
666
667 /* Renesas Security Library Common Callback
668 * Callback for Master Secret generation
669 * Register callback for Session Key Generation when master secret is
670 * successfully generated by SCE/TSIP
671 *
672 * ssl the WOLFSSL object
673 * ctx Callback context
674 * return FSP_SUCCESS(0) on sucess,
675 * otherwise PROTOCOLCB_UNAVAILABLE
676 * so that caller could continue to process if want
677 */
Renesas_cmn_genMasterSecret(struct WOLFSSL * ssl,void * ctx)678 WOLFSSL_LOCAL int Renesas_cmn_genMasterSecret(struct WOLFSSL* ssl, void* ctx)
679 {
680 int ret = WOLFSSL_NOT_IMPLEMENTED;
681
682 (void) ret;
683 (void) ctx;
684
685 WOLFSSL_ENTER("Renesas_cmn_genMasterSecret");
686
687 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
688
689 if (Renesas_cmn_usable(ssl, 0)) {
690 #if (WOLFSSL_RENESAS_TSIP_VER >= 109 )
691 ret = wc_tsip_generateMasterSecretEx(
692 ssl->options.cipherSuite0,
693 ssl->options.cipherSuite,
694 ssl->arrays->preMasterSecret,
695 ssl->arrays->clientRandom,
696 ssl->arrays->serverRandom,
697 ssl->arrays->tsip_masterSecret);
698 #else
699 ret = wc_tsip_generateMasterSecret(
700 ssl->arrays->preMasterSecret,
701 ssl->arrays->clientRandom,
702 ssl->arrays->serverRandom,
703 ssl->arrays->tsip_masterSecret);
704 #endif
705
706 if (ret == 0) {
707 wc_tsip_storeKeyCtx(ssl, (TsipUserCtx*)ctx);
708 /* set Session Key generation Callback for use */
709 wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx,
710 Renesas_cmn_generateSessionKey);
711 wolfSSL_SetGenSessionKeyCtx(ssl, ctx);
712 }
713 else {
714 wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx, NULL);
715 wolfSSL_SetGenSessionKeyCtx(ssl, NULL);
716 }
717 }
718 else
719 ret = PROTOCOLCB_UNAVAILABLE;
720
721 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
722 if (Renesas_cmn_usable(ssl, 0)) {
723 ret = wc_sce_generateMasterSecret(
724 ssl->options.cipherSuite0,
725 ssl->options.cipherSuite,
726 ssl->arrays->preMasterSecret,
727 ssl->arrays->clientRandom,
728 ssl->arrays->serverRandom,
729 ssl->arrays->sce_masterSecret);
730 if (ret == 0) {
731 wc_sce_storeKeyCtx(ssl, ctx);
732 /* set Session Key generation Callback for use */
733 wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx,
734 Renesas_cmn_generateSessionKey);
735 wolfSSL_SetGenSessionKeyCtx(ssl, ctx);
736 }
737 else {
738 wolfSSL_CTX_SetGenSessionKeyCb(ssl->ctx, NULL);
739 wolfSSL_SetGenSessionKeyCtx(ssl, NULL);
740 }
741 }
742 else
743 ret = PROTOCOLCB_UNAVAILABLE;
744
745 #endif
746 return ret;
747 }
748
749 /* Renesas Security Library Common Callback
750 * Callback for Rsa Encryption
751 *
752 * ssl the WOLFSSL object
753 * in Buffer to hold plain text
754 * inSz Length of plain text
755 * out Buffer to hold cipher text
756 * outSz Length of cipher text buffer
757 * KeyDer Buffer holding Key in der format
758 * KeySz Length of Key Der
759 * ctx Callback context
760 * return FSP_SUCCESS(0) on sucess,
761 * otherwise CRYPTOCB_UNAVAILABLE
762 * so that caller could continue to process if want
763 */
Renesas_cmn_RsaEnc(WOLFSSL * ssl,const unsigned char * in,unsigned int inSz,unsigned char * out,word32 * outSz,const unsigned char * keyDer,unsigned int keySz,void * ctx)764 WOLFSSL_LOCAL int Renesas_cmn_RsaEnc(WOLFSSL* ssl, const unsigned char* in,
765 unsigned int inSz, unsigned char* out, word32* outSz,
766 const unsigned char* keyDer, unsigned int keySz, void* ctx)
767 {
768 int ret;
769 int EncSz;
770
771 (void)ctx;
772 (void)in;
773 (void)inSz;
774 (void)keyDer;
775 (void)keySz;
776 (void)EncSz;
777
778 WOLFSSL_ENTER("Renesas_cmn_RsaEnc");
779
780 /* sanity check */
781 if (ssl == NULL || in == NULL || out == NULL || keyDer == NULL ||
782 ctx == NULL) {
783 return BAD_FUNC_ARG;
784 }
785
786 EncSz = wc_RsaEncryptSize(ssl->peerRsaKey);
787
788 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
789
790 if (tsip_usable(ssl, 0)) {
791 if (EncSz == 256) {
792 ret = wc_tsip_generateEncryptPreMasterSecret(ssl, out, outSz);
793 }
794 else {
795 WOLFSSL_MSG("TSIP can only handle 256 bytes for RSA encrypt size.");
796 ret = CRYPTOCB_UNAVAILABLE;
797 }
798 }
799 else {
800 ret = CRYPTOCB_UNAVAILABLE;
801 }
802
803 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
804 if (wc_sce_usable(ssl, 0) && EncSz == 256) {
805 ret = wc_sce_generateEncryptPreMasterSecret(ssl, out, outSz);
806 }
807 else {
808 if (EncSz != 256)
809 WOLFSSL_MSG("SCE cannot use"
810 "because Rsa Encrypt Size isn't 256 bytes(2048 bits).");
811
812 ret = CRYPTOCB_UNAVAILABLE;
813 }
814 #endif
815 return ret;
816 }
817
818 /* Renesas Security Library Common Callback
819 * Callback for Verify hmac
820 *
821 * ssl the WOLFSSL object
822 * message Buffer to hold message
823 * inSz Length of message
824 * macSz Length of mac size
825 * content content of inner data
826 * ctx Callback context
827 * return FSP_SUCCESS(0) on sucess,
828 * otherwise PROTOCOLCB_UNAVAILABLE
829 * so that caller could continue to process if want
830 */
Renesas_cmn_VerifyHmac(WOLFSSL * ssl,const byte * message,word32 messageSz,word32 macSz,word32 content,void * ctx)831 WOLFSSL_LOCAL int Renesas_cmn_VerifyHmac(WOLFSSL *ssl, const byte* message,
832 word32 messageSz, word32 macSz, word32 content, void* ctx)
833 {
834 int ret;
835 (void)ctx;
836
837 WOLFSSL_ENTER("Renesas_cmn_VerifyHmac");
838
839 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
840 if (tsip_usable(ssl, 1)) {
841 ret = wc_tsip_ShaXHmacVerify(ssl, message, messageSz, macSz, content);
842 }
843 else
844 ret = PROTOCOLCB_UNAVAILABLE;
845 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
846 if (wc_sce_usable(ssl, 1)) {
847 ret = wc_sce_Sha256VerifyHmac(ssl, message, messageSz, macSz, content);
848 }
849 else
850 ret = PROTOCOLCB_UNAVAILABLE;
851 #endif
852
853 return ret;
854 }
855
856 /* Renesas Security Library Common Callback
857 * Callback for TLS hmac
858 *
859 * ssl the WOLFSSL object
860 * digest Buffer to hold digest by hmac
861 * in Buffer to hold in data
862 * sz Length of in data
863 * padSz Length of padding
864 * content content of inner data
865 * epocOrder
866 * return FSP_SUCCESS(0) on sucess, otherwise error code
867 */
Renesas_cmn_TLS_hmac(WOLFSSL * ssl,byte * digest,const byte * in,word32 sz,int padSz,int content,int verify,int epochOrder)868 WOLFSSL_LOCAL int Renesas_cmn_TLS_hmac(WOLFSSL* ssl, byte* digest,
869 const byte* in, word32 sz, int padSz, int content, int verify, int epochOrder)
870 {
871 int ret;
872 byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
873
874 WOLFSSL_ENTER("Renesas_cmn_TLS_hmac");
875
876 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
877 if (Renesas_cmn_usable(ssl, 1)) {
878
879 wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
880
881 if (ssl->specs.hash_size == WC_SHA_DIGEST_SIZE) {
882 ret = wc_tsip_Sha1HmacGenerate(ssl, myInner,
883 WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
884 }
885 else if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE) {
886 ret = wc_tsip_Sha256HmacGenerate(ssl, myInner,
887 WOLFSSL_TLS_HMAC_INNER_SZ, in, sz, digest);
888 }
889 else {
890 ret = TSIP_MAC_DIGSZ_E;
891 }
892 }
893 else {
894 WOLFSSL_MSG("TLS_hmac is used instead of TSIP");
895 /* fall through to original TLS hmac method when TSIP cannot be used */
896 ret = TLS_hmac(ssl, digest, in, sz, padSz, content, verify, epochOrder);
897
898 }
899
900 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
901 if (Renesas_cmn_usable(ssl, 1)) {
902 if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE) {
903 wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
904 ret = wc_sce_Sha256GenerateHmac(ssl, myInner, WOLFSSL_TLS_HMAC_INNER_SZ,
905 in, sz, digest);
906 }
907 else
908 ret = TSIP_MAC_DIGSZ_E;
909 }
910 else {
911 /* fall through to original TLS hmac method when SCE cannot be used */
912 ret = TLS_hmac(ssl, digest, in, sz, padSz, content, verify, epochOrder);
913 }
914
915 #endif
916
917 return ret;
918 }
919
920 /* Renesas Security Library Common Callback
921 * Callback for Signature PK Rsa verify
922 *
923 * sig Buffer holding signature
924 * sigSz Length of signature in bytes
925 * out Buffer to hold hash
926 * keyDer Buffer to hold rsa key
927 * keySz Length of key in bytes
928 * ctx Callback context
929 * return FSP_SUCCESS(0) on sucess,
930 * otherwise CRYPTOCB_UNAVAILABLE
931 * so that caller could continue to process if want
932 */
Renesas_cmn_SigPkCbRsaVerify(unsigned char * sig,unsigned int sigSz,unsigned char ** out,const unsigned char * keyDer,unsigned int keySz,void * ctx)933 WOLFSSL_LOCAL int Renesas_cmn_SigPkCbRsaVerify(unsigned char* sig,
934 unsigned int sigSz, unsigned char** out, const unsigned char* keyDer,
935 unsigned int keySz, void* ctx)
936 {
937 int ret;
938 CertAttribute* CertAtt;
939
940 (void)out;
941 (void)keyDer;
942 (void)keySz;
943
944 WOLFSSL_ENTER("Renesas_cmn_SigPkCbRsaVerify");
945
946 /* sanity check */
947 if (sig == NULL || out == NULL || keyDer == NULL || ctx == NULL)
948 return BAD_FUNC_ARG;
949
950 CertAtt = (CertAttribute*)ctx;
951 if (!CertAtt) {
952 return CRYPTOCB_UNAVAILABLE;
953 }
954
955 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
956 if (CertAtt->keyIndex != NULL)
957 {
958 ret = wc_tsip_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
959 CertAtt->pubkey_n_start - CertAtt->certBegin,
960 CertAtt->pubkey_n_len - 1,
961 CertAtt->pubkey_e_start - CertAtt->certBegin,
962 CertAtt->pubkey_e_len -1,
963 (uint8_t*)CertAtt->keyIndex);
964 if (ret == 0){
965 CertAtt->verifyByTSIP_SCE = 1;
966 }
967 else {
968 WOLFSSL_MSG("RSA Verify by TSIP didn't match");
969 ret = ASN_SIG_CONFIRM_E;
970 }
971 }
972 else
973 ret = CRYPTOCB_UNAVAILABLE;
974
975 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
976 if (CertAtt->keyIndex != NULL)
977 {
978 ret = wc_sce_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
979 CertAtt->pubkey_n_start - CertAtt->certBegin,
980 CertAtt->pubkey_n_len - 1,
981 CertAtt->pubkey_e_start - CertAtt->certBegin,
982 CertAtt->pubkey_e_len -1,
983 (uint8_t*)CertAtt->keyIndex);
984 if (ret == 0){
985 CertAtt->verifyByTSIP_SCE = 1;
986 }
987 else {
988 WOLFSSL_MSG("RSA Verify by SCE didn't match");
989 ret = ASN_SIG_CONFIRM_E;
990 }
991 }
992 else
993 ret = CRYPTOCB_UNAVAILABLE;
994 #endif
995
996 return ret;
997 }
998
999 /* Renesas Security Library Common Callback
1000 * Callback for Signature PK Ecc verify
1001 *
1002 * sig Buffer holding signature
1003 * sigSz Length of signature in bytes
1004 * has Buffer to hold hash
1005 * hashSz Length of hash
1006 * keyDer Buffer to hold rsa key
1007 * keySz Length of key in bytes
1008 * result A pointer to int indicates a result
1009 * ctx Callback context
1010 * return FSP_SUCCESS(0) on sucess,
1011 * otherwise CRYPTOCB_UNAVAILABLE
1012 * so that caller could continue to process if want
1013 */
Renesas_cmn_SigPkCbEccVerify(const unsigned char * sig,unsigned int sigSz,const unsigned char * hash,unsigned int hashSz,const unsigned char * keyDer,unsigned int keySz,int * result,void * ctx)1014 WOLFSSL_LOCAL int Renesas_cmn_SigPkCbEccVerify(const unsigned char* sig,
1015 unsigned int sigSz, const unsigned char* hash, unsigned int hashSz,
1016 const unsigned char* keyDer, unsigned int keySz,
1017 int* result, void* ctx)
1018 {
1019 int ret;
1020 CertAttribute* CertAtt;
1021
1022 (void)result;
1023 (void)keyDer;
1024 (void)keySz;
1025 (void)hash;
1026 (void)hashSz;
1027
1028 WOLFSSL_ENTER("Renesas_cmn_SigPkCbEccVerify");
1029
1030 /* sanity check */
1031 if (sig == NULL || keyDer == NULL || hash == NULL || ctx == NULL ||
1032 result == NULL)
1033 return BAD_FUNC_ARG;
1034
1035
1036 CertAtt = (CertAttribute*)ctx;
1037 if (!CertAtt) {
1038 return CRYPTOCB_UNAVAILABLE;
1039 }
1040
1041 #if defined(WOLFSSL_RENESAS_TSIP_TLS)
1042 if (CertAtt->keyIndex != NULL)
1043 {
1044 ret = wc_tsip_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
1045 CertAtt->pubkey_n_start - CertAtt->certBegin,
1046 CertAtt->pubkey_n_len - 1,
1047 CertAtt->pubkey_e_start - CertAtt->certBegin,
1048 CertAtt->pubkey_e_len -1,
1049 (uint8_t*)CertAtt->keyIndex);
1050 if (ret == 0) {
1051 CertAtt->verifyByTSIP_SCE = 1;
1052 *result = 1;
1053 }
1054 else {
1055 WOLFSSL_MSG("RSA Verify by TSIP didn't match");
1056 ret = ASN_SIG_CONFIRM_E;
1057 }
1058 }
1059 else
1060 ret = CRYPTOCB_UNAVAILABLE;
1061 #elif defined(WOLFSSL_RENESAS_SCEPROTECT)
1062 if (CertAtt->keyIndex != NULL)
1063 {
1064 ret = wc_sce_tls_CertVerify(CertAtt->cert, CertAtt->certSz, sig, sigSz,
1065 CertAtt->pubkey_n_start - CertAtt->certBegin,
1066 CertAtt->pubkey_n_len - 1,
1067 CertAtt->pubkey_e_start - CertAtt->certBegin,
1068 CertAtt->pubkey_e_len -1,
1069 (uint8_t*)CertAtt->keyIndex);
1070 if (ret == 0) {
1071 CertAtt->verifyByTSIP_SCE = 1;
1072 *result = 1;
1073 }
1074 else {
1075 WOLFSSL_MSG("RSA Verify by SCE didn't match");
1076 ret = ASN_SIG_CONFIRM_E;
1077 }
1078 }
1079 else
1080 ret = CRYPTOCB_UNAVAILABLE;
1081 #endif
1082
1083 return ret;
1084 }
1085
1086 #endif /* SCEPROTECT || TSIP */
1087