⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardlib.c

📁 cpu卡驱动源程序,根据需要可自行修改 cpu卡驱动源程序,根据需要可自行修改
💻 C
📖 第 1 页 / 共 3 页
字号:
	else apdu_comm.command[3]=0x04;
	apdu_comm.lc=len;
	apdu_comm.in_data=record;
	apdu_comm.le=0;

	for(i=0;i<3;i++)
	{
		rn=smart_card_apdu(UC_card_location,&apdu_comm,&apdu_resp);
		*status=apdu_resp.status;
		if(rn==0)
		{
			if ( apdu_resp.status == 0x9000 ) return 0;
			else return 1;
		}
		else 
		{
			if(rn==0xc2) return rn;
		}
	}
	return rn;
}

/*-------------------------------------------------------------
功能:
	校验PIN
输入:
	id PIN标识
	len PIN长度		
	pin 使用口令
	status 状态字指针
输出:
	0 成功
	1 操作完成,返回状态字不对
	0xd3 接收数据超时出错
	0xd6 奇偶校验出错
	0xd9 通信出错
	0xc2 该卡是另类CPU卡
	status 状态字
-----------------------------------------------------------------*/
uchar IC_verify(uchar id,uchar len,uchar *pin,uint *status)
{
	uchar i,rn;

	apdu_resp.status = 0;

	apdu_comm.command[0]=0;
    apdu_comm.command[1]=0x20;
	apdu_comm.command[2]=0;
	apdu_comm.command[3]=id;
	apdu_comm.lc=len;
	apdu_comm.in_data=pin;
	apdu_comm.le=0;

	for(i=0;i<3;i++)
	{
		rn=smart_card_apdu(UC_card_location,&apdu_comm,&apdu_resp);
		*status=apdu_resp.status;
		if(rn==0)
		{
			if ( apdu_resp.status == 0x9000 ) return 0;
			else return 1;
		}
		else 
		{
			if(rn==0xc2) return rn;
		}
	}
	return rn;
}

/*-------------------------------------------------------------
功能:
	请求一个相关过程的随机数
输入:
	kind 产生几位随机数的种类
		0 产生4位随机数
		1 产生8位随机数
	len 随机数的长度指针
	random 产生的随机数指针
	status 状态字指针
输出:
	0 成功
	1 操作完成,返回状态字不对
	0xd3 接收数据超时出错
	0xd6 奇偶校验出错
	0xd9 通信出错
	0xc2 该卡是另类CPU卡
	len 随机数的长度
	random 随机数
	status 状态字
-----------------------------------------------------------------*/
uchar IC_get_challenge(uchar kind,uchar *len,uchar *random,uint *status)
{
	uchar i,rn;

	apdu_resp.status = 0;

	apdu_comm.command[0]=0;
    apdu_comm.command[1]=0x84;
	apdu_comm.command[2]=0;
	apdu_comm.command[3]=0;
	apdu_comm.lc=0;
	if(kind) apdu_comm.le=8;
	else apdu_comm.le=4;
	apdu_resp.out_data=random;

	for(i=0;i<3;i++)
	{
		rn=smart_card_apdu(UC_card_location,&apdu_comm,&apdu_resp);
		*status=apdu_resp.status;
		if(rn==0)
		{
			*len=apdu_resp.out_len;
			if ( apdu_resp.status == 0x9000 ) return 0;
			else return 1;
		}
		else 
		{
			if(rn==0xc2) return rn;
		}
	}
	return rn;
}

