; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: OS dependent part of different random number generators.
;
;  Target OS: Win32
;
;  Dependencies: memory.asm
;
;  Notes:
;
;_________________________________________________________________________________________

uses advapi32


; Provider Types
PROV_RSA_FULL             = 1
PROV_RSA_SIG              = 2
PROV_DSS                  = 3
PROV_FORTEZZA             = 4
PROV_MS_EXCHANGE          = 5
PROV_SSL                  = 6
PROV_RSA_SCHANNEL         = 12
PROV_DSS_DH               = 13
PROV_EC_ECDSA_SIG         = 14
PROV_EC_ECNRA_SIG         = 15
PROV_EC_ECDSA_FULL        = 16
PROV_EC_ECNRA_FULL        = 17
PROV_DH_SCHANNEL          = 18
PROV_SPYRUS_LYNKS         = 20
PROV_RNG                  = 21
PROV_INTEL_SEC            = 22
PROV_REPLACE_OWF          = 23
PROV_RSA_AES              = 24


; CryptAcquireContext

CRYPT_VERIFYCONTEXT       = 0xF0000000
CRYPT_NEWKEYSET           = 0x00000008
CRYPT_DELETEKEYSET        = 0x00000010
CRYPT_MACHINE_KEYSET      = 0x00000020
CRYPT_SILENT              = 0x00000040




body GetRandomBytes
.prov dd ?
begin
        pushad

        stdcall GetMem, [.count]
        mov     esi, eax

        lea     eax, [.prov]
        invoke  CryptAcquireContextW, eax, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT or CRYPT_SILENT
        test    eax, eax
        jz      .error


        invoke  CryptGenRandom, [.prov], [.count], esi
        test    eax, eax
        jz      .error2

        clc
        mov     [esp+4*regEAX], esi
        popad
        return

.error2:
        invoke  CryptReleaseContext, [.prov], 0

.error:
        stc
        popad
        return
endp
