📄 ic.c
字号:
#include "reg.h"
/*本函数用于通过IC卡对密码卡进行初始化*/
int card_init()
{
extern int r_IC(),inputkey();
extern void flash_w(),secerase(),poke(),memcopy();
extern int peek();
int i,status;
secerase(0x01440000); /*将flash的0x01440000扇区擦除*/
status=r_IC(0x331,0x80008804,2); /*从IC卡中读出RSA公钥模长*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
status=r_IC(0x333,0x80008808,4); /*从IC卡中读出RSA公钥的加密指数*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
flash_w(0x80008804,0x01440000,2); /*向flash中写入公钥模长和加密指数*/
status=r_IC(0x437,0x80008804,2); /*从IC卡中读出RSA私钥模长*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
flash_w(0x80008804,0x01440108,1); /*向flash中写入私钥模长*/
status=r_IC(0x6B9,0x80008804,2); /*从IC卡中读出SMC公钥模长*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
status=r_IC(0x6BB,0x80008808,4); /*从IC卡中读出SMC公钥的加密指数*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
flash_w(0x80008804,0x0144038C,2); /*向flash中写入SMC公钥模长和加密指数*/
status=r_IC(0x321,0x80008808,16); /*从IC卡中读出会话密钥*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
status=r_IC(0x337,0x80008818,128); /*从IC卡中读出RSA公钥的积(密文)*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
poke(0x80008804,8,2); /*输入待解密数据的组数*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,1,0); /*用会话密钥解密RSA公钥的积*/
for(i=0;i<16;i++)
{
memcopy(0x800088F4-i*16,0x80008908-i*16,4,2);
} /*移动RSA公钥的积(明文),为用kp1加密做准备*/
status=r_IC(0x9,0x80008808,16); /*从IC卡中读出主密钥kp1*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
poke(0x80008804,8,2); /*输入待加密数据的组数*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,0,0); /*用主密钥kp1加密RSA公钥的积*/
flash_w(0x80008804,0x01440008,32); /*向flash中写入RSA公钥的密文积*/
status=r_IC(0x321,0x80008808,16); /*从IC卡中读出会话密钥*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
}
status=r_IC(0x439,0x80008818,640); /*从IC卡中读出RSA私钥的密文部分*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
poke(0x80008804,40,2); /*输入待解密数据的组数*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,1,0); /*用会话密钥解密RSA私钥的密文部分*/
for(i=0;i<40;i++)
{
memcopy(0x80008A74-i*16,0x80008A88-i*16,4,2);
} /*移动RSA私钥,为用kp1加密做准备*/
/*若读IC卡失败,进行相应处理*/
status=r_IC(0x09,0x80008808,16); /*从IC卡中读出会话密钥*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
}
poke(0x80008804,40,2); /*输入待加密数据的组数*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,0,0); /*用主密钥kp1加密RSA私钥*/
flash_w(0x80008804,0x0144010C,160); /*向flash中写入RSA私钥的密文*/
status=r_IC(0x321,0x80008808,16); /*从IC卡中读出会话密钥*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
}
status=r_IC(0x6BF,0x80008818,128); /*从IC卡中读出SMC公钥的积(密文)*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
poke(0x80008804,8,2); /*输入待解密数据的组数*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,1,0); /*用会话密钥解密SMC公钥的积*/
for(i=0;i<16;i++)
{
memcopy(0x800088F4-i*16,0x80008908-i*16,4,2);
} /*移动数据,为用kp1加密做准备*/
status=r_IC(0x9,0x80008808,16); /*从IC卡中读出会话密钥*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
}
poke(0x80008804,8,2); /*输入待加密数据的组数*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,0,0); /*用主密钥加密SMC公钥的积*/
/*用主密钥加密SMC公钥的积*/
flash_w(0x80008804,0x01440394,32); /*向flash中写入SMC公钥的积(密文)*/
secerase(0x01480000); /*将flash的0x01480000扇区擦除,即清空用户信息*/
memcopy(0x01480000,0x8000C800,50,2);
poke(0x8000BFFC,0,2); /*将用户状态码变为无用户使用状态*/
status=0;
poke(0x80008800,0,2);
label1: return status;
}
int makeICHead()
{
extern int w_IC();
extern void poke();
int status;
status=w_IC(0x80008804,0x0001,800); /*将头信息写入IC卡中*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label8;
}
if (status==0x32)
{
status=0x71;
poke(0x80008800,0x71,2);
goto label8;
} /*若写IC卡失败,进行相应处理*/
poke(0x80008800,0,2);
status=0;
label8: return status;
}
/*本函数用于用所给的参数制密钥卡*/
int keyIC_create()
{
extern int w_IC(),inputkey(),erandx();
extern void poke(),memcopy();
int i,status = 0;
unsigned int random[4];
for (i=0;i<4;i++)
{
random[i] = exrandx();
poke(0x80008808+i*4,random[i],2);
}
status=w_IC(0x80008808,0x321,16); /*将会话密钥存入IC卡*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label2;
}
if (status==0x32)
{
status=0x72;
poke(0x80008800,0x72,2);
goto label2;
} /*若写IC卡失败,进行相应处理*/
status=w_IC(0x80008B24,0x331,6); /*将RSA公钥模长和加密指数存入IC卡*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label2;
}
if (status==0x32)
{
status=0x73;
poke(0x80008800,0x73,2);
goto label2;
} /*若写IC卡失败,进行相应处理*/
memcopy(0x80008B2A,0x80008818,128,1); /*将RSA公钥的积移到指定位置,准备加密*/
poke(0x80008804,8,2); /*输入数据组数,准备加密*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,0,0); /*用会话密钥加密RSA公钥的积*/
status=w_IC(0x80008804,0x337,128); /*将加密后的RSA公钥的积写入IC卡*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label2;
}
if (status==0x32)
{
status=0x74;
poke(0x80008800,0x74,2);
goto label2;
} /*若写IC卡失败,进行相应处理*/
status=w_IC(0x80008C2A,0x437,2); /*将RSA私钥的模长写入IC卡*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label2;
}
if (status==0x32)
{
status=0x75;
poke(0x80008800,0x75,2);
goto label2;
} /*若写IC卡失败,进行相应处理*/
memcopy(random,0x80008808,4,2); /*将会话密钥写入加密位置*/
memcopy(0x80008C2C,0x80008818,160,2); /*将RSA私钥的要加密部分移到加密位置*/
poke(0x80008804,40,2); /*输入数据组数,准备加密*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,0,0); /*用会话密钥加密RSA私钥*/
status=w_IC(0x80008804,0x439,640); /*将加密后的RSA私钥写入IC卡*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label2;
}
if (status==0x32)
{
status=0x76;
poke(0x80008800,0x76,2);
goto label2;
} /*若写IC卡失败,进行相应处理*/
status=w_IC(0x80008EAC,0x6B9,6); /*将SMC公钥模长和加密指数存入IC卡*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label2;
}
if (status==0x32)
{
status=0x77;
poke(0x80008800,0x77,2);
goto label2;
} /*若写IC卡失败,进行相应处理*/
memcopy(random,0x80008808,4,2); /*将会话密钥写入加密位置*/
memcopy(0x80008EB2,0x80008818,128,1); /*将SMC公钥的积移到加密位置*/
poke(0x80008804,8,2); /*输入数据组数,准备加密*/
inputkey(0x80008804,0x80008808,0x80008818,0x80008804,0,0); /*用会话密钥加密SMC公钥的积*/
status=w_IC(0x80008804,0x6BF,128); /*将加密后的SMC公钥的积存入IC卡*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label2;
}
if (status==0x32)
{
status=0x78;
poke(0x80008800,0x78,2);
goto label2;
} /*若写IC卡失败,进行相应处理*/
status=0;
poke(0x80008800,0,2);
label2: return status;
}
/*本函数用于从IC卡的指定地址读取指定长度的数据*/
int IC_read()
{
extern int r_IC(),peek();
extern void poke();
int status;
unsigned int IC_addr,IC_len;
IC_addr=peek(0x80008804,2); /*读出IC卡地址*/
IC_len=peek(0x80008808,2); /*读出数据长度*/
status=r_IC(IC_addr,0x80008804,IC_len); /*从IC卡读出指定长度的数据*/
if (status==0x30)
{
status=0x3;
poke(0x80008800,0x3,2);
goto label1;
}
if (status==0x32)
{
status=0x7;
poke(0x80008800,0x7,2);
goto label1;
} /*若读IC卡失败,进行相应处理*/
status=0;
poke(0x80008800,0,2);
label1: return status;
}
/*本函数用于向IC卡的指定地址写入指定长度的数据*/
int IC_write()
{
extern int w_IC(),peek();
extern void poke();
unsigned int IC_addr,IC_len;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -