DLCCard.h 3.36 KB
/* ×÷ÕߣºÁõÒ°
 * 
 **/

#ifndef DLC_CARD_H
#define DLC_CARD_H

#include "stdafx.h"
#include "DLCDevice.h"

enum CardManufacturer
{
	kCardZXRC = 0x00,
	kCardJFYT = 0x10,
	kCardHS = 0x20,
	kCardMTLTLD = 0x30,
	kCardKDZN = 0x40,
	kCardNULL = 0xff
};

enum KeyType
{
	kDEAK = 0,
	kDAMK = 1,
	kDCCK = 2
};

enum FileType
{
	kFTMF = 0,
	kFTDDF = 1,
	kFTADF = 2,
	kFTEF = 3,
	kFTKF = 4
};


class DLCCard
{
public:
	DLCCard();
	~DLCCard();

	static DLCCard& Instance();
	int ReadCardData(const unsigned char* dir, const unsigned char file_id, const unsigned int data_offset, const unsigned short data_len, const unsigned char* key, unsigned char* data) const;
	int WriteCardData(const unsigned char* dir, const unsigned char file_id, const unsigned char data_offset, const unsigned short data_len, const unsigned char* key, const unsigned char* data) const;
	int InstallCardKey(const unsigned char* dir, const unsigned char key_id, const unsigned char* key_main, const int key_type, const unsigned char* key) const;
	int AuthenticationCardKey(const unsigned char* dir, const unsigned char key_id, const unsigned char* key) const;
	int CreatCardFile(const unsigned char* parent_dir, const unsigned char* parent_key, const unsigned char* file_name, const unsigned char* file_size, const int file_type) const;
	int GetRandom(unsigned char* random, const unsigned char random_len) const;
	int SelectDir(const unsigned char* dir) const;
	int ExternalAuthenticate(const unsigned char key_id, const unsigned char* key, const unsigned char* random) const;
	int SelectCardDir(const unsigned char* dir) const;

	int SOGetRandom(unsigned char* get_randam_cmd) const;
	int SOAuthenticationKey(const unsigned char key_id, const unsigned char* key, const unsigned char* random, unsigned char* cmd) const;
	int SOReadData(const unsigned int data_offfset, const unsigned int data_len, unsigned char* cmd) const;
	int SOWriteData(const unsigned char* write_key, const unsigned char* random, const unsigned int data_offfset, const unsigned int data_len, const unsigned char* data, unsigned char* cmd) const;
	int SOSelectDir(const unsigned char* dir, unsigned char* cmd) const;

	int M1AuthenticationKey(const unsigned short section, unsigned char* key) const;
	int M1ReadCard(const unsigned short block_num, const unsigned char* data) const;
	int M1WriteCard(const unsigned short block_num, const unsigned char* data) const;

private:

	CardManufacturer CheckCardMF() const;

	
	int ReadData(const unsigned short data_offset, const short data_len, unsigned char* data) const;
	int CalculateMac(const unsigned char* data, const short data_len, const unsigned char* random, const unsigned char* key, unsigned char* mac) const;
	int InstallKey(unsigned char* cmd, unsigned char cmd_len) const;
	int WriteData(unsigned char* cmd, unsigned char cmd_len, bool is_need_recv, unsigned char* recv, unsigned char* recv_len) const;
	int Encrypt(const unsigned char* src, unsigned char len, const unsigned char* key, unsigned char* ciphert_text) const;
	int FirstInstallKey(const unsigned char* dir_hex, const unsigned char key_id, const unsigned char key_type, const unsigned char* main_key, const unsigned char* key) const;

	//DLCDevice m_cDevice;
	unsigned char select_cmd_[5];
	unsigned char read_cmd_[2];
	unsigned char write_cmd_[2];
	unsigned char get_random_[4];
	unsigned char external_authenticate_[3];
	unsigned char install_key_cmd_[5];

};


#endif //end DLC_CARD_H