Arm Mbed OS support forum

How to use "mbedtls_ecdsa_sign_det()"?

Hi @matthewc
If you have a standard library, why are you using the memory_buffer feature?
I wouldn’t advise that.

However, in your example, you can see that Q is initiated at your code, soit should be freed at your code. How does it come from outside? Are you using by any chance a pointer to a pointer of Q?
Regards

Hi Roneld,

First off, I’m sorry for my poor explanation.

At first, mbedtls_pk_init() and mbedtls_pk_parse_public_key() is called for extracting Q.
(This is executed with standard library.)
And then mbedtls_pk_context value for Q is delivered to another function.

And then, this new called function will run below with delivered Q value.
mbedtls_ecp_point Q;
mbedtls_memory_buffer_alloc_init()
ecp = mbedtls_pk_ec(key)
mbedtls_ecp_point_init(&Q)
Q = ecp->Q;
(Standard library cannot be run here. So, mbedtls_memory_buffer_alloc_init() is called here. )

I called “mbedtls_ecp_point_init(&Q)” after mbedtls_pk_init() and mbedtls_pk_parse_public_key() are called.
I might guess I don’t need to initiate Q value through mbedtls_ecp_point_init() again.
I also guess It might not make “FATAL: mbedtls_free() outside of managed space”, if I don’t call mbedtls_ecp_point_init(&Q).
What do you think about it?

Thank a lot,
Matthew

Hi @matthewc
If you have parsed the key, and already have the key context, then yes, you shouldn’t initiate Q with mbedtls_ecp_point_init() and you shouldn’t free it at the end of this function.
In addition, you shouldn’t assign ecp->Q by value, but Q should be a pointer to ecp->Q
In short, instead of Q define mbedtls_ecp_point* Q; and assign pQ = & ecp->Q;
After that you should use pQ instead of %Q in all the places in the function.
However, if the only place you are using Q is for mbedtls_ecp_check_pubkey(&grp, &Q), then you could remove Q and call mbedtls_ecp_check_pubkey(&grp, &ecp->Q)
Regards

Hi Roneld,

It seems that all things are cleared. I appreciate your help.

Thanks,
Matthew