diff options
author | tb <> | 2022-09-02 15:45:52 +0000 |
---|---|---|
committer | tb <> | 2022-09-02 15:45:52 +0000 |
commit | 683000615ee42607c643c323988c280f7edb4247 (patch) | |
tree | b4aea13bd292cb3f30d6dd67ae782d0749841848 /src | |
parent | 2718201c7f4d69ac94cd6d86e0a97e8ae6b73af2 (diff) | |
download | openbsd-683000615ee42607c643c323988c280f7edb4247.tar.gz openbsd-683000615ee42607c643c323988c280f7edb4247.tar.bz2 openbsd-683000615ee42607c643c323988c280f7edb4247.zip |
Rewrite RMD-160 tests to be table-driven.
From Joshua Sing
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/rmd/Makefile | 4 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/rmd/rmd_test.c | 201 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/rmd/rmdtest.c | 128 |
3 files changed, 203 insertions, 130 deletions
diff --git a/src/regress/lib/libcrypto/rmd/Makefile b/src/regress/lib/libcrypto/rmd/Makefile index 843b6b54c9..264cbd5fec 100644 --- a/src/regress/lib/libcrypto/rmd/Makefile +++ b/src/regress/lib/libcrypto/rmd/Makefile | |||
@@ -1,6 +1,6 @@ | |||
1 | # $OpenBSD: Makefile,v 1.3 2014/07/08 15:53:53 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.4 2022/09/02 15:45:52 tb Exp $ |
2 | 2 | ||
3 | PROG= rmdtest | 3 | PROG= rmd_test |
4 | LDADD= -lcrypto | 4 | LDADD= -lcrypto |
5 | DPADD= ${LIBCRYPTO} | 5 | DPADD= ${LIBCRYPTO} |
6 | WARNINGS= Yes | 6 | WARNINGS= Yes |
diff --git a/src/regress/lib/libcrypto/rmd/rmd_test.c b/src/regress/lib/libcrypto/rmd/rmd_test.c new file mode 100644 index 0000000000..0a88a9bbba --- /dev/null +++ b/src/regress/lib/libcrypto/rmd/rmd_test.c | |||
@@ -0,0 +1,201 @@ | |||
1 | /* $OpenBSD: rmd_test.c,v 1.1 2022/09/02 15:45:52 tb Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and 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 <openssl/evp.h> | ||
19 | #include <openssl/ripemd.h> | ||
20 | |||
21 | #include <stdint.h> | ||
22 | #include <string.h> | ||
23 | |||
24 | struct rmd_test { | ||
25 | const uint8_t in[128]; | ||
26 | const size_t in_len; | ||
27 | const uint8_t out[EVP_MAX_MD_SIZE]; | ||
28 | }; | ||
29 | |||
30 | static const struct rmd_test rmd_tests[] = { | ||
31 | /* | ||
32 | * RIPEMD-160 - Test vectors from | ||
33 | * https://homes.esat.kuleuven.be/~bosselae/ripemd160.html | ||
34 | */ | ||
35 | { | ||
36 | .in = "", | ||
37 | .in_len = 0, | ||
38 | .out = { | ||
39 | 0x9c, 0x11, 0x85, 0xa5, 0xc5, 0xe9, 0xfc, 0x54, | ||
40 | 0x61, 0x28, 0x08, 0x97, 0x7e, 0xe8, 0xf5, 0x48, | ||
41 | 0xb2, 0x25, 0x8d, 0x31, | ||
42 | }, | ||
43 | }, | ||
44 | { | ||
45 | .in = "a", | ||
46 | .in_len = 1, | ||
47 | .out = { | ||
48 | 0x0b, 0xdc, 0x9d, 0x2d, 0x25, 0x6b, 0x3e, 0xe9, | ||
49 | 0xda, 0xae, 0x34, 0x7b, 0xe6, 0xf4, 0xdc, 0x83, | ||
50 | 0x5a, 0x46, 0x7f, 0xfe, | ||
51 | }, | ||
52 | }, | ||
53 | { | ||
54 | .in = "abc", | ||
55 | .in_len = 3, | ||
56 | .out = { | ||
57 | 0x8e, 0xb2, 0x08, 0xf7, 0xe0, 0x5d, 0x98, 0x7a, | ||
58 | 0x9b, 0x04, 0x4a, 0x8e, 0x98, 0xc6, 0xb0, 0x87, | ||
59 | 0xf1, 0x5a, 0x0b, 0xfc, | ||
60 | }, | ||
61 | }, | ||
62 | { | ||
63 | .in = "message digest", | ||
64 | .in_len = 14, | ||
65 | .out = { | ||
66 | 0x5d, 0x06, 0x89, 0xef, 0x49, 0xd2, 0xfa, 0xe5, | ||
67 | 0x72, 0xb8, 0x81, 0xb1, 0x23, 0xa8, 0x5f, 0xfa, | ||
68 | 0x21, 0x59, 0x5f, 0x36, | ||
69 | }, | ||
70 | }, | ||
71 | { | ||
72 | .in = "abcdefghijklmnopqrstuvwxyz", | ||
73 | .in_len = 26, | ||
74 | .out = { | ||
75 | 0xf7, 0x1c, 0x27, 0x10, 0x9c, 0x69, 0x2c, 0x1b, | ||
76 | 0x56, 0xbb, 0xdc, 0xeb, 0x5b, 0x9d, 0x28, 0x65, | ||
77 | 0xb3, 0x70, 0x8d, 0xbc, | ||
78 | }, | ||
79 | }, | ||
80 | { | ||
81 | .in = | ||
82 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | ||
83 | .in_len = 56, | ||
84 | .out = { | ||
85 | 0x12, 0xa0, 0x53, 0x38, 0x4a, 0x9c, 0x0c, 0x88, | ||
86 | 0xe4, 0x05, 0xa0, 0x6c, 0x27, 0xdc, 0xf4, 0x9a, | ||
87 | 0xda, 0x62, 0xeb, 0x2b, | ||
88 | }, | ||
89 | }, | ||
90 | { | ||
91 | .in = | ||
92 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv" | ||
93 | "wxyz0123456789", | ||
94 | .in_len = 62, | ||
95 | .out = { | ||
96 | 0xb0, 0xe2, 0x0b, 0x6e, 0x31, 0x16, 0x64, 0x02, | ||
97 | 0x86, 0xed, 0x3a, 0x87, 0xa5, 0x71, 0x30, 0x79, | ||
98 | 0xb2, 0x1f, 0x51, 0x89, | ||
99 | }, | ||
100 | }, | ||
101 | { | ||
102 | .in = | ||
103 | "123456789012345678901234567890123456789012345678" | ||
104 | "90123456789012345678901234567890", | ||
105 | .in_len = 80, | ||
106 | .out = { | ||
107 | 0x9b, 0x75, 0x2e, 0x45, 0x57, 0x3d, 0x4b, 0x39, | ||
108 | 0xf4, 0xdb, 0xd3, 0x32, 0x3c, 0xab, 0x82, 0xbf, | ||
109 | 0x63, 0x32, 0x6b, 0xfb, | ||
110 | }, | ||
111 | }, | ||
112 | }; | ||
113 | |||
114 | #define N_RMD_TESTS (sizeof(rmd_tests) / sizeof(rmd_tests[0])) | ||
115 | |||
116 | static int | ||
117 | rmd_test(void) | ||
118 | { | ||
119 | const struct rmd_test *rt; | ||
120 | EVP_MD_CTX *hash = NULL; | ||
121 | uint8_t out[EVP_MAX_MD_SIZE]; | ||
122 | size_t in_len; | ||
123 | size_t i; | ||
124 | int failed = 1; | ||
125 | |||
126 | if ((hash = EVP_MD_CTX_new()) == NULL) { | ||
127 | fprintf(stderr, "FAIL: EVP_MD_CTX_new() failed\n"); | ||
128 | goto failed; | ||
129 | } | ||
130 | |||
131 | for (i = 0; i < N_RMD_TESTS; i++) { | ||
132 | rt = &rmd_tests[i]; | ||
133 | |||
134 | /* Digest */ | ||
135 | memset(out, 0, sizeof(out)); | ||
136 | RIPEMD160(rt->in, rt->in_len, out); | ||
137 | if (memcmp(rt->out, out, RIPEMD160_DIGEST_LENGTH) != 0) { | ||
138 | fprintf(stderr, "FAIL: mismatch\n"); | ||
139 | goto failed; | ||
140 | } | ||
141 | |||
142 | /* EVP single-shot digest */ | ||
143 | memset(out, 0, sizeof(out)); | ||
144 | if (!EVP_Digest(rt->in, rt->in_len, out, NULL, EVP_ripemd160(), NULL)) { | ||
145 | fprintf(stderr, "FAIL: EVP_Digest failed\n"); | ||
146 | goto failed; | ||
147 | } | ||
148 | |||
149 | if (memcmp(rt->out, out, RIPEMD160_DIGEST_LENGTH) != 0) { | ||
150 | fprintf(stderr, "FAIL: EVP single-shot mismatch\n"); | ||
151 | goto failed; | ||
152 | } | ||
153 | |||
154 | /* EVP digest */ | ||
155 | memset(out, 0, sizeof(out)); | ||
156 | if (!EVP_DigestInit_ex(hash, EVP_ripemd160(), NULL)) { | ||
157 | fprintf(stderr, "FAIL: EVP_DigestInit_ex failed\n"); | ||
158 | goto failed; | ||
159 | } | ||
160 | |||
161 | in_len = rt->in_len / 2; | ||
162 | if (!EVP_DigestUpdate(hash, rt->in, in_len)) { | ||
163 | fprintf(stderr, | ||
164 | "FAIL: EVP_DigestUpdate first half failed\n"); | ||
165 | goto failed; | ||
166 | } | ||
167 | |||
168 | if (!EVP_DigestUpdate(hash, rt->in + in_len, | ||
169 | rt->in_len - in_len)) { | ||
170 | fprintf(stderr, | ||
171 | "FAIL: EVP_DigestUpdate second half failed\n"); | ||
172 | goto failed; | ||
173 | } | ||
174 | |||
175 | if (!EVP_DigestFinal_ex(hash, out, NULL)) { | ||
176 | fprintf(stderr, "FAIL: EVP_DigestFinal_ex failed\n"); | ||
177 | goto failed; | ||
178 | } | ||
179 | |||
180 | if (memcmp(rt->out, out, RIPEMD160_DIGEST_LENGTH) != 0) { | ||
181 | fprintf(stderr, "FAIL: EVP mismatch\n"); | ||
182 | goto failed; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | failed = 0; | ||
187 | |||
188 | failed: | ||
189 | EVP_MD_CTX_free(hash); | ||
190 | return failed; | ||
191 | } | ||
192 | |||
193 | int | ||
194 | main(int argc, char **argv) | ||
195 | { | ||
196 | int failed = 0; | ||
197 | |||
198 | failed |= rmd_test(); | ||
199 | |||
200 | return failed; | ||
201 | } | ||
diff --git a/src/regress/lib/libcrypto/rmd/rmdtest.c b/src/regress/lib/libcrypto/rmd/rmdtest.c deleted file mode 100644 index aa6cb0174e..0000000000 --- a/src/regress/lib/libcrypto/rmd/rmdtest.c +++ /dev/null | |||
@@ -1,128 +0,0 @@ | |||
1 | /* $OpenBSD: rmdtest.c,v 1.2 2018/07/17 17:06:49 tb Exp $ */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | |||
59 | #include <stdio.h> | ||
60 | #include <string.h> | ||
61 | #include <stdlib.h> | ||
62 | |||
63 | #include <openssl/ripemd.h> | ||
64 | #include <openssl/evp.h> | ||
65 | |||
66 | static char *test[]={ | ||
67 | "", | ||
68 | "a", | ||
69 | "abc", | ||
70 | "message digest", | ||
71 | "abcdefghijklmnopqrstuvwxyz", | ||
72 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | ||
73 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | ||
74 | "12345678901234567890123456789012345678901234567890123456789012345678901234567890", | ||
75 | NULL, | ||
76 | }; | ||
77 | |||
78 | static char *ret[]={ | ||
79 | "9c1185a5c5e9fc54612808977ee8f548b2258d31", | ||
80 | "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe", | ||
81 | "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", | ||
82 | "5d0689ef49d2fae572b881b123a85ffa21595f36", | ||
83 | "f71c27109c692c1b56bbdceb5b9d2865b3708dbc", | ||
84 | "12a053384a9c0c88e405a06c27dcf49ada62eb2b", | ||
85 | "b0e20b6e3116640286ed3a87a5713079b21f5189", | ||
86 | "9b752e45573d4b39f4dbd3323cab82bf63326bfb", | ||
87 | }; | ||
88 | |||
89 | static char *pt(unsigned char *md); | ||
90 | int main(int argc, char *argv[]) | ||
91 | { | ||
92 | int i,err=0; | ||
93 | char **P,**R; | ||
94 | char *p; | ||
95 | unsigned char md[RIPEMD160_DIGEST_LENGTH]; | ||
96 | |||
97 | P=test; | ||
98 | R=ret; | ||
99 | i=1; | ||
100 | while (*P != NULL) | ||
101 | { | ||
102 | EVP_Digest(&(P[0][0]),strlen((char *)*P),md,NULL,EVP_ripemd160(), NULL); | ||
103 | p=pt(md); | ||
104 | if (strcmp(p,(char *)*R) != 0) | ||
105 | { | ||
106 | printf("error calculating RIPEMD160 on '%s'\n",*P); | ||
107 | printf("got %s instead of %s\n",p,*R); | ||
108 | err++; | ||
109 | } | ||
110 | else | ||
111 | printf("test %d ok\n",i); | ||
112 | i++; | ||
113 | R++; | ||
114 | P++; | ||
115 | } | ||
116 | exit(err); | ||
117 | return(0); | ||
118 | } | ||
119 | |||
120 | static char *pt(unsigned char *md) | ||
121 | { | ||
122 | int i; | ||
123 | static char buf[80]; | ||
124 | |||
125 | for (i=0; i<RIPEMD160_DIGEST_LENGTH; i++) | ||
126 | snprintf(buf + i*2, sizeof(buf) - i*2, "%02x",md[i]); | ||
127 | return(buf); | ||
128 | } | ||