diff options
author | vnugent <public@vaughnnugent.com> | 2024-03-03 15:02:02 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-03-03 15:02:02 -0500 |
commit | 21f6c0a9cdd5ed67e48bb1f39f72217b5fe4758f (patch) | |
tree | b081f9ccae15037372bfd906767247bbd58c2907 /tests/hex.h | |
parent | 120022aa349f5e4cac28da74d568373c49245884 (diff) |
Squashed commit of the following:
commit 490dfee4ef22479009627435c6ad728c3cbbab54
Author: vnugent <public@vaughnnugent.com>
Date: Sun Mar 3 14:59:25 2024 -0500
test: #3 tests for encryption/description and Macs
commit efa97490b7ed47f4e2f05bee52e2b33e14e439e6
Merge: 1b84e3c 120022a
Author: vnugent <public@vaughnnugent.com>
Date: Sun Mar 3 14:55:48 2024 -0500
merge master
commit 1b84e3c7c2e55b1ff9ffdd09b66873e11c131441
Author: vnugent <public@vaughnnugent.com>
Date: Sat Mar 2 22:57:36 2024 -0500
fix: #2 constent usage of sizeof() operator on struct types
commit 9de5a214c66adea0ef2d0bac63c59449de202a88
Author: vnugent <public@vaughnnugent.com>
Date: Fri Mar 1 14:30:36 2024 -0500
perf: avoid nc_key struct copy, cast and verify instead
commit b917b761120ed684af28d0707673ffadcf14b8fe
Author: vnugent <public@vaughnnugent.com>
Date: Mon Feb 12 22:06:50 2024 -0500
fix: found the constant time memcompare function
commit 9f85fff3b9f25da7410569ea94f994b88feb3910
Author: vnugent <public@vaughnnugent.com>
Date: Fri Feb 9 22:48:35 2024 -0500
feat: added/update MAC functions to sign or verify nip44 payload
commit aa5113741bb419b02d6ea416bba571fa3d65db46
Author: vnugent <public@vaughnnugent.com>
Date: Wed Feb 7 01:37:53 2024 -0500
add missing hmac-key output buffer
commit 55f47d22cc9ce4d1e22b70814d608c7ef3b1bbc9
Author: vnugent <public@vaughnnugent.com>
Date: Sun Feb 4 21:08:13 2024 -0500
simple bug fixes, and public api argument validation tests
commit 73c5a713fb164ae8b4ac8a891a8020e08eae0a3b
Author: vnugent <public@vaughnnugent.com>
Date: Fri Feb 2 23:05:48 2024 -0500
update api to return secpvalidate return code instead of internal return codes
commit 06c73004e1a39a7ea4ea3a89c22dee0f66adb236
Author: vnugent <public@vaughnnugent.com>
Date: Fri Feb 2 19:25:17 2024 -0500
change to lgpl license
commit 6e79fdb3b6b6739fc7797d47e55a7691306cf736
Author: vnugent <public@vaughnnugent.com>
Date: Wed Jan 31 21:30:49 2024 -0500
move validation macros, and optionally disable them
commit ac1e58837f1ba687939f78b5c03cadd346c10ddd
Author: vnugent <public@vaughnnugent.com>
Date: Tue Jan 30 12:25:05 2024 -0500
couple more tests, renable range checks, set flags for all projects
Diffstat (limited to 'tests/hex.h')
-rw-r--r-- | tests/hex.h | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/tests/hex.h b/tests/hex.h new file mode 100644 index 0000000..793e9f9 --- /dev/null +++ b/tests/hex.h @@ -0,0 +1,148 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Package: noscrypt +* File: hex.h +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public License +* as published by the Free Software Foundation; either version 2.1 +* of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with NativeHeapApi. If not, see http://www.gnu.org/licenses/. +*/ + + +#ifndef HEX_HELPERS_H +#define HEX_HELPERS_H + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + #include <assert.h> + #define STATIC_ASSERT(x, m) static_assert(x, m) +#else + #define STATIC_ASSERT(x, m) + #pragma message("Static assertions are not supported by this language version") +#endif + +typedef struct hexBytes +{ + uint8_t* data; + size_t length; +} HexBytes; + +/* Deferred list of HexBytes to be freed on exit */ +static HexBytes* _hdeferList[10]; +static size_t _hdeferListIndex = 0; + +/* + Allocates a HexBytes and decodes the hexadecimal string into it's binary + representation. The string must be a valid hexadecimal string and the length + and may not be NULL. The length may be known at compile time and can be used + to assert the length of the string literal. + @param hexLiteral The hexadecimal string to decode + @param strLen The length of the string +*/ +#define FromHexString(str, len) _fromHexString(str, sizeof(str) - 1); STATIC_ASSERT(sizeof(str)/2 == len && len > 0, "Invalid length hex string literal"); + +static HexBytes* __allocHexBytes(size_t length) +{ + length /= 2; + + HexBytes* hexBytes = (HexBytes*)malloc(length + sizeof(HexBytes)); + if(!hexBytes) + { + return NULL; + } + + hexBytes->length = length; + /* data starts after the structure size */ + hexBytes-> data = ((uint8_t*)hexBytes) + sizeof(HexBytes); + /* add new value to deferred cleanup list */ + _hdeferList[_hdeferListIndex++] = hexBytes; + return hexBytes; +} + +static HexBytes* _fromHexString(const char* hexLiteral, size_t strLen) +{ + HexBytes* hexBytes; + size_t i; + + if(!hexLiteral) + { + return NULL; + } + + /* alloc the raw bytes */ + hexBytes = __allocHexBytes(strLen); + + /* read every 2 chars into */ + for (i = 0; i < strLen; i += 2) + { + /* slice string into smaller 2 char strings then parse */ + char byteString[3] = { hexLiteral[i], hexLiteral[i + 1], '\0'}; + hexBytes->data[i / 2] = (uint8_t)strtol(byteString, NULL, 16); + } + + return hexBytes; +} + +/* + Frees all the HexBytes that were allocated by the + FromHexString function. To be called at the end of + the program. +*/ +static void FreeHexBytes(void) +{ + while(_hdeferListIndex > 0) + { + free(_hdeferList[--_hdeferListIndex]); + _hdeferList[_hdeferListIndex] = NULL; + } +} + +/* +* Prints the value of the buffer as a hexadecimal string +* @param bytes The buffer to print +* @param len The length of the buffer +*/ +static void PrintHexRaw(void* bytes, size_t len) +{ + size_t i; + for (i = 0; i < len; i++) + { + printf("%02x", ((uint8_t*)bytes)[i]); + } + + puts("\n"); +} + +/* +* Prints the value of the HexBytes as a hexadecimal string +* @param hexBytes A pointer to the HexBytes structure to print the value of +*/ +static void PrintHexBytes(HexBytes* hexBytes) +{ + if (!hexBytes) + { + puts("NULL"); + } + else + { + PrintHexRaw(hexBytes->data, hexBytes->length); + } +} + + +#endif // !HEX_HELPERS_H + + |