1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert * Copyright 2017 Ribose Inc. All Rights Reserved.
4*e0c4386eSCy Schubert *
5*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
6*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
7*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
8*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
9*e0c4386eSCy Schubert */
10*e0c4386eSCy Schubert
11*e0c4386eSCy Schubert /*
12*e0c4386eSCy Schubert * Internal tests for the SM4 module.
13*e0c4386eSCy Schubert */
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert #include <string.h>
16*e0c4386eSCy Schubert #include <openssl/opensslconf.h>
17*e0c4386eSCy Schubert #include "testutil.h"
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubert #ifndef OPENSSL_NO_SM4
20*e0c4386eSCy Schubert # include "crypto/sm4.h"
21*e0c4386eSCy Schubert
test_sm4_ecb(void)22*e0c4386eSCy Schubert static int test_sm4_ecb(void)
23*e0c4386eSCy Schubert {
24*e0c4386eSCy Schubert static const uint8_t k[SM4_BLOCK_SIZE] = {
25*e0c4386eSCy Schubert 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
26*e0c4386eSCy Schubert 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
27*e0c4386eSCy Schubert };
28*e0c4386eSCy Schubert
29*e0c4386eSCy Schubert static const uint8_t input[SM4_BLOCK_SIZE] = {
30*e0c4386eSCy Schubert 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
31*e0c4386eSCy Schubert 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
32*e0c4386eSCy Schubert };
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubert /*
35*e0c4386eSCy Schubert * This test vector comes from Example 1 of GB/T 32907-2016,
36*e0c4386eSCy Schubert * and described in Internet Draft draft-ribose-cfrg-sm4-02.
37*e0c4386eSCy Schubert */
38*e0c4386eSCy Schubert static const uint8_t expected[SM4_BLOCK_SIZE] = {
39*e0c4386eSCy Schubert 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
40*e0c4386eSCy Schubert 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46
41*e0c4386eSCy Schubert };
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert /*
44*e0c4386eSCy Schubert * This test vector comes from Example 2 from GB/T 32907-2016,
45*e0c4386eSCy Schubert * and described in Internet Draft draft-ribose-cfrg-sm4-02.
46*e0c4386eSCy Schubert * After 1,000,000 iterations.
47*e0c4386eSCy Schubert */
48*e0c4386eSCy Schubert static const uint8_t expected_iter[SM4_BLOCK_SIZE] = {
49*e0c4386eSCy Schubert 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
50*e0c4386eSCy Schubert 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66
51*e0c4386eSCy Schubert };
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubert int i;
54*e0c4386eSCy Schubert SM4_KEY key;
55*e0c4386eSCy Schubert uint8_t block[SM4_BLOCK_SIZE];
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert ossl_sm4_set_key(k, &key);
58*e0c4386eSCy Schubert memcpy(block, input, SM4_BLOCK_SIZE);
59*e0c4386eSCy Schubert
60*e0c4386eSCy Schubert ossl_sm4_encrypt(block, block, &key);
61*e0c4386eSCy Schubert if (!TEST_mem_eq(block, SM4_BLOCK_SIZE, expected, SM4_BLOCK_SIZE))
62*e0c4386eSCy Schubert return 0;
63*e0c4386eSCy Schubert
64*e0c4386eSCy Schubert for (i = 0; i != 999999; ++i)
65*e0c4386eSCy Schubert ossl_sm4_encrypt(block, block, &key);
66*e0c4386eSCy Schubert
67*e0c4386eSCy Schubert if (!TEST_mem_eq(block, SM4_BLOCK_SIZE, expected_iter, SM4_BLOCK_SIZE))
68*e0c4386eSCy Schubert return 0;
69*e0c4386eSCy Schubert
70*e0c4386eSCy Schubert for (i = 0; i != 1000000; ++i)
71*e0c4386eSCy Schubert ossl_sm4_decrypt(block, block, &key);
72*e0c4386eSCy Schubert
73*e0c4386eSCy Schubert if (!TEST_mem_eq(block, SM4_BLOCK_SIZE, input, SM4_BLOCK_SIZE))
74*e0c4386eSCy Schubert return 0;
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert return 1;
77*e0c4386eSCy Schubert }
78*e0c4386eSCy Schubert #endif
79*e0c4386eSCy Schubert
setup_tests(void)80*e0c4386eSCy Schubert int setup_tests(void)
81*e0c4386eSCy Schubert {
82*e0c4386eSCy Schubert #ifndef OPENSSL_NO_SM4
83*e0c4386eSCy Schubert ADD_TEST(test_sm4_ecb);
84*e0c4386eSCy Schubert #endif
85*e0c4386eSCy Schubert return 1;
86*e0c4386eSCy Schubert }
87