/*-------------------------------------------------------------
功能:
	IC卡验证随机数经外部密码运算后的数据是否正确
输入:
	id 外部鉴别密钥标识符
	encrypt 随机数经外部密码运算后的8字节数据
	status 状态字指针
输出:
	0 成功
	1 操作完成,返回状态字不对
	0xd3 接收数据超时出错
	0xd6 奇偶校验出错
	0xd9 通信出错
	0xc2 该卡是另类CPU卡
	status 状态字
-----------------------------------------------------------------*/
uchar IC_external_authentication(uchar id,uchar *encrypt,uint *status)
{
	uchar i,rn;

	apdu_resp.status = 0;

	apdu_comm.command[0]=0;
    apdu_comm.command[1]=0x82;
	apdu_comm.command[2]=0;
	apdu_comm.command[3]=id;
	apdu_comm.lc=0x08;
	apdu_comm.in_data=encrypt;
	apdu_comm.le=0;

	for(i=0;i<3;i++)
	{
		rn=smart_card_apdu(UC_card_location,&apdu_comm,&apdu_resp);
		*status=apdu_resp.status;
		if(rn==0)
		{			
			if ( apdu_resp.status == 0x9000 ) return 0;
			else return 1;
		}
		else 
		{
			if(rn==0xc2) return rn;
		}
	}
	return rn;
}

/*-------------------------------------------------------------
功能:
	IC卡对要认证的数据(例如接口设备发来的随机数)和自身存储的内部鉴别密钥进行密码运算并获取运算结果
输入:
	id 内部鉴别密钥标识符
	source 要认证的8字节数据
	len 密码运算后的数据长度指针
	encrypt 密码运算后的数据指针
	status 状态字指针
输出:
	0 成功
	1 操作完成,返回状态字不对
	0xd3 接收数据超时出错
	0xd6 奇偶校验出错
	0xd9 通信出错
	0xc2 该卡是另类CPU卡
	len 密码运算后的数据长度
	encrypt 密码运算后的8字节数据
	status 状态字
-----------------------------------------------------------------*/
uchar IC_internal_authentication(uchar id,uchar *source,uchar *len,uchar *encrypt,uint *status)
{
	uchar i,rn;

	apdu_resp.status = 0;

	apdu_comm.command[0]=0;
    apdu_comm.command[1]=0x88;
	apdu_comm.command[2]=0;
	apdu_comm.command[3]=id;
	apdu_comm.lc=0x08;
	apdu_comm.in_data=source;
	apdu_comm.le=0;
	apdu_resp.out_data=encrypt;

	for(i=0;i<3;i++)
	{
		rn=smart_card_apdu(UC_card_location,&apdu_comm,&apdu_resp);
		*status=apdu_resp.status;
		if(rn==0)
		{
			*len=apdu_resp.out_len;
			if ( apdu_resp.status == 0x9000 ) return 0;
			else return 1;
		}
		else 
		{
			if(rn==0xc2) return rn;
		}
	}
	return rn;
}

//税控卡专用
/*-------------------------------------------------------------
功能:
	获取税控卡编号,用于以后税控卡注册
输入:
	len 注册数据的长度指针
	reg 注册数据指针
	status 状态字指针
输出:
	0 成功
	1 操作完成,返回状态字不对
	0xd3 接收数据超时出错
	0xd6 奇偶校验出错
	0xd9 通信出错
	0xc2 该卡是另类CPU卡
	len 注册数据的长度
	reg 16字节的注册数据
		包括4字节随机数+8字节税控卡编号+4字节MAC1
	status 状态字
--------------------------------------------------------------*/
uchar TSAM_get_register_nb(uchar *len,uchar *reg,uint *status)
{
	uchar i,rn;

	apdu_resp.status = 0;

    apdu_comm.command[0]=0xc0;
    apdu_comm.command[1]=0xf0;
    apdu_comm.command[2]=0;
    apdu_comm.command[3]=0;
    apdu_comm.lc=0;
    apdu_comm.le=0x10;
	apdu_resp.out_data=reg;

	for(i=0;i<3;i++)
	{
		rn=smart_card_apdu(0,&apdu_comm,&apdu_resp);
		*status=apdu_resp.status;
		if(rn==0)
		{
			*len=apdu_resp.out_len;
			if ( apdu_resp.status == 0x9000 ) return 0;
			else return 1;
		}
		else 
		{
			if(rn==0xc2) return rn;
		}
	}
	return rn;
}

