From 5aef2971e659ee5f6a7362a7cda3290e07c501cd Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Thu, 30 Jun 2022 04:38:02 -0400 Subject: [PATCH] mbedtls_x509_time_cmp() perf faster comparison of mbedtls_x509_time values with valid ranges per elt Signed-off-by: Glenn Strauss --- library/x509.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/library/x509.c b/library/x509.c index 031a3f0e3..ba800377c 100644 --- a/library/x509.c +++ b/library/x509.c @@ -997,27 +997,17 @@ int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name) int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1, const mbedtls_x509_time *t2) { - if (t1->year != t2->year) { - return t1->year - t2->year; + int x; + + x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) - + ((t2->year << 9) | (t2->mon << 5) | (t2->day))); + if (x != 0) { + return x; } - if (t1->mon != t2->mon) { - return t1->mon - t2->mon; - } - - if (t1->day != t2->day) { - return t1->day - t2->day; - } - - if (t1->hour != t2->hour) { - return t1->hour - t2->hour; - } - - if (t1->min != t2->min) { - return t1->min - t2->min; - } - - return t1->sec - t2->sec; + x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) - + ((t2->hour << 12) | (t2->min << 6) | (t2->sec))); + return x; } #if defined(MBEDTLS_HAVE_TIME_DATE)