summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h
diff options
context:
space:
mode:
authortb <>2024-12-20 00:07:12 +0000
committertb <>2024-12-20 00:07:12 +0000
commit8aaed9d4e73e75e021900f9a05721eacb93f999d (patch)
treef04653a882e3c8a90b00b8e6f900e4a225736db9 /src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h
parent4152a99459d7137840a7c9ad6477912d3da74426 (diff)
downloadopenbsd-8aaed9d4e73e75e021900f9a05721eacb93f999d.tar.gz
openbsd-8aaed9d4e73e75e021900f9a05721eacb93f999d.tar.bz2
openbsd-8aaed9d4e73e75e021900f9a05721eacb93f999d.zip
Rework and fix the mlkem tests
Make proper use of CBB and CBS. If a CBS ever owns data, you're holding it wrong. Ditch gross macros, sscanf, and globals. The use of fgets is annoying here, so replace it with getline, which be provided by portable if needed. Most importantly, make the tests actually signal failure rather than only printing an error. Fix the state machines in a few of them. Some tests didn't parse the .txt file at all. Others mostly did but didn't actually test what they were supposed to be testing. Such failures were hidden by the way the tests were written. This basically needed a complete revamp. It still isn't pretty and much of it could be deduplicated, but I only have so much time alotted on this blue planet.
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h')
-rw-r--r--src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h63
1 files changed, 24 insertions, 39 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h
index 934de44009..cbb0f83f8c 100644
--- a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h
+++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h
@@ -1,6 +1,7 @@
1/* $OpenBSD: mlkem_tests_util.h,v 1.2 2024/12/14 19:16:24 tb Exp $ */ 1/* $OpenBSD: mlkem_tests_util.h,v 1.3 2024/12/20 00:07:12 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2024, Bob Beck <beck@obtuse.com> 3 * Copyright (c) 2024 Bob Beck <beck@obtuse.com>
4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org>
4 * 5 *
5 * Permission to use, copy, modify, and/or distribute this software for any 6 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
@@ -18,47 +19,31 @@
18#ifndef MLKEM_TEST_UTIL_H 19#ifndef MLKEM_TEST_UTIL_H
19#define MLKEM_TEST_UTIL_H 20#define MLKEM_TEST_UTIL_H
20 21
22#include <stddef.h>
21#include <stdint.h> 23#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25 24
26#include <openssl/bytestring.h> 25#include "bytestring.h"
27 26
28#define TEST(cond, msg) do { \ 27struct MLKEM1024_private_key;
29 if ((cond)) { \ 28struct MLKEM1024_public_key;
30 failure = 1; \ 29struct MLKEM768_private_key;
31 fprintf(stderr, "FAIL: %s:%d - Test %d: %s\n", \ 30struct MLKEM768_public_key;
32 __FILE__, __LINE__, test_number, msg); \
33 } \
34} while(0)
35 31
36#define MALLOC(A, B) do { \ 32/* XXX - return values of the two compare functions are inconsistent */
37 if (((A) = malloc(B)) == NULL) { \ 33int compare_data(const uint8_t *want, const uint8_t *got, size_t len,
38 failure = 1; \ 34 size_t line, const char *msg);
39 fprintf(stderr, "FAIL: %s:%d - Test %d: malloc\n", \ 35int compare_length(size_t want, size_t got, size_t line, const char *msg);
40 __FILE__, __LINE__, test_number); \
41 exit(1); \
42 } \
43} while(0)
44 36
45#define TEST_DATAEQ(values, expected, len, msg) do { \ 37void hex_decode_cbs(CBS *cbs, CBB *cbb, size_t line, const char *msg);
46 if (memcmp((values), (expected), (len)) != 0) { \ 38int get_string_cbs(CBS *cbs, const char *str, size_t line, const char *msg);
47 failure = 1; \
48 fprintf(stderr, "FAIL: %s:%d - Test %d: %s differs\n", \
49 __FILE__, __LINE__, test_number, msg); \
50 fprintf(stderr, "values:\n"); \
51 hexdump(values, len, expected); \
52 fprintf(stderr, "expected:\n"); \
53 hexdump(expected, len, values); \
54 fprintf(stderr, "\n"); \
55 } \
56} while(0)
57 39
58extern int failure, test_number; 40int mlkem768_encode_private_key(const struct MLKEM768_private_key *priv,
41 uint8_t **out_buf, size_t *out_len);
42int mlkem768_encode_public_key(const struct MLKEM768_public_key *pub,
43 uint8_t **out_buf, size_t *out_len);
44int mlkem1024_encode_private_key(const struct MLKEM1024_private_key *priv,
45 uint8_t **out_buf, size_t *out_len);
46int mlkem1024_encode_public_key(const struct MLKEM1024_public_key *pub,
47 uint8_t **out_buf, size_t *out_len);
59 48
60void hexdump(const uint8_t *buf, size_t len, const uint8_t *compare); 49#endif /* MLKEM_TEST_UTIL_H */
61int hex_decode(char *buf, size_t len, uint8_t **out_buf, size_t *out_len);
62void grab_data(CBS *cbs, char *buf, size_t offset);
63
64#endif