Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "hw_sce_private.h"
36 #include "hw_sce_rsa_private.h"
37 #include "hw_sce_ecc_private.h"
48 #if (S5D9_CRYPTO_PKC_SUPPORT == ENABLED)
54 #if (MPI_SUPPORT == ENABLED)
82 if((aLen <= 128 && eLen <= 4 && pLen <= 128) ||
83 (aLen <= 256 && eLen <= 4 && pLen <= 256))
86 n = (pLen <= 128) ? 128 : 256;
101 status = HW_SCE_RSA_1024PublicEncrypt(0, rsaArgs.
m, rsaArgs.
e,
102 rsaArgs.
n, rsaArgs.
c);
106 status = HW_SCE_RSA_2048PublicEncrypt(0, rsaArgs.
m, rsaArgs.
e,
107 rsaArgs.
n, rsaArgs.
c);
111 status = SSP_ERR_CRYPTO_NOT_IMPLEMENTED;
115 if(status == SSP_SUCCESS)
166 if((aLen <= 128 && eLen <= 128 && pLen <= 128) ||
167 (aLen <= 256 && eLen <= 256 && pLen <= 256))
170 n = (pLen <= 128) ? 128 : 256;
185 status = HW_SCE_RSA_1024PrivateKeyDecrypt(0, rsaArgs.
c, rsaArgs.
d,
186 rsaArgs.
n, rsaArgs.
m);
190 status = HW_SCE_RSA_2048PrivateKeyDecrypt(0, rsaArgs.
c, rsaArgs.
d,
191 rsaArgs.
n, rsaArgs.
m);
195 status = SSP_ERR_CRYPTO_NOT_IMPLEMENTED;
199 if(status == SSP_SUCCESS)
224 #if (RSA_SUPPORT == ENABLED)
244 if(e != 65537 || privateKey == NULL)
254 status = HW_SCE_RSA_1024KeyGenerate(UINT32_MAX, rsaArgs.
d, rsaArgs.
n,
260 status = HW_SCE_RSA_2048KeyGenerate(UINT32_MAX, rsaArgs.
d, rsaArgs.
n,
266 status = SSP_ERR_CRYPTO_NOT_IMPLEMENTED;
270 if(status == SSP_SUCCESS)
284 error =
mpiReadRaw(&privateKey->
d, (uint8_t *) rsaArgs.
d, k);
291 error =
mpiReadRaw(&privateKey->
n, (uint8_t *) rsaArgs.
n, k);
299 (uint8_t *) rsaArgs.
params +
n * 3,
n);
315 (uint8_t *) rsaArgs.
params +
n * 2,
n);
323 (uint8_t *) rsaArgs.
params,
n);
331 (uint8_t *) rsaArgs.
params +
n * 4,
n);
389 if(nLen <= 128 && dLen <= 128)
394 else if(nLen > 0 && pLen > 0 && qLen > 0 && dpLen > 0 && dqLen > 0 &&
452 else if(nLen > 0 && dLen > 0)
468 #if (EC_SUPPORT == ENABLED)
489 modLen = (curve->fieldSize + 7) / 8;
496 else if(modLen <= 28)
500 else if(modLen <= 32)
504 else if(modLen <= 48)
539 status = HW_SCE_ECC_192GenerateKey(ecArgs.
params, ecArgs.
g, ecArgs.
d,
544 status = HW_SCE_ECC_224GenerateKey(ecArgs.
params, ecArgs.
g, ecArgs.
d,
549 status = HW_SCE_ECC_256GenerateKey(ecArgs.
params, ecArgs.
g, ecArgs.
d,
554 status = HW_SCE_ECC_384GenerateKey(ecArgs.
params, ecArgs.
g, ecArgs.
d,
559 status = SSP_ERR_CRYPTO_NOT_IMPLEMENTED;
563 if(status == SSP_SUCCESS)
566 privateKey->
curve = curve;
567 privateKey->
q.
curve = curve;
602 if(publicKey != NULL)
605 *publicKey = privateKey->
q;
649 modLen = (curve->fieldSize + 7) / 8;
656 else if(modLen <= 28)
660 else if(modLen <= 32)
664 else if(modLen <= 48)
703 status = HW_SCE_ECC_192ScalarMultiplication(ecArgs.
params, ecArgs.
d,
708 status = HW_SCE_ECC_224ScalarMultiplication(ecArgs.
params, ecArgs.
d,
713 status = HW_SCE_ECC_256ScalarMultiplication(ecArgs.
params, ecArgs.
d,
718 status = HW_SCE_ECC_384ScalarMultiplication(ecArgs.
params, ecArgs.
d,
723 status = SSP_ERR_CRYPTO_NOT_IMPLEMENTED;
727 if(status == SSP_SUCCESS)
762 #if (ECDSA_SUPPORT == ENABLED)
776 const EcPrivateKey *privateKey,
const uint8_t *digest,
size_t digestLen,
787 if(privateKey == NULL || digest == NULL || signature == NULL)
791 if(privateKey->
curve == NULL)
795 curve = privateKey->
curve;
798 modLen = (curve->fieldSize + 7) / 8;
800 orderLen = (curve->orderSize + 7) / 8;
807 else if(modLen <= 28)
811 else if(modLen <= 32)
815 else if(modLen <= 48)
825 digestLen =
MIN(digestLen, orderLen);
832 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
861 status = HW_SCE_ECC_192GenerateSign(ecArgs.
params, ecArgs.
g, ecArgs.
d,
866 status = HW_SCE_ECC_224GenerateSign(ecArgs.
params, ecArgs.
g, ecArgs.
d,
871 status = HW_SCE_ECC_256GenerateSign(ecArgs.
params, ecArgs.
g, ecArgs.
d,
876 status = HW_SCE_ECC_384GenerateSign(ecArgs.
params, ecArgs.
g, ecArgs.
d,
881 status = SSP_ERR_CRYPTO_NOT_IMPLEMENTED;
885 if(status == SSP_SUCCESS)
888 signature->
curve = curve;
926 const uint8_t *digest,
size_t digestLen,
const EcdsaSignature *signature)
935 if(publicKey == NULL || digest == NULL || signature == NULL)
939 if(publicKey->
curve == NULL)
965 curve = publicKey->
curve;
968 modLen = (curve->fieldSize + 7) / 8;
970 orderLen = (curve->orderSize + 7) / 8;
977 else if(modLen <= 28)
981 else if(modLen <= 32)
985 else if(modLen <= 48)
995 digestLen =
MIN(digestLen, orderLen);
1002 osMemcpy((uint8_t *) ecArgs.
digest +
n - digestLen, digest, digestLen);
1041 status = HW_SCE_ECC_192VerifySign(ecArgs.
params, ecArgs.
g, ecArgs.
q,
1042 ecArgs.
digest, ecArgs.
r, ecArgs.
s);
1046 status = HW_SCE_ECC_224VerifySign(ecArgs.
params, ecArgs.
g, ecArgs.
q,
1047 ecArgs.
digest, ecArgs.
r, ecArgs.
s);
1051 status = HW_SCE_ECC_256VerifySign(ecArgs.
params, ecArgs.
g, ecArgs.
q,
1052 ecArgs.
digest, ecArgs.
r, ecArgs.
s);
1056 status = HW_SCE_ECC_384VerifySign(ecArgs.
params, ecArgs.
g, ecArgs.
q,
1057 ecArgs.
digest, ecArgs.
r, ecArgs.
s);
1061 status = SSP_ERR_CRYPTO_NOT_IMPLEMENTED;
error_t ecScalarImport(uint32_t *r, uint_t n, const uint8_t *input, size_t length, EcScalarFormat format)
Octet string to integer conversion.
error_t ecScalarExport(const uint32_t *a, uint_t n, uint8_t *output, size_t length, EcScalarFormat format)
Integer to octet string conversion.
Arbitrary precision integer.
ECDSA (Elliptic Curve Digital Signature Algorithm)
const EcCurve * curve
Elliptic curve parameters.
const EcCurve * curve
Elliptic curve parameters.
error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (regular calculation)
#define EC_MAX_ORDER_SIZE
error_t rsaGeneratePrivateKey(const PrngAlgo *prngAlgo, void *prngContext, size_t k, uint_t e, RsaPrivateKey *privateKey)
RSA private key generation.
error_t ecdsaGenerateSignature(const PrngAlgo *prngAlgo, void *prngContext, const EcPrivateKey *privateKey, const uint8_t *digest, size_t digestLen, EcdsaSignature *signature)
ECDSA signature generation.
#define mpiWriteRaw(a, data, length)
uint32_t y[EC_MAX_MODULUS_SIZE]
y-coordinate
error_t mpiExpModFast(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (fast calculation)
void mpiInit(Mpi *r)
Initialize a multiple precision integer.
#define mpiReadRaw(r, data, length)
error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p)
Modulo operation.
@ ERROR_INVALID_ELLIPTIC_CURVE
error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision multiplication.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
#define osMemcpy(dest, src, length)
error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision subtraction.
error_t mpiSetValue(Mpi *r, mpi_sword_t a)
Set the value of a multiple precision integer.
error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision addition.
@ ERROR_FAILURE
Generic error code.
void ecScalarSetInt(uint32_t *a, uint32_t b, uint_t n)
Set integer value.
Synergy S5D9 public-key hardware accelerator.
uint32_t r[EC_MAX_ORDER_SIZE]
Integer R.
General definitions for cryptographic algorithms.
RSA public-key cryptography standard.
error_t ecGenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext, const EcCurve *curve, EcPrivateKey *privateKey, EcPublicKey *publicKey)
EC key pair generation.
Mpi dq
Second factor's CRT exponent.
error_t ecMulFast(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (fast calculation)
error_t rsadp(const RsaPrivateKey *key, const Mpi *c, Mpi *m)
RSA decryption primitive.
__weak_func bool_t ecIsPointAffine(const EcCurve *curve, const EcPoint *s)
Check whether the affine point S is on the curve.
uint32_t d[EC_MAX_ORDER_SIZE]
Private key.
int_t ecScalarCompInt(const uint32_t *a, uint32_t b, uint_t n)
Compare integers.
@ EC_SCALAR_FORMAT_BIG_ENDIAN
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
EC point (projective coordinates)
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
uint32_t s[EC_MAX_ORDER_SIZE]
Integer S.
error_t ecdsaVerifySignature(const EcPublicKey *publicKey, const uint8_t *digest, size_t digestLen, const EcdsaSignature *signature)
ECDSA signature verification.
int_t mpiComp(const Mpi *a, const Mpi *b)
Compare two multiple precision integers.
Mpi dp
First factor's CRT exponent.
int_t ecScalarComp(const uint32_t *a, const uint32_t *b, uint_t n)
Compare integers.
int_t mpiCompInt(const Mpi *a, mpi_sword_t b)
Compare a multiple precision integer with an integer.
uint32_t x[EC_MAX_MODULUS_SIZE]
x-coordinate
#define osMemset(p, value, length)
error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular multiplication.
ECC (Elliptic Curve Cryptography)
@ ERROR_INVALID_SIGNATURE
const EcCurve * curve
Elliptic curve parameters.
error_t mpiExpModRegular(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation (regular calculation)
error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
#define EC_MAX_MODULUS_SIZE
Synergy S5D9 hardware cryptographic accelerator (SCE7)
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
void mpiFree(Mpi *r)
Release a multiple precision integer.