HKDS: Heirarchal Key Derivation System 1.0.0.2 (A2)
A fast post-quantum secure replacement for DUKPT
utils.h
1/* 2025 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE: This software and all accompanying materials are the exclusive
5 * property of Quantum Resistant Cryptographic Solutions Corporation (QRCS).
6 * The intellectual and technical concepts contained within this implementation
7 * are proprietary to QRCS and its authorized licensors and are protected under
8 * applicable U.S. and international copyright, patent, and trade secret laws.
9 *
10 * CRYPTOGRAPHIC STANDARDS:
11 * - This software includes implementations of cryptographic algorithms such as
12 * SHA3, AES, and others. These algorithms are public domain or standardized
13 * by organizations such as NIST and are NOT the property of QRCS.
14 * - However, all source code, optimizations, and implementations in this library
15 * are original works of QRCS and are protected under this license.
16 *
17 * RESTRICTIONS:
18 * - Redistribution, modification, or unauthorized distribution of this software,
19 * in whole or in part, is strictly prohibited.
20 * - This software is provided for non-commercial, educational, and research
21 * purposes only. Commercial use in any form is expressly forbidden.
22 * - Licensing and authorized distribution are solely at the discretion of QRCS.
23 * - Any use of this software implies acceptance of these restrictions.
24 *
25 * DISCLAIMER:
26 * This software is provided "as is," without warranty of any kind, express or
27 * implied, including but not limited to warranties of merchantability or fitness
28 * for a particular purpose. QRCS disclaims all liability for any direct, indirect,
29 * incidental, or consequential damages resulting from the use or misuse of this software.
30 *
31 * FULL LICENSE:
32 * This software is subject to the **Quantum Resistant Cryptographic Solutions
33 * Proprietary License (QRCS-PL)**. The complete license terms are included
34 * in the LICENSE.txt file distributed with this software.
35 *
36 * Written by: John G. Underhill
37 * Contact: john.underhill@protonmail.com
38 */
39
40#ifndef HKDS_UTILS_H
41#define HKDS_UTILS_H
42
43#include "common.h"
44#include <time.h>
45#if defined(SYSTEM_OS_WINDOWS)
46# include <Windows.h>
47# if defined(SYSTEM_COMPILER_MSC)
48# pragma comment(lib, "advapi32.lib")
49# endif
50#else
51# include <sys/types.h>
52# include <sys/stat.h>
53# include <errno.h>
54# include <fcntl.h>
55# include <limits.h>
56# include <stdlib.h>
57# include <stdio.h>
58# include <sys/types.h>
59# include <unistd.h>
60# if !defined(O_NOCTTY)
61# define O_NOCTTY 0
62# endif
63#endif
64
65#if defined(__OpenBSD__) || defined(__CloudABI__) || defined(__wasi__)
66# define HAVE_SAFE_ARC4RANDOM
67#endif
68
74
79#define UTILS_TOKEN_NOT_FOUND -1
80
85#define UTILS_STRING_MAX_LEN 4096
86
91#define UTILS_CPUIDEX_SERIAL_SIZE 12
92
97#if defined(SYSTEM_OS_APPLE) && defined(SYSTEM_COMPILER_GCC)
98# define UTILS_CPUIDEX_VENDOR_SIZE 32
99#else
100# define UTILS_CPUIDEX_VENDOR_SIZE 12
101#endif
102
107typedef enum utils_cpu_maker
108{
109 hkds_cpuid_unknown = 0,
110 hkds_cpuid_amd = 1,
111 hkds_cpuid_intel = 2,
112 hkds_cpuid_via = 3,
113 hkds_cpuid_hygion = 4,
114} utils_cpu_maker;
115
120HKDS_EXPORT_API typedef struct utils_cpu_features
121{
122 bool adx;
123 bool aesni;
124 bool pcmul;
125
126 bool armv7;
127 bool neon;
128 bool sha256;
129 bool sha512;
130 bool sha3;
131
132 bool avx;
133 bool avx2;
134 bool avx512f;
136 bool rdrand;
137 bool rdtcsp;
138
139 uint32_t cacheline;
140 uint32_t cores;
141 uint32_t cpus;
142 uint32_t freqbase;
143 uint32_t freqmax;
144 uint32_t freqref;
145 uint32_t l1cache;
146 uint32_t l1cacheline;
147 uint32_t l2associative;
148 uint32_t l2cache;
149 char serial[UTILS_CPUIDEX_SERIAL_SIZE];
150 char vendor[UTILS_CPUIDEX_VENDOR_SIZE];
151 utils_cpu_maker cputype;
153
158#define UTILS_SEED_MAX 1024000
159
160/* pseudo-random generation */
161
169HKDS_EXPORT_API bool utils_seed_generate(uint8_t* output, size_t length);
170
171/* cpuid */
172
179HKDS_EXPORT_API bool utils_cpu_features_set(utils_cpu_features* const features);
180
181/* console functions */
182
190HKDS_EXPORT_API void utils_hex_to_bin(const char* hexstr, uint8_t* output, size_t length);
191
197HKDS_EXPORT_API void utils_print_safe(const char* input);
198
204HKDS_EXPORT_API void utils_print_line(const char* input);
205
206/* timer functions */
207
213HKDS_EXPORT_API uint64_t utils_stopwatch_start();
214
220HKDS_EXPORT_API uint64_t utils_stopwatch_elapsed(uint64_t start);
221
222/* string functions */
223
231HKDS_EXPORT_API int64_t utils_find_string(const char* source, const char* token);
232
240HKDS_EXPORT_API bool utils_string_contains(const char* source, const char* token);
241
248HKDS_EXPORT_API size_t utils_string_size(const char* source);
249
255HKDS_EXPORT_API void utils_string_to_lowercase(char* source);
256
257/* memory functions */
258
265HKDS_EXPORT_API void utils_memory_clear(void* output, size_t length);
266
272HKDS_EXPORT_API void utils_memory_aligned_free(void* block);
273
282HKDS_EXPORT_API void* utils_memory_aligned_alloc(int32_t align, size_t length);
283
293HKDS_EXPORT_API bool utils_memory_are_equal(const uint8_t* a, const uint8_t* b, size_t length);
294
303HKDS_EXPORT_API bool utils_memory_are_equal_128(const uint8_t* a, const uint8_t* b);
304
313HKDS_EXPORT_API bool utils_memory_are_equal_256(const uint8_t* a, const uint8_t* b);
314
323HKDS_EXPORT_API bool utils_memory_are_equal_512(const uint8_t* a, const uint8_t* b);
324
332HKDS_EXPORT_API void utils_memory_copy(void* output, const void* input, size_t length);
333
341HKDS_EXPORT_API void utils_memory_xor(uint8_t* output, const uint8_t* input, size_t length);
342
343/* integer functions */
350HKDS_EXPORT_API uint32_t utils_integer_be8to32(const uint8_t* input);
351
358HKDS_EXPORT_API void utils_integer_be32to8(uint8_t* output, uint32_t value);
359
366HKDS_EXPORT_API uint64_t utils_integer_le8to64(const uint8_t* input);
367
374HKDS_EXPORT_API void utils_integer_le64to8(uint8_t* output, uint64_t value);
375
382HKDS_EXPORT_API void utils_integer_be8increment(uint8_t* output, size_t otplen);
383
391HKDS_EXPORT_API uint64_t utils_integer_rotl64(uint64_t value, size_t shift);
392
401HKDS_EXPORT_API int32_t utils_integer_verify(const uint8_t* a, const uint8_t* b, size_t length);
402
403#endif
Contains the CPU feature availability.
Definition utils.h:121
bool avx512f
Definition utils.h:134
char serial[UTILS_CPUIDEX_SERIAL_SIZE]
Definition utils.h:149
uint32_t cpus
Definition utils.h:141
uint32_t freqref
Definition utils.h:144
uint32_t freqbase
Definition utils.h:142
bool adx
Definition utils.h:122
uint32_t l2associative
Definition utils.h:147
uint32_t cores
Definition utils.h:140
bool hyperthread
Definition utils.h:135
uint32_t l1cache
Definition utils.h:145
bool sha256
Definition utils.h:128
utils_cpu_maker cputype
Definition utils.h:151
bool sha3
Definition utils.h:130
uint32_t l2cache
Definition utils.h:148
bool avx
Definition utils.h:132
uint32_t freqmax
Definition utils.h:143
bool aesni
Definition utils.h:123
bool avx2
Definition utils.h:133
bool rdrand
Definition utils.h:136
bool armv7
Definition utils.h:126
bool sha512
Definition utils.h:129
uint32_t l1cacheline
Definition utils.h:146
char vendor[UTILS_CPUIDEX_VENDOR_SIZE]
Definition utils.h:150
uint32_t cacheline
Definition utils.h:139
bool rdtcsp
Definition utils.h:137
bool pcmul
Definition utils.h:124
bool neon
Definition utils.h:127