summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/mlkem/parse_test_file.c
blob: 4138e82b678d6895e3c2721893bff85a0de1ab94 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*	$OpenBSD: parse_test_file.c,v 1.2 2024/12/26 05:51:41 tb Exp $ */

/*
 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org>
 *
 * 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.
 */

#include <sys/types.h>

#include <assert.h>
#include <err.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "bytestring.h"

#include "parse_test_file.h"

struct line_data {
	uint8_t	*data;
	size_t	 data_len;
	CBS	 cbs;
	int	 val;
};

static struct line_data *
line_data_new(void)
{
	return calloc(1, sizeof(struct line_data));
}

static void
line_data_clear(struct line_data *ld)
{
	freezero(ld->data, ld->data_len);
	explicit_bzero(ld, sizeof(*ld));
}

static void
line_data_free(struct line_data *ld)
{
	if (ld == NULL)
		return;
	line_data_clear(ld);
	free(ld);
}

static void
line_data_get_int(struct line_data *ld, int *out)
{
	*out = ld->val;
}

static void
line_data_get_cbs(struct line_data *ld, CBS *out)
{
	CBS_dup(&ld->cbs, out);
}

static void
line_data_set_int(struct line_data *ld, int val)
{
	ld->val = val;
}

static int
line_data_set_from_cbb(struct line_data *ld, CBB *cbb)
{
	if (!CBB_finish(cbb, &ld->data, &ld->data_len))
		return 0;

	CBS_init(&ld->cbs, ld->data, ld->data_len);

	return 1;
}

struct parse_state {
	size_t line;
	size_t test;

	size_t max;
	size_t cur;
	struct line_data **data;

	size_t instruction_max;
	size_t instruction_cur;
	struct line_data **instruction_data;

	int running_test_case;
};

static void
parse_state_init(struct parse_state *ps, size_t max, size_t instruction_max)
{
	size_t i;

	assert(max > 0);

	memset(ps, 0, sizeof(*ps));
	ps->test = 1;

	ps->max = max;
	if ((ps->data = calloc(max, sizeof(*ps->data))) == NULL)
		err(1, NULL);
	for (i = 0; i < max; i++) {
		if ((ps->data[i] = line_data_new()) == NULL)
			err(1, NULL);
	}

	if ((ps->instruction_max = instruction_max) > 0) {
		if ((ps->instruction_data = calloc(instruction_max,
		    sizeof(*ps->instruction_data))) == NULL)
			err(1, NULL);
		for (i = 0; i < instruction_max; i++)
			if ((ps->instruction_data[i] = line_data_new()) == NULL)
				err(1, NULL);
	}
}

static void
parse_state_finish(struct parse_state *ps)
{
	size_t i;

	for (i = 0; i < ps->max; i++)
		line_data_free(ps->data[i]);
	free(ps->data);

	for (i = 0; i < ps->instruction_max; i++)
		line_data_free(ps->instruction_data[i]);
	free(ps->instruction_data);
}

static void
parse_state_new_line(struct parse_state *ps)
{
	ps->line++;
}

static void
parse_instruction_advance(struct parse_state *ps)
{
	assert(ps->instruction_cur < ps->instruction_max);
	ps->instruction_cur++;
}

static void
parse_state_advance(struct parse_state *ps)
{
	assert(ps->cur < ps->max);

	ps->cur++;
	if ((ps->cur %= ps->max) == 0)
		ps->test++;
}

struct parse {
	struct parse_state state;

	char	*buf;
	size_t	 buf_max;
	CBS	 cbs;

	const struct test_parse *tctx;
	void *ctx;

	const char *fn;
	FILE *fp;
};

static int
parse_instructions_parsed(struct parse *p)
{
	return p->state.instruction_max == p->state.instruction_cur;
}

static void
parse_advance(struct parse *p)
{
	if (!parse_instructions_parsed(p)) {
		parse_instruction_advance(&p->state);
		return;
	}
	parse_state_advance(&p->state);
}

static size_t
parse_max(struct parse *p)
{
	return p->state.max;
}

static size_t
parse_instruction_max(struct parse *p)
{
	return p->state.instruction_max;
}

static size_t
parse_cur(struct parse *p)
{
	if (!parse_instructions_parsed(p)) {
		assert(p->state.instruction_cur < p->state.instruction_max);
		return p->state.instruction_cur;
	}

	assert(p->state.cur < parse_max(p));
	return p->state.cur;
}

