1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #include "Common/Crypto/sha256.h"
19 #include "Common/Log.h"
20 #include "Core/HLE/HLE.h"
21 #include "Core/HLE/FunctionWrappers.h"
22 #include "Core/HLE/sceSha256.h"
23 
sceSha256Digest(u32 data,int dataLen,u32 digestPtr)24 static int sceSha256Digest(u32 data, int dataLen, u32 digestPtr) {
25 	if (!Memory::IsValidAddress(data) || !Memory::IsValidAddress(digestPtr) || !Memory::IsValidAddress(data + dataLen)) {
26 		ERROR_LOG(HLE, "sceSha256Digest(data=%08x, len=%d, digest=%08x) - bad address(es)", data, dataLen, digestPtr);
27 		return -1;
28 	}
29 	INFO_LOG(HLE, "sceSha256Digest(data=%08x, len=%d, digest=%08x)", data, dataLen, digestPtr);
30 
31 	// Already checked above...
32 	u8 *digest = Memory::GetPointerUnchecked(digestPtr);
33 	sha256_context ctx;
34 	sha256_starts(&ctx);
35 	sha256_update(&ctx, Memory::GetPointerUnchecked(data), dataLen);
36 	sha256_finish(&ctx, digest);
37 
38 	return 0;
39 }
40 
41 const HLEFunction sceSha256[] =
42 {
43 	{0X318A350C, &WrapI_UIU<sceSha256Digest>,        "sceSha256Digest", 'i', "xix"},
44 };
45 
Register_sceSha256()46 void Register_sceSha256()
47 {
48 	RegisterModule("sceSha256", ARRAY_SIZE(sceSha256), sceSha256);
49 }
50