Now we have all keys which are required to decrypt all files. So what we should also know?
ENC encryption uses an AES algorithm in CBC mode and the initialization vector of all zeros. The actual process of decryption of CONFIG and ISO.BIN.ENC started at seeking to the offset of 16384 bytes. There is a first meta data section so we should use the meta key as key for AES and decrypt the entire segment of 16384 bytes. As I said before each meta data sections contains of some entries and each entry have a size of 32 bytes. Each entry contains a SHA-1 hash (20 bytes) of the corresponding entire encrypted file data section and all these sections are located after this meta data section. After the SHA-1 hash we can see the section index of the corresponding file data section (4 bytes). The rest is padded of zeros. After decryption of the meta data section we can decrypt all file data sections after it. Now we should use the data key! Before the actual decryption we can check the SHA-1 hash of each encrypted file data section and see if they matched to the hashes at entry table of the meta data section. If the actual file size of the disc image is not a multiple of 16834 bytes then we have less entries inside the latest meta data section. After we finished the decryption of first 512 file data sections we can started decryption of the second meta data section and set of 512 file data sections after it and so on. I recommend to write decrypted meta data entries to another file than in the same file as file data section. It will make a process more easier. After decryption you should truncate your actual file to the data size specified at the header. Now you got an UDF disc image and you can mount it on your PC, for example.
So what is the next step? The next step is the decryption of encrypted virtual memory cards. Each PS2 classics package contains two empty encrypted virtual memory cards which located at SCEVMC0.VME and SCEVMC1.VME. As far I see they are identical for all games so we can use templates for all new virtual memory cards but only encrypts them with the new klicensee. To decrypt virtual memory cards you need to read an each segment of 16384 bytes and apply an AES encryption in CBC mode too but for this case you should use the VMC key. After decryption you should see Sony PS2 Memory Card Format 1.2.0.0 at the top of file.
Well, I attached a draft script for decryption of ENC/VME files. It was written for Python 2.7 and requires CryptoPlus (can be downloaded from:
http://repo.or.cz/w/python-cryptoplus.git) and "ecdsa" (use EasyInstall or another package manager) libraries. I intentionally left all keys as SHA-1 hashes because of legal issues but you can find all keys by yourself using my hints. My script uses CONFIG/ISO.BIN.ENC/SCEVM0.VME/SCEVM1.VME file and klicensee file as input parameters. I hope that someone will create tools for that.
To use the script you need to create a file with name vsh.curves and put the contents of the curve table from VSH (get it from
http://ps3devwiki.com/wiki/Keys at vsh pub + curvetable) and replace all hashes of keys by their real values (see FIXME comments). Also replace three NPDRM OMAC keys and VSH public key by their values from
http://ps3devwiki.com/wiki/Keys.
I think that creation of PS2 remastering tool can lead us to getting the fully working games on our consoles but it requires testing. I recommend to create a static klicensee which can be used to encrypt all images in the same manner (static klicensee can also be implemented by patching VSH/LV2 at runtime, for example). After generating a klicensee you should create all keys based on it.
To build an encrypted disc image you should dump the original disc image and then append zero bytes to the end to make it multiple of 16384 bytes. Then you need to encrypt each of 512 segments using the generated data key. Then you should calculate SHA-1 hashes of each encrypted segment and generate meta data section for each pair of segment hash and segment index. After this you need to encrypt meta data section and so on. At the end you need to write an original disc image size to the header, write a content id for it and generate hashes at the file header.
After building ISO.BIN.ENC file you should create a file with the title id and pad it with zero bytes from the right side to get 12 bytes total. Then you need to create an EDAT container for this file. Hint: you can see a correct title id when mounting a disc image on your PC and looking at SYSTEM.CNF of it.
Unfortunately, I hadn't time to see what the CONFIG file does so I will skip this step. I only know that this file is optional or can be empty inside (after decryption).
You are not required (and you simply can't do it) to generate a valid ECDSA signature for files because all custom firmwares are patched to skip the ECDSA check.
Will be nice to be able to generate a game package for your PS2 game too if everything will works fine. Remember, that some flags at PS2 pkg format can be different.
http://pastie.org/private/ykyyrbim7rkotvcnw8m8w
Credits to:
graf_chokolo, fail0verflow, JuanNadie, ps3dev.net, glevand and all my friends (you know who you are).