xref: /netbsd-src/external/bsd/ntp/dist/tests/libntp/digests.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: digests.c,v 1.2 2024/08/18 20:47:26 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos #include "config.h"
4897be3a4Schristos 
5897be3a4Schristos #include <fcntl.h>
6897be3a4Schristos #include <sys/types.h>
7897be3a4Schristos #include <sys/stat.h>
8897be3a4Schristos #include <unistd.h>
9897be3a4Schristos 
10897be3a4Schristos #include "unity.h"
11897be3a4Schristos #include "ntp.h"
12897be3a4Schristos #include "ntp_stdlib.h"
13897be3a4Schristos 
14897be3a4Schristos /*
15897be3a4Schristos  * tests/libntp/data/ntp.keys has two keys for each algorithm, 50 keyids apart.
16897be3a4Schristos  * The first is 20 random ASCII chars, the 2nd 40 random hex values.
17897be3a4Schristos  */
18897be3a4Schristos #define HEX_KEYID_OFFSET	50
19897be3a4Schristos 
20897be3a4Schristos /* in generated srcdir.c */
21897be3a4Schristos extern const char srcdir[];
22897be3a4Schristos 
23897be3a4Schristos /* needed by authtrust() */
24897be3a4Schristos u_long			current_time;
25897be3a4Schristos 
26897be3a4Schristos static bool		setup;
27897be3a4Schristos static u_int32 *	pkt;
28897be3a4Schristos static size_t		pkt_sz;
29897be3a4Schristos static u_char *		mac;
30897be3a4Schristos 
31897be3a4Schristos /* helper routine */
32897be3a4Schristos void dump_mac(keyid_t keyid, u_char *pmac, size_t octets);
33897be3a4Schristos 
34897be3a4Schristos 
35897be3a4Schristos /* unity calls setUp before each test routine */
36897be3a4Schristos void setUp(void);
37897be3a4Schristos void
38897be3a4Schristos setUp(void)
39897be3a4Schristos {
40897be3a4Schristos 	static bool	done_once;
41897be3a4Schristos 	const char	msg_rel_fname[] =	"data/mills,david-03.jpg";
42897be3a4Schristos 	const char	keys_rel_fname[] =	"data/ntp.keys";
43897be3a4Schristos 	char		msg_fname[PATH_MAX];
44897be3a4Schristos 	char		keys_fname[PATH_MAX];
45897be3a4Schristos 	int		msgf;
46897be3a4Schristos 	int		result;
47897be3a4Schristos 	struct stat	msg_stat;
48897be3a4Schristos 	u_char *	msg;
49897be3a4Schristos 	size_t		msg_sz;
50897be3a4Schristos 	size_t		pad_sz;
51897be3a4Schristos 	ssize_t		octets;
52897be3a4Schristos 
53897be3a4Schristos 	if (done_once) {
54897be3a4Schristos 		return;
55897be3a4Schristos 	}
56897be3a4Schristos 	done_once = TRUE;
57897be3a4Schristos 
58897be3a4Schristos 	init_auth();
59897be3a4Schristos 
60897be3a4Schristos 	snprintf(keys_fname, sizeof(keys_fname), "%s/%s", srcdir,
61897be3a4Schristos 		 keys_rel_fname);
62897be3a4Schristos 	if (! authreadkeys(keys_fname)) {
63897be3a4Schristos 		fprintf(stderr, "could not load keys %s\n", keys_fname);
64897be3a4Schristos 		return;
65897be3a4Schristos 	}
66897be3a4Schristos 
67897be3a4Schristos 	snprintf(msg_fname, sizeof(msg_fname), "%s/%s", srcdir, msg_rel_fname);
68897be3a4Schristos 	msgf = open(msg_fname, O_RDONLY);
69897be3a4Schristos 	if (msgf < 0) {
70897be3a4Schristos 		fprintf(stderr, "could not open msg file %s\n", msg_fname);
71897be3a4Schristos 		return;
72897be3a4Schristos 	}
73897be3a4Schristos 
74897be3a4Schristos 	result = fstat(msgf, &msg_stat);
75897be3a4Schristos 	if (result < 0) {
76897be3a4Schristos 		fprintf(stderr, "could not get msg file %s size\n", msg_fname);
77897be3a4Schristos 		return;
78897be3a4Schristos 	}
79897be3a4Schristos 
80897be3a4Schristos 	msg_sz = msg_stat.st_size;
81897be3a4Schristos 	/* round up to next multiple of 4 as needed by MD5authencrypt() */
82897be3a4Schristos 	pad_sz = sizeof(u_int32) - (msg_sz % sizeof(u_int32));
83897be3a4Schristos 	if (sizeof(u_int32) == pad_sz) {
84897be3a4Schristos 		pad_sz = 0;
85897be3a4Schristos 	}
86897be3a4Schristos 	/* allocate room for the message, key ID, and MAC */
87897be3a4Schristos 	msg = emalloc_zero(msg_sz + pad_sz + MAX_MAC_LEN);
88897be3a4Schristos 	octets = read(msgf, msg, msg_sz);
89897be3a4Schristos 	if (octets != msg_sz) {
90897be3a4Schristos 		fprintf(stderr, "could not read msg from file %s, %u != %u\n",
91897be3a4Schristos 			msg_fname, (u_int)octets, (u_int)msg_sz);
92897be3a4Schristos 		return;
93897be3a4Schristos 	}
94897be3a4Schristos 	zero_mem(msg + msg_sz, pad_sz);
95897be3a4Schristos 	pkt_sz = msg_sz + pad_sz;
96897be3a4Schristos 	mac = (void *)((u_char *)msg + pkt_sz);
97897be3a4Schristos 	pkt = (void *)msg;
98897be3a4Schristos 
99897be3a4Schristos 	setup = TRUE;
100897be3a4Schristos }
101897be3a4Schristos 
102897be3a4Schristos /* reduce code duplication with an ugly macro */
103897be3a4Schristos #define TEST_ONE_DIGEST(key, exp_sz, exp_mac)				\
104897be3a4Schristos do {									\
105897be3a4Schristos 	size_t res_sz;							\
106897be3a4Schristos 									\
107897be3a4Schristos 	zero_mem(mac, MAX_MAC_LEN);					\
108897be3a4Schristos 	if (!auth_findkey(key)) {					\
109897be3a4Schristos 		TEST_IGNORE_MESSAGE("MAC unsupported on this system");	\
110897be3a4Schristos 		return;							\
111897be3a4Schristos 	}								\
112897be3a4Schristos 	authtrust((key), 1);						\
113897be3a4Schristos 									\
114897be3a4Schristos 	res_sz = authencrypt((key), pkt, pkt_sz);			\
115897be3a4Schristos 	if (0 == res_sz) {						\
116897be3a4Schristos 		TEST_IGNORE_MESSAGE("Likely OpenSSL 3 failed digest "	\
117897be3a4Schristos 				    "init.");				\
118897be3a4Schristos 		return;							\
119897be3a4Schristos 	}								\
120897be3a4Schristos 	TEST_ASSERT_EQUAL_UINT((u_int)((exp_sz) + KEY_MAC_LEN), res_sz);\
121897be3a4Schristos 	dump_mac((key), mac, res_sz);					\
122897be3a4Schristos 	TEST_ASSERT_EQUAL_HEX8_ARRAY((exp_mac), mac, MAX_MAC_LEN);	\
123897be3a4Schristos } while (FALSE)
124897be3a4Schristos 
125897be3a4Schristos 
126897be3a4Schristos #define AES128CMAC_KEYID	1
127897be3a4Schristos #undef KEYID_A
128897be3a4Schristos #define KEYID_A			AES128CMAC_KEYID
129897be3a4Schristos #undef DG_SZ
130897be3a4Schristos #define DG_SZ			16
131897be3a4Schristos #undef KEYID_B
132897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
133897be3a4Schristos void test_Digest_AES128CMAC(void);
134897be3a4Schristos void test_Digest_AES128CMAC(void)
135897be3a4Schristos {
136897be3a4Schristos #if defined(OPENSSL) && defined(ENABLE_CMAC)
137897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
138897be3a4Schristos 		{
139897be3a4Schristos 			0, 0, 0, KEYID_A,
140897be3a4Schristos 			0x34, 0x5b, 0xcf, 0xa8,
141897be3a4Schristos 			0x85, 0x6e, 0x9d, 0x01,
142897be3a4Schristos 			0xeb, 0x81, 0x25, 0xc2,
143897be3a4Schristos 			0xa4, 0xb8, 0x1b, 0xe0
144897be3a4Schristos 		};
145897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
146897be3a4Schristos 		{
147897be3a4Schristos 			0, 0, 0, KEYID_B,
148897be3a4Schristos 			0xd1, 0x04, 0x4e, 0xbf,
149897be3a4Schristos 			0x79, 0x2d, 0x3a, 0x40,
150897be3a4Schristos 			0xcd, 0xdc, 0x5a, 0x44,
151897be3a4Schristos 			0xde, 0xe0, 0x0c, 0x84
152897be3a4Schristos 		};
153897be3a4Schristos 
154897be3a4Schristos 	TEST_ASSERT(setup);
155897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
156897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
157897be3a4Schristos #else	/* ! (OPENSSL && ENABLE_CMAC) follows  */
158897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL or not ENABLE_CMAC");
159897be3a4Schristos #endif
160897be3a4Schristos }
161897be3a4Schristos 
162897be3a4Schristos 
163897be3a4Schristos #define MD4_KEYID		2
164897be3a4Schristos #undef KEYID_A
165897be3a4Schristos #define KEYID_A			MD4_KEYID
166897be3a4Schristos #undef DG_SZ
167897be3a4Schristos #define DG_SZ			16
168897be3a4Schristos #undef KEYID_B
169897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
170897be3a4Schristos void test_Digest_MD4(void);
171897be3a4Schristos void test_Digest_MD4(void)
172897be3a4Schristos {
173897be3a4Schristos #ifdef OPENSSL
174897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
175897be3a4Schristos 		{
176897be3a4Schristos 			0, 0, 0, KEYID_A,
177897be3a4Schristos 			0xf3, 0x39, 0x34, 0xca,
178897be3a4Schristos 			0xe0, 0x48, 0x26, 0x0f,
179897be3a4Schristos 			0x13, 0xca, 0x56, 0x9e,
180897be3a4Schristos 			0xbc, 0x53, 0x9c, 0x66
181897be3a4Schristos 		};
182897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
183897be3a4Schristos 		{
184897be3a4Schristos 			0, 0, 0, KEYID_B,
185897be3a4Schristos 			0x5e, 0xe6, 0x81, 0xf2,
186897be3a4Schristos 			0x57, 0x57, 0x8a, 0x2b,
187897be3a4Schristos 			0xa8, 0x76, 0x8e, 0x7a,
188897be3a4Schristos 			0xc4, 0xf4, 0x34, 0x7e
189897be3a4Schristos 		};
190897be3a4Schristos 
191897be3a4Schristos 	TEST_ASSERT(setup);
192897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
193897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
194897be3a4Schristos #else	/* ! OPENSSL follows  */
195897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL");
196897be3a4Schristos #endif
197897be3a4Schristos }
198897be3a4Schristos 
199897be3a4Schristos 
200897be3a4Schristos #define MD5_KEYID		3
201897be3a4Schristos #undef KEYID_A
202897be3a4Schristos #define KEYID_A			MD5_KEYID
203897be3a4Schristos #undef DG_SZ
204897be3a4Schristos #define DG_SZ			16
205897be3a4Schristos #undef KEYID_B
206897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
207897be3a4Schristos void test_Digest_MD5(void);
208897be3a4Schristos void test_Digest_MD5(void)
209897be3a4Schristos {
210897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
211897be3a4Schristos 		{
212897be3a4Schristos 			0, 0, 0, KEYID_A,
213897be3a4Schristos 			0xa6, 0x8d, 0x3a, 0xfe,
214897be3a4Schristos 			0x52, 0xe5, 0xf7, 0xe9,
215897be3a4Schristos 			0x4c, 0x97, 0x72, 0x16,
216897be3a4Schristos 			0x7c, 0x28, 0x18, 0xaf
217897be3a4Schristos 		};
218897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
219897be3a4Schristos 		{
220897be3a4Schristos 			0, 0, 0, KEYID_B,
221897be3a4Schristos 			0xd4, 0x11, 0x2c, 0xc6,
222897be3a4Schristos 			0x66, 0x74, 0x46, 0x8b,
223897be3a4Schristos 			0x12, 0xb1, 0x8c, 0x49,
224897be3a4Schristos 			0xb0, 0x06, 0xda, 0x34
225897be3a4Schristos 		};
226897be3a4Schristos 
227897be3a4Schristos 	TEST_ASSERT(setup);
228897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
229897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
230897be3a4Schristos }
231897be3a4Schristos 
232897be3a4Schristos 
233897be3a4Schristos #define MDC2_KEYID		4
234897be3a4Schristos #undef KEYID_A
235897be3a4Schristos #define KEYID_A			MDC2_KEYID
236897be3a4Schristos #undef DG_SZ
237897be3a4Schristos #define DG_SZ			16
238897be3a4Schristos #undef KEYID_B
239897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
240897be3a4Schristos void test_Digest_MDC2(void);
241897be3a4Schristos void test_Digest_MDC2(void)
242897be3a4Schristos {
243897be3a4Schristos #ifdef OPENSSL
244897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
245897be3a4Schristos 		{
246897be3a4Schristos 			0, 0, 0, KEYID_A,
247897be3a4Schristos 			0xa0, 0xfc, 0x18, 0xb6,
248897be3a4Schristos 			0xea, 0xba, 0xa5, 0x27,
249897be3a4Schristos 			0xc9, 0x64, 0x0e, 0x41,
250897be3a4Schristos 			0x95, 0x90, 0x5d, 0xf5
251897be3a4Schristos 		};
252897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
253897be3a4Schristos 		{
254897be3a4Schristos 			0, 0, 0, KEYID_B,
255897be3a4Schristos 			0xe3, 0x2c, 0x1e, 0x64,
256897be3a4Schristos 			0x7f, 0x85, 0x81, 0xe7,
257897be3a4Schristos 			0x3b, 0xc3, 0x93, 0x5e,
258897be3a4Schristos 			0xcd, 0x0e, 0x89, 0xeb
259897be3a4Schristos 		};
260897be3a4Schristos 
261897be3a4Schristos 	TEST_ASSERT(setup);
262897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
263897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
264897be3a4Schristos #else	/* ! OPENSSL follows  */
265897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL");
266897be3a4Schristos #endif
267897be3a4Schristos }
268897be3a4Schristos 
269897be3a4Schristos 
270897be3a4Schristos #define RIPEMD160_KEYID		5
271897be3a4Schristos #undef KEYID_A
272897be3a4Schristos #define KEYID_A			RIPEMD160_KEYID
273897be3a4Schristos #undef DG_SZ
274897be3a4Schristos #define DG_SZ			20
275897be3a4Schristos #undef KEYID_B
276897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
277897be3a4Schristos void test_Digest_RIPEMD160(void);
278897be3a4Schristos void test_Digest_RIPEMD160(void)
279897be3a4Schristos {
280897be3a4Schristos #ifdef OPENSSL
281897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
282897be3a4Schristos 		{
283897be3a4Schristos 			0, 0, 0, KEYID_A,
284897be3a4Schristos 			0x8c, 0x3e, 0x55, 0xbb,
285897be3a4Schristos 			0xec, 0x7c, 0xf6, 0x30,
286897be3a4Schristos 			0xef, 0xd1, 0x45, 0x8c,
287897be3a4Schristos 			0xdd, 0x29, 0x32, 0x7e,
288897be3a4Schristos 			0x04, 0x87, 0x6c, 0xd7
289897be3a4Schristos 		};
290897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
291897be3a4Schristos 		{
292897be3a4Schristos 			0, 0, 0, KEYID_B,
293897be3a4Schristos 			0x2d, 0x4a, 0x48, 0xdd,
294897be3a4Schristos 			0x28, 0x02, 0xb4, 0x9d,
295897be3a4Schristos 			0xe3, 0x6d, 0x1b, 0x90,
296897be3a4Schristos 			0x2b, 0xc4, 0x3f, 0xe5,
297897be3a4Schristos 			0x19, 0x60, 0x12, 0xbc
298897be3a4Schristos 		};
299897be3a4Schristos 
300897be3a4Schristos 	TEST_ASSERT(setup);
301897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
302897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
303897be3a4Schristos #else	/* ! OPENSSL follows  */
304897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL");
305897be3a4Schristos #endif
306897be3a4Schristos }
307897be3a4Schristos 
308897be3a4Schristos 
309897be3a4Schristos #define SHA1_KEYID		6
310897be3a4Schristos #undef KEYID_A
311897be3a4Schristos #define KEYID_A			SHA1_KEYID
312897be3a4Schristos #undef DG_SZ
313897be3a4Schristos #define DG_SZ			20
314897be3a4Schristos #undef KEYID_B
315897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
316897be3a4Schristos void test_Digest_SHA1(void);
317897be3a4Schristos void test_Digest_SHA1(void)
318897be3a4Schristos {
319897be3a4Schristos #ifdef OPENSSL
320897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
321897be3a4Schristos 		{
322897be3a4Schristos 			0, 0, 0, KEYID_A,
323897be3a4Schristos 			0xe2, 0xc6, 0x17, 0x71,
324897be3a4Schristos 			0x03, 0xc1, 0x85, 0x56,
325897be3a4Schristos 			0x35, 0xc7, 0x4e, 0x75,
326897be3a4Schristos 			0x79, 0x82, 0x9d, 0xcb,
327897be3a4Schristos 			0x2d, 0x06, 0x0e, 0xfa
328897be3a4Schristos 		};
329897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
330897be3a4Schristos 		{
331897be3a4Schristos 			0, 0, 0, KEYID_B,
332897be3a4Schristos 			0x01, 0x16, 0x37, 0xb4,
333897be3a4Schristos 			0xf5, 0x2d, 0xe0, 0x97,
334897be3a4Schristos 			0xaf, 0xd8, 0x58, 0xf7,
335897be3a4Schristos 			0xad, 0xb3, 0x7e, 0x38,
336897be3a4Schristos 			0x86, 0x85, 0x78, 0x44
337897be3a4Schristos 		};
338897be3a4Schristos 
339897be3a4Schristos 	TEST_ASSERT(setup);
340897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
341897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
342897be3a4Schristos #else	/* ! OPENSSL follows  */
343897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL");
344897be3a4Schristos #endif
345897be3a4Schristos }
346897be3a4Schristos 
347897be3a4Schristos 
348897be3a4Schristos #define SHAKE128_KEYID		7
349897be3a4Schristos #undef KEYID_A
350897be3a4Schristos #define KEYID_A			SHAKE128_KEYID
351897be3a4Schristos #undef DG_SZ
352897be3a4Schristos #define DG_SZ			16
353897be3a4Schristos #undef KEYID_B
354897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
355897be3a4Schristos void test_Digest_SHAKE128(void);
356897be3a4Schristos void test_Digest_SHAKE128(void)
357897be3a4Schristos {
358897be3a4Schristos #ifdef OPENSSL
359897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
360897be3a4Schristos 		{
361897be3a4Schristos 			0, 0, 0, KEYID_A,
362897be3a4Schristos 			0x5c, 0x0c, 0x1a, 0x85,
363897be3a4Schristos 			0xad, 0x03, 0xb2, 0x9a,
364897be3a4Schristos 			0xe4, 0x75, 0x37, 0x93,
365897be3a4Schristos 			0xaa, 0xa6, 0xcd, 0x76
366897be3a4Schristos 		};
367897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
368897be3a4Schristos 		{
369897be3a4Schristos 			0, 0, 0, KEYID_B,
370897be3a4Schristos 			0x07, 0x04, 0x63, 0xcc,
371897be3a4Schristos 			0x46, 0xaf, 0xca, 0x00,
372897be3a4Schristos 			0x7d, 0xd1, 0x5a, 0x39,
373897be3a4Schristos 			0xfd, 0x34, 0xca, 0x10
374897be3a4Schristos 		};
375897be3a4Schristos 
376897be3a4Schristos 	TEST_ASSERT(setup);
377897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
378897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
379897be3a4Schristos #else	/* ! OPENSSL follows  */
380897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL");
381897be3a4Schristos #endif
382897be3a4Schristos }
383897be3a4Schristos 
384897be3a4Schristos 
385897be3a4Schristos #define DSA_KEYID		8
386897be3a4Schristos #undef KEYID_A
387897be3a4Schristos #define KEYID_A			DSA_KEYID
388897be3a4Schristos #undef DG_SZ
389897be3a4Schristos #define DG_SZ			20
390897be3a4Schristos #undef KEYID_B
391897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
392897be3a4Schristos void test_Digest_DSA(void);
393897be3a4Schristos void test_Digest_DSA(void)
394897be3a4Schristos {
395897be3a4Schristos #ifdef OPENSSL
396897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
397897be3a4Schristos 		{
398897be3a4Schristos 			0, 0, 0, KEYID_A,
399897be3a4Schristos 			0xaf, 0xa0, 0x1d, 0x0c,
400897be3a4Schristos 			0x92, 0xcb, 0xca, 0x95,
401897be3a4Schristos 			0x0d, 0x57, 0x60, 0x49,
402897be3a4Schristos 			0xe5, 0x28, 0x03, 0xf2,
403897be3a4Schristos 			0x7b, 0x5b, 0xb1, 0x4a
404897be3a4Schristos 		};
405897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
406897be3a4Schristos 		{
407897be3a4Schristos 			0, 0, 0, KEYID_B,
408897be3a4Schristos 			0x77, 0xcd, 0x88, 0xc2,
409897be3a4Schristos 			0xed, 0x5d, 0x57, 0xc5,
410897be3a4Schristos 			0x28, 0x92, 0xf0, 0x21,
411897be3a4Schristos 			0x2b, 0xb9, 0x48, 0xac,
412897be3a4Schristos 			0xfe, 0x9f, 0xf5, 0x1c
413897be3a4Schristos 		};
414897be3a4Schristos 
415897be3a4Schristos 	TEST_ASSERT(setup);
416897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
417897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
418897be3a4Schristos #else	/* ! OPENSSL follows  */
419897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL");
420897be3a4Schristos #endif
421897be3a4Schristos }
422897be3a4Schristos 
423897be3a4Schristos 
424897be3a4Schristos #define DSA_SHA_KEYID		9
425897be3a4Schristos #undef KEYID_A
426897be3a4Schristos #define KEYID_A			DSA_SHA_KEYID
427897be3a4Schristos #undef DG_SZ
428897be3a4Schristos #define DG_SZ			20
429897be3a4Schristos #undef KEYID_B
430897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
431897be3a4Schristos void test_Digest_DSA_SHA(void);
432897be3a4Schristos void test_Digest_DSA_SHA(void)
433897be3a4Schristos {
434897be3a4Schristos #ifdef OPENSSL
435897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
436897be3a4Schristos 		{
437897be3a4Schristos 			0, 0, 0, KEYID_A,
438897be3a4Schristos 			0x7c, 0xb5, 0x79, 0xd0,
439897be3a4Schristos 			0xf2, 0xcd, 0x47, 0xc0,
440897be3a4Schristos 			0x21, 0xf3, 0xf5, 0x04,
441897be3a4Schristos 			0x10, 0xc4, 0x59, 0x5c,
442897be3a4Schristos 			0xd9, 0xa4, 0x4f, 0x3b
443897be3a4Schristos 		};
444897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
445897be3a4Schristos 		{
446897be3a4Schristos 			0, 0, 0, KEYID_B,
447897be3a4Schristos 			0xb9, 0xca, 0xa6, 0x8e,
448897be3a4Schristos 			0xd3, 0xcb, 0x94, 0x6a,
449897be3a4Schristos 			0x6d, 0xae, 0xb4, 0xc8,
450897be3a4Schristos 			0x0e, 0xc9, 0xf6, 0xed,
451897be3a4Schristos 			0x58, 0x1a, 0xed, 0x22
452897be3a4Schristos 		};
453897be3a4Schristos 
454897be3a4Schristos 	TEST_ASSERT(setup);
455897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
456897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
457897be3a4Schristos #else	/* ! OPENSSL follows  */
458897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL");
459897be3a4Schristos #endif
460897be3a4Schristos }
461897be3a4Schristos 
462897be3a4Schristos 
463897be3a4Schristos #define SHA_KEYID		10
464897be3a4Schristos #undef KEYID_A
465897be3a4Schristos #define KEYID_A			SHA_KEYID
466897be3a4Schristos #undef DG_SZ
467897be3a4Schristos #define DG_SZ			20
468897be3a4Schristos #undef KEYID_B
469897be3a4Schristos #define KEYID_B			(KEYID_A + HEX_KEYID_OFFSET)
470897be3a4Schristos void test_Digest_SHA(void);
471897be3a4Schristos void test_Digest_SHA(void)
472897be3a4Schristos {
473897be3a4Schristos #ifdef OPENSSL
474897be3a4Schristos 	u_char expectedA[MAX_MAC_LEN] =
475897be3a4Schristos 		{
476897be3a4Schristos 			0, 0, 0, KEYID_A,
477897be3a4Schristos 			0xd5, 0xbd, 0xb8, 0x55,
478897be3a4Schristos 			0x9b, 0x9e, 0x5e, 0x8f,
479897be3a4Schristos 			0x1a, 0x3d, 0x99, 0x60,
480897be3a4Schristos 			0xbd, 0x70, 0x0c, 0x5c,
481897be3a4Schristos 			0x68, 0xae, 0xb0, 0xbd
482897be3a4Schristos 		};
483897be3a4Schristos 	u_char expectedB[MAX_MAC_LEN] =
484897be3a4Schristos 		{
485897be3a4Schristos 			0, 0, 0, KEYID_B,
486897be3a4Schristos 			0x63, 0x05, 0x41, 0x45,
487897be3a4Schristos 			0xe9, 0x61, 0x84, 0xe7,
488897be3a4Schristos 			0xc6, 0x94, 0x24, 0xa4,
489897be3a4Schristos 			0x84, 0x76, 0xc7, 0xc9,
490897be3a4Schristos 			0xdd, 0x80, 0x80, 0x89
491897be3a4Schristos 		};
492897be3a4Schristos 
493897be3a4Schristos 	TEST_ASSERT(setup);
494897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_A, DG_SZ, expectedA);
495897be3a4Schristos 	TEST_ONE_DIGEST(KEYID_B, DG_SZ, expectedB);
496897be3a4Schristos #else	/* ! OPENSSL follows  */
497897be3a4Schristos 	TEST_IGNORE_MESSAGE("Skipping, no OPENSSL");
498897be3a4Schristos #endif
499897be3a4Schristos }
500897be3a4Schristos 
501897be3a4Schristos 
502897be3a4Schristos /*
503897be3a4Schristos  * Dump a MAC in a form easy to cut and paste into the expected declaration.
504897be3a4Schristos  */
505897be3a4Schristos void dump_mac(
506897be3a4Schristos 	keyid_t		keyid,
507897be3a4Schristos 	u_char *	pmac,
508897be3a4Schristos 	size_t		octets
509897be3a4Schristos 	)
510897be3a4Schristos {
511897be3a4Schristos 	char	dump[128];
512897be3a4Schristos 	size_t	dc = 0;
513897be3a4Schristos 	size_t	idx;
514897be3a4Schristos 
515897be3a4Schristos 	dc += snprintf(dump + dc, sizeof(dump) - dc, "digest with key %u { ", keyid);
516897be3a4Schristos 
517897be3a4Schristos 	for (idx = 4; idx < octets; idx++) {
518897be3a4Schristos 		if (14 == idx) {
519897be3a4Schristos 			msyslog(LOG_DEBUG, "%s", dump);
520897be3a4Schristos 			dc = 0;
521897be3a4Schristos 		}
522897be3a4Schristos 		if (dc < sizeof(dump)) {
523897be3a4Schristos 			dc += snprintf(dump + dc, sizeof(dump) - dc,
524897be3a4Schristos 				       "0x%02x, ", pmac[idx]);
525897be3a4Schristos 		}
526897be3a4Schristos 	}
527897be3a4Schristos 
528897be3a4Schristos 	if (dc < sizeof(dump)) {
529897be3a4Schristos 		dc += snprintf(dump + dc, sizeof(dump) - dc, "}");
530897be3a4Schristos 	}
531897be3a4Schristos 
532897be3a4Schristos 	msyslog(LOG_DEBUG, "%s", dump);
533897be3a4Schristos }
534897be3a4Schristos 
535