summaryrefslogtreecommitdiff
blob: 717c3e6575ffc1cb0c0b3a4fc4c340dcc061f1bc (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * The blacklist encoder for RSA/DSA key blacklisting based on partial
 * fingerprints,
 * developed under Openwall Project for Owl - http://www.openwall.com/Owl/
 *
 * Copyright (c) 2008 Dmitry V. Levin <ldv at cvs.openwall.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * The blacklist encoding was designed by Solar Designer and Dmitry V. Levin.
 * No intellectual property rights to the encoding scheme are claimed.
 *
 * This effort was supported by CivicActions - http://www.civicactions.com
 *
 * The file size to encode 294,903 of 48-bit fingerprints is just 1.3 MB,
 * which corresponds to less than 4.5 bytes per fingerprint.
 */

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif

#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <limits.h>

static void *
xmalloc(size_t size)
{
	void   *r = malloc(size);

	if (!r)
		error(EXIT_FAILURE, errno, "malloc: allocating %lu bytes",
		      (unsigned long) size);
	return r;
}

static void *
xcalloc(size_t nmemb, size_t size)
{
	void   *r = calloc(nmemb, size);

	if (!r)
		error(EXIT_FAILURE, errno, "calloc: allocating %lu*%lu bytes",
		      (unsigned long) nmemb, (unsigned long) size);
	return r;
}

static void *
xrealloc(void *ptr, size_t nmemb, size_t elem_size)
{
	if (nmemb && ULONG_MAX / nmemb < elem_size)
		error(EXIT_FAILURE, 0, "realloc: nmemb*size > ULONG_MAX");

	size_t  size = nmemb * elem_size;
	void   *r = realloc(ptr, size);

	if (!r)
		error(EXIT_FAILURE, errno,
		      "realloc: allocating %lu*%lu bytes",
		      (unsigned long) nmemb, (unsigned long) elem_size);
	return r;
}

static char *
xstrdup(const char *s)
{
	size_t  len = strlen(s);
	char   *r = xmalloc(len + 1);

	memcpy(r, s, len + 1);
	return r;
}

static unsigned
c2u(uint8_t c)
{
	return (c >= 'a') ? (c - 'a' + 10) : (c - '0');
}

static char **records = NULL;
static unsigned records_count = 0;

static int
comparator(const void *p1, const void *p2)
{
	return strcmp(*(char *const *) p1, *(char *const *) p2);
}

static void
read_stream(FILE *fp, unsigned bytes)
{
	char   *line = NULL;
	unsigned size = 0, allocated = 0, len = bytes * 2;
	int     n;

	while ((n = getline(&line, &size, fp)) >= 0)
	{
		if (n > 0 && line[n - 1] == '\n')
			line[--n] = '\0';
		if (n < len || strspn(line, "0123456789abcdef") < n)
			continue;	/* ignore short or invalid lines */
		line[len] = '\0';

		if (!records)
			records = xcalloc(allocated = 1024, sizeof(*records));
		if (records_count >= allocated)
			records = xrealloc(records, allocated *= 2,
					   sizeof(*records));
		records[records_count++] = xstrdup(line);
	}
	free(line);
	records = xrealloc(records, records_count, sizeof(*records));
	if (records_count >= (1U << 24))
		error(EXIT_FAILURE, 0, "too many records: %u", records_count);

	qsort(records, records_count, sizeof(*records), comparator);
}

static void
print_uint8(FILE *fp, uint8_t v)
{
	fprintf(fp, "%c", v);
}

static void
print_uint16(FILE *fp, uint16_t v)
{
	fprintf(fp, "%c%c", v >> 8, v & 0xff);
}

static void
print_uint24(FILE *fp, uint32_t v)
{
	fprintf(fp, "%c%c%c", (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff);
}

int
main(int ac, const char **av)
{
	unsigned count, i, record_bytes, first_index = 0, prev_index = 0;
	int     min_offset, max_offset;
	int    *offsets;

	if (ac < 2)
		error(EXIT_FAILURE, 0, "insufficient arguments");
	if (ac > 2)
		error(EXIT_FAILURE, 0, "too many arguments");
	record_bytes = atoi(av[1]);
	if (record_bytes < 6 || record_bytes > 16)
		error(EXIT_FAILURE, 0, "fingerprint size out of bounds");

	read_stream(stdin, record_bytes);

	/* initialize global records offset table */
	offsets = xcalloc(65536, sizeof(*offsets));
	for (count = 0; count < records_count; ++count, prev_index = i)
	{
		const char *r = records[count];

		i = (((((c2u(r[0]) << 4) + c2u(r[1])) << 4) +
		      c2u(r[2])) << 4) + c2u(r[3]);
		if (count == 0)
			first_index = i;
		else if (i == prev_index)
			continue;
		offsets[i] = count;
	}

	/* set offsets for indices without records */
	if (offsets[65536 - 1] == 0)
		offsets[65536 - 1] = records_count;
	for (i = 65536 - 2; i > first_index; --i)
		if (offsets[i] == 0)
			offsets[i] = offsets[i + 1];

	/* make global records offset table relative to
	   expected position assuming uniform distribution. */
	for (i = 0, min_offset = 0, max_offset = 0; i < 65536; ++i)
	{
		offsets[i] -= (i * (unsigned long long) records_count) >> 16;
		if (offsets[i] < min_offset)
			min_offset = offsets[i];
		if (offsets[i] > max_offset)
			max_offset = offsets[i];
	}
	min_offset = -min_offset;
	if (min_offset < 0)
		error(EXIT_FAILURE, 0,
		      "invalid offset shift: %d", min_offset);
	for (i = 0; i < 65536; ++i)
	{
		offsets[i] += min_offset;
		if (offsets[i] < 0 || offsets[i] >= 65536)
			error(EXIT_FAILURE, 0,
			      "offset overflow for index %#x: %d",
			      i, offsets[i]);
	}
	max_offset += min_offset;

	/* Header, 16 bytes */

	/* format version identifier */
	printf("SSH-FP00");
	/* index size, in bits */
	print_uint8(stdout, 16);
	/* offset size, in bits */
	print_uint8(stdout, 16);
	/* record size, in bits */
	print_uint8(stdout, record_bytes * 8);
	/* records count */
	print_uint24(stdout, records_count);
	/* offset shift */
	print_uint16(stdout, min_offset);
	fprintf(stderr, "records=%u, offset shift=%d, max offset=%d\n",
		records_count, min_offset, max_offset);

	/* Index, 65536 * 2 bytes */
	for (i = 0; i < 65536; ++i)
		print_uint16(stdout, offsets[i]);

	/* Fingerprints, records_count * (record_bytes-2) bytes */
	for (count = 0; count < records_count; ++count)
	{
		const char *r = records[count] + 4;

		for (i = 0; i < record_bytes - 2; ++i)
			print_uint8(stdout,
				    c2u(r[i * 2]) * 16 + c2u(r[i * 2 + 1]));
	}

	if (fclose(stdout))
		error(EXIT_FAILURE, errno, "stdout");
	return 0;
}