xref: /openbsd-src/regress/lib/libcrypto/rmd/rmd_test.c (revision bfee0ecd4fb21f9b8b29f379ee1345f256e96833)
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
rmd_test(void)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
main(int argc,char ** argv)194 main(int argc, char **argv)
195 {
196 	int failed = 0;
197 
198 	failed |= rmd_test();
199 
200 	return failed;
201 }
202