CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libkirk/kirk_engine.h
Views: 1401
/*1Draan proudly presents:23With huge help from community:4coyotebean, Davee, hitchhikr, kgsws, liquidzigong, Mathieulh, Proxima, SilverSpring56******************** KIRK-ENGINE ********************7An Open-Source implementation of KIRK (PSP crypto engine) algorithms and keys.8Includes also additional routines for hash forging.910********************1112This program is free software: you can redistribute it and/or modify13it under the terms of the GNU General Public License as published by14the Free Software Foundation, either version 3 of the License, or15(at your option) any later version.1617This program is distributed in the hope that it will be useful,18but WITHOUT ANY WARRANTY; without even the implied warranty of19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20GNU General Public License for more details.2122You should have received a copy of the GNU General Public License23along with this program. If not, see <http://www.gnu.org/licenses/>.24*/2526#ifndef KIRK_ENGINE27#define KIRK_ENGINE28typedef unsigned char u8;29typedef unsigned short int u16;30typedef unsigned int u32;3132//Kirk return values33#define KIRK_OPERATION_SUCCESS 034#define KIRK_NOT_ENABLED 135#define KIRK_INVALID_MODE 236#define KIRK_HEADER_HASH_INVALID 337#define KIRK_DATA_HASH_INVALID 438#define KIRK_SIG_CHECK_INVALID 539#define KIRK_UNK_1 640#define KIRK_UNK_2 741#define KIRK_UNK_3 842#define KIRK_UNK_4 943#define KIRK_UNK_5 0xA44#define KIRK_UNK_6 0xB45#define KIRK_NOT_INITIALIZED 0xC46#define KIRK_INVALID_OPERATION 0xD47#define KIRK_INVALID_SEED_CODE 0xE48#define KIRK_INVALID_SIZE 0xF49#define KIRK_DATA_SIZE_ZERO 0x105051typedef struct52{53int mode; //054int unk_4; //455int unk_8; //856int keyseed; //C57int data_size; //1058} KIRK_AES128CBC_HEADER; //0x145960typedef struct61{62u8 AES_key[16]; //063u8 CMAC_key[16]; //1064u8 CMAC_header_hash[16]; //2065u8 CMAC_data_hash[16]; //3066u8 unused[32]; //4067u32 mode; //6068u8 ecdsa_hash; //6469u8 unk3[11]; //6570u32 data_size; //7071u32 data_offset; //7472u8 unk4[8]; //7873u8 unk5[16]; //8074} KIRK_CMD1_HEADER; //0x907576typedef struct77{78u8 AES_key[16]; //079u8 header_sig_r[20]; //1080u8 header_sig_s[20]; //2481u8 data_sig_r[20]; //3882u8 data_sig_s[20]; //4C83u32 mode; //6084u8 ecdsa_hash; //6485u8 unk3[11]; //6586u32 data_size; //7087u32 data_offset; //7488u8 unk4[8]; //7889u8 unk5[16]; //8090} KIRK_CMD1_ECDSA_HEADER; //0x90919293typedef struct94{95u8 r[0x14];96u8 s[0x14];97} ECDSA_SIG; //0x2898typedef struct99{100u8 x[0x14];101u8 y[0x14];102} ECDSA_POINT; //0x28103104typedef struct105{106u32 data_size; //0107} KIRK_SHA1_HEADER; //4108109typedef struct110{111u8 private_key[0x14];112ECDSA_POINT public_key;113} KIRK_CMD12_BUFFER;114115typedef struct116{117u8 multiplier[0x14];118ECDSA_POINT public_key;119} KIRK_CMD13_BUFFER;120121122typedef struct123{124u8 enc_private[0x20]; //0125u8 message_hash[0x14]; //20126} KIRK_CMD16_BUFFER;//0x34127128typedef struct129{130ECDSA_POINT public_key; //0131u8 message_hash[0x14]; //28132ECDSA_SIG signature; //3C133} KIRK_CMD17_BUFFER;//0x64134135//mode passed to sceUtilsBufferCopyWithRange136#define KIRK_CMD_DECRYPT_PRIVATE 1137#define KIRK_CMD_2 2138#define KIRK_CMD_3 3139#define KIRK_CMD_ENCRYPT_IV_0 4140#define KIRK_CMD_ENCRYPT_IV_FUSE 5141#define KIRK_CMD_ENCRYPT_IV_USER 6142#define KIRK_CMD_DECRYPT_IV_0 7143#define KIRK_CMD_DECRYPT_IV_FUSE 8144#define KIRK_CMD_DECRYPT_IV_USER 9145#define KIRK_CMD_PRIV_SIGN_CHECK 10146#define KIRK_CMD_SHA1_HASH 11147#define KIRK_CMD_ECDSA_GEN_KEYS 12148#define KIRK_CMD_ECDSA_MULTIPLY_POINT 13149#define KIRK_CMD_PRNG 14150#define KIRK_CMD_15 15151#define KIRK_CMD_ECDSA_SIGN 16152#define KIRK_CMD_ECDSA_VERIFY 17153154//"mode" in header155#define KIRK_MODE_CMD1 1156#define KIRK_MODE_CMD2 2157#define KIRK_MODE_CMD3 3158#define KIRK_MODE_ENCRYPT_CBC 4159#define KIRK_MODE_DECRYPT_CBC 5160161//sceUtilsBufferCopyWithRange errors162#define SUBCWR_NOT_16_ALGINED 0x90A163#define SUBCWR_HEADER_HASH_INVALID 0x920164#define SUBCWR_BUFFER_TOO_SMALL 0x1000165166/*167// Private Sig + Cipher1680x01: Super-Duper decryption (no inverse)1690x02: Encrypt Operation (inverse of 0x03)1700x03: Decrypt Operation (inverse of 0x02)171172// Cipher1730x04: Encrypt Operation (inverse of 0x07) (IV=0)1740x05: Encrypt Operation (inverse of 0x08) (IV=FuseID)1750x06: Encrypt Operation (inverse of 0x09) (IV=UserDefined)1760x07: Decrypt Operation (inverse of 0x04)1770x08: Decrypt Operation (inverse of 0x05)1780x09: Decrypt Operation (inverse of 0x06)179180// Sig Gens1810x0A: Private Signature Check (checks for private SCE sig)1820x0B: SHA1 Hash1830x0C: Mul11840x0D: Mul21850x0E: Random Number Gen1860x0F: (absolutely no idea ?could be KIRK initialization)1870x10: Signature Gen188// Sig Checks1890x11: Signature Check (checks for generated sigs)1900x12: Certificate Check (idstorage signatures)191*/192193//kirk-like funcs194int kirk_CMD0(u8* outbuff, u8* inbuff, int size, int generate_trash);195int kirk_CMD1(u8* outbuff, u8* inbuff, int size);196197int kirk_CMD4(u8* outbuff, u8* inbuff, int size);198int kirk_CMD7(u8* outbuff, u8* inbuff, int size);199int kirk_CMD10(u8* inbuff, int insize);200int kirk_CMD11(u8* outbuff, u8* inbuff, int size);201int kirk_CMD12(u8* outbuff, int outsize);202int kirk_CMD13(u8* outbuff, int outsize,u8* inbuff, int insize);203int kirk_CMD14(u8* outbuff, int outsize);204int kirk_CMD16(u8* outbuff, int outsize,u8* inbuff, int insize);205int kirk_CMD17(u8* inbuff, int insize);206207int kirk_init(); //CMD 0xF?208int kirk_init2(u8 *, u32, u32, u32);209210// overhead free functions211void kirk4(u8* outbuff, const u8* inbuff, size_t size, int keyId);212void kirk7(u8* outbuff, const u8* inbuff, size_t size, int keyId);213214//helper funcs215u8* kirk_4_7_get_key(int key_type);216217//kirk "ex" functions218int kirk_CMD1_ex(u8* outbuff, u8* inbuff, int size, KIRK_CMD1_HEADER* header);219220//sce-like func. sceUtilsBufferCopyWithRange is clearly intentionally confusingly named.221int kirk_sceUtilsBufferCopyWithRange(u8* outbuff, int outsize, const u8* inbuff, int insize, int cmd);222void decrypt_kirk16_private(u8 *dA_out, u8 *dA_enc);223void encrypt_kirk16_private(u8 *dA_out, u8 *dA_dec);224225// Prototypes for the Elliptic Curve and Big Number functions226int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy);227int ecdsa_set_curve(u8* p,u8* a,u8* b,u8* N,u8* Gx,u8* Gy);228void ecdsa_set_pub(u8 *Q);229void ecdsa_set_priv(u8 *k);230int ecdsa_verify(u8 *hash, u8 *R, u8 *S);231void ecdsa_sign(u8 *hash, u8 *R, u8 *S);232void ec_priv_to_pub(u8 *k, u8 *Q);233void ec_pub_mult(u8 *k, u8 *Q);234void bn_copy(u8 *d, u8 *a, u32 n);235int bn_compare(u8 *a, u8 *b, u32 n);236void bn_reduce(u8 *d, u8 *N, u32 n);237void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);238void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);239void bn_to_mon(u8 *d, u8 *N, u32 n);240void bn_from_mon(u8 *d, u8 *N, u32 n);241void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n);242void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n);243void hex_dump(char *str, u8 *buf, int size);244245#define round_up(x,n) (-(-(x) & -(n)))246247#define array_size(x) (sizeof(x) / sizeof(*(x)))248#endif249250251