[SM国密算法][OpenSSL] PBOC Offline Data验证之CDA

void CLibTesterDlg::VerifyIssuserPublicKeyWithSignature_CDA()
{
    VerifyIssuserPublicKeyWithSignature_DDA();
}

void CLibTesterDlg::VerifyIccPublicKeyWithSignature_CDA()
{
    VerifyIccPublicKeyWithSignature_DDA();
}

void CLibTesterDlg::VerifyGacDataWithSignature_CDA()
{
    //PBOC
    //////////////////////////////////////////////////////////////////////////
    //Step 1 (Issuer public key)
    BYTE byPublicKeyXA[32] = {0xC3, 0xAC, 0x12, 0xB8, 0x1B, 0x9D, 0x17, 0x59, 0x36, 0xB5, 0xBF, 0x72, 0xBB, 0x8F, 0xE3, 0xA2,
                              0x26, 0x6B, 0xC0, 0x13, 0xB2, 0xE9, 0x4F, 0x58, 0x37, 0xF1, 0x6A, 0xA1, 0xC0, 0x1A, 0xA7, 0x32};
    BYTE byPublicKeyYA[32] = {0x3B, 0x75, 0x62, 0x6A, 0xB6, 0x4D, 0x02, 0xAE, 0xD2, 0x0C, 0xC6, 0xF4, 0x40, 0x84, 0x1F, 0x10,
                              0xEE, 0x68, 0x73, 0xBC, 0xBE, 0xA3, 0xF4, 0x1D, 0x68, 0x69, 0xD0, 0xFE, 0xAD, 0xD7, 0x11, 0x54};
    SetupBasiSMAlgorithm(byPublicKeyXA, byPublicKeyYA);

    //Step 2
    BYTE byHash[32] = {0};
    SM3_GenZaHash(byHash);

    //Step 3
    BYTE byData[] = {0x15,
                     0x2C,
                     0x02, 0x11, 0x22,
                     0x40, 0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF, 0xAB, 0xCD,
                     0xA3, 0x47, 0xA7, 0x97, 0x92, 0x7B, 0x6B, 0xFA, 0xAC, 0x9B, 0xFD, 0xBE, 0x89, 0xC4, 0x2C, 0x25,
                     0xC9, 0x31, 0x06, 0xB9, 0xB9, 0xDE, 0x9B, 0x61, 0xB1, 0xE9, 0xC5, 0x98, 0x1F, 0x3A, 0xDD, 0x98,
                     0x34, 0xEA, 0x02, 0xAB};

    BYTE byR[32] =  {0xF0, 0x09, 0x93, 0xF3, 0xD2, 0x07, 0x1E, 0xEE, 0x6E, 0xD6, 0x85, 0x91, 0xA1, 0x55, 0x12, 0x98,
                     0x55, 0x1F, 0x90, 0x3D, 0x12, 0x32, 0x86, 0x64, 0x3B, 0x6D, 0xC3, 0x5C, 0xA2, 0x7E, 0x17, 0x85};
    BYTE byS[32] =  {0x9B, 0x17, 0xE7, 0x7C, 0x69, 0x65, 0x72, 0x6A, 0x94, 0x48, 0x2C, 0xD9, 0xA0, 0xA5, 0x63, 0xE0,
                     0xF9, 0xD1, 0x0F, 0x38, 0x09, 0xE6, 0xAD, 0x9A, 0x1F, 0x96, 0xA3, 0x43, 0xD6, 0x5C, 0x85, 0x31};
    BYTE byFullData[1024] = {0};
    int nFillLen = 0;

    memcpy(byFullData, byHash, 32);
    nFillLen += 32;
    memcpy(byFullData+nFillLen, byData, sizeof(byData));
    nFillLen += sizeof(byData);
    SM3(byFullData, nFillLen, byHash); //Get Hash for Signature

    BOOL bRet = SM2_VerifySignature(byHash, 32, byR, byS);

    CString strLog;
    GetDlgItem(IDC_LOG_EDIT)->GetWindowText(strLog);
    if (bRet)
    {
        strLog += _T("VerifyGacDataWithSignature OK!\r\n");
    }
    else
    {
        strLog += _T("VerifyGacDataWithSignature Failed!\r\n");
    }
    GetDlgItem(IDC_LOG_EDIT)->SetWindowText(strLog);
}

