国密项目中的签到

    国密项目中的签到,即下载工作密钥,需要下载TPK、TAK密钥。

    签到的代码实现如下:    

s32 SendLoginBag(u16 usManNo)
{
    s32 slt = 0;

    memset(&gstTradeData, 0, sizeof(gstTradeData));
    gstTradeData.usManNo = usManNo;
    gstSys.isLogined = false;
    gstSys.isPosLogin = false;
    DispTradeTitle("签   到");  //显示标题
    gstTradeData.ucTradeId = TRADEID_MANLOGIN;

    /*与后台进行交互*/
    slt = Send8583Bag(false, MODE_SAVENONE, NULL, NULL, FormSendLoginBag, DISP_APPTRADE);

    slt = DealLoginReponse(slt);  //签到响应处理    
	
    return slt;
}

     终端向服务器组织报文数据代码如下:

static s32 FormSendLoginBag(struct _TradeData *pstTradeData, SDK_8583_ST8583 *pstBag8583)
{
    u8 temp[128] = {0},szSN[40];
    u32 i = 0;
    u8 possn[32 +1] = {0};
    u8 cupsn[5] = {0};
    int nRet;

    if(pstBag8583 == NULL)
    {
        Assert(0);
        return SDK_ERR;
    }
    
    /*填充位元表*/
    nRet = FormPublicSend(pstBag8583, pstTradeData, "0020000000C00016");  //11、41、42、60、62、63域有数据
    if(nRet <= 0)
    {
        return 0;
    }
    
    SetFieldData(pstBag8583, SDK_8583_FIELD_MSG, "0800", 4);  //填充消息类型数据,请求

    /*填充60域数据*/
    memset(temp, 0, sizeof(temp));
    strcat(temp, "00");  //交易类型码
    strcat(temp, gstSys.asBatchNo);  //批次号

    /*网络管理信息码*/
    if(gstAppSetting.stKeySystemParam.bIsMk3Des)  //支持3DES
    {
        if(gstAppSetting.stKeySystemParam.bIsSupportTdk)  //支持TDK
        {
            strcat(temp, "004");  //双倍长密钥算法(含磁道密钥)
        }
        else
        {
            strcat(temp, "003");  //双倍长密钥算法
        }
    }
    else
    {
        strcat(temp, "001");  //单倍长密钥算法
    }
    SetFieldData(pstBag8583, 60, temp, strlen(temp));    

    /*填充62域数据*/
    memset(temp,0,sizeof(temp));
    memset(szSN,0,sizeof(szSN));
    
    sdkSecurityRequestDeviceSn(szSN,sizeof(szSN));
    memcpy(possn, &szSN[strlen(szSN) - 8], 8);	
    sprintf((char *)temp,"Sequence No12%4.4s%8.8s","0057",possn);
    SetFieldData(pstBag8583, 62, temp, strlen(temp));
    
    /*填充63域数据*/
    memset(temp, 0, sizeof(temp));
    sprintf(temp, "%02d", gstTradeData.usManNo);  //操作员代码
    i = strlen(temp);
    if(i < 3)
    {
        temp[2] = 0x20;
    }
    SetFieldData(pstBag8583, 63, temp, 3);    

    return pstBag8583->nBagLen;
}

      签到响应处理的代码实现如下:

s32 DealLoginReponse(const s32 siRet)
{
    s32 slt = SDK_OK;
    s32 len  = 3;
    u8 res[3] = {0},Buff[2];
    u8 temp[128] = {0};
    int tmkIndex = 0;
   
    memset(res, 0, sizeof(res));
    len = GetFieldData(pgstRecvBag, 39, res, 3);  //取出应答码

    if(siRet == SDK_OK)  //交互成功
    {
        sdkCommDestoryLink();

        /*取出60域数据*/
        len = GetFieldData(pgstRecvBag, 60, temp, sizeof(temp));
        if(len <= 0)
        {
            return SDK_ESC;
        }
        
        /*保存使用的主密钥索引*/
        memset(Buff,0,sizeof(Buff));
        memcpy(Buff,temp+11,1);
        tmkIndex= atoi((char *)Buff);
        gstAppSetting.stKeySystemParam.uiTMkIndex= tmkIndex;
        Trace("chenke", "_______   使用的主秘钥索引  %d  \n", tmkIndex );
		
        SaveSetting();  //写"/mtd0/dir1setting"文件保存参数

        /*更新工作密钥*/
        slt = DecryptWK();  
        if(slt == SDK_OK)  //更新成功  
        {
            gstSys.isPosLogin = true;                                          
            gstSys.usLoginMan = gstTradeData.usManNo;
            gstSys.isLogined = true;

            /*获取60域数据*/
            len = GetFieldData(pgstRecvBag, 60, temp, sizeof(temp));
            if(len <= 0)
            {
                return SDK_ESC;
            }
            memcpy(gstSys.asBatchNo, &temp[2], 6);  //批次号
            
            SaveAppPosCfg();  //写文件保存终端配置参数            

            sdkSysBeep(SDK_SYS_BEEP_OK);
            strcpy(temp, "签到成功!");
            sdkDispPromptBox(false, TV_SHOW, temp);

            return SDK_OK;
        }

        sdkDispPromptBox(false, TV_SHOW, "签到失败");
        return SDK_ESC;
    }
    else
    {
        slt = DealTradeErrReponse(siRet);
    }

    return slt;
}

      更新工作密钥的代码实现如下:

