- fixed STOP_TRANS response token read.
[libogc.git] / gc / ogc / es.h
blobdc0fd9d372b914558ef25d32771fd9ae6f997cbf
1 /*-------------------------------------------------------------
3 es.h -- tik services
5 Copyright (C) 2008
6 Michael Wiedenbauer (shagkur)
7 Dave Murphy (WinterMute)
8 Hector Martin (marcan)
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
27 distribution.
29 -------------------------------------------------------------*/
31 #ifndef __ES_H__
32 #define __ES_H__
34 #if defined(HW_RVL)
36 #include <gctypes.h>
37 #include <gcutil.h>
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
50 #define ES_CERT_ECC 2
52 #define ES_KEY_COMMON 4
53 #define ES_KEY_SDCARD 6
55 #ifdef __cplusplus
56 extern "C" {
57 #endif /* __cplusplus */
59 typedef u32 sigtype;
60 typedef sigtype sig_header;
61 typedef sig_header signed_blob;
63 typedef u8 sha1[20];
64 typedef u8 aeskey[16];
66 typedef struct _sig_rsa2048 {
67 sigtype type;
68 u8 sig[256];
69 u8 fill[60];
70 } __attribute__((packed)) sig_rsa2048;
72 typedef struct _sig_rsa4096 {
73 sigtype type;
74 u8 sig[512];
75 u8 fill[60];
76 } __attribute__((packed)) sig_rsa4096;
78 typedef char sig_issuer[0x40];
80 typedef struct _tiklimit {
81 u32 tag;
82 u32 value;
83 } __attribute__((packed)) tiklimit;
85 typedef struct _tikview {
86 u32 view;
87 u64 ticketid;
88 u32 devicetype;
89 u64 titleid;
90 u16 access_mask;
91 u8 reserved[0x3c];
92 u8 cidx_mask[0x40];
93 u16 padding;
94 tiklimit limits[8];
95 } __attribute__((packed)) tikview;
97 typedef struct _tik {
98 sig_issuer issuer;
99 u8 fill[63]; //TODO: not really fill
100 aeskey cipher_title_key;
101 u8 fill2;
102 u64 ticketid;
103 u32 devicetype;
104 u64 titleid;
105 u16 access_mask;
106 u8 reserved[0x3c];
107 u8 cidx_mask[0x40];
108 u16 padding;
109 tiklimit limits[8];
110 } __attribute__((packed)) tik;
112 typedef struct _tmd_content {
113 u32 cid;
114 u16 index;
115 u16 type;
116 u64 size;
117 sha1 hash;
118 } __attribute__((packed)) tmd_content;
120 typedef struct _tmd {
121 sig_issuer issuer; //0x140
122 u8 version; //0x180
123 u8 ca_crl_version; //0x181
124 u8 signer_crl_version; //0x182
125 u8 fill2; //0x183
126 u64 sys_version; //0x184
127 u64 title_id; //0x18c
128 u32 title_type; //0x194
129 u16 group_id; //0x198
130 u16 zero; //0x19a
131 u16 region; //0x19c
132 u8 ratings[16]; //0x19e
133 u8 reserved[12]; //0x1ae
134 u8 ipc_mask[12];
135 u8 reserved2[18];
136 u32 access_rights;
137 u16 title_version;
138 u16 num_contents;
139 u16 boot_index;
140 u16 fill3;
141 // content records follow
142 // C99 flexible array
143 tmd_content contents[];
144 } __attribute__((packed)) tmd;
146 typedef struct _cert_header {
147 sig_issuer issuer;
148 u32 cert_type;
149 char cert_name[64];
150 u32 cert_id; //???
151 } __attribute__((packed)) cert_header;
153 typedef struct _cert_rsa2048 {
154 sig_issuer issuer;
155 u32 cert_type;
156 char cert_name[64];
157 u32 cert_id;
158 u8 modulus[256];
159 u32 exponent;
160 u8 pad[0x34];
161 } __attribute__((packed)) cert_rsa2048;
163 typedef struct _cert_rsa4096 {
164 sig_issuer issuer;
165 u32 cert_type;
166 char cert_name[64];
167 u32 cert_id;
168 u8 modulus[512];
169 u32 exponent;
170 u8 pad[0x34];
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) )
206 s32 __ES_Init(void);
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);
256 #ifdef __cplusplus
258 #endif /* __cplusplus */
260 #endif /* defined(HW_RVL) */
262 #endif