static size_t
parse_must_run_test_case(struct parse *p)
{
	return parse_instructions_parsed(p) && parse_max(p) - parse_cur(p) == 1;
}

static const struct line_spec *
parse_states(struct parse *p)
{
	if (!parse_instructions_parsed(p))
		return p->tctx->instructions;
	return p->tctx->states;
}

static const struct line_spec *
parse_instruction_states(struct parse *p)
{
	return p->tctx->instructions;
}

static const struct line_spec *
parse_state(struct parse *p)
{
	return &parse_states(p)[parse_cur(p)];
}

static size_t
line(struct parse *p)
{
	return p->state.line;
}

static size_t
test(struct parse *p)
{
	return p->state.test;
}

static const char *
name(struct parse *p)
{
	if (p->state.running_test_case)
		return "running test case";
	return parse_state(p)->name;
}

static const char *
label(struct parse *p)
{
	return parse_state(p)->label;
}

static const char *
match(struct parse *p)
{
	return parse_state(p)->match;
}

static enum line
parse_line_type(struct parse *p)
{
	return parse_state(p)->type;
}

static void
parse_vinfo(struct parse *p, const char *fmt, va_list ap)
{
	fprintf(stderr, "%s:%zu test #%zu (%s): ",
	    p->fn, line(p), test(p), name(p));
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");
}

void
parse_info(struct parse *p, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	parse_vinfo(p, fmt, ap);
	va_end(ap);
}

void
parse_errx(struct parse *p, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	parse_vinfo(p, fmt, ap);
	va_end(ap);

	exit(1);
}

int
parse_length_equal(struct parse *p, const char *descr, size_t want, size_t got)
{
	if (want == got)
		return 1;

	parse_info(p, "%s length: want %zu, got %zu", descr, want, got);
	return 0;
}

static void
hexdump(const uint8_t *buf, size_t len, const uint8_t *compare)
{
	const char *mark = "", *newline;
	size_t i;

	for (i = 1; i <= len; i++) {
		if (compare != NULL)
			mark = (buf[i - 1] != compare[i - 1]) ? "*" : " ";
		newline = i % 8 ? "" : "\n";
		fprintf(stderr, " %s0x%02x,%s", mark, buf[i - 1], newline);
	}
	if ((len % 8) != 0)
		fprintf(stderr, "\n");
}

int
parse_data_equal(struct parse *p, const char *descr, CBS *want,
    const uint8_t *got, size_t got_len)
{
	if (!parse_length_equal(p, descr, CBS_len(want), got_len))
		return 0;
	if (CBS_mem_equal(want, got, got_len))
		return 1;

	parse_info(p, "%s differs", descr);
	fprintf(stderr, "want:\n");
	hexdump(CBS_data(want), CBS_len(want), got);
	fprintf(stderr, "got:\n");
	hexdump(got, got_len, CBS_data(want));
	fprintf(stderr, "\n");

	return 0;
}

static void
parse_line_data_clear(struct parse *p)
{
	size_t i;

	for (i = 0; i < parse_max(p); i++)
		line_data_clear(p->state.data[i]);
}

static struct line_data **
parse_state_data(struct parse *p)
{
	if (!parse_instructions_parsed(p))
		return p->state.instruction_data;
	return p->state.data;
}

static void
parse_state_set_int(struct parse *p, int val)
{
	if (parse_line_type(p) != LINE_STRING_MATCH)
		parse_errx(p, "%s: want %d, got %d", __func__,
		    LINE_STRING_MATCH, parse_line_type(p));
	line_data_set_int(parse_state_data(p)[parse_cur(p)], val);
}

static void
parse_state_set_from_cbb(struct parse *p, CBB *cbb)
{
	if (parse_line_type(p) != LINE_HEX)
		parse_errx(p, "%s: want %d, got %d", __func__,
		    LINE_HEX, parse_line_type(p));
	if (!line_data_set_from_cbb(parse_state_data(p)[parse_cur(p)], cbb))
		parse_errx(p, "line_data_set_from_cbb");
}

int
parse_get_int(struct parse *p, size_t idx, int *out)
{
	assert(parse_must_run_test_case(p));
	assert(idx < parse_max(p));
	assert(parse_states(p)[idx].type == LINE_STRING_MATCH);

	line_data_get_int(p->state.data[idx], out);

	return 1;
}

