Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
47 #if (GD32W5XX_CRYPTO_PKC_SUPPORT == ENABLED)
58 rcu_periph_clock_enable(RCU_PKCAU);
64 while((PKCAU_CTL & PKCAU_CTL_PKCAUEN) == 0)
66 PKCAU_CTL = PKCAU_CTL_PKCAUEN;
70 PKCAU_STATC = PKCAU_STATC_ADDRERRC | PKCAU_STATC_RAMERRC | PKCAU_STATC_ENDC;
93 destLen = (destLen + 31) / 32;
96 for(i = 0, j = 0; i < srcLen; i++)
101 temp = src[srcLen - i - 1];
104 temp |= src[srcLen - i - 1] << 8;
107 temp |= src[srcLen - i - 1] << 16;
110 temp |= src[srcLen - i - 1] << 24;
118 for(; i < (destLen * 4); i++)
158 for(i = 0; i <
n && i <
length; i++)
203 for(i = 0; i <
length; i++)
209 for(; i <
r->size; i++)
267 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
271 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
272 PKCAU_CTL = temp | PKCAU_MODE_MOD_EXP;
275 PKCAU_CTL |= PKCAU_CTL_START;
279 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
287 PKCAU_STATC = PKCAU_STATC_ENDC;
332 if(nLen <=
PKCAU_MAX_ROS && pLen <= (nLen / 2) && qLen <= (nLen / 2) &&
333 dpLen <= (nLen / 2) && dqLen <= (nLen / 2) && qinvLen <= (nLen / 2))
350 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
354 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
355 PKCAU_CTL = temp | PKCAU_MODE_CRT_EXP;
358 PKCAU_CTL |= PKCAU_CTL_START;
362 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
370 PKCAU_STATC = PKCAU_STATC_ENDC;
451 if(nLen == 0 || eLen == 0)
546 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
550 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
551 PKCAU_CTL = temp | PKCAU_MODE_ECC_MUL;
554 PKCAU_CTL |= PKCAU_CTL_START;
558 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
580 PKCAU_STATC = PKCAU_STATC_ENDC;
610 const uint8_t *digest,
size_t digestLen,
EcdsaSignature *signature)
619 if(params == NULL || privateKey == NULL || digest == NULL || signature == NULL)
660 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
668 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
672 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
673 PKCAU_CTL = temp | PKCAU_MODE_ECDSA_SIGN;
676 PKCAU_CTL |= PKCAU_CTL_START;
680 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
709 PKCAU_STATC = PKCAU_STATC_ENDC;
734 const EcPublicKey *publicKey,
const uint8_t *digest,
size_t digestLen,
743 if(params == NULL || publicKey == NULL || digest == NULL || signature == NULL)
793 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
801 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
805 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
806 PKCAU_CTL = temp | PKCAU_MODE_ECDSA_VERIFICATION;
809 PKCAU_CTL |= PKCAU_CTL_START;
813 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
828 PKCAU_STATC = PKCAU_STATC_ENDC;
838 #if (X25519_SUPPORT == ENABLED || ED25519_SUPPORT == ENABLED)
886 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
890 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
891 PKCAU_CTL = temp | PKCAU_MODE_ARITHMETIC_MUL;
894 PKCAU_CTL |= PKCAU_CTL_START;
898 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
921 PKCAU_STATC = PKCAU_STATC_ENDC;
927 temp = (
u[7] >> 31) * 19;
932 for(i = 0; i < 8; i++)
935 temp += (uint64_t)
u[i + 8] * 38;
936 u[i] = temp & 0xFFFFFFFF;
943 temp += (
u[7] >> 31) * 19;
948 for(i = 0; i < 8; i++)
951 u[i] = temp & 0xFFFFFFFF;
960 #if (X448_SUPPORT == ENABLED || ED448_SUPPORT == ENABLED)
1021 PKCAU_CTL &= ~(PKCAU_CTL_ADDRERRIE | PKCAU_CTL_RAMERRIE | PKCAU_CTL_ENDIE);
1025 temp = PKCAU_CTL & ~PKCAU_CTL_MODESEL;
1026 PKCAU_CTL = temp | PKCAU_MODE_ARITHMETIC_MUL;
1029 PKCAU_CTL |= PKCAU_CTL_START;
1033 while((PKCAU_STAT & PKCAU_STAT_ENDF) == 0)
1068 PKCAU_STATC = PKCAU_STATC_ENDC;
1074 for(temp = 0, i = 0; i < 7; i++)
1079 u[i] = temp & 0xFFFFFFFF;
1083 for(i = 7; i < 14; i++)
1087 temp += (uint64_t)
u[i + 14] << 1;
1088 u[i] = temp & 0xFFFFFFFF;
1093 for(
c = temp, i = 0; i < 7; i++)
1096 u[i] = temp & 0xFFFFFFFF;
1100 for(temp +=
c, i = 7; i < 14; i++)
1103 u[i] = temp & 0xFFFFFFFF;
#define PKCAU_RSA_CRT_EXP_IN_MOD_LEN
#define PKCAU_ECDSA_VERIF_IN_P
#define PKCAU_ECDSA_SIGN_IN_Z
#define PKCAU_ECDSA_VERIF_IN_QY
#define PKCAU_RSA_CRT_EXP_OUT_R
#define PKCAU_ECDSA_SIGN_IN_K
Curve448 elliptic curve (constant-time implementation)
#define PKCAU_ECC_MUL_IN_K
#define PKCAU_ECDSA_VERIF_IN_GY
#define PKCAU_ECDSA_VERIF_IN_MOD_LEN
#define PKCAU_ECC_MUL_IN_A_SIGN
Arbitrary precision integer.
#define PKCAU_RSA_CRT_EXP_IN_QINV
#define PKCAU_ECC_MUL_IN_Y
#define PKCAU_RSA_CRT_EXP_IN_DQ
ECDSA (Elliptic Curve Digital Signature Algorithm)
void curve25519Mul(uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular multiplication.
#define PKCAU_ECC_MUL_IN_A
error_t mpiSetValue(Mpi *r, int_t a)
Set the value of a multiple precision integer.
error_t pkcauRsaCrtExp(const RsaPrivateKey *key, const Mpi *c, Mpi *m)
Modular exponentiation with CRT.
#define PKCAU_MOD_EXP_IN_N
error_t mpiRandRange(Mpi *r, const Mpi *p, const PrngAlgo *prngAlgo, void *prngContext)
Generate a random value in the range 1 to p-1.
#define PKCAU_ECDSA_VERIF_IN_GX
#define PKCAU_MOD_EXP_IN_A
#define PKCAU_ECDSA_VERIF_IN_A
error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d, const EcPoint *s)
Scalar multiplication.
#define PKCAU_ECDSA_VERIF_IN_R
#define PKCAU_ECC_MUL_IN_X
void mpiInit(Mpi *r)
Initialize a multiple precision integer.
#define PKCAU_ECDSA_SIGN_IN_A_SIGN
GD32W5 public-key hardware accelerator (PKCAU)
#define PKCAU_MOD_EXP_IN_EXP_LEN
error_t rsaep(const RsaPublicKey *key, const Mpi *m, Mpi *c)
RSA encryption primitive.
#define PKCAU_ECDSA_SIGN_IN_N
error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p)
Modulo operation.
#define PKCAU_ECDSA_VERIF_IN_Z
error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision multiplication.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
#define PKCAU_MOD_EXP_OUT_R
OsMutex gd32w5xxCryptoMutex
error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision subtraction.
#define PKCAU_STATUS_SUCCESS
void curve25519Red(uint32_t *r, const uint32_t *a)
Modular reduction.
void pkcauImportMpi(const Mpi *a, uint_t length, uint_t offset)
Import multiple-precision integer.
#define PKCAU_ECDSA_SIGN_IN_GY
error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b)
Multiple precision addition.
#define PKCAU_ECDSA_VERIF_IN_ORDER_LEN
#define PKCAU_ECDSA_SIGN_OUT_R
#define PKCAU_RSA_CRT_EXP_IN_A
@ ERROR_FAILURE
Generic error code.
#define PKCAU_ECC_MUL_OUT_Y
#define PKCAU_ECDSA_SIGN_IN_GX
#define PKCAU_ARITH_MUL_IN_A
#define PKCAU_ECDSA_SIGN_OUT_S
#define PKCAU_RSA_CRT_EXP_IN_Q
#define PKCAU_ECDSA_VERIF_IN_N
#define PKCAU_ECC_MUL_IN_MOD_LEN
General definitions for cryptographic algorithms.
RSA public-key cryptography standard.
#define PKCAU_ECDSA_SIGN_OUT_ERROR
#define PKCAU_ARITH_MUL_IN_OP_LEN
error_t ecdsaVerifySignature(const EcDomainParameters *params, const EcPublicKey *publicKey, const uint8_t *digest, size_t digestLen, const EcdsaSignature *signature)
ECDSA signature verification.
#define PKCAU_ECDSA_SIGN_IN_A
#define PKCAU_STATUS_INVALID
#define PKCAU_ECDSA_VERIF_OUT_RES
void curve448Mul(uint32_t *r, const uint32_t *a, const uint32_t *b)
Modular multiplication.
uint_t mpiGetBitLength(const Mpi *a)
Get the actual length in bits.
Mpi dq
Second factor's CRT exponent.
#define PKCAU_MOD_EXP_IN_OP_LEN
uint_t mpiGetLength(const Mpi *a)
Get the actual length in words.
#define PKCAU_ARITH_MUL_OUT_R
#define PKCAU_MOD_EXP_IN_E
#define PKCAU_ECDSA_SIGN_IN_ORDER_LEN
error_t pkcauModExp(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
error_t pkcauExportMpi(Mpi *r, uint_t length, uint_t offset)
Export multiple-precision integer.
#define PKCAU_ECDSA_VERIF_IN_QX
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
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_DP
#define PKCAU_ECDSA_VERIF_IN_A_SIGN
Curve25519 elliptic curve (constant-time implementation)
GD32W5 hardware cryptographic accelerator.
void pkcauImportArray(const uint8_t *src, size_t srcLen, uint_t destLen, uint_t offset)
Import byte array.
int_t mpiComp(const Mpi *a, const Mpi *b)
Compare two multiple precision integers.
Mpi dp
First factor's CRT exponent.
void curve448Red(uint32_t *r, const uint32_t *a, uint32_t h)
Modular reduction.
error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p)
Modular multiplication.
#define PKCAU_ECDSA_SIGN_IN_D
ECC (Elliptic Curve Cryptography)
@ ERROR_INVALID_SIGNATURE
int_t mpiCompInt(const Mpi *a, int_t b)
Compare a multiple precision integer with an integer.
#define PKCAU_ECDSA_SIGN_IN_P
#define PKCAU_RSA_CRT_EXP_IN_P
error_t mpiGrow(Mpi *r, uint_t size)
Adjust the size of multiple precision integer.
#define PKCAU_ECDSA_SIGN_IN_MOD_LEN
#define PKCAU_ARITH_MUL_IN_B
Mpi q
Order of the point G.
error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p)
Modular exponentiation.
#define PKCAU_ECC_MUL_IN_SCALAR_LEN
#define PKCAU_ECC_MUL_OUT_X
#define PKCAU_ECDSA_VERIF_IN_S
#define PKCAU_ECC_MUL_IN_P
void mpiFree(Mpi *r)
Release a multiple precision integer.
error_t ecdsaGenerateSignature(const PrngAlgo *prngAlgo, void *prngContext, const EcDomainParameters *params, const EcPrivateKey *privateKey, const uint8_t *digest, size_t digestLen, EcdsaSignature *signature)
ECDSA signature generation.