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