2024-05-28 04:10 AM
Hi,
I´m trying to implement a simple ECDH test by implementing the functions from the Cryptographic Library V4.2.0.
There are two persons A and B generating a pair of private and public keys. The keys are generated by the cmox_ecdsa_keyGen(...) function.
The curve is BrainpoolP256R1.
When trying to calculate the shared secret with cmox_ecdh(...) the function returns with CMOX_ECC_ERR_INVALID_PUBKEY.
I´m assuming that the generated public Key is not valid (it´s not placed in the elliptic curve).
What could be the cause?
Thank you!
Here´s my code:
void ECDH_test(void)
{
//Prepare Time Measurement
#define CYCCNTENA 0x00000001
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CTRL |= CYCCNTENA;
/***********************************************************************************************************/
//Variables
cmox_ecc_handle_t Ecc_Ctx;
uint8_t Working_Buffer[2048];
cmox_ecc_retval_t retval;
size_t computed_size;
//Choose Random 32 Byte Value for Person A
uint8_t RandomA[] =
{
0x7e, 0x21, 0x78, 0xf7, 0x1e, 0xb2, 0x4d, 0xda, 0xf8, 0x0d, 0x45, 0x14, 0x63, 0x2e, 0xdd, 0xe0,
0x22, 0x90, 0x58, 0xaf, 0x1f, 0xb6, 0x00, 0x2e, 0xd8, 0x11, 0xdd, 0xb6, 0x2b, 0xc1, 0xa5, 0x34
};
//Choose Random 32 Byte Value for Person B
uint8_t RandomB[] =
{
0x8e, 0xf1, 0x73, 0xf7, 0x1e, 0xb2, 0x4d, 0xda, 0xf8, 0x0d, 0x00, 0x14, 0x63, 0x2e, 0xdd, 0xe0,
0x22, 0x90, 0x58, 0xaf, 0xee, 0xb6, 0x00, 0x2e, 0xd8, 0x11, 0x11, 0xb6, 0x2b, 0xc1, 0xa5, 0x22
};
//Private Keys
uint8_t Private_KeyA[CMOX_ECC_BPP256R1_PRIVKEY_LEN];
uint8_t Private_KeyB[CMOX_ECC_BPP256R1_PRIVKEY_LEN];
//Public Keys
uint8_t Remote_Public_KeyB[CMOX_ECC_BPP256R1_PUBKEY_LEN];
uint8_t Remote_Public_KeyA[CMOX_ECC_BPP256R1_PUBKEY_LEN];
//Secret
uint8_t Computed_SecretA[CMOX_ECC_BPP256R1_SECRET_LEN];
uint8_t Computed_SecretB[CMOX_ECC_BPP256R1_SECRET_LEN];
/***********************************************************************************************************/
//Start Time Measurement
timestamp1 = DWT->CYCCNT;
//Reset CRC, then Enable CRC
HAL_CRC_DeInit(&hcrc);
cmox_ll_init(NULL);
if (cmox_initialize(NULL) != CMOX_INIT_SUCCESS)
{
Error_Handler();
}
cmox_ecc_construct(&Ecc_Ctx, CMOX_ECC256_MATH_FUNCS, Working_Buffer, sizeof(Working_Buffer));
//Generate Key Pair for A
retval = cmox_ecdsa_keyGen(&Ecc_Ctx,
CMOX_ECC_CURVE_BPP256R1,
RandomA, sizeof(RandomA),
Private_KeyA, NULL,
Remote_Public_KeyA, NULL);
if (retval != CMOX_ECC_SUCCESS)
{
Error_Handler();
}
//Generate Key Pair for B
retval = cmox_ecdsa_keyGen(&Ecc_Ctx,
CMOX_ECC_CURVE_BPP256R1,
RandomB, sizeof(RandomA),
Private_KeyB, NULL,
Remote_Public_KeyB, NULL);
if (retval != CMOX_ECC_SUCCESS)
{
Error_Handler();
}
//A calculates the secret with his own private Key and B´s Public Key
retval = cmox_ecdh(&Ecc_Ctx,
CMOX_ECC_CURVE_BPP256R1,
Private_KeyA, sizeof(Private_KeyA),
Remote_Public_KeyB, sizeof(Remote_Public_KeyB),
Computed_SecretA, &computed_size);
if (retval != CMOX_ECC_SUCCESS)
{
Error_Handler();
}
/* Cleanup context */
cmox_ecc_cleanup(&Ecc_Ctx);
/* No more need of cryptographic services, finalize cryptographic library */
if (cmox_finalize(NULL) != CMOX_INIT_SUCCESS)
{
Error_Handler();
}
//Stop Time Measurement
timestamp2 = DWT->CYCCNT;
runtime = (timestamp2 - timestamp1) / 16000;
__NOP();
}
Solved! Go to Solution.
2024-05-29 02:25 AM
I found the solution:
The CRC Module won´t reset to the default state by simply calling
HAL_CRC_DeInit(&hcrc);
You have to call:
HAL_CRC_DeInit(&hcrc);
__HAL_RCC_CRC_FORCE_RESET();
__HAL_RCC_CRC_RELEASE_RESET();
__HAL_RCC_CRC_CLK_ENABLE();
2024-05-29 02:25 AM
I found the solution:
The CRC Module won´t reset to the default state by simply calling
HAL_CRC_DeInit(&hcrc);
You have to call:
HAL_CRC_DeInit(&hcrc);
__HAL_RCC_CRC_FORCE_RESET();
__HAL_RCC_CRC_RELEASE_RESET();
__HAL_RCC_CRC_CLK_ENABLE();