32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "hw_sce_private.h"
36 #include "hw_sce_ra_private.h"
37 #include "hw_sce_rsa_private.h"
38 #include "hw_sce_ecc_private.h"
48 #if (RA6_CRYPTO_PKC_SUPPORT == ENABLED)
80 #if (BSP_FEATURE_CRYPTO_HAS_SCE9 != 0)
82 if((aLen <= 256 && eLen <= 4 && pLen <= 256) ||
83 (aLen <= 384 && eLen <= 4 && pLen <= 384) ||
84 (aLen <= 512 && eLen <= 4 && pLen <= 512))
86 sce_oem_cmd_t command;
91 command = SCE_OEM_CMD_RSA2048_PUBLIC;
96 command = SCE_OEM_CMD_RSA3072_PUBLIC;
101 command = SCE_OEM_CMD_RSA4096_PUBLIC;
116 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
117 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
120 if(status == FSP_SUCCESS)
125 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
126 rsaArgs.
m, rsaArgs.
c);
130 status = HW_SCE_Rsa3072ModularExponentEncryptSub(rsaArgs.
wrappedKey,
131 rsaArgs.
m, rsaArgs.
c);
135 status = HW_SCE_Rsa4096ModularExponentEncryptSub(rsaArgs.
wrappedKey,
136 rsaArgs.
m, rsaArgs.
c);
140 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
145 if(status == FSP_SUCCESS)
161 if(aLen <= 256 && eLen <= 4 && pLen <= 256)
174 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
175 SCE_OEM_CMD_RSA2048_PUBLIC, NULL, NULL, (uint8_t *) rsaArgs.
key,
179 if(status == FSP_SUCCESS)
182 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
183 rsaArgs.
m, rsaArgs.
c);
187 if(status == FSP_SUCCESS)
238 if(aLen <= 256 && eLen <= 256 && pLen <= 256)
251 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
252 SCE_OEM_CMD_RSA2048_PRIVATE, NULL, NULL, (uint8_t *) rsaArgs.
key,
256 if(status == FSP_SUCCESS)
259 status = HW_SCE_Rsa2048ModularExponentDecryptSub(rsaArgs.
wrappedKey,
260 rsaArgs.
c, rsaArgs.
m);
264 if(status == FSP_SUCCESS)
330 if(nLen <= 256 && dLen <= 256)
335 else if(nLen > 0 && pLen > 0 && qLen > 0 && dpLen > 0 && dqLen > 0 &&
393 else if(nLen > 0 && dLen > 0)
426 sce_oem_cmd_t oemCommand;
431 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
432 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
438 curveType = SCE_ECC_CURVE_TYPE_NIST;
439 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
445 curveType = SCE_ECC_CURVE_TYPE_NIST;
446 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
452 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
453 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
459 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
460 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
480 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
481 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
484 if(status == FSP_SUCCESS)
489 status = HW_SCE_Ecc256ScalarMultiplicationSub(&curveType,
494 status = HW_SCE_Ecc384ScalarMultiplicationSub(&curveType,
499 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
504 if(status == FSP_SUCCESS)
551 const uint8_t *digest,
size_t digestLen,
EcdsaSignature *signature)
559 sce_oem_cmd_t oemCommand;
562 if(params == NULL || privateKey == NULL || digest == NULL || signature == NULL)
571 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
572 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
578 curveType = SCE_ECC_CURVE_TYPE_NIST;
579 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
585 curveType = SCE_ECC_CURVE_TYPE_NIST;
586 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
592 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
593 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
599 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
600 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
610 digestLen =
MIN(digestLen, orderLen);
617 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
623 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
624 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
627 if(status == FSP_SUCCESS)
632 status = HW_SCE_EcdsaSignatureGenerateSub(&curveType, &command,
637 status = HW_SCE_EcdsaP384SignatureGenerateSub(&curveType,
642 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
647 if(status == FSP_SUCCESS)
684 const EcPublicKey *publicKey,
const uint8_t *digest,
size_t digestLen,
692 sce_oem_cmd_t oemCommand;
695 if(params == NULL || publicKey == NULL || digest == NULL || signature == NULL)
720 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
721 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PUBLIC;
727 curveType = SCE_ECC_CURVE_TYPE_NIST;
728 oemCommand = SCE_OEM_CMD_ECC_P256_PUBLIC;
734 curveType = SCE_ECC_CURVE_TYPE_NIST;
735 oemCommand = SCE_OEM_CMD_ECC_P384_PUBLIC;
741 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
742 oemCommand = SCE_OEM_CMD_ECC_P256R1_PUBLIC;
748 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
749 oemCommand = SCE_OEM_CMD_ECC_P384R1_PUBLIC;
759 digestLen =
MIN(digestLen, orderLen);
766 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
777 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
778 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
q, ecArgs.
wrappedKey);
781 if(status == FSP_SUCCESS)
786 status = HW_SCE_EcdsaSignatureVerificationSub(&curveType, &command,
791 status = HW_SCE_EcdsaP384SignatureVerificationSub(&curveType,
796 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;