32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "stm32mp13xx.h"
36 #include "stm32mp13xx_hal.h"
46 #if (STM32MP13XX_CRYPTO_PKC_SUPPORT == ENABLED)
57 __HAL_RCC_PKA_CLK_ENABLE();
63 while((PKA->CR & PKA_CR_EN) == 0)
69 PKA->CLRFR = PKA_CLRFR_ADDRERRFC | PKA_CLRFR_RAMERRFC | PKA_CLRFR_PROCENDFC;
92 destLen = (destLen + 63) / 64;
95 for(i = 0, j = 0; i < srcLen; i++)
100 temp = src[srcLen - i - 1];
103 temp |= src[srcLen - i - 1] << 8;
106 temp |= src[srcLen - i - 1] << 16;
109 temp |= src[srcLen - i - 1] << 24;
110 PKA->RAM[offset + j] = temp;
117 for(; i < (destLen * 8); i++)
125 PKA->RAM[offset + j] = temp;
134 PKA->RAM[offset + j] = 0;
135 PKA->RAM[offset + j + 1] = 0;
158 for(i = 0; i <
n && i < (
length * 2); i++)
160 PKA->RAM[offset + i] =
a->data[i];
164 for(; i < (
length * 2); i++)
166 PKA->RAM[offset + i] = 0;
170 PKA->RAM[offset + i] = 0;
171 PKA->RAM[offset + i + 1] = 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;
259 PKA->RAM[PKA_MODULAR_EXP_IN_OP_NB_BITS + 1] = 0;
262 PKA->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS] = expLen;
263 PKA->RAM[PKA_MODULAR_EXP_IN_EXP_NB_BITS + 1] = 0;
271 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
275 temp = PKA->CR & ~PKA_CR_MODE;
279 PKA->CR |= PKA_CR_START;
283 while((PKA->SR & PKA_SR_PROCENDF) == 0)
291 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
336 if(nLen <=
PKA_MAX_ROS && pLen <= (nLen / 2) && qLen <= (nLen / 2) &&
337 dpLen <= (nLen / 2) && dqLen <= (nLen / 2) && qinvLen <= (nLen / 2))
343 PKA->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS] = nLen;
344 PKA->RAM[PKA_RSA_CRT_EXP_IN_MOD_NB_BITS + 1] = 0;
355 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
359 temp = PKA->CR & ~PKA_CR_MODE;
363 PKA->CR |= PKA_CR_START;
367 while((PKA->SR & PKA_SR_PROCENDF) == 0)
375 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
456 scalarLen =
MAX(scalarLen, orderLen);
465 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS] = modLen;
466 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_OP_NB_BITS + 1] = 0;
469 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS] = scalarLen;
470 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_EXP_NB_BITS + 1] = 0;
473 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN] = 0;
474 PKA->RAM[PKA_ECC_SCALAR_MUL_IN_A_COEFF_SIGN + 1] = 0;
477 pkaImportMpi(¶ms->
p, modLen, PKA_ECC_SCALAR_MUL_IN_MOD_GF);
478 pkaImportMpi(¶ms->
a, modLen, PKA_ECC_SCALAR_MUL_IN_A_COEFF);
479 pkaImportMpi(¶ms->
b, modLen, PKA_ECC_SCALAR_MUL_IN_B_COEFF);
480 pkaImportMpi(¶ms->
q, scalarLen, PKA_ECC_SCALAR_MUL_IN_N_PRIME_ORDER);
482 pkaImportMpi(&
s->x, modLen, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_X);
483 pkaImportMpi(&
s->y, modLen, PKA_ECC_SCALAR_MUL_IN_INITIAL_POINT_Y);
489 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
493 temp = PKA->CR & ~PKA_CR_MODE;
497 PKA->CR |= PKA_CR_START;
501 while((PKA->SR & PKA_SR_PROCENDF) == 0)
519 error =
pkaExportMpi(&
r->x, modLen, PKA_ECC_SCALAR_MUL_OUT_RESULT_X);
526 error =
pkaExportMpi(&
r->y, modLen, PKA_ECC_SCALAR_MUL_OUT_RESULT_Y);
537 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
567 const uint8_t *digest,
size_t digestLen,
EcdsaSignature *signature)
576 if(params == NULL || privateKey == NULL || digest == NULL || signature == NULL)
601 PKA->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS] = modLen;
602 PKA->RAM[PKA_ECDSA_SIGN_IN_MOD_NB_BITS + 1] = 0;
605 PKA->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS] = orderLen;
606 PKA->RAM[PKA_ECDSA_SIGN_IN_ORDER_NB_BITS + 1] = 0;
609 PKA->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN] = 0;
610 PKA->RAM[PKA_ECDSA_SIGN_IN_A_COEFF_SIGN + 1] = 0;
616 pkaImportMpi(¶ms->
g.
x, modLen, PKA_ECDSA_SIGN_IN_INITIAL_POINT_X);
617 pkaImportMpi(¶ms->
g.
y, modLen, PKA_ECDSA_SIGN_IN_INITIAL_POINT_Y);
618 pkaImportMpi(¶ms->
q, orderLen, PKA_ECDSA_SIGN_IN_ORDER_N);
619 pkaImportMpi(&privateKey->
d, orderLen, PKA_ECDSA_SIGN_IN_PRIVATE_KEY_D);
623 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
625 pkaImportArray(digest, digestLen, orderLen, PKA_ECDSA_SIGN_IN_HASH_E);
631 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
635 temp = PKA->CR & ~PKA_CR_MODE;
639 PKA->CR |= PKA_CR_START;
643 while((PKA->SR & PKA_SR_PROCENDF) == 0)
661 error =
pkaExportMpi(&signature->
r, orderLen, PKA_ECDSA_SIGN_OUT_SIGNATURE_R);
668 error =
pkaExportMpi(&signature->
s, orderLen, PKA_ECDSA_SIGN_OUT_SIGNATURE_S);
672 PKA->CLRFR = PKA_CLRFR_PROCENDFC;
697 const EcPublicKey *publicKey,
const uint8_t *digest,
size_t digestLen,
706 if(params == NULL || publicKey == NULL || digest == NULL || signature == NULL)
738 PKA->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS] = modLen;
739 PKA->RAM[PKA_ECDSA_VERIF_IN_MOD_NB_BITS + 1] = 0;
742 PKA->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS] = orderLen;
743 PKA->RAM[PKA_ECDSA_VERIF_IN_ORDER_NB_BITS + 1] = 0;
746 PKA->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN] = 0;
747 PKA->RAM[PKA_ECDSA_VERIF_IN_A_COEFF_SIGN + 1] = 0;
751 pkaImportMpi(¶ms->
a, modLen, PKA_ECDSA_VERIF_IN_A_COEFF);
752 pkaImportMpi(¶ms->
g.
x, modLen, PKA_ECDSA_VERIF_IN_INITIAL_POINT_X);
753 pkaImportMpi(¶ms->
g.
y, modLen, PKA_ECDSA_VERIF_IN_INITIAL_POINT_Y);
754 pkaImportMpi(¶ms->
q, orderLen, PKA_ECDSA_VERIF_IN_ORDER_N);
755 pkaImportMpi(&publicKey->
q.
x, modLen, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_X);
756 pkaImportMpi(&publicKey->
q.
y, modLen, PKA_ECDSA_VERIF_IN_PUBLIC_KEY_POINT_Y);
757 pkaImportMpi(&signature->
r, orderLen, PKA_ECDSA_VERIF_IN_SIGNATURE_R);
758 pkaImportMpi(&signature->
s, orderLen, PKA_ECDSA_VERIF_IN_SIGNATURE_S);
761 digestLen =
MIN(digestLen, (orderLen + 7) / 8);
763 pkaImportArray(digest, digestLen, orderLen, PKA_ECDSA_VERIF_IN_HASH_E);
769 PKA->CR &= ~(PKA_CR_ADDRERRIE | PKA_CR_RAMERRIE | PKA_CR_PROCENDIE);
773 temp = PKA->CR & ~PKA_CR_MODE;
777 PKA->CR |= PKA_CR_START;
781 while((PKA->SR & PKA_SR_PROCENDF) == 0)
796 PKA->CLRFR = PKA_CLRFR_PROCENDFC;