From d6ba2397803c5f2d6a39a8684239c53f84e73b80 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 22 Jul 2026 23:48:16 +0200 Subject: aes: always require full inputs I.e. no 31 digit for 128-bit blocks, reuse code, etc. Also, generalize hex string parsing. --- aes/src/hex.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 aes/src/hex.c (limited to 'aes/src/hex.c') diff --git a/aes/src/hex.c b/aes/src/hex.c new file mode 100644 index 0000000..62b6e5a --- /dev/null +++ b/aes/src/hex.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2026 Egor Tensin + * This file is part of the "AES tools" project. + * For details, see https://github.com/egor-tensin/aes-tools. + * Distributed under the MIT License. + */ + +#include + +#include +#include + +AES_StatusCode aes_parse_hex_string( + unsigned char* dest, + const char* src, + size_t numof_bytes, + AES_ErrorDetails* err_details +) { + if (dest == NULL) + return aes_error_null_argument(err_details, "dest"); + if (src == NULL) + return aes_error_null_argument(err_details, "src"); + + char what[30]; + snprintf(what, sizeof(what), "a %zu-byte hex string", numof_bytes); + + const char* cursor = src; + + for (size_t i = 0; i < numof_bytes; ++i) { + int n; + unsigned int byte; + if (sscanf(cursor, "%2x%n", &byte, &n) != 1 || n != 2) + return aes_error_parse(err_details, src, what); + dest[i] = (unsigned char)byte; + cursor += n; + } + + if (*cursor != '\0') + return aes_error_parse(err_details, src, what); + + return AES_SUCCESS; +} -- cgit v1.3