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 (RA4_CRYPTO_PKC_SUPPORT == ENABLED)
55 #if (MPI_SUPPORT == ENABLED)
82 if(aLen <= 512 && eLen <= 4 && pLen <= 512)
85 sce_oem_cmd_t command;
88 if(aLen <= 128 && pLen <= 128)
90 command = SCE_OEM_CMD_RSA1024_PUBLIC;
93 else if(aLen <= 256 && pLen <= 256)
95 command = SCE_OEM_CMD_RSA2048_PUBLIC;
98 else if(aLen <= 384 && pLen <= 384)
100 command = SCE_OEM_CMD_RSA3072_PUBLIC;
105 command = SCE_OEM_CMD_RSA4096_PUBLIC;
120 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
121 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
124 if(status == FSP_SUCCESS)
129 status = HW_SCE_Rsa1024ModularExponentEncryptSub(rsaArgs.
wrappedKey,
130 rsaArgs.
m, rsaArgs.
c);
134 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
135 rsaArgs.
m, rsaArgs.
c);
139 status = HW_SCE_Rsa3072ModularExponentEncryptSub(rsaArgs.
wrappedKey,
140 rsaArgs.
m, rsaArgs.
c);
144 status = HW_SCE_Rsa4096ModularExponentEncryptSub(rsaArgs.
wrappedKey,
145 rsaArgs.
m, rsaArgs.
c);
149 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
154 if(status == FSP_SUCCESS)
204 if(aLen <= 256 && eLen <= 256 && pLen <= 256)
207 sce_oem_cmd_t command;
210 if(aLen <= 128 && eLen <= 128 && pLen <= 128)
212 command = SCE_OEM_CMD_RSA1024_PRIVATE;
217 command = SCE_OEM_CMD_RSA2048_PRIVATE;
232 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
233 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
236 if(status == FSP_SUCCESS)
241 status = HW_SCE_Rsa1024ModularExponentDecryptSub(rsaArgs.
wrappedKey,
242 rsaArgs.
c, rsaArgs.
m);
246 status = HW_SCE_Rsa2048ModularExponentDecryptSub(rsaArgs.
wrappedKey,
247 rsaArgs.
c, rsaArgs.
m);
251 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
256 if(status == FSP_SUCCESS)
281 #if (RSA_SUPPORT == ENABLED)
324 if(nLen <= 256 && dLen <= 256)
329 else if(nLen > 0 && pLen > 0 && qLen > 0 && dpLen > 0 && dqLen > 0 &&
387 else if(nLen > 0 && dLen > 0)
403 #if (EC_SUPPORT == ENABLED)
439 sce_oem_cmd_t oemCommand;
442 if(
osStrcmp(curve->name,
"secp256k1") == 0)
444 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
445 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
449 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
451 curveType = SCE_ECC_CURVE_TYPE_NIST;
452 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
456 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
458 curveType = SCE_ECC_CURVE_TYPE_NIST;
459 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
463 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
465 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
466 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
470 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
472 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
473 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
497 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
498 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
501 if(status == FSP_SUCCESS)
504 if(curve->fieldSize == 256)
506 status = HW_SCE_Ecc256ScalarMultiplicationSub(&curveType,
509 else if(curve->fieldSize == 384)
511 status = HW_SCE_Ecc384ScalarMultiplicationSub(&curveType,
516 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
521 if(status == FSP_SUCCESS)
556 #if (ECDSA_SUPPORT == ENABLED)
570 const EcPrivateKey *privateKey,
const uint8_t *digest,
size_t digestLen,
579 sce_oem_cmd_t oemCommand;
583 if(privateKey == NULL || digest == NULL || signature == NULL)
587 if(privateKey->
curve == NULL)
591 curve = privateKey->
curve;
594 orderLen = (curve->orderSize + 7) / 8;
597 if(
osStrcmp(curve->name,
"secp256k1") == 0)
599 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
600 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
604 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
606 curveType = SCE_ECC_CURVE_TYPE_NIST;
607 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
611 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
613 curveType = SCE_ECC_CURVE_TYPE_NIST;
614 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
618 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
620 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
621 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
625 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
627 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
628 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
638 digestLen =
MIN(digestLen, orderLen);
645 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
652 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
653 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
656 if(status == FSP_SUCCESS)
659 if(curve->fieldSize == 256)
661 status = HW_SCE_EcdsaSignatureGenerateSub(&curveType, &command,
664 else if(curve->fieldSize == 384)
666 status = HW_SCE_EcdsaP384SignatureGenerateSub(&curveType,
671 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
676 if(status == FSP_SUCCESS)
679 signature->
curve = curve;
717 const uint8_t *digest,
size_t digestLen,
const EcdsaSignature *signature)
724 sce_oem_cmd_t oemCommand;
728 if(publicKey == NULL || digest == NULL || signature == NULL)
732 if(publicKey->
curve == NULL)
758 curve = publicKey->
curve;
761 orderLen = (curve->orderSize + 7) / 8;
764 if(
osStrcmp(curve->name,
"secp256k1") == 0)
766 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
767 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PUBLIC;
771 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
773 curveType = SCE_ECC_CURVE_TYPE_NIST;
774 oemCommand = SCE_OEM_CMD_ECC_P256_PUBLIC;
778 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
780 curveType = SCE_ECC_CURVE_TYPE_NIST;
781 oemCommand = SCE_OEM_CMD_ECC_P384_PUBLIC;
785 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
787 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
788 oemCommand = SCE_OEM_CMD_ECC_P256R1_PUBLIC;
792 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
794 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
795 oemCommand = SCE_OEM_CMD_ECC_P384R1_PUBLIC;
805 digestLen =
MIN(digestLen, orderLen);
812 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
829 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
830 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
q, ecArgs.
wrappedKey);
833 if(status == FSP_SUCCESS)
836 if(curve->fieldSize == 256)
838 status = HW_SCE_EcdsaSignatureVerificationSub(&curveType, &command,
841 else if(curve->fieldSize == 384)
843 status = HW_SCE_EcdsaP384SignatureVerificationSub(&curveType,
848 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;