CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/libkirk/kirk_engine.h
Views: 1401
1
/*
2
Draan proudly presents:
3
4
With huge help from community:
5
coyotebean, Davee, hitchhikr, kgsws, liquidzigong, Mathieulh, Proxima, SilverSpring
6
7
******************** KIRK-ENGINE ********************
8
An Open-Source implementation of KIRK (PSP crypto engine) algorithms and keys.
9
Includes also additional routines for hash forging.
10
11
********************
12
13
This program is free software: you can redistribute it and/or modify
14
it under the terms of the GNU General Public License as published by
15
the Free Software Foundation, either version 3 of the License, or
16
(at your option) any later version.
17
18
This program is distributed in the hope that it will be useful,
19
but WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
GNU General Public License for more details.
22
23
You should have received a copy of the GNU General Public License
24
along with this program. If not, see <http://www.gnu.org/licenses/>.
25
*/
26
27
#ifndef KIRK_ENGINE
28
#define KIRK_ENGINE
29
typedef unsigned char u8;
30
typedef unsigned short int u16;
31
typedef unsigned int u32;
32
33
//Kirk return values
34
#define KIRK_OPERATION_SUCCESS 0
35
#define KIRK_NOT_ENABLED 1
36
#define KIRK_INVALID_MODE 2
37
#define KIRK_HEADER_HASH_INVALID 3
38
#define KIRK_DATA_HASH_INVALID 4
39
#define KIRK_SIG_CHECK_INVALID 5
40
#define KIRK_UNK_1 6
41
#define KIRK_UNK_2 7
42
#define KIRK_UNK_3 8
43
#define KIRK_UNK_4 9
44
#define KIRK_UNK_5 0xA
45
#define KIRK_UNK_6 0xB
46
#define KIRK_NOT_INITIALIZED 0xC
47
#define KIRK_INVALID_OPERATION 0xD
48
#define KIRK_INVALID_SEED_CODE 0xE
49
#define KIRK_INVALID_SIZE 0xF
50
#define KIRK_DATA_SIZE_ZERO 0x10
51
52
typedef struct
53
{
54
int mode; //0
55
int unk_4; //4
56
int unk_8; //8
57
int keyseed; //C
58
int data_size; //10
59
} KIRK_AES128CBC_HEADER; //0x14
60
61
typedef struct
62
{
63
u8 AES_key[16]; //0
64
u8 CMAC_key[16]; //10
65
u8 CMAC_header_hash[16]; //20
66
u8 CMAC_data_hash[16]; //30
67
u8 unused[32]; //40
68
u32 mode; //60
69
u8 ecdsa_hash; //64
70
u8 unk3[11]; //65
71
u32 data_size; //70
72
u32 data_offset; //74
73
u8 unk4[8]; //78
74
u8 unk5[16]; //80
75
} KIRK_CMD1_HEADER; //0x90
76
77
typedef struct
78
{
79
u8 AES_key[16]; //0
80
u8 header_sig_r[20]; //10
81
u8 header_sig_s[20]; //24
82
u8 data_sig_r[20]; //38
83
u8 data_sig_s[20]; //4C
84
u32 mode; //60
85
u8 ecdsa_hash; //64
86
u8 unk3[11]; //65
87
u32 data_size; //70
88
u32 data_offset; //74
89
u8 unk4[8]; //78
90
u8 unk5[16]; //80
91
} KIRK_CMD1_ECDSA_HEADER; //0x90
92
93
94
typedef struct
95
{
96
u8 r[0x14];
97
u8 s[0x14];
98
} ECDSA_SIG; //0x28
99
typedef struct
100
{
101
u8 x[0x14];
102
u8 y[0x14];
103
} ECDSA_POINT; //0x28
104
105
typedef struct
106
{
107
u32 data_size; //0
108
} KIRK_SHA1_HEADER; //4
109
110
typedef struct
111
{
112
u8 private_key[0x14];
113
ECDSA_POINT public_key;
114
} KIRK_CMD12_BUFFER;
115
116
typedef struct
117
{
118
u8 multiplier[0x14];
119
ECDSA_POINT public_key;
120
} KIRK_CMD13_BUFFER;
121
122
123
typedef struct
124
{
125
u8 enc_private[0x20]; //0
126
u8 message_hash[0x14]; //20
127
} KIRK_CMD16_BUFFER;//0x34
128
129
typedef struct
130
{
131
ECDSA_POINT public_key; //0
132
u8 message_hash[0x14]; //28
133
ECDSA_SIG signature; //3C
134
} KIRK_CMD17_BUFFER;//0x64
135
136
//mode passed to sceUtilsBufferCopyWithRange
137
#define KIRK_CMD_DECRYPT_PRIVATE 1
138
#define KIRK_CMD_2 2
139
#define KIRK_CMD_3 3
140
#define KIRK_CMD_ENCRYPT_IV_0 4
141
#define KIRK_CMD_ENCRYPT_IV_FUSE 5
142
#define KIRK_CMD_ENCRYPT_IV_USER 6
143
#define KIRK_CMD_DECRYPT_IV_0 7
144
#define KIRK_CMD_DECRYPT_IV_FUSE 8
145
#define KIRK_CMD_DECRYPT_IV_USER 9
146
#define KIRK_CMD_PRIV_SIGN_CHECK 10
147
#define KIRK_CMD_SHA1_HASH 11
148
#define KIRK_CMD_ECDSA_GEN_KEYS 12
149
#define KIRK_CMD_ECDSA_MULTIPLY_POINT 13
150
#define KIRK_CMD_PRNG 14
151
#define KIRK_CMD_15 15
152
#define KIRK_CMD_ECDSA_SIGN 16
153
#define KIRK_CMD_ECDSA_VERIFY 17
154
155
//"mode" in header
156
#define KIRK_MODE_CMD1 1
157
#define KIRK_MODE_CMD2 2
158
#define KIRK_MODE_CMD3 3
159
#define KIRK_MODE_ENCRYPT_CBC 4
160
#define KIRK_MODE_DECRYPT_CBC 5
161
162
//sceUtilsBufferCopyWithRange errors
163
#define SUBCWR_NOT_16_ALGINED 0x90A
164
#define SUBCWR_HEADER_HASH_INVALID 0x920
165
#define SUBCWR_BUFFER_TOO_SMALL 0x1000
166
167
/*
168
// Private Sig + Cipher
169
0x01: Super-Duper decryption (no inverse)
170
0x02: Encrypt Operation (inverse of 0x03)
171
0x03: Decrypt Operation (inverse of 0x02)
172
173
// Cipher
174
0x04: Encrypt Operation (inverse of 0x07) (IV=0)
175
0x05: Encrypt Operation (inverse of 0x08) (IV=FuseID)
176
0x06: Encrypt Operation (inverse of 0x09) (IV=UserDefined)
177
0x07: Decrypt Operation (inverse of 0x04)
178
0x08: Decrypt Operation (inverse of 0x05)
179
0x09: Decrypt Operation (inverse of 0x06)
180
181
// Sig Gens
182
0x0A: Private Signature Check (checks for private SCE sig)
183
0x0B: SHA1 Hash
184
0x0C: Mul1
185
0x0D: Mul2
186
0x0E: Random Number Gen
187
0x0F: (absolutely no idea ?could be KIRK initialization)
188
0x10: Signature Gen
189
// Sig Checks
190
0x11: Signature Check (checks for generated sigs)
191
0x12: Certificate Check (idstorage signatures)
192
*/
193
194
//kirk-like funcs
195
int kirk_CMD0(u8* outbuff, u8* inbuff, int size, int generate_trash);
196
int kirk_CMD1(u8* outbuff, u8* inbuff, int size);
197
198
int kirk_CMD4(u8* outbuff, u8* inbuff, int size);
199
int kirk_CMD7(u8* outbuff, u8* inbuff, int size);
200
int kirk_CMD10(u8* inbuff, int insize);
201
int kirk_CMD11(u8* outbuff, u8* inbuff, int size);
202
int kirk_CMD12(u8* outbuff, int outsize);
203
int kirk_CMD13(u8* outbuff, int outsize,u8* inbuff, int insize);
204
int kirk_CMD14(u8* outbuff, int outsize);
205
int kirk_CMD16(u8* outbuff, int outsize,u8* inbuff, int insize);
206
int kirk_CMD17(u8* inbuff, int insize);
207
208
int kirk_init(); //CMD 0xF?
209
int kirk_init2(u8 *, u32, u32, u32);
210
211
// overhead free functions
212
void kirk4(u8* outbuff, const u8* inbuff, size_t size, int keyId);
213
void kirk7(u8* outbuff, const u8* inbuff, size_t size, int keyId);
214
215
//helper funcs
216
u8* kirk_4_7_get_key(int key_type);
217
218
//kirk "ex" functions
219
int kirk_CMD1_ex(u8* outbuff, u8* inbuff, int size, KIRK_CMD1_HEADER* header);
220
221
//sce-like func. sceUtilsBufferCopyWithRange is clearly intentionally confusingly named.
222
int kirk_sceUtilsBufferCopyWithRange(u8* outbuff, int outsize, const u8* inbuff, int insize, int cmd);
223
void decrypt_kirk16_private(u8 *dA_out, u8 *dA_enc);
224
void encrypt_kirk16_private(u8 *dA_out, u8 *dA_dec);
225
226
// Prototypes for the Elliptic Curve and Big Number functions
227
int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy);
228
int ecdsa_set_curve(u8* p,u8* a,u8* b,u8* N,u8* Gx,u8* Gy);
229
void ecdsa_set_pub(u8 *Q);
230
void ecdsa_set_priv(u8 *k);
231
int ecdsa_verify(u8 *hash, u8 *R, u8 *S);
232
void ecdsa_sign(u8 *hash, u8 *R, u8 *S);
233
void ec_priv_to_pub(u8 *k, u8 *Q);
234
void ec_pub_mult(u8 *k, u8 *Q);
235
void bn_copy(u8 *d, u8 *a, u32 n);
236
int bn_compare(u8 *a, u8 *b, u32 n);
237
void bn_reduce(u8 *d, u8 *N, u32 n);
238
void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
239
void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
240
void bn_to_mon(u8 *d, u8 *N, u32 n);
241
void bn_from_mon(u8 *d, u8 *N, u32 n);
242
void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);
243
void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n);
244
void hex_dump(char *str, u8 *buf, int size);
245
246
#define round_up(x,n) (-(-(x) & -(n)))
247
248
#define array_size(x) (sizeof(x) / sizeof(*(x)))
249
#endif
250
251