diff options
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 | } | ||