aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorrfl890 <rfl890mc@gmail.com>2026-01-26 04:29:31 -0500
committerRon Yorston <rmy@pobox.com>2026-01-27 12:45:11 +0000
commit74344deef28da6fa279eb12ab8ab4e84a59900ad (patch)
treeb3380afd00fa75654df86e50c31f4b3bc7770321 /include
parentf0cd749ff7ad15665a5d03e58eb73ef48f60381d (diff)
downloadbusybox-w32-74344deef28da6fa279eb12ab8ab4e84a59900ad.tar.gz
busybox-w32-74344deef28da6fa279eb12ab8ab4e84a59900ad.tar.bz2
busybox-w32-74344deef28da6fa279eb12ab8ab4e84a59900ad.zip
libbb: refactor CNG hashing implementation
The existing CNG implementation relied on multiple features only implemented in "newer" Windows versions. This new implementation does not use the aforementioned features and is therefore compatible with systems running Windows Vista or higher. function old new delta .rdata 87112 87288 +176 hmac_peek_hash 80 208 +128 get_alg_handle - 96 +96 algorithm_provider_hmac_cache - 48 +48 algorithm_provider_cache - 48 +48 alg_id_mappings - 48 +48 sha512_begin 16 48 +32 sha384_begin 16 48 +32 sha256_begin 16 48 +32 sha1_begin 16 48 +32 md5_begin 16 32 +16 hmac_hash_v 48 64 +16 hmac_end 48 64 +16 hmac_blocks.constprop.0 96 112 +16 __imp_BCryptOpenAlgorithmProvider - 8 +8 __imp_BCryptDuplicateHash - 8 +8 BCryptOpenAlgorithmProvider - 8 +8 BCryptDuplicateHash - 8 +8 ------------------------------------------------------------------------------ (add/remove: 8/0 grow/shrink: 10/0 up/down: 768/0) Total: 768 bytes
Diffstat (limited to 'include')
-rw-r--r--include/libbb.h51
1 files changed, 30 insertions, 21 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 09ebad1f6..1c75df523 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -290,11 +290,8 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
290#if ENABLE_FEATURE_USE_CNG_API 290#if ENABLE_FEATURE_USE_CNG_API
291# include <bcrypt.h> 291# include <bcrypt.h>
292 292
293// these work on Windows >= 10 293# define sha1_begin_hmac (get_alg_handle(CNG_ALG_ID_SHA1, true))
294# define BCRYPT_HMAC_SHA1_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x000000a1) 294# define sha256_begin_hmac (get_alg_handle(CNG_ALG_ID_SHA256, true))
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 295#else
299# define sha1_begin_hmac sha1_begin 296# define sha1_begin_hmac sha1_begin
300# define sha256_begin_hmac sha256_begin 297# define sha256_begin_hmac sha256_begin
@@ -2388,28 +2385,37 @@ enum {
2388}; 2385};
2389 2386
2390#if defined CONFIG_FEATURE_USE_CNG_API 2387#if defined CONFIG_FEATURE_USE_CNG_API
2391struct bcrypt_hash_ctx_t { 2388enum cng_algorithm_identifier {
2389 CNG_ALG_ID_MD5 = 0,
2390 CNG_ALG_ID_SHA1 = 1,
2391 CNG_ALG_ID_SHA256 = 2,
2392 CNG_ALG_ID_SHA384 = 3,
2393 CNG_ALG_ID_SHA512 = 4
2394};
2395BCRYPT_ALG_HANDLE get_alg_handle(enum cng_algorithm_identifier algorithm_identifier, bool hmac);
2396
2397typedef struct bcrypt_hash_ctx {
2392 void *handle; 2398 void *handle;
2393 void *hash_obj; 2399 void *hash_obj;
2394 unsigned int output_size; 2400 unsigned int output_size;
2395}; 2401} bcrypt_hash_ctx_t;
2396typedef struct bcrypt_hash_ctx_t md5_ctx_t; 2402typedef struct bcrypt_hash_ctx md5_ctx_t;
2397typedef struct bcrypt_hash_ctx_t sha1_ctx_t; 2403typedef struct bcrypt_hash_ctx sha1_ctx_t;
2398typedef struct bcrypt_hash_ctx_t sha256_ctx_t; 2404typedef struct bcrypt_hash_ctx sha256_ctx_t;
2399typedef struct bcrypt_hash_ctx_t sha384_ctx_t; 2405typedef struct bcrypt_hash_ctx sha384_ctx_t;
2400typedef struct bcrypt_hash_ctx_t sha512_ctx_t; 2406typedef struct bcrypt_hash_ctx sha512_ctx_t;
2401typedef struct sha3_ctx_t { 2407typedef struct sha3_ctx_t {
2402 uint64_t state[25]; 2408 uint64_t state[25];
2403 unsigned bytes_queued; 2409 unsigned bytes_queued;
2404 unsigned input_block_bytes; 2410 unsigned input_block_bytes;
2405} sha3_ctx_t; 2411} sha3_ctx_t;
2406void md5_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC; 2412void md5_begin(struct bcrypt_hash_ctx *ctx) FAST_FUNC;
2407void sha1_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC; 2413void sha1_begin(struct bcrypt_hash_ctx *ctx) FAST_FUNC;
2408void sha256_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC; 2414void sha256_begin(struct bcrypt_hash_ctx *ctx) FAST_FUNC;
2409void sha384_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC; 2415void sha384_begin(struct bcrypt_hash_ctx *ctx) FAST_FUNC;
2410void sha512_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC; 2416void sha512_begin(struct bcrypt_hash_ctx *ctx) FAST_FUNC;
2411void generic_hash(struct bcrypt_hash_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; 2417void generic_hash(struct bcrypt_hash_ctx *ctx, const void *buffer, size_t len) FAST_FUNC;
2412unsigned generic_end(struct bcrypt_hash_ctx_t *ctx, void *resbuf) FAST_FUNC; 2418unsigned generic_end(struct bcrypt_hash_ctx *ctx, void *resbuf) FAST_FUNC;
2413# define md5_hash generic_hash 2419# define md5_hash generic_hash
2414# define sha1_hash generic_hash 2420# define sha1_hash generic_hash
2415# define sha256_hash generic_hash 2421# define sha256_hash generic_hash
@@ -2462,7 +2468,7 @@ unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC;
2462void FAST_FUNC sha256_block(const void *in, size_t len, uint8_t hash[32]); 2468void FAST_FUNC sha256_block(const void *in, size_t len, uint8_t hash[32]);
2463/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */ 2469/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */
2464#if defined CONFIG_FEATURE_USE_CNG_API 2470#if defined CONFIG_FEATURE_USE_CNG_API
2465typedef struct bcrypt_hash_ctx_t md5sha_ctx_t; 2471typedef struct bcrypt_hash_ctx md5sha_ctx_t;
2466#define md5sha_hash generic_hash 2472#define md5sha_hash generic_hash
2467#define sha_end generic_end 2473#define sha_end generic_end
2468#else 2474#else
@@ -2478,7 +2484,10 @@ typedef struct hmac_ctx {
2478 md5sha_ctx_t hashed_key_xor_opad; 2484 md5sha_ctx_t hashed_key_xor_opad;
2479} hmac_ctx_t; 2485} hmac_ctx_t;
2480#else 2486#else
2481typedef struct bcrypt_hash_ctx_t hmac_ctx_t; 2487typedef struct hmac_ctx {
2488 BCRYPT_ALG_HANDLE alg_handle;
2489 bcrypt_hash_ctx_t hash_ctx;
2490} hmac_ctx_t;
2482#endif 2491#endif
2483#define HMAC_ONLY_SHA256 (!ENABLE_FEATURE_TLS_SHA1) 2492#define HMAC_ONLY_SHA256 (!ENABLE_FEATURE_TLS_SHA1)
2484typedef void md5sha_begin_func(md5sha_ctx_t *ctx) FAST_FUNC; 2493typedef void md5sha_begin_func(md5sha_ctx_t *ctx) FAST_FUNC;