From: Chris Denton christophersdenton@gmail.com
Support constant values for algorithm handles in `BCryptGenRandom` and make no attempt to dereference such handles.
MSDN Documentation: https://docs.microsoft.com/en-us/windows/win32/seccng/cng-algorithm-pseudo-h... --- dlls/bcrypt/bcrypt_main.c | 10 ++++++++++ dlls/bcrypt/tests/bcrypt.c | 8 ++++++++ include/bcrypt.h | 3 +++ 3 files changed, 21 insertions(+)
diff --git a/dlls/bcrypt/bcrypt_main.c b/dlls/bcrypt/bcrypt_main.c index 0c4c13a4c86..bbe74e3f49d 100644 --- a/dlls/bcrypt/bcrypt_main.c +++ b/dlls/bcrypt/bcrypt_main.c @@ -190,6 +190,16 @@ NTSTATUS WINAPI BCryptGenRandom(BCRYPT_ALG_HANDLE handle, UCHAR *buffer, ULONG c if (!(flags & BCRYPT_USE_SYSTEM_PREFERRED_RNG)) return STATUS_INVALID_HANDLE; } + /* Pseudo algorithm handles are denoted by having the lowest bit set. + * An aligned algorithm pointer will never have this bit set. + */ + else if (((uintptr_t)algorithm & 1) == 1) + { + if (algorithm != BCRYPT_RNG_ALG_HANDLE) { + FIXME( "pseudo-handle algorithm %p not supported\n", algorithm); + return STATUS_NOT_IMPLEMENTED; + } + } else if (algorithm->hdr.magic != MAGIC_ALG || algorithm->id != ALG_ID_RNG) return STATUS_INVALID_HANDLE;
diff --git a/dlls/bcrypt/tests/bcrypt.c b/dlls/bcrypt/tests/bcrypt.c index 2af105463d4..95a09b9395f 100644 --- a/dlls/bcrypt/tests/bcrypt.c +++ b/dlls/bcrypt/tests/bcrypt.c @@ -57,6 +57,14 @@ static void test_BCryptGenRandom(void) ret = BCryptGenRandom(NULL, buffer, 8, BCRYPT_USE_SYSTEM_PREFERRED_RNG); ok(ret == STATUS_SUCCESS, "Expected success, got %#lx\n", ret); ok(memcmp(buffer, buffer + 8, 8), "Expected a random number, got 0\n"); + + /* Test pseudo handle, which was introduced at the same time as BCryptHash */ + if (pBCryptHash) + { + ret = BCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, buffer, sizeof(buffer), 0); + ok(ret == STATUS_SUCCESS, "Expected success, got %#lx\n", ret); + } + else win_skip("BCryptGenRandom pseudo handles are not available\n"); }
static void test_BCryptGetFipsAlgorithmMode(void) diff --git a/include/bcrypt.h b/include/bcrypt.h index c9c4cb7b1fd..21bfa990967 100644 --- a/include/bcrypt.h +++ b/include/bcrypt.h @@ -401,6 +401,9 @@ typedef PVOID BCRYPT_HANDLE; typedef PVOID BCRYPT_HASH_HANDLE; typedef PVOID BCRYPT_SECRET_HANDLE;
+/* Pseudo handles for BCryptGenRandom */ +#define BCRYPT_RNG_ALG_HANDLE ((BCRYPT_ALG_HANDLE)129) + /* Flags for BCryptGenRandom */ #define BCRYPT_RNG_USE_ENTROPY_IN_BUFFER 0x00000001 #define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002