problem of the savedata decrypt program for 3.71 m33-2

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
kkHAIKE
Posts: 4
Joined: Wed Nov 07, 2007 3:13 pm

problem of the savedata decrypt program for 3.71 m33-2

Post by kkHAIKE »

I want to creat a program like the tile,so i found the PSPsdk\samples\savedata\decrypt\main.c,but the program is run on 1.5 kernel,i search many bbs,the program must be run on the user mode,but scechnnlsv() must be run ont the kernel mode,how to solve this

thanks :)
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

There are already several threads on how to call kernel functions from 3.xx user apps. If you'd bothered to look at them, you wouldn't have made this post. Read the threads, and then if you have a real question related to it, ask at the end of the thread related to the question.
kkHAIKE
Posts: 4
Joined: Wed Nov 07, 2007 3:13 pm

Post by kkHAIKE »

J.F,thx your Reply,i share the src for Everyone,help me please
the main.c
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <kubridge.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "decrypt.h"

PSP_MODULE_INFO("SaveDecrypt", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
PSP_HEAP_SIZE_KB(20480);

#define printf pspDebugScreenPrintf
unsigned int align16(unsigned int v)
{
return ((v + 0xF) >> 4) << 4;
}
// New format save with a key, supported by 2.0+ firmware
const char *decrypted = "ms0:/ULJM05156.bin";
const char *encrypted = "ms0:/PSP/SAVEDATA/ULJM05156/MHP2ND.BIN";
const unsigned char gamekey[] = {
0xE3, 0xB5, 0xCE, 0xFA, 0xE8, 0x4E, 0xB0, 0xA1,
0x85, 0x9A, 0xB7, 0x1B, 0xDD, 0xE6, 0xD8, 0xF3
};
int decrypt_file(const char *decrypted_filename,
const char *encrypted_filename,
const unsigned char *gamekey)
{
FILE *in, *out;
int len, aligned_len;
unsigned char *data, *cryptkey;
int retval;

/* Open file and get size */

if ((in = fopen(encrypted_filename, "r")) == NULL) {
retval = -1;
goto out;
}

fseek(in, 0, SEEK_END);
len = ftell(in);
fseek(in, 0, SEEK_SET);

if (len <= 0) {
retval = -2;
goto out1;
}

/* Allocate buffers */

aligned_len = align16(len);

if ((data = (unsigned char *) memalign(0x10, aligned_len)) == NULL) {
retval = -3;
goto out1;
}

if ((cryptkey = (unsigned char *) memalign(0x10, 0x10)) == NULL) {
retval = -4;
goto out2;
}

/* Fill buffers */

if (gamekey != NULL)
memcpy(cryptkey, gamekey, 0x10);

memset(data + len, 0, aligned_len - len);
if (fread(data, 1, len, in) != len) {
retval = -5;
goto out3;
}

/* Do the decryption */

if ((retval = decrypt_data( gamekey ? 3 : 1,
data, &len, &aligned_len,
gamekey ? cryptkey : NULL)) < 0) {
retval -= 100;
goto out3;
}

/* Write the data out. decrypt_data has set len correctly. */

if ((out = fopen(decrypted_filename, "w")) == NULL) {
retval = -6;
goto out3;
}

if (fwrite(data, 1, len, out) != len) {
retval = -7;
goto out4;
}

/* All done. Return file length. */
retval = len;
out4:
fclose(out);
out3:
free(cryptkey);
out2:
free(data);
out1:
fclose(in);
out:
return retval;
}
int waitbutton(int mask) {
SceCtrlData paddata;
do {
sceDisplayWaitVblankStart();
sceCtrlReadBufferPositive(&paddata, 1);
} while((paddata.Buttons & mask));
do {
sceDisplayWaitVblankStart();
sceCtrlReadBufferPositive(&paddata, 1);
} while(!(paddata.Buttons & mask));
return paddata.Buttons;
}

int main(int argc, char *argv[])
{
int i, ret;
int loadResult;

pspDebugScreenInit();
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL);

printf("MHP2Save Decrypt\n");
loadResult = sceKernelStartModule(kuKernelLoadModule("flash0:/kd/chnnlsv.prx", 0, 0), 0, 0, 0, 0);
printf("%d",loadResult);
printf("Will decrypt: %s\n", encrypted);
printf(" Using key:");
if(gamekey) {
for (i = 0; i < 0x10; i++)
printf(" %02x", gamekey);
} else {
printf(" none");
}
printf("\n\n");
printf(" Output file: %s\n\n", decrypted);
printf("Press X to continue, or O to quit.\n\n");

if (waitbutton(PSP_CTRL_CROSS | PSP_CTRL_CIRCLE) & PSP_CTRL_CIRCLE)
sceKernelExitGame();;

printf("Working...\n\n");
loadResult = sceKernelStartModule(kuKernelLoadModule("decrypt.prx", 0, 0), 0, 0, 0, 0);
printf("%d",loadResult);
ret = decrypt_file(decrypted, encrypted, gamekey);
if(ret < 0) {
printf("Error: decrypt_file() returned %d\n\n", ret);
} else {
printf("Successfully wrote %d bytes to\n", ret);
printf(" %s\n\n", decrypted);
}

printf("Press any button to quit\n");
waitbutton(-1);
sceKernelExitGame();
return 0;
}


