From 33287ae1340ea4a6225dc9e2016b91bf115214f2 Mon Sep 17 00:00:00 2001 From: Dave Rodgman Date: Mon, 11 Sep 2023 17:03:22 +0100 Subject: [PATCH] Tidy up mbedtls_asn1_write_len Signed-off-by: Dave Rodgman --- library/asn1write.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/library/asn1write.c b/library/asn1write.c index 4887f1551..7d533491f 100644 --- a/library/asn1write.c +++ b/library/asn1write.c @@ -37,9 +37,8 @@ int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t #endif int required = 1; - if (len < 0x80) { - required = 1; - } else { + + if (len >= 0x80) { for (size_t l = len; l != 0; l >>= 8) { required++; } @@ -55,7 +54,7 @@ int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t } while (len); if (required > 1) { - *--(*p) = (unsigned char) (required + 0x7f); + *--(*p) = (unsigned char) (0x80 + required - 1); } return required;