diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/sm3/sm3test.c | 66 |
1 files changed, 36 insertions, 30 deletions
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 @@ | |||
1 | /* $OpenBSD: sm3test.c,v 1.1 2018/11/11 07:12:33 tb Exp $ */ | 1 | /* $OpenBSD: sm3test.c,v 1.2 2018/11/12 15:55:59 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2018, Ribose Inc | 3 | * Copyright (c) 2018 Ribose Inc |
4 | * | 4 | * |
5 | * Permission to use, copy, modify, and/or distribute this software for any | 5 | * Permission to use, copy, modify, and/or distribute this software for any |
6 | * purpose with or without fee is hereby granted, provided that the above | 6 | * purpose with or without fee is hereby granted, provided that the above |
@@ -26,33 +26,41 @@ | |||
26 | const char *sm3_input[SM3_TESTS] = { | 26 | const char *sm3_input[SM3_TESTS] = { |
27 | "", | 27 | "", |
28 | "abc", | 28 | "abc", |
29 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" | 29 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", |
30 | }; | 30 | }; |
31 | 31 | ||
32 | const char *sm3_expected[SM3_TESTS] = { | 32 | const uint8_t sm3_expected[SM3_TESTS][32] = { |
33 | "1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b", | 33 | { |
34 | "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", | 34 | 0x1a, 0xb2, 0x1d, 0x83, 0x55, 0xcf, 0xa1, 0x7f, |
35 | "debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732" | 35 | 0x8e, 0x61, 0x19, 0x48, 0x31, 0xe8, 0x1a, 0x8f, |
36 | 0x22, 0xbe, 0xc8, 0xc7, 0x28, 0xfe, 0xfb, 0x74, | ||
37 | 0x7e, 0xd0, 0x35, 0xeb, 0x50, 0x82, 0xaa, 0x2b, | ||
38 | }, | ||
39 | { | ||
40 | 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9, | ||
41 | 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2, | ||
42 | 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2, | ||
43 | 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0, | ||
44 | }, | ||
45 | { | ||
46 | 0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1, | ||
47 | 0x38, 0x60, 0x48, 0x89, 0xc1, 0x8e, 0x5a, 0x4d, | ||
48 | 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e, 0x57, 0x65, | ||
49 | 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32, | ||
50 | }, | ||
36 | }; | 51 | }; |
37 | 52 | ||
38 | char *hex_encode(const uint8_t *, size_t); | 53 | /* Tweaked version of libssl/key_schedule/key_schedule.c. */ |
39 | 54 | static void | |
40 | char * | 55 | hexdump(const uint8_t *buf, size_t len) |
41 | hex_encode(const uint8_t *input, size_t len) | ||
42 | { | 56 | { |
43 | const char *hex = "0123456789abcdef"; | ||
44 | char *out; | ||
45 | size_t i; | 57 | size_t i; |
46 | 58 | ||
47 | if ((out = malloc(len * 2 + 1)) == NULL) | 59 | for (i = 1; i <= len; i++) |
48 | err(1, NULL); | 60 | fprintf(stderr, " 0x%02x,%s", buf[i - 1], (i % 8) ? "" : "\n"); |
49 | for (i = 0; i < len; i++) { | ||
50 | out[i * 2] = hex[input[i] >> 4]; | ||
51 | out[i * 2 + 1] = hex[input[i] & 0x0f]; | ||
52 | } | ||
53 | out[len * 2] = '\0'; | ||
54 | 61 | ||
55 | return out; | 62 | if (i % 8 != 1) |
63 | fprintf(stderr, "\n"); | ||
56 | } | 64 | } |
57 | 65 | ||
58 | int | 66 | int |
@@ -60,7 +68,6 @@ main(int argc, char *argv[]) | |||
60 | { | 68 | { |
61 | EVP_MD_CTX *ctx; | 69 | EVP_MD_CTX *ctx; |
62 | uint8_t digest[32]; | 70 | uint8_t digest[32]; |
63 | char *hexdigest; | ||
64 | int numerrors = 0, i; | 71 | int numerrors = 0, i; |
65 | 72 | ||
66 | if ((ctx = EVP_MD_CTX_new()) == NULL) | 73 | if ((ctx = EVP_MD_CTX_new()) == NULL) |
@@ -73,17 +80,16 @@ main(int argc, char *argv[]) | |||
73 | errx(1, "EVP_DigestInit() failed"); | 80 | errx(1, "EVP_DigestInit() failed"); |
74 | if (!EVP_DigestFinal(ctx, digest, NULL)) | 81 | if (!EVP_DigestFinal(ctx, digest, NULL)) |
75 | errx(1, "EVP_DigestFinal() failed"); | 82 | errx(1, "EVP_DigestFinal() failed"); |
76 | 83 | ||
77 | hexdigest = hex_encode(digest, sizeof(digest)); | 84 | if (memcmp(digest, sm3_expected[i], sizeof(digest)) != 0) { |
78 | 85 | fprintf(stderr, "TEST %d failed\n", i); | |
79 | if (strcmp(hexdigest, sm3_expected[i]) != 0) { | 86 | fprintf(stderr, "Produced:\n"); |
80 | fprintf(stderr, | 87 | hexdump(digest, sizeof(digest)); |
81 | "TEST %d failed\nProduced %s\nExpected %s\n", | 88 | fprintf(stderr, "Expected:\n"); |
82 | i, hexdigest, sm3_expected[i]); | 89 | hexdump(sm3_expected[i], sizeof(sm3_expected[i])); |
83 | numerrors++; | 90 | numerrors++; |
84 | } else | 91 | } else |
85 | fprintf(stderr, "SM3 test %d ok\n", i); | 92 | fprintf(stderr, "SM3 test %d ok\n", i); |
86 | free(hexdigest); | ||
87 | } | 93 | } |
88 | 94 | ||
89 | EVP_MD_CTX_free(ctx); | 95 | EVP_MD_CTX_free(ctx); |