diff options
author | tb <> | 2024-12-26 00:04:24 +0000 |
---|---|---|
committer | tb <> | 2024-12-26 00:04:24 +0000 |
commit | 31d1b04da9af806cdb66a2b49ed6490e67479eef (patch) | |
tree | f187d226245651988501e2fb8891081ff9eea9f2 /src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h | |
parent | fe8b80dbfd7a71d866da84cfdab5d2ce23feac28 (diff) | |
download | openbsd-31d1b04da9af806cdb66a2b49ed6490e67479eef.tar.gz openbsd-31d1b04da9af806cdb66a2b49ed6490e67479eef.tar.bz2 openbsd-31d1b04da9af806cdb66a2b49ed6490e67479eef.zip |
Overhaul ML-KEM regress once more
Implement a file parser that drives a state machine to extract the test data
from the .txt files and manages the parsed data. Comments and empty lines are
ignored. The code currently assumes that instruction lines are at the start
of the file (which isn't generally true) and only supports two line types for
now. This is good enough for all the ML-KEM tests but should be easy enough
to extend.
Once all data for a test case is parsed in the expected order, a test handler
is called which can retrieve the test data via a simple API and throw warnings
and errors with information on the test case line number, etc.
Merge the tests into three programs: one parsing the .txt files and running
the corresponding test cases, a unit test and the iteration tests. Deduplicate
the actual test code and let the caller pass in an object containing the
API functions, private keys and arrays that need to be different between the
768 version and the 1024 version. This way we don't have two sets of half a
dozen .c files differing only in 3 or 4 occurrences of 768 and 1024.
All this will also make it a lot easier to hook these tests into portable.
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h')
-rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h | 80 |
1 files changed, 60 insertions, 20 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h index cbb0f83f8c..7fbe6f76a9 100644 --- a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h +++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: mlkem_tests_util.h,v 1.3 2024/12/20 00:07:12 tb Exp $ */ | 1 | /* $OpenBSD: mlkem_tests_util.h,v 1.4 2024/12/26 00:04:24 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 | * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> |
@@ -24,26 +24,66 @@ | |||
24 | 24 | ||
25 | #include "bytestring.h" | 25 | #include "bytestring.h" |
26 | 26 | ||
27 | struct MLKEM1024_private_key; | 27 | #include "mlkem.h" |
28 | struct MLKEM1024_public_key; | 28 | #include "mlkem_internal.h" |
29 | struct MLKEM768_private_key; | ||
30 | struct MLKEM768_public_key; | ||
31 | 29 | ||
32 | /* XXX - return values of the two compare functions are inconsistent */ | ||
33 | int compare_data(const uint8_t *want, const uint8_t *got, size_t len, | 30 | int compare_data(const uint8_t *want, const uint8_t *got, size_t len, |
34 | size_t line, const char *msg); | 31 | const char *msg); |
35 | int compare_length(size_t want, size_t got, size_t line, const char *msg); | 32 | |
36 | 33 | int mlkem768_encode_private_key(const void *priv, uint8_t **out_buf, | |
37 | void hex_decode_cbs(CBS *cbs, CBB *cbb, size_t line, const char *msg); | 34 | size_t *out_len); |
38 | int get_string_cbs(CBS *cbs, const char *str, size_t line, const char *msg); | 35 | int mlkem768_encode_public_key(const void *pub, uint8_t **out_buf, |
39 | 36 | size_t *out_len); | |
40 | int mlkem768_encode_private_key(const struct MLKEM768_private_key *priv, | 37 | int mlkem1024_encode_private_key(const void *priv, uint8_t **out_buf, |
41 | uint8_t **out_buf, size_t *out_len); | 38 | size_t *out_len); |
42 | int mlkem768_encode_public_key(const struct MLKEM768_public_key *pub, | 39 | int mlkem1024_encode_public_key(const void *pub, uint8_t **out_buf, |
43 | uint8_t **out_buf, size_t *out_len); | 40 | size_t *out_len); |
44 | int mlkem1024_encode_private_key(const struct MLKEM1024_private_key *priv, | 41 | |
45 | uint8_t **out_buf, size_t *out_len); | 42 | int mlkem768_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], |
46 | int mlkem1024_encode_public_key(const struct MLKEM1024_public_key *pub, | 43 | const uint8_t *ciphertext, size_t ciphertext_len, const void *priv); |
47 | uint8_t **out_buf, size_t *out_len); | 44 | void mlkem768_encap(uint8_t *out_ciphertext, |
45 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const void *pub); | ||
46 | void mlkem768_encap_external_entropy(uint8_t *out_ciphertext, | ||
47 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const void *pub, | ||
48 | const uint8_t entropy[MLKEM_ENCAP_ENTROPY]); | ||
49 | void mlkem768_generate_key(uint8_t *out_encoded_public_key, | ||
50 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key); | ||
51 | void mlkem768_generate_key_external_entropy(uint8_t *out_encoded_public_key, | ||
52 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]); | ||
53 | int mlkem768_parse_private_key(void *priv, CBS *private_key_cbs); | ||
54 | int mlkem768_parse_public_key(void *pub, CBS *in); | ||
55 | void mlkem768_public_from_private(void *out_public_key, const void *private_key); | ||
56 | |||
57 | int mlkem1024_decap(uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], | ||
58 | const uint8_t *ciphertext, size_t ciphertext_len, const void *priv); | ||
59 | void mlkem1024_encap(uint8_t *out_ciphertext, | ||
60 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const void *pub); | ||
61 | void mlkem1024_encap_external_entropy(uint8_t *out_ciphertext, | ||
62 | uint8_t out_shared_secret[MLKEM_SHARED_SECRET_BYTES], const void *pub, | ||
63 | const uint8_t entropy[MLKEM_ENCAP_ENTROPY]); | ||
64 | void mlkem1024_generate_key(uint8_t *out_encoded_public_key, | ||
65 | uint8_t optional_out_seed[MLKEM_SEED_BYTES], void *out_private_key); | ||
66 | void mlkem1024_generate_key_external_entropy(uint8_t *out_encoded_public_key, | ||
67 | void *out_private_key, const uint8_t entropy[MLKEM_SEED_BYTES]); | ||
68 | int mlkem1024_parse_private_key(void *priv, CBS *private_key_cbs); | ||
69 | int mlkem1024_parse_public_key(void *pub, CBS *in); | ||
70 | void mlkem1024_public_from_private(void *out_public_key, const void *private_key); | ||
71 | |||
72 | typedef int (*mlkem_encode_private_key_fn)(const void *, uint8_t **, size_t *); | ||
73 | typedef int (*mlkem_encode_public_key_fn)(const void *, uint8_t **, size_t *); | ||
74 | typedef int (*mlkem_decap_fn)(uint8_t [MLKEM_SHARED_SECRET_BYTES], | ||
75 | const uint8_t *, size_t, const void *); | ||
76 | typedef void (*mlkem_encap_fn)(uint8_t *, uint8_t [MLKEM_SHARED_SECRET_BYTES], | ||
77 | const void *); | ||
78 | typedef void (*mlkem_encap_external_entropy_fn)(uint8_t *, | ||
79 | uint8_t [MLKEM_SHARED_SECRET_BYTES], const void *, | ||
80 | const uint8_t [MLKEM_ENCAP_ENTROPY]); | ||
81 | typedef void (*mlkem_generate_key_fn)(uint8_t *, uint8_t *, void *); | ||
82 | typedef void (*mlkem_generate_key_external_entropy_fn)(uint8_t *, void *, | ||
83 | const uint8_t [MLKEM_SEED_BYTES]); | ||
84 | typedef int (*mlkem_parse_private_key_fn)(void *, CBS *); | ||
85 | typedef int (*mlkem_parse_public_key_fn)(void *, CBS *); | ||
86 | typedef void (*mlkem_public_from_private_fn)(void *out_public_key, | ||
87 | const void *private_key); | ||
48 | 88 | ||
49 | #endif /* MLKEM_TEST_UTIL_H */ | 89 | #endif /* MLKEM_TEST_UTIL_H */ |