1 /* $OpenBSD: mlkem_tests_util.c,v 1.2 2024/12/14 19:16:24 tb Exp $ */ 2 /* 3 * Copyright (c) 2024, Google Inc. 4 * Copyright (c) 2024, Bob Beck <beck@obtuse.com> 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <err.h> 20 #include <stdint.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 25 #include "mlkem_tests_util.h" 26 27 int failure; 28 int test_number; 29 30 void 31 hexdump(const uint8_t *buf, size_t len, const uint8_t *compare) 32 { 33 const char *mark = ""; 34 size_t i; 35 36 for (i = 1; i <= len; i++) { 37 if (compare != NULL) 38 mark = (buf[i - 1] != compare[i - 1]) ? "*" : " "; 39 fprintf(stderr, " %s0x%02hhx,%s", mark, buf[i - 1], 40 i % 8 && i != len ? "" : "\n"); 41 } 42 fprintf(stderr, "\n"); 43 } 44 45 int 46 hex_decode(char *buf, size_t len, uint8_t **out_buf, size_t *out_len) 47 { 48 size_t i; 49 if (*out_buf != NULL) 50 abort(); /* Du hast einin rotweinflarsche... */ 51 52 MALLOC(*out_buf, len); 53 *out_len = 0; 54 55 for (i = 0; i < len; i += 2) { 56 if (sscanf(buf + i, "%2hhx", *out_buf + *out_len) != 1) 57 err(1, "FAIL- hex decode failed for %d\n", 58 (int)*out_len); 59 (*out_len)++; 60 } 61 return 1; 62 } 63 64 void 65 grab_data(CBS *cbs, char *buf, size_t offset) 66 { 67 char *start = buf + offset; 68 size_t len = strlen(start); 69 uint8_t *new = NULL; 70 size_t new_len = 0; 71 /* This is hex encoded - decode it. */ 72 TEST(!hex_decode(start, len - 1, &new, &new_len), "hex decode failed"); 73 CBS_init(cbs, new, new_len); 74 } 75