diff options
| author | Ron Yorston <rmy@pobox.com> | 2026-01-28 08:03:02 +0000 |
|---|---|---|
| committer | Ron Yorston <rmy@pobox.com> | 2026-01-28 08:03:02 +0000 |
| commit | 04eadd2f424f491b5c77f4a247dc0b6da3b9aef2 (patch) | |
| tree | 3f80081642b7ce0bff2efbcc5c596a9090d630c2 | |
| parent | 67b5d0f248461a3c5db2d29d370e650ac95f3f05 (diff) | |
| download | busybox-w32-04eadd2f424f491b5c77f4a247dc0b6da3b9aef2.tar.gz busybox-w32-04eadd2f424f491b5c77f4a247dc0b6da3b9aef2.tar.bz2 busybox-w32-04eadd2f424f491b5c77f4a247dc0b6da3b9aef2.zip | |
libbb: code shrink
Fix some compiler warnings and move the CNG algorithm caches into
get_alg_handle().
Saves 32 bytes in the x86_64 build with CNG enabled.
| -rw-r--r-- | libbb/hash_hmac.c | 2 | ||||
| -rw-r--r-- | libbb/hash_md5_sha.c | 44 |
2 files changed, 30 insertions, 16 deletions
diff --git a/libbb/hash_hmac.c b/libbb/hash_hmac.c index 8cda2590a..3f8ea0252 100644 --- a/libbb/hash_hmac.c +++ b/libbb/hash_hmac.c | |||
| @@ -169,13 +169,13 @@ void FAST_FUNC hmac_hash_v(hmac_ctx_t *ctx, va_list va) | |||
| 169 | */ | 169 | */ |
| 170 | unsigned hmac_peek_hash(hmac_ctx_t *ctx, uint8_t *out, ...) | 170 | unsigned hmac_peek_hash(hmac_ctx_t *ctx, uint8_t *out, ...) |
| 171 | { | 171 | { |
| 172 | va_list va; | ||
| 172 | #if !ENABLE_FEATURE_USE_CNG_API | 173 | #if !ENABLE_FEATURE_USE_CNG_API |
| 173 | hmac_ctx_t tmpctx = *ctx; /* struct copy */ | 174 | hmac_ctx_t tmpctx = *ctx; /* struct copy */ |
| 174 | #else | 175 | #else |
| 175 | hmac_ctx_t tmpctx; | 176 | hmac_ctx_t tmpctx; |
| 176 | hmac_clone(ctx, &tmpctx); | 177 | hmac_clone(ctx, &tmpctx); |
| 177 | #endif | 178 | #endif |
| 178 | va_list va; | ||
| 179 | 179 | ||
| 180 | va_start(va, out); | 180 | va_start(va, out); |
| 181 | hmac_hash_v(&tmpctx, va); | 181 | hmac_hash_v(&tmpctx, va); |
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index 587cf2e3d..1a8581c63 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c | |||
| @@ -17,30 +17,44 @@ | |||
| 17 | # include <windows.h> | 17 | # include <windows.h> |
| 18 | # include <bcrypt.h> | 18 | # include <bcrypt.h> |
| 19 | 19 | ||
| 20 | |||
| 21 | static wchar_t *alg_id_mappings[] = { | ||
| 22 | BCRYPT_MD5_ALGORITHM, | ||
| 23 | BCRYPT_SHA1_ALGORITHM, | ||
| 24 | BCRYPT_SHA256_ALGORITHM, | ||
| 25 | BCRYPT_SHA384_ALGORITHM, | ||
| 26 | BCRYPT_SHA512_ALGORITHM | ||
| 27 | }; | ||
| 28 | |||
| 29 | static BCRYPT_ALG_HANDLE algorithm_provider_cache[5] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; | ||
| 30 | static BCRYPT_ALG_HANDLE algorithm_provider_hmac_cache[5] = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE }; | ||
| 31 | |||
| 32 | /* | 20 | /* |
| 33 | * Requests an algorithm handle from the store, | 21 | * Requests an algorithm handle from the store, |
| 34 | * or creates it if it does not exist. | 22 | * or creates it if it does not exist. |
| 35 | */ | 23 | */ |
| 36 | 24 | ||
| 37 | BCRYPT_ALG_HANDLE get_alg_handle(enum cng_algorithm_identifier algorithm_identifier, bool hmac) { | 25 | BCRYPT_ALG_HANDLE get_alg_handle(enum cng_algorithm_identifier algorithm_identifier, bool hmac) |
| 38 | BCRYPT_ALG_HANDLE *cache = hmac ? algorithm_provider_hmac_cache : algorithm_provider_cache; | 26 | { |
| 27 | const wchar_t *alg_id_mappings[] = { | ||
| 28 | BCRYPT_MD5_ALGORITHM, | ||
| 29 | BCRYPT_SHA1_ALGORITHM, | ||
| 30 | BCRYPT_SHA256_ALGORITHM, | ||
| 31 | BCRYPT_SHA384_ALGORITHM, | ||
| 32 | BCRYPT_SHA512_ALGORITHM | ||
| 33 | }; | ||
| 34 | static BCRYPT_ALG_HANDLE algorithm_provider_cache[5] = { | ||
| 35 | INVALID_HANDLE_VALUE, | ||
| 36 | INVALID_HANDLE_VALUE, | ||
| 37 | INVALID_HANDLE_VALUE, | ||
| 38 | INVALID_HANDLE_VALUE, | ||
| 39 | INVALID_HANDLE_VALUE | ||
| 40 | }; | ||
| 41 | static BCRYPT_ALG_HANDLE algorithm_provider_hmac_cache[5] = { | ||
| 42 | INVALID_HANDLE_VALUE, | ||
| 43 | INVALID_HANDLE_VALUE, | ||
| 44 | INVALID_HANDLE_VALUE, | ||
| 45 | INVALID_HANDLE_VALUE, | ||
| 46 | INVALID_HANDLE_VALUE | ||
| 47 | }; | ||
| 48 | BCRYPT_ALG_HANDLE *cache; | ||
| 49 | NTSTATUS status; | ||
| 39 | 50 | ||
| 51 | cache = hmac ? algorithm_provider_hmac_cache : algorithm_provider_cache; | ||
| 40 | if (cache[algorithm_identifier] != INVALID_HANDLE_VALUE) { | 52 | if (cache[algorithm_identifier] != INVALID_HANDLE_VALUE) { |
| 41 | return cache[algorithm_identifier]; | 53 | return cache[algorithm_identifier]; |
| 42 | } | 54 | } |
| 43 | NTSTATUS status = BCryptOpenAlgorithmProvider(&cache[algorithm_identifier], alg_id_mappings[algorithm_identifier], NULL, hmac ? BCRYPT_ALG_HANDLE_HMAC_FLAG : 0); | 55 | status = BCryptOpenAlgorithmProvider(&cache[algorithm_identifier], |
| 56 | alg_id_mappings[algorithm_identifier], NULL, | ||
| 57 | hmac ? BCRYPT_ALG_HANDLE_HMAC_FLAG : 0); | ||
| 44 | mingw_die_if_error(status, "BCryptOpenAlgorithmProvider"); | 58 | mingw_die_if_error(status, "BCryptOpenAlgorithmProvider"); |
| 45 | 59 | ||
| 46 | return cache[algorithm_identifier]; | 60 | return cache[algorithm_identifier]; |
