1*3011d9aeStb /* $OpenBSD: sm3test.c,v 1.3 2021/04/06 15:00:19 tb Exp $ */
238c0c5f6Stb /*
3b2a13badStb * Copyright (c) 2018 Ribose Inc
438c0c5f6Stb *
538c0c5f6Stb * Permission to use, copy, modify, and/or distribute this software for any
638c0c5f6Stb * purpose with or without fee is hereby granted, provided that the above
738c0c5f6Stb * copyright notice and this permission notice appear in all copies.
838c0c5f6Stb *
938c0c5f6Stb * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1038c0c5f6Stb * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1138c0c5f6Stb * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1238c0c5f6Stb * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1338c0c5f6Stb * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1438c0c5f6Stb * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1538c0c5f6Stb * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1638c0c5f6Stb */
1738c0c5f6Stb
1838c0c5f6Stb #include <err.h>
1938c0c5f6Stb #include <stdio.h>
2038c0c5f6Stb #include <string.h>
2138c0c5f6Stb
2238c0c5f6Stb #include <openssl/evp.h>
2338c0c5f6Stb
2438c0c5f6Stb #define SM3_TESTS 3
2538c0c5f6Stb
2638c0c5f6Stb const char *sm3_input[SM3_TESTS] = {
2738c0c5f6Stb "",
2838c0c5f6Stb "abc",
29b2a13badStb "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd",
3038c0c5f6Stb };
3138c0c5f6Stb
32b2a13badStb const uint8_t sm3_expected[SM3_TESTS][32] = {
3338c0c5f6Stb {
34b2a13badStb 0x1a, 0xb2, 0x1d, 0x83, 0x55, 0xcf, 0xa1, 0x7f,
35b2a13badStb 0x8e, 0x61, 0x19, 0x48, 0x31, 0xe8, 0x1a, 0x8f,
36b2a13badStb 0x22, 0xbe, 0xc8, 0xc7, 0x28, 0xfe, 0xfb, 0x74,
37b2a13badStb 0x7e, 0xd0, 0x35, 0xeb, 0x50, 0x82, 0xaa, 0x2b,
38b2a13badStb },
39b2a13badStb {
40b2a13badStb 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9,
41b2a13badStb 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2,
42b2a13badStb 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2,
43b2a13badStb 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0,
44b2a13badStb },
45b2a13badStb {
46b2a13badStb 0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1,
47b2a13badStb 0x38, 0x60, 0x48, 0x89, 0xc1, 0x8e, 0x5a, 0x4d,
48b2a13badStb 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e, 0x57, 0x65,
49b2a13badStb 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32,
50b2a13badStb },
51b2a13badStb };
52b2a13badStb
53b2a13badStb /* Tweaked version of libssl/key_schedule/key_schedule.c. */
54b2a13badStb static void
hexdump(const uint8_t * buf,size_t len)55b2a13badStb hexdump(const uint8_t *buf, size_t len)
56b2a13badStb {
5738c0c5f6Stb size_t i;
5838c0c5f6Stb
59b2a13badStb for (i = 1; i <= len; i++)
60b2a13badStb fprintf(stderr, " 0x%02x,%s", buf[i - 1], (i % 8) ? "" : "\n");
6138c0c5f6Stb
62b2a13badStb if (i % 8 != 1)
63b2a13badStb fprintf(stderr, "\n");
6438c0c5f6Stb }
6538c0c5f6Stb
6638c0c5f6Stb int
main(int argc,char * argv[])6738c0c5f6Stb main(int argc, char *argv[])
6838c0c5f6Stb {
6938c0c5f6Stb EVP_MD_CTX *ctx;
7038c0c5f6Stb uint8_t digest[32];
71*3011d9aeStb int i;
72*3011d9aeStb int numerrors = 0;
7338c0c5f6Stb
7438c0c5f6Stb if ((ctx = EVP_MD_CTX_new()) == NULL)
7538c0c5f6Stb err(1, NULL);
7638c0c5f6Stb
77*3011d9aeStb for (i = 0; i < SM3_TESTS; i++) {
7838c0c5f6Stb if (!EVP_DigestInit(ctx, EVP_sm3()))
7938c0c5f6Stb errx(1, "EVP_DigestInit() failed");
8038c0c5f6Stb if (!EVP_DigestUpdate(ctx, sm3_input[i], strlen(sm3_input[i])))
8138c0c5f6Stb errx(1, "EVP_DigestInit() failed");
8238c0c5f6Stb if (!EVP_DigestFinal(ctx, digest, NULL))
8338c0c5f6Stb errx(1, "EVP_DigestFinal() failed");
8438c0c5f6Stb
85b2a13badStb if (memcmp(digest, sm3_expected[i], sizeof(digest)) != 0) {
86b2a13badStb fprintf(stderr, "TEST %d failed\n", i);
87b2a13badStb fprintf(stderr, "Produced:\n");
88b2a13badStb hexdump(digest, sizeof(digest));
89b2a13badStb fprintf(stderr, "Expected:\n");
90b2a13badStb hexdump(sm3_expected[i], sizeof(sm3_expected[i]));
9138c0c5f6Stb numerrors++;
9238c0c5f6Stb } else
9338c0c5f6Stb fprintf(stderr, "SM3 test %d ok\n", i);
9438c0c5f6Stb }
9538c0c5f6Stb
9638c0c5f6Stb EVP_MD_CTX_free(ctx);
9738c0c5f6Stb
9838c0c5f6Stb return (numerrors > 0) ? 1 : 0;
9938c0c5f6Stb }
100