- Timestamp:
- Sep 20, 2016, 6:39:06 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kWorker/kWorker.c
r2948 r2949 71 71 * they are included. */ 72 72 #define WITH_HASH_MD5_CACHE 73 74 /** @def WITH_CRYPT_CTX_REUSE 75 * Enables reusing crypt contexts. The Visual C++ compiler always creates a 76 * context which is only used for MD5 and maybe some random bytes (VS 2010). 77 * So, only create it once and add a reference to it instead of creating new 78 * ones. Saves registry access among other things. */ 79 #define WITH_CRYPT_CTX_REUSE 73 80 74 81 /** @def WITH_CONSOLE_OUTPUT_BUFFERING … … 769 776 } LastHashRead; 770 777 #endif 778 779 #ifdef WITH_CRYPT_CTX_REUSE 780 /** Reusable crypt contexts. */ 781 struct 782 { 783 /** The creation provider type. */ 784 KU32 dwProvType; 785 /** The creation flags. */ 786 KU32 dwFlags; 787 /** The length of the container name. */ 788 KU32 cwcContainer; 789 /** The length of the provider name. */ 790 KU32 cwcProvider; 791 /** The container name string. */ 792 wchar_t *pwszContainer; 793 /** The provider name string. */ 794 wchar_t *pwszProvider; 795 /** The context handle. */ 796 HCRYPTPROV hProv; 797 } aCryptCtxs[4]; 798 /** Number of reusable crypt conexts in aCryptCtxs. */ 799 KU32 cCryptCtxs; 800 #endif 801 771 802 772 803 #ifdef WITH_CONSOLE_OUTPUT_BUFFERING … … 7543 7574 #ifdef WITH_HASH_MD5_CACHE 7544 7575 7545 /** Adv api32 - CryptCreateHash */7576 /** AdvApi32 - CryptCreateHash */ 7546 7577 static BOOL WINAPI kwSandbox_Advapi32_CryptCreateHash(HCRYPTPROV hProv, ALG_ID idAlg, HCRYPTKEY hKey, DWORD dwFlags, 7547 7578 HCRYPTHASH *phHash) … … 7602 7633 7603 7634 7604 /** Adv api32 - CryptHashData */7635 /** AdvApi32 - CryptHashData */ 7605 7636 static BOOL WINAPI kwSandbox_Advapi32_CryptHashData(HCRYPTHASH hHash, CONST BYTE *pbData, DWORD cbData, DWORD dwFlags) 7606 7637 { … … 7717 7748 7718 7749 7719 /** Adv api32 - CryptGetHashParam */7750 /** AdvApi32 - CryptGetHashParam */ 7720 7751 static BOOL WINAPI kwSandbox_Advapi32_CryptGetHashParam(HCRYPTHASH hHash, DWORD dwParam, 7721 7752 BYTE *pbData, DWORD *pcbData, DWORD dwFlags) … … 7875 7906 7876 7907 7877 /** Adv api32 - CryptDestroyHash */7908 /** AdvApi32 - CryptDestroyHash */ 7878 7909 static BOOL WINAPI kwSandbox_Advapi32_CryptDestroyHash(HCRYPTHASH hHash) 7879 7910 { … … 7921 7952 #endif /* WITH_HASH_MD5_CACHE */ 7922 7953 7954 7955 /* 7956 * 7957 * Reuse crypt context. 7958 * Reuse crypt context. 7959 * Reuse crypt context. 7960 * 7961 * 7962 * This saves a little bit of time and registry accesses each time CL, C1 or C1XX runs. 7963 * 7964 */ 7965 7966 #ifdef WITH_CRYPT_CTX_REUSE 7967 7968 /** AdvApi32 - CryptAcquireContextW. */ 7969 static BOOL WINAPI kwSandbox_Advapi32_CryptAcquireContextW(HCRYPTPROV *phProv, LPCWSTR pwszContainer, LPCWSTR pwszProvider, 7970 DWORD dwProvType, DWORD dwFlags) 7971 { 7972 BOOL fRet; 7973 7974 /* 7975 * Lookup reusable context based on the input. 7976 */ 7977 KSIZE const cwcContainer = pwszContainer ? kwUtf16Len(pwszContainer) : 0; 7978 KSIZE const cwcProvider = pwszProvider ? kwUtf16Len(pwszProvider) : 0; 7979 KU32 iCtx = g_Sandbox.cCryptCtxs; 7980 while (iCtx-- > 0) 7981 { 7982 if ( g_Sandbox.aCryptCtxs[iCtx].cwcContainer == cwcContainer 7983 && g_Sandbox.aCryptCtxs[iCtx].cwcProvider == cwcProvider 7984 && g_Sandbox.aCryptCtxs[iCtx].dwProvType == dwProvType 7985 && g_Sandbox.aCryptCtxs[iCtx].dwFlags == dwFlags 7986 && kHlpMemComp(g_Sandbox.aCryptCtxs[iCtx].pwszContainer, pwszContainer, cwcContainer * sizeof(wchar_t)) == 0 7987 && kHlpMemComp(g_Sandbox.aCryptCtxs[iCtx].pwszProvider, pwszProvider, cwcProvider * sizeof(wchar_t)) == 0) 7988 { 7989 if (CryptContextAddRef(g_Sandbox.aCryptCtxs[iCtx].hProv, NULL, 0)) 7990 { 7991 *phProv = g_Sandbox.aCryptCtxs[iCtx].hProv; 7992 KWCRYPT_LOG(("CryptAcquireContextW(,%ls, %ls, %#x, %#x) -> TRUE, %p [reused]\n", 7993 pwszContainer, pwszProvider, dwProvType, dwFlags, *phProv)); 7994 return TRUE; 7995 } 7996 } 7997 } 7998 7999 /* 8000 * Create it and enter it into the reused array if possible. 8001 */ 8002 fRet = CryptAcquireContextW(phProv, pwszContainer, pwszProvider, dwProvType, dwFlags); 8003 if (fRet) 8004 { 8005 iCtx = g_Sandbox.cCryptCtxs; 8006 if (iCtx < K_ELEMENTS(g_Sandbox.aCryptCtxs)) 8007 { 8008 /* Try duplicate the input strings. */ 8009 g_Sandbox.aCryptCtxs[iCtx].pwszContainer = kHlpDup(pwszContainer ? pwszContainer : L"", 8010 (cwcContainer + 1) * sizeof(wchar_t)); 8011 if (g_Sandbox.aCryptCtxs[iCtx].pwszContainer) 8012 { 8013 g_Sandbox.aCryptCtxs[iCtx].pwszProvider = kHlpDup(pwszProvider ? pwszProvider : L"", 8014 (cwcProvider + 1) * sizeof(wchar_t)); 8015 if (g_Sandbox.aCryptCtxs[iCtx].pwszProvider) 8016 { 8017 /* Add a couple of references just to be on the safe side and all that. */ 8018 HCRYPTPROV hProv = *phProv; 8019 if (CryptContextAddRef(hProv, NULL, 0)) 8020 { 8021 if (CryptContextAddRef(hProv, NULL, 0)) 8022 { 8023 /* Okay, finish the entry and return success */ 8024 g_Sandbox.aCryptCtxs[iCtx].hProv = hProv; 8025 g_Sandbox.aCryptCtxs[iCtx].dwProvType = dwProvType; 8026 g_Sandbox.aCryptCtxs[iCtx].dwFlags = dwFlags; 8027 g_Sandbox.cCryptCtxs = iCtx + 1; 8028 8029 KWCRYPT_LOG(("CryptAcquireContextW(,%ls, %ls, %#x, %#x) -> TRUE, %p [new]\n", 8030 pwszContainer, pwszProvider, dwProvType, dwFlags, *phProv)); 8031 return TRUE; 8032 } 8033 CryptReleaseContext(hProv, 0); 8034 } 8035 KWCRYPT_LOG(("CryptAcquireContextW: CryptContextAddRef failed!\n")); 8036 8037 kHlpFree(g_Sandbox.aCryptCtxs[iCtx].pwszProvider); 8038 g_Sandbox.aCryptCtxs[iCtx].pwszProvider = NULL; 8039 } 8040 kHlpFree(g_Sandbox.aCryptCtxs[iCtx].pwszContainer); 8041 g_Sandbox.aCryptCtxs[iCtx].pwszContainer = NULL; 8042 } 8043 } 8044 else 8045 KWCRYPT_LOG(("CryptAcquireContextW: Too many crypt contexts to keep and reuse!\n")); 8046 } 8047 8048 KWCRYPT_LOG(("CryptAcquireContextW(,%ls, %ls, %#x, %#x) -> %d, %p\n", 8049 pwszContainer, pwszProvider, dwProvType, dwFlags, *phProv)); 8050 return fRet; 8051 } 8052 8053 8054 /** AdvApi32 - CryptReleaseContext */ 8055 static BOOL WINAPI kwSandbox_Advapi32_CryptReleaseContext(HCRYPTPROV hProv, DWORD dwFlags) 8056 { 8057 BOOL fRet = CryptReleaseContext(hProv, dwFlags); 8058 KWCRYPT_LOG(("CryptReleaseContext(%p,%#x) -> %d\n", hProv, dwFlags, fRet)); 8059 return fRet; 8060 } 8061 8062 8063 /** AdvApi32 - CryptContextAddRef */ 8064 static BOOL WINAPI kwSandbox_Advapi32_CryptContextAddRef(HCRYPTPROV hProv, DWORD *pdwReserved, DWORD dwFlags) 8065 { 8066 BOOL fRet = CryptContextAddRef(hProv, pdwReserved, dwFlags); 8067 KWCRYPT_LOG(("CryptContextAddRef(%p,%p,%#x) -> %d\n", hProv, pdwReserved, dwFlags, fRet)); 8068 return fRet; 8069 } 8070 8071 #endif /* WITH_CRYPT_CTX_REUSE */ 7923 8072 7924 8073 /* … … 8252 8401 #endif 8253 8402 8254 8255 8403 #ifdef WITH_HASH_MD5_CACHE 8256 8404 { TUPLE("CryptCreateHash"), NULL, (KUPTR)kwSandbox_Advapi32_CryptCreateHash }, … … 8258 8406 { TUPLE("CryptGetHashParam"), NULL, (KUPTR)kwSandbox_Advapi32_CryptGetHashParam }, 8259 8407 { TUPLE("CryptDestroyHash"), NULL, (KUPTR)kwSandbox_Advapi32_CryptDestroyHash }, 8408 #endif 8409 8410 #ifdef WITH_CRYPT_CTX_REUSE 8411 { TUPLE("CryptAcquireContextW"), NULL, (KUPTR)kwSandbox_Advapi32_CryptAcquireContextW }, 8412 { TUPLE("CryptReleaseContext"), NULL, (KUPTR)kwSandbox_Advapi32_CryptReleaseContext }, 8413 { TUPLE("CryptContextAddRef"), NULL, (KUPTR)kwSandbox_Advapi32_CryptContextAddRef }, 8260 8414 #endif 8261 8415
Note:
See TracChangeset
for help on using the changeset viewer.