1 /* 2 * ck - C11 Annex K wrappers (selected functions; not complete) 3 * 4 * ck is also an abbreviation for "check". 5 * These are validating, checking functions. 6 * 7 * Copyright(c) 2016,2021 Glenn Strauss gstrauss()gluelogic.com All rights reserved 8 * License: BSD 3-clause (same as lighttpd) 9 */ 10 #ifndef INCLUDED_CK_H 11 #define INCLUDED_CK_H 12 #ifndef __STDC_WANT_LIB_EXT1__ /*(enable C11 Annex K ext1 *_s functions)*/ 13 #define __STDC_WANT_LIB_EXT1__ 1 14 #endif 15 #include "first.h" 16 #ifdef __FreeBSD__ 17 #ifndef _RSIZE_T_DEFINED /* expecting __EXT1_VISIBLE 1 and _RSIZE_T_DEFINED */ 18 #define _RSIZE_T_DEFINED 19 typedef size_t rsize_t; 20 #endif 21 #include <errno.h> 22 #endif 23 24 __BEGIN_DECLS 25 26 27 #ifndef RSIZE_MAX 28 #define RSIZE_MAX (SIZE_MAX >> 1) 29 typedef size_t rsize_t; 30 typedef int errno_t; 31 #endif 32 33 34 errno_t ck_getenv_s (size_t * restrict len, char * restrict value, rsize_t maxsize, const char * restrict name); 35 36 /*(ck_memclear_s() is not from C11 Annex K 37 * ck_memclear_s() is similar to memset_s() using constant byte 0 for fill)*/ 38 errno_t ck_memclear_s (void *s, rsize_t smax, rsize_t n); 39 40 /*(ck_memzero() is not from C11 Annex K 41 * ck_memzero() is a convenience wrapper around ck_memclear_s())*/ 42 static inline errno_t ck_memzero(void *s, rsize_t n); 43 static inline errno_t ck_memzero(void *s, rsize_t n) { 44 return ck_memclear_s(s, n, n); 45 } 46 47 errno_t ck_strerror_s (char *s, rsize_t maxsize, errno_t errnum); 48 49 /*(ck_memeq_const_time() is not from C11 Annex K) 50 * constant time memory compare for equality 51 * rounds to next multiple of 64 to avoid potentially leaking exact 52 * string lengths when subject to high precision timing attacks */ 53 __attribute_nonnull__ 54 int ck_memeq_const_time (const void *a, size_t alen, const void *b, size_t blen); 55 56 /*(ck_memeq_const_time_fixed_len() is not from C11 Annex K) 57 * constant time memory compare for equality for fixed len (e.g. digests) 58 * (padding not necessary for digests, which have fixed, defined lengths) */ 59 __attribute_nonnull__ 60 int ck_memeq_const_time_fixed_len (const void *a, const void *b, size_t len); 61 62 63 /*(ck_bt() is not from C11 Annex K) 64 * ck_bt() prints backtrace to stderr */ 65 __attribute_cold__ 66 __attribute_nonnull__ 67 void ck_bt(const char *filename, unsigned int line, const char *msg); 68 69 /*(ck_bt_abort() is not from C11 Annex K) 70 * ck_bt_abort() prints backtrace to stderr and calls abort() */ 71 __attribute_cold__ 72 __attribute_nonnull__ 73 __attribute_noreturn__ 74 void ck_bt_abort(const char *filename, unsigned int line, const char *msg); 75 76 /*(ck_assert() and ck_assert_failed() are not from C11 Annex K) 77 * ck_assert() executes a runtime assertion test or aborts 78 * ck_assert() *is not* optimized away if defined(NDEBUG) 79 * (unlike standard assert(), which *is* optimized away if defined(NDEBUG)) */ 80 __attribute_cold__ 81 __attribute_nonnull__ 82 __attribute_noreturn__ 83 void ck_assert_failed(const char *filename, unsigned int line, const char *msg); 84 85 #define ck_assert(x) \ 86 do { if (!(x)) ck_assert_failed(__FILE__, __LINE__, #x); } while (0) 87 88 89 __END_DECLS 90 91 92 #endif 93