1*72a866e5Stb /* $OpenBSD: sm4test.c,v 1.1 2019/03/17 17:48:31 tb Exp $ */
2*72a866e5Stb /*
3*72a866e5Stb * Copyright (c) 2017, 2019 Ribose Inc
4*72a866e5Stb *
5*72a866e5Stb * Permission to use, copy, modify, and/or distribute this software for any
6*72a866e5Stb * purpose with or without fee is hereby granted, provided that the above
7*72a866e5Stb * copyright notice and this permission notice appear in all copies.
8*72a866e5Stb *
9*72a866e5Stb * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*72a866e5Stb * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*72a866e5Stb * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*72a866e5Stb * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*72a866e5Stb * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*72a866e5Stb * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*72a866e5Stb * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*72a866e5Stb */
17*72a866e5Stb
18*72a866e5Stb #include <err.h>
19*72a866e5Stb #include <stdio.h>
20*72a866e5Stb #include <string.h>
21*72a866e5Stb
22*72a866e5Stb #include <openssl/sm4.h>
23*72a866e5Stb
24*72a866e5Stb static void
hexdump(FILE * fp,const char * title,const uint8_t * buf,size_t len)25*72a866e5Stb hexdump(FILE *fp, const char *title, const uint8_t *buf, size_t len)
26*72a866e5Stb {
27*72a866e5Stb size_t i;
28*72a866e5Stb
29*72a866e5Stb fprintf(fp, "%s:\n", title);
30*72a866e5Stb for (i = 1; i <= len; i++)
31*72a866e5Stb fprintf(fp, " 0x%02x,%s", buf[i - 1], (i % 8) ? "" : "\n");
32*72a866e5Stb
33*72a866e5Stb if (i % 8 != 1)
34*72a866e5Stb fprintf(fp, "\n");
35*72a866e5Stb }
36*72a866e5Stb
37*72a866e5Stb int
main(int argc,char * argv[])38*72a866e5Stb main(int argc, char *argv[])
39*72a866e5Stb {
40*72a866e5Stb int i;
41*72a866e5Stb SM4_KEY key;
42*72a866e5Stb uint8_t block[SM4_BLOCK_SIZE];
43*72a866e5Stb
44*72a866e5Stb static const uint8_t k[SM4_BLOCK_SIZE] = {
45*72a866e5Stb 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
46*72a866e5Stb 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
47*72a866e5Stb };
48*72a866e5Stb
49*72a866e5Stb static const uint8_t input[SM4_BLOCK_SIZE] = {
50*72a866e5Stb 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
51*72a866e5Stb 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
52*72a866e5Stb };
53*72a866e5Stb
54*72a866e5Stb /*
55*72a866e5Stb * This test vector comes from Example 1 of GB/T 32907-2016,
56*72a866e5Stb * and described in Internet Draft draft-ribose-cfrg-sm4-02.
57*72a866e5Stb */
58*72a866e5Stb static const uint8_t expected[SM4_BLOCK_SIZE] = {
59*72a866e5Stb 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
60*72a866e5Stb 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46
61*72a866e5Stb };
62*72a866e5Stb
63*72a866e5Stb /*
64*72a866e5Stb * This test vector comes from Example 2 from GB/T 32907-2016,
65*72a866e5Stb * and described in Internet Draft draft-ribose-cfrg-sm4-02.
66*72a866e5Stb * After 1,000,000 iterations.
67*72a866e5Stb */
68*72a866e5Stb static const uint8_t expected_iter[SM4_BLOCK_SIZE] = {
69*72a866e5Stb 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
70*72a866e5Stb 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66
71*72a866e5Stb };
72*72a866e5Stb
73*72a866e5Stb if (!SM4_set_key(k, &key))
74*72a866e5Stb errx(1, "SM4_set_key() failed");
75*72a866e5Stb
76*72a866e5Stb memcpy(block, input, SM4_BLOCK_SIZE);
77*72a866e5Stb
78*72a866e5Stb SM4_encrypt(block, block, &key);
79*72a866e5Stb
80*72a866e5Stb if (memcmp(block, expected, SM4_BLOCK_SIZE) != 0) {
81*72a866e5Stb fprintf(stderr, "FAIL: Encryption failed\n");
82*72a866e5Stb hexdump(stderr, "Got", block, SM4_BLOCK_SIZE);
83*72a866e5Stb hexdump(stderr, "Expected", expected, SM4_BLOCK_SIZE);
84*72a866e5Stb return 1;
85*72a866e5Stb }
86*72a866e5Stb
87*72a866e5Stb for (i = 0; i < 999999; i++)
88*72a866e5Stb SM4_encrypt(block, block, &key);
89*72a866e5Stb
90*72a866e5Stb if (memcmp(block, expected_iter, SM4_BLOCK_SIZE) != 0) {
91*72a866e5Stb fprintf(stderr, "FAIL: Multi-iteration encryption failed\n");
92*72a866e5Stb hexdump(stderr, "Got", block, SM4_BLOCK_SIZE);
93*72a866e5Stb hexdump(stderr, "Expected", expected_iter, SM4_BLOCK_SIZE);
94*72a866e5Stb return 1;
95*72a866e5Stb }
96*72a866e5Stb
97*72a866e5Stb for (i = 0; i < 1000000; i++)
98*72a866e5Stb SM4_decrypt(block, block, &key);
99*72a866e5Stb
100*72a866e5Stb if (memcmp(block, input, SM4_BLOCK_SIZE) != 0) {
101*72a866e5Stb fprintf(stderr, "FAIL: Decrypted data does not match input\n");
102*72a866e5Stb hexdump(stderr, "Got", block, SM4_BLOCK_SIZE);
103*72a866e5Stb hexdump(stderr, "Expected", input, SM4_BLOCK_SIZE);
104*72a866e5Stb return 1;
105*72a866e5Stb }
106*72a866e5Stb
107*72a866e5Stb return 0;
108*72a866e5Stb }
109