void CLibTesterDlg::VerifyGacDataWithHash_CDA()
{
    BYTE byData[] = {0x01,  //PDOL
                     0x34, 0xEA, 0x02, 0xAB, //CDOL1
                     0x00, 0x00, 0x80, 0x00, 0x00,
                     0xC8, 0x00,
                     0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                     0x01, 0x56,
                     0x01, 0x56,
                     0x12, 0x03, 0x03,
                     0x00,
                     //GAC1 response 
                     0x9F, 0x27, 0x01, 0x40,
                     0x9F, 0x36, 0x02, 0x00, 0x01};
    BYTE byExceptedHash[32] = {0xA3, 0x47, 0xA7, 0x97, 0x92, 0x7B, 0x6B, 0xFA, 0xAC, 0x9B, 0xFD, 0xBE, 0x89, 0xC4, 0x2C, 0x25,
                               0xC9, 0x31, 0x06, 0xB9, 0xB9, 0xDE, 0x9B, 0x61, 0xB1, 0xE9, 0xC5, 0x98, 0x1F, 0x3A, 0xDD, 0x98};
    BYTE byHash[32] = {0};

    SM3(byData, sizeof(byData), byHash);

    CString strLog;
    GetDlgItem(IDC_LOG_EDIT)->GetWindowText(strLog);
    if (memcmp(byHash, byExceptedHash, 32) == 0)
    {
        strLog += _T("VerifyGacDataWithHash OK!\r\n");
    }
    else
    {
        strLog += _T("VerifyGacDataWithHash Failed!\r\n");
    }
    GetDlgItem(IDC_LOG_EDIT)->SetWindowText(strLog);
}

void CLibTesterDlg::VerifyIssuserPublicKeyWithSignature2_CDA()
{
    VerifyIssuserPublicKeyWithSignature2_DDA();
}

void CLibTesterDlg::VerifyIccPublicKeyWithSignature2_CDA()
{
    VerifyIccPublicKeyWithSignature2_DDA();
}

void CLibTesterDlg::VerifyGacDataWithSignature2_CDA()
{
    //PBOC
    //////////////////////////////////////////////////////////////////////////
    //Step 1 (Issuer public key)
    BYTE byPublicKeyXA[32] = {0xC3, 0xAC, 0x12, 0xB8, 0x1B, 0x9D, 0x17, 0x59, 0x36, 0xB5, 0xBF, 0x72, 0xBB, 0x8F, 0xE3, 0xA2,
                              0x26, 0x6B, 0xC0, 0x13, 0xB2, 0xE9, 0x4F, 0x58, 0x37, 0xF1, 0x6A, 0xA1, 0xC0, 0x1A, 0xA7, 0x32};
    BYTE byPublicKeyYA[32] = {0x3B, 0x75, 0x62, 0x6A, 0xB6, 0x4D, 0x02, 0xAE, 0xD2, 0x0C, 0xC6, 0xF4, 0x40, 0x84, 0x1F, 0x10,
                              0xEE, 0x68, 0x73, 0xBC, 0xBE, 0xA3, 0xF4, 0x1D, 0x68, 0x69, 0xD0, 0xFE, 0xAD, 0xD7, 0x11, 0x54};
    SetupBasiSMAlgorithm(byPublicKeyXA, byPublicKeyYA);

    //Step 2
    BYTE byData[] = {0x15,
                     0x2C,
                     0x02, 0x11, 0x22,
                     0x40, 0xAB, 0xCD, 0xEF, 0xAB, 0xCD, 0xEF, 0xAB, 0xCD,
                     0xA3, 0x47, 0xA7, 0x97, 0x92, 0x7B, 0x6B, 0xFA, 0xAC, 0x9B, 0xFD, 0xBE, 0x89, 0xC4, 0x2C, 0x25,
                     0xC9, 0x31, 0x06, 0xB9, 0xB9, 0xDE, 0x9B, 0x61, 0xB1, 0xE9, 0xC5, 0x98, 0x1F, 0x3A, 0xDD, 0x98,
                     0x34, 0xEA, 0x02, 0xAB};
    BYTE byR[32] =  {0xF0, 0x09, 0x93, 0xF3, 0xD2, 0x07, 0x1E, 0xEE, 0x6E, 0xD6, 0x85, 0x91, 0xA1, 0x55, 0x12, 0x98,
                     0x55, 0x1F, 0x90, 0x3D, 0x12, 0x32, 0x86, 0x64, 0x3B, 0x6D, 0xC3, 0x5C, 0xA2, 0x7E, 0x17, 0x85};
    BYTE byS[32] =  {0x9B, 0x17, 0xE7, 0x7C, 0x69, 0x65, 0x72, 0x6A, 0x94, 0x48, 0x2C, 0xD9, 0xA0, 0xA5, 0x63, 0xE0,
                     0xF9, 0xD1, 0x0F, 0x38, 0x09, 0xE6, 0xAD, 0x9A, 0x1F, 0x96, 0xA3, 0x43, 0xD6, 0x5C, 0x85, 0x31};

    BOOL bRet = SM2_VerifySignature2(byData, sizeof(byData), byR, byS);

    CString strLog;
    GetDlgItem(IDC_LOG_EDIT)->GetWindowText(strLog);
    if (bRet)
    {
        strLog += _T("VerifyGacDataWithSignature2 OK!\r\n");
    }
    else
    {
        strLog += _T("VerifyGacDataWithSignature2 Failed!\r\n");
    }
    GetDlgItem(IDC_LOG_EDIT)->SetWindowText(strLog);
}

你可能感兴趣的:(OpenSSL)