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)
81 #if (BSP_FEATURE_RSIP_SCE9_SUPPORTED != 0)
83 if(aLen <= 512 && eLen <= 4 && pLen <= 512)
86 sce_oem_cmd_t command;
89 if(aLen <= 128 && pLen <= 128)
91 command = SCE_OEM_CMD_RSA1024_PUBLIC;
94 else if(aLen <= 256 && pLen <= 256)
96 command = SCE_OEM_CMD_RSA2048_PUBLIC;
99 else if(aLen <= 384 && pLen <= 384)
101 command = SCE_OEM_CMD_RSA3072_PUBLIC;
106 command = SCE_OEM_CMD_RSA4096_PUBLIC;
121 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
122 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
125 if(status == FSP_SUCCESS)
130 status = HW_SCE_Rsa1024ModularExponentEncryptSub(rsaArgs.
wrappedKey,
131 rsaArgs.
m, rsaArgs.
c);
135 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
136 rsaArgs.
m, rsaArgs.
c);
140 status = HW_SCE_Rsa3072ModularExponentEncryptSub(rsaArgs.
wrappedKey,
141 rsaArgs.
m, rsaArgs.
c);
145 status = HW_SCE_Rsa4096ModularExponentEncryptSub(rsaArgs.
wrappedKey,
146 rsaArgs.
m, rsaArgs.
c);
150 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
155 if(status == FSP_SUCCESS)
171 if(aLen <= 256 && eLen <= 4 && pLen <= 256)
184 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
185 SCE_OEM_CMD_RSA2048_PUBLIC, NULL, NULL, (uint8_t *) rsaArgs.
key,
189 if(status == FSP_SUCCESS)
192 status = HW_SCE_Rsa2048ModularExponentEncryptSub(rsaArgs.
wrappedKey,
193 rsaArgs.
m, rsaArgs.
c);
197 if(status == FSP_SUCCESS)
247 #if (BSP_FEATURE_RSIP_SCE9_SUPPORTED != 0)
249 if(aLen <= 256 && eLen <= 256 && pLen <= 256)
252 sce_oem_cmd_t command;
255 if(aLen <= 128 && eLen <= 128 && pLen <= 128)
257 command = SCE_OEM_CMD_RSA1024_PRIVATE;
262 command = SCE_OEM_CMD_RSA2048_PRIVATE;
277 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
278 command, NULL, NULL, (uint8_t *) rsaArgs.
key, rsaArgs.
wrappedKey);
281 if(status == FSP_SUCCESS)
286 status = HW_SCE_Rsa1024ModularExponentDecryptSub(rsaArgs.
wrappedKey,
287 rsaArgs.
c, rsaArgs.
m);
291 status = HW_SCE_Rsa2048ModularExponentDecryptSub(rsaArgs.
wrappedKey,
292 rsaArgs.
c, rsaArgs.
m);
296 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
301 if(status == FSP_SUCCESS)
317 if(aLen <= 256 && eLen <= 256 && pLen <= 256)
330 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
331 SCE_OEM_CMD_RSA2048_PRIVATE, NULL, NULL, (uint8_t *) rsaArgs.
key,
335 if(status == FSP_SUCCESS)
338 status = HW_SCE_Rsa2048ModularExponentDecryptSub(rsaArgs.
wrappedKey,
339 rsaArgs.
c, rsaArgs.
m);
343 if(status == FSP_SUCCESS)
369 #if (RSA_SUPPORT == ENABLED)
412 if(nLen <= 256 && dLen <= 256)
417 else if(nLen > 0 && pLen > 0 && qLen > 0 && dpLen > 0 && dqLen > 0 &&
475 else if(nLen > 0 && dLen > 0)
491 #if (EC_SUPPORT == ENABLED)
527 sce_oem_cmd_t oemCommand;
530 if(
osStrcmp(curve->name,
"secp256k1") == 0)
532 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
533 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
537 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
539 curveType = SCE_ECC_CURVE_TYPE_NIST;
540 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
544 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
546 curveType = SCE_ECC_CURVE_TYPE_NIST;
547 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
551 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
553 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
554 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
558 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
560 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
561 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
585 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
586 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
589 if(status == FSP_SUCCESS)
592 if(curve->fieldSize == 256)
594 status = HW_SCE_Ecc256ScalarMultiplicationSub(&curveType,
597 else if(curve->fieldSize == 384)
599 status = HW_SCE_Ecc384ScalarMultiplicationSub(&curveType,
604 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
609 if(status == FSP_SUCCESS)
644 #if (ECDSA_SUPPORT == ENABLED)
658 const EcPrivateKey *privateKey,
const uint8_t *digest,
size_t digestLen,
667 sce_oem_cmd_t oemCommand;
671 if(privateKey == NULL || digest == NULL || signature == NULL)
675 if(privateKey->
curve == NULL)
679 curve = privateKey->
curve;
682 orderLen = (curve->orderSize + 7) / 8;
685 if(
osStrcmp(curve->name,
"secp256k1") == 0)
687 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
688 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PRIVATE;
692 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
694 curveType = SCE_ECC_CURVE_TYPE_NIST;
695 oemCommand = SCE_OEM_CMD_ECC_P256_PRIVATE;
699 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
701 curveType = SCE_ECC_CURVE_TYPE_NIST;
702 oemCommand = SCE_OEM_CMD_ECC_P384_PRIVATE;
706 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
708 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
709 oemCommand = SCE_OEM_CMD_ECC_P256R1_PRIVATE;
713 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
715 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
716 oemCommand = SCE_OEM_CMD_ECC_P384R1_PRIVATE;
726 digestLen =
MIN(digestLen, orderLen);
733 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
740 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
741 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
d, ecArgs.
wrappedKey);
744 if(status == FSP_SUCCESS)
747 if(curve->fieldSize == 256)
749 status = HW_SCE_EcdsaSignatureGenerateSub(&curveType, &command,
752 else if(curve->fieldSize == 384)
754 status = HW_SCE_EcdsaP384SignatureGenerateSub(&curveType,
759 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
764 if(status == FSP_SUCCESS)
767 signature->
curve = curve;
805 const uint8_t *digest,
size_t digestLen,
const EcdsaSignature *signature)
812 sce_oem_cmd_t oemCommand;
816 if(publicKey == NULL || digest == NULL || signature == NULL)
820 if(publicKey->
curve == NULL)
846 curve = publicKey->
curve;
849 orderLen = (curve->orderSize + 7) / 8;
852 if(
osStrcmp(curve->name,
"secp256k1") == 0)
854 curveType = SCE_ECC_CURVE_TYPE_KOBLITZ;
855 oemCommand = SCE_OEM_CMD_ECC_SECP256K1_PUBLIC;
859 else if(
osStrcmp(curve->name,
"secp256r1") == 0)
861 curveType = SCE_ECC_CURVE_TYPE_NIST;
862 oemCommand = SCE_OEM_CMD_ECC_P256_PUBLIC;
866 else if(
osStrcmp(curve->name,
"secp384r1") == 0)
868 curveType = SCE_ECC_CURVE_TYPE_NIST;
869 oemCommand = SCE_OEM_CMD_ECC_P384_PUBLIC;
873 else if(
osStrcmp(curve->name,
"brainpoolP256r1") == 0)
875 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
876 oemCommand = SCE_OEM_CMD_ECC_P256R1_PUBLIC;
880 else if(
osStrcmp(curve->name,
"brainpoolP384r1") == 0)
882 curveType = SCE_ECC_CURVE_TYPE_BRAINPOOL;
883 oemCommand = SCE_OEM_CMD_ECC_P384R1_PUBLIC;
893 digestLen =
MIN(digestLen, orderLen);
900 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
917 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
918 oemCommand, NULL, NULL, (uint8_t *) ecArgs.
q, ecArgs.
wrappedKey);
921 if(status == FSP_SUCCESS)
924 if(curve->fieldSize == 256)
926 status = HW_SCE_EcdsaSignatureVerificationSub(&curveType, &command,
929 else if(curve->fieldSize == 384)
931 status = HW_SCE_EcdsaP384SignatureVerificationSub(&curveType,
936 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;