1 /*
2  **********************************************************************
3  * Copyright (C) Miroslav Lichvar  2020
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  **********************************************************************
19  */
20 
21 #include <config.h>
22 #include "test.h"
23 
24 #ifdef FEAT_NTS
25 
26 #include <nts_ntp_auth.c>
27 
28 #include "ntp_ext.h"
29 #include "siv.h"
30 
31 void
test_unit(void)32 test_unit(void)
33 {
34   unsigned char key[SIV_MAX_KEY_LENGTH], nonce[256], plaintext[256], plaintext2[256];
35   NTP_PacketInfo info;
36   NTP_Packet packet;
37   SIV_Instance siv;
38   int i, j, r, packet_length, nonce_length, key_length;
39   int plaintext_length, plaintext2_length, min_ef_length;
40 
41   siv = SIV_CreateInstance(AEAD_AES_SIV_CMAC_256);
42   TEST_CHECK(siv);
43 
44   for (i = 0; i < 10000; i++) {
45     key_length = SIV_GetKeyLength(AEAD_AES_SIV_CMAC_256);
46     for (j = 0; j < key_length; j++)
47       key[j] = random() % 256;
48     TEST_CHECK(SIV_SetKey(siv, key, key_length));
49 
50     nonce_length = random() % sizeof (nonce) + 1;
51     for (j = 0; j < nonce_length; j++)
52       nonce[j] = random() % 256;
53 
54     plaintext_length = random() % (sizeof (plaintext) + 1);
55     for (j = 0; j < plaintext_length; j++)
56       plaintext[j] = random() % 256;
57 
58     packet_length = NTP_HEADER_LENGTH + random() % 100 * 4;
59     min_ef_length = random() % (sizeof (packet) - packet_length);
60 
61     memset(&packet, 0, sizeof (packet));
62     packet.lvm = NTP_LVM(0, 4, 0);
63     memset(&info, 0, sizeof (info));
64     info.version = 4;
65     info.length = packet_length;
66 
67     DEBUG_LOG("packet_length=%d nonce_length=%d plaintext_length=%d min_ef_length=%d",
68               packet_length, nonce_length, plaintext_length, min_ef_length);
69 
70     r = NNA_GenerateAuthEF(&packet, &info, siv, nonce, nonce_length, plaintext,
71                            -1, 0);
72     TEST_CHECK(!r);
73     r = NNA_GenerateAuthEF(&packet, &info, siv, nonce, 0, plaintext,
74                            plaintext_length, 0);
75     TEST_CHECK(!r);
76     r = NNA_GenerateAuthEF(&packet, &info, siv, nonce, nonce_length, plaintext,
77                            plaintext_length, sizeof (packet) - info.length + 1);
78     TEST_CHECK(!r);
79 
80     r = NNA_GenerateAuthEF(&packet, &info, siv, nonce, nonce_length, plaintext,
81                            plaintext_length, min_ef_length);
82     TEST_CHECK(r);
83     TEST_CHECK(info.length - packet_length >= min_ef_length);
84 
85     r = NNA_DecryptAuthEF(&packet, &info, siv, packet_length, plaintext2,
86                           -1, &plaintext2_length);
87     TEST_CHECK(!r);
88 
89     r = NNA_DecryptAuthEF(&packet, &info, siv, packet_length, plaintext2,
90                           sizeof (plaintext2), &plaintext2_length);
91     TEST_CHECK(r);
92     TEST_CHECK(plaintext_length == plaintext2_length);
93     TEST_CHECK(memcmp(plaintext, plaintext2, plaintext_length) == 0);
94 
95     j = random() % (packet_length + plaintext_length +
96                     nonce_length + SIV_GetTagLength(siv) + 8) / 4 * 4;
97     ((unsigned char *)&packet)[j]++;
98     r = NNA_DecryptAuthEF(&packet, &info, siv, packet_length, plaintext2,
99                           sizeof (plaintext2), &plaintext2_length);
100     TEST_CHECK(!r);
101     ((unsigned char *)&packet)[j]--;
102   }
103 
104   SIV_DestroyInstance(siv);
105 }
106 #else
107 void
test_unit(void)108 test_unit(void)
109 {
110   TEST_REQUIRE(0);
111 }
112 #endif
113