diff options
Diffstat (limited to 'libbb/hash_md5_sha.c')
-rw-r--r-- | libbb/hash_md5_sha.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index 75a61c32c..22dd890bf 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c | |||
@@ -13,6 +13,82 @@ | |||
13 | 13 | ||
14 | #define NEED_SHA512 (ENABLE_SHA512SUM || ENABLE_USE_BB_CRYPT_SHA) | 14 | #define NEED_SHA512 (ENABLE_SHA512SUM || ENABLE_USE_BB_CRYPT_SHA) |
15 | 15 | ||
16 | #if ENABLE_FEATURE_USE_CNG_API | ||
17 | # include <windows.h> | ||
18 | # include <bcrypt.h> | ||
19 | |||
20 | // these work on Windows >= 10 | ||
21 | # define BCRYPT_MD5_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x00000021) | ||
22 | # define BCRYPT_SHA1_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x00000031) | ||
23 | # define BCRYPT_SHA256_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x00000041) | ||
24 | # define BCRYPT_SHA512_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x00000061) | ||
25 | |||
26 | /* Initialize structure containing state of computation. | ||
27 | * (RFC 1321, 3.3: Step 3) | ||
28 | */ | ||
29 | |||
30 | static void generic_init(struct bcrypt_hash_ctx_t *ctx, BCRYPT_ALG_HANDLE alg_handle) { | ||
31 | DWORD hash_object_length = 0; | ||
32 | ULONG _unused; | ||
33 | NTSTATUS status; | ||
34 | |||
35 | status = BCryptGetProperty(alg_handle, BCRYPT_OBJECT_LENGTH, (PUCHAR)&hash_object_length, sizeof(DWORD), &_unused, 0); | ||
36 | mingw_die_if_error(status, "BCryptGetProperty"); | ||
37 | status = BCryptGetProperty(alg_handle, BCRYPT_HASH_LENGTH, (PUCHAR)&ctx->output_size, sizeof(DWORD), &_unused, 0); | ||
38 | mingw_die_if_error(status, "BCryptGetProperty"); | ||
39 | |||
40 | |||
41 | ctx->hash_obj = xmalloc(hash_object_length); | ||
42 | |||
43 | status = BCryptCreateHash(alg_handle, &ctx->handle, ctx->hash_obj, hash_object_length, NULL, 0, 0); | ||
44 | mingw_die_if_error(status, "BCryptCreateHash"); | ||
45 | } | ||
46 | |||
47 | void FAST_FUNC md5_begin(md5_ctx_t *ctx) | ||
48 | { | ||
49 | generic_init(ctx, BCRYPT_MD5_ALG_HANDLE); | ||
50 | } | ||
51 | |||
52 | void FAST_FUNC sha1_begin(sha1_ctx_t *ctx) | ||
53 | { | ||
54 | generic_init(ctx, BCRYPT_SHA1_ALG_HANDLE); | ||
55 | } | ||
56 | |||
57 | /* Initialize structure containing state of computation. | ||
58 | (FIPS 180-2:5.3.2) */ | ||
59 | void FAST_FUNC sha256_begin(sha256_ctx_t *ctx) | ||
60 | { | ||
61 | generic_init(ctx, BCRYPT_SHA256_ALG_HANDLE); | ||
62 | } | ||
63 | |||
64 | #if NEED_SHA512 | ||
65 | /* Initialize structure containing state of computation. | ||
66 | (FIPS 180-2:5.3.3) */ | ||
67 | void FAST_FUNC sha512_begin(sha512_ctx_t *ctx) | ||
68 | { | ||
69 | generic_init(ctx, BCRYPT_SHA512_ALG_HANDLE); | ||
70 | } | ||
71 | #endif /* NEED_SHA512 */ | ||
72 | |||
73 | void FAST_FUNC generic_hash(struct bcrypt_hash_ctx_t *ctx, const void *buffer, size_t len) | ||
74 | { | ||
75 | /* | ||
76 | for perf, no error checking here | ||
77 | */ | ||
78 | /*NTSTATUS status = */ BCryptHashData(ctx->handle, (const PUCHAR)buffer, len, 0); | ||
79 | // mingw_die_if_error(status, "BCryptHashData"); | ||
80 | } | ||
81 | |||
82 | unsigned FAST_FUNC generic_end(struct bcrypt_hash_ctx_t *ctx, void *resbuf) | ||
83 | { | ||
84 | NTSTATUS status = BCryptFinishHash(ctx->handle, resbuf, ctx->output_size, 0); | ||
85 | mingw_die_if_error(status, "BCryptFinishHash"); | ||
86 | BCryptDestroyHash(ctx->handle); | ||
87 | free(ctx->hash_obj); | ||
88 | return ctx->output_size; | ||
89 | } | ||
90 | #endif /* !ENABLE_FEATURE_USE_CNG_API */ | ||
91 | |||
16 | #if ENABLE_SHA1_HWACCEL || ENABLE_SHA256_HWACCEL | 92 | #if ENABLE_SHA1_HWACCEL || ENABLE_SHA256_HWACCEL |
17 | # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) | 93 | # if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) |
18 | static void cpuid_eax_ebx_ecx(unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *edx) | 94 | static void cpuid_eax_ebx_ecx(unsigned *eax, unsigned *ebx, unsigned *ecx, unsigned *edx) |
@@ -80,6 +156,7 @@ static ALWAYS_INLINE uint64_t rotl64(uint64_t x, unsigned n) | |||
80 | return (x << n) | (x >> (64 - n)); | 156 | return (x << n) | (x >> (64 - n)); |
81 | } | 157 | } |
82 | 158 | ||
159 | #if !ENABLE_FEATURE_USE_CNG_API | ||
83 | /* Process the remaining bytes in the buffer */ | 160 | /* Process the remaining bytes in the buffer */ |
84 | static void FAST_FUNC common64_end(md5_ctx_t *ctx, int swap_needed) | 161 | static void FAST_FUNC common64_end(md5_ctx_t *ctx, int swap_needed) |
85 | { | 162 | { |
@@ -1367,6 +1444,7 @@ unsigned FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) | |||
1367 | return sizeof(ctx->hash); | 1444 | return sizeof(ctx->hash); |
1368 | } | 1445 | } |
1369 | #endif /* NEED_SHA512 */ | 1446 | #endif /* NEED_SHA512 */ |
1447 | #endif /* !ENABLE_FEATURE_USE_CNG_API */ | ||
1370 | 1448 | ||
1371 | 1449 | ||
1372 | /* | 1450 | /* |