1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos * Copyright 2021 UnionTech. All Rights Reserved.
4*b0d17251Schristos *
5*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
6*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
7*b0d17251Schristos * in the file LICENSE in the source distribution or at
8*b0d17251Schristos * https://www.openssl.org/source/license.html
9*b0d17251Schristos */
10*b0d17251Schristos
11*b0d17251Schristos /*
12*b0d17251Schristos * Internal tests for the SM3 module.
13*b0d17251Schristos */
14*b0d17251Schristos
15*b0d17251Schristos #include <string.h>
16*b0d17251Schristos #include <openssl/opensslconf.h>
17*b0d17251Schristos #include "testutil.h"
18*b0d17251Schristos
19*b0d17251Schristos #ifndef OPENSSL_NO_SM3
20*b0d17251Schristos # include "internal/sm3.h"
21*b0d17251Schristos
test_sm3(void)22*b0d17251Schristos static int test_sm3(void)
23*b0d17251Schristos {
24*b0d17251Schristos static const unsigned char input1[] = {
25*b0d17251Schristos 0x61, 0x62, 0x63
26*b0d17251Schristos };
27*b0d17251Schristos
28*b0d17251Schristos /*
29*b0d17251Schristos * This test vector comes from Example 1 (A.1) of GM/T 0004-2012
30*b0d17251Schristos */
31*b0d17251Schristos static const unsigned char expected1[SM3_DIGEST_LENGTH] = {
32*b0d17251Schristos 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9,
33*b0d17251Schristos 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2,
34*b0d17251Schristos 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2,
35*b0d17251Schristos 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0
36*b0d17251Schristos };
37*b0d17251Schristos
38*b0d17251Schristos static const unsigned char input2[] = {
39*b0d17251Schristos 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
40*b0d17251Schristos 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
41*b0d17251Schristos 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
42*b0d17251Schristos 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
43*b0d17251Schristos 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
44*b0d17251Schristos 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
45*b0d17251Schristos 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
46*b0d17251Schristos 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64
47*b0d17251Schristos };
48*b0d17251Schristos
49*b0d17251Schristos /*
50*b0d17251Schristos * This test vector comes from Example 2 (A.2) from GM/T 0004-2012
51*b0d17251Schristos */
52*b0d17251Schristos static const unsigned char expected2[SM3_DIGEST_LENGTH] = {
53*b0d17251Schristos 0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1,
54*b0d17251Schristos 0x38, 0x60, 0x48, 0x89, 0xc1, 0x8e, 0x5a, 0x4d,
55*b0d17251Schristos 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e, 0x57, 0x65,
56*b0d17251Schristos 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32
57*b0d17251Schristos };
58*b0d17251Schristos
59*b0d17251Schristos SM3_CTX ctx1, ctx2;
60*b0d17251Schristos unsigned char md1[SM3_DIGEST_LENGTH], md2[SM3_DIGEST_LENGTH];
61*b0d17251Schristos
62*b0d17251Schristos if (!TEST_true(ossl_sm3_init(&ctx1))
63*b0d17251Schristos || !TEST_true(ossl_sm3_update(&ctx1, input1, sizeof(input1)))
64*b0d17251Schristos || !TEST_true(ossl_sm3_final(md1, &ctx1))
65*b0d17251Schristos || !TEST_mem_eq(md1, SM3_DIGEST_LENGTH, expected1, SM3_DIGEST_LENGTH))
66*b0d17251Schristos return 0;
67*b0d17251Schristos
68*b0d17251Schristos if (!TEST_true(ossl_sm3_init(&ctx2))
69*b0d17251Schristos || !TEST_true(ossl_sm3_update(&ctx2, input2, sizeof(input2)))
70*b0d17251Schristos || !TEST_true(ossl_sm3_final(md2, &ctx2))
71*b0d17251Schristos || !TEST_mem_eq(md2, SM3_DIGEST_LENGTH, expected2, SM3_DIGEST_LENGTH))
72*b0d17251Schristos return 0;
73*b0d17251Schristos
74*b0d17251Schristos return 1;
75*b0d17251Schristos }
76*b0d17251Schristos #endif
77*b0d17251Schristos
setup_tests(void)78*b0d17251Schristos int setup_tests(void)
79*b0d17251Schristos {
80*b0d17251Schristos #ifndef OPENSSL_NO_SM3
81*b0d17251Schristos ADD_TEST(test_sm3);
82*b0d17251Schristos #endif
83*b0d17251Schristos return 1;
84*b0d17251Schristos }
85