diff options
author | tb <> | 2018-11-11 07:12:33 +0000 |
---|---|---|
committer | tb <> | 2018-11-11 07:12:33 +0000 |
commit | 6ae1a2e9ce9eed61e0f7ba2f7969912581d1ff33 (patch) | |
tree | 19fe4bf9a6e44daec0db3f93e09f9c5133d838be | |
parent | dd0e7cacb751bc5832444ddfdf7044c615dfa111 (diff) | |
download | openbsd-6ae1a2e9ce9eed61e0f7ba2f7969912581d1ff33.tar.gz openbsd-6ae1a2e9ce9eed61e0f7ba2f7969912581d1ff33.tar.bz2 openbsd-6ae1a2e9ce9eed61e0f7ba2f7969912581d1ff33.zip |
Add sm3 regress tests.
-rw-r--r-- | src/regress/lib/libcrypto/sm3/Makefile | 9 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/sm3/sm3test.c | 92 |
2 files changed, 101 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/sm3/Makefile b/src/regress/lib/libcrypto/sm3/Makefile new file mode 100644 index 0000000000..a0b5006ebc --- /dev/null +++ b/src/regress/lib/libcrypto/sm3/Makefile | |||
@@ -0,0 +1,9 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2018/11/11 07:12:33 tb Exp $ | ||
2 | |||
3 | PROG= sm3test | ||
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/sm3/sm3test.c b/src/regress/lib/libcrypto/sm3/sm3test.c new file mode 100644 index 0000000000..ec6f7b39c0 --- /dev/null +++ b/src/regress/lib/libcrypto/sm3/sm3test.c | |||
@@ -0,0 +1,92 @@ | |||
1 | /* $OpenBSD: sm3test.c,v 1.1 2018/11/11 07:12:33 tb Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018, 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/evp.h> | ||
23 | |||
24 | #define SM3_TESTS 3 | ||
25 | |||
26 | const char *sm3_input[SM3_TESTS] = { | ||
27 | "", | ||
28 | "abc", | ||
29 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" | ||
30 | }; | ||
31 | |||
32 | const char *sm3_expected[SM3_TESTS] = { | ||
33 | "1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b", | ||
34 | "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0", | ||
35 | "debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732" | ||
36 | }; | ||
37 | |||
38 | char *hex_encode(const uint8_t *, size_t); | ||
39 | |||
40 | char * | ||
41 | hex_encode(const uint8_t *input, size_t len) | ||
42 | { | ||
43 | const char *hex = "0123456789abcdef"; | ||
44 | char *out; | ||
45 | size_t i; | ||
46 | |||
47 | if ((out = malloc(len * 2 + 1)) == NULL) | ||
48 | err(1, NULL); | ||
49 | for (i = 0; i < len; i++) { | ||
50 | out[i * 2] = hex[input[i] >> 4]; | ||
51 | out[i * 2 + 1] = hex[input[i] & 0x0f]; | ||
52 | } | ||
53 | out[len * 2] = '\0'; | ||
54 | |||
55 | return out; | ||
56 | } | ||
57 | |||
58 | int | ||
59 | main(int argc, char *argv[]) | ||
60 | { | ||
61 | EVP_MD_CTX *ctx; | ||
62 | uint8_t digest[32]; | ||
63 | char *hexdigest; | ||
64 | int numerrors = 0, i; | ||
65 | |||
66 | if ((ctx = EVP_MD_CTX_new()) == NULL) | ||
67 | err(1, NULL); | ||
68 | |||
69 | for (i = 0; i != SM3_TESTS; ++i) { | ||
70 | if (!EVP_DigestInit(ctx, EVP_sm3())) | ||
71 | errx(1, "EVP_DigestInit() failed"); | ||
72 | if (!EVP_DigestUpdate(ctx, sm3_input[i], strlen(sm3_input[i]))) | ||
73 | errx(1, "EVP_DigestInit() failed"); | ||
74 | if (!EVP_DigestFinal(ctx, digest, NULL)) | ||
75 | errx(1, "EVP_DigestFinal() failed"); | ||
76 | |||
77 | hexdigest = hex_encode(digest, sizeof(digest)); | ||
78 | |||
79 | if (strcmp(hexdigest, sm3_expected[i]) != 0) { | ||
80 | fprintf(stderr, | ||
81 | "TEST %d failed\nProduced %s\nExpected %s\n", | ||
82 | i, hexdigest, sm3_expected[i]); | ||
83 | numerrors++; | ||
84 | } else | ||
85 | fprintf(stderr, "SM3 test %d ok\n", i); | ||
86 | free(hexdigest); | ||
87 | } | ||
88 | |||
89 | EVP_MD_CTX_free(ctx); | ||
90 | |||
91 | return (numerrors > 0) ? 1 : 0; | ||
92 | } | ||