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