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 (RA8_CRYPTO_PKC_SUPPORT == ENABLED)
55 #if (MPI_SUPPORT == ENABLED)
73 sce_oem_cmd_t command;
83 if((aLen <= 128 && eLen <= 4 && pLen == 128) ||
84 (aLen <= 256 && eLen <= 4 && pLen == 256) ||
85 (aLen <= 384 && eLen <= 4 && pLen == 384) ||
86 (aLen <= 512 && eLen <= 4 && pLen == 512))
91 command = SCE_OEM_CMD_RSA1024_PUBLIC;
95 command = SCE_OEM_CMD_RSA2048_PUBLIC;
99 command = SCE_OEM_CMD_RSA3072_PUBLIC;
103 command = SCE_OEM_CMD_RSA4096_PUBLIC;
117 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
118 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
121 if(status == FSP_SUCCESS)
126 status = HW_SCE_Rsa1024ModularExponentEncryptSub(rsaArgs.
wrappedKey,
127 rsaArgs.
m, rsaArgs.
c);
131 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
132 rsaArgs.
m, rsaArgs.
c);
136 status = HW_SCE_Rsa3072ModularExponentEncryptSub(rsaArgs.
wrappedKey,
137 rsaArgs.
m, rsaArgs.
c);
141 status = HW_SCE_Rsa4096ModularExponentEncryptSub(rsaArgs.
wrappedKey,
142 rsaArgs.
m, rsaArgs.
c);
146 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
151 if(status == FSP_SUCCESS)
192 sce_oem_cmd_t command;
202 if((aLen <= 128 && eLen <= 128 && pLen == 128) ||
203 (aLen <= 256 && eLen <= 256 && pLen == 256) ||
204 (aLen <= 384 && eLen <= 384 && pLen == 384) ||
205 (aLen <= 512 && eLen <= 512 && pLen == 512))
210 command = SCE_OEM_CMD_RSA1024_PRIVATE;
214 command = SCE_OEM_CMD_RSA2048_PRIVATE;
218 command = SCE_OEM_CMD_RSA3072_PRIVATE;
222 command = SCE_OEM_CMD_RSA4096_PRIVATE;
236 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
237 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
240 if(status == FSP_SUCCESS)
245 status = HW_SCE_Rsa1024ModularExponentDecryptSub(rsaArgs.
wrappedKey,
246 rsaArgs.
c, rsaArgs.
m);
250 status = HW_SCE_Rsa2048ModularExponentDecryptSub(rsaArgs.
wrappedKey,
251 rsaArgs.
c, rsaArgs.
m);
255 status = HW_SCE_Rsa3072ModularExponentDecryptSub(rsaArgs.
wrappedKey,
256 rsaArgs.
c, rsaArgs.
m);
260 status = HW_SCE_Rsa4096ModularExponentDecryptSub(rsaArgs.
wrappedKey,
261 rsaArgs.
c, rsaArgs.
m);
265 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
270 if(status == FSP_SUCCESS)
295 #if (RSA_SUPPORT == ENABLED)
338 if((nLen == 128 && dLen <= 128) || (nLen == 384 && dLen <= 384))
343 else if(nLen > 0 && pLen > 0 && qLen > 0 && dpLen > 0 && dqLen > 0 &&
401 else if(nLen > 0 && dLen > 0)
417 #if (EC_SUPPORT == ENABLED)
456 sce_oem_cmd_t oemCommand;
457 const uint32_t *domainParams;
460 modLen = (curve->fieldSize + 31) / 32;
462 orderLen = (curve->orderSize + 31) / 32;
465 n = (curve->fieldSize + 7) / 8;
466 n = (
n + 15U) & ~15U;
469 if(
osStrcmp(curve->name,
"secp256k1") == 0)
471 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
472 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
473 domainParams = DomainParam_Koblitz_secp256k1;
477 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
479 curveType = SCE_ECC_CURVE_TYPE_NIST;
480 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
481 domainParams = DomainParam_NIST_P256;
485 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
487 curveType = SCE_ECC_CURVE_TYPE_NIST;
488 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
489 domainParams = DomainParam_NIST_P384;
493 else if(
osStrcmp(curve->name,
"secp521r1") == 0)
495 curveType = SCE_ECC_CURVE_TYPE_NIST;
496 oemCommand = SCE_OEM_CMD_ECC_P521_PRIVATE;
497 domainParams = DomainParam_NIST_P521;
501 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
503 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
504 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
505 domainParams = DomainParam_Brainpool_256r1;
509 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
511 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
512 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
513 domainParams = DomainParam_Brainpool_384r1;
517 else if(
osStrcmp(curve->name,
"brainpoolP512r1") == 0)
519 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
520 oemCommand = SCE_OEM_CMD_ECC_P512R1_PRIVATE;
521 domainParams = DomainParam_Brainpool_512r1;
545 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
546 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
549 if(status == FSP_SUCCESS)
552 if(curve->fieldSize == 256)
554 status = HW_SCE_Ecc256ScalarMultiplicationSub(&curveType,
555 &command, ecArgs.
wrappedKey, ecArgs.
g, domainParams, ecArgs.
q);
557 else if(curve->fieldSize == 384)
559 status = HW_SCE_Ecc384ScalarMultiplicationSub(&curveType,
562 else if(curve->fieldSize == 512)
564 status = HW_SCE_Ecc512ScalarMultiplicationSub(ecArgs.
wrappedKey,
565 ecArgs.
g, domainParams, ecArgs.
q);
567 else if(curve->fieldSize == 521)
569 status = HW_SCE_Ecc521ScalarMultiplicationSub(ecArgs.
wrappedKey,
570 ecArgs.
g, domainParams, ecArgs.
q);
574 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
579 if(status == FSP_SUCCESS)
583 (uint8_t *) ecArgs.
q + offset, modLen * 4,
591 (uint8_t *) ecArgs.
q + offset +
n, modLen * 4,
616 #if (ECDSA_SUPPORT == ENABLED)
630 const EcPrivateKey *privateKey,
const uint8_t *digest,
size_t digestLen,
640 sce_oem_cmd_t oemCommand;
641 const uint32_t *domainParams;
645 if(privateKey == NULL || digest == NULL || signature == NULL)
649 if(privateKey->
curve == NULL)
653 curve = privateKey->
curve;
656 orderLen = (curve->orderSize + 31) / 32;
659 n = (curve->fieldSize + 7) / 8;
660 n = (
n + 15U) & ~15U;
663 if(
osStrcmp(curve->name,
"secp256k1") == 0)
665 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
666 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
667 domainParams = DomainParam_Koblitz_secp256k1;
671 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
673 curveType = SCE_ECC_CURVE_TYPE_NIST;
674 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
675 domainParams = DomainParam_NIST_P256;
679 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
681 curveType = SCE_ECC_CURVE_TYPE_NIST;
682 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
683 domainParams = DomainParam_NIST_P384;
687 else if(
osStrcmp(curve->name,
"secp521r1") == 0)
689 curveType = SCE_ECC_CURVE_TYPE_NIST;
690 oemCommand = SCE_OEM_CMD_ECC_P521_PRIVATE;
691 domainParams = DomainParam_NIST_P521;
695 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
697 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
698 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
699 domainParams = DomainParam_Brainpool_256r1;
703 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
705 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
706 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
707 domainParams = DomainParam_Brainpool_384r1;
711 else if(
osStrcmp(curve->name,
"brainpoolP512r1") == 0)
713 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
714 oemCommand = SCE_OEM_CMD_ECC_P512R1_PRIVATE;
715 domainParams = DomainParam_Brainpool_512r1;
725 digestLen =
MIN(digestLen, (curve->orderSize + 7) / 8);
732 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
739 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
740 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
743 if(status == FSP_SUCCESS)
746 if(curve->fieldSize == 256)
748 status = HW_SCE_EcdsaSignatureGenerateSub(&curveType, &command,
751 else if(curve->fieldSize == 384)
753 status = HW_SCE_EcdsaP384SignatureGenerateSub(&curveType,
756 else if(curve->fieldSize == 512)
758 status = HW_SCE_EcdsaP512SignatureGenerateSub(ecArgs.
wrappedKey,
761 else if(curve->fieldSize == 521)
763 status = HW_SCE_EcdsaP521SignatureGenerateSub(ecArgs.
wrappedKey,
768 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
773 if(status == FSP_SUCCESS)
776 signature->
curve = curve;
780 (uint8_t *) ecArgs.
signature + offset, orderLen * 4,
788 (uint8_t *) ecArgs.
signature + offset +
n, orderLen * 4,
816 const uint8_t *digest,
size_t digestLen,
const EcdsaSignature *signature)
824 sce_oem_cmd_t oemCommand;
825 const uint32_t *domainParams;
829 if(publicKey == NULL || digest == NULL || signature == NULL)
833 if(publicKey->
curve == NULL)
859 curve = publicKey->
curve;
862 modLen = (curve->fieldSize + 31) / 32;
864 orderLen = (curve->orderSize + 31) / 32;
867 n = (curve->fieldSize + 7) / 8;
868 n = (
n + 15U) & ~15U;
871 if(
osStrcmp(curve->name,
"secp256k1") == 0)
873 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
874 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PUBLIC;
875 domainParams = DomainParam_Koblitz_secp256k1;
878 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
880 curveType = SCE_ECC_CURVE_TYPE_NIST;
881 oemCommand = SCE_OEM_CMD_ECC_P256_PUBLIC;
882 domainParams = DomainParam_NIST_P256;
885 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
887 curveType = SCE_ECC_CURVE_TYPE_NIST;
888 oemCommand = SCE_OEM_CMD_ECC_P384_PUBLIC;
889 domainParams = DomainParam_NIST_P384;
892 else if(
osStrcmp(curve->name,
"secp521r1") == 0)
894 curveType = SCE_ECC_CURVE_TYPE_NIST;
895 oemCommand = SCE_OEM_CMD_ECC_P521_PUBLIC;
896 domainParams = DomainParam_NIST_P521;
899 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
901 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
902 oemCommand = SCE_OEM_CMD_ECC_P256R1_PUBLIC;
903 domainParams = DomainParam_Brainpool_256r1;
906 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
908 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
909 oemCommand = SCE_OEM_CMD_ECC_P384R1_PUBLIC;
910 domainParams = DomainParam_Brainpool_384r1;
913 else if(
osStrcmp(curve->name,
"brainpoolP512r1") == 0)
915 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
916 oemCommand = SCE_OEM_CMD_ECC_P512R1_PUBLIC;
917 domainParams = DomainParam_Brainpool_512r1;
926 digestLen =
MIN(digestLen, (curve->orderSize + 7) / 8);
933 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
950 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
951 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
q, ecArgs.
wrappedKey);
954 if(status == FSP_SUCCESS)
957 if(curve->fieldSize == 256)
959 status = HW_SCE_EcdsaSignatureVerificationSub(&curveType, &command,
962 else if(curve->fieldSize == 384)
964 status = HW_SCE_EcdsaP384SignatureVerificationSub(&curveType,
967 else if(curve->fieldSize == 512)
969 status = HW_SCE_EcdsaP512SignatureVerificationSub(ecArgs.
wrappedKey,
972 else if(curve->fieldSize == 521)
974 status = HW_SCE_EcdsaP521SignatureVerificationSub(ecArgs.
wrappedKey,
979 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;