32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "stm32h5xx.h"
36 #include "stm32h5xx_hal.h"
48 #if (STM32H5XX_CRYPTO_PKC_SUPPORT == ENABLED)
59 __HAL_RCC_PKA_CLK_ENABLE();
65 while((PKA->CR & PKA_CR_EN) == 0)
71 PKA->CLRFR = PKA_CLRFR_ADDRERRFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_PROCENDFC;
94 destLen = (destLen + 63) / 64;
97 for(i = 0, j = 0; i < srcLen; i++)
102 temp = src[srcLen - i - 1];
105 temp |= src[srcLen - i - 1] << 8;
108 temp |= src[srcLen - i - 1] << 16;
111 temp |= src[srcLen - i - 1] << 24;
112 PKA->RAM[offset + j] = temp;
119 for(; i < (destLen * 8); i++)
127 PKA->RAM[offset + j] = temp;
136 PKA->RAM[offset + j] = 0;
137 PKA->RAM[offset + j + 1] = 0;
160 for(i = 0; i <
n && i < (
length * 2); i++)
162 PKA->RAM[offset + i] =
a->data[i];
166 for(; i < (
length * 2); i++)
168 PKA->RAM[offset + i] = 0;
172 PKA->RAM[offset + i] = 0;
173 PKA->RAM[offset + i + 1] = 0;
206 for(i = 0; i <
length; i++)
208 r->data[i] = PKA->RAM[offset + i];
212 for(; i <
r->size; i++)
226 #if (MPI_SUPPORT == ENABLED) && defined(PKA_CR_MODE_MODULAR_EXP)
262 PKA->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = modLen;
263 PKA->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS + 1] = 0;
266 PKA->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = expLen;
267 PKA->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS + 1] = 0;
275 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
279 temp = PKA->CR & ~PKA_CR_MODE;
283 PKA->CR |= PKA_CR_START;
287 while((PKA->SR & PKA_SR_PROCENDF) == 0)
295 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
312 #if (RSA_SUPPORT == ENABLED) && defined(PKA_CR_MODE_RSA_CRT_EXP)
342 if(nLen <=
PKA_MAX_ROS && pLen <= (nLen / 2) && qLen <= (nLen / 2) &&
343 dpLen <= (nLen / 2) && dqLen <= (nLen / 2) && qinvLen <= (nLen / 2))
349 PKA->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = nLen;
350 PKA->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS + 1] = 0;
361 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
365 temp = PKA->CR & ~PKA_CR_MODE;
369 PKA->CR |= PKA_CR_START;
373 while((PKA->SR & PKA_SR_PROCENDF) == 0)
381 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
437 #if (EC_SUPPORT == ENABLED) && defined(PKA_CR_MODE_ECC_MUL)
464 scalarLen =
MAX(scalarLen, orderLen);
473 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = modLen;
474 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS + 1] = 0;
477 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = scalarLen;
478 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS + 1] = 0;
481 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = 0;
482 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN + 1] = 0;
485 pkaImportMpi(¶ms->
p, modLen, PKA_ECC_SCALAR_MUL_IN_MOD_GF);
486 pkaImportMpi(¶ms->
a, modLen, PKA_ECC_SCALAR_MUL_IN_A_COEFF);
487 pkaImportMpi(¶ms->
b, modLen, PKA_ECC_SCALAR_MUL_IN_B_COEFF);
488 pkaImportMpi(¶ms->
q, scalarLen, PKA_ECC_SCALAR_MUL_IN_N_PRIME_ORDER);
490 pkaImportMpi(&
s->x, modLen, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X);
491 pkaImportMpi(&
s->y, modLen, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y);
497 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
501 temp = PKA->CR & ~PKA_CR_MODE;
505 PKA->CR |= PKA_CR_START;
509 while((PKA->SR & PKA_SR_PROCENDF) == 0)
527 error =
pkaExportMpi(&
r->x, modLen, PKA_ECC_SCALAR_MUL_OUT_RESULT_X);
534 error =
pkaExportMpi(&
r->y, modLen, PKA_ECC_SCALAR_MUL_OUT_RESULT_Y);
545 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
561 #if (ECDSA_SUPPORT == ENABLED) && defined(PKA_CR_MODE_ECDSA_SIGN)
577 const uint8_t *digest,
size_t digestLen,
EcdsaSignature *signature)
586 if(params == NULL || privateKey == NULL || digest == NULL || signature == NULL)
611 PKA->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = modLen;
612 PKA->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS + 1] = 0;
615 PKA->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = orderLen;
616 PKA->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS + 1] = 0;
619 PKA->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = 0;
620 PKA->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN + 1] = 0;
626 pkaImportMpi(¶ms->
g.
x, modLen, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X);
627 pkaImportMpi(¶ms->
g.
y, modLen, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y);
628 pkaImportMpi(¶ms->
q, orderLen, PKA_ECDSA_SIGN_IN_ORDER_N);
629 pkaImportMpi(&privateKey->
d, orderLen, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D);
633 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
635 pkaImportArray(digest, digestLen, orderLen, PKA_ECDSA_SIGN_IN_HASH_E);
641 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
645 temp = PKA->CR & ~PKA_CR_MODE;
649 PKA->CR |= PKA_CR_START;
653 while((PKA->SR & PKA_SR_PROCENDF) == 0)
671 error =
pkaExportMpi(&signature->
r, orderLen, PKA_ECDSA_SIGN_OUT_SIGNATURE_R);
678 error =
pkaExportMpi(&signature->
s, orderLen, PKA_ECDSA_SIGN_OUT_SIGNATURE_S);
682 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
696 #if (ECDSA_SUPPORT == ENABLED) && defined(PKA_CR_MODE_ECDSA_VERIFY)
709 const EcPublicKey *publicKey,
const uint8_t *digest,
size_t digestLen,
718 if(params == NULL || publicKey == NULL || digest == NULL || signature == NULL)
750 PKA->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = modLen;
751 PKA->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS + 1] = 0;
754 PKA->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = orderLen;
755 PKA->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS + 1] = 0;
758 PKA->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = 0;
759 PKA->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN + 1] = 0;
763 pkaImportMpi(¶ms->
a, modLen, PKA_ECDSA_VERIF_IN_A_COEFF);
764 pkaImportMpi(¶ms->
g.
x, modLen, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X);
765 pkaImportMpi(¶ms->
g.
y, modLen, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y);
766 pkaImportMpi(¶ms->
q, orderLen, PKA_ECDSA_VERIF_IN_ORDER_N);
767 pkaImportMpi(&publicKey->
q.
x, modLen, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X);
768 pkaImportMpi(&publicKey->
q.
y, modLen, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y);
769 pkaImportMpi(&signature->
r, orderLen, PKA_ECDSA_VERIF_IN_SIGNATURE_R);
770 pkaImportMpi(&signature->
s, orderLen, PKA_ECDSA_VERIF_IN_SIGNATURE_S);
773 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
775 pkaImportArray(digest, digestLen, orderLen, PKA_ECDSA_VERIF_IN_HASH_E);
781 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
785 temp = PKA->CR & ~PKA_CR_MODE;
789 PKA->CR |= PKA_CR_START;
793 while((PKA->SR & PKA_SR_PROCENDF) == 0)
808 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
818 #if (X25519_SUPPORT == ENABLED || ED25519_SUPPORT == ENABLED) && \
819 defined(PKA_CR_MODE_ARITHMETIC_MUL)
838 PKA->RAM[PKA_ARITHMETIC_MUL_NB_BITS] = 255;
841 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1] =
a[0];
842 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 1] =
a[1];
843 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 2] =
a[2];
844 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 3] =
a[3];
845 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 4] =
a[4];
846 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 5] =
a[5];
847 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 6] =
a[6];
848 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 7] =
a[7];
851 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 8] = 0;
852 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 9] = 0;
855 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2] =
b[0];
856 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 1] =
b[1];
857 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 2] =
b[2];
858 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 3] =
b[3];
859 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 4] =
b[4];
860 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 5] =
b[5];
861 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 6] =
b[6];
862 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 7] =
b[7];
865 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 8] = 0;
866 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 9] = 0;
869 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
873 temp = PKA->CR & ~PKA_CR_MODE;
877 PKA->CR |= PKA_CR_START;
881 while((PKA->SR & PKA_SR_PROCENDF) == 0)
886 u[0] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT];
887 u[1] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 1];
888 u[2] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 2];
889 u[3] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 3];
890 u[4] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 4];
891 u[5] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 5];
892 u[6] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 6];
893 u[7] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 7];
894 u[8] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 8];
895 u[9] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 9];
896 u[10] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 10];
897 u[11] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 11];
898 u[12] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 12];
899 u[13] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 13];
900 u[14] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 14];
901 u[15] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 15];
904 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
910 temp = (
u[7] >> 31) * 19;
915 for(i = 0; i < 8; i++)
918 temp += (uint64_t)
u[i + 8] * 38;
919 u[i] = temp & 0xFFFFFFFF;
926 temp += (
u[7] >> 31) * 19;
931 for(i = 0; i < 8; i++)
934 u[i] = temp & 0xFFFFFFFF;
943 #if (X448_SUPPORT == ENABLED || ED448_SUPPORT == ENABLED) && \
944 defined(PKA_CR_MODE_ARITHMETIC_MUL)
953 void curve448Mul(uint32_t *
r,
const uint32_t *
a,
const uint32_t *
b)
964 PKA->RAM[PKA_ARITHMETIC_MUL_NB_BITS] = 448;
967 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1] =
a[0];
968 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 1] =
a[1];
969 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 2] =
a[2];
970 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 3] =
a[3];
971 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 4] =
a[4];
972 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 5] =
a[5];
973 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 6] =
a[6];
974 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 7] =
a[7];
975 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 8] =
a[8];
976 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 9] =
a[9];
977 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 10] =
a[10];
978 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 11] =
a[11];
979 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 12] =
a[12];
980 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 13] =
a[13];
983 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 14] = 0;
984 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 15] = 0;
987 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2] =
b[0];
988 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 1] =
b[1];
989 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 2] =
b[2];
990 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 3] =
b[3];
991 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 4] =
b[4];
992 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 5] =
b[5];
993 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 6] =
b[6];
994 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 7] =
b[7];
995 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 8] =
b[8];
996 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 9] =
b[9];
997 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 10] =
b[10];
998 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 11] =
b[11];
999 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 12] =
b[12];
1000 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 13] =
b[13];
1003 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 14] = 0;
1004 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 15] = 0;
1007 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
1011 temp = PKA->CR & ~PKA_CR_MODE;
1015 PKA->CR |= PKA_CR_START;
1019 while((PKA->SR & PKA_SR_PROCENDF) == 0)
1024 u[0] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT];
1025 u[1] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 1];
1026 u[2] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 2];
1027 u[3] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 3];
1028 u[4] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 4];
1029 u[5] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 5];
1030 u[6] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 6];
1031 u[7] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 7];
1032 u[8] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 8];
1033 u[9] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 9];
1034 u[10] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 10];
1035 u[11] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 11];
1036 u[12] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 12];
1037 u[13] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 13];
1038 u[14] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 14];
1039 u[15] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 15];
1040 u[16] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 16];
1041 u[17] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 17];
1042 u[18] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 18];
1043 u[19] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 19];
1044 u[20] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 20];
1045 u[21] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 21];
1046 u[22] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 22];
1047 u[23] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 23];
1048 u[24] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 24];
1049 u[25] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 25];
1050 u[26] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 26];
1051 u[27] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 27];
1054 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
1060 for(temp = 0, i = 0; i < 7; i++)
1065 u[i] = temp & 0xFFFFFFFF;
1069 for(i = 7; i < 14; i++)
1073 temp += (uint64_t)
u[i + 14] << 1;
1074 u[i] = temp & 0xFFFFFFFF;
1079 for(
c = temp, i = 0; i < 7; i++)
1082 u[i] = temp & 0xFFFFFFFF;
1086 for(temp +=
c, i = 7; i < 14; i++)
1089 u[i] = temp & 0xFFFFFFFF;