Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
46 #if (GD32W5XX_CRYPTO_PKC_SUPPORT == ENABLED)
57 rcu_periph_clock_enable(RCU_PKCAU);
63 while((PKCAU_CTL & PKCAU_CTL_PKCAUEN) == 0)
65 PKCAU_CTL = PKCAU_CTL_PKCAUEN;
69 PKCAU_STATC = PKCAU_STATC_ADDRERRC | PKCAU_STATC_RAMERRC | PKCAU_STATC_ENDC;
95 destLen = (destLen + 31) / 32;
98 for(i = 0, j = 0; i < srcLen; i++)
103 temp = src[srcLen - i - 1];
106 temp |= src[srcLen - i - 1] << 8;
109 temp |= src[srcLen - i - 1] << 16;
112 temp |= src[srcLen - i - 1] << 24;
120 for(; i < (destLen * 4); i++)
156 for(i = 0; i <
length; i++)
185 for(i = 0; i <
n && i <
length; i++)
216 for(i = 0; i <
length; i++)
252 for(i = 0; i <
length; i++)
258 for(; i < dest->
size; i++)
272 #if (MPI_SUPPORT == ENABLED)
318 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
322 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
323 PKCAU_CTL = temp | PKCAU_MODE_MOD_EXP;
326 PKCAU_CTL |= PKCAU_CTL_START;
333 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
341 PKCAU_STATC = PKCAU_STATC_ENDC;
358 #if (RSA_SUPPORT == ENABLED)
388 if(nLen <=
PKCAU_MAX_ROS && pLen <= (nLen / 2) && qLen <= (nLen / 2) &&
389 dpLen <= (nLen / 2) && dqLen <= (nLen / 2) && qinvLen <= (nLen / 2))
406 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
410 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
411 PKCAU_CTL = temp | PKCAU_MODE_CRT_EXP;
414 PKCAU_CTL |= PKCAU_CTL_START;
421 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
429 PKCAU_STATC = PKCAU_STATC_ENDC;
510 if(nLen == 0 || eLen == 0)
562 #if (EC_SUPPORT == ENABLED)
599 modLen = curve->fieldSize;
601 orderLen = curve->orderSize;
624 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
628 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
629 PKCAU_CTL = temp | PKCAU_MODE_ECC_MUL;
632 PKCAU_CTL |= PKCAU_CTL_START;
639 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
655 PKCAU_STATC = PKCAU_STATC_ENDC;
690 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
696 #if (CRYPTO_STATIC_MEM_SUPPORT == DISABLED)
707 state->
curve = curve;
731 #if (ECDSA_SUPPORT == ENABLED)
745 const EcPrivateKey *privateKey,
const uint8_t *digest,
size_t digestLen,
756 if(privateKey == NULL || digest == NULL || signature == NULL)
760 if(privateKey->
curve == NULL)
764 curve = privateKey->
curve;
767 modLen = curve->fieldSize;
769 orderLen = curve->orderSize;
801 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
809 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
813 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
814 PKCAU_CTL = temp | PKCAU_MODE_ECDSA_SIGN;
817 PKCAU_CTL |= PKCAU_CTL_START;
824 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
842 signature->
curve = curve;
854 PKCAU_STATC = PKCAU_STATC_ENDC;
875 const uint8_t *digest,
size_t digestLen,
const EcdsaSignature *signature)
884 if(publicKey == NULL || digest == NULL || signature == NULL)
888 if(publicKey->
curve == NULL)
914 curve = publicKey->
curve;
917 modLen = curve->fieldSize;
919 orderLen = curve->orderSize;
947 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
955 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
959 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
960 PKCAU_CTL = temp | PKCAU_MODE_ECDSA_VERIFICATION;
963 PKCAU_CTL |= PKCAU_CTL_START;
970 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
985 PKCAU_STATC = PKCAU_STATC_ENDC;
#define PKCAU_ECC_MUL_IN_A_SIGN
Arbitrary precision integer.
#define PKCAU_STATUS_INVALID
ECDSA (Elliptic Curve Digital Signature Algorithm)
const EcCurve * curve
Elliptic curve parameters.
#define PKCAU_ECC_MUL_IN_A
const EcCurve * curve
Elliptic curve parameters.
void ecFullAdd(EcState *state, EcPoint3 *r, const EcPoint3 *s, const EcPoint3 *t)
Point addition.
#define EC_MAX_ORDER_SIZE
#define PKCAU_ECDSA_VERIF_IN_R
#define PKCAU_ECC_MUL_OUT_Y
error_t pkcauRsaCrtExp(const RsaPrivateKey *key, const Mpi *c, Mpi *m)
Modular exponentiation with CRT.
error_t ecTwinMul(const EcCurve *curve, EcPoint3 *r, const uint32_t *d0, const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t)
Twin multiplication.
error_t ecdsaVerifySignature(const EcPublicKey *publicKey, const uint8_t *digest, size_t digestLen, const EcdsaSignature *signature)
ECDSA signature verification.
#define PKCAU_ECDSA_VERIF_IN_GX
error_t ecMulFast(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (fast calculation)
uint32_t y[EC_MAX_MODULUS_SIZE]
y-coordinate
#define PKCAU_ECDSA_VERIF_IN_MOD_LEN
#define PKCAU_MOD_EXP_IN_OP_LEN
void mpiInit(Mpi *r)
Initialize a multiple precision integer.
GD32W5 public-key hardware accelerator (PKCAU)
#define PKCAU_ECDSA_VERIF_IN_N
error_t rsaep(const RsaPublicKey *key, const Mpi *m, Mpi *c)
RSA encryption primitive.
#define PKCAU_STATUS_SUCCESS
#define PKCAU_ECC_MUL_IN_MOD_LEN
#define PKCAU_ECDSA_VERIF_IN_P
#define PKCAU_RSA_CRT_EXP_OUT_R
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 PKCAU_RSA_CRT_EXP_IN_P
OsMutex gd32w5xxCryptoMutex
error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision subtraction.
#define PKCAU_ECDSA_VERIF_IN_A_SIGN
#define PKCAU_ECDSA_VERIF_OUT_RES
void pkcauImportMpi(const Mpi *src, uint_t length, uint_t offset)
Import multiple-precision integer.
#define PKCAU_ECDSA_SIGN_IN_MOD_LEN
#define PKCAU_ECDSA_SIGN_IN_K
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.
#define PKCAU_MOD_EXP_OUT_R
error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (regular calculation)
uint32_t r[EC_MAX_ORDER_SIZE]
Integer R.
General definitions for cryptographic algorithms.
#define PKCAU_MOD_EXP_IN_N
RSA public-key cryptography standard.
void pkcauExportScalar(uint32_t *dest, uint_t length, uint_t offset)
Export scalar.
#define PKCAU_ECDSA_SIGN_IN_D
#define PKCAU_RSA_CRT_EXP_IN_A
#define PKCAU_ECDSA_SIGN_OUT_ERROR
#define PKCAU_ECDSA_SIGN_OUT_S
#define PKCAU_ECC_MUL_IN_Y
#define PKCAU_MOD_EXP_IN_E
#define PKCAU_ECDSA_SIGN_IN_N
#define PKCAU_ECDSA_SIGN_IN_Z
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Mpi dq
Second factor's CRT exponent.
__weak_func bool_t ecIsPointAffine(const EcCurve *curve, const EcPoint *s)
Check whether the affine point S is on the curve.
uint_t mpiGetLength(const Mpi *a)
Get the actual length in words.
#define PKCAU_ECDSA_SIGN_IN_P
#define PKCAU_ECDSA_VERIF_IN_QY
#define PKCAU_MOD_EXP_IN_A
#define PKCAU_ECC_MUL_OUT_X
#define PKCAU_ECC_MUL_IN_K
#define PKCAU_ECC_MUL_IN_SCALAR_LEN
void pkcauImportScalar(const uint32_t *src, uint_t length, uint_t offset)
Import scalar.
error_t pkcauModExp(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
uint32_t d[EC_MAX_ORDER_SIZE]
Private key.
int_t ecScalarCompInt(const uint32_t *a, uint32_t b, uint_t n)
Compare integers.
Working state (point addition/subtraction/doubling)
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
error_t pkcauExportMpi(Mpi *dest, uint_t length, uint_t offset)
Export multiple-precision integer.
EC point (projective coordinates)
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
error_t pkcauInit(void)
PKCAU module initialization.
error_t rsadp(const RsaPrivateKey *key, const Mpi *c, Mpi *m)
RSA decryption primitive.
#define PKCAU_RSA_CRT_EXP_IN_Q
#define PKCAU_RSA_CRT_EXP_IN_DP
#define PKCAU_ECDSA_SIGN_IN_GX
GD32W5 hardware cryptographic accelerator.
uint32_t s[EC_MAX_ORDER_SIZE]
Integer S.
#define PKCAU_RSA_CRT_EXP_IN_QINV
error_t ecdsaGenerateSignature(const PrngAlgo *prngAlgo, void *prngContext, const EcPrivateKey *privateKey, const uint8_t *digest, size_t digestLen, EcdsaSignature *signature)
ECDSA signature generation.
#define PKCAU_ECDSA_VERIF_IN_S
error_t ecScalarRand(const EcCurve *curve, uint32_t *r, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value.
#define cryptoAllocMem(size)
#define PKCAU_ECDSA_SIGN_IN_A_SIGN
#define PKCAU_ECDSA_SIGN_OUT_R
#define PKCAU_RSA_CRT_EXP_IN_MOD_LEN
#define PKCAU_ECDSA_VERIF_IN_QX
#define PKCAU_ECC_MUL_IN_X
#define PKCAU_ECDSA_SIGN_IN_A
#define PKCAU_MOD_EXP_IN_EXP_LEN
void pkcauImportArray(const uint8_t *src, size_t srcLen, uint_t destLen, uint_t offset)
Import byte array.
#define PKCAU_ECDSA_VERIF_IN_GY
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.
#define PKCAU_RSA_CRT_EXP_IN_DQ
#define PKCAU_ECDSA_VERIF_IN_Z
ECC (Elliptic Curve Cryptography)
@ ERROR_INVALID_SIGNATURE
error_t mpiGrow(Mpi *r, uint_t size)
Adjust the size of multiple precision integer.
const EcCurve * curve
Elliptic curve parameters.
#define PKCAU_ECDSA_SIGN_IN_GY
#define PKCAU_ECDSA_VERIF_IN_A
error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
#define EC_MAX_MODULUS_SIZE
#define PKCAU_ECDSA_SIGN_IN_ORDER_LEN
#define PKCAU_ECDSA_VERIF_IN_ORDER_LEN
#define PKCAU_ECC_MUL_IN_P
void mpiFree(Mpi *r)
Release a multiple precision integer.