32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "stm32h7rsxx.h"
36 #include "stm32h7rsxx_hal.h"
48 #if (STM32H7RSXX_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++)
260 PKA->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = modLen;
261 PKA->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS + 1] = 0;
264 PKA->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = expLen;
265 PKA->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS + 1] = 0;
273 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
277 temp = PKA->CR & ~PKA_CR_MODE;
281 PKA->CR |= PKA_CR_START;
285 while((PKA->SR & PKA_SR_PROCENDF) == 0)
293 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
338 if(nLen <=
PKA_MAX_ROS && pLen <= (nLen / 2) && qLen <= (nLen / 2) &&
339 dpLen <= (nLen / 2) && dqLen <= (nLen / 2) && qinvLen <= (nLen / 2))
345 PKA->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = nLen;
346 PKA->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS + 1] = 0;
357 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
361 temp = PKA->CR & ~PKA_CR_MODE;
365 PKA->CR |= PKA_CR_START;
369 while((PKA->SR & PKA_SR_PROCENDF) == 0)
377 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
458 scalarLen =
MAX(scalarLen, orderLen);
467 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = modLen;
468 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS + 1] = 0;
471 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = scalarLen;
472 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS + 1] = 0;
475 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = 0;
476 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN + 1] = 0;
479 pkaImportMpi(¶ms->
p, modLen, PKA_ECC_SCALAR_MUL_IN_MOD_GF);
480 pkaImportMpi(¶ms->
a, modLen, PKA_ECC_SCALAR_MUL_IN_A_COEFF);
481 pkaImportMpi(¶ms->
b, modLen, PKA_ECC_SCALAR_MUL_IN_B_COEFF);
482 pkaImportMpi(¶ms->
q, scalarLen, PKA_ECC_SCALAR_MUL_IN_N_PRIME_ORDER);
484 pkaImportMpi(&
s->x, modLen, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X);
485 pkaImportMpi(&
s->y, modLen, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y);
491 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
495 temp = PKA->CR & ~PKA_CR_MODE;
499 PKA->CR |= PKA_CR_START;
503 while((PKA->SR & PKA_SR_PROCENDF) == 0)
521 error =
pkaExportMpi(&
r->x, modLen, PKA_ECC_SCALAR_MUL_OUT_RESULT_X);
528 error =
pkaExportMpi(&
r->y, modLen, PKA_ECC_SCALAR_MUL_OUT_RESULT_Y);
539 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
569 const uint8_t *digest,
size_t digestLen,
EcdsaSignature *signature)
578 if(params == NULL || privateKey == NULL || digest == NULL || signature == NULL)
603 PKA->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = modLen;
604 PKA->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS + 1] = 0;
607 PKA->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = orderLen;
608 PKA->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS + 1] = 0;
611 PKA->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = 0;
612 PKA->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN + 1] = 0;
618 pkaImportMpi(¶ms->
g.
x, modLen, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X);
619 pkaImportMpi(¶ms->
g.
y, modLen, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y);
620 pkaImportMpi(¶ms->
q, orderLen, PKA_ECDSA_SIGN_IN_ORDER_N);
621 pkaImportMpi(&privateKey->
d, orderLen, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D);
625 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
627 pkaImportArray(digest, digestLen, orderLen, PKA_ECDSA_SIGN_IN_HASH_E);
633 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
637 temp = PKA->CR & ~PKA_CR_MODE;
641 PKA->CR |= PKA_CR_START;
645 while((PKA->SR & PKA_SR_PROCENDF) == 0)
663 error =
pkaExportMpi(&signature->
r, orderLen, PKA_ECDSA_SIGN_OUT_SIGNATURE_R);
670 error =
pkaExportMpi(&signature->
s, orderLen, PKA_ECDSA_SIGN_OUT_SIGNATURE_S);
674 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
699 const EcPublicKey *publicKey,
const uint8_t *digest,
size_t digestLen,
708 if(params == NULL || publicKey == NULL || digest == NULL || signature == NULL)
740 PKA->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = modLen;
741 PKA->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS + 1] = 0;
744 PKA->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = orderLen;
745 PKA->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS + 1] = 0;
748 PKA->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = 0;
749 PKA->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN + 1] = 0;
753 pkaImportMpi(¶ms->
a, modLen, PKA_ECDSA_VERIF_IN_A_COEFF);
754 pkaImportMpi(¶ms->
g.
x, modLen, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X);
755 pkaImportMpi(¶ms->
g.
y, modLen, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y);
756 pkaImportMpi(¶ms->
q, orderLen, PKA_ECDSA_VERIF_IN_ORDER_N);
757 pkaImportMpi(&publicKey->
q.
x, modLen, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X);
758 pkaImportMpi(&publicKey->
q.
y, modLen, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y);
759 pkaImportMpi(&signature->
r, orderLen, PKA_ECDSA_VERIF_IN_SIGNATURE_R);
760 pkaImportMpi(&signature->
s, orderLen, PKA_ECDSA_VERIF_IN_SIGNATURE_S);
763 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
765 pkaImportArray(digest, digestLen, orderLen, PKA_ECDSA_VERIF_IN_HASH_E);
771 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
775 temp = PKA->CR & ~PKA_CR_MODE;
779 PKA->CR |= PKA_CR_START;
783 while((PKA->SR & PKA_SR_PROCENDF) == 0)
798 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
808 #if (X25519_SUPPORT == ENABLED || ED25519_SUPPORT == ENABLED)
827 PKA->RAM[PKA_ARITHMETIC_MUL_NB_BITS] = 255;
830 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1] =
a[0];
831 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 1] =
a[1];
832 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 2] =
a[2];
833 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 3] =
a[3];
834 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 4] =
a[4];
835 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 5] =
a[5];
836 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 6] =
a[6];
837 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 7] =
a[7];
840 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 8] = 0;
841 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 9] = 0;
844 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2] =
b[0];
845 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 1] =
b[1];
846 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 2] =
b[2];
847 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 3] =
b[3];
848 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 4] =
b[4];
849 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 5] =
b[5];
850 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 6] =
b[6];
851 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 7] =
b[7];
854 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 8] = 0;
855 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 9] = 0;
858 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
862 temp = PKA->CR & ~PKA_CR_MODE;
866 PKA->CR |= PKA_CR_START;
870 while((PKA->SR & PKA_SR_PROCENDF) == 0)
875 u[0] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT];
876 u[1] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 1];
877 u[2] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 2];
878 u[3] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 3];
879 u[4] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 4];
880 u[5] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 5];
881 u[6] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 6];
882 u[7] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 7];
883 u[8] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 8];
884 u[9] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 9];
885 u[10] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 10];
886 u[11] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 11];
887 u[12] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 12];
888 u[13] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 13];
889 u[14] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 14];
890 u[15] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 15];
893 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
899 temp = (
u[7] >> 31) * 19;
904 for(i = 0; i < 8; i++)
907 temp += (uint64_t)
u[i + 8] * 38;
908 u[i] = temp & 0xFFFFFFFF;
915 temp += (
u[7] >> 31) * 19;
920 for(i = 0; i < 8; i++)
923 u[i] = temp & 0xFFFFFFFF;
932 #if (X448_SUPPORT == ENABLED || ED448_SUPPORT == ENABLED)
952 PKA->RAM[PKA_ARITHMETIC_MUL_NB_BITS] = 448;
955 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1] =
a[0];
956 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 1] =
a[1];
957 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 2] =
a[2];
958 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 3] =
a[3];
959 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 4] =
a[4];
960 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 5] =
a[5];
961 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 6] =
a[6];
962 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 7] =
a[7];
963 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 8] =
a[8];
964 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 9] =
a[9];
965 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 10] =
a[10];
966 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 11] =
a[11];
967 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 12] =
a[12];
968 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 13] =
a[13];
971 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 14] = 0;
972 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 15] = 0;
975 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2] =
b[0];
976 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 1] =
b[1];
977 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 2] =
b[2];
978 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 3] =
b[3];
979 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 4] =
b[4];
980 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 5] =
b[5];
981 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 6] =
b[6];
982 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 7] =
b[7];
983 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 8] =
b[8];
984 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 9] =
b[9];
985 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 10] =
b[10];
986 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 11] =
b[11];
987 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 12] =
b[12];
988 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 13] =
b[13];
991 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 14] = 0;
992 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 15] = 0;
995 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
999 temp = PKA->CR & ~PKA_CR_MODE;
1003 PKA->CR |= PKA_CR_START;
1007 while((PKA->SR & PKA_SR_PROCENDF) == 0)
1012 u[0] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT];
1013 u[1] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 1];
1014 u[2] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 2];
1015 u[3] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 3];
1016 u[4] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 4];
1017 u[5] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 5];
1018 u[6] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 6];
1019 u[7] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 7];
1020 u[8] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 8];
1021 u[9] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 9];
1022 u[10] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 10];
1023 u[11] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 11];
1024 u[12] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 12];
1025 u[13] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 13];
1026 u[14] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 14];
1027 u[15] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 15];
1028 u[16] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 16];
1029 u[17] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 17];
1030 u[18] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 18];
1031 u[19] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 19];
1032 u[20] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 20];
1033 u[21] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 21];
1034 u[22] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 22];
1035 u[23] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 23];
1036 u[24] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 24];
1037 u[25] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 25];
1038 u[26] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 26];
1039 u[27] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 27];
1042 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
1048 for(temp = 0, i = 0; i < 7; i++)
1053 u[i] = temp & 0xFFFFFFFF;
1057 for(i = 7; i < 14; i++)
1061 temp += (uint64_t)
u[i + 14] << 1;
1062 u[i] = temp & 0xFFFFFFFF;
1067 for(
c = temp, i = 0; i < 7; i++)
1070 u[i] = temp & 0xFFFFFFFF;
1074 for(temp +=
c, i = 7; i < 14; i++)
1077 u[i] = temp & 0xFFFFFFFF;