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"
49 #if (RA6_CRYPTO_PKC_SUPPORT == ENABLED)
55 #if (MPI_SUPPORT == ENABLED)
82 #if (BSP_FEATURE_CRYPTO_HAS_SCE9 != 0)
84 if((aLen <= 256 && eLen <= 4 && pLen <= 256) ||
85 (aLen <= 384 && eLen <= 4 && pLen <= 384) ||
86 (aLen <= 512 && eLen <= 4 && pLen <= 512))
88 sce_oem_cmd_t command;
93 command = SCE_OEM_CMD_RSA2048_PUBLIC;
98 command = SCE_OEM_CMD_RSA3072_PUBLIC;
103 command = SCE_OEM_CMD_RSA4096_PUBLIC;
118 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
119 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
122 if(status == FSP_SUCCESS)
127 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
128 rsaArgs.
m, rsaArgs.
c);
132 status = HW_SCE_Rsa3072ModularExponentEncryptSub(rsaArgs.
wrappedKey,
133 rsaArgs.
m, rsaArgs.
c);
137 status = HW_SCE_Rsa4096ModularExponentEncryptSub(rsaArgs.
wrappedKey,
138 rsaArgs.
m, rsaArgs.
c);
142 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
147 if(status == FSP_SUCCESS)
163 if(aLen <= 256 && eLen <= 4 && pLen <= 256)
176 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
177 SCE_OEM_CMD_RSA2048_PUBLIC, NULL, NULL, (uint8_t *) rsaArgs.
key,
181 if(status == FSP_SUCCESS)
184 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
185 rsaArgs.
m, rsaArgs.
c);
189 if(status == FSP_SUCCESS)
240 if(aLen <= 256 && eLen <= 256 && pLen <= 256)
253 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
254 SCE_OEM_CMD_RSA2048_PRIVATE, NULL, NULL, (uint8_t *) rsaArgs.
key,
258 if(status == FSP_SUCCESS)
261 status = HW_SCE_Rsa2048ModularExponentDecryptSub(rsaArgs.
wrappedKey,
262 rsaArgs.
c, rsaArgs.
m);
266 if(status == FSP_SUCCESS)
291 #if (RSA_SUPPORT == ENABLED)
334 if(nLen <= 256 && dLen <= 256)
339 else if(nLen > 0 && pLen > 0 && qLen > 0 && dpLen > 0 && dqLen > 0 &&
397 else if(nLen > 0 && dLen > 0)
413 #if (EC_SUPPORT == ENABLED)
449 sce_oem_cmd_t oemCommand;
452 if(
osStrcmp(curve->name,
"secp256k1") == 0)
454 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
455 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
459 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
461 curveType = SCE_ECC_CURVE_TYPE_NIST;
462 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
466 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
468 curveType = SCE_ECC_CURVE_TYPE_NIST;
469 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
473 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
475 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
476 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
480 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
482 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
483 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
507 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
508 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
511 if(status == FSP_SUCCESS)
514 if(curve->fieldSize == 256)
516 status = HW_SCE_Ecc256ScalarMultiplicationSub(&curveType,
519 else if(curve->fieldSize == 384)
521 status = HW_SCE_Ecc384ScalarMultiplicationSub(&curveType,
526 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
531 if(status == FSP_SUCCESS)
566 #if (ECDSA_SUPPORT == ENABLED)
580 const EcPrivateKey *privateKey,
const uint8_t *digest,
size_t digestLen,
589 sce_oem_cmd_t oemCommand;
593 if(privateKey == NULL || digest == NULL || signature == NULL)
597 if(privateKey->
curve == NULL)
601 curve = privateKey->
curve;
604 orderLen = (curve->orderSize + 7) / 8;
607 if(
osStrcmp(curve->name,
"secp256k1") == 0)
609 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
610 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
614 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
616 curveType = SCE_ECC_CURVE_TYPE_NIST;
617 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
621 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
623 curveType = SCE_ECC_CURVE_TYPE_NIST;
624 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
628 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
630 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
631 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
635 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
637 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
638 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
648 digestLen =
MIN(digestLen, orderLen);
655 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
662 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
663 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
666 if(status == FSP_SUCCESS)
669 if(curve->fieldSize == 256)
671 status = HW_SCE_EcdsaSignatureGenerateSub(&curveType, &command,
674 else if(curve->fieldSize == 384)
676 status = HW_SCE_EcdsaP384SignatureGenerateSub(&curveType,
681 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
686 if(status == FSP_SUCCESS)
689 signature->
curve = curve;
727 const uint8_t *digest,
size_t digestLen,
const EcdsaSignature *signature)
734 sce_oem_cmd_t oemCommand;
738 if(publicKey == NULL || digest == NULL || signature == NULL)
742 if(publicKey->
curve == NULL)
768 curve = publicKey->
curve;
771 orderLen = (curve->orderSize + 7) / 8;
774 if(
osStrcmp(curve->name,
"secp256k1") == 0)
776 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
777 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PUBLIC;
781 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
783 curveType = SCE_ECC_CURVE_TYPE_NIST;
784 oemCommand = SCE_OEM_CMD_ECC_P256_PUBLIC;
788 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
790 curveType = SCE_ECC_CURVE_TYPE_NIST;
791 oemCommand = SCE_OEM_CMD_ECC_P384_PUBLIC;
795 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
797 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
798 oemCommand = SCE_OEM_CMD_ECC_P256R1_PUBLIC;
802 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
804 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
805 oemCommand = SCE_OEM_CMD_ECC_P384R1_PUBLIC;
815 digestLen =
MIN(digestLen, orderLen);
822 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
839 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
840 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
q, ecArgs.
wrappedKey);
843 if(status == FSP_SUCCESS)
846 if(curve->fieldSize == 256)
848 status = HW_SCE_EcdsaSignatureVerificationSub(&curveType, &command,
851 else if(curve->fieldSize == 384)
853 status = HW_SCE_EcdsaP384SignatureVerificationSub(&curveType,
858 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;