static s32 DecryptWK(void)
{
    s32 slt = 0;
    u8 revbuf[128] = {0};
    s32 len = 0;
    s32 num = 0;
    SDK_PED_WK_CFG stwkcfg;
    SDK_PED_WK_CFG stAllWKCfg[3];
    u32 nCount = 0;

    /*取出62域数据*/
    len = GetFieldData(pgstRecvBag, 62, revbuf, 128);
    if(len < 0)
    {
        Trace("DecryptWK", "取RECV62错误\r\n");
        return SDK_ERR;
    }   

    /*数据长度错误*/
    if(len != 24 && len != 40 && len != 56 && len != 60 && len != 84)
    {
        Trace("DecryptWK", "62长度错误len = %d \r\n", len);
        return SDK_ERR;
    }
    memset(&stwkcfg, 0, sizeof(stwkcfg));

    /*取出TPK密钥的密文和校验值*/
    if(gstAppSetting.stKeySystemParam.bIsMk3Des)
    {
        if(gstAppSetting.bisSupportGM == 0)
        {
            stwkcfg.eTmkType = SDK_PED_DES_TRIPLE;
        }
        else
        {
            stwkcfg.eTmkType = SDK_PED_SM4;
        }
				
        stwkcfg.eWkType = SDK_PED_TYPE_PINK;  
        stwkcfg.ucTmkIndex = gstAppSetting.stKeySystemParam.uiTMkIndex;
        stwkcfg.ucEnWkLen = 16;

        /*取出TPK密钥的密文*/
        memcpy(stwkcfg.heEnWk, &revbuf[nCount], 16); 
        nCount += 16;
				
        stwkcfg.ucCheckDataLen = 16;
        memset(stwkcfg.heCheckData, 0, 16);
        stwkcfg.ucWkIndex = WKINDEX;
        
        /*取出TPK密钥的校验值*/		
        memcpy(stwkcfg.heCheckValue, &revbuf[nCount], 4);
        nCount += 4;
				
        stwkcfg.ucCheckValueLen = 4;
        stwkcfg.bOnlyCalcCheckValue = false;
    }
    else
    {
        stwkcfg.eTmkType = SDK_PED_DES_SINGLE;
        stwkcfg.eWkType = SDK_PED_TYPE_PINK;
        stwkcfg.ucTmkIndex = gstAppSetting.stKeySystemParam.uiTMkIndex;
        stwkcfg.ucEnWkLen = 8;
				
        memcpy(stwkcfg.heEnWk, &revbuf[nCount], 8);
        nCount += 8;
				
        stwkcfg.ucCheckDataLen = 8;
        memset(stwkcfg.heCheckData, 0, 8);
        stwkcfg.ucWkIndex = WKINDEX;
			
        memcpy(stwkcfg.heCheckValue, &revbuf[nCount], 4);
        Count += 4;
		 
        stwkcfg.ucCheckValueLen = 4;
        stwkcfg.bOnlyCalcCheckValue = false;
    }   

    stAllWKCfg[num] = stwkcfg;
    num++;    
    memset(&stwkcfg, 0, sizeof(stwkcfg));
    
    /*取出TAK密钥的密文和校验值*/
    if(gstAppSetting.stKeySystemParam.bIsMk3Des)
    {
        if(gstAppSetting.bisSupportGM == 0)
        {
            stwkcfg.eTmkType = SDK_PED_DES_TRIPLE;
            stwkcfg.ucEnWkLen = 8;
					
            memcpy(stwkcfg.heEnWk, &revbuf[nCount], 8);                              
            nCount += 8;

            stwkcfg.ucCheckDataLen = 8;
            memset(stwkcfg.heCheckData, 0, 8);			
        }
        else
        {
            stwkcfg.eTmkType = SDK_PED_SM4;
            stwkcfg.ucEnWkLen = 16;

            /*取出TAK密钥的密文*/
            memcpy(stwkcfg.heEnWk, &revbuf[nCount], 16);
            nCount += 16;

            stwkcfg.ucCheckDataLen = 16;
            memset(stwkcfg.heCheckData, 0, 16);
        }
		
        stwkcfg.eWkType = SDK_PED_TYPE_MACK;
        stwkcfg.ucTmkIndex = gstAppSetting.stKeySystemParam.uiTMkIndex;
        stwkcfg.ucWkIndex = WKINDEX;
        
        /*取出TAK密钥的校验值*/ 
        memcpy(stwkcfg.heCheckValue, &revbuf[nCount], 4);
        nCount += 4;
		 
        stwkcfg.ucCheckValueLen = 4;
        stwkcfg.bOnlyCalcCheckValue = false;
    }
    else
    {
        stwkcfg.eTmkType = SDK_PED_DES_SINGLE;
        stwkcfg.eWkType = SDK_PED_TYPE_MACK;
        stwkcfg.ucTmkIndex = gstAppSetting.stKeySystemParam.uiTMkIndex;
        stwkcfg.ucEnWkLen = 8;
				
        memcpy(stwkcfg.heEnWk, &revbuf[nCount], 8);
        nCount += 8;
				
        stwkcfg.ucCheckDataLen = 8;
        memset(stwkcfg.heCheckData, 0, 8);
        stwkcfg.ucWkIndex = WKINDEX;
				
        memcpy(stwkcfg.heCheckValue, &revbuf[nCount], 4);
        nCount += 4;
		 
        stwkcfg.ucCheckValueLen = 4;
        stwkcfg.bOnlyCalcCheckValue = false;
    }
   
    stAllWKCfg[num] = stwkcfg;
    num++;

    /*取出TDK密钥的密文和校验值*/
    if(gstAppSetting.stKeySystemParam.bIsMk3Des && gstAppSetting.stKeySystemParam.bIsSupportTdk)
    {
        memset(&stwkcfg, 0, sizeof(stwkcfg));

        if(gstAppSetting.bisSupportGM == 0)
        {
            stwkcfg.eTmkType = SDK_PED_DES_TRIPLE;
        }
        else
        {
            stwkcfg.eTmkType = SDK_PED_SM4;
        }
		
        stwkcfg.eWkType = SDK_PED_TYPE_TDK;
        stwkcfg.ucTmkIndex = gstAppSetting.stKeySystemParam.uiTMkIndex;
        stwkcfg.ucEnWkLen = 16;
        
        /*取出TDK密钥的密文*/
        memcpy(stwkcfg.heEnWk, &revbuf[nCount], 16);                          
        nCount += 16;
				
        stwkcfg.ucCheckDataLen = 16;
        memset(stwkcfg.heCheckData, 0, 16);
        stwkcfg.ucWkIndex = WKINDEX;
        
        /*取出TDK密钥的校验值*/		
        memcpy(stwkcfg.heCheckValue, &revbuf[nCount], 4);
        nCount += 4;
		 
        stwkcfg.ucCheckValueLen = 4;
        stwkcfg.bOnlyCalcCheckValue = false;        
        stAllWKCfg[num] = stwkcfg;
        num++;
    }

    /*更新工作密钥*/
    slt = sdkPedUpdateWk(stAllWKCfg, num, 1000);
    if (slt != SDK_OK)
    {
        if(slt == SDK_PED_TPK_ERR)
        {
            slt = ERR_TPK;
        }
        else if(slt == SDK_PED_TAK_ERR)
        {
            slt = ERR_TAK;
        }
        else 
        {
            slt = ERR_FLUSHWK;
        }
        
        return slt;
    }
	
    return slt;
}

      更新工作密钥的代码实现如下:

