SELF - SPRX: Difference between revisions

From PS3 Developer wiki
Jump to navigation Jump to search
(introduction)
(→‎SCE specific segment types (p_type): update PT_SCE_MODULE_PARAM (PS4))
 
(184 intermediate revisions by 21 users not shown)
Line 1: Line 1:
[[Category:Software]]
SELF stands for Signed Executable and Linkable Format. SELF is the file format used by the executables on the PS3 and PS Vita. SPRX stands for Signed Playstation Relocatable eXecutable. SPRX is a file extension, derived from PRX, of SELF files that are loaded as "libraries" rather than "applications". SELF files of applications are often named "EBOOT.BIN" as in "encrypted BOOT.BIN" as a legacy of PS1, PS2 and PSP executables filenames.
 
SELF stands for Signed Executable and Linkable Format.


[[File:Self.png|thumb|alt=A screenshot of f0f's presentation at CCC2010.]]
[[File:Self.png|thumb|alt=A screenshot of f0f's presentation at CCC2010.]]
[[File:Self_hdr.png|thumb]]


=Introduction=
= Introduction =


It is the format used by the executables on the PS3
SELF consists of a [[Certified File]] header with an Extended Header followed by the encapsulated ELF file. ELF sections can be compressed using gzip. A SELF is usually encrypted and signed, but [[#fSELF|fSELF]] are unsigned SELFs. ELF sections can be encrypted using AES128 and signed using ECDSA160 or RSA2048 + HMAC-SHA1 or HMAC-SHA256. A SELF has a specific header called Extended header where it stores all the parameters for this program.
It has a specific header here called SCE header where it stores all the parameters for this process


*SCE Header
*Extended Header
It consist on information regarding the structure and offsets of the self. The first part is in plaintext until you reach Metadata Info.  
Extended Header consists of information regarding the structure and offsets of the SELF. The first part is in plaintext until the Encryption Root Header.


*Metadata Info
*Encryption Root Header
Metadata Info is itself under AES 256 CBC. This part contains KEY + IV to further decrypt the header using AES 128 CTR.
Encryption Root Header is encrypted under an AES256CBC layer. It contains key and IV to further decrypt the Certification data using AES128 (CBC or CTR).


*Metadata
*Certification data
The metadata header, Metadata Section Headers, Section Hash, Capabilities and Signature are under this AES 128 CTR layer and is decrypted with the key above.  
Certification Header, Certification Body and CF Signature are encrypted under an AES128 (CBC or CTR) layer with the Encryption Root Header key and IV.


*Metadata Header
*Certification Header
Metadata header contains the info required to authenticate the header and the structure of the metadata. The signature is ECDSA of the SHA1 hash of the self file starting at 0x0 and ending at 0x0+signatureInputLength
Signature is an ECDSA160/RSA2048 signature of the SHA1/SHA256 digest of the SELF file starting at offset 0 and ending at offset sign_offset.


*Data Sections
*Data Segments
The data sections might be encrypted using AES 128 CTR and/or compressed. HMAC-SHA1 is used to authenticate they have not been modified.
Data segments can be encrypted and/or compressed. SHA1/HMAC-SHA1/HMAC-SHA256 is used to ensure that they have not been modified.
 
Note: not only ELF/PRX files can be signed with this format, other known files with SCE header are :
* revoke (e.g. RL_FOR_PACKAGE.img/RL_FOR_PROGRAM.img and pkg.srvk/prog.srvk)
* spp (e.g. default.spp)
* package (e.g. .pkg/.spkg_hdr.X)
* edat
 
----


= Cryptography =
= Cryptography =


Here is a small summary on how the self cryptography works.  
Here is a small summary on how the SELF cryptography works.  


Basically here are the steps being involved by the loaders:  
Basically here are the steps being involved by the loaders:  


Loaders all have a static key and iv called respectively <abbr title="ERK - 256bit Encryption Round Key">erk</abbr> and <abbr title="RIV - 128bit Reset Initialization Vector">riv</abbr>, those are keys for the first decryption step which are used to decrypt the very first 0x40 bytes of the self's metadata using <abbr title="AES256CBC - Advanced Encryption Standard - 256 bit - Cipher-block chaining">AES256CBC</abbr>
Loaders all have a static key and iv called respectively <abbr title="ERK - 256bit Encryption Round Key">erk</abbr> and <abbr title="RIV - 128bit Reset Initialization Vector">riv</abbr>. Those are keys for the first decryption step which is to decrypt the Encryption Root Header using <abbr title="AES256CBC - Advanced Encryption Standard - 256 bit - Cipher-block chaining">AES256CBC</abbr>. When the SELF is protected by [[NPDRM]] ?instead of using static keys?, erk and riv are derived by decrypting klicensee using "NP_klic_key".


Then the result is used as a key and iv to decrypt the rest of the metadata using <abbr title="AESCTR - Advanced Encryption Standard - Counter Mode">AESCTR</abbr>, finally the decrypted metadata contains the keys and iv for each data sections which are still decrypted through AES128CTR. This security model is based on the fact that the first 0x40 bytes of the self's metadata once decrypted by the static AES256CBC key in the loader should never be the same from one binary to the other. The same goes for any other value used as an AES128CTR key or iv.  
Then the result is used as key and iv to decrypt the rest of the Certification data using <abbr title="AESCTR - Advanced Encryption Standard - Counter Mode">AES128CTR</abbr> (PS3) or AES128CBC (PS Vita). Finally the decrypted Certification data contains the key and iv for each data segments which are still decrypted following the Segment Certification Header information. This security model is based on the fact that the Certification Root Header once decrypted by the static AES256CBC key in the loader should never be the same from one binary to the other. The same goes for any other value used as an AES128 key or iv.


Loaders are also involved with inflating the binaries using zlib.  
Loaders are also involved with inflating the binaries using zlib.


The self authenticity is based on other independent steps, <abbr title="HMAC-SHA1 - Hash-based Message Authentication Code - Secure Hash Algorithm 1">HMAC-SHA1</abbr> and <abbr title="ECDSA - Elliptic Curve Digital Signature Algorithm">ECDSA</abbr> for the actual signature.
The SELF authenticity is based on other independent steps, <abbr title="HMAC-SHA1 - Hash-based Message Authentication Code - Secure Hash Algorithm 1">HMAC-SHA1</abbr> or HMAC-SHA256 of the data segments and <abbr title="ECDSA - Elliptic Curve Digital Signature Algorithm">ECDSA160</abbr> or RSA2048 for the signature of the header and the Certification data.


== Short references ==
== Short references ==
*[http://en.wikipedia.org/wiki/Advanced_Encryption_Standard AES]
*[http://en.wikipedia.org/wiki/Advanced_Encryption_Standard AES]
*[http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation Block cipher modes of operation]
*[http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation Block cipher modes of operation]
Line 55: Line 46:
More indepth Online course about encryption in generic (also AES/ECDSA): [https://engineering.purdue.edu/kak/compsec/Lectures.html Lecture Notes on Computer and Network Security by Avinash Kak]
More indepth Online course about encryption in generic (also AES/ECDSA): [https://engineering.purdue.edu/kak/compsec/Lectures.html Lecture Notes on Computer and Network Security by Avinash Kak]


= File Format =
= fSELF =
 
fSELF stands for fake signed ELF. It is the format output by Software Development Kits and ran on activated TestKits and DevKits. As it name suggests, a fSELF is not signed and usually not encrypted. If a fSELF was signed, that would imply that SDK embeds the private keys required to sign it.
 
A fSELF has Attribute set to 0x8000. ?always?
 
A fSELF has usually less rights than a System SELF but more than a finalized Non-System SELF. The OS recognizes a fSELF by looking at its program-authority-id.
 
To document more...
 
= Location =
 
== PS3 ==
 
Files with extensions: eboot.bin, *.self, *.sprx.
 
See also [[SELFs inside ELFs]].
 
== PS Vita ==
 
Files with extensions: eboot.bin, *.self, *.suprx, *.skprx, *.skarx.
 
= File Format =
 
Notes:
* Warning: PS3 uses big endian, PS Vita uses little endian.
* Encapsulated ELF header fields are useless (only the EI_CLASS EI_DATA and EI_VERSION fields are checked).
 
== PS3 early Program Type 3 SELFs ==
 
<pre>
SDK 0.60: No sce version nor elf digest
SDK 0.8X: No sce version, with 0x30 elf digest
SDK 0.9X: With sce version and 0x40 elf digest (scetool produces this type)
</pre>
 
== Special SELF samples ==
 
=== Warhawk public beta release 012 ===


Notes:  
Oldest official game SELF (although not even NPDRM protected!!!) found by now: 2007-06-08.


*Numbers are stored in big endian format.
*https://web.archive.org/web/*/http://download-prod.online.scea.com/medius-patch/warhawk-pubeta/warhawk//*
*https://web.archive.org/web/20070711084313/http://download-prod.online.scea.com/medius-patch/warhawk-*pubeta/warhawk/20070608_r012/NPUA80093_113_release.self
*https://web.archive.org/web/20070711084217/http://download-prod.online.scea.com/medius-patch/warhawk-pubeta/warhawk/20070608_r012/NPUA80093_113_PARAM.sfo
*https://web.archive.org/web/20070621155928/http://download-prod.online.scea.com/medius-patch/warhawk-pubeta/warhawk/20070608_r012/NPUA80093.cfg


== SELF/SCE Header ==
*https://twitter.com/Mathieulh/status/1057395385319211008?s=20
 
== Extended Header ==
 
Extended Header offsets are relative to [[Certified File]] start.


=== Struct ===
=== Struct ===
typedef struct {
  uint32_t magic;                /* 53434500 = SCE\0 */
  uint32_t hdr_version;          /* header version */
  uint16_t key_revision;          /* 0x0 retail (type 0)
                                  * 0x1 retail (0.92-3.30)
                                  * 0x2 retail (type 1)
                                  * 0x3 unknown (npdrm1?)
                                  * 0x4 retail (3.40-3.42)
                                  * 0x5 unknown (npdrm1?)
                                  * 0x6 unknown (npdrm2?)
                                  * 0x7 retail (3.50)
                                  * 0x8 unknown (npdrm1?)
                                  * 0x9 unknown (npdrm2?)
                                  * 0xa retail (3.55)
                                  * 0xb unknown (npdrm1?)
                                  * 0xc unknown (npdrm2?)
                                  * 0xd retail (3.56)
                                  * 0xe unknown (npdrm1?)
                                  * 0xf unknown (npdrm2?)
                                  * 0x10 retail (3.60-3.61)
                                  * 0x11 unknown (npdrm1?)
                                  * 0x12 unknown (npdrm2?)
                                  * 0x13 retail (3.65)
                                  * 0x14 unknown (npdrm1?)
                                  * 0x15 unknown (npdrm2?)
                                  * 0x16 retail (3.70-3.74)
                                  * 0x17 unknown (npdrm1?)
                                  * 0x18 unknown (npdrm2?)
                                  * 0x19 retail (4.00)
                                  * 0x8000 DEBUG (devkit)
                                  */
  uint16_t header_type;          /* 1 self, 2 unknown, 3 pkg */
  uint32_t metadata_offset;      /* metadata offset */
  uint64_t header_length;        /* self header length */
  uint64_t elf_length;            /* ELF file length */


   uint64_t unknown;               /* UNKNOWN always 3 */
<source lang="C">
   uint64_t appinfo_offset;       /* app info offset */
typedef struct { // Size is 0x50 bytes
   uint64_t elf_offset;           /* ELF #1 offset */
   uint64_t ext_hdr_version;
   uint64_t phdr_offset;           /* program header offset */
   uint64_t program_identification_hdr_offset;
   uint64_t shdr_offset;           /* section header offset */
   uint64_t ehdr_offset;
   uint64_t section_info_offset;   /* section info offset */
   uint64_t phdr_offset;
   uint64_t sceversion_offset;     /* version offset */
   uint64_t shdr_offset;
   uint64_t controlinfo_offset;   /* control info offset */
   uint64_t segment_ext_hdr_offset;
   uint64_t controlinfo_length;   /* control length */
   uint64_t version_hdr_offset;
   uint64_t padding;               /* padding */
   uint64_t supplemental_hdr_offset;
} __attribute__((packed)) SELF;
   uint64_t supplemental_hdr_size;
   uint64_t padding;
} __attribute__((packed)) ext_hdr;
</source>


===Table===
=== Table ===


{| class="wikitable"
{| class="wikitable"
|-
! field !! offset !! type !! notes
! field !! offset !! type !! notes
|-
|-
| sce_magic || 0x0 || u32 || Must be "SCE\0"
| Extended Header version || 0x0 || u64 || 3 for PS3, 4 for PS Vita
|-
|-
| header_version || 0x4 || u32 || This must be 2 or the Self loader will abort
| Program Identification Header offset || 0x8 || u64 || Offset to Program Identification Header.
|-
|-
| key_revision  || 0x8  || u16|| This corresponds to the revision of the key to decrypt the [[SELF_File_Format_and_Decryption#Metadata_Header|metadata header]], see: [[Keys#Appldr|appldr]], [[Keys#Key_Table_Structure|key structure in loaders]]
| ELF Header offset || 0x10 || u64 || Offset to ELF Header.
*known appldr keys:  0x0000 - 0x000F
*known NPDRM keys: 0x0001, 0x0004, 0x0007, 0x000A
*debug: 0x8000
key_obj = key_table[sdk_type]
|-
| header_type || 0xA || u16 || 1 self, 2 rvk, 3 pkg, 4 spp
|-
| metadata_offset || 0xC  || u32 || Offset to the checksums. Must be at least 20 bytes before the end of the header
|-
| header_length || 0x10 || u64 || This is the length of the header (including the fake elf headers)
*The total length of the header must be less that 8KB
|-
| Encrypted size || 0x18 || u64 || The size of the encrypted part of the self file.
|-
| unknown || 0x20 || u64 || Must be 3, or 80010007 error
|-
| app_info_offset || 0x28 || u64 || An offset in the header, usually at 0x70. See App info header.
|-
| elf_offset || 0x30 || u64 || offset to the elf header
|-  
|-  
| phdr_offset  || 0x38 || u64 || offset to phdr
| Program Header offset || 0x18 || u64 || Offset to ELF Program Header.
|-
|-
| shdr_offset  || 0x40 || u64 || offset to shdr
| Section Header offset || 0x20 || u64 || Offset to ELF Section Header.
|-
|-
| encrypted phdr sizes/offsets table offset || 0x48 || u64 || Offset to a table which maps phdr entries to the actual offset/size within the encrypted fself.
| Segment Extended Header offset || 0x28 || u64 || Offset to Segment Extended Header.
Because fselfs can be compressed, they might not match the values listed within the elf.  
|-
|-
| sceversion header || 0x50 || u64 || Offset to a header which contains some version information, including an offset to the .sceversion section of the encrypted elf.  
| Version Header offset || 0x30 || u64 || Offset to Version Header.
The playstation 3 doesn't care about this, and doesn't even check the header
|-
|-
| control info offset   || 0x58 || u64 ||  
| Supplemental Header offset || 0x38 || u64 || Offset to Supplemental Header.
|-
|-
| control info length  || 0x60  || u64 ||  
| Supplemental Header size || 0x40 || u64 || Size of Supplemental Header.
|-
|-
| padding || 0x68 || u64 ||
| Padding || 0x48 || u64 ||
|}
|}


===comments===
=== Comments ===
The real ELF data is located after the SCE header (see header size). It is encrypted, unless the flags are 0x8000. unfself works by cutting the SCE header from the (fake) SELF.


== App Info  ==
The real ELF data is located after the Extended Header (see Extended Header size in [[Certified File#Header|Certified File Header]]). It is encrypted, unless [[Certified File]] attribute is 0x8000 (fake CF). unfself works by cutting the SCE header from the fSELF and if needed decompressing segments.


===Struct===
== Program Identification Header ==
typedef struct {
 
  uint64_t authid;               /* auth id */
Temporary name was App Info. Official name is Program Identification Header.
  uint32_t vendor_id;             /* vendor id */
 
  uint32_t self_type;             /* app type
=== Struct ===
                                  * 1 level0,
 
                                  * 2 level1,
<source lang="C">
                                  * 3 level2,
typedef struct {
                                  * 4 application,
  uint64_t program_authority_id;
                                  * 5 isolated SPU module,
  uint32_t program_vender_id;
                                  * 6 secure loader,
  uint32_t program_type;
                                  * 7 unknown, handled by appldr,
  uint64_t program_sceversion;
                                  * 8 NPDRM app */
  uint64_t padding;
  uint64_t version;               /* app version */
} __attribute__((packed)) program_identification_header;
  uint64_t padding;               /* UNKNOWN */
</source>
} __attribute__((packed)) APP_INFO;


===Table===
=== Table ===


{| class="wikitable"
{| class="wikitable"
! field !! offset !! type !! notes !! example
|-
|-
! field !! offset !! type !! notes
| program_authority_id || 0x00 || u64 || See [[Program Authority Id]] || ex: 21 00 00 10 1C CA 01 30
|-
|-
| authid    || 0x00 || u64
| program_vender_id || 0x08 || u32 || See [[Program Vender Id]] || ex: 00 00 00 00
|-
|-
|unknown    || 0x08 ||u32
| program_type || 0x0C || u32 || See [[Program Type]] || ex: 08 00 00 00
|-
|-
|app_type  || 0x0c ||u32 ||
| program_sceversion || 0x10 || u64 || ?SDK version or app version? [[Revision versus Version|version]] ex: 01.02 is translated by make_fself.exe to 02 01 00 00 || ex: 0x0000036000000000 (3.600.011), 0x0000009450000000 (0.945.040)
                          *1 -- level 0
                          *2 -- level 1
                          *3 -- level 2
                          *4 -- application
                          *5 -- isolated SPU module
                          *6 -- secure loader
                          *8 -- NP-DRM application
|-
|-
|app_version ||0x10 ||u64  
| padding || 0x18 || u64 || Padding || 00 00 00 00 00 00 00 00
|}
|}


===Comments===
=== Comments ===


Aligned to 0x10 bytes.
Aligned to 0x10 bytes.


== ELF Header ==
== ELF Header ==
 
=== Struct ===
 
==== PS3 ====


===Struct===
<source lang="C">
  typedef struct {
typedef struct {
     uint8_t e_ident[16];              /* ELF identification */
     uint8_t e_ident[16];              /* ELF identification */
     uint16_t e_type;                  /* object file type */
     uint16_t e_type;                  /* object file type */
Line 219: Line 197:
     uint64_t e_phoff;                /* program header offset */
     uint64_t e_phoff;                /* program header offset */
     uint64_t e_shoff;                /* section header offset */
     uint64_t e_shoff;                /* section header offset */
     uint16_t e_flags;                /* processor-specific flags */
    uint32_t e_flags;                /* processor-specific flags */
     uint32_t e_ehsize;                /* ELF header size */
    uint16_t e_ehsize;                /* ELF header size */
    uint16_t e_phentsize;            /* size of program header entry */
    uint16_t e_phnum;                /* number of program header entries */
    uint16_t e_shentsize;            /* size of section header entry */
    uint16_t e_shnum;                /* number of section header entries */
    uint16_t e_shstrndx;              /* section name string table index */
} __attribute__((packed)) ELF;
</source>
 
See also specifications:
[http://www.sco.com/developers/gabi/latest/ch4.eheader.html ELF Header]
[http://www.openwatcom.com/ftp/devel/docs/elf-64-gen.pdf ELF-64 Object File Format]
 
==== PS Vita ====
 
<source lang="C">
typedef struct {
    uint8_t e_ident[16];              /* ELF identification */
     uint16_t e_type;                  /* object file type */
    uint16_t e_machine;              /* machine type */
    uint32_t e_version;              /* object file version */
    uint32_t e_entry;                /* entry point address */
    uint32_t e_phoff;                /* program header offset */
    uint32_t e_shoff;                /* section header offset */
    uint32_t e_flags;                /* processor-specific flags */
     uint16_t e_ehsize;                /* ELF header size */
     uint16_t e_phentsize;            /* size of program header entry */
     uint16_t e_phentsize;            /* size of program header entry */
     uint16_t e_phnum;                /* number of program header entries */
     uint16_t e_phnum;                /* number of program header entries */
Line 226: Line 229:
     uint16_t e_shnum;                /* number of section header entries */
     uint16_t e_shnum;                /* number of section header entries */
     uint16_t e_shstrndx;              /* section name string table index */
     uint16_t e_shstrndx;              /* section name string table index */
  } __attribute__((packed)) ELF;
} __attribute__((packed)) ELF;
</source>
 
{| class="wikitable sortable"
! Name of the variable !! Offset !! Size !! Notes
|-
| e_ident[0..3] || 0 || 4 || Magic.
|-
| e_ident[4] || 4 || 1 || Class Type. Must be [ELFCLASS32 = 0x01].
|-
| e_ident[5] || 5 || 1 || Data Type. Must be [ELFDATA2LSB = 0x01].
|-
| e_ident[6] || 6 || 1 || File version. Must be 1.
|-
| e_ident[7...15] || 7 || 9 || unused
|-
| e_type || 0x10 || 2 || See SCE-specific e_type.
|-
| e_machine || 0x12 || 2 || Must be [EM_ARM = 0x0028].
|-
| e_version || 0x14 || 4 || Must be 0x1.
|-
| e_entry || 0x18 || 4 || Address to jump to in order to start program (warning: not always set).
|-
| e_phoff || 0x1C || 4 || Boundary checked, but unused (already given by SELF header). ?and for fSELF?
|-
| e_shoff || 0x20 || 4 || unused
|-
| e_flags || 0x24 || 4 || unused
|-
| e_ehsize || 0x28 || 2 || Must be sizeof(Elf32_Ehdr) = 0x0034.
|-
| e_phentsize || 0x2A || 2 || Must be sizeof(Elf32_Phdr) = 0x0020.
|-
| e_phnum || 0x2C || 2 || Count of Program Header in this ELF.
|-
| e_shentsize || 0x2E || 2 || unused
|-
| e_shnum || 0x30 || 2 || unused
|-
| e_shstrndx || 0x32 || 2 || unused
|}
 
See also [https://wiki.henkaku.xyz/vita/images/a/a2/Vita_SDK_specifications.pdf yifan lu's outdated specifications for PS Vita fSELF]


===Table===
=== SCE specific ELF types (e_type) ===


===Comments===
<source lang="C">
/* SCE-specific definitions for e_type: */
#define ET_SCE_EXEC 0xFE00 /* SCE Executable - PRX2 */
#define ET_SCE_RELEXEC 0xFE04 /* SCE Relocatable Executable - PRX2 */
#define ET_SCE_STUBLIB 0xFE0C /* SCE SDK Stubs */
#define ET_SCE_DYNEXEC 0xFE10 /* SCE EXEC_ASLR (PS4 Executable with ASLR) */
#define ET_SCE_DYNAMIC 0xFE18 /* ? */
#define ET_SCE_IOPRELEXEC 0xFF80 /* SCE IOP Relocatable Executable */
#define ET_SCE_IOPRELEXEC2 0xFF81 /* SCE IOP Relocatable Executable Version 2 */
#define ET_SCE_EERELEXEC 0xFF90 /* SCE EE Relocatable Executable */
#define ET_SCE_EERELEXEC2 0xFF91 /* SCE EE Relocatable Executable Version 2 */
#define ET_SCE_PSPRELEXEC 0xFFA0 /* SCE PSP Relocatable Executable */
#define ET_SCE_PPURELEXEC 0xFFA4 /* SCE PPU Relocatable Executable */
#define ET_SCE_ARMRELEXEC 0xFFA5 /* ?SCE ARM Relocatable Executable (PS Vita System Software earlier or equal 0.931.010) */
#define ET_SCE_PSPOVERLAY 0xFFA8 /* ? */
</source>


See Specifications here:
=== OS ABI identification ===
[http://www.sco.com/developers/gabi/latest/ch4.eheader.html ELF Header]
 
[http://www.openwatcom.com/ftp/devel/docs/elf-64-gen.pdf ELF-64 Object File Format]
<source lang="C">
#define ELFOSABI_CELL_LV2 102 /* CELL LV2 */
</source>
 
== ELF Program Segment Header ==
 
=== Struct ===
 
==== PS3 ====
 
<source lang="C">
typedef struct
{
  Elf64_Word p_type; /* Segment type */
  Elf64_Word p_flags; /* Segment flags */
  Elf64_Off p_offset; /* Segment file offset */
  Elf64_Addr p_vaddr; /* Segment virtual address */
  Elf64_Addr p_paddr; /* Segment physical address */
  Elf64_Xword p_filesz; /* Segment size in file */
  Elf64_Xword p_memsz; /* Segment size in memory */
  Elf64_Xword p_align; /* Segment alignment */
} Elf64_Phdr;
</source>
 
==== PS Vita ====
 
<source lang="C">
typedef struct
{
  Elf32_Word p_type; /* Segment type */
  Elf32_Off p_offset; /* Segment file offset */
  Elf32_Addr p_vaddr; /* Segment virtual address */
  Elf32_Addr p_paddr; /* Segment physical address */
  Elf32_Word p_filesz; /* Segment size in file */
  Elf32_Word p_memsz; /* Segment size in memory */
  Elf32_Word p_flags; /* Segment flags */
  Elf32_Word p_align; /* Segment alignment */
} Elf32_Phdr;
</source>
 
See Spec here: [http://www.sco.com/developers/gabi/latest/ch5.pheader.html ELF Program Segment Header]  
 
=== SCE specific segment types (p_type) ===


Notes:
<source lang="c">
#define PT_SCE_RELA 0x60000000
#define PT_SCE_LICINFO_1 0x60000001
#ddfine PT_SCE_LICINFO_2 0x60000002
#define PT_SCE_DYNLIBDATA 0x61000000
#define PT_SCE_PROCESS_PARAM 0x61000001
#define PT_SCE_MODULE_PARAM 0x61000002
#define PT_SCE_RELRO 0x61000010 // for PS4
#define PT_SCE_COMMENT 0x6FFFFF00
#define PT_SCE_LIBVERSION 0x6FFFFF01
#define PT_SCE_UNK_70000001 0x70000001
#define PT_SCE_IOPMOD 0x70000080
#define PT_SCE_EEMOD 0x70000090
#define PT_SCE_PSPRELA 0x700000A0
#define PT_SCE_PSPRELA2 0x700000A1
#define PT_SCE_PPURELA 0x700000A4
#define PT_SCE_SEGSYM 0x700000A8
</source>
'''PT_SCE_MODULE_PARAM (PS4)'''


*e_type: ET_PS3PRX=0xFFA4
This segment contains a single C struct. It appears instead of PT_SCE_PROCESS_PARAM if the ELF file was compiled to be a library. Like PT_SCE_PROCESS_PARAM, the .data segment contains it and it is always at offset 0.
*EI_OSABI: ELFOSABI_CELL_LV2=0x66
<source lang="c">
struct SceModuleParamBase {
    // current size of the struct this version
    uint64_t size;
    // magic bytes
    uint32_t magic; // 0x3c13f4bf
    // current version of the struct format used
    uint32_t version;
    // for example: 0x0803_0001 for firmware version 8.03
    uint32_t sdk_version;
}
</source>
The struct is the base or head of the actual struct used. The above fields are always present and their offsets do not change between versions.


== ELF Program Headers  ==
Fields whose name are of the form: unk_0x[0-9 aA-fF], are unknown fields whose offset is in the name, e.g. unk_0x18 is at offset 0x18.<source lang="c">
// checked FW 2.xx-7.00
// FW < 2.00 files do not have a PT_SCE_MODULE_PARAM segment, unchecked 7.00 < FW < 7.55
struct SceModuleParamV1 {
    uint64_t size; // 0x18
    uint32_t magic; // 0x3c13f4bf
    uint32_t version; // 1 for V1
    uint32_t sdk_version;
}
// checked 7.55-9.00, unchecked FW > 10.50 and 7.00 < FW < 7.55
struct SceModuleParamV2 {
    uint64_t size; // 0x20
    uint32_t magic; // 0x3c13f4bf
    uint32_t version; // 2 for V2
    uint32_t sdk_version;
    uint64_t unk_0x18;
}
</source>


===Struct===
=== SCE specific segment flags (p_flags) ===
  typedef struct {
    uint32_t p_type;                  /* type of segment */
    uint32_t p_flags;                /* segment attributes */
    uint64_t p_offset;                /* offset in file */
    uint64_t p_vaddr;                /* virtual address in memory */
    uint64_t p_paddr;                /* reserved */
    uint64_t p_filesz;                /* size of segment in file */
    uint64_t p_memsz;                /* size of segment in memory */
    uint64_t p_align;                /* alignment of segment */
  } __attribute__((packed)) ELF_PHDR;


===Table===
<source lang="C">
*PF_SPU_X = 0x00100000
*PF_SPU_W = 0x00200000
*PF_SPU_R = 0x00400000
*PF_RSX_X = 0x01000000
*PF_RSX_W = 0x02000000
*PF_RSX_R = 0x04000000
</source>


===Comments===
== ELF Section Header ==
See Spec here: [http://www.sco.com/developers/gabi/latest/ch5.pheader.html ELF Program Headers]


== ELF Section Headers ==
=== Struct ===


===Struct===
<source lang="C">
   typedef struct {
   typedef struct {
     uint32_t sh_name;                /* section name */
     uint32_t sh_name;                /* section name */
Line 275: Line 421:
     uint64_t sh_entsize;              /* size of entries, if section has table */
     uint64_t sh_entsize;              /* size of entries, if section has table */
   } __attribute__((packed)) ELF_SHDR;
   } __attribute__((packed)) ELF_SHDR;
</source>


===Table===
=== SCE specific section types (sh_type) ===


===Comments===
<source lang="C">
#define SHT_SCE_RELA 0x60000000
#define SHT_SCE_NID 0x61000001
#define SHT_SCE_IOPMOD 0x70000080
#define SHT_SCE_EEMOD 0x70000090
#define SHT_SCE_PSPRELA 0x700000A0
#define SHT_SCE_PPURELA 0x700000A4
</source>


== Segment Information  ==
== Segment Extended Header ==
 
Temporary name was Section Info. Official name is segment_ext_header.
 
Segment Extended Header is a table which maps each phdr/shdr entry to the actual offset/size within the encrypted Certified File. Indeed, because segments can be compressed, they might not match the values listed within the ELF phdr/shdr.
 
There is one of these entries for each ELF phdr (ELF Program Segment Header) entry in the ELF file so that the console knows where to decrypt the data from, because it might also be compressed.


=== Struct ===
=== Struct ===


<source lang="C">
typedef struct {
typedef struct {
   uint64_t offset;
   uint64_t offset;
   uint64_t size;
   uint64_t size;
   uint32_t compressed; // 2=compressed
   uint32_t compression;
   uint32_t unknown1;
   uint32_t unknown;
   uint32_t unknown2;
   uint64_t encryption;
  uint32_t encrypted; // 1=encrypted
} __attribute__((packed)) segment_ext_header;
} __attribute__((packed)) SECTION_INFO;
</source>


=== Table ===
=== Table ===


{| class="wikitable"
{| class="wikitable"
! field  !! offset !! type !! notes
|-
|-
! field !! offset !! type !! notes
| Offset || 0x00 ||u64 || Offset to data
|-
|Encrypted Data Offset || 0x00 ||u64
|-
|-
|Encrypted Data Size   || 0x08 ||u64
| Size || 0x08 || u64 || Size of data
|-   
|-   
|unknown || 0x10 || u32 || This has been 1 in all the examples I have seen.
| compress_algorithm || 0x10 || u32 || 1 = plain, 2 = zlib
|-   
|-   
|unknown || 0x14 || u32 || Always 0, as far as I know.
| Unknown || 0x14 || u32 || Always 0, as far as I know.
|-   
|-   
|unknown || 0x18 || u32 || Always 0, as far as I know.
| request_encryption || 0x18 || u64 || 0 = unrequested, 1 = completed, 2 = requested
|- 
|unknown || 0x1c || u32 || This is 2 for loadable segment types, and 0 for other types.
|-
|}
|}


=== Comments ===
PS3 OS uses the following structure to handle that data:


There is one of these entries for each phdr entry in the elf file so that the ps3 knows where to decrypt the data from. (because it might also be compressed.)
<source lang="C">
 
Notes:
 
*There is one Segment Information for each ELF Program Header.<br>
== SCE Version Info ==
 
===Struct===
typedef struct {
typedef struct {
   uint32_t unknown1;
   void *data;
   uint32_t unknown2;
   uint64_t size;
   uint32_t unknown3;
   uint64_t offset;
  uint32_t unknown4;
} segment_ext_header;
} __attribute__((packed)) SCEVERSION_INFO;
</source>


===Table===
== Version Header ==


===Comment===
It contains some version information, including an offset to the .sceversion section of the encrypted ELF.


== Control Information  ==
=== Struct ===


===Struct===
<source lang="C">
 
typedef struct {
typedef struct {
   uint32_t subheader_type; // 1 - sceversion
   uint32_t type; // 1==control flags; 2==file digest; 3==npdrm
  uint32_t present;       // 0 = false, 1 = true
   uint32_t size;
   uint32_t size;           // usually 0x10
   uint64_t unknown; // 0;1
   uint32_t unknown4;
} __attribute__((packed)) version_header;
</source>


  union {
=== Data Struct ===
    // type 1 0x30 bytes
    struct {
      uint32_t ctrl_flag1; // 0x80000000(all?);0x40000000(root);0x20000000(dbg);0x00000000(normal?)
      uint32_t unknown2;
      uint32_t unknown3;
      uint32_t unknown4;
      uint32_t unknown5;
      uint32_t unknown6;
      uint32_t unknown7;  // 0;8;9;0xC
      uint32_t unknown8;  // 0;1;2;4
    } control_flags;


    // type 2 0x40 bytes
<source lang="C">
    struct {
typedef struct {
      uint8_t digest1[20]; //hash digest, same for every file
  uint16 unknown_1;
      uint8_t digest2[20]; //sha1 hash digest calculated of .elf file...
  uint16 unknown_2; // 0x1
      uint64_t padding;
  uint32 unknown_3;
     } file_digest40;
  uint32 unknown_4; // ?Number of sections?
  uint32 unknown_5;
  ////
     uint64 offset;    // Data offset
    uint64 size;      // Data size
  //// <- these are supposed to be sections
} SCE_VERSION_DATA_30;
</source>


    // type 2 0x30 bytes
=== Comments ===
    struct {
      uint8_t digest1[20];
      uint64_t unknown2;
    } file_digest30;


    // type 3 0x90 bytes
== Supplemental Header Table ==
    struct {
      uint32_t magic;
      uint32_t unknown2;
      uint32_t license; /* 1 network, 2 local, 3 free */
      uint32_t type; /* 1 exec, 21 update */
      uint8_t content_id[48];
      uint8_t digest[16];
      uint8_t invdigest[16];
      uint8_t xordigest[16];
      uint64_t unknown3;
      uint64_t unknown4;
    } npdrm;
  };
} __attribute__((packed)) CONTROL_INFO;


===Table===
Temporary name was Control Information. Official name is supplemental_header_table.


===Comments===
=== Struct ===


<source lang="C">
typedef struct ECDSA224_signature { // size is 0x38
  unsigned char r[0x1C];
  unsigned char s[0x1C];
} ECDSA224_signature;


== Metadata Information  ==
// current hypothesis of SceSharedSecret is full (0x40 bytes) shared_secret overwritten with klicensee at offset 0x10
typedef struct SceSharedSecret { // size is 0x40 on PS Vita SDK versions 0.931.010-3.740.011
  uint8_t shared_secret_0[0x10]; // ex: 0x7E7FD126A7B9614940607EE1BF9DDF5E or full of zeroes
  uint8_t klicensee[0x10]; // usually full of zeroes for NPDRM and fSELF. Usually retrieved from bound RIF for NPDRM.
  uint8_t shared_secret_2[0x10]; // usually full of zeroes for NPDRM and fSELF
  uint32_t shared_secret_3_0; // ex: 0x10, usually full of zeroes for NPDRM and fSELF
  uint32_t shared_secret_3_1; // usually full of zeroes for NPDRM and fSELF
  uint32_t shared_secret_3_2; // usually full of zeroes for NPDRM and fSELF
  uint32_t shared_secret_3_3; // usually full of zeroes for NPDRM and fSELF
} SceSharedSecret;


===Struct===
typedef struct {
typedef struct {
  uint32_t type; // 1=PS3 plaintext_capability; 2=PS3 ELF digest; 3=PS3 NPDRM, 4=PS Vita ELF digest; 5=PS Vita NPDRM; 6=PS Vita boot param; 7=PS Vita shared secret
  uint8_t key[16];
   uint32_t size;
   uint8_t key_pad[16];
   uint64_t next; // 1 if another Supplemental Header element follows else 0
   uint8_t iv[16];
  uint8_t iv_pad[16];
} __attribute__((packed)) METADATA_INFO;


===Table===
  union {
    // type 1, 0x30 bytes
    struct { // 0x20 bytes of data
      plaintext_capability_t plaintext_capability;
    } PS3_plaintext_capability_header;


    // type 2, 0x40 bytes
    struct { // 0x30 bytes of data
      uint8_t constant[0x14]; // same for every PS3/PS Vita SELF, hardcoded in make_fself.exe: 627CB1808AB938E32C8C091708726A579E2586E4
      uint8_t elf_digest[0x14]; // SHA-1. Hash F2C552BF716ED24759CBE8A0A9A6DB9965F3811C is blacklisted by appldr
      uint64_t required_system_version; // filled on Sony authentication server, contains decimal PS3_SYSTEM_VER value from PARAM.SFO
    } PS3_elf_digest_header_40;


===Comments===
    // type 2, 0x30 bytes
Notes:
    struct { // 0x20 bytes of data
      uint8_t constant_or_elf_digest[0x14];
      uint8_t padding[0xC];
    } PS3_elf_digest_header_30;


*The key and ivec fields are encrypted using AES256CBC.
    // type 3, 0x90 bytes
*This is not present if it is an FSELF.
    struct { // 0x80 bytes of data
      PS3_NPD npd; // See [[NPD]]
    } PS3_npdrm_header;


== Metadata Header ==
    // type 4, 0x50 bytes
    struct { // 0x40 bytes of data
      uint8_t constant[0x14]; // same for every PS3/PS Vita SELF, hardcoded in make_fself.exe: 627CB1808AB938E32C8C091708726A579E2586E4
      uint8_t elf_digest[0x20]; // SHA-256 of source ELF file.
      uint8_t padding[8];
      uint32_t min_required_fw; // ex: 0x363 for 3.63
    } PSVita_elf_digest_header;
   
    // type 5, 0x110 bytes
    struct { // 0x100 bytes of data
      uint32_t magic;              // 7F 44 52 4D (".DRM")
      uint32_t finalized_flag;      // ex: 80 00 00 01. It may be version like in NPD.
      uint32_t drm_type;            // [[NPDRM#DRM_Type]]
      uint32_t padding;            // It may be Application Type like in NPD.
      uint8_t content_id[0x30];
      uint8_t digest[0x10];        // ?sha-1 hash of debug SELF/SPRX created using make_fself_npdrm? content_id hash?
      uint8_t padding_78[0x78];
      ECDSA224_signature sig[0x38]; // signature of PSVita_npdrm_header? signature of an external NPDRM file?
    } PSVita_npdrm_header;
   
    // type 6, 0x110 bytes
    struct { // 0x100 bytes of data
      uint8_t boot_param[0x100];
    } PSVita_boot_param_header;
   
    // type 7, 0x50 bytes
    struct { // 0x40 bytes of data
      SceSharedSecret shared_secret;
    } PSVita_shared_secret_header;
  };
  } __attribute__((packed)) supplemental_header;
</source>


===Struct===
=== Table ===


typedef struct {
=== Comments ===
  uint64_t signatureInputLength;
  uint32_t unknown02;
  uint32_t sectionCount;
  uint32_t keyCount;
  uint32_t signatureInfoSize;
  uint32_t unknown06;
  uint32_t unknown07;
} __attribute__((packed)) METADATA_HEADER;
 
===Table===
 
 
===Comments===
Notes:
 
*The metadata header is located after the metadata info in the SELF file.
*It is decrypted using AES128CTR with the key and ivec entries from the metadata information.
*The signature input length is the number of bytes which are used to generate the SHA-1 which is used to generate the ECDSA signature. The length should be eveything from the beginning until the signature itself. The decrypted version of the input data is used.
*This is only present if the metadata Information is present.
 
== Metadata Section Headers  ==
 
===Struct===
 
typedef struct {
  uint64_t data_offset;
  uint64_t data_size;
  uint32_t type; // 1 = shdr, 2 == phdr, 3 == unknown
  uint32_t program_idx;
  uint32_t unknown;
  uint32_t sha1_idx;
  uint32_t encrypted; // 3=yes; 1=no
  uint32_t key_idx;
  uint32_t iv_idx;
  uint32_t compressed; // 2=yes; 1=no
} __attribute__((packed)) METADATA_SECTION_HEADER;
 
===Table===
 
===Comments===
 
Notes:


*The metadata section headers are located after the metadata header in the SELF file.  
* See [[Capability_Flags]].
*The number of sections is indicated by the sectionCount entry in the metadata header.
* PS3 loader uses supplemental_header_table to handle some data:
*They are decrypted using AES128CTR with the key and ivec entries from the metadata information.
<source lang="C">
*Section data is decrypted using AES128CTR&nbsp;with the key and ivec from the metadata keys specified by keyIndex and ivecIndex.
*Section data will also need to be uncompressed using zlib.
*The dataOffsets of the metadata section headers match in general the segment information dataOffsets.
*This is only present if the metadata header is present.
 
== Section Hash ==
 
===Struct===
typedef struct {
typedef struct {
   uint8_t sha1[20];
   plaintext_capability_t plaintext_capability;      /* Plaintext Capability */
   uint8_t padding[12];
  uint8[0x14] elf_digest;         /* sha1 hash of the ELF file */
   uint8_t hmac_key[64];
   uint32_t    unknown_0;          /* seems to be padding */
} __attribute__((packed)) SECTION_HASH;
   uint64_t    required_system_version;     /* PS3_SYSTEM_VER, decimal format */
} supplemental_header_table;
</source>


===Table===
= Extraction =


===Comments===
* Load the Encryption Root Header and decrypt the key and IV entries using AES256CBC with key and IV from OS.
Notes:  
* Load the Certification Header and decrypt it using AES128CTR/CBC with the key and IV entries from the Encryption Root Header.
* Load the Segment Certification Headers and decrypt them using AES128CTR/CBC with the key and ivec entries from the Encryption Root Header.
* Load the Segment Certifications and decrypt them using AES128CTR/CBC with the key and ivec entries from the Encryption Root Header.
* For each Segment Certification:
** In the SELF file, fseek to data_offset and read in data_size bytes.
** Decrypt the data using the algorithm, key and ivec from the Segment Certification specified by keyIndex and ivecIndex in the Segment Certification Header.
** Uncompress the data using the algorithm specified in the Segment Certification Header.
** Write the output data to the ELF file as the program section specified by segment_id in the Segment Certification Header.


*The metadata keys (section hash) are located after the metadata section headers in the SELF file.
= Tools =
*The number of keys is indicated by the keyCount entry in the metadata header.
*They are decrypted using AES128CTR with the key and ivec entries from the metadata information.
*If the sha1Index points to a key, then key[sha1Index] and key[sha1Index+1] form the 160-bit hash. key[sha1Index+2] to key[key[sha1Index+6] form the 512-bit key for the HMAC-SHA1. The HMAC-SHA1 is calculated on the decrypted data and before the decompression.


== Capabilities Info ==
== Official tools ==
 
===Struct===
typedef struct {
  uint32_t unknown1;
  uint32_t capabilities_size;
  uint64_t unknown2;
  uint64_t unknown3;
  uint64_t unknown4;
  uint64_t flags;
  uint32_t unknown6;
  uint32_t unknown7;
} __attribute__((packed)) CAPABILITIES_INFO;
===Table===
===Comments===
 
== Signature ==
===Struct===
typedef struct {
  uint8_t r[21];
  uint8_t s[21];
  uint8_t padding[6];
} __attribute__((packed)) SIGNATURE;
===Table===
===Comments===
 
Notes:
 
*The signature is located after the the signature information in the SELF file.
*It is even present if the signature information is not present.
*It is decrypted using AES128CTR with the key and ivec entries from the metadata information.
 
==Self Section Info==
 
===Struct===
typedef struct {
  uint8_t *data;
  uint64_t size;
  uint64_t offset;
} SELF_SECTION;
===Table===


===Comments===
=== make_fself ===


= Extracting an ELF<br> =
==== make_fself version 1.9.0 (2009-02-15) ====


=== ELF Header  ===
Found in SCE PS3 SDK 1.92.


Elf64_Ehdr elfHeader;
=== make_fself_npdrm ===
fseek ( selfFile, fix64 ( selfHeader.elfHeaderOffset ), SEEK_SET );
fread ( &amp;elfHeader, sizeof ( Elf64_Ehdr ), 1, selfFile );
fseek ( elfFile, 0, SEEK_SET );
fwrite ( &amp;elfHeader, sizeof ( Elf64_Ehdr ), 1, elfFile );


=== Section Headers  ===
==== make_fself_npdrm version 1.9.0 (2009-02-15) ====


Elf64_Shdr elfSectionHeaders[100];
Found in SCE PS3 SDK 1.92.
fseek ( selfFile, fix64 ( selfHeader.elfSectionHeadersOffset ), SEEK_SET );
fread ( elfSectionHeaders, sizeof ( Elf64_Shdr ), fix16 ( elfHeader.e_shnum ), selfFile );
fseek ( elfFile, fix64 ( elfHeader.e_shoff ), SEEK_SET );
fwrite ( elfSectionHeaders, sizeof ( Elf64_Shdr ), fix16 ( elfHeader.e_shnum ), elfFile );


=== Section Data  ===
=== unfself ===


Notes:
==== unfself version 1.9.0 (2009-02-15) ====


*Unknown, manually copying the data over works for now.  
Found in SCE PS3 SDK 1.92.
*There should be a section data offset somewhere.


=== Program Headers  ===
== Unofficial tools ==


Elf64_Phdr elfProgramHeaders[100];
=== some tool by geohot (2009) ===
fseek ( selfFile, fix64 ( selfHeader.elfProgramHeadersOffset ), SEEK_SET );
fread ( elfProgramHeaders, sizeof ( Elf64_Phdr ), fix16 ( elfHeader.e_phnum ), selfFile );
fseek ( elfFile, fix64 ( elfHeader.e_phoff ), SEEK_SET );
fwrite ( elfProgramHeaders, sizeof ( Elf64_Phdr ), fix16 ( elfHeader.e_phnum ), elfFile );


=== Program Data  ===
To be documented.


Notes:
=== scetool by fail0verflow (?2010?) ===


*Load the metadata information and decrypt the key and ivec entries using AES256CBC using erk and riv.
To be documented.
*Load the metadata header and decrypt it using AES128CTR with the key and ivec entries from the metadata information.
*Load sectionCount metadata section headers and decrypt them using AES128CTR with the key and ivec entries from the metadata information.
*Load keyCount metadata keys and decrypt them using AES128CTR with the key and ivec entries from the metadata information.
*For each metadata section:
**In the SELF file, fseek to dataOffset and read in dataSize bytes.
**Decrypt the data using AES128CTR with the key and ivec from the metadata keys specified by keyIndex and ivecIndex from the metadata section header.
**Uncompress the data using zlib.
**Write it to the ELF file as the program section specified by section Index in the metadata section header.


=== Meta Checksums ===
=== some tool by xorloser ===
There are 3 checksums at the offset specified by meta_offset.


*The first is the sha1 checksum of the entire self file.  
To be documented.
*The 2nd checksum is the inverse of the first checksum.
*The 3rd checksum is the first checksum XORed with 0xAAAAAA..AAAAAB


The PSJailbreak payload ignores the actual checksums, but checks that the 3rd checksum is the 2nd checksum XORed with 0xAAAAAA..AAAAAB


''' Source: http://ps3wiki.lan.st/index.php?title=SELF_File_Format_and_Decryption '''
{{File Formats}}<noinclude>
[[Category:Main]]
</noinclude>

Latest revision as of 06:32, 9 February 2024

SELF stands for Signed Executable and Linkable Format. SELF is the file format used by the executables on the PS3 and PS Vita. SPRX stands for Signed Playstation Relocatable eXecutable. SPRX is a file extension, derived from PRX, of SELF files that are loaded as "libraries" rather than "applications". SELF files of applications are often named "EBOOT.BIN" as in "encrypted BOOT.BIN" as a legacy of PS1, PS2 and PSP executables filenames.

A screenshot of f0f's presentation at CCC2010.
Self hdr.png

Introduction[edit | edit source]

SELF consists of a Certified File header with an Extended Header followed by the encapsulated ELF file. ELF sections can be compressed using gzip. A SELF is usually encrypted and signed, but fSELF are unsigned SELFs. ELF sections can be encrypted using AES128 and signed using ECDSA160 or RSA2048 + HMAC-SHA1 or HMAC-SHA256. A SELF has a specific header called Extended header where it stores all the parameters for this program.

  • Extended Header

Extended Header consists of information regarding the structure and offsets of the SELF. The first part is in plaintext until the Encryption Root Header.

  • Encryption Root Header

Encryption Root Header is encrypted under an AES256CBC layer. It contains key and IV to further decrypt the Certification data using AES128 (CBC or CTR).

  • Certification data

Certification Header, Certification Body and CF Signature are encrypted under an AES128 (CBC or CTR) layer with the Encryption Root Header key and IV.

  • Certification Header

Signature is an ECDSA160/RSA2048 signature of the SHA1/SHA256 digest of the SELF file starting at offset 0 and ending at offset sign_offset.

  • Data Segments

Data segments can be encrypted and/or compressed. SHA1/HMAC-SHA1/HMAC-SHA256 is used to ensure that they have not been modified.

Cryptography[edit | edit source]

Here is a small summary on how the SELF cryptography works.

Basically here are the steps being involved by the loaders:

Loaders all have a static key and iv called respectively erk and riv. Those are keys for the first decryption step which is to decrypt the Encryption Root Header using AES256CBC. When the SELF is protected by NPDRM ?instead of using static keys?, erk and riv are derived by decrypting klicensee using "NP_klic_key".

Then the result is used as key and iv to decrypt the rest of the Certification data using AES128CTR (PS3) or AES128CBC (PS Vita). Finally the decrypted Certification data contains the key and iv for each data segments which are still decrypted following the Segment Certification Header information. This security model is based on the fact that the Certification Root Header once decrypted by the static AES256CBC key in the loader should never be the same from one binary to the other. The same goes for any other value used as an AES128 key or iv.

Loaders are also involved with inflating the binaries using zlib.

The SELF authenticity is based on other independent steps, HMAC-SHA1 or HMAC-SHA256 of the data segments and ECDSA160 or RSA2048 for the signature of the header and the Certification data.

Short references[edit | edit source]

More indepth Online course about encryption in generic (also AES/ECDSA): Lecture Notes on Computer and Network Security by Avinash Kak

fSELF[edit | edit source]

fSELF stands for fake signed ELF. It is the format output by Software Development Kits and ran on activated TestKits and DevKits. As it name suggests, a fSELF is not signed and usually not encrypted. If a fSELF was signed, that would imply that SDK embeds the private keys required to sign it.

A fSELF has Attribute set to 0x8000. ?always?

A fSELF has usually less rights than a System SELF but more than a finalized Non-System SELF. The OS recognizes a fSELF by looking at its program-authority-id.

To document more...

Location[edit | edit source]

PS3[edit | edit source]

Files with extensions: eboot.bin, *.self, *.sprx.

See also SELFs inside ELFs.

PS Vita[edit | edit source]

Files with extensions: eboot.bin, *.self, *.suprx, *.skprx, *.skarx.

File Format[edit | edit source]

Notes:

  • Warning: PS3 uses big endian, PS Vita uses little endian.
  • Encapsulated ELF header fields are useless (only the EI_CLASS EI_DATA and EI_VERSION fields are checked).

PS3 early Program Type 3 SELFs[edit | edit source]

SDK 0.60: No sce version nor elf digest
SDK 0.8X: No sce version, with 0x30 elf digest
SDK 0.9X: With sce version and 0x40 elf digest (scetool produces this type)

Special SELF samples[edit | edit source]

Warhawk public beta release 012[edit | edit source]

Oldest official game SELF (although not even NPDRM protected!!!) found by now: 2007-06-08.

Extended Header[edit | edit source]

Extended Header offsets are relative to Certified File start.

Struct[edit | edit source]

 typedef struct { // Size is 0x50 bytes
  uint64_t ext_hdr_version;
  uint64_t program_identification_hdr_offset;
  uint64_t ehdr_offset;
  uint64_t phdr_offset;
  uint64_t shdr_offset;
  uint64_t segment_ext_hdr_offset;
  uint64_t version_hdr_offset;
  uint64_t supplemental_hdr_offset;
  uint64_t supplemental_hdr_size;
  uint64_t padding;
 } __attribute__((packed)) ext_hdr;

Table[edit | edit source]

field offset type notes
Extended Header version 0x0 u64 3 for PS3, 4 for PS Vita
Program Identification Header offset 0x8 u64 Offset to Program Identification Header.
ELF Header offset 0x10 u64 Offset to ELF Header.
Program Header offset 0x18 u64 Offset to ELF Program Header.
Section Header offset 0x20 u64 Offset to ELF Section Header.
Segment Extended Header offset 0x28 u64 Offset to Segment Extended Header.
Version Header offset 0x30 u64 Offset to Version Header.
Supplemental Header offset 0x38 u64 Offset to Supplemental Header.
Supplemental Header size 0x40 u64 Size of Supplemental Header.
Padding 0x48 u64

Comments[edit | edit source]

The real ELF data is located after the Extended Header (see Extended Header size in Certified File Header). It is encrypted, unless Certified File attribute is 0x8000 (fake CF). unfself works by cutting the SCE header from the fSELF and if needed decompressing segments.

Program Identification Header[edit | edit source]

Temporary name was App Info. Official name is Program Identification Header.

Struct[edit | edit source]

 typedef struct {
   uint64_t program_authority_id;
   uint32_t program_vender_id;
   uint32_t program_type;
   uint64_t program_sceversion;
   uint64_t padding;
 } __attribute__((packed)) program_identification_header;

Table[edit | edit source]

field offset type notes example
program_authority_id 0x00 u64 See Program Authority Id ex: 21 00 00 10 1C CA 01 30
program_vender_id 0x08 u32 See Program Vender Id ex: 00 00 00 00
program_type 0x0C u32 See Program Type ex: 08 00 00 00
program_sceversion 0x10 u64 ?SDK version or app version? version ex: 01.02 is translated by make_fself.exe to 02 01 00 00 ex: 0x0000036000000000 (3.600.011), 0x0000009450000000 (0.945.040)
padding 0x18 u64 Padding 00 00 00 00 00 00 00 00

Comments[edit | edit source]

Aligned to 0x10 bytes.

ELF Header[edit | edit source]

Struct[edit | edit source]

PS3[edit | edit source]

typedef struct {
    uint8_t e_ident[16];              /* ELF identification */
    uint16_t e_type;                  /* object file type */
    uint16_t e_machine;               /* machine type */
    uint32_t e_version;               /* object file version */
    uint64_t e_entry;                 /* entry point address */
    uint64_t e_phoff;                 /* program header offset */
    uint64_t e_shoff;                 /* section header offset */
    uint32_t e_flags;                 /* processor-specific flags */
    uint16_t e_ehsize;                /* ELF header size */
    uint16_t e_phentsize;             /* size of program header entry */
    uint16_t e_phnum;                 /* number of program header entries */
    uint16_t e_shentsize;             /* size of section header entry */
    uint16_t e_shnum;                 /* number of section header entries */
    uint16_t e_shstrndx;              /* section name string table index */
} __attribute__((packed)) ELF;

See also specifications: ELF Header ELF-64 Object File Format

PS Vita[edit | edit source]

typedef struct {
    uint8_t e_ident[16];              /* ELF identification */
    uint16_t e_type;                  /* object file type */
    uint16_t e_machine;               /* machine type */
    uint32_t e_version;               /* object file version */
    uint32_t e_entry;                 /* entry point address */
    uint32_t e_phoff;                 /* program header offset */
    uint32_t e_shoff;                 /* section header offset */
    uint32_t e_flags;                 /* processor-specific flags */
    uint16_t e_ehsize;                /* ELF header size */
    uint16_t e_phentsize;             /* size of program header entry */
    uint16_t e_phnum;                 /* number of program header entries */
    uint16_t e_shentsize;             /* size of section header entry */
    uint16_t e_shnum;                 /* number of section header entries */
    uint16_t e_shstrndx;              /* section name string table index */
} __attribute__((packed)) ELF;
Name of the variable Offset Size Notes
e_ident[0..3] 0 4 Magic.
e_ident[4] 4 1 Class Type. Must be [ELFCLASS32 = 0x01].
e_ident[5] 5 1 Data Type. Must be [ELFDATA2LSB = 0x01].
e_ident[6] 6 1 File version. Must be 1.
e_ident[7...15] 7 9 unused
e_type 0x10 2 See SCE-specific e_type.
e_machine 0x12 2 Must be [EM_ARM = 0x0028].
e_version 0x14 4 Must be 0x1.
e_entry 0x18 4 Address to jump to in order to start program (warning: not always set).
e_phoff 0x1C 4 Boundary checked, but unused (already given by SELF header). ?and for fSELF?
e_shoff 0x20 4 unused
e_flags 0x24 4 unused
e_ehsize 0x28 2 Must be sizeof(Elf32_Ehdr) = 0x0034.
e_phentsize 0x2A 2 Must be sizeof(Elf32_Phdr) = 0x0020.
e_phnum 0x2C 2 Count of Program Header in this ELF.
e_shentsize 0x2E 2 unused
e_shnum 0x30 2 unused
e_shstrndx 0x32 2 unused

See also yifan lu's outdated specifications for PS Vita fSELF

SCE specific ELF types (e_type)[edit | edit source]

/* SCE-specific definitions for e_type: */
#define ET_SCE_EXEC		0xFE00		/* SCE Executable - PRX2 */
#define ET_SCE_RELEXEC		0xFE04		/* SCE Relocatable Executable - PRX2 */
#define ET_SCE_STUBLIB		0xFE0C		/* SCE SDK Stubs */
#define ET_SCE_DYNEXEC		0xFE10		/* SCE EXEC_ASLR (PS4 Executable with ASLR) */
#define ET_SCE_DYNAMIC		0xFE18		/* ? */
#define ET_SCE_IOPRELEXEC	0xFF80		/* SCE IOP Relocatable Executable */
#define ET_SCE_IOPRELEXEC2	0xFF81		/* SCE IOP Relocatable Executable Version 2 */
#define ET_SCE_EERELEXEC	0xFF90		/* SCE EE Relocatable Executable */
#define ET_SCE_EERELEXEC2	0xFF91		/* SCE EE Relocatable Executable Version 2 */
#define ET_SCE_PSPRELEXEC	0xFFA0		/* SCE PSP Relocatable Executable */
#define ET_SCE_PPURELEXEC	0xFFA4		/* SCE PPU Relocatable Executable */
#define ET_SCE_ARMRELEXEC	0xFFA5		/* ?SCE ARM Relocatable Executable (PS Vita System Software earlier or equal 0.931.010) */
#define ET_SCE_PSPOVERLAY	0xFFA8		/* ? */

OS ABI identification[edit | edit source]

#define ELFOSABI_CELL_LV2		102	/* CELL LV2 */

ELF Program Segment Header[edit | edit source]

Struct[edit | edit source]

PS3[edit | edit source]

typedef struct
{
  Elf64_Word	p_type;			/* Segment type */
  Elf64_Word	p_flags;		/* Segment flags */
  Elf64_Off	p_offset;		/* Segment file offset */
  Elf64_Addr	p_vaddr;		/* Segment virtual address */
  Elf64_Addr	p_paddr;		/* Segment physical address */
  Elf64_Xword	p_filesz;		/* Segment size in file */
  Elf64_Xword	p_memsz;		/* Segment size in memory */
  Elf64_Xword	p_align;		/* Segment alignment */
} Elf64_Phdr;

PS Vita[edit | edit source]

typedef struct
{
  Elf32_Word	p_type;			/* Segment type */
  Elf32_Off	p_offset;		/* Segment file offset */
  Elf32_Addr	p_vaddr;		/* Segment virtual address */
  Elf32_Addr	p_paddr;		/* Segment physical address */
  Elf32_Word	p_filesz;		/* Segment size in file */
  Elf32_Word	p_memsz;		/* Segment size in memory */
  Elf32_Word	p_flags;		/* Segment flags */
  Elf32_Word	p_align;		/* Segment alignment */
} Elf32_Phdr;

See Spec here: ELF Program Segment Header

SCE specific segment types (p_type)[edit | edit source]

#define PT_SCE_RELA 0x60000000
#define PT_SCE_LICINFO_1 0x60000001
#ddfine PT_SCE_LICINFO_2 0x60000002
#define PT_SCE_DYNLIBDATA 0x61000000
#define PT_SCE_PROCESS_PARAM 0x61000001
#define PT_SCE_MODULE_PARAM 0x61000002
#define PT_SCE_RELRO 0x61000010 // for PS4
#define PT_SCE_COMMENT 0x6FFFFF00
#define PT_SCE_LIBVERSION 0x6FFFFF01
#define PT_SCE_UNK_70000001 0x70000001
#define PT_SCE_IOPMOD 0x70000080
#define PT_SCE_EEMOD 0x70000090
#define PT_SCE_PSPRELA 0x700000A0
#define PT_SCE_PSPRELA2 0x700000A1
#define PT_SCE_PPURELA 0x700000A4
#define PT_SCE_SEGSYM 0x700000A8

PT_SCE_MODULE_PARAM (PS4)

This segment contains a single C struct. It appears instead of PT_SCE_PROCESS_PARAM if the ELF file was compiled to be a library. Like PT_SCE_PROCESS_PARAM, the .data segment contains it and it is always at offset 0.

struct SceModuleParamBase {
    // current size of the struct this version
    uint64_t size;
    // magic bytes
    uint32_t magic; // 0x3c13f4bf
    // current version of the struct format used
    uint32_t version;
    // for example: 0x0803_0001 for firmware version 8.03
    uint32_t sdk_version;
}

The struct is the base or head of the actual struct used. The above fields are always present and their offsets do not change between versions.

Fields whose name are of the form: unk_0x[0-9 aA-fF], are unknown fields whose offset is in the name, e.g. unk_0x18 is at offset 0x18.

// checked FW 2.xx-7.00
// FW < 2.00 files do not have a PT_SCE_MODULE_PARAM segment, unchecked 7.00 < FW < 7.55 
struct SceModuleParamV1 {
    uint64_t size; // 0x18
    uint32_t magic; // 0x3c13f4bf
    uint32_t version; // 1 for V1
    uint32_t sdk_version;
}
// checked 7.55-9.00, unchecked FW > 10.50 and 7.00 < FW < 7.55
struct SceModuleParamV2 {
    uint64_t size; // 0x20
    uint32_t magic; // 0x3c13f4bf
    uint32_t version; // 2 for V2
    uint32_t sdk_version;
    uint64_t unk_0x18;
}

SCE specific segment flags (p_flags)[edit | edit source]

*PF_SPU_X = 0x00100000
*PF_SPU_W = 0x00200000
*PF_SPU_R = 0x00400000
*PF_RSX_X = 0x01000000
*PF_RSX_W = 0x02000000
*PF_RSX_R = 0x04000000

ELF Section Header[edit | edit source]

Struct[edit | edit source]

  typedef struct {
    uint32_t sh_name;                 /* section name */
    uint32_t sh_type;                 /* section type */
    uint64_t sh_flags;                /* section attributes */
    uint64_t sh_addr;                 /* virtual address in memory */
    uint64_t sh_offset;               /* offset in file */
    uint64_t sh_size;                 /* size of section */
    uint32_t sh_link;                 /* link to other section */
    uint32_t sh_info;                 /* miscellaneous information */
    uint64_t sh_addralign;            /* address alignment boundary */
    uint64_t sh_entsize;              /* size of entries, if section has table */
  } __attribute__((packed)) ELF_SHDR;

SCE specific section types (sh_type)[edit | edit source]

#define SHT_SCE_RELA 0x60000000
#define SHT_SCE_NID 0x61000001
#define SHT_SCE_IOPMOD 0x70000080
#define SHT_SCE_EEMOD 0x70000090
#define SHT_SCE_PSPRELA 0x700000A0
#define SHT_SCE_PPURELA 0x700000A4

Segment Extended Header[edit | edit source]

Temporary name was Section Info. Official name is segment_ext_header.

Segment Extended Header is a table which maps each phdr/shdr entry to the actual offset/size within the encrypted Certified File. Indeed, because segments can be compressed, they might not match the values listed within the ELF phdr/shdr.

There is one of these entries for each ELF phdr (ELF Program Segment Header) entry in the ELF file so that the console knows where to decrypt the data from, because it might also be compressed.

Struct[edit | edit source]

typedef struct {
  uint64_t offset;
  uint64_t size;
  uint32_t compression;
  uint32_t unknown;
  uint64_t encryption;
} __attribute__((packed)) segment_ext_header;

Table[edit | edit source]

field offset type notes
Offset 0x00 u64 Offset to data
Size 0x08 u64 Size of data
compress_algorithm 0x10 u32 1 = plain, 2 = zlib
Unknown 0x14 u32 Always 0, as far as I know.
request_encryption 0x18 u64 0 = unrequested, 1 = completed, 2 = requested

PS3 OS uses the following structure to handle that data:

typedef struct {
  void *data;
  uint64_t size;
  uint64_t offset;
} segment_ext_header;

Version Header[edit | edit source]

It contains some version information, including an offset to the .sceversion section of the encrypted ELF.

Struct[edit | edit source]

 typedef struct {
  uint32_t subheader_type; // 1 - sceversion
  uint32_t present;        // 0 = false, 1 = true
  uint32_t size;           // usually 0x10
  uint32_t unknown4;
 } __attribute__((packed)) version_header;

Data Struct[edit | edit source]

 typedef struct {
  uint16 unknown_1;
  uint16 unknown_2; // 0x1
  uint32 unknown_3;
  uint32 unknown_4; // ?Number of sections?
  uint32 unknown_5;
  ////
    uint64 offset;    // Data offset
    uint64 size;      // Data size
  //// <- these are supposed to be sections
 } SCE_VERSION_DATA_30;

Comments[edit | edit source]

Supplemental Header Table[edit | edit source]

Temporary name was Control Information. Official name is supplemental_header_table.

Struct[edit | edit source]

typedef struct ECDSA224_signature { // size is 0x38
   unsigned char r[0x1C];
   unsigned char s[0x1C];
} ECDSA224_signature;

// current hypothesis of SceSharedSecret is full (0x40 bytes) shared_secret overwritten with klicensee at offset 0x10
typedef struct SceSharedSecret { // size is 0x40 on PS Vita SDK versions 0.931.010-3.740.011
   uint8_t shared_secret_0[0x10]; // ex: 0x7E7FD126A7B9614940607EE1BF9DDF5E or full of zeroes
   uint8_t klicensee[0x10]; // usually full of zeroes for NPDRM and fSELF. Usually retrieved from bound RIF for NPDRM.
   uint8_t shared_secret_2[0x10]; // usually full of zeroes for NPDRM and fSELF
   uint32_t shared_secret_3_0; // ex: 0x10, usually full of zeroes for NPDRM and fSELF
   uint32_t shared_secret_3_1; // usually full of zeroes for NPDRM and fSELF
   uint32_t shared_secret_3_2; // usually full of zeroes for NPDRM and fSELF
   uint32_t shared_secret_3_3; // usually full of zeroes for NPDRM and fSELF
} SceSharedSecret;

 typedef struct {
  uint32_t type; // 1=PS3 plaintext_capability; 2=PS3 ELF digest; 3=PS3 NPDRM, 4=PS Vita ELF digest; 5=PS Vita NPDRM; 6=PS Vita boot param; 7=PS Vita shared secret
  uint32_t size;
  uint64_t next; // 1 if another Supplemental Header element follows else 0

  union {
    // type 1, 0x30 bytes
    struct { // 0x20 bytes of data
      plaintext_capability_t plaintext_capability;
    } PS3_plaintext_capability_header;

    // type 2, 0x40 bytes
    struct { // 0x30 bytes of data
      uint8_t constant[0x14]; // same for every PS3/PS Vita SELF, hardcoded in make_fself.exe: 627CB1808AB938E32C8C091708726A579E2586E4
      uint8_t elf_digest[0x14]; // SHA-1. Hash F2C552BF716ED24759CBE8A0A9A6DB9965F3811C is blacklisted by appldr
      uint64_t required_system_version; // filled on Sony authentication server, contains decimal PS3_SYSTEM_VER value from PARAM.SFO
    } PS3_elf_digest_header_40;

    // type 2, 0x30 bytes
    struct { // 0x20 bytes of data
      uint8_t constant_or_elf_digest[0x14];
      uint8_t padding[0xC];
    } PS3_elf_digest_header_30;

    // type 3, 0x90 bytes
    struct { // 0x80 bytes of data
      PS3_NPD npd; // See [[NPD]]
    } PS3_npdrm_header;

     // type 4, 0x50 bytes
     struct { // 0x40 bytes of data
       uint8_t constant[0x14]; // same for every PS3/PS Vita SELF, hardcoded in make_fself.exe: 627CB1808AB938E32C8C091708726A579E2586E4
       uint8_t elf_digest[0x20]; // SHA-256 of source ELF file.
       uint8_t padding[8];
       uint32_t min_required_fw; // ex: 0x363 for 3.63
     } PSVita_elf_digest_header;
     
     // type 5, 0x110 bytes
     struct { // 0x100 bytes of data
       uint32_t magic;               // 7F 44 52 4D (".DRM")
       uint32_t finalized_flag;      // ex: 80 00 00 01. It may be version like in NPD.
       uint32_t drm_type;            // [[NPDRM#DRM_Type]]
       uint32_t padding;             // It may be Application Type like in NPD.
       uint8_t content_id[0x30];
       uint8_t digest[0x10];         // ?sha-1 hash of debug SELF/SPRX created using make_fself_npdrm? content_id hash?
       uint8_t padding_78[0x78];
       ECDSA224_signature sig[0x38]; // signature of PSVita_npdrm_header? signature of an external NPDRM file?
     } PSVita_npdrm_header;
     
     // type 6, 0x110 bytes
     struct { // 0x100 bytes of data
       uint8_t boot_param[0x100];
     } PSVita_boot_param_header;
     
     // type 7, 0x50 bytes
     struct { // 0x40 bytes of data
       SceSharedSecret shared_secret;
     } PSVita_shared_secret_header;
  };
 } __attribute__((packed)) supplemental_header;

Table[edit | edit source]

Comments[edit | edit source]

  • See Capability_Flags.
  • PS3 loader uses supplemental_header_table to handle some data:
typedef struct {
  plaintext_capability_t plaintext_capability;      /* Plaintext Capability */
  uint8[0x14] elf_digest;         /* sha1 hash of the ELF file */
  uint32_t    unknown_0;          /* seems to be padding */
  uint64_t    required_system_version;     /* PS3_SYSTEM_VER, decimal format */
} supplemental_header_table;

Extraction[edit | edit source]

  • Load the Encryption Root Header and decrypt the key and IV entries using AES256CBC with key and IV from OS.
  • Load the Certification Header and decrypt it using AES128CTR/CBC with the key and IV entries from the Encryption Root Header.
  • Load the Segment Certification Headers and decrypt them using AES128CTR/CBC with the key and ivec entries from the Encryption Root Header.
  • Load the Segment Certifications and decrypt them using AES128CTR/CBC with the key and ivec entries from the Encryption Root Header.
  • For each Segment Certification:
    • In the SELF file, fseek to data_offset and read in data_size bytes.
    • Decrypt the data using the algorithm, key and ivec from the Segment Certification specified by keyIndex and ivecIndex in the Segment Certification Header.
    • Uncompress the data using the algorithm specified in the Segment Certification Header.
    • Write the output data to the ELF file as the program section specified by segment_id in the Segment Certification Header.

Tools[edit | edit source]

Official tools[edit | edit source]

make_fself[edit | edit source]

make_fself version 1.9.0 (2009-02-15)[edit | edit source]

Found in SCE PS3 SDK 1.92.

make_fself_npdrm[edit | edit source]

make_fself_npdrm version 1.9.0 (2009-02-15)[edit | edit source]

Found in SCE PS3 SDK 1.92.

unfself[edit | edit source]

unfself version 1.9.0 (2009-02-15)[edit | edit source]

Found in SCE PS3 SDK 1.92.

Unofficial tools[edit | edit source]

some tool by geohot (2009)[edit | edit source]

To be documented.

scetool by fail0verflow (?2010?)[edit | edit source]

To be documented.

some tool by xorloser[edit | edit source]

To be documented.