Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/libkirk/kirk_engine.h
3187 views
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
#pragma once
28
29
#include "kirk_common.h"
30
#include "SHA1.h"
31
#include "AES.h"
32
33
#ifdef __cplusplus
34
extern "C" {
35
#endif
36
37
//Kirk return values
38
#define KIRK_OPERATION_SUCCESS 0
39
#define KIRK_NOT_ENABLED 1
40
#define KIRK_INVALID_MODE 2
41
#define KIRK_HEADER_HASH_INVALID 3
42
#define KIRK_DATA_HASH_INVALID 4
43
#define KIRK_SIG_CHECK_INVALID 5
44
#define KIRK_UNK_1 6
45
#define KIRK_UNK_2 7
46
#define KIRK_UNK_3 8
47
#define KIRK_UNK_4 9
48
#define KIRK_UNK_5 0xA
49
#define KIRK_UNK_6 0xB
50
#define KIRK_NOT_INITIALIZED 0xC
51
#define KIRK_INVALID_OPERATION 0xD
52
#define KIRK_INVALID_SEED_CODE 0xE
53
#define KIRK_INVALID_SIZE 0xF
54
#define KIRK_DATA_SIZE_ZERO 0x10
55
56
typedef struct
57
{
58
int mode; //0
59
int unk_4; //4
60
int unk_8; //8
61
int keyseed; //C
62
int data_size; //10
63
} KIRK_AES128CBC_HEADER; //0x14
64
65
typedef struct
66
{
67
u8 AES_key[16]; //0
68
u8 CMAC_key[16]; //10
69
u8 CMAC_header_hash[16]; //20
70
u8 CMAC_data_hash[16]; //30
71
u8 unused[32]; //40
72
u32 mode; //60
73
u8 ecdsa_hash; //64
74
u8 unk3[11]; //65
75
u32 data_size; //70
76
u32 data_offset; //74
77
u8 unk4[8]; //78
78
u8 unk5[16]; //80
79
} KIRK_CMD1_HEADER; //0x90
80
81
typedef struct
82
{
83
u8 AES_key[16]; //0
84
u8 header_sig_r[20]; //10
85
u8 header_sig_s[20]; //24
86
u8 data_sig_r[20]; //38
87
u8 data_sig_s[20]; //4C
88
u32 mode; //60
89
u8 ecdsa_hash; //64
90
u8 unk3[11]; //65
91
u32 data_size; //70
92
u32 data_offset; //74
93
u8 unk4[8]; //78
94
u8 unk5[16]; //80
95
} KIRK_CMD1_ECDSA_HEADER; //0x90
96
97
98
typedef struct
99
{
100
u8 r[0x14];
101
u8 s[0x14];
102
} ECDSA_SIG; //0x28
103
typedef struct
104
{
105
u8 x[0x14];
106
u8 y[0x14];
107
} ECDSA_POINT; //0x28
108
109
typedef struct
110
{
111
u32 data_size; //0
112
} KIRK_SHA1_HEADER; //4
113
114
typedef struct
115
{
116
u8 private_key[0x14];
117
ECDSA_POINT public_key;
118
} KIRK_CMD12_BUFFER;
119
120
typedef struct
121
{
122
u8 multiplier[0x14];
123
ECDSA_POINT public_key;
124
} KIRK_CMD13_BUFFER;
125
126
127
typedef struct
128
{
129
u8 enc_private[0x20]; //0
130
u8 message_hash[0x14]; //20
131
} KIRK_CMD16_BUFFER;//0x34
132
133
typedef struct
134
{
135
ECDSA_POINT public_key; //0
136
u8 message_hash[0x14]; //28
137
ECDSA_SIG signature; //3C
138
} KIRK_CMD17_BUFFER;//0x64
139
140
//mode passed to sceUtilsBufferCopyWithRange
141
#define KIRK_CMD_DECRYPT_PRIVATE 1
142
#define KIRK_CMD_2 2
143
#define KIRK_CMD_3 3
144
#define KIRK_CMD_ENCRYPT_IV_0 4
145
#define KIRK_CMD_ENCRYPT_IV_FUSE 5
146
#define KIRK_CMD_ENCRYPT_IV_USER 6
147
#define KIRK_CMD_DECRYPT_IV_0 7
148
#define KIRK_CMD_DECRYPT_IV_FUSE 8
149
#define KIRK_CMD_DECRYPT_IV_USER 9
150
#define KIRK_CMD_PRIV_SIGN_CHECK 10
151
#define KIRK_CMD_SHA1_HASH 11
152
#define KIRK_CMD_ECDSA_GEN_KEYS 12
153
#define KIRK_CMD_ECDSA_MULTIPLY_POINT 13
154
#define KIRK_CMD_PRNG 14
155
#define KIRK_CMD_15 15
156
#define KIRK_CMD_ECDSA_SIGN 16
157
#define KIRK_CMD_ECDSA_VERIFY 17
158
159
//"mode" in header
160
#define KIRK_MODE_CMD1 1
161
#define KIRK_MODE_CMD2 2
162
#define KIRK_MODE_CMD3 3
163
#define KIRK_MODE_ENCRYPT_CBC 4
164
#define KIRK_MODE_DECRYPT_CBC 5
165
166
//sceUtilsBufferCopyWithRange errors
167
#define SUBCWR_NOT_16_ALGINED 0x90A
168
#define SUBCWR_HEADER_HASH_INVALID 0x920
169
#define SUBCWR_BUFFER_TOO_SMALL 0x1000
170
171
/*
172
// Private Sig + Cipher
173
0x01: Super-Duper decryption (no inverse)
174
0x02: Encrypt Operation (inverse of 0x03)
175
0x03: Decrypt Operation (inverse of 0x02)
176
177
// Cipher
178
0x04: Encrypt Operation (inverse of 0x07) (IV=0)
179
0x05: Encrypt Operation (inverse of 0x08) (IV=FuseID)
180
0x06: Encrypt Operation (inverse of 0x09) (IV=UserDefined)
181
0x07: Decrypt Operation (inverse of 0x04)
182
0x08: Decrypt Operation (inverse of 0x05)
183
0x09: Decrypt Operation (inverse of 0x06)
184
185
// Sig Gens
186
0x0A: Private Signature Check (checks for private SCE sig)
187
0x0B: SHA1 Hash
188
0x0C: Mul1
189
0x0D: Mul2
190
0x0E: Random Number Gen
191
0x0F: (absolutely no idea ?could be KIRK initialization)
192
0x10: Signature Gen
193
// Sig Checks
194
0x11: Signature Check (checks for generated sigs)
195
0x12: Certificate Check (idstorage signatures)
196
*/
197
198
typedef struct KirkState {
199
u32 g_fuse90; // This is to match FuseID HW at BC100090 and BC100094
200
u32 g_fuse94;
201
202
AES_ctx aes_kirk1; //global
203
u8 PRNG_DATA[0x14];
204
u8 kirk_buf[0x0814]; // 1DC0 1DD4
205
206
char is_kirk_initialized; //"init" emulation
207
} KirkState;
208
209
//kirk-like funcs
210
int kirk_CMD0(KirkState *kirk, u8* outbuff, const u8* inbuff, int size, int generate_trash);
211
int kirk_CMD1(KirkState *kirk, u8* outbuff, u8* inbuff, int size);
212
213
int kirk_CMD4(KirkState *kirk, u8* outbuff, const u8* inbuff, int size);
214
int kirk_CMD7(KirkState *kirk, u8* outbuff, const u8* inbuff, int size);
215
int kirk_CMD10(KirkState *kirk, u8* inbuff, int insize);
216
int kirk_CMD11(KirkState *kirk, u8* outbuff, const u8* inbuff, int size);
217
int kirk_CMD12(KirkState *kirk, u8* outbuff, int outsize);
218
int kirk_CMD13(KirkState *kirk, u8* outbuff, int outsize,u8* inbuff, int insize);
219
int kirk_CMD14(KirkState *kirk, u8* outbuff, int outsize);
220
int kirk_CMD16(KirkState *kirk, u8* outbuff, int outsize,u8* inbuff, int insize);
221
int kirk_CMD17(KirkState *kirk, const u8* inbuff, int insize);
222
223
int kirk_init(KirkState *kirk); //CMD 0xF?
224
int kirk_init2(KirkState *kirk, u8 *, u32, u32, u32);
225
226
// overhead free functions
227
void kirk4(u8* outbuff, const u8* inbuff, size_t size, int keyId);
228
void kirk7(u8* outbuff, const u8* inbuff, size_t size, int keyId);
229
230
//helper funcs
231
const u8* kirk_4_7_get_key(int key_type);
232
233
//kirk "ex" functions
234
int kirk_CMD1_ex(KirkState *kirk, u8* outbuff, u8* inbuff, int size, KIRK_CMD1_HEADER* header);
235
236
//sce-like func. sceUtilsBufferCopyWithRange is clearly intentionally confusingly named.
237
// Also, some commands do write to inbuff!
238
int kirk_sceUtilsBufferCopyWithRange(KirkState *kirk, u8* outbuff, int outsize, u8* inbuff, int insize, int cmd);
239
void decrypt_kirk16_private(KirkState *kirk, u8 *dA_out, u8 *dA_enc);
240
void encrypt_kirk16_private(KirkState *kirk, u8 *dA_out, u8 *dA_dec);
241
242
// Prototypes for the Elliptic Curve and Big Number functions
243
int ecdsa_set_curve(const u8* p, const u8* a, const u8* b, const u8* N, const u8* Gx, const u8* Gy);
244
void ecdsa_set_pub(u8 *Q);
245
void ecdsa_set_priv(u8 *k);
246
int ecdsa_verify(u8 *hash, u8 *R, u8 *S);
247
void ecdsa_sign(KirkState *kirk, u8 *hash, u8 *R, u8 *S);
248
void ec_priv_to_pub(u8 *k, u8 *Q);
249
void ec_pub_mult(u8 *k, u8 *Q);
250
251
void bn_copy(u8 *d, const u8 *a, u32 n);
252
int bn_compare(const u8 *a, const u8 *b, u32 n);
253
void bn_reduce(u8 *d, const u8 *N, u32 n);
254
void bn_add(u8 *d, const u8 *a, const u8 *b, const u8 *N, u32 n);
255
void bn_sub(u8 *d, const u8 *a, const u8 *b, const u8 *N, u32 n);
256
void bn_to_mon(u8 *d, const u8 *N, u32 n);
257
void bn_from_mon(u8 *d, const u8 *N, u32 n);
258
void bn_mon_mul(u8 *d, const u8 *a, const u8 *b, const u8 *N, u32 n);
259
void bn_mon_inv(u8 *d, const u8 *a, const u8 *N, u32 n);
260
void hex_dump(const char *str, const u8 *buf, int size);
261
262
#define round_up(x,n) (-(-(x) & -(n)))
263
264
#define array_size(x) (sizeof(x) / sizeof(*(x)))
265
266
#ifdef __cplusplus
267
}
268
#endif
269
270