1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci * Copyright 2017 Ribose Inc. 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 SM4 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_SM4
20e1051a39Sopenharmony_ci# include "crypto/sm4.h"
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_cistatic int test_sm4_ecb(void)
23e1051a39Sopenharmony_ci{
24e1051a39Sopenharmony_ci    static const uint8_t k[SM4_BLOCK_SIZE] = {
25e1051a39Sopenharmony_ci        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
26e1051a39Sopenharmony_ci        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
27e1051a39Sopenharmony_ci    };
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci    static const uint8_t input[SM4_BLOCK_SIZE] = {
30e1051a39Sopenharmony_ci        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
31e1051a39Sopenharmony_ci        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
32e1051a39Sopenharmony_ci    };
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci    /*
35e1051a39Sopenharmony_ci     * This test vector comes from Example 1 of GB/T 32907-2016,
36e1051a39Sopenharmony_ci     * and described in Internet Draft draft-ribose-cfrg-sm4-02.
37e1051a39Sopenharmony_ci     */
38e1051a39Sopenharmony_ci    static const uint8_t expected[SM4_BLOCK_SIZE] = {
39e1051a39Sopenharmony_ci        0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
40e1051a39Sopenharmony_ci        0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46
41e1051a39Sopenharmony_ci    };
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci    /*
44e1051a39Sopenharmony_ci     * This test vector comes from Example 2 from GB/T 32907-2016,
45e1051a39Sopenharmony_ci     * and described in Internet Draft draft-ribose-cfrg-sm4-02.
46e1051a39Sopenharmony_ci     * After 1,000,000 iterations.
47e1051a39Sopenharmony_ci     */
48e1051a39Sopenharmony_ci    static const uint8_t expected_iter[SM4_BLOCK_SIZE] = {
49e1051a39Sopenharmony_ci        0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
50e1051a39Sopenharmony_ci        0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66
51e1051a39Sopenharmony_ci    };
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_ci    int i;
54e1051a39Sopenharmony_ci    SM4_KEY key;
55e1051a39Sopenharmony_ci    uint8_t block[SM4_BLOCK_SIZE];
56e1051a39Sopenharmony_ci
57e1051a39Sopenharmony_ci    ossl_sm4_set_key(k, &key);
58e1051a39Sopenharmony_ci    memcpy(block, input, SM4_BLOCK_SIZE);
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci    ossl_sm4_encrypt(block, block, &key);
61e1051a39Sopenharmony_ci    if (!TEST_mem_eq(block, SM4_BLOCK_SIZE, expected, SM4_BLOCK_SIZE))
62e1051a39Sopenharmony_ci        return 0;
63e1051a39Sopenharmony_ci
64e1051a39Sopenharmony_ci    for (i = 0; i != 999999; ++i)
65e1051a39Sopenharmony_ci        ossl_sm4_encrypt(block, block, &key);
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci    if (!TEST_mem_eq(block, SM4_BLOCK_SIZE, expected_iter, SM4_BLOCK_SIZE))
68e1051a39Sopenharmony_ci        return 0;
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ci    for (i = 0; i != 1000000; ++i)
71e1051a39Sopenharmony_ci        ossl_sm4_decrypt(block, block, &key);
72e1051a39Sopenharmony_ci
73e1051a39Sopenharmony_ci    if (!TEST_mem_eq(block, SM4_BLOCK_SIZE, input, SM4_BLOCK_SIZE))
74e1051a39Sopenharmony_ci        return 0;
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ci    return 1;
77e1051a39Sopenharmony_ci}
78e1051a39Sopenharmony_ci#endif
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ciint setup_tests(void)
81e1051a39Sopenharmony_ci{
82e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SM4
83e1051a39Sopenharmony_ci    ADD_TEST(test_sm4_ecb);
84e1051a39Sopenharmony_ci#endif
85e1051a39Sopenharmony_ci    return 1;
86e1051a39Sopenharmony_ci}
87