Open gilles-peskine-arm opened 3 months ago
unsigned char *octets_head;
size_t octets_len;
size_t bits = mbedtls_mpi_core_bitlen(A, A_limbs); // or mbedtls_mpi_bitlen(A)
mbedtls_asn1_write_mpi(p, start, bits, &octets_head, &octets_len);
mbedtls_mpi_core_write_be(A, A_limbs, octets_head, octets_len);
Downside: two functions, requires the caller to pass matching arguments.
mbedtls_asn1_write_mpi_core(p, start, A, A_limbs);
Downside: the same code needs to understand both the ASN.1 encoding and the bignum representation.
In Mbed TLS 4.0,
mbedtls_mpi
is becoming private (to be removed in 4.0 or a later 4.x in favor of the representations inbignum_core.h
andbignum_mod.h
). We need to change the interface of the ASN.1 integer writing function accordingly.The typical usage will be to encode an integer exported with
mbedtls_mpi_core_write_be
. What should the interface be? Note that ASN.1 encoding needs to know the exact bit-length, not just the exact byte-length, since the top bit of the first byte of the content must be 0 (it's a sign bit), e.g. 0x007f0000 is encoded as02037f0000
but 0x00800000 as020400080000
. That makes it difficult to combine an ASN.1 writing function withmbedtls_mpi_core_write_be
.Definition of done:
mbedtls_asn1_write_integer
with a bignum.mbedtls_asn1_write_mpi
to an internal header (bignum.h
, which will be made non-public?), as it is still useful for modules that usembedtls_mpi
internally.