xref: /netbsd-src/external/bsd/ntp/dist/sntp/tests/packetProcessing.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: packetProcessing.c,v 1.5 2024/08/18 20:47:26 christos Exp $	*/
248f8ae19Schristos 
3f17b710fSchristos #include "config.h"
4ae49d4a4Schristos 
5f17b710fSchristos #include "sntptest.h"
6f17b710fSchristos #include "networking.h"
7f17b710fSchristos #include "ntp_stdlib.h"
8f17b710fSchristos #include "unity.h"
9f17b710fSchristos 
1056f2724eSchristos #define CMAC		"AES128CMAC"
1156f2724eSchristos #define CMAC_LENGTH	16
1256f2724eSchristos 
13a6f3f22fSchristos 
145645e8e7Schristos /* Hacks into the key database. */
15f17b710fSchristos extern struct key* key_ptr;
16f17b710fSchristos extern int key_cnt;
17f17b710fSchristos 
18f17b710fSchristos 
19a6f3f22fSchristos void PrepareAuthenticationTest(int key_id,int key_len,const char* type,const void* key_seq);
20a6f3f22fSchristos void setUp(void);
21a6f3f22fSchristos void tearDown(void);
22a6f3f22fSchristos void test_TooShortLength(void);
23a6f3f22fSchristos void test_LengthNotMultipleOfFour(void);
24a6f3f22fSchristos void test_TooShortExtensionFieldLength(void);
25a6f3f22fSchristos void test_UnauthenticatedPacketReject(void);
26a6f3f22fSchristos void test_CryptoNAKPacketReject(void);
27a6f3f22fSchristos void test_AuthenticatedPacketInvalid(void);
28a6f3f22fSchristos void test_AuthenticatedPacketUnknownKey(void);
29a6f3f22fSchristos void test_ServerVersionTooOld(void);
30a6f3f22fSchristos void test_ServerVersionTooNew(void);
31a6f3f22fSchristos void test_NonWantedMode(void);
32a6f3f22fSchristos void test_KoDRate(void);
33a6f3f22fSchristos void test_KoDDeny(void);
34a6f3f22fSchristos void test_RejectUnsyncedServer(void);
35a6f3f22fSchristos void test_RejectWrongResponseServerMode(void);
36a6f3f22fSchristos void test_AcceptNoSentPacketBroadcastMode(void);
37a6f3f22fSchristos void test_CorrectUnauthenticatedPacket(void);
38a6f3f22fSchristos void test_CorrectAuthenticatedPacketMD5(void);
39*eabc0478Schristos void test_CorrectAuthenticatedPacketSHAKE128(void);
40a6f3f22fSchristos void test_CorrectAuthenticatedPacketSHA1(void);
4156f2724eSchristos void test_CorrectAuthenticatedPacketCMAC(void);
42a6f3f22fSchristos 
435645e8e7Schristos /* [Bug 2998] There are some issues whith the definition of 'struct pkt'
445645e8e7Schristos  * when AUTOKEY is undefined -- the formal struct is too small to hold
455645e8e7Schristos  * all the extension fields that are going to be tested. We have to make
46*eabc0478Schristos  * sure we have the extra bytes, or the test yields undefined results due
475645e8e7Schristos  * to buffer overrun.
485645e8e7Schristos  */
495645e8e7Schristos #ifndef AUTOKEY
505645e8e7Schristos # define EXTRA_BUFSIZE 256
515645e8e7Schristos #else
525645e8e7Schristos # define EXTRA_BUFSIZE 0
535645e8e7Schristos #endif
54a6f3f22fSchristos 
555645e8e7Schristos union tpkt {
565645e8e7Schristos 	struct pkt p;
575645e8e7Schristos 	u_char     b[sizeof(struct pkt) + EXTRA_BUFSIZE];
585645e8e7Schristos };
595645e8e7Schristos 
605645e8e7Schristos static union tpkt testpkt;
615645e8e7Schristos static union tpkt testspkt;
62f17b710fSchristos static sockaddr_u testsock;
63f17b710fSchristos bool restoreKeyDb;
64f17b710fSchristos 
65a6f3f22fSchristos 
66a6f3f22fSchristos void
67ae49d4a4Schristos PrepareAuthenticationTest(
68ae49d4a4Schristos 	int		key_id,
69f17b710fSchristos 	int		key_len,
70f17b710fSchristos 	const char *	type,
71ae49d4a4Schristos 	const void *	key_seq
72ae49d4a4Schristos 	)
73ae49d4a4Schristos {
74f17b710fSchristos 	char str[25];
75*eabc0478Schristos 
76*eabc0478Schristos 	snprintf(str, sizeof(str), "%d", key_id);
77f17b710fSchristos 	ActivateOption("-a", str);
78f17b710fSchristos 
79f17b710fSchristos 	key_cnt = 1;
80*eabc0478Schristos 	if (NULL == key_ptr) {
81*eabc0478Schristos 		key_ptr = emalloc(sizeof(*key_ptr));
82*eabc0478Schristos 	}
83f17b710fSchristos 	key_ptr->next = NULL;
84f17b710fSchristos 	key_ptr->key_id = key_id;
85f17b710fSchristos 	key_ptr->key_len = key_len;
86*eabc0478Schristos 	strncpy(key_ptr->typen, type, sizeof(key_ptr->typen));
87f17b710fSchristos 
88f17b710fSchristos 	TEST_ASSERT_TRUE(key_len < sizeof(key_ptr->key_seq));
89f17b710fSchristos 
90*eabc0478Schristos 	memcpy(key_ptr->key_seq, key_seq,
91*eabc0478Schristos 	       min(key_len, sizeof(key_ptr->key_seq)));
92f17b710fSchristos 	restoreKeyDb = true;
93f17b710fSchristos }
94f17b710fSchristos 
95a6f3f22fSchristos 
96a6f3f22fSchristos void
97ae49d4a4Schristos setUp(void)
98ae49d4a4Schristos {
99f17b710fSchristos 
100f17b710fSchristos 	sntptest();
101f17b710fSchristos 	restoreKeyDb = false;
102f17b710fSchristos 
103f17b710fSchristos 	/* Initialize the test packet and socket,
104ae49d4a4Schristos 	 * so they contain at least some valid data.
105ae49d4a4Schristos 	 */
1065645e8e7Schristos 	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION,
107f17b710fSchristos 					      MODE_SERVER);
1085645e8e7Schristos 	testpkt.p.stratum = STRATUM_REFCLOCK;
1095645e8e7Schristos 	memcpy(&testpkt.p.refid, "GPS\0", 4);
110f17b710fSchristos 
111f17b710fSchristos 	/* Set the origin timestamp of the received packet to the
112ae49d4a4Schristos 	 * same value as the transmit timestamp of the sent packet.
113ae49d4a4Schristos 	 */
114f17b710fSchristos 	l_fp tmp;
115f17b710fSchristos 	tmp.l_ui = 1000UL;
116f17b710fSchristos 	tmp.l_uf = 0UL;
117f17b710fSchristos 
1185645e8e7Schristos 	HTONL_FP(&tmp, &testpkt.p.org);
1195645e8e7Schristos 	HTONL_FP(&tmp, &testspkt.p.xmt);
120f17b710fSchristos }
121f17b710fSchristos 
122a6f3f22fSchristos 
123a6f3f22fSchristos void
124ae49d4a4Schristos tearDown(void)
125ae49d4a4Schristos {
126f17b710fSchristos 	if (restoreKeyDb) {
127f17b710fSchristos 		key_cnt = 0;
128f17b710fSchristos 		free(key_ptr);
129f17b710fSchristos 		key_ptr = NULL;
130f17b710fSchristos 	}
131f17b710fSchristos 
132ae49d4a4Schristos 	sntptest_destroy(); /* only on the final test!! if counter == 0 etc... */
133f17b710fSchristos }
134f17b710fSchristos 
135f17b710fSchristos 
136a6f3f22fSchristos void
137ae49d4a4Schristos test_TooShortLength(void)
138ae49d4a4Schristos {
139f17b710fSchristos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
1405645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC - 1,
1415645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
142f17b710fSchristos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
1435645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC - 1,
1445645e8e7Schristos 				      MODE_BROADCAST, &testspkt.p, "UnitTest"));
145f17b710fSchristos }
146f17b710fSchristos 
147a6f3f22fSchristos 
148a6f3f22fSchristos void
149ae49d4a4Schristos test_LengthNotMultipleOfFour(void)
150ae49d4a4Schristos {
151f17b710fSchristos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
1525645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC + 6,
1535645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
154f17b710fSchristos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
1555645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC + 3,
1565645e8e7Schristos 				      MODE_BROADCAST, &testspkt.p, "UnitTest"));
157f17b710fSchristos }
158f17b710fSchristos 
159a6f3f22fSchristos 
160a6f3f22fSchristos void
161ae49d4a4Schristos test_TooShortExtensionFieldLength(void)
162ae49d4a4Schristos {
1635645e8e7Schristos 	/* [Bug 2998] We have to get around the formal specification of
1645645e8e7Schristos 	 * the extension field if AUTOKEY is undefined. (At least CLANG
1655645e8e7Schristos 	 * issues a warning in this case. It's just a warning, but
1665645e8e7Schristos 	 * still...
1675645e8e7Schristos 	 */
1685645e8e7Schristos 	uint32_t * pe = testpkt.p.exten + 7;
1695645e8e7Schristos 
170f17b710fSchristos 	/* The lower 16-bits are the length of the extension field.
171f17b710fSchristos 	 * This lengths must be multiples of 4 bytes, which gives
172ae49d4a4Schristos 	 * a minimum of 4 byte extension field length.
173ae49d4a4Schristos 	 */
1745645e8e7Schristos 	*pe = htonl(3); /* 3 bytes is too short. */
175f17b710fSchristos 
176f17b710fSchristos 	/* We send in a pkt_len of header size + 4 byte extension
177f17b710fSchristos 	 * header + 24 byte MAC, this prevents the length error to
178ae49d4a4Schristos 	 * be caught at an earlier stage
179ae49d4a4Schristos 	 */
180f17b710fSchristos 	int pkt_len = LEN_PKT_NOMAC + 4 + 24;
181f17b710fSchristos 
182f17b710fSchristos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
1835645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
1845645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
185f17b710fSchristos }
186f17b710fSchristos 
187a6f3f22fSchristos 
188a6f3f22fSchristos void
189ae49d4a4Schristos test_UnauthenticatedPacketReject(void)
190ae49d4a4Schristos {
191ae49d4a4Schristos 	/* Activate authentication option */
192f17b710fSchristos 	ActivateOption("-a", "123");
193f17b710fSchristos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
194f17b710fSchristos 
195f17b710fSchristos 	int pkt_len = LEN_PKT_NOMAC;
196f17b710fSchristos 
197ae49d4a4Schristos 	/* We demand authentication, but no MAC header is present. */
198f17b710fSchristos 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
1995645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
2005645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
201f17b710fSchristos }
202f17b710fSchristos 
203a6f3f22fSchristos 
204a6f3f22fSchristos void
205ae49d4a4Schristos test_CryptoNAKPacketReject(void)
206ae49d4a4Schristos {
207ae49d4a4Schristos 	/* Activate authentication option */
208f17b710fSchristos 	ActivateOption("-a", "123");
209f17b710fSchristos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
210f17b710fSchristos 
211ae49d4a4Schristos 	int pkt_len = LEN_PKT_NOMAC + 4; /* + 4 byte MAC = Crypto-NAK */
212f17b710fSchristos 
213f17b710fSchristos 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
2145645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
2155645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
216f17b710fSchristos }
217f17b710fSchristos 
218a6f3f22fSchristos 
219a6f3f22fSchristos void
220ae49d4a4Schristos test_AuthenticatedPacketInvalid(void)
221ae49d4a4Schristos {
222*eabc0478Schristos #ifdef OPENSSL
223*eabc0478Schristos 	size_t pkt_len = LEN_PKT_NOMAC;
224*eabc0478Schristos 	size_t mac_len;
225*eabc0478Schristos 
226ae49d4a4Schristos 	/* Activate authentication option */
227*eabc0478Schristos 	PrepareAuthenticationTest(50, 9, "SHAKE128", "123456789");
228f17b710fSchristos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
229f17b710fSchristos 
230ae49d4a4Schristos 	/* Prepare the packet. */
2315645e8e7Schristos 	testpkt.p.exten[0] = htonl(50);
232*eabc0478Schristos 	mac_len = make_mac(&testpkt.p, pkt_len, key_ptr,
233*eabc0478Schristos 			   &testpkt.p.exten[1], MAX_MDG_LEN);
234f17b710fSchristos 
235*eabc0478Schristos 	pkt_len += KEY_MAC_LEN + mac_len;
236f17b710fSchristos 
237ae49d4a4Schristos 	/* Now, alter the MAC so it becomes invalid. */
2385645e8e7Schristos 	testpkt.p.exten[1] += 1;
239f17b710fSchristos 
240f17b710fSchristos 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
2415645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
2425645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
243*eabc0478Schristos 
244*eabc0478Schristos #else
245*eabc0478Schristos 
246*eabc0478Schristos 	TEST_IGNORE_MESSAGE("OpenSSL not enabled, skipping...");
247*eabc0478Schristos 
248*eabc0478Schristos #endif
249f17b710fSchristos }
250f17b710fSchristos 
251a6f3f22fSchristos 
252a6f3f22fSchristos void
253ae49d4a4Schristos test_AuthenticatedPacketUnknownKey(void)
254ae49d4a4Schristos {
255*eabc0478Schristos #ifdef OPENSSL
256*eabc0478Schristos 	size_t pkt_len = LEN_PKT_NOMAC;
257*eabc0478Schristos 	size_t mac_len;
258*eabc0478Schristos 
259ae49d4a4Schristos 	/* Activate authentication option */
260*eabc0478Schristos 	PrepareAuthenticationTest(30, 9, "SHAKE128", "123456789");
261f17b710fSchristos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
262f17b710fSchristos 
263ae49d4a4Schristos 	/* Prepare the packet. Note that the Key-ID expected is 30, but
264ae49d4a4Schristos 	 * the packet has a key id of 50.
265ae49d4a4Schristos 	 */
2665645e8e7Schristos 	testpkt.p.exten[0] = htonl(50);
267*eabc0478Schristos 	mac_len = make_mac(&testpkt.p, pkt_len, key_ptr,
268*eabc0478Schristos 			   &testpkt.p.exten[1], MAX_MDG_LEN);
26956f2724eSchristos 	pkt_len += KEY_MAC_LEN + mac_len;
270f17b710fSchristos 
271f17b710fSchristos 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
2725645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
2735645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
274*eabc0478Schristos 
275*eabc0478Schristos #else
276*eabc0478Schristos 
277*eabc0478Schristos 	TEST_IGNORE_MESSAGE("OpenSSL not enabled, skipping...");
278*eabc0478Schristos 
279*eabc0478Schristos #endif
280f17b710fSchristos }
281f17b710fSchristos 
282a6f3f22fSchristos 
283a6f3f22fSchristos void
284ae49d4a4Schristos test_ServerVersionTooOld(void)
285ae49d4a4Schristos {
286f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
287f17b710fSchristos 
2885645e8e7Schristos 	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
289f17b710fSchristos 					      NTP_OLDVERSION - 1,
290f17b710fSchristos 					      MODE_CLIENT);
2915645e8e7Schristos 	TEST_ASSERT_TRUE(PKT_VERSION(testpkt.p.li_vn_mode) < NTP_OLDVERSION);
292f17b710fSchristos 
293f17b710fSchristos 	int pkt_len = LEN_PKT_NOMAC;
294f17b710fSchristos 
295f17b710fSchristos 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
2965645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
2975645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
298f17b710fSchristos }
299f17b710fSchristos 
300a6f3f22fSchristos 
301a6f3f22fSchristos void
302ae49d4a4Schristos test_ServerVersionTooNew(void)
303ae49d4a4Schristos {
304f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
305f17b710fSchristos 
3065645e8e7Schristos 	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
307f17b710fSchristos 					      NTP_VERSION + 1,
308f17b710fSchristos 					      MODE_CLIENT);
3095645e8e7Schristos 	TEST_ASSERT_TRUE(PKT_VERSION(testpkt.p.li_vn_mode) > NTP_VERSION);
310f17b710fSchristos 
311f17b710fSchristos 	int pkt_len = LEN_PKT_NOMAC;
312f17b710fSchristos 
313f17b710fSchristos 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
3145645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
3155645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
316f17b710fSchristos }
317f17b710fSchristos 
318a6f3f22fSchristos 
319a6f3f22fSchristos void
320ae49d4a4Schristos test_NonWantedMode(void)
321ae49d4a4Schristos {
322f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
323f17b710fSchristos 
3245645e8e7Schristos 	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
325f17b710fSchristos 					      NTP_VERSION,
326f17b710fSchristos 					      MODE_CLIENT);
327f17b710fSchristos 
328ae49d4a4Schristos 	/* The packet has a mode of MODE_CLIENT, but process_pkt expects
329ae49d4a4Schristos 	 * MODE_SERVER
330ae49d4a4Schristos 	 */
331f17b710fSchristos 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
3325645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
3335645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
334f17b710fSchristos }
335f17b710fSchristos 
336a6f3f22fSchristos 
337f17b710fSchristos /* Tests bug 1597 */
338a6f3f22fSchristos void
339ae49d4a4Schristos test_KoDRate(void)
340ae49d4a4Schristos {
341f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
342f17b710fSchristos 
3435645e8e7Schristos 	testpkt.p.stratum = STRATUM_PKT_UNSPEC;
3445645e8e7Schristos 	memcpy(&testpkt.p.refid, "RATE", 4);
345f17b710fSchristos 
346f17b710fSchristos 	TEST_ASSERT_EQUAL(KOD_RATE,
3475645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
3485645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
349f17b710fSchristos }
350f17b710fSchristos 
351a6f3f22fSchristos 
352a6f3f22fSchristos void
353ae49d4a4Schristos test_KoDDeny(void)
354ae49d4a4Schristos {
355f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
356f17b710fSchristos 
3575645e8e7Schristos 	testpkt.p.stratum = STRATUM_PKT_UNSPEC;
3585645e8e7Schristos 	memcpy(&testpkt.p.refid, "DENY", 4);
359f17b710fSchristos 
360f17b710fSchristos 	TEST_ASSERT_EQUAL(KOD_DEMOBILIZE,
3615645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
3625645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
363f17b710fSchristos }
364f17b710fSchristos 
365a6f3f22fSchristos 
366a6f3f22fSchristos void
367ae49d4a4Schristos test_RejectUnsyncedServer(void)
368ae49d4a4Schristos {
369f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
370f17b710fSchristos 
3715645e8e7Schristos 	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
372f17b710fSchristos 					      NTP_VERSION,
373f17b710fSchristos 					      MODE_SERVER);
374f17b710fSchristos 
375f17b710fSchristos 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
3765645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
3775645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
378f17b710fSchristos }
379f17b710fSchristos 
380a6f3f22fSchristos 
381a6f3f22fSchristos void
382ae49d4a4Schristos test_RejectWrongResponseServerMode(void)
383ae49d4a4Schristos {
384f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
385f17b710fSchristos 
386f17b710fSchristos 	l_fp tmp;
387f17b710fSchristos 	tmp.l_ui = 1000UL;
388f17b710fSchristos 	tmp.l_uf = 0UL;
3895645e8e7Schristos 	HTONL_FP(&tmp, &testpkt.p.org);
390f17b710fSchristos 
391f17b710fSchristos 	tmp.l_ui = 2000UL;
392f17b710fSchristos 	tmp.l_uf = 0UL;
3935645e8e7Schristos 	HTONL_FP(&tmp, &testspkt.p.xmt);
394f17b710fSchristos 
395f17b710fSchristos 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
3965645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
3975645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
398f17b710fSchristos }
399f17b710fSchristos 
400a6f3f22fSchristos 
401a6f3f22fSchristos void
402ae49d4a4Schristos test_AcceptNoSentPacketBroadcastMode(void)
403ae49d4a4Schristos {
404f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
405f17b710fSchristos 
4065645e8e7Schristos 	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
407f17b710fSchristos 					      NTP_VERSION,
408f17b710fSchristos 					      MODE_BROADCAST);
409f17b710fSchristos 
410f17b710fSchristos 	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
4115645e8e7Schristos 		  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
412f17b710fSchristos 			      MODE_BROADCAST, NULL, "UnitTest"));
413f17b710fSchristos }
414f17b710fSchristos 
415a6f3f22fSchristos 
416a6f3f22fSchristos void
417ae49d4a4Schristos test_CorrectUnauthenticatedPacket(void)
418ae49d4a4Schristos {
419f17b710fSchristos 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
420f17b710fSchristos 
421f17b710fSchristos 	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
4225645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
4235645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
424f17b710fSchristos }
425f17b710fSchristos 
426a6f3f22fSchristos 
427a6f3f22fSchristos void
428ae49d4a4Schristos test_CorrectAuthenticatedPacketMD5(void)
429ae49d4a4Schristos {
430*eabc0478Schristos #ifdef OPENSSL
431*eabc0478Schristos 
432*eabc0478Schristos 	keyid_t k_id = 10;
433*eabc0478Schristos 	int pkt_len = LEN_PKT_NOMAC;
434*eabc0478Schristos 	int mac_len;
435*eabc0478Schristos 
436*eabc0478Schristos 	PrepareAuthenticationTest(k_id, 15, "MD5", "123456789abcdef");
437f17b710fSchristos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
438f17b710fSchristos 
439ae49d4a4Schristos 	/* Prepare the packet. */
440*eabc0478Schristos 	testpkt.p.exten[0] = htonl(k_id);
441*eabc0478Schristos 	mac_len = make_mac(&testpkt.p, pkt_len, key_ptr,
442*eabc0478Schristos 			   &testpkt.p.exten[1], MAX_MDG_LEN);
443*eabc0478Schristos 
444*eabc0478Schristos 	/* TODO: Should not expect failure if non-FIPS OpenSSL */
445*eabc0478Schristos 	TEST_EXPECT_FAIL_MESSAGE("FIPS OpenSSL bars MD5");
446f17b710fSchristos 
44756f2724eSchristos 	pkt_len += KEY_MAC_LEN + mac_len;
448f17b710fSchristos 
449f17b710fSchristos 	TEST_ASSERT_EQUAL(pkt_len,
4505645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
4515645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
452*eabc0478Schristos 
453*eabc0478Schristos #else
454*eabc0478Schristos 
455*eabc0478Schristos 	TEST_IGNORE_MESSAGE("OpenSSL not enabled, skipping...");
456*eabc0478Schristos 
457*eabc0478Schristos #endif
458*eabc0478Schristos }
459*eabc0478Schristos 
460*eabc0478Schristos 
461*eabc0478Schristos void
462*eabc0478Schristos test_CorrectAuthenticatedPacketSHAKE128(void)
463*eabc0478Schristos {
464*eabc0478Schristos #ifdef OPENSSL
465*eabc0478Schristos 
466*eabc0478Schristos 	keyid_t k_id = 10;
467*eabc0478Schristos 	int pkt_len = LEN_PKT_NOMAC;
468*eabc0478Schristos 	int mac_len;
469*eabc0478Schristos 
470*eabc0478Schristos 	PrepareAuthenticationTest(k_id, 15, "SHAKE128", "123456789abcdef");
471*eabc0478Schristos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
472*eabc0478Schristos 
473*eabc0478Schristos 	/* Prepare the packet. */
474*eabc0478Schristos 	testpkt.p.exten[0] = htonl(k_id);
475*eabc0478Schristos 	mac_len = make_mac(&testpkt.p, pkt_len, key_ptr, &testpkt.p.exten[1],
476*eabc0478Schristos 			   SHAKE128_LENGTH);
477*eabc0478Schristos 
478*eabc0478Schristos 	pkt_len += KEY_MAC_LEN + mac_len;
479*eabc0478Schristos 
480*eabc0478Schristos 	TEST_ASSERT_EQUAL(pkt_len,
481*eabc0478Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
482*eabc0478Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
483*eabc0478Schristos 
484*eabc0478Schristos #else
485*eabc0478Schristos 
486*eabc0478Schristos 	TEST_IGNORE_MESSAGE("OpenSSL not enabled, skipping...");
487*eabc0478Schristos 
488*eabc0478Schristos #endif
489f17b710fSchristos }
490f17b710fSchristos 
491a6f3f22fSchristos 
492a6f3f22fSchristos void
493ae49d4a4Schristos test_CorrectAuthenticatedPacketSHA1(void)
494ae49d4a4Schristos {
495*eabc0478Schristos #ifdef OPENSSL
496*eabc0478Schristos 
497*eabc0478Schristos 	keyid_t k_id = 20;
498*eabc0478Schristos 	int pkt_len = LEN_PKT_NOMAC;
499*eabc0478Schristos 	int mac_len;
500*eabc0478Schristos 
501*eabc0478Schristos 	PrepareAuthenticationTest(k_id, 15, "SHA1", "abcdefghijklmno");
502f17b710fSchristos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
503f17b710fSchristos 
504ae49d4a4Schristos 	/* Prepare the packet. */
505*eabc0478Schristos 	testpkt.p.exten[0] = htonl(k_id);
506*eabc0478Schristos 	mac_len = make_mac(&testpkt.p, pkt_len, key_ptr, &testpkt.p.exten[1],
507*eabc0478Schristos 			   SHA1_LENGTH);
50856f2724eSchristos 
50956f2724eSchristos 	pkt_len += KEY_MAC_LEN + mac_len;
51056f2724eSchristos 
51156f2724eSchristos 	TEST_ASSERT_EQUAL(pkt_len,
51256f2724eSchristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
51356f2724eSchristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
514*eabc0478Schristos 
515*eabc0478Schristos #else
516*eabc0478Schristos 
517*eabc0478Schristos 	TEST_IGNORE_MESSAGE("OpenSSL not enabled, skipping...");
518*eabc0478Schristos 
519*eabc0478Schristos #endif
52056f2724eSchristos }
52156f2724eSchristos 
52256f2724eSchristos 
52356f2724eSchristos void
52456f2724eSchristos test_CorrectAuthenticatedPacketCMAC(void)
52556f2724eSchristos {
52679045f13Schristos #if defined(OPENSSL) && defined(ENABLE_CMAC)
52779045f13Schristos 
52856f2724eSchristos 	PrepareAuthenticationTest(30, CMAC_LENGTH, CMAC, "abcdefghijklmnop");
52956f2724eSchristos 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
53056f2724eSchristos 
53156f2724eSchristos 	int pkt_len = LEN_PKT_NOMAC;
53256f2724eSchristos 
53356f2724eSchristos 	/* Prepare the packet. */
53456f2724eSchristos 	testpkt.p.exten[0] = htonl(30);
535*eabc0478Schristos 	int mac_len = make_mac(&testpkt.p, pkt_len, key_ptr,
536*eabc0478Schristos 			       &testpkt.p.exten[1], MAX_MAC_LEN);
537f17b710fSchristos 
538f17b710fSchristos 	pkt_len += 4 + mac_len;
539f17b710fSchristos 
540f17b710fSchristos 	TEST_ASSERT_EQUAL(pkt_len,
5415645e8e7Schristos 			  process_pkt(&testpkt.p, &testsock, pkt_len,
5425645e8e7Schristos 				      MODE_SERVER, &testspkt.p, "UnitTest"));
54379045f13Schristos 
54479045f13Schristos #else
54579045f13Schristos 
546*eabc0478Schristos 	TEST_IGNORE_MESSAGE("CMAC not enabled, skipping...");
54779045f13Schristos 
54879045f13Schristos #endif	/* OPENSSL */
549f17b710fSchristos }
55056f2724eSchristos 
551