/*-------------------------------------------------------------
功能:
	进行税控卡注册,并获取税控卡的使用口令
输入:
	mac2 由用户卡注册获取的4字节MAC2
	len 税控卡使用口令的长度指针
	pin 税控卡使用口令指针
	status 状态字指针
输出:
	0 成功
	1 操作完成,返回状态字不对
	0xd3 接收数据超时出错
	0xd6 奇偶校验出错
	0xd9 通信出错
	0xc2 该卡是另类CPU卡
	len 税控卡使用口令的长度
	pin 8字节税控卡使用口令
	status 状态字
--------------------------------------------------------------*/
uchar TSAM_terminal_register(uchar *mac2,uchar *len,uchar *pin,uint *status)
{
	uchar i,rn;

	apdu_resp.status = 0;

    apdu_comm.command[0]=0xc0;
    apdu_comm.command[1]=0xf1;
    apdu_comm.command[2]=0;
    apdu_comm.command[3]=0;
    apdu_comm.lc=0x04;
    apdu_comm.in_data=mac2;
    apdu_comm.le=0x08;
	apdu_resp.out_data=pin;

	for(i=0;i<3;i++)
	{
		rn=smart_card_apdu(0,&apdu_comm,&apdu_resp);
		*status=apdu_resp.status;
		if(rn==0)
		{
			*len=apdu_resp.out_len;
			if ( apdu_resp.status == 0x9000 ) return 0;
			else return 1;
		}
		else 
		{
			if(rn==0xc2) return rn;
		}
	}
	return rn;
}

/*-------------------------------------------------------------
功能:
	生成发票税控码,更新日交易记录
输入:
	invoice 长度为43字节的开票数据
		包括4字节开票日期+1字节开票类型+4字节发票号+30字节分类金额+4字节开票总金额
	len 税控码的长度指针
	control_code 税控码指针
	status 状态字指针
输出:
	0 成功
	1 操作完成,返回状态字不对
	0xd3 接收数据超时出错
	0xd6 奇偶校验出错
	0xd9 通信出错
	0xc2 该卡是另类CPU卡
	len 税控码的长度
	control_code 8字节税控码
	status 状态字
--------------------------------------------------------------*/
uchar TSAM_issue_invoice(uchar *invoice,uchar *len,uchar *control_code,uint *status)
{
	uchar i,rn;

	apdu_resp.status = 0;

    apdu_comm.command[0]=0xc0;
    apdu_comm.command[1]=0xf2;
    apdu_comm.command[2]=0;
    apdu_comm.command[3]=0;
    apdu_comm.lc=0x2c;
    apdu_comm.in_data=invoice;
    apdu_comm.le=0x08;
	apdu_resp.out_data=control_code;

	for(i=0;i<3;i++)
	{
		rn=smart_card_apdu(0,&apdu_comm,&apdu_resp);
		*status=apdu_resp.status;
		if(rn==0)
		{
			*len=apdu_resp.out_len;
			if ( apdu_resp.status == 0x9000 ) return 0;
			else return 1;
		}
		else 
		{
			if(rn==0xc2) return rn;
		}
	}
	return rn;
}

/*--------------------------------------------------------------------------------------------
功能:
	生成申报数据
输入:
	declare 长度为70字节的申报数据
		包括4字节起始日期+4字节截止日期+4正常发票张数+2字节退票张数+2字节废票张数
		+6字节税种税目索引号+24字节正常发票分类累计金额+24字节退票分类累计金额
	len 签字数据的长度指针
	sign 签字数据指针
	status 状态字指针
输出:
	0 成功
	1 操作完成,返回状态字不对
	0xd3 接收数据超时出错
	0xd6 奇偶校验出错
	0xd9 通信出错
	0xc2 该卡是另类CPU卡
	len 签字数据的长度
	sign 141字节签字数据

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -