1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
**********************************************************************
* Copyright (C) Miroslav Lichvar 2017
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
**********************************************************************
*/
#include <keys.c>
#include "test.h"
#define KEYS 100
#define KEYFILE "keys.test-keys"
static
uint32_t write_random_key(FILE *f)
{
const char *hash_name;
char key[128];
uint32_t id;
int i, length;
length = random() % sizeof (key) + 1;
length = MAX(length, 4);
UTI_GetRandomBytes(&id, sizeof (id));
UTI_GetRandomBytes(key, length);
switch (random() % 6) {
#ifdef FEAT_SECHASH
case 0:
hash_name = "SHA1";
break;
case 1:
hash_name = "SHA256";
break;
case 2:
hash_name = "SHA384";
break;
case 3:
hash_name = "SHA512";
break;
#endif
case 4:
hash_name = "MD5";
break;
default:
hash_name = "";
}
fprintf(f, "%u %s %s", id, hash_name, random() % 2 ? "HEX:" : "");
for (i = 0; i < length; i++)
fprintf(f, "%02hhX", key[i]);
fprintf(f, "\n");
return id;
}
static void
generate_key_file(const char *name, uint32_t *keys)
{
FILE *f;
int i;
f = fopen(name, "w");
TEST_CHECK(f);
for (i = 0; i < KEYS; i++)
keys[i] = write_random_key(f);
fclose(f);
}
void
test_unit(void)
{
int i, j, data_len, auth_len;
uint32_t keys[KEYS], key;
unsigned char data[100], auth[MAX_HASH_LENGTH];
char conf[][100] = {
"keyfile "KEYFILE
};
CNF_Initialise(0, 0);
for (i = 0; i < sizeof conf / sizeof conf[0]; i++)
CNF_ParseLine(NULL, i + 1, conf[i]);
generate_key_file(KEYFILE, keys);
KEY_Initialise();
for (i = 0; i < 100; i++) {
DEBUG_LOG("iteration %d", i);
if (i) {
generate_key_file(KEYFILE, keys);
KEY_Reload();
}
UTI_GetRandomBytes(data, sizeof (data));
for (j = 0; j < KEYS; j++) {
TEST_CHECK(KEY_KeyKnown(keys[j]));
TEST_CHECK(KEY_GetAuthDelay(keys[j]) >= 0);
TEST_CHECK(KEY_GetAuthLength(keys[j]) >= 16);
data_len = random() % (sizeof (data) + 1);
auth_len = KEY_GenerateAuth(keys[j], data, data_len, auth, sizeof (auth));
TEST_CHECK(auth_len >= 16);
TEST_CHECK(KEY_CheckAuth(keys[j], data, data_len, auth, auth_len, auth_len));
if (j > 0 && keys[j - 1] != keys[j])
TEST_CHECK(!KEY_CheckAuth(keys[j - 1], data, data_len, auth, auth_len, auth_len));
auth_len = random() % auth_len + 1;
if (auth_len < MAX_HASH_LENGTH)
auth[auth_len]++;
TEST_CHECK(KEY_CheckAuth(keys[j], data, data_len, auth, auth_len, auth_len));
auth[auth_len - 1]++;
TEST_CHECK(!KEY_CheckAuth(keys[j], data, data_len, auth, auth_len, auth_len));
}
for (j = 0; j < 1000; j++) {
UTI_GetRandomBytes(&key, sizeof (key));
if (KEY_KeyKnown(key))
continue;
TEST_CHECK(!KEY_GenerateAuth(key, data, data_len, auth, sizeof (auth)));
TEST_CHECK(!KEY_CheckAuth(key, data, data_len, auth, auth_len, auth_len));
}
}
unlink(KEYFILE);
KEY_Finalise();
CNF_Finalise();
HSH_Finalise();
}
|