@@ -864,6 +864,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t macro_test(void);
864864WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void);
865865WOLFSSL_TEST_SUBROUTINE wc_test_ret_t flags_test(void);
866866WOLFSSL_TEST_SUBROUTINE wc_test_ret_t octets_test(void);
867+ #ifdef WC_TEST_CONSTANTCOMPARE
868+ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t constantcompare_test(void);
869+ #endif
867870WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void);
868871WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void);
869872WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void);
@@ -2729,6 +2732,13 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
27292732 else
27302733 TEST_PASS("MEMORY test passed!\n");
27312734
2735+ #ifdef WC_TEST_CONSTANTCOMPARE
2736+ if ( (ret = constantcompare_test()) != 0)
2737+ TEST_FAIL("constantcompare test failed!\n", ret);
2738+ else
2739+ TEST_PASS("constantcompare test passed!\n");
2740+ #endif
2741+
27322742 if ( (ret = octets_test()) != 0)
27332743 TEST_FAIL("octets test failed!\n", ret);
27342744 else
@@ -5523,6 +5533,75 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t octets_test(void)
55235533 return 0;
55245534}
55255535
5536+ #ifdef WC_TEST_CONSTANTCOMPARE
5537+
5538+ typedef int (*constantcompare_fn)(const byte* a, const byte* b, int length);
5539+
5540+ /* Run the ConstantCompare() checks against cmp.
5541+ * Returns 0 on success, a test error code on failure. */
5542+ static wc_test_ret_t constantcompare_sweep(constantcompare_fn cmp)
5543+ {
5544+ byte a[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
5545+ byte b[9];
5546+ int i;
5547+ int length = (int)sizeof(a) - 1; /* leave a[8]/b[8] out of range */
5548+
5549+ XMEMCPY(b, a, sizeof(a));
5550+ if (cmp(a, b, length) != 0)
5551+ return WC_TEST_RET_ENC_NC;
5552+ b[0] ^= 0x01; /* length 0 must ignore even a differing first byte */
5553+ if (cmp(a, b, 0) != 0)
5554+ return WC_TEST_RET_ENC_NC;
5555+
5556+ /* A difference at any index must return exactly 1.
5557+ * Exercise both low and high bits (e.g. bit 15 on WOLFSSL_WIDE_BYTE). */
5558+ for (i = 0; i < length; i++) {
5559+ XMEMCPY(b, a, sizeof(a));
5560+ b[i] ^= 0x01;
5561+ if (cmp(a, b, length) != 1)
5562+ return WC_TEST_RET_ENC_I(i);
5563+ b[i] ^= (byte)~0;
5564+ if (cmp(a, b, length) != 1)
5565+ return WC_TEST_RET_ENC_I(i);
5566+ }
5567+
5568+ /* A difference past length must be ignored. */
5569+ XMEMCPY(b, a, sizeof(a));
5570+ b[length] ^= 0x01;
5571+ if (cmp(a, b, length) != 0)
5572+ return WC_TEST_RET_ENC_NC;
5573+
5574+ /* Negative length must return 1, not 0. */
5575+ XMEMSET(b, (byte)~a[0], sizeof(b));
5576+ if (cmp(a, b, -1) != 1)
5577+ return WC_TEST_RET_ENC_NC;
5578+
5579+ return 0;
5580+ }
5581+
5582+ /* Test ConstantCompare() and wc_ConstantCompare().
5583+ * Returns 0 on success, a test error code on failure. */
5584+ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t constantcompare_test(void)
5585+ {
5586+ wc_test_ret_t ret = 0;
5587+
5588+ WOLFSSL_ENTER("constantcompare_test");
5589+
5590+ /* Inline copy from misc.c. Under NO_INLINE it is hidden in the library
5591+ * and cannot be linked; wc_ConstantCompare() covers the same code. */
5592+ #ifndef NO_INLINE
5593+ ret = constantcompare_sweep(ConstantCompare);
5594+ if (ret != 0)
5595+ return ret;
5596+ #endif
5597+
5598+ #ifdef WC_TEST_HAVE_WC_CONSTANTCOMPARE
5599+ ret = constantcompare_sweep(wc_ConstantCompare);
5600+ #endif
5601+ return ret;
5602+ }
5603+ #endif /* WC_TEST_CONSTANTCOMPARE */
5604+
55265605#ifndef NO_CODING
55275606
55285607WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
0 commit comments