From 61d35c9252c016877c63cf994f19f3e14e0e77fc Mon Sep 17 00:00:00 2001 From: tb <> Date: Mon, 12 Nov 2018 15:55:59 +0000 Subject: Rework the sm3 regress based on a suggestion by jsing. Zap the weird hex_encode() function and use byte arrays instead of strings to store the expected values. Snatch and tweak hexdump() from beck's key_schedule test to pretty-print data in case of failure. --- src/regress/lib/libcrypto/sm3/sm3test.c | 66 ++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 30 deletions(-) (limited to 'src/regress/lib/libcrypto/sm3/sm3test.c') diff --git a/src/regress/lib/libcrypto/sm3/sm3test.c b/src/regress/lib/libcrypto/sm3/sm3test.c index ec6f7b39c0..6611372848 100644 --- a/src/regress/lib/libcrypto/sm3/sm3test.c +++ b/src/regress/lib/libcrypto/sm3/sm3test.c @@ -1,6 +1,6 @@ -/* $OpenBSD: sm3test.c,v 1.1 2018/11/11 07:12:33 tb Exp $ */ +/* $OpenBSD: sm3test.c,v 1.2 2018/11/12 15:55:59 tb Exp $ */ /* - * Copyright (c) 2018, Ribose Inc + * Copyright (c) 2018 Ribose Inc * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -26,33 +26,41 @@ const char *sm3_input[SM3_TESTS] = { "", "abc", - "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", }; -const char *sm3_expected[SM3_TESTS] = { - "1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b", - "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", - "debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732" +const uint8_t sm3_expected[SM3_TESTS][32] = { + { + 0x1a, 0xb2, 0x1d, 0x83, 0x55, 0xcf, 0xa1, 0x7f, + 0x8e, 0x61, 0x19, 0x48, 0x31, 0xe8, 0x1a, 0x8f, + 0x22, 0xbe, 0xc8, 0xc7, 0x28, 0xfe, 0xfb, 0x74, + 0x7e, 0xd0, 0x35, 0xeb, 0x50, 0x82, 0xaa, 0x2b, + }, + { + 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9, + 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2, + 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2, + 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0, + }, + { + 0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1, + 0x38, 0x60, 0x48, 0x89, 0xc1, 0x8e, 0x5a, 0x4d, + 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e, 0x57, 0x65, + 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32, + }, }; -char *hex_encode(const uint8_t *, size_t); - -char * -hex_encode(const uint8_t *input, size_t len) +/* Tweaked version of libssl/key_schedule/key_schedule.c. */ +static void +hexdump(const uint8_t *buf, size_t len) { - const char *hex = "0123456789abcdef"; - char *out; size_t i; - if ((out = malloc(len * 2 + 1)) == NULL) - err(1, NULL); - for (i = 0; i < len; i++) { - out[i * 2] = hex[input[i] >> 4]; - out[i * 2 + 1] = hex[input[i] & 0x0f]; - } - out[len * 2] = '\0'; + for (i = 1; i <= len; i++) + fprintf(stderr, " 0x%02x,%s", buf[i - 1], (i % 8) ? "" : "\n"); - return out; + if (i % 8 != 1) + fprintf(stderr, "\n"); } int @@ -60,7 +68,6 @@ main(int argc, char *argv[]) { EVP_MD_CTX *ctx; uint8_t digest[32]; - char *hexdigest; int numerrors = 0, i; if ((ctx = EVP_MD_CTX_new()) == NULL) @@ -73,17 +80,16 @@ main(int argc, char *argv[]) errx(1, "EVP_DigestInit() failed"); if (!EVP_DigestFinal(ctx, digest, NULL)) errx(1, "EVP_DigestFinal() failed"); - - hexdigest = hex_encode(digest, sizeof(digest)); - - if (strcmp(hexdigest, sm3_expected[i]) != 0) { - fprintf(stderr, - "TEST %d failed\nProduced %s\nExpected %s\n", - i, hexdigest, sm3_expected[i]); + + if (memcmp(digest, sm3_expected[i], sizeof(digest)) != 0) { + fprintf(stderr, "TEST %d failed\n", i); + fprintf(stderr, "Produced:\n"); + hexdump(digest, sizeof(digest)); + fprintf(stderr, "Expected:\n"); + hexdump(sm3_expected[i], sizeof(sm3_expected[i])); numerrors++; } else fprintf(stderr, "SM3 test %d ok\n", i); - free(hexdigest); } EVP_MD_CTX_free(ctx); -- cgit v1.2.3-55-g6feb