s32 sdkPedUpdateWk(SDK_PED_WK_CFG *pstWkCfg, const s32 iWknum, s32 iTimeout)
{
    s32 i, rslt = 0;
    u8 tempwkindex;
    strInnerTKeyDEncryptPara sttkey;
    u8 buff[64] = {0};

    if(!(pstWkCfg != NULL) || !(iWknum > 0) || !(iTimeout >= 100))
    {
        Assert(0);
        return SDK_PARA_ERR;
    }
    
    /*TPK、TAK、TDK工作密钥更新*/
    for(i = 0; i < iWknum; i++)
    {
        if(!(pstWkCfg[i].ucTmkIndex < PCIKEYTYPE_TMK_MAXINDEX))
        {
            Assert(0);
            return SDK_PARA_ERR;
        }

        if(pstWkCfg[0].bOnlyCalcCheckValue)
        {
            pstWkCfg[i].ucCheckValueLen = pstWkCfg[i].ucCheckDataLen;
        }

        if(pstWkCfg[i].eTmkType == SDK_PED_DES_SINGLE)
        {
            if (pstWkCfg[i].ucEnWkLen != 8 || pstWkCfg[i].ucCheckDataLen != 8 ||
					pstWkCfg[i].ucCheckValueLen > 8)
            {
                return SDK_PARA_ERR;
            }
        }
        else if (pstWkCfg[i].eTmkType == SDK_PED_DES_TRIPLE)
        {
            if (pstWkCfg[i].ucEnWkLen != 16 || pstWkCfg[i].ucCheckDataLen != 16 ||
					pstWkCfg[i].ucCheckValueLen > 16)
            {
                return SDK_PARA_ERR;
            }
        }
		
        if (!(pstWkCfg[i].ucEnWkLen <= 32))
        {
            Assert(0);
            return SDK_PARA_ERR;
        }

        if (!(pstWkCfg[i].ucCheckDataLen <= 32))
        {
            Assert(0);
            return SDK_PARA_ERR;
        }

        tempwkindex = pstWkCfg[i].ucWkIndex;       

        /*灌注工作密钥*/
        if(pstWkCfg[i].eTmkType != SDK_PED_SM4)
        {
            rslt = ddi_innerkey_inject(pstWkCfg[i].ucTmkIndex,pstWkCfg[i].eWkType,tempwkindex, pstWkCfg[i].heEnWk,pstWkCfg[i].ucEnWkLen,WKTYPE_TMKENCRYPTED_NOCHECKVALUE);
        }
        else  //国密算法
        {
            rslt = ddi_innerkey_inject(pstWkCfg[i].ucTmkIndex,pstWkCfg[i].eWkType,tempwkindex, pstWkCfg[i].heEnWk,pstWkCfg[i].ucEnWkLen,WKTYPE_TMKENCRYPTED_NOCHECKVALUE_SM);
        }       

        return_if_ddi_error(rslt);

        memset(buff, 0, sizeof(buff));
        sttkey.m_indata = pstWkCfg[i].heCheckData;
        sttkey.m_len = pstWkCfg[i].ucCheckDataLen;
        sttkey.m_outdata = buff;
        memcpy(sttkey.m_outdata,sttkey.m_indata,pstWkCfg[i].ucCheckDataLen);
       
        /*计算校验值*/
        if(pstWkCfg[i].eTmkType != SDK_PED_SM4)
        {
            rslt = ddi_innerkey_encrypt(KEY_GROUPINDEX,pstWkCfg[i].eWkType,tempwkindex,sttkey.m_outdata,sttkey.m_len,0);
        }
        else
        {
            rslt = ddi_innerkey_encrypt(KEY_GROUPINDEX,pstWkCfg[i].eWkType,tempwkindex,sttkey.m_outdata,sttkey.m_len,5);
        }

        return_if_ddi_error(rslt);       
		
        if(pstWkCfg[0].bOnlyCalcCheckValue)
        {
            memcpy(pstWkCfg[i].heCheckValue, sttkey.m_outdata, pstWkCfg[i].ucCheckValueLen);
        }
        else
        {
            /*比较校验值*/
            if(memcmp(sttkey.m_outdata, pstWkCfg[i].heCheckValue, pstWkCfg[i].ucCheckValueLen))
            {
                return SDK_ERR;
            }
        }
    
        if(!pstWkCfg[0].bOnlyCalcCheckValue)
        {
            if (!(pstWkCfg[i].ucWkIndex < 125))
            {
                Assert(0);
                return SDK_PARA_ERR;
            }

            rslt = DDI_OK;
        }
    }

    return SDK_OK;
}

 

发布了17 篇原创文章 · 获赞 0 · 访问量 633
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览