MCPcopy Create free account
hub / github.com/abbeycode/UnrarKit / SecHideData

Function SecHideData

Libraries/unrar/secpassword.cpp:166–216  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

164
165
166void SecHideData(void *Data,size_t DataSize,bool Encode,bool CrossProcess)
167{
168 // CryptProtectMemory is not available in UWP and CryptProtectData
169 // increases data size not allowing in place conversion.
170#if defined(_WIN_ALL)
171 // Try to utilize the secure Crypt[Un]ProtectMemory if possible.
172 if (GlobalCryptLoader.pCryptProtectMemory==NULL)
173 GlobalCryptLoader.Load();
174 size_t Aligned=DataSize-DataSize%CRYPTPROTECTMEMORY_BLOCK_SIZE;
175 DWORD Flags=CrossProcess ? CRYPTPROTECTMEMORY_CROSS_PROCESS : CRYPTPROTECTMEMORY_SAME_PROCESS;
176 if (Encode)
177 {
178 if (GlobalCryptLoader.pCryptProtectMemory!=NULL)
179 {
180 if (!GlobalCryptLoader.pCryptProtectMemory(Data,DWORD(Aligned),Flags))
181 {
182 ErrHandler.GeneralErrMsg(L"CryptProtectMemory failed");
183 ErrHandler.SysErrMsg();
184 ErrHandler.Exit(RARX_FATAL);
185 }
186 return;
187 }
188 }
189 else
190 {
191 if (GlobalCryptLoader.pCryptUnprotectMemory!=NULL)
192 {
193 if (!GlobalCryptLoader.pCryptUnprotectMemory(Data,DWORD(Aligned),Flags))
194 {
195 ErrHandler.GeneralErrMsg(L"CryptUnprotectMemory failed");
196 ErrHandler.SysErrMsg();
197 ErrHandler.Exit(RARX_FATAL);
198 }
199 return;
200 }
201 }
202#endif
203
204 // CryptProtectMemory is not available, so only slightly obfuscate data.
205 uint Key;
206#ifdef _WIN_ALL
207 Key=GetCurrentProcessId();
208#elif defined(_UNIX)
209 Key=getpid();
210#else
211 Key=0; // Just an arbitrary value.
212#endif
213
214 for (size_t I=0;I<DataSize;I++)
215 *((byte *)Data+I)^=Key+I+75;
216}

Callers 3

ProcessMethod · 0.85
SetKey50Method · 0.85
SetKey30Method · 0.85

Calls 4

GeneralErrMsgMethod · 0.80
SysErrMsgMethod · 0.80
ExitMethod · 0.80
LoadMethod · 0.45

Tested by

no test coverage detected