From 12fd92723d71325b74a6c94eee4ca504773ad9da Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Sun, 6 Sep 2015 13:46:41 -0400 Subject: Add ECPoint I/O functions. ASN.1 cleanup. --- asn1.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'asn1.c') diff --git a/asn1.c b/asn1.c index 98cb734..2ea44bd 100644 --- a/asn1.c +++ b/asn1.c @@ -58,8 +58,18 @@ #include "asn1_internal.h" /* - * Encode tag and length fields of an ASN.1 object. If der is NULL, - * just return the size that would be encoded. + * Encode tag and length fields of an ASN.1 object. + * + * Sets *der_len to the size of of the ASN.1 header (tag and length + * fields); caller supplied length of value field, so presumably + * already knows it. + * + * If der is NULL, just return the size of the header that would be + * encoded and returns HAL_OK. + * + * If der isn't NULL, returns HAL_ERROR_RESULT_TOO_LONG unless full + * header plus value will fit; this is a bit weird, but is useful when + * using this to construct encoders for complte ASN.1 objects. */ hal_error_t hal_asn1_encode_header(const uint8_t tag, @@ -126,17 +136,15 @@ hal_error_t hal_asn1_encode_integer(const fp_int * const bn, hal_error_t err; size_t hlen; - if ((err = hal_asn1_encode_header(ASN1_INTEGER, vlen, der, &hlen, der_max)) != HAL_OK) - return err; + err = hal_asn1_encode_header(ASN1_INTEGER, vlen, der, &hlen, der_max); if (der_len != NULL) *der_len = hlen + vlen; - if (der == NULL) - return HAL_OK; + if (der == NULL || err != HAL_OK) + return err; - if (hlen + vlen > der_max) - return HAL_ERROR_RESULT_TOO_LONG; + assert(hlen + vlen <= der_max); der += hlen; if (leading_zero) -- cgit v1.2.3