32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "stm32wbxx.h"
36 #include "stm32wbxx_hal.h"
48 #if (STM32WBXX_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 + 31) / 32;
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 * 4); i++)
127 PKA->RAM[offset + j] = temp;
136 PKA->RAM[offset + j] = 0;
159 for(i = 0; i <
n && i <
length; i++)
161 PKA->RAM[offset + i] =
a->data[i];
167 PKA->RAM[offset + i] = 0;
171 PKA->RAM[offset + i] = 0;
204 for(i = 0; i <
length; i++)
206 r->data[i] = PKA->RAM[offset + i];
210 for(; i <
r->size; i++)
258 PKA->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS] = modLen;
260 PKA->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = expLen;
268 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
272 temp = PKA->CR & ~PKA_CR_MODE;
276 PKA->CR |= PKA_CR_START;
280 while((PKA->SR & PKA_SR_PROCENDF) == 0)
285 error =
pkaExportMpi(
r, modLen, PKA_MODULAR_EXP_OUT_SM_ALGO_ACC1);
288 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
333 if(nLen <=
PKA_MAX_ROS && pLen <= (nLen / 2) && qLen <= (nLen / 2) &&
334 dpLen <= (nLen / 2) && dqLen <= (nLen / 2) && qinvLen <= (nLen / 2))
340 PKA->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = nLen;
351 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
355 temp = PKA->CR & ~PKA_CR_MODE;
359 PKA->CR |= PKA_CR_START;
363 while((PKA->SR & PKA_SR_PROCENDF) == 0)
371 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
452 if(nLen == 0 || eLen == 0)
533 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = modLen;
535 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = scalarLen;
537 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = 0;
540 pkaImportMpi(¶ms->
p, modLen, PKA_ECC_SCALAR_MUL_IN_MOD_GF);
541 pkaImportMpi(¶ms->
a, modLen, PKA_ECC_SCALAR_MUL_IN_A_COEFF);
543 pkaImportMpi(&
s->x, modLen, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X);
544 pkaImportMpi(&
s->y, modLen, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y);
547 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
551 temp = PKA->CR & ~PKA_CR_MODE;
555 PKA->CR |= PKA_CR_START;
559 while((PKA->SR & PKA_SR_PROCENDF) == 0)
564 error =
pkaExportMpi(&
r->x, modLen, PKA_ECC_SCALAR_MUL_OUT_RESULT_X);
570 error =
pkaExportMpi(&
r->y, modLen, PKA_ECC_SCALAR_MUL_OUT_RESULT_Y);
581 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
611 const uint8_t *digest,
size_t digestLen,
EcdsaSignature *signature)
620 if(params == NULL || privateKey == NULL || digest == NULL || signature == NULL)
645 PKA->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = modLen;
647 PKA->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = orderLen;
649 PKA->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = 0;
654 pkaImportMpi(¶ms->
g.
x, modLen, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X);
655 pkaImportMpi(¶ms->
g.
y, modLen, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y);
656 pkaImportMpi(¶ms->
q, orderLen, PKA_ECDSA_SIGN_IN_ORDER_N);
657 pkaImportMpi(&privateKey->
d, orderLen, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D);
661 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
663 pkaImportArray(digest, digestLen, orderLen, PKA_ECDSA_SIGN_IN_HASH_E);
669 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
673 temp = PKA->CR & ~PKA_CR_MODE;
677 PKA->CR |= PKA_CR_START;
681 while((PKA->SR & PKA_SR_PROCENDF) == 0)
699 error =
pkaExportMpi(&signature->
r, orderLen, PKA_ECDSA_SIGN_OUT_SIGNATURE_R);
706 error =
pkaExportMpi(&signature->
s, orderLen, PKA_ECDSA_SIGN_OUT_SIGNATURE_S);
710 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
735 const EcPublicKey *publicKey,
const uint8_t *digest,
size_t digestLen,
744 if(params == NULL || publicKey == NULL || digest == NULL || signature == NULL)
776 PKA->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = modLen;
778 PKA->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = orderLen;
780 PKA->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = 0;
784 pkaImportMpi(¶ms->
a, modLen, PKA_ECDSA_VERIF_IN_A_COEFF);
785 pkaImportMpi(¶ms->
g.
x, modLen, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X);
786 pkaImportMpi(¶ms->
g.
y, modLen, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y);
787 pkaImportMpi(¶ms->
q, orderLen, PKA_ECDSA_VERIF_IN_ORDER_N);
788 pkaImportMpi(&publicKey->
q.
x, modLen, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X);
789 pkaImportMpi(&publicKey->
q.
y, modLen, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y);
790 pkaImportMpi(&signature->
r, orderLen, PKA_ECDSA_VERIF_IN_SIGNATURE_R);
791 pkaImportMpi(&signature->
s, orderLen, PKA_ECDSA_VERIF_IN_SIGNATURE_S);
794 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
796 pkaImportArray(digest, digestLen, orderLen, PKA_ECDSA_VERIF_IN_HASH_E);
802 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
806 temp = PKA->CR & ~PKA_CR_MODE;
810 PKA->CR |= PKA_CR_START;
814 while((PKA->SR & PKA_SR_PROCENDF) == 0)
829 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
839 #if (X25519_SUPPORT == ENABLED || ED25519_SUPPORT == ENABLED)
858 PKA->RAM[PKA_ARITHMETIC_MUL_NB_BITS] = 255;
861 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1] =
a[0];
862 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 1] =
a[1];
863 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 2] =
a[2];
864 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 3] =
a[3];
865 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 4] =
a[4];
866 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 5] =
a[5];
867 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 6] =
a[6];
868 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 7] =
a[7];
871 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 8] = 0;
874 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2] =
b[0];
875 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 1] =
b[1];
876 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 2] =
b[2];
877 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 3] =
b[3];
878 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 4] =
b[4];
879 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 5] =
b[5];
880 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 6] =
b[6];
881 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 7] =
b[7];
884 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 8] = 0;
887 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
891 temp = PKA->CR & ~PKA_CR_MODE;
895 PKA->CR |= PKA_CR_START;
899 while((PKA->SR & PKA_SR_PROCENDF) == 0)
904 u[0] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT];
905 u[1] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 1];
906 u[2] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 2];
907 u[3] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 3];
908 u[4] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 4];
909 u[5] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 5];
910 u[6] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 6];
911 u[7] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 7];
912 u[8] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 8];
913 u[9] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 9];
914 u[10] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 10];
915 u[11] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 11];
916 u[12] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 12];
917 u[13] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 13];
918 u[14] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 14];
919 u[15] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 15];
922 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
928 temp = (
u[7] >> 31) * 19;
933 for(i = 0; i < 8; i++)
936 temp += (uint64_t)
u[i + 8] * 38;
937 u[i] = temp & 0xFFFFFFFF;
944 temp += (
u[7] >> 31) * 19;
949 for(i = 0; i < 8; i++)
952 u[i] = temp & 0xFFFFFFFF;
961 #if (X448_SUPPORT == ENABLED || ED448_SUPPORT == ENABLED)
981 PKA->RAM[PKA_ARITHMETIC_MUL_NB_BITS] = 448;
984 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1] =
a[0];
985 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 1] =
a[1];
986 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 2] =
a[2];
987 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 3] =
a[3];
988 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 4] =
a[4];
989 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 5] =
a[5];
990 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 6] =
a[6];
991 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 7] =
a[7];
992 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 8] =
a[8];
993 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 9] =
a[9];
994 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 10] =
a[10];
995 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 11] =
a[11];
996 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 12] =
a[12];
997 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 13] =
a[13];
1000 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP1 + 14] = 0;
1003 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2] =
b[0];
1004 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 1] =
b[1];
1005 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 2] =
b[2];
1006 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 3] =
b[3];
1007 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 4] =
b[4];
1008 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 5] =
b[5];
1009 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 6] =
b[6];
1010 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 7] =
b[7];
1011 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 8] =
b[8];
1012 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 9] =
b[9];
1013 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 10] =
b[10];
1014 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 11] =
b[11];
1015 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 12] =
b[12];
1016 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 13] =
b[13];
1019 PKA->RAM[PKA_ARITHMETIC_MUL_IN_OP2 + 14] = 0;
1022 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
1026 temp = PKA->CR & ~PKA_CR_MODE;
1030 PKA->CR |= PKA_CR_START;
1034 while((PKA->SR & PKA_SR_PROCENDF) == 0)
1039 u[0] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT];
1040 u[1] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 1];
1041 u[2] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 2];
1042 u[3] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 3];
1043 u[4] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 4];
1044 u[5] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 5];
1045 u[6] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 6];
1046 u[7] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 7];
1047 u[8] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 8];
1048 u[9] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 9];
1049 u[10] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 10];
1050 u[11] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 11];
1051 u[12] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 12];
1052 u[13] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 13];
1053 u[14] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 14];
1054 u[15] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 15];
1055 u[16] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 16];
1056 u[17] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 17];
1057 u[18] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 18];
1058 u[19] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 19];
1059 u[20] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 20];
1060 u[21] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 21];
1061 u[22] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 22];
1062 u[23] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 23];
1063 u[24] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 24];
1064 u[25] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 25];
1065 u[26] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 26];
1066 u[27] = PKA->RAM[PKA_ARITHMETIC_MUL_OUT_RESULT + 27];
1069 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
1075 for(temp = 0, i = 0; i < 7; i++)
1080 u[i] = temp & 0xFFFFFFFF;
1084 for(i = 7; i < 14; i++)
1088 temp += (uint64_t)
u[i + 14] << 1;
1089 u[i] = temp & 0xFFFFFFFF;
1094 for(
c = temp, i = 0; i < 7; i++)
1097 u[i] = temp & 0xFFFFFFFF;
1101 for(temp +=
c, i = 7; i < 14; i++)
1104 u[i] = temp & 0xFFFFFFFF;