the decrypt.S
.set noreorder

#include "pspstub.s"

STUB_START "decrypt",0x40090000,0x00010005
STUB_FUNC 0xA9D1F3AE,decrypt_data
STUB_END

the main.c makefile
TARGET = main
OBJS = main.o decrypt.o

INCDIR = ../include
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

LIBDIR =../lib
LIBS =-lpspkubridge
LDFLAGS =

PSP_FW_VERSION = 371

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE =MPH2 Save Decrypt

BUILD_PRX = 1

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak


the MODULE decrypt.c
#include "decrypt.h"
#include <pspkernel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <pspchnnlsv.h>

PSP_MODULE_INFO("Decrypt",PSP_MODULE_KERNEL, 1, 0);
PSP_MAIN_THREAD_ATTR(0);

const char *decrypted = "ms0:/ULJM05156.bin";
const char *encrypted = "ms0:/PSP/SAVEDATA/ULJM05156/MHP2ND.BIN";
const unsigned char gamekey[] = {
0xE3, 0xB5, 0xCE, 0xFA, 0xE8, 0x4E, 0xB0, 0xA1,
0x85, 0x9A, 0xB7, 0x1B, 0xDD, 0xE6, 0xD8, 0xF3
};

int sceChnnlsv_E7833020(pspChnnlsvContext1 *ctx, int mode);
int sceChnnlsv_ABFDFC8B(pspChnnlsvContext2 *ctx, int mode1, int mode2,
unsigned char *hashkey, unsigned char *cipherkey);
int sceChnnlsv_850A7FA1(pspChnnlsvContext2 *ctx, unsigned char *data, int len);
int sceChnnlsv_21BE78B4(pspChnnlsvContext2 *ctx);
int sceChnnlsv_F21A1FCA(pspChnnlsvContext1 *ctx, unsigned char *data, int len);
/* Read, decrypt, and write a savedata file. See main.c for example usage. */


