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