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 "Core/HLE/HLE.h"
19 #include "Core/HLE/FunctionWrappers.h"
20 #include "Core/HLE/sceMd5.h"
21 #include "Core/MemMap.h"
22 #include "Core/Reporting.h"
23 #include "Common/Crypto/md5.h"
24 #include "Common/Crypto/sha1.h"
25 
26 #ifdef USE_CRT_DBG
27 #undef new
28 #endif
29 
30 // Not really sure where these belong - is it worth giving them their own file?
sceKernelUtilsMt19937Init(u32 ctx,u32 seed)31 u32 sceKernelUtilsMt19937Init(u32 ctx, u32 seed) {
32 	DEBUG_LOG(HLE, "sceKernelUtilsMt19937Init(%08x, %08x)", ctx, seed);
33 	if (!Memory::IsValidAddress(ctx))
34 		return -1;
35 	void *ptr = Memory::GetPointer(ctx);
36 	// This is made to match the memory layout of a PSP MT structure exactly.
37 	// Let's just construct it in place with placement new. Elite C++ hackery FTW.
38 	new (ptr) MersenneTwister(seed);
39 	return 0;
40 }
41 
sceKernelUtilsMt19937UInt(u32 ctx)42 u32 sceKernelUtilsMt19937UInt(u32 ctx) {
43 	VERBOSE_LOG(HLE, "sceKernelUtilsMt19937UInt(%08x)", ctx);
44 	if (!Memory::IsValidAddress(ctx))
45 		return -1;
46 	MersenneTwister *mt = (MersenneTwister *)Memory::GetPointer(ctx);
47 	return mt->R32();
48 }
49 
50 // TODO: This MD5 stuff needs tests!
51 
52 static md5_context md5_ctx;
53 
sceMd5Digest(u32 dataAddr,u32 len,u32 digestAddr)54 static int sceMd5Digest(u32 dataAddr, u32 len, u32 digestAddr) {
55 	DEBUG_LOG(HLE, "sceMd5Digest(%08x, %d, %08x)", dataAddr, len, digestAddr);
56 
57 	if (!Memory::IsValidAddress(dataAddr) || !Memory::IsValidAddress(digestAddr))
58 		return -1;
59 
60 	md5(Memory::GetPointer(dataAddr), (int)len, Memory::GetPointer(digestAddr));
61 	return 0;
62 }
63 
sceMd5BlockInit(u32 ctxAddr)64 static int sceMd5BlockInit(u32 ctxAddr) {
65 	DEBUG_LOG(HLE, "sceMd5BlockInit(%08x)", ctxAddr);
66 	if (!Memory::IsValidAddress(ctxAddr))
67 		return -1;
68 
69 	// TODO: Until I know how large a context is, we just go all lazy and use a global context,
70 	// which will work just fine unless games do several MD5 concurrently.
71 
72 	md5_starts(&md5_ctx);
73 	return 0;
74 }
75 
sceMd5BlockUpdate(u32 ctxAddr,u32 dataPtr,u32 len)76 static int sceMd5BlockUpdate(u32 ctxAddr, u32 dataPtr, u32 len) {
77 	DEBUG_LOG(HLE, "sceMd5BlockUpdate(%08x, %08x, %d)", ctxAddr, dataPtr, len);
78 	if (!Memory::IsValidAddress(ctxAddr) || !Memory::IsValidAddress(dataPtr))
79 		return -1;
80 
81 	md5_update(&md5_ctx, Memory::GetPointer(dataPtr), (int)len);
82 	return 0;
83 }
84 
sceMd5BlockResult(u32 ctxAddr,u32 digestAddr)85 static int sceMd5BlockResult(u32 ctxAddr, u32 digestAddr) {
86 	DEBUG_LOG(HLE, "sceMd5BlockResult(%08x, %08x)", ctxAddr, digestAddr);
87 	if (!Memory::IsValidAddress(ctxAddr) || !Memory::IsValidAddress(digestAddr))
88 		return -1;
89 
90 	md5_finish(&md5_ctx, Memory::GetPointer(digestAddr));
91 	return 0;
92 }
93 
sceKernelUtilsMd5Digest(u32 dataAddr,int len,u32 digestAddr)94 int sceKernelUtilsMd5Digest(u32 dataAddr, int len, u32 digestAddr) {
95 	DEBUG_LOG(HLE, "sceKernelUtilsMd5Digest(%08x, %d, %08x)", dataAddr, len, digestAddr);
96 
97 	if (!Memory::IsValidAddress(dataAddr) || !Memory::IsValidAddress(digestAddr))
98 		return -1;
99 
100 	md5(Memory::GetPointer(dataAddr), (int)len, Memory::GetPointer(digestAddr));
101 	return 0;
102 }
103 
sceKernelUtilsMd5BlockInit(u32 ctxAddr)104 int sceKernelUtilsMd5BlockInit(u32 ctxAddr) {
105 	DEBUG_LOG(HLE, "sceKernelUtilsMd5BlockInit(%08x)", ctxAddr);
106 	if (!Memory::IsValidAddress(ctxAddr))
107 		return -1;
108 
109 	// TODO: Until I know how large a context is, we just go all lazy and use a global context,
110 	// which will work just fine unless games do several MD5 concurrently.
111 
112 	md5_starts(&md5_ctx);
113 	return 0;
114 }
115 
sceKernelUtilsMd5BlockUpdate(u32 ctxAddr,u32 dataPtr,int len)116 int sceKernelUtilsMd5BlockUpdate(u32 ctxAddr, u32 dataPtr, int len) {
117 	DEBUG_LOG(HLE, "sceKernelUtilsMd5BlockUpdate(%08x, %08x, %d)", ctxAddr, dataPtr, len);
118 	if (!Memory::IsValidAddress(ctxAddr) || !Memory::IsValidAddress(dataPtr))
119 		return -1;
120 
121 	md5_update(&md5_ctx, Memory::GetPointer(dataPtr), (int)len);
122 	return 0;
123 }
124 
sceKernelUtilsMd5BlockResult(u32 ctxAddr,u32 digestAddr)125 int sceKernelUtilsMd5BlockResult(u32 ctxAddr, u32 digestAddr) {
126 	DEBUG_LOG(HLE, "sceKernelUtilsMd5BlockResult(%08x, %08x)", ctxAddr, digestAddr);
127 	if (!Memory::IsValidAddress(ctxAddr) || !Memory::IsValidAddress(digestAddr))
128 		return -1;
129 
130 	md5_finish(&md5_ctx, Memory::GetPointer(digestAddr));
131 	return 0;
132 }
133 
134 
135 static sha1_context sha1_ctx;
136 
sceKernelUtilsSha1Digest(u32 dataAddr,int len,u32 digestAddr)137 int sceKernelUtilsSha1Digest(u32 dataAddr, int len, u32 digestAddr) {
138 	DEBUG_LOG(HLE, "sceKernelUtilsSha1Digest(%08x, %d, %08x)", dataAddr, len, digestAddr);
139 
140 	if (!Memory::IsValidAddress(dataAddr) || !Memory::IsValidAddress(digestAddr))
141 		return -1;
142 
143 	sha1(Memory::GetPointer(dataAddr), (int)len, Memory::GetPointer(digestAddr));
144 	return 0;
145 }
146 
sceKernelUtilsSha1BlockInit(u32 ctxAddr)147 int sceKernelUtilsSha1BlockInit(u32 ctxAddr) {
148 	DEBUG_LOG(HLE, "sceKernelUtilsSha1BlockInit(%08x)", ctxAddr);
149 	if (!Memory::IsValidAddress(ctxAddr))
150 		return -1;
151 
152 	// TODO: Until I know how large a context is, we just go all lazy and use a global context,
153 	// which will work just fine unless games do several MD5 concurrently.
154 
155 	sha1_starts(&sha1_ctx);
156 
157 	return 0;
158 }
159 
sceKernelUtilsSha1BlockUpdate(u32 ctxAddr,u32 dataAddr,int len)160 int sceKernelUtilsSha1BlockUpdate(u32 ctxAddr, u32 dataAddr, int len) {
161 	DEBUG_LOG(HLE, "sceKernelUtilsSha1BlockUpdate(%08x, %08x, %d)", ctxAddr, dataAddr, len);
162 	if (!Memory::IsValidAddress(ctxAddr) || !Memory::IsValidAddress(dataAddr))
163 		return -1;
164 
165 	sha1_update(&sha1_ctx, Memory::GetPointer(dataAddr), (int)len);
166 	return 0;
167 }
168 
sceKernelUtilsSha1BlockResult(u32 ctxAddr,u32 digestAddr)169 int sceKernelUtilsSha1BlockResult(u32 ctxAddr, u32 digestAddr) {
170 	DEBUG_LOG(HLE, "sceKernelUtilsSha1BlockResult(%08x, %08x)", ctxAddr, digestAddr);
171 	if (!Memory::IsValidAddress(ctxAddr) || !Memory::IsValidAddress(digestAddr))
172 		return -1;
173 
174 	sha1_finish(&sha1_ctx, Memory::GetPointer(digestAddr));
175 	return 0;
176 }
177 
178 
179 const HLEFunction sceMd5[] = {
180 	{0X19884A15, &WrapI_U<sceMd5BlockInit>,          "sceMd5BlockInit",   'i', "x"  },
181 	{0XA30206C2, &WrapI_UUU<sceMd5BlockUpdate>,      "sceMd5BlockUpdate", 'i', "xxx"},
182 	{0X4876AFFF, &WrapI_UU<sceMd5BlockResult>,       "sceMd5BlockResult", 'i', "xx" },
183 	{0X98E31A9E, &WrapI_UUU<sceMd5Digest>,           "sceMd5Digest",      'i', "xxx"},
184 };
185 
Register_sceMd5()186 void Register_sceMd5() {
187 	RegisterModule("sceMd5", ARRAY_SIZE(sceMd5), sceMd5);
188 }
189