diff options
author | beck <> | 2024-12-13 00:03:57 +0000 |
---|---|---|
committer | beck <> | 2024-12-13 00:03:57 +0000 |
commit | ed3c5d3a4797d4b1d8f9769cfc43f8e686a59621 (patch) | |
tree | a9ff1c725db56dbeb46224505b3dd6fd05a21777 /src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c | |
parent | fd906c7b27573203602764309c3cf5faaefdf573 (diff) | |
download | openbsd-ed3c5d3a4797d4b1d8f9769cfc43f8e686a59621.tar.gz openbsd-ed3c5d3a4797d4b1d8f9769cfc43f8e686a59621.tar.bz2 openbsd-ed3c5d3a4797d4b1d8f9769cfc43f8e686a59621.zip |
Add ML-KEM 768 from BoringSSL
Changes include conversion from C++, basic KNF, then adaptation to
use our sha3 functions for sha3 and shake instead of the BorinSSL
version. This Adds units tests to run against BoringSSL and NIST test
vectors.
The future public API is the same as Boring's - but is not yet exposed
pending making bytesring.h public (which will happen separately) and
a minor bump
Currently this will just ensure we build and run regress.
ok tb@ to get it into the tree and massage from there.
Diffstat (limited to 'src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c')
-rw-r--r-- | src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c new file mode 100644 index 0000000000..ee73e19267 --- /dev/null +++ b/src/regress/lib/libcrypto/mlkem/mlkem_tests_util.c | |||
@@ -0,0 +1,70 @@ | |||
1 | /* Copyright (c) 2024, Google Inc. | ||
2 | * Copyright (c) 2024, Bob Beck <beck@obtuse.com> | ||
3 | * | ||
4 | * Permission to use, copy, modify, and/or distribute this software for any | ||
5 | * purpose with or without fee is hereby granted, provided that the above | ||
6 | * copyright notice and this permission notice appear in all copies. | ||
7 | * | ||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
11 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION | ||
13 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN | ||
14 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ | ||
15 | |||
16 | #include <err.h> | ||
17 | #include <stdlib.h> | ||
18 | #include <stdio.h> | ||
19 | #include <string.h> | ||
20 | |||
21 | #include "mlkem_tests_util.h" | ||
22 | |||
23 | int failure; | ||
24 | int test_number; | ||
25 | |||
26 | void | ||
27 | hexdump(const uint8_t *buf, size_t len, const uint8_t *compare) | ||
28 | { | ||
29 | const char *mark = ""; | ||
30 | size_t i; | ||
31 | |||
32 | for (i = 1; i <= len; i++) { | ||
33 | if (compare != NULL) | ||
34 | mark = (buf[i - 1] != compare[i - 1]) ? "*" : " "; | ||
35 | fprintf(stderr, " %s0x%02hhx,%s", mark, buf[i - 1], | ||
36 | i % 8 && i != len ? "" : "\n"); | ||
37 | } | ||
38 | fprintf(stderr, "\n"); | ||
39 | } | ||
40 | |||
41 | int | ||
42 | hex_decode(char *buf, size_t len, uint8_t **out_buf, size_t *out_len) | ||
43 | { | ||
44 | size_t i; | ||
45 | if (*out_buf != NULL) | ||
46 | abort(); /* Du hast einin rotweinflarsche... */ | ||
47 | |||
48 | MALLOC(*out_buf, len); | ||
49 | *out_len = 0; | ||
50 | |||
51 | for (i = 0; i < len; i += 2) { | ||
52 | if (sscanf(buf + i, "%2hhx", *out_buf + *out_len) != 1) | ||
53 | err(1, "FAIL- hex decode failed for %d\n", | ||
54 | (int)*out_len); | ||
55 | (*out_len)++; | ||
56 | } | ||
57 | return 1; | ||
58 | } | ||
59 | |||
60 | void | ||
61 | grab_data(CBS *cbs, char *buf, size_t offset) | ||
62 | { | ||
63 | char *start = buf + offset; | ||
64 | size_t len = strlen(start); | ||
65 | uint8_t *new = NULL; | ||
66 | size_t new_len = 0; | ||
67 | /* This is hex encoded - decode it. */ | ||
68 | TEST(!hex_decode(start, len - 1, &new, &new_len), "hex decode failed"); | ||
69 | CBS_init(cbs, new, new_len); | ||
70 | } | ||