mbedtls_ecp_write_key() function
This function exports an elliptic curve private key.
Arguments
buf
The output buffer for containing the binary representation of the key. For Weierstrass curves, this is the big-endian representation, padded with null bytes at the beginning to reach \p buflen bytes. For Montgomery curves, this is the standard byte string representation (which is little-endian), padded with null bytes at the end to reach \p buflen bytes.
buflen
The total length of the buffer in bytes. The length of the output is (`grp->nbits` + 7) / 8 bytes where `grp->nbits` is the private key size in bits. For Weierstrass keys, if the output buffer is smaller, leading zeros are trimmed to fit if possible. For Montgomery keys, the output buffer must always be large enough for the nominal length.
Return value
\c 0 on success. #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL or #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the \p key representation is larger than the available space in \p buf. Another negative error code on different kinds of failure.
Notes
Note that although this function accepts an output buffer that is smaller or larger than the key, most key import interfaces require the output to have exactly key's nominal length. It is generally simplest to pass the key's nominal length as \c buflen, after checking that the output buffer is large enough. See the description of the \p buflen parameter for how to calculate the nominal length. If the private key was not set in \p key, the output is unspecified. Future versions may return an error in that case. This function does not support Curve448 yet.