123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 |
- #include-once
- #include "FileConstants.au3"
- #include "WinAPIError.au3"
- ; #INDEX# =======================================================================================================================
- ; Title .........: Crypt
- ; AutoIt Version : 3.3.14.5
- ; Language ......: English
- ; Description ...: Functions for encrypting and hashing data.
- ; Author(s) .....: Andreas Karlsson (monoceres), jchd
- ; Dll(s) ........: Advapi32.dll
- ; ===============================================================================================================================
- ; #CURRENT# =====================================================================================================================
- ; _Crypt_DecryptData
- ; _Crypt_DecryptFile
- ; _Crypt_DeriveKey
- ; _Crypt_DestroyKey
- ; _Crypt_EncryptData
- ; _Crypt_EncryptFile
- ; _Crypt_GenRandom
- ; _Crypt_HashData
- ; _Crypt_HashFile
- ; _Crypt_Shutdown
- ; _Crypt_Startup
- ; ===============================================================================================================================
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; __Crypt_Context
- ; __Crypt_ContextSet
- ; __Crypt_DllHandle
- ; __Crypt_DllHandleSet
- ; __Crypt_GetCalgFromCryptKey
- ; __Crypt_RefCount
- ; __Crypt_RefCountDec
- ; __Crypt_RefCountInc
- ; ===============================================================================================================================
- ; #CONSTANTS# ===================================================================================================================
- Global Const $PROV_RSA_FULL = 0x1
- Global Const $PROV_RSA_AES = 24
- Global Const $CRYPT_VERIFYCONTEXT = 0xF0000000
- Global Const $HP_HASHSIZE = 0x0004
- Global Const $HP_HASHVAL = 0x0002
- Global Const $CRYPT_EXPORTABLE = 0x00000001
- Global Const $CRYPT_USERDATA = 1
- Global Const $KP_ALGID = 0x00000007
- Global Const $CALG_MD2 = 0x00008001
- Global Const $CALG_MD4 = 0x00008002
- Global Const $CALG_MD5 = 0x00008003
- Global Const $CALG_SHA1 = 0x00008004
- Global Const $CALG_SHA_256 = 0x0000800c
- Global Const $CALG_SHA_384 = 0x0000800d
- Global Const $CALG_SHA_512 = 0x0000800e
- Global Const $CALG_3DES = 0x00006603
- Global Const $CALG_AES_128 = 0x0000660e
- Global Const $CALG_AES_192 = 0x0000660f
- Global Const $CALG_AES_256 = 0x00006610
- Global Const $CALG_DES = 0x00006601
- Global Const $CALG_RC2 = 0x00006602
- Global Const $CALG_RC4 = 0x00006801
- Global Const $CALG_USERKEY = 0
- ; #VARIABLES# ===================================================================================================================
- Global $__g_aCryptInternalData[3]
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jpm
- ; ===============================================================================================================================
- Func _Crypt_Startup()
- If __Crypt_RefCount() = 0 Then
- Local $hAdvapi32 = DllOpen("Advapi32.dll")
- If $hAdvapi32 = -1 Then Return SetError(1001, 0, False)
- __Crypt_DllHandleSet($hAdvapi32)
- Local $iProviderID = $PROV_RSA_AES
- Local $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptAcquireContext", "handle*", 0, "ptr", 0, "ptr", 0, "dword", $iProviderID, "dword", $CRYPT_VERIFYCONTEXT)
- If @error Or Not $aRet[0] Then
- Local $iError = @error + 1002, $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- DllClose(__Crypt_DllHandle())
- Return SetError($iError, $iExtended, False)
- Else
- __Crypt_ContextSet($aRet[1])
- ; Fall through to success.
- EndIf
- EndIf
- __Crypt_RefCountInc()
- Return True
- EndFunc ;==>_Crypt_Startup
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......:
- ; ===============================================================================================================================
- Func _Crypt_Shutdown()
- __Crypt_RefCountDec()
- If __Crypt_RefCount() = 0 Then
- DllCall(__Crypt_DllHandle(), "bool", "CryptReleaseContext", "handle", __Crypt_Context(), "dword", 0)
- DllClose(__Crypt_DllHandle())
- EndIf
- EndFunc ;==>_Crypt_Shutdown
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jpm
- ; ===============================================================================================================================
- Func _Crypt_DeriveKey($vPassword, $iAlgID, $iHashPasswordID = $CALG_MD5)
- Local $aRet = 0, _
- $tBuff = 0, $hCryptHash = 0, _
- $iError = 0, $iExtended = 0, _
- $vReturn = 0
- _Crypt_Startup()
- If @error Then Return SetError(@error, @extended, -1)
- Do
- ; Create Hash object
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptCreateHash", "handle", __Crypt_Context(), "uint", $iHashPasswordID, "ptr", 0, "dword", 0, "handle*", 0)
- If @error Or Not $aRet[0] Then
- $iError = @error + 10
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- $hCryptHash = $aRet[5]
- $tBuff = DllStructCreate("byte[" & BinaryLen($vPassword) & "]")
- DllStructSetData($tBuff, 1, $vPassword)
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptHashData", "handle", $hCryptHash, "struct*", $tBuff, "dword", DllStructGetSize($tBuff), "dword", $CRYPT_USERDATA)
- If @error Or Not $aRet[0] Then
- $iError = @error + 20
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- ; Create key
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptDeriveKey", "handle", __Crypt_Context(), "uint", $iAlgID, "handle", $hCryptHash, "dword", $CRYPT_EXPORTABLE, "handle*", 0)
- If @error Or Not $aRet[0] Then
- $iError = @error + 30
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- $vReturn = $aRet[5]
- Until True
- If $hCryptHash <> 0 Then DllCall(__Crypt_DllHandle(), "bool", "CryptDestroyHash", "handle", $hCryptHash)
- Return SetError($iError, $iExtended, $vReturn)
- EndFunc ;==>_Crypt_DeriveKey
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jpm
- ; ===============================================================================================================================
- Func _Crypt_DestroyKey($hCryptKey)
- Local $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptDestroyKey", "handle", $hCryptKey)
- Local $iError = @error, $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- _Crypt_Shutdown()
- If $iError Or Not $aRet[0] Then
- Return SetError($iError + 10, $iExtended, False)
- Else
- Return True
- EndIf
- EndFunc ;==>_Crypt_DestroyKey
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jchd, jpm
- ; ===============================================================================================================================
- Func _Crypt_EncryptData($vData, $vCryptKey, $iAlgID, $bFinal = True)
- Switch $iAlgID
- Case $CALG_USERKEY
- Local $iCalgUsed = __Crypt_GetCalgFromCryptKey($vCryptKey)
- If @error Then Return SetError(@error, @extended, -1)
- If $iCalgUsed = $CALG_RC4 Then ContinueCase
- Case $CALG_RC4
- If BinaryLen($vData) = 0 Then Return SetError(0, 0, Binary(''))
- EndSwitch
- Local $iReqBuffSize = 0, _
- $aRet = 0, _
- $tBuff = 0, _
- $iError = 0, $iExtended = 0, _
- $vReturn = 0
- _Crypt_Startup()
- If @error Then Return SetError(@error, @extended, -1)
- Do
- If $iAlgID <> $CALG_USERKEY Then
- $vCryptKey = _Crypt_DeriveKey($vCryptKey, $iAlgID)
- If @error Then
- $iError = @error
- $iExtended = @extended
- $vReturn = -1
- ExitLoop
- EndIf
- EndIf
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptEncrypt", "handle", $vCryptKey, "handle", 0, "bool", $bFinal, "dword", 0, "ptr", 0, _
- "dword*", BinaryLen($vData), "dword", 0)
- If @error Or Not $aRet[0] Then
- $iError = @error + 50
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- $iReqBuffSize = $aRet[6]
- $tBuff = DllStructCreate("byte[" & $iReqBuffSize + 1 & "]")
- DllStructSetData($tBuff, 1, $vData)
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptEncrypt", "handle", $vCryptKey, "handle", 0, "bool", $bFinal, "dword", 0, "struct*", $tBuff, _
- "dword*", BinaryLen($vData), "dword", $iReqBuffSize)
- If @error Or Not $aRet[0] Then
- $iError = @error + 60
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- $vReturn = BinaryMid(DllStructGetData($tBuff, 1), 1, $iReqBuffSize)
- Until True
- If $iAlgID <> $CALG_USERKEY Then _Crypt_DestroyKey($vCryptKey)
- _Crypt_Shutdown()
- Return SetError($iError, $iExtended, $vReturn)
- EndFunc ;==>_Crypt_EncryptData
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jchd, jpm
- ; ===============================================================================================================================
- Func _Crypt_DecryptData($vData, $vCryptKey, $iAlgID, $bFinal = True)
- Switch $iAlgID
- Case $CALG_USERKEY
- Local $iCalgUsed = __Crypt_GetCalgFromCryptKey($vCryptKey)
- If @error Then Return SetError(@error, @extended, -1)
- If $iCalgUsed = $CALG_RC4 Then ContinueCase
- Case $CALG_RC4
- If BinaryLen($vData) = 0 Then Return SetError(0, 0, Binary(''))
- EndSwitch
- Local $aRet = 0, _
- $tBuff = 0, $tTempStruct = 0, _
- $iError = 0, $iExtended = 0, $iPlainTextSize = 0, _
- $vReturn = 0
- _Crypt_Startup()
- If @error Then Return SetError(@error, @extended, -1)
- Do
- If $iAlgID <> $CALG_USERKEY Then
- $vCryptKey = _Crypt_DeriveKey($vCryptKey, $iAlgID)
- If @error Then
- $iError = @error
- $iExtended = @extended
- $vReturn = -1
- ExitLoop
- EndIf
- EndIf
- $tBuff = DllStructCreate("byte[" & BinaryLen($vData) + 1000 & "]")
- If BinaryLen($vData) > 0 Then DllStructSetData($tBuff, 1, $vData)
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptDecrypt", "handle", $vCryptKey, "handle", 0, "bool", $bFinal, "dword", 0, "struct*", $tBuff, "dword*", BinaryLen($vData))
- If @error Or Not $aRet[0] Then
- $iError = @error + 70
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- $iPlainTextSize = $aRet[6]
- $tTempStruct = DllStructCreate("byte[" & $iPlainTextSize + 1 & "]", DllStructGetPtr($tBuff))
- $vReturn = BinaryMid(DllStructGetData($tTempStruct, 1), 1, $iPlainTextSize)
- Until True
- If $iAlgID <> $CALG_USERKEY Then _Crypt_DestroyKey($vCryptKey)
- _Crypt_Shutdown()
- Return SetError($iError, $iExtended, $vReturn)
- EndFunc ;==>_Crypt_DecryptData
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jpm
- ; ===============================================================================================================================
- Func _Crypt_HashData($vData, $iAlgID, $bFinal = True, $hCryptHash = 0)
- Local $aRet = 0, _
- $tBuff = 0, _
- $iError = 0, $iExtended = 0, $iHashSize = 0, _
- $vReturn = 0
- _Crypt_Startup()
- If @error Then Return SetError(@error, @extended, -1)
- Do
- If $hCryptHash = 0 Then
- ; Create Hash object
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptCreateHash", "handle", __Crypt_Context(), "uint", $iAlgID, "ptr", 0, "dword", 0, "handle*", 0)
- If @error Or Not $aRet[0] Then
- $iError = @error + 10
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- $hCryptHash = $aRet[5]
- EndIf
- $tBuff = DllStructCreate("byte[" & BinaryLen($vData) & "]")
- DllStructSetData($tBuff, 1, $vData)
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptHashData", "handle", $hCryptHash, "struct*", $tBuff, "dword", DllStructGetSize($tBuff), "dword", $CRYPT_USERDATA)
- If @error Or Not $aRet[0] Then
- $iError = @error + 20
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- If $bFinal Then
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptGetHashParam", "handle", $hCryptHash, "dword", $HP_HASHSIZE, "dword*", 0, "dword*", 4, "dword", 0)
- If @error Or Not $aRet[0] Then
- $iError = @error + 30
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- $iHashSize = $aRet[3]
- ; Get Hash
- $tBuff = DllStructCreate("byte[" & $iHashSize & "]")
- $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptGetHashParam", "handle", $hCryptHash, "dword", $HP_HASHVAL, "struct*", $tBuff, "dword*", $iHashSize, "dword", 0)
- If @error Or Not $aRet[0] Then
- $iError = @error + 40
- $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- $vReturn = DllStructGetData($tBuff, 1)
- Else
- $vReturn = $hCryptHash
- EndIf
- Until True
- ; Cleanup and return hash
- If $hCryptHash <> 0 And $bFinal Then DllCall(__Crypt_DllHandle(), "bool", "CryptDestroyHash", "handle", $hCryptHash)
- _Crypt_Shutdown()
- Return SetError($iError, $iExtended, $vReturn)
- EndFunc ;==>_Crypt_HashData
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jpm
- ; ===============================================================================================================================
- Func _Crypt_HashFile($sFilePath, $iAlgID)
- Local $dTempData = 0, _
- $hFile = 0, $hHashObject = 0, _
- $iError = 0, $iExtended = 0, _
- $vReturn = 0
- _Crypt_Startup()
- If @error Then Return SetError(@error, @extended, -1)
- Do
- $hFile = FileOpen($sFilePath, $FO_BINARY)
- If $hFile = -1 Then
- $iError = 1
- $iExtended = _WinAPI_GetLastError()
- $vReturn = -1
- ExitLoop
- EndIf
- Do
- $dTempData = FileRead($hFile, 512 * 1024)
- If @error Then
- $vReturn = _Crypt_HashData($dTempData, $iAlgID, True, $hHashObject)
- If @error Then
- $iError = @error
- $iExtended = @extended
- $vReturn = -1
- ExitLoop 2
- EndIf
- ExitLoop 2
- Else
- $hHashObject = _Crypt_HashData($dTempData, $iAlgID, False, $hHashObject)
- If @error Then
- $iError = @error + 100
- $iExtended = @extended
- $vReturn = -1
- ExitLoop 2
- EndIf
- EndIf
- Until False
- Until True
- _Crypt_Shutdown()
- If $hFile <> -1 Then FileClose($hFile)
- Return SetError($iError, $iExtended, $vReturn)
- EndFunc ;==>_Crypt_HashFile
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jpm
- ; ===============================================================================================================================
- Func _Crypt_EncryptFile($sSourceFile, $sDestinationFile, $vCryptKey, $iAlgID)
- Local $dTempData = 0, _
- $hInFile = 0, $hOutFile = 0, _
- $iError = 0, $iExtended = 0, $iFileSize = FileGetSize($sSourceFile), $iRead = 0, _
- $bReturn = True
- _Crypt_Startup()
- If @error Then Return SetError(@error, @extended, -1)
- Do
- If $iAlgID <> $CALG_USERKEY Then
- $vCryptKey = _Crypt_DeriveKey($vCryptKey, $iAlgID)
- If @error Then
- $iError = @error
- $iExtended = @extended
- $bReturn = False
- ExitLoop
- EndIf
- EndIf
- $hInFile = FileOpen($sSourceFile, $FO_BINARY)
- If $hInFile = -1 Then
- $iError = 2
- $iExtended = _WinAPI_GetLastError()
- $bReturn = False
- ExitLoop
- EndIf
- $hOutFile = FileOpen($sDestinationFile, $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY)
- If $hOutFile = -1 Then
- $iError = 3
- $iExtended = _WinAPI_GetLastError()
- $bReturn = False
- ExitLoop
- EndIf
- Do
- $dTempData = FileRead($hInFile, 1024 * 1024)
- $iRead += BinaryLen($dTempData)
- If $iRead = $iFileSize Then
- $dTempData = _Crypt_EncryptData($dTempData, $vCryptKey, $CALG_USERKEY, True)
- If @error Then
- $iError = @error + 400
- $iExtended = @extended
- $bReturn = False
- EndIf
- FileWrite($hOutFile, $dTempData)
- ExitLoop 2
- Else
- $dTempData = _Crypt_EncryptData($dTempData, $vCryptKey, $CALG_USERKEY, False)
- If @error Then
- $iError = @error + 500
- $iExtended = @extended
- $bReturn = False
- ExitLoop 2
- EndIf
- FileWrite($hOutFile, $dTempData)
- EndIf
- Until False
- Until True
- If $iAlgID <> $CALG_USERKEY Then _Crypt_DestroyKey($vCryptKey)
- _Crypt_Shutdown()
- If $hInFile <> -1 Then FileClose($hInFile)
- If $hOutFile <> -1 Then FileClose($hOutFile)
- Return SetError($iError, $iExtended, $bReturn)
- EndFunc ;==>_Crypt_EncryptFile
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Andreas Karlsson (monoceres)
- ; Modified ......: jpm
- ; ===============================================================================================================================
- Func _Crypt_DecryptFile($sSourceFile, $sDestinationFile, $vCryptKey, $iAlgID)
- Local $dTempData = 0, _
- $hInFile = 0, $hOutFile = 0, _
- $iError = 0, $iExtended = 0, $iFileSize = FileGetSize($sSourceFile), $iRead = 0, _
- $bReturn = True
- _Crypt_Startup()
- If @error Then Return SetError(@error, @extended, -1)
- Do
- If $iAlgID <> $CALG_USERKEY Then
- $vCryptKey = _Crypt_DeriveKey($vCryptKey, $iAlgID)
- If @error Then
- $iError = @error
- $iExtended = @extended
- $bReturn = False
- ExitLoop
- EndIf
- EndIf
- $hInFile = FileOpen($sSourceFile, $FO_BINARY)
- If $hInFile = -1 Then
- $iError = 2
- $iExtended = _WinAPI_GetLastError()
- $bReturn = False
- ExitLoop
- EndIf
- $hOutFile = FileOpen($sDestinationFile, $FO_OVERWRITE + $FO_CREATEPATH + $FO_BINARY)
- If $hOutFile = -1 Then
- $iError = 3
- $iExtended = _WinAPI_GetLastError()
- $bReturn = False
- ExitLoop
- EndIf
- Do
- $dTempData = FileRead($hInFile, 1024 * 1024)
- $iRead += BinaryLen($dTempData)
- If $iRead = $iFileSize Then
- $dTempData = _Crypt_DecryptData($dTempData, $vCryptKey, $CALG_USERKEY, True)
- If @error Then
- $iError = @error + 400
- $iExtended = @extended
- $bReturn = False
- EndIf
- FileWrite($hOutFile, $dTempData)
- ExitLoop 2
- Else
- $dTempData = _Crypt_DecryptData($dTempData, $vCryptKey, $CALG_USERKEY, False)
- If @error Then
- $iError = @error + 500
- $iExtended = @extended
- $bReturn = False
- ExitLoop 2
- EndIf
- FileWrite($hOutFile, $dTempData)
- EndIf
- Until False
- Until True
- If $iAlgID <> $CALG_USERKEY Then _Crypt_DestroyKey($vCryptKey)
- _Crypt_Shutdown()
- If $hInFile <> -1 Then FileClose($hInFile)
- If $hOutFile <> -1 Then FileClose($hOutFile)
- Return SetError($iError, $iExtended, $bReturn)
- EndFunc ;==>_Crypt_DecryptFile
- ; #FUNCTION# ====================================================================================================================
- ; Author ........: Erik Pilsits (wraithdu)
- ; Modified ......: jpm
- ; ===============================================================================================================================
- Func _Crypt_GenRandom($pBuffer, $iSize)
- _Crypt_Startup()
- If @error Then Return SetError(@error, @extended, False)
- Local $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptGenRandom", "handle", __Crypt_Context(), "dword", $iSize, "struct*", $pBuffer)
- Local $iError = @error, $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- _Crypt_Shutdown()
- If $iError Or (Not $aRet[0]) Then
- Return SetError($iError + 10, $iExtended, False)
- Else
- Return True
- EndIf
- EndFunc ;==>_Crypt_GenRandom
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; Name...........: __Crypt_RefCount
- ; Description ...: Retrieves the internal reference count.
- ; Syntax.........: __Crypt_RefCount ( )
- ; Parameters ....:
- ; Return values .: The current internal reference count.
- ; Author ........: Valik
- ; Modified.......:
- ; Remarks .......: For Internal Use Only
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Crypt_RefCount()
- Return $__g_aCryptInternalData[0]
- EndFunc ;==>__Crypt_RefCount
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; Name...........: __Crypt_RefCountInc
- ; Description ...: Increments the internal reference count.
- ; Syntax.........: __Crypt_RefCountInc ( )
- ; Parameters ....:
- ; Return values .:
- ; Author ........: Valik
- ; Modified.......:
- ; Remarks .......: For Internal Use Only
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Crypt_RefCountInc()
- $__g_aCryptInternalData[0] += 1
- EndFunc ;==>__Crypt_RefCountInc
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; Name...........: __Crypt_RefCountDec
- ; Description ...: Decrements the internal reference count.
- ; Syntax.........: __Crypt_RefCountDec ( )
- ; Parameters ....:
- ; Return values .:
- ; Author ........: Valik
- ; Modified.......:
- ; Remarks .......: For Internal Use Only
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Crypt_RefCountDec()
- If $__g_aCryptInternalData[0] > 0 Then $__g_aCryptInternalData[0] -= 1
- EndFunc ;==>__Crypt_RefCountDec
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; Name...........: __Crypt_DllHandle
- ; Description ...: Retrieves the internal DLL handle.
- ; Syntax.........: __Crypt_DllHandle ( )
- ; Parameters ....:
- ; Return values .: The current internal DLL handle.
- ; Author ........: Valik
- ; Modified.......:
- ; Remarks .......: For Internal Use Only
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Crypt_DllHandle()
- Return $__g_aCryptInternalData[1]
- EndFunc ;==>__Crypt_DllHandle
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; Name...........: __Crypt_DllHandleSet
- ; Description ...: Stores the internal DLL handle.
- ; Syntax.........: __Crypt_DllHandleSet ( $hAdvapi32 )
- ; Parameters ....: $hAdvapi32 - The new handle to store.
- ; Return values .:
- ; Author ........: Valik
- ; Modified.......:
- ; Remarks .......: For Internal Use Only
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Crypt_DllHandleSet($hAdvapi32)
- $__g_aCryptInternalData[1] = $hAdvapi32
- EndFunc ;==>__Crypt_DllHandleSet
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; Name...........: __Crypt_Context
- ; Description ...: Retrieves the internal crypt context.
- ; Syntax.........: __Crypt_Context ( )
- ; Parameters ....:
- ; Return values .: The current internal crypt context.
- ; Author ........: Valik
- ; Modified.......:
- ; Remarks .......: For Internal Use Only
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Crypt_Context()
- Return $__g_aCryptInternalData[2]
- EndFunc ;==>__Crypt_Context
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; Name...........: __Crypt_ContextSet
- ; Description ...: Stores the internal crypt context.
- ; Syntax.........: __Crypt_ContextSet ( $hCryptContext )
- ; Parameters ....: $hCryptContext - The new crypt context to store.
- ; Return values .:
- ; Author ........: Valik
- ; Modified.......:
- ; Remarks .......: For Internal Use Only
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Crypt_ContextSet($hCryptContext)
- $__g_aCryptInternalData[2] = $hCryptContext
- EndFunc ;==>__Crypt_ContextSet
- ; #INTERNAL_USE_ONLY# ===========================================================================================================
- ; Name...........: __Crypt_GetCalgFromCryptKey
- ; Description ...: Retrieves the crypto-algorithm use by a USERKEY.
- ; Syntax.........: __Crypt_GetCalgFromCryptKey($vCryptKey)
- ; Parameters ....: $vCryptKey - The USERKEY handle.
- ; Return values .:
- ; Author ........: jchd
- ; Modified.......: jpm
- ; Remarks .......: For Internal Use Only
- ; Related .......:
- ; Link ..........:
- ; Example .......:
- ; ===============================================================================================================================
- Func __Crypt_GetCalgFromCryptKey($vCryptKey)
- Local $tAlgId = DllStructCreate("uint")
- Local $aRet = DllCall(__Crypt_DllHandle(), "bool", "CryptGetKeyParam", "handle", $vCryptKey, "dword", $KP_ALGID, "struct*", $tAlgId, "dword*", DllStructGetSize($tAlgId), "dword", 0)
- Local $iError = @error, $iExtended = @extended
- If Not $aRet[0] Then $iExtended = _WinAPI_GetLastError()
- If $iError Or Not $aRet[0] Then
- Return SetError($iError + 80, $iExtended, $CRYPT_USERDATA)
- Else
- Return DllStructGetData($tAlgId, 1)
- EndIf
- EndFunc ;==>__Crypt_GetCalgFromCryptKey
|