1 /*-------------------------------------------------------------
6 Michael Wiedenbauer (shagkur)
7 Dave Murphy (WinterMute)
10 This software is provided 'as-is', without any express or implied
11 warranty. In no event will the authors be held liable for any
12 damages arising from the use of this software.
14 Permission is granted to anyone to use this software for any
15 purpose, including commercial applications, and to alter it and
16 redistribute it freely, subject to the following restrictions:
18 1. The origin of this software must not be misrepresented; you
19 must not claim that you wrote the original software. If you use
20 this software in a product, an acknowledgment in the product
21 documentation would be appreciated but is not required.
23 2. Altered source versions must be plainly marked as such, and
24 must not be misrepresented as being the original software.
26 3. This notice may not be removed or altered from any source
29 -------------------------------------------------------------*/
39 #define ES_EINVAL -0x1004
40 #define ES_ENOMEM -0x100C
41 #define ES_ENOTINIT -0x1100
42 #define ES_EALIGN -0x1101
44 #define ES_SIG_RSA4096 0x10000
45 #define ES_SIG_RSA2048 0x10001
46 #define ES_SIG_ECC 0x10002
48 #define ES_CERT_RSA4096 0
49 #define ES_CERT_RSA2048 1
52 #define ES_KEY_COMMON 4
53 #define ES_KEY_SDCARD 6
57 #endif /* __cplusplus */
60 typedef sigtype sig_header
;
61 typedef sig_header signed_blob
;
64 typedef u8 aeskey
[16];
66 typedef struct _sig_rsa2048
{
70 } __attribute__((packed
)) sig_rsa2048
;
72 typedef struct _sig_rsa4096
{
76 } __attribute__((packed
)) sig_rsa4096
;
78 typedef char sig_issuer
[0x40];
80 typedef struct _tiklimit
{
83 } __attribute__((packed
)) tiklimit
;
85 typedef struct _tikview
{
95 } __attribute__((packed
)) tikview
;
99 u8 fill
[63]; //TODO: not really fill
100 aeskey cipher_title_key
;
110 } __attribute__((packed
)) tik
;
112 typedef struct _tmd_content
{
118 } __attribute__((packed
)) tmd_content
;
120 typedef struct _tmd
{
121 sig_issuer issuer
; //0x140
123 u8 ca_crl_version
; //0x181
124 u8 signer_crl_version
; //0x182
126 u64 sys_version
; //0x184
127 u64 title_id
; //0x18c
128 u32 title_type
; //0x194
129 u16 group_id
; //0x198
132 u8 ratings
[16]; //0x19e
133 u8 reserved
[12]; //0x1ae
141 // content records follow
142 // C99 flexible array
143 tmd_content contents
[];
144 } __attribute__((packed
)) tmd
;
146 typedef struct _cert_header
{
151 } __attribute__((packed
)) cert_header
;
153 typedef struct _cert_rsa2048
{
161 } __attribute__((packed
)) cert_rsa2048
;
163 typedef struct _cert_rsa4096
{
171 } __attribute__((packed
)) cert_rsa4096
;
173 #define TMD_SIZE(x) (((x)->num_contents)*sizeof(tmd_content) + sizeof(tmd))
174 // backwards compatibility
175 #define TMD_CONTENTS(x) ((x)->contents)
177 //TODO: add ECC stuff
179 #define IS_VALID_SIGNATURE(x) (((*(x))==ES_SIG_RSA2048) || ((*(x))==ES_SIG_RSA4096))
181 #define SIGNATURE_SIZE(x) (\
182 ((*(x))==ES_SIG_RSA2048) ? sizeof(sig_rsa2048) : ( \
183 ((*(x))==ES_SIG_RSA4096) ? sizeof(sig_rsa4096) : 0 ))
185 #define SIGNATURE_SIG(x) (((u8*)x)+4)
187 #define IS_VALID_CERT(x) ((((x)->cert_type)==ES_CERT_RSA2048) || (((x)->cert_type)==ES_CERT_RSA4096))
189 #define CERTIFICATE_SIZE(x) (\
190 (((x)->cert_type)==ES_CERT_RSA2048) ? sizeof(cert_rsa2048) : ( \
191 (((x)->cert_type)==ES_CERT_RSA4096) ? sizeof(cert_rsa4096) : 0 ))
193 #define SIGNATURE_PAYLOAD(x) ((void *)(((u8*)(x)) + SIGNATURE_SIZE(x)))
195 #define SIGNED_TMD_SIZE(x) ( TMD_SIZE((tmd*)SIGNATURE_PAYLOAD(x)) + SIGNATURE_SIZE(x))
196 #define SIGNED_TIK_SIZE(x) ( sizeof(tik) + SIGNATURE_SIZE(x) )
197 #define SIGNED_CERT_SIZE(x) ( CERTIFICATE_SIZE((cert_header*)SIGNATURE_PAYLOAD(x)) + SIGNATURE_SIZE(x))
199 #define STD_SIGNED_TIK_SIZE ( sizeof(tik) + sizeof(sig_rsa2048) )
201 #define MAX_NUM_TMD_CONTENTS 512
203 #define MAX_TMD_SIZE ( sizeof(tmd) + MAX_NUM_TMD_CONTENTS*sizeof(tmd_content) )
204 #define MAX_SIGNED_TMD_SIZE ( MAX_TMD_SIZE + sizeof(sig_rsa2048) )
207 s32
__ES_Close(void);
208 s32
__ES_Reset(void);
209 s32
ES_GetTitleID(u64
*titleID
);
210 s32
ES_SetUID(u64 uid
);
211 s32
ES_GetDataDir(u64 titleID
, char *filepath
);
212 s32
ES_GetNumTicketViews(u64 titleID
, u32
*cnt
);
213 s32
ES_GetTicketViews(u64 titleID
, tikview
*views
, u32 cnt
);
214 s32
ES_GetNumOwnedTitles(u32
*cnt
);
215 s32
ES_GetOwnedTitles(u64
*titles
, u32 cnt
);
216 s32
ES_GetNumTitles(u32
*cnt
);
217 s32
ES_GetTitles(u64
*titles
, u32 cnt
);
218 s32
ES_GetNumStoredTMDContents(const signed_blob
*stmd
, u32 tmd_size
, u32
*cnt
);
219 s32
ES_GetStoredTMDContents(const signed_blob
*stmd
, u32 tmd_size
, u32
*contents
, u32 cnt
);
220 s32
ES_GetStoredTMDSize(u64 titleID
, u32
*size
);
221 s32
ES_GetStoredTMD(u64 titleID
, signed_blob
*stmd
, u32 size
);
222 s32
ES_GetTitleContentsCount(u64 titleID
, u32
*num
);
223 s32
ES_GetTitleContents(u64 titleID
, u8
*data
, u32 size
);
224 s32
ES_GetTMDViewSize(u64 titleID
, u32
*size
);
225 s32
ES_GetTMDView(u64 titleID
, u8
*data
, u32 size
);
226 s32
ES_GetNumSharedContents(u32
*cnt
);
227 s32
ES_GetSharedContents(sha1
*contents
, u32 cnt
);
228 s32
ES_LaunchTitle(u64 titleID
, const tikview
*view
);
229 s32
ES_LaunchTitleBackground(u64 titleID
, const tikview
*view
);
230 s32
ES_Identify(const signed_blob
*certificates
, u32 certificates_size
, const signed_blob
*tmd
, u32 tmd_size
, const signed_blob
*ticket
, u32 ticket_size
, u32
*keyid
);
231 s32
ES_AddTicket(const signed_blob
*tik
, u32 tik_size
, const signed_blob
*certificates
, u32 certificates_size
, const signed_blob
*crl
, u32 crl_size
);
232 s32
ES_DeleteTicket(const tikview
*view
);
233 s32
ES_AddTitleTMD(const signed_blob
*tmd
, u32 tmd_size
);
234 s32
ES_AddTitleStart(const signed_blob
*tmd
, u32 tmd_size
, const signed_blob
*certificatess
, u32 certificatess_size
, const signed_blob
*crl
, u32 crl_size
);
235 s32
ES_AddContentStart(u64 titleID
, u32 cid
);
236 s32
ES_AddContentData(s32 cid
, u8
*data
, u32 data_size
);
237 s32
ES_AddContentFinish(u32 cid
);
238 s32
ES_AddTitleFinish(void);
239 s32
ES_AddTitleCancel(void);
240 s32
ES_ImportBoot(const signed_blob
*tik
, u32 tik_size
,const signed_blob
*tik_certs
, u32 tik_certs_size
,const signed_blob
*tmd
, u32 tmd_size
,const signed_blob
*tmd_certs
, u32 tmd_certs_size
,const u8
*content
, u32 content_size
);
241 s32
ES_OpenContent(u16 index
);
242 s32
ES_OpenTitleContent(u64 titleID
, u16 index
);
243 s32
ES_ReadContent(s32 cfd
, u8
*data
, u32 data_size
);
244 s32
ES_SeekContent(s32 cfd
, s32 where
, s32 whence
);
245 s32
ES_CloseContent(s32 cfd
);
246 s32
ES_DeleteTitle(u64 titleID
);
247 s32
ES_DeleteTitleContent(u64 titleID
);
248 s32
ES_Encrypt(u32 keynum
, u8
*iv
, u8
*source
, u32 size
, u8
*dest
);
249 s32
ES_Decrypt(u32 keynum
, u8
*iv
, u8
*source
, u32 size
, u8
*dest
);
250 s32
ES_Sign(u8
*source
, u32 size
, u8
*sig
, u8
*certs
);
251 s32
ES_GetDeviceCert(u8
*outbuf
);
252 s32
ES_GetDeviceID(u32
*device_id
);
253 s32
ES_GetBoot2Version(u32
*version
);
254 signed_blob
*ES_NextCert(const signed_blob
*certs
);
258 #endif /* __cplusplus */
260 #endif /* defined(HW_RVL) */