int
parse_get_cbs(struct parse *p, size_t idx, CBS *out)
{
	assert(parse_must_run_test_case(p));
	assert(idx < parse_max(p));
	assert(parse_states(p)[idx].type == LINE_HEX);

	line_data_get_cbs(p->state.data[idx], out);

	return 1;
}

int
parse_instruction_get_int(struct parse *p, size_t idx, int *out)
{
	assert(parse_must_run_test_case(p));
	assert(idx < parse_instruction_max(p));
	assert(parse_instruction_states(p)[idx].type == LINE_STRING_MATCH);

	line_data_get_int(p->state.instruction_data[idx], out);

	return 1;
}

int
parse_instruction_get_cbs(struct parse *p, size_t idx, CBS *out)
{
	assert(parse_must_run_test_case(p));
	assert(idx < parse_instruction_max(p));
	assert(parse_instruction_states(p)[idx].type == LINE_HEX);

	line_data_get_cbs(p->state.instruction_data[idx], out);

	return 1;
}

static int
CBS_peek_bytes(CBS *cbs, CBS *out, size_t len)
{
	CBS dup;

	CBS_dup(cbs, &dup);
	return CBS_get_bytes(&dup, out, len);
}

static int
parse_peek_string_cbs(struct parse *p, const char *str)
{
	CBS cbs;
	size_t len = strlen(str);

	if (!CBS_peek_bytes(&p->cbs, &cbs, len))
		parse_errx(p, "CBS_peek_data");

	return CBS_mem_equal(&cbs, (const uint8_t *)str, len);
}

static int
parse_get_string_cbs(struct parse *p, const char *str)
{
	CBS cbs;
	size_t len = strlen(str);

	if (!CBS_get_bytes(&p->cbs, &cbs, len))
		parse_errx(p, "CBS_get_bytes");

	return CBS_mem_equal(&cbs, (const uint8_t *)str, len);
}

static int
parse_get_string_end_cbs(struct parse *p, const char *str)
{
	CBS cbs;
	int equal = 1;

	CBS_init(&cbs, (const uint8_t *)str, strlen(str));

	if (CBS_len(&p->cbs) < CBS_len(&cbs))
		parse_errx(p, "line too short to match %s", str);

	while (CBS_len(&cbs) > 0) {
		uint8_t want, got;

		if (!CBS_get_last_u8(&cbs, &want))
			parse_errx(p, "CBS_get_last_u8");
		if (!CBS_get_last_u8(&p->cbs, &got))
			parse_errx(p, "CBS_get_last_u8");
		if (want != got)
			equal = 0;
	}

	return equal;
}

static void
parse_check_label_matches(struct parse *p)
{
	const char *sep = ": ";

	if (!parse_get_string_cbs(p, label(p)))
		parse_errx(p, "label mismatch %s", label(p));

	/* Now we expect either ": " or " = ". */
	if (!parse_peek_string_cbs(p, sep))
		sep = " = ";
	if (!parse_get_string_cbs(p, sep))
		parse_errx(p, "error getting \"%s\"", sep);
}

static int
parse_empty_or_comment_line(struct parse *p)
{
	if (CBS_len(&p->cbs) == 0) {
		return 1;
	}
	if (parse_peek_string_cbs(p, "#")) {
		if (!CBS_skip(&p->cbs, CBS_len(&p->cbs)))
			parse_errx(p, "CBS_skip");
		return 1;
	}
	return 0;
}

static void
parse_string_match_line(struct parse *p)
{
	int string_matches;

	parse_check_label_matches(p);

	string_matches = parse_get_string_cbs(p, match(p));
	parse_state_set_int(p, string_matches);

	if (!string_matches) {
		if (!CBS_skip(&p->cbs, CBS_len(&p->cbs)))
			parse_errx(p, "CBS_skip");
	}
}

static int
parse_get_hex_nibble_cbs(CBS *cbs, uint8_t *out_nibble)
{
	uint8_t c;

	if (!CBS_get_u8(cbs, &c))
		return 0;

	if (c >= '0' && c <= '9') {
		*out_nibble = c - '0';
		return 1;
	}
	if (c >= 'a' && c <= 'f') {
		*out_nibble = c - 'a' + 10;
		return 1;
	}
	if (c >= 'A' && c <= 'F') {
		*out_nibble = c - 'A' + 10;
		return 1;
	}

	return 0;
}

