aboutsummaryrefslogtreecommitdiff
path: root/include/libbb.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/libbb.h294
1 files changed, 222 insertions, 72 deletions
diff --git a/include/libbb.h b/include/libbb.h
index bc1453e12..60037ed3d 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -281,6 +281,26 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
281# endif 281# endif
282#endif 282#endif
283 283
284#if ENABLE_FEATURE_TLS_SCHANNEL || ENABLE_FEATURE_USE_CNG_API
285# define SECURITY_WIN32
286# include <windows.h>
287# include <security.h>
288#endif
289
290#if ENABLE_FEATURE_USE_CNG_API
291# include <bcrypt.h>
292
293// these work on Windows >= 10
294# define BCRYPT_HMAC_SHA1_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x000000a1)
295# define BCRYPT_HMAC_SHA256_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x000000b1)
296# define sha1_begin_hmac BCRYPT_HMAC_SHA1_ALG_HANDLE
297# define sha256_begin_hmac BCRYPT_HMAC_SHA256_ALG_HANDLE
298#else
299# define sha1_begin_hmac sha1_begin
300# define sha256_begin_hmac sha256_begin
301# define hmac_uninit(...) ((void)0)
302#endif
303
284/* Tested to work correctly with all int types (IIRC :]) */ 304/* Tested to work correctly with all int types (IIRC :]) */
285#define MAXINT(T) (T)( \ 305#define MAXINT(T) (T)( \
286 ((T)-1) > 0 \ 306 ((T)-1) > 0 \
@@ -899,7 +919,36 @@ struct hostent *xgethostbyname(const char *name) FAST_FUNC;
899// Also mount.c and inetd.c are using gethostbyname(), 919// Also mount.c and inetd.c are using gethostbyname(),
900// + inet_common.c has additional IPv4-only stuff 920// + inet_common.c has additional IPv4-only stuff
901 921
922#if defined CONFIG_FEATURE_TLS_SCHANNEL
923typedef struct tls_state {
924 int ofd;
925 int ifd;
926
927 // handles
928 CredHandle cred_handle;
929 CtxtHandle ctx_handle;
930
931 // buffers
932 char in_buffer[16384 + 256]; // input buffer (to read from server)
933 unsigned long in_buffer_size; // amount of data currently in input buffer
934
935 char *out_buffer; // output buffer (for decrypted data), this is essentially the same as input buffer as data is decrypted in place
936 unsigned long out_buffer_size; // amount of data currently in output buffer
937 unsigned long out_buffer_used; // amount of extra data currently in output buffer
902 938
939 // data
940 char *hostname;
941 SecPkgContext_StreamSizes stream_sizes;
942
943 // booleans
944
945 // context initialized
946 int initialized;
947
948 // closed by remote peer
949 int closed;
950} tls_state_t;
951#else
903struct tls_aes { 952struct tls_aes {
904 uint32_t key[60]; 953 uint32_t key[60];
905 unsigned rounds; 954 unsigned rounds;
@@ -956,12 +1005,14 @@ typedef struct tls_state {
956 struct tls_aes aes_decrypt; 1005 struct tls_aes aes_decrypt;
957 uint8_t H[16]; //used by AES_GCM 1006 uint8_t H[16]; //used by AES_GCM
958} tls_state_t; 1007} tls_state_t;
1008#endif
959 1009
960static inline tls_state_t *new_tls_state(void) 1010static inline tls_state_t *new_tls_state(void)
961{ 1011{
962 tls_state_t *tls = xzalloc(sizeof(*tls)); 1012 tls_state_t *tls = xzalloc(sizeof(*tls));
963 return tls; 1013 return tls;
964} 1014}
1015
965void tls_handshake(tls_state_t *tls, const char *sni) FAST_FUNC; 1016void tls_handshake(tls_state_t *tls, const char *sni) FAST_FUNC;
966#define TLSLOOP_EXIT_ON_LOCAL_EOF (1 << 0) 1017#define TLSLOOP_EXIT_ON_LOCAL_EOF (1 << 0)
967void tls_run_copy_loop(tls_state_t *tls, unsigned flags) FAST_FUNC; 1018void tls_run_copy_loop(tls_state_t *tls, unsigned flags) FAST_FUNC;
@@ -1071,13 +1122,13 @@ unsigned bb_clk_tck(void) FAST_FUNC;
1071 1122
1072#if SEAMLESS_COMPRESSION 1123#if SEAMLESS_COMPRESSION
1073/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */ 1124/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
1074int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC; 1125int setup_unzip_on_fd(int fd, int die_if_not_compressed) FAST_FUNC;
1075/* Autodetects .gz etc */ 1126/* Autodetects .gz etc */
1076extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC; 1127extern int open_zipped(const char *fname, int die_if_not_compressed) FAST_FUNC;
1077extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; 1128extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
1078#else 1129#else
1079# define setup_unzip_on_fd(...) (0) 1130# define setup_unzip_on_fd(...) (0)
1080# define open_zipped(fname, fail_if_not_compressed) open((fname), O_RDONLY); 1131# define open_zipped(fname, die_if_not_compressed) open((fname), O_RDONLY);
1081# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p)) 1132# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p))
1082#endif 1133#endif
1083/* lzma has no signature, need a little helper. NB: exist only for ENABLE_FEATURE_SEAMLESS_LZMA=y */ 1134/* lzma has no signature, need a little helper. NB: exist only for ENABLE_FEATURE_SEAMLESS_LZMA=y */
@@ -1173,6 +1224,32 @@ char *bin2hex(char *dst, const char *src, int count) FAST_FUNC;
1173/* Reverse */ 1224/* Reverse */
1174char* hex2bin(char *dst, const char *src, int count) FAST_FUNC; 1225char* hex2bin(char *dst, const char *src, int count) FAST_FUNC;
1175 1226
1227/* Returns strlen as a bonus */
1228//size_t replace_char(char *s, char what, char with) FAST_FUNC;
1229static inline size_t replace_char(char *str, char from, char to)
1230{
1231 char *p = str;
1232 while (*p) {
1233 if (*p == from)
1234 *p = to;
1235 p++;
1236 }
1237 return p - str;
1238}
1239
1240extern const char c_escape_conv_str00[];
1241#define c_escape_conv_str07 (c_escape_conv_str00+3)
1242
1243void FAST_FUNC xorbuf_3(void *dst, const void *src1, const void *src2, unsigned count);
1244void FAST_FUNC xorbuf(void* buf, const void* mask, unsigned count);
1245void FAST_FUNC xorbuf16_aligned_long(void* buf, const void* mask);
1246void FAST_FUNC xorbuf64_3_aligned64(void *dst, const void *src1, const void *src2);
1247#if BB_UNALIGNED_MEMACCESS_OK
1248# define xorbuf16(buf,mask) xorbuf16_aligned_long(buf,mask)
1249#else
1250void FAST_FUNC xorbuf16(void* buf, const void* mask);
1251#endif
1252
1176/* Generate a UUID */ 1253/* Generate a UUID */
1177void generate_uuid(uint8_t *buf) FAST_FUNC; 1254void generate_uuid(uint8_t *buf) FAST_FUNC;
1178 1255
@@ -1887,18 +1964,25 @@ extern char *pw_encrypt(const char *clear, const char *salt, int cleanup) FAST_F
1887extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC; 1964extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC;
1888/* 1965/*
1889 * rnd is additional random input. New one is returned. 1966 * rnd is additional random input. New one is returned.
1890 * Useful if you call crypt_make_salt many times in a row: 1967 * Useful if you call crypt_make_rand64encoded many times in a row:
1891 * rnd = crypt_make_salt(buf1, 4, 0); 1968 * rnd = crypt_make_rand64encoded(buf1, 4, 0);
1892 * rnd = crypt_make_salt(buf2, 4, rnd); 1969 * rnd = crypt_make_rand64encoded(buf2, 4, rnd);
1893 * rnd = crypt_make_salt(buf3, 4, rnd); 1970 * rnd = crypt_make_rand64encoded(buf3, 4, rnd);
1894 * (otherwise we risk having same salt generated) 1971 * (otherwise we risk having same salt generated)
1895 */ 1972 */
1896extern int crypt_make_salt(char *p, int cnt /*, int rnd*/) FAST_FUNC; 1973extern int crypt_make_rand64encoded(char *p, int cnt /*, int rnd*/) FAST_FUNC;
1897/* "$N$" + sha_salt_16_bytes + NUL */ 1974/* Size of char salt[] to hold randomly-generated salt string
1898#define MAX_PW_SALT_LEN (3 + 16 + 1) 1975 * sha256/512:
1976 * "$5$" ["rounds=999999999$"] "<sha_salt_16_chars><NUL>"
1977 * "$6$" ["rounds=999999999$"] "<sha_salt_16_chars><NUL>"
1978 * #define MAX_PW_SALT_LEN (3 + sizeof("rounds=999999999$")-1 + 16 + 1)
1979 * yescrypt:
1980 * "$y$" <up to 8 params of up to 6 chars each> "$" <up to 86 chars salt><NUL>
1981 * (86 chars are ascii64-encoded 64 binary bytes)
1982 */
1983#define MAX_PW_SALT_LEN (3 + 8*6 + 1 + 86 + 1)
1899extern char* crypt_make_pw_salt(char p[MAX_PW_SALT_LEN], const char *algo) FAST_FUNC; 1984extern char* crypt_make_pw_salt(char p[MAX_PW_SALT_LEN], const char *algo) FAST_FUNC;
1900 1985
1901
1902/* Returns number of lines changed, or -1 on error */ 1986/* Returns number of lines changed, or -1 on error */
1903#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP) 1987#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
1904#define update_passwd(filename, username, data, member) \ 1988#define update_passwd(filename, username, data, member) \
@@ -2041,6 +2125,10 @@ int64_t windows_read_key(int fd, char *buffer, int timeout) FAST_FUNC;
2041int64_t safe_read_key(int fd, char *buffer, int timeout) FAST_FUNC; 2125int64_t safe_read_key(int fd, char *buffer, int timeout) FAST_FUNC;
2042void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC; 2126void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC;
2043 2127
2128int check_got_signal_and_poll(struct pollfd pfd[1], int timeout) FAST_FUNC;
2129#if ENABLE_PLATFORM_MINGW32
2130# define check_got_signal_and_poll(p, t) poll(p, 1, t)
2131#endif
2044 2132
2045#if ENABLE_FEATURE_EDITING 2133#if ENABLE_FEATURE_EDITING
2046/* It's NOT just ENABLEd or disabled. It's a number: */ 2134/* It's NOT just ENABLEd or disabled. It's a number: */
@@ -2087,7 +2175,7 @@ typedef struct line_input_t {
2087# if MAX_HISTORY 2175# if MAX_HISTORY
2088 int cnt_history; 2176 int cnt_history;
2089 int cur_history; 2177 int cur_history;
2090 int max_history; /* must never be <= 0 */ 2178 int max_history; /* must never be < 0 */
2091# if ENABLE_FEATURE_EDITING_SAVEHISTORY 2179# if ENABLE_FEATURE_EDITING_SAVEHISTORY
2092 /* meaning of this field depends on FEATURE_EDITING_SAVE_ON_EXIT: 2180 /* meaning of this field depends on FEATURE_EDITING_SAVE_ON_EXIT:
2093 * if !FEATURE_EDITING_SAVE_ON_EXIT: "how many lines are 2181 * if !FEATURE_EDITING_SAVE_ON_EXIT: "how many lines are
@@ -2153,33 +2241,6 @@ enum { COMM_LEN = 16 };
2153# endif 2241# endif
2154#endif 2242#endif
2155 2243
2156struct smaprec {
2157 unsigned long mapped_rw;
2158 unsigned long mapped_ro;
2159 unsigned long shared_clean;
2160 unsigned long shared_dirty;
2161 unsigned long private_clean;
2162 unsigned long private_dirty;
2163 unsigned long stack;
2164 unsigned long smap_pss, smap_swap;
2165 unsigned long smap_size;
2166 // For mixed 32/64 userspace, 32-bit pmap still needs
2167 // 64-bit field here to correctly show 64-bit processes:
2168 unsigned long long smap_start;
2169 // (strictly speaking, other fields need to be wider too,
2170 // but they are in kbytes, not bytes, and they hold sizes,
2171 // not start addresses, sizes tend to be less than 4 terabytes)
2172 char smap_mode[5];
2173 char *smap_name;
2174};
2175
2176#if !ENABLE_PMAP
2177#define procps_read_smaps(pid, total, cb, data) \
2178 procps_read_smaps(pid, total)
2179#endif
2180int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
2181 void (*cb)(struct smaprec *, void *), void *data);
2182
2183typedef struct procps_status_t { 2244typedef struct procps_status_t {
2184#if !ENABLE_PLATFORM_MINGW32 2245#if !ENABLE_PLATFORM_MINGW32
2185 DIR *dir; 2246 DIR *dir;
@@ -2215,7 +2276,13 @@ typedef struct procps_status_t {
2215#endif 2276#endif
2216 unsigned tty_major,tty_minor; 2277 unsigned tty_major,tty_minor;
2217#if ENABLE_FEATURE_TOPMEM 2278#if ENABLE_FEATURE_TOPMEM
2218 struct smaprec smaps; 2279 unsigned long mapped_rw;
2280 unsigned long mapped_ro;
2281 unsigned long shared_clean;
2282 unsigned long shared_dirty;
2283 unsigned long private_clean;
2284 unsigned long private_dirty;
2285 unsigned long stack;
2219#endif 2286#endif
2220 char state[4]; 2287 char state[4];
2221 /* basename of executable in exec(2), read from /proc/N/stat 2288 /* basename of executable in exec(2), read from /proc/N/stat
@@ -2264,11 +2331,15 @@ void free_procps_scan(procps_status_t* sp) FAST_FUNC;
2264procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC; 2331procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC;
2265/* Format cmdline (up to col chars) into char buf[size] */ 2332/* Format cmdline (up to col chars) into char buf[size] */
2266/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */ 2333/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
2267void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC; 2334int read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC;
2268pid_t *find_pid_by_name(const char* procName) FAST_FUNC; 2335pid_t *find_pid_by_name(const char* procName) FAST_FUNC;
2269pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC; 2336pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC;
2270int starts_with_cpu(const char *str) FAST_FUNC; 2337int starts_with_cpu(const char *str) FAST_FUNC;
2271unsigned get_cpu_count(void) FAST_FUNC; 2338unsigned get_cpu_count(void) FAST_FUNC;
2339/* Some internals reused by pmap: */
2340unsigned long FAST_FUNC fast_strtoul_10(char **endptr);
2341unsigned long long FAST_FUNC fast_strtoull_16(char **endptr);
2342char* FAST_FUNC skip_fields(char *str, int count);
2272 2343
2273 2344
2274/* Use strict=1 if you process input from untrusted source: 2345/* Use strict=1 if you process input from untrusted source:
@@ -2294,6 +2365,56 @@ char *decode_base64(char *dst, const char **pp_src) FAST_FUNC;
2294char *decode_base32(char *dst, const char **pp_src) FAST_FUNC; 2365char *decode_base32(char *dst, const char **pp_src) FAST_FUNC;
2295void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC; 2366void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC;
2296 2367
2368int FAST_FUNC i2a64(int i);
2369int FAST_FUNC a2i64(char c);
2370char* FAST_FUNC num2str64_lsb_first(char *s, unsigned v, int n);
2371
2372enum {
2373 /* how many bytes XYZ_end() fills */
2374 MD5_OUTSIZE = 16,
2375 SHA1_OUTSIZE = 20,
2376 SHA256_OUTSIZE = 32,
2377 SHA384_OUTSIZE = 48,
2378 SHA512_OUTSIZE = 64,
2379 //SHA3-224_OUTSIZE = 28,
2380 /* size of input block */
2381 SHA2_INSIZE = 64,
2382};
2383
2384#if defined CONFIG_FEATURE_USE_CNG_API
2385struct bcrypt_hash_ctx_t {
2386 void *handle;
2387 void *hash_obj;
2388 unsigned int output_size;
2389};
2390typedef struct bcrypt_hash_ctx_t md5_ctx_t;
2391typedef struct bcrypt_hash_ctx_t sha1_ctx_t;
2392typedef struct bcrypt_hash_ctx_t sha256_ctx_t;
2393typedef struct bcrypt_hash_ctx_t sha384_ctx_t;
2394typedef struct bcrypt_hash_ctx_t sha512_ctx_t;
2395typedef struct sha3_ctx_t {
2396 uint64_t state[25];
2397 unsigned bytes_queued;
2398 unsigned input_block_bytes;
2399} sha3_ctx_t;
2400void md5_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2401void sha1_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2402void sha256_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2403void sha384_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2404void sha512_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2405void generic_hash(struct bcrypt_hash_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
2406unsigned generic_end(struct bcrypt_hash_ctx_t *ctx, void *resbuf) FAST_FUNC;
2407# define md5_hash generic_hash
2408# define sha1_hash generic_hash
2409# define sha256_hash generic_hash
2410# define sha384_hash generic_hash
2411# define sha512_hash generic_hash
2412# define md5_end generic_end
2413# define sha1_end generic_end
2414# define sha256_end generic_end
2415# define sha384_end generic_end
2416# define sha512_end generic_end
2417#else
2297typedef struct md5_ctx_t { 2418typedef struct md5_ctx_t {
2298 uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */ 2419 uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
2299 void (*process_block)(struct md5_ctx_t*) FAST_FUNC; 2420 void (*process_block)(struct md5_ctx_t*) FAST_FUNC;
@@ -2307,6 +2428,7 @@ typedef struct sha512_ctx_t {
2307 uint64_t hash[8]; 2428 uint64_t hash[8];
2308 uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */ 2429 uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */
2309} sha512_ctx_t; 2430} sha512_ctx_t;
2431typedef struct sha512_ctx_t sha384_ctx_t;
2310typedef struct sha3_ctx_t { 2432typedef struct sha3_ctx_t {
2311 uint64_t state[25]; 2433 uint64_t state[25];
2312 unsigned bytes_queued; 2434 unsigned bytes_queued;
@@ -2324,20 +2446,69 @@ void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
2324void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC; 2446void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
2325void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; 2447void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
2326unsigned sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC; 2448unsigned sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC;
2449void sha384_begin(sha384_ctx_t *ctx) FAST_FUNC;
2450#define sha384_hash sha512_hash
2451unsigned sha384_end(sha384_ctx_t *ctx, void *resbuf) FAST_FUNC;
2452#endif
2327void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC; 2453void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC;
2328void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; 2454void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
2329unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC; 2455unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC;
2456void FAST_FUNC sha256_block(const void *in, size_t len, uint8_t hash[32]);
2330/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */ 2457/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */
2458#if defined CONFIG_FEATURE_USE_CNG_API
2459typedef struct bcrypt_hash_ctx_t md5sha_ctx_t;
2460#define md5sha_hash generic_hash
2461#define sha_end generic_end
2462#else
2331typedef struct md5_ctx_t md5sha_ctx_t; 2463typedef struct md5_ctx_t md5sha_ctx_t;
2332#define md5sha_hash md5_hash 2464#define md5sha_hash md5_hash
2333#define sha_end sha1_end 2465#define sha_end sha1_end
2334enum { 2466#endif
2335 MD5_OUTSIZE = 16, 2467
2336 SHA1_OUTSIZE = 20, 2468/* RFC 2104 HMAC (hash-based message authentication code) */
2337 SHA256_OUTSIZE = 32, 2469#if !ENABLE_FEATURE_USE_CNG_API
2338 SHA512_OUTSIZE = 64, 2470typedef struct hmac_ctx {
2339 SHA3_OUTSIZE = 28, 2471 md5sha_ctx_t hashed_key_xor_ipad;
2340}; 2472 md5sha_ctx_t hashed_key_xor_opad;
2473} hmac_ctx_t;
2474#else
2475typedef struct bcrypt_hash_ctx_t hmac_ctx_t;
2476#endif
2477#define HMAC_ONLY_SHA256 (!ENABLE_FEATURE_TLS_SHA1)
2478typedef void md5sha_begin_func(md5sha_ctx_t *ctx) FAST_FUNC;
2479#if !ENABLE_FEATURE_USE_CNG_API
2480#if HMAC_ONLY_SHA256
2481#define hmac_begin(ctx,key,key_size,begin) \
2482 hmac_begin(ctx,key,key_size)
2483#endif
2484void FAST_FUNC hmac_begin(hmac_ctx_t *ctx, const uint8_t *key, unsigned key_size, md5sha_begin_func *begin);
2485static ALWAYS_INLINE void hmac_hash(hmac_ctx_t *ctx, const void *in, size_t len)
2486{
2487 md5sha_hash(&ctx->hashed_key_xor_ipad, in, len);
2488}
2489#else
2490# if HMAC_ONLY_SHA256
2491# define hmac_begin(pre,key,key_size,begin) \
2492 _hmac_begin(pre, key, key_size, sha256_begin_hmac)
2493# else
2494# define hmac_begin _hmac_begin
2495# endif
2496void _hmac_begin(hmac_ctx_t *pre, uint8_t *key, unsigned key_size,
2497 BCRYPT_ALG_HANDLE alg_handle);
2498void hmac_uninit(hmac_ctx_t *pre);
2499#endif
2500unsigned FAST_FUNC hmac_end(hmac_ctx_t *ctx, uint8_t *out);
2501#if HMAC_ONLY_SHA256
2502#define hmac_block(key,key_size,begin,in,sz,out) \
2503 hmac_block(key,key_size,in,sz,out)
2504#endif
2505unsigned FAST_FUNC hmac_block(const uint8_t *key, unsigned key_size,
2506 md5sha_begin_func *begin,
2507 const void *in, unsigned sz,
2508 uint8_t *out);
2509/* HMAC helpers for TLS: */
2510void FAST_FUNC hmac_hash_v(hmac_ctx_t *ctx, va_list va);
2511unsigned hmac_peek_hash(hmac_ctx_t *ctx, uint8_t *out, ...);
2341 2512
2342extern uint32_t *global_crc32_table; 2513extern uint32_t *global_crc32_table;
2343uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC; 2514uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;
@@ -2473,31 +2644,10 @@ extern struct globals *BB_GLOBAL_CONST ptr_to_globals;
2473#define barrier() asm volatile ("":::"memory") 2644#define barrier() asm volatile ("":::"memory")
2474 2645
2475#if defined(__clang_major__) && __clang_major__ >= 9 2646#if defined(__clang_major__) && __clang_major__ >= 9
2476/* Clang/llvm drops assignment to "constant" storage. Silently. 2647/* {ASSIGN,XZALLOC}_CONST_PTR() are out-of-line functions
2477 * Needs serious convincing to not eliminate the store. 2648 * to prevent clang from reading pointer before it is assigned.
2478 */
2479static ALWAYS_INLINE void* not_const_pp(const void *p)
2480{
2481 void *pp;
2482 asm volatile (
2483 "# forget that p points to const"
2484 : /*outputs*/ "=r" (pp)
2485 : /*inputs*/ "0" (p)
2486 );
2487 return pp;
2488}
2489# if !ENABLE_PLATFORM_MINGW32
2490# define ASSIGN_CONST_PTR(pptr, v) do { \
2491 *(void**)not_const_pp(pptr) = (void*)(v); \
2492 barrier(); \
2493} while (0)
2494#else
2495/* On Windows it seems necessary for this to be a function too. */
2496void ASSIGN_CONST_PTR(const void *pptr, const void *ptr) FAST_FUNC;
2497#endif
2498/* XZALLOC_CONST_PTR() is an out-of-line function to prevent
2499 * clang from reading pointer before it is assigned.
2500 */ 2649 */
2650void ASSIGN_CONST_PTR(const void *pptr, void *v) FAST_FUNC;
2501void XZALLOC_CONST_PTR(const void *pptr, size_t size) FAST_FUNC; 2651void XZALLOC_CONST_PTR(const void *pptr, size_t size) FAST_FUNC;
2502#else 2652#else
2503# define ASSIGN_CONST_PTR(pptr, v) do { \ 2653# define ASSIGN_CONST_PTR(pptr, v) do { \