Arm Mbed and Pelion Device Management support forum

Keys serialization

Hi !

I have generated ECC521 key pair and keep it as mbedtls_pk_context blob.
It seems to me It consume > 5K of memory from mbedtls heap.
( No idea yet why )

Are mbedtls has some API for serialize mbedtls_pk_context to plain array and recover back ?
I would like to store pair as file and recover back when need.

Or how this kind of issues are handled usually ?

Serialization and recover can be used for compact mbedtls heap as well. Blob occupy ~80 blocks and may be they not so contiguous. If I have few key pairs they fragment heap a lot and I need more heap for execute other operations. As result more and more heap reservation for predict worst case scenario.

Regards,
Eugene

Hi @Eugene68!

As mentioned here, Mbed TLS is now maintained under open governance at TrustedFirmware.org

please post your questions there next time, if these are not Mbed OS or Pelion related.

It is better you don’t store it as an mbedtls_pk_context blob, as it also stores pointers to data in non persistent memory.

Have you considered using mbedtls_pk_write_key_der() which converts the context into its DER format?
Regards,
Mbed Support
Ron

Hi Ron !

Be honest, I haven’t find any forum at T,F.org. Should I just write free rotm email to mbed-tls@lists.trustedfirmware.org ?

I can see mbedtls_pk_write_key_der() and mbedtls_pk_write_pubkey_der() dump private or/and public keys only. But may be I 'm not understand whole functionality of those.

What API I should use to restore mbedtls_pk_context from der for be sure if I not loze nothing.

Regards,
Eugene

Hi @Eugene68

Be honest, I haven’t find any forum at T,F.org. Should I just write free rotm email to mbed-tls@lists.trustedfirmware.org ?

Yes, they don’t use a forum, but a public mailing list.

I can see mbedtls_pk_write_key_der() and mbedtls_pk_write_pubkey_der() dump private or/and public keys only. But may be I 'm not understand whole functionality of those.

Correct, this writes the public\private key in DER form (PKCS8). You can store the result in a non volatile memory to be used later. Once you have it stored, you don’t need to repeat this operation, until you wish to repalce the key ( for example if it was revoked)

What API I should use to restore mbedtls_pk_context from der for be sure if I not loze nothing.

In order to get the mbedtls_pk_context, you should call mbedtls_pk_parse_key()
This is the proper way to store your data, as the DER format is standardized, and can be used by third party as well.

Regards,

Hi Ron !

Thank you !

You mean, I should save public and private key separately and use 2 different APIs for extract those.
The same vice versa , restore key what is need for current operation and do what is need.

Does this topic is still valid : https://tls.mbed.org/discussions/generic/howto-determine-exact-buffer-len-for-mbedtls_pk_write_pubkey_der ?

And I should just take bug enough buffer for keep ECC521 keypair.

Regards,
Eugene

Hi Eugene,

You mean, I should save public and private key separately and use 2 different APIs for extract those.
The same vice versa , restore key what is need for current operation and do what is need.

Depending on what you need. A private key is combined from both the private and the public components of the key pair, while the public key is combined only from the public components.
If you need in your device to do operations that require the private components ( asymmetric signing), then you should store the private key in a secure storage in your device. If you need only public operations ( signature verification) then you should store only the public key, however it is most likely that the signature verification that you will need to do is using someone else’s public key. No real need to store both. ( of same key). However, if the device is using a key, it is most likely to sign its messages to an external peer, in this case, that external peer would need your public key

Hi Ron !

I found one more operation what will need.
If I keep key pair in DER format and need to export from it public key only in DER format.
I can’t see any direct API. Looks like I should restore mbedtls_pk_context and after that export public key in DER only.
Is this true ?

Regards,
Eugene

Hi Eugene,
Well, yes. But if you only need the public key, you can store the public key alone.
If you need the public key to send to external peers, then yes, you will probably need the mbedtls_pk_context for all the PK operations anyway, so use this context to get the public key in DER \ PEM format for the remote peer
Regards

Hi Ron !

Could you help to estimate max possible buffer size for ECC521 keypair.
I can see mbedtls_pk_write_key_der returns 222 or 223 bytes sometimes.
How constant this value for certain keypair type.
I think I can reserve 256 or even 512 bytes for output buffer and it should be enough for all deviation in DER output. Is this right approach ?

By the way why mbedtls_pk_context allocate from heap ~5000 bytes / 80 blocks in case of ECC521 ?
Looks like quite big values, but internal mbedtls allocator show those digits.
Or it is OK ?

Regards,
Eugene