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;
116 #if (BSP_FEATURE_RSIP_RSIP_E51A_SUPPORTED == 1)
123 status = HW_SCE_Rsa1024ModularExponentEncryptSub(rsaArgs.
key,
124 rsaArgs.
m, rsaArgs.
c);
128 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
key,
129 rsaArgs.
m, rsaArgs.
c);
133 status = HW_SCE_Rsa3072ModularExponentEncryptSub(rsaArgs.
key,
134 rsaArgs.
m, rsaArgs.
c);
138 status = HW_SCE_Rsa4096ModularExponentEncryptSub(rsaArgs.
key,
139 rsaArgs.
m, rsaArgs.
c);
143 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
147 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
148 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
151 if(status == FSP_SUCCESS)
156 status = HW_SCE_Rsa1024ModularExponentEncryptSub(rsaArgs.
wrappedKey,
157 rsaArgs.
m, rsaArgs.
c);
161 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
162 rsaArgs.
m, rsaArgs.
c);
166 status = HW_SCE_Rsa3072ModularExponentEncryptSub(rsaArgs.
wrappedKey,
167 rsaArgs.
m, rsaArgs.
c);
171 status = HW_SCE_Rsa4096ModularExponentEncryptSub(rsaArgs.
wrappedKey,
172 rsaArgs.
m, rsaArgs.
c);
176 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
182 if(status == FSP_SUCCESS)
225 sce_oem_cmd_t command;
235 if((aLen <= 128 && eLen <= 128 && pLen == 128) ||
236 (aLen <= 256 && eLen <= 256 && pLen == 256) ||
237 (aLen <= 384 && eLen <= 384 && pLen == 384) ||
238 (aLen <= 512 && eLen <= 512 && pLen == 512))
243 command = SCE_OEM_CMD_RSA1024_PRIVATE;
247 command = SCE_OEM_CMD_RSA2048_PRIVATE;
251 command = SCE_OEM_CMD_RSA3072_PRIVATE;
255 command = SCE_OEM_CMD_RSA4096_PRIVATE;
274 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
275 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
278 if(status == FSP_SUCCESS)
283 status = HW_SCE_Rsa1024ModularExponentDecryptSub(&keyType,
288 status = HW_SCE_Rsa2048ModularExponentDecryptSub(&keyType,
293 status = HW_SCE_Rsa3072ModularExponentDecryptSub(&keyType,
298 status = HW_SCE_Rsa4096ModularExponentDecryptSub(&keyType,
303 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
308 if(status == FSP_SUCCESS)
333 #if (RSA_SUPPORT == ENABLED)
376 if((nLen == 128 && dLen <= 128) || (nLen == 384 && dLen <= 384))
381 else if(nLen > 0 && pLen > 0 && qLen > 0 && dpLen > 0 && dqLen > 0 &&
439 else if(nLen > 0 && dLen > 0)
455 #if (EC_SUPPORT == ENABLED)
496 sce_oem_cmd_t oemCommand;
497 const uint32_t *domainParams;
500 modLen = (curve->fieldSize + 31) / 32;
502 orderLen = (curve->orderSize + 31) / 32;
505 n = (curve->fieldSize + 7) / 8;
506 n = (
n + 15U) & ~15U;
509 if(
osStrcmp(curve->name,
"secp256k1") == 0)
511 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
512 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
513 domainParams = DomainParam_Koblitz_secp256k1;
517 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
519 curveType = SCE_ECC_CURVE_TYPE_NIST;
520 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
521 domainParams = DomainParam_NIST_P256;
525 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
527 curveType = SCE_ECC_CURVE_TYPE_NIST;
528 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
529 domainParams = DomainParam_NIST_P384;
533 else if(
osStrcmp(curve->name,
"secp521r1") == 0)
535 curveType = SCE_ECC_CURVE_TYPE_NIST;
536 oemCommand = SCE_OEM_CMD_ECC_P521_PRIVATE;
537 domainParams = DomainParam_NIST_P521;
541 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
543 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
544 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
545 domainParams = DomainParam_Brainpool_256r1;
549 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
551 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
552 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
553 domainParams = DomainParam_Brainpool_384r1;
557 else if(
osStrcmp(curve->name,
"brainpoolP512r1") == 0)
559 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
560 oemCommand = SCE_OEM_CMD_ECC_P512R1_PRIVATE;
561 domainParams = DomainParam_Brainpool_512r1;
590 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
591 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
594 if(status == FSP_SUCCESS)
597 if(curve->fieldSize == 256)
599 status = HW_SCE_Ecc256ScalarMultiplicationSub(&curveType, &command,
600 &keyType, ecArgs.
wrappedKey, &dummy, ecArgs.
g, domainParams,
603 else if(curve->fieldSize == 384)
605 status = HW_SCE_Ecc384ScalarMultiplicationSub(&curveType, &keyType,
606 ecArgs.
wrappedKey, &dummy, ecArgs.
g, domainParams, ecArgs.
q);
608 else if(curve->fieldSize == 512)
610 status = HW_SCE_Ecc512ScalarMultiplicationSub(&keyType,
611 ecArgs.
wrappedKey, &dummy, ecArgs.
g, domainParams, ecArgs.
q);
613 else if(curve->fieldSize == 521)
615 status = HW_SCE_Ecc521ScalarMultiplicationSub(&keyType,
616 ecArgs.
wrappedKey, &dummy, ecArgs.
g, domainParams, ecArgs.
q);
620 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
625 if(status == FSP_SUCCESS)
629 (uint8_t *) ecArgs.
q + offset, modLen * 4,
637 (uint8_t *) ecArgs.
q + offset +
n, modLen * 4,
662 #if (ECDSA_SUPPORT == ENABLED)
676 const EcPrivateKey *privateKey,
const uint8_t *digest,
size_t digestLen,
688 sce_oem_cmd_t oemCommand;
689 const uint32_t *domainParams;
693 if(privateKey == NULL || digest == NULL || signature == NULL)
697 if(privateKey->
curve == NULL)
701 curve = privateKey->
curve;
704 orderLen = (curve->orderSize + 31) / 32;
707 n = (curve->fieldSize + 7) / 8;
708 n = (
n + 15U) & ~15U;
711 if(
osStrcmp(curve->name,
"secp256k1") == 0)
713 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
714 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
715 domainParams = DomainParam_Koblitz_secp256k1;
719 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
721 curveType = SCE_ECC_CURVE_TYPE_NIST;
722 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
723 domainParams = DomainParam_NIST_P256;
727 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
729 curveType = SCE_ECC_CURVE_TYPE_NIST;
730 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
731 domainParams = DomainParam_NIST_P384;
735 else if(
osStrcmp(curve->name,
"secp521r1") == 0)
737 curveType = SCE_ECC_CURVE_TYPE_NIST;
738 oemCommand = SCE_OEM_CMD_ECC_P521_PRIVATE;
739 domainParams = DomainParam_NIST_P521;
743 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
745 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
746 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
747 domainParams = DomainParam_Brainpool_256r1;
751 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
753 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
754 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
755 domainParams = DomainParam_Brainpool_384r1;
759 else if(
osStrcmp(curve->name,
"brainpoolP512r1") == 0)
761 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
762 oemCommand = SCE_OEM_CMD_ECC_P512R1_PRIVATE;
763 domainParams = DomainParam_Brainpool_512r1;
778 digestLen =
MIN(digestLen, (curve->orderSize + 7) / 8);
785 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
792 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
793 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
796 if(status == FSP_SUCCESS)
799 if(curve->fieldSize == 256)
801 status = HW_SCE_EcdsaSignatureGenerateSub(&curveType, &command,
805 else if(curve->fieldSize == 384)
807 status = HW_SCE_EcdsaP384SignatureGenerateSub(&curveType, &keyType,
811 else if(curve->fieldSize == 512)
813 status = HW_SCE_EcdsaP512SignatureGenerateSub(&keyType,
817 else if(curve->fieldSize == 521)
819 status = HW_SCE_EcdsaP521SignatureGenerateSub(&keyType,
825 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
830 if(status == FSP_SUCCESS)
833 signature->
curve = curve;
837 (uint8_t *) ecArgs.
signature + offset, orderLen * 4,
845 (uint8_t *) ecArgs.
signature + offset +
n, orderLen * 4,
873 const uint8_t *digest,
size_t digestLen,
const EcdsaSignature *signature)
881 sce_oem_cmd_t oemCommand;
882 const uint32_t *domainParams;
886 if(publicKey == NULL || digest == NULL || signature == NULL)
890 if(publicKey->
curve == NULL)
916 curve = publicKey->
curve;
919 modLen = (curve->fieldSize + 31) / 32;
921 orderLen = (curve->orderSize + 31) / 32;
924 n = (curve->fieldSize + 7) / 8;
925 n = (
n + 15U) & ~15U;
928 if(
osStrcmp(curve->name,
"secp256k1") == 0)
930 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
931 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PUBLIC;
932 domainParams = DomainParam_Koblitz_secp256k1;
935 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
937 curveType = SCE_ECC_CURVE_TYPE_NIST;
938 oemCommand = SCE_OEM_CMD_ECC_P256_PUBLIC;
939 domainParams = DomainParam_NIST_P256;
942 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
944 curveType = SCE_ECC_CURVE_TYPE_NIST;
945 oemCommand = SCE_OEM_CMD_ECC_P384_PUBLIC;
946 domainParams = DomainParam_NIST_P384;
949 else if(
osStrcmp(curve->name,
"secp521r1") == 0)
951 curveType = SCE_ECC_CURVE_TYPE_NIST;
952 oemCommand = SCE_OEM_CMD_ECC_P521_PUBLIC;
953 domainParams = DomainParam_NIST_P521;
956 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
958 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
959 oemCommand = SCE_OEM_CMD_ECC_P256R1_PUBLIC;
960 domainParams = DomainParam_Brainpool_256r1;
963 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
965 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
966 oemCommand = SCE_OEM_CMD_ECC_P384R1_PUBLIC;
967 domainParams = DomainParam_Brainpool_384r1;
970 else if(
osStrcmp(curve->name,
"brainpoolP512r1") == 0)
972 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
973 oemCommand = SCE_OEM_CMD_ECC_P512R1_PUBLIC;
974 domainParams = DomainParam_Brainpool_512r1;
983 digestLen =
MIN(digestLen, (curve->orderSize + 7) / 8);
990 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
1006 #if (BSP_FEATURE_RSIP_RSIP_E51A_SUPPORTED == 1)
1011 if(curve->fieldSize == 256)
1013 status = HW_SCE_EcdsaSignatureVerificationSub(&curveType, &command,
1016 else if(curve->fieldSize == 384)
1018 status = HW_SCE_EcdsaP384SignatureVerificationSub(&curveType, ecArgs.
q,
1021 else if(curve->fieldSize == 512)
1023 status = HW_SCE_EcdsaP512SignatureVerificationSub(ecArgs.
q,
1026 else if(curve->fieldSize == 521)
1028 status = HW_SCE_EcdsaP521SignatureVerificationSub(ecArgs.
q,
1033 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
1037 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
1038 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
q, ecArgs.
wrappedKey);
1041 if(status == FSP_SUCCESS)
1044 if(curve->fieldSize == 256)
1046 status = HW_SCE_EcdsaSignatureVerificationSub(&curveType, &command,
1049 else if(curve->fieldSize == 384)
1051 status = HW_SCE_EcdsaP384SignatureVerificationSub(&curveType,
1054 else if(curve->fieldSize == 512)
1056 status = HW_SCE_EcdsaP512SignatureVerificationSub(ecArgs.
wrappedKey,
1059 else if(curve->fieldSize == 521)
1061 status = HW_SCE_EcdsaP521SignatureVerificationSub(ecArgs.
wrappedKey,
1066 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;