jeudi 3 octobre 2019

How to properly open handle to TPMs RNG in C?

I'm trying to use TPM to generate random numbers. This is purely to learn to use TPM.

The issue i encountered, is i can't obtain the handle. When i use MS_PLATFORM_CRYPTO_PROVIDER, the handle is not created with STATUS_NOT_FOUND message. It works with default NULL.

How do i properly set up the handle to use TPM as RNG?

#include <Windows.h>
#include <bcrypt.h>
#pragma comment(lib, "bcrypt.lib")
...
BCRYPT_ALG_HANDLE *handle;
NTSTATUS handle_status = BCryptOpenAlgorithmProvider(
    &handle,
    BCRYPT_RNG_ALGORITHM,
    MS_PLATFORM_CRYPTO_PROVIDER,
    0
);
// Status code 0xC0000225 STATUS_NOT_FOUND
unsigned char *buffer = calloc(BIT_SIZE/sizeof(char), sizeof(char));
NTSTATUS gen_status = BCryptGenRandom(
    handle,
    buffer,
    BIT_SIZE / sizeof(char),
    0
);
...

I have an Intel TPM with 2.0 Specification Version. Status as ready.




Aucun commentaire:

Enregistrer un commentaire