static void
parse_hex_line(struct parse *p)
{
	CBB cbb;

	parse_check_label_matches(p);

	if (!CBB_init(&cbb, 0))
		parse_errx(p, "CBB_init");

	while (CBS_len(&p->cbs) > 0) {
		uint8_t hi, lo;

		if (!parse_get_hex_nibble_cbs(&p->cbs, &hi))
			parse_errx(p, "parse_get_hex_nibble_cbs");
		if (!parse_get_hex_nibble_cbs(&p->cbs, &lo))
			parse_errx(p, "parse_get_hex_nibble_cbs");

		if (!CBB_add_u8(&cbb, hi << 4 | lo))
			parse_errx(p, "CBB_add_u8");
	}

	parse_state_set_from_cbb(p, &cbb);
}

static void
parse_maybe_prepare_instruction_line(struct parse *p)
{
	if (parse_instructions_parsed(p))
		return;

	/* Should not happen due to parse_empty_or_comment_line(). */
	if (CBS_len(&p->cbs) == 0)
		parse_errx(p, "empty instruction line");

	if (!parse_peek_string_cbs(p, "["))
		parse_errx(p, "expected instruction line");
	if (!parse_get_string_cbs(p, "["))
		parse_errx(p, "expected start of instruction line");
	if (!parse_get_string_end_cbs(p, "]"))
		parse_errx(p, "expected end of instruction line");
}

static void
parse_check_line_consumed(struct parse *p)
{
	if (CBS_len(&p->cbs) > 0)
		parse_errx(p, "%zu unprocessed bytes", CBS_len(&p->cbs));
}

static int
parse_run_test_case(struct parse *p)
{
	const struct test_parse *tctx = p->tctx;

	p->state.running_test_case = 1;
	return tctx->run_test_case(p->ctx);
}

static void
parse_reinit(struct parse *p)
{
	const struct test_parse *tctx = p->tctx;

	p->state.running_test_case = 0;
	parse_line_data_clear(p);
	tctx->finish(p->ctx);
	tctx->init(p->ctx, p);
}

static int
parse_maybe_run_test_case(struct parse *p)
{
	int failed = 0;

	if (parse_must_run_test_case(p)) {
		failed |= parse_run_test_case(p);
		parse_reinit(p);
	}

	parse_advance(p);

	return failed;
}

static int
parse_process_line(struct parse *p)
{
	if (parse_empty_or_comment_line(p))
		return 0;

	parse_maybe_prepare_instruction_line(p);

	switch (parse_line_type(p)) {
	case LINE_STRING_MATCH:
		parse_string_match_line(p);
		break;
	case LINE_HEX:
		parse_hex_line(p);
		break;
	default:
		parse_errx(p, "unknown line type %d", parse_line_type(p));
	}
	parse_check_line_consumed(p);

	return parse_maybe_run_test_case(p);
}

static void
parse_init(struct parse *p, const char *fn, const struct test_parse *tctx,
    void *ctx)
{
	FILE *fp;

	memset(p, 0, sizeof(*p));

	if ((fp = fopen(fn, "r")) == NULL)
		err(1, "error opening %s", fn);

	/* Poor man's basename since POSIX basename is stupid. */
	if ((p->fn = strrchr(fn, '/')) != NULL)
		p->fn++;
	else
		p->fn = fn;

	p->fp = fp;
	parse_state_init(&p->state, tctx->num_states, tctx->num_instructions);
	p->tctx = tctx;
	p->ctx = ctx;
	tctx->init(ctx, p);
}

static int
parse_next_line(struct parse *p)
{
	ssize_t len;
	uint8_t u8;

	if ((len = getline(&p->buf, &p->buf_max, p->fp)) == -1)
		return 0;

	CBS_init(&p->cbs, (const uint8_t *)p->buf, len);
	parse_state_new_line(&p->state);

	if (!CBS_get_last_u8(&p->cbs, &u8))
		parse_errx(p, "CBS_get_last_u8");

	assert(u8 == '\n');

	return 1;
}

static void
parse_finish(struct parse *p)
{
	parse_state_finish(&p->state);

	free(p->buf);

	if (ferror(p->fp))
		err(1, "%s", p->fn);
	fclose(p->fp);
}

int
parse_test_file(const char *fn, const struct test_parse *tctx, void *ctx)
{
	struct parse p;
	int failed = 0;

	parse_init(&p, fn, tctx, ctx);

	while (parse_next_line(&p))
		failed |= parse_process_line(&p);

	parse_finish(&p);

	return failed;
}