/* Do the actual hardware decryption.
mode is 3 for saves with a cryptkey, or 1 otherwise
data, dataLen, and cryptkey must be multiples of 0x10.
cryptkey is NULL if mode == 1.
*/
int decrypt_data(unsigned int mode,
unsigned char *data,
int *dataLen,
int *alignedLen,
unsigned char *cryptkey)
{
pspChnnlsvContext1 ctx1;
pspChnnlsvContext2 ctx2;

/* Need a 16-byte IV plus some data */
if (*alignedLen <= 0x10)
return -1;
*dataLen -= 0x10;
*alignedLen -= 0x10;

/* Set up buffers */
memset(&ctx1, 0, sizeof(pspChnnlsvContext1));
memset(&ctx2, 0, sizeof(pspChnnlsvContext2));

/* Perform the magic */
if (sceChnnlsv_E7833020(&ctx1, mode) < 0)
return -2;
if (sceChnnlsv_ABFDFC8B(&ctx2, mode, 2, data, cryptkey) < 0)
return -3;
if (sceChnnlsv_F21A1FCA(&ctx1, data, 0x10) < 0)
return -4;
if (sceChnnlsv_F21A1FCA(&ctx1, data + 0x10, *alignedLen) < 0)
return -5;
if (sceChnnlsv_850A7FA1(&ctx2, data + 0x10, *alignedLen) < 0)
return -6;

/* Verify that it decrypted correctly */
if (sceChnnlsv_21BE78B4(&ctx2) < 0)
return -7;

/* If desired, a new file hash from this PSP can be computed now:
if (sceChnnlsv_C4C494F8(ctx1, newhash, cryptkey) < 0)
return -8;
*/

/* The decrypted data starts at data + 0x10, so shift it back. */
memmove(data, data + 0x10, *dataLen);

/* All done */
return 0;
}
int module_start(SceSize args, void *argp)
{
return 0;
}


int module_stop(void)
{
return 0;
}

decrypt.h
int decrypt_data(unsigned int mode,
unsigned char *data,
int *dataLen,
int *alignedLen,
unsigned char *cryptkey);



exports.exp
# Define the exports for the prx
PSP_BEGIN_EXPORTS

# These four lines are mandatory (although you can add other functions like module_stop)
# syslib is a psynonym for the single mandatory export.
PSP_EXPORT_START(syslib, 0, 0x8000)
PSP_EXPORT_FUNC_HASH(module_start)
PSP_EXPORT_VAR_HASH(module_info)
PSP_EXPORT_END

# Export our function
PSP_EXPORT_START(decrypt, 0, 0x4001)
PSP_EXPORT_FUNC(decrypt_data)
PSP_EXPORT_END

PSP_END_EXPORTS

sceChnnlsv371.S
.set noreorder

#include "pspimport.s"

IMPORT_START "sceChnnlsv",0x40010000
IMPORT_FUNC "sceChnnlsv",0xE7833020,sceChnnlsv_E7833020
IMPORT_FUNC "sceChnnlsv",0xF21A1FCA,sceChnnlsv_F21A1FCA
IMPORT_FUNC "sceChnnlsv",0xABFDFC8B,sceChnnlsv_ABFDFC8B
IMPORT_FUNC "sceChnnlsv",0x850A7FA1,sceChnnlsv_850A7FA1
IMPORT_FUNC "sceChnnlsv",0x21BE78B4,sceChnnlsv_21BE78B4

makefile
TARGET = decrypt
OBJS = decrypt.o exports.o sceChnnlsv371.o

BUILD_PRX = 1
PRX_EXPORTS=exports.exp

USE_KERNEL_LIBC=1
USE_KERNEL_LIBS=1

INCDIR =
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)


LIBDIR =
LIBS =
LDFLAGS = -mno-crt0 -nostartfiles

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak

all:
psp-build-exports -s $(PRX_EXPORTS)
@cp $(TARGET).S ../decrypt/
@cp $(TARGET).h ../decrypt/
@rm -f $(TARGET).S
@rm -f *.o
@rm -f *.elf


now problem as follows
1.i don't know that the "flash0:/kd/chnnlsv.prx" was load Success,the ret still >0
2.when load "decrypt.prx",still return 8002013C,so i can know that "flash0:/kd/chnnlsv.prx" can't load maybe
3.more and more


help me!
kkHAIKE
Posts: 4
Joined: Wed Nov 07, 2007 3:13 pm

Post by kkHAIKE »

3.71m33-3 out today
i use new sdk for make,it's success to load my prx,
but but but but but ``````a very very very very very veyr big problem
the decrypt Calculation is too old,the Result of mypro Different Result of laboko~~~~~~
-----------------------------------
now,I should do what
Post Reply