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

📄 sec2_diag.c

📁 freescale ppc sec2加解密单元驱动
💻 C
📖 第 1 页 / 共 4 页
字号:
    cbc->iv = (UINT8*)malloc(cbc->ivlen);
    if (cbc->iv == NULL)
    {
        printf("sec2_test_aes: failed to malloc iv!\n");
        free(cbc->inData);
        free(cbc->key);
        free(cbc);
        return SEC2_ERROR;
    }
    memcpy(cbc->iv, pt3, cbc->ivlen);                             /*初始化向量赋值*/

    cbc->outLen = 128;
    cbc->outData = (UINT8*)malloc(cbc->outLen);                    /*分配输出内存*/
    if (cbc->outData == NULL)                                     /*如果分配失败则释放*/
    {
        printf("sec2_test_aes: failed to malloc outData!\n");
        free(cbc->inData);
        free(cbc->key);
        free(cbc->iv);
        free(cbc);
        return SEC2_ERROR;
    }
    memset(cbc->outData, 0, 128);

    return SEC2_SUCCESS;
}


int sec2_diag_aes_ctrl(void)
{
    algo_msg_crypt_t *cbc;
    int test_cnt;
    
    for(test_cnt = 0;test_cnt < 10;test_cnt++)
    {
        cbc = (algo_msg_crypt_t*)malloc(sizeof(algo_msg_crypt_t));
        if(NULL == cbc)
        {
             return SEC2_ERROR;
        }
        
        sec2_diag_aes(cbc);

        if(SEC2_SUCCESS == sec2_test_aes_encrypt(cbc))
        {
           diag_sec2->sec2_aes_enc_succ_cnt++;
        }
        else
        {
            if(NULL != cbc->outData)
            {
                free(cbc->outData);
            }
            if(NULL != cbc->inData)
            {
                free(cbc->inData);
            }
            if(NULL != cbc->key)
            {
                free(cbc->key);
            }
            if(NULL != cbc->iv)
            {
                free(cbc->iv);
            }
            if(NULL != cbc)
            {
                free(cbc);
            }
        }
        taskDelay(1);
    }

    return SEC2_SUCCESS;    
}

int sec2_test_hmac(DIAG_SEC2_HMAC choose,algo_msg_hmac_t *hmac)
{
    UINT32  dpdReqLen;       /* 描述符字节长度 */
    int  ipsec_status  = SEC2_ERROR;    /* 返回状态*/
    
    DRV_SEC2_CONTEXT  *ctx     = (DRV_SEC2_CONTEXT *)NULL;  /* 上下文结构 */ 
    HMAC_PAD_REQ *padReq       = (HMAC_PAD_REQ *)NULL;    /* SEC2的 HMAC pad 请求结构 */ 
    
    /**************** 分配上下文内存 ****************/
    ctx = (DRV_SEC2_CONTEXT*)SEC2_GETBUF(sizeof(DRV_SEC2_CONTEXT));
    if (NULL == ctx)
    {
        DRV_IPSEC_DBG("drv_sec2_hmac_auth:  malloc DRV_SEC2_CONTEXT failed!\n");
        return SEC2_ERROR;
    }

    /**************** 分配描述符内存 ****************/
    dpdReqLen = sizeof(HMAC_PAD_REQ);
    ctx->dpdReqMem= (UINT8*)SEC2_GETBUF(dpdReqLen);
    if (NULL == ctx->dpdReqMem)
    {
        DRV_IPSEC_DBG("drv_sec2_hmac_auth:  malloc ctx->dpdReqMem failed!\n");
        drv_sec2_free_context(ctx);
        return SEC2_ERROR;
    }
    padReq = (HMAC_PAD_REQ*) (ctx->dpdReqMem);

    /**************** 描述符填充: HMAC_IPAD_REQ  ****************/
    if(choose == DIAG_SEC2_HMAC_MD5)
    {
        padReq->opId  = DPD_MD5_LDCTX_HMAC_PAD_ULCTX;
    }
    else
    {
        padReq->opId  = DPD_SHA_LDCTX_HMAC_PAD_ULCTX;
    }
    padReq->channel = 0;
    padReq->notify  = (PSEC_NOTIFY_ROUTINE)NULL; 
    padReq->pNotifyCtx      = NULL;
    padReq->notify_on_error = (PSEC_NOTIFY_ON_ERROR_ROUTINE)NULL; 
    padReq->ctxNotifyOnErr.request = NULL;
    padReq->status   = 0;
    padReq->nextReq  = NULL;
    padReq->keyBytes = (unsigned long)hmac->keyLen;
    padReq->keyData  = (unsigned char*)hmac->key;
    padReq->inBytes  = (unsigned long)hmac->inLen;
    padReq->inData   = (unsigned char*)hmac->inData;
    padReq->outBytes = (unsigned long)hmac->outLen;
    padReq->outData  = ipsec_onlytest;

    /**************** 下发硬件处理 ****************/
    ipsec_status = (UINT32)SEC2_ioctl(IOCTL_PROC_REQ, padReq); 
    if (SEC2_SUCCESS != ipsec_status)
    {
       DRV_IPSEC_DBG("drv_sec2_hmac_auth: Ioctl failed!\n");
       drv_sec2_free_context(ctx);
       return SEC2_ERROR;
    }
    /*调用软件算法*/
    if(choose == DIAG_SEC2_HMAC_MD5)
    { 
        crypto_hmac_md5(hmac);
        taskDelay(1);
        
        if(0 == memcmp(hmac->outData,ipsec_onlytest,hmac->outLen))
        {
            diag_sec2->sec2_hmac_md5_succ_cnt++;
        }
    }
    else
    {
        crypto_hmac_sha1(hmac);
        taskDelay(1);
        if(0 == memcmp(hmac->outData,ipsec_onlytest,hmac->outLen))
        {
            diag_sec2->sec2_hmac_sha_succ_cnt++;
        }
    }
    
    /*释放ctx*/
    drv_sec2_free_context(ctx);
    
    if(NULL != hmac->inData)
    {
        SEC2_FREEBUF(hmac->inData);
    }
    if(NULL != hmac->outData)
    {
        SEC2_FREEBUF(hmac->outData);
    }
    if(NULL != hmac->key)
    {
        SEC2_FREEBUF(hmac->key);
    }
    if(NULL != hmac)
    {
        SEC2_FREEBUF(hmac);
    }

    return SEC2_SUCCESS;
}


int sec2_diag_hmac(DIAG_SEC2_HMAC choose,algo_msg_hmac_t *hmac)
{
    UINT8 pt3[20] = "01234567890123456789"; 
    
    hmac->inLen = 128;                                                      /*输入数据长度*/
    hmac->inData = (UINT8*)SEC2_GETBUF(hmac->inLen);                                 /*分配输入内存*/
    if (hmac->inData == NULL)
    {
        printf("sec2_test_hmac: failed to malloc inData!\n");
        free(hmac);
        return SEC2_ERROR;
    }
    memset(hmac->inData, 0x67, hmac->inLen);

    if(choose == DIAG_SEC2_HMAC_MD5)
    {
        hmac->keyLen = 16;
        hmac->key = (UINT8*)SEC2_GETBUF(hmac->keyLen); 
        if (hmac->key == NULL)
        {
            printf("sec2_test_hmac: failed to malloc key!\n");
            free(hmac->inData);
            free(hmac);
            return SEC2_ERROR;
        }
    }
    else
    {
        hmac->keyLen = 20;
        hmac->key = (UINT8*)SEC2_GETBUF(hmac->keyLen);  
        if (hmac->key == NULL)
        {
            printf("sec2_test_hmac: failed to malloc key!\n");
            free(hmac->inData);
            free(hmac);
            return SEC2_ERROR;
        }
    }
    memcpy(hmac->key, pt3, hmac->keyLen);          

    if(choose == DIAG_SEC2_HMAC_MD5)
    {
        hmac->outLen = 16;
        hmac->outData = (UINT8*)SEC2_GETBUF(hmac->outLen);
        if (hmac->outData == NULL)
        {
            printf("sec2_test_hmac: failed to malloc outData!\n");
            free(hmac->inData);
            free(hmac->key);
            free(hmac);
            return SEC2_ERROR;
        }
    }
    else
    {
        hmac->outLen = 20;
        hmac->outData = (UINT8*)SEC2_GETBUF(hmac->outLen);
        if (hmac->outData == NULL)
        {
            printf("sec2_test_hmac: failed to malloc outData!\n");
            free(hmac->inData);
            free(hmac->key);
            free(hmac);
            return SEC2_ERROR;
        }      
    }
    memset(hmac->outData, 0, hmac->outLen);
    
    return SEC2_SUCCESS;
}

int sec2_diag_hmac_ctrl(DIAG_SEC2_HMAC choose)
{
    algo_msg_hmac_t *hmac;
    int test_cnt;
    
    for(test_cnt = 0;test_cnt < 10;test_cnt++)
    {
        hmac = (algo_msg_hmac_t*)SEC2_GETBUF(sizeof(algo_msg_hmac_t));
        if(NULL == hmac)
        {
             return SEC2_ERROR;
        }
        
        sec2_diag_hmac(choose,hmac);

        if(SEC2_SUCCESS == sec2_test_hmac(choose,hmac))
        {
            /*do nothing*/
        }
        else
        {
            if(NULL != hmac->outData)
            {
                SEC2_FREEBUF(hmac->outData);
            }
            if(NULL != hmac->inData)
            {
                SEC2_FREEBUF(hmac->inData);
            }
            if(NULL != hmac->key)
            {
                SEC2_FREEBUF(hmac->key);
            }
            if(NULL != hmac)
            {
                SEC2_FREEBUF(hmac);
            }
        }
        taskDelay(1);
    }

    return SEC2_SUCCESS;
    
}


int sec2_diag_algo(void)
{
    if(SEC2_SUCCESS != sec2_diag_des_ctrl())
    {
        printf("sec2_test_des error!\n");
        return SEC2_ERROR;
    }
    
    if(SEC2_SUCCESS != sec2_diag_3des_ctrl())
    {
        printf("sec2_test_3des error!\n");
        return SEC2_ERROR;
    }

    if(SEC2_SUCCESS != sec2_diag_aes_ctrl())
    {
        printf("sec2_test_aes error!\n");
        return SEC2_ERROR;
    }

    if(SEC2_SUCCESS != sec2_diag_hmac_ctrl(DIAG_SEC2_HMAC_MD5))
    {
        printf("sec2_test_md5 error!\n");
        return SEC2_ERROR;
    }

    if(SEC2_SUCCESS != sec2_diag_hmac_ctrl(DIAG_SEC2_HMAC_SHA))
    {
        printf("sec2_test_sha error!\n");
        return SEC2_ERROR;
    }

    return SEC2_SUCCESS;
}

int drv_Sec2Diaginternal(char *buffer)
{
    diag_sec2 = (diag_sec2_internal*)malloc(sizeof(diag_sec2_internal));
    if((diag_sec2_internal*)NULL == diag_sec2)
    {
        return SEC2_ERROR;
    }
    else
    {
        memset((void*)diag_sec2,0,sizeof(diag_sec2_internal));
    }

    sec2_diag_algo();
    sprintf( buffer,"\n======\tSec2 Core diag internal begin\t ======\n");
    sprintf((buffer+strlen(buffer)),"==test sec2 des 10 times==\n");
    sprintf((buffer+strlen(buffer)),"encrypto success:%d\n",(int)diag_sec2->sec2_des_enc_succ_cnt);
    sprintf((buffer+strlen(buffer)),"decrypto success:%d\n\n",(int)diag_sec2->sec2_des_dec_succ_cnt);

    sprintf((buffer+strlen(buffer)),"==test sec2 3des 10 times==\n");
    sprintf((buffer+strlen(buffer)),"encrypto success:%d\n",(int)diag_sec2->sec2_3des_enc_succ_cnt);
    sprintf((buffer+strlen(buffer)),"decrypto success:%d\n\n",(int)diag_sec2->sec2_3des_dec_succ_cnt);

    sprintf((buffer+strlen(buffer)),"==test sec2 aes 10 times==\n");
    sprintf((buffer+strlen(buffer)),"encrypto success:%d\n",(int)diag_sec2->sec2_aes_enc_succ_cnt);
    sprintf((buffer+strlen(buffer)),"decrypto success:%d\n\n",(int)diag_sec2->sec2_aes_dec_succ_cnt);

    sprintf((buffer+strlen(buffer)),"==test sec2 md5 10 times==\n");
    sprintf((buffer+strlen(buffer)),"hmac-md5 success:%d\n\n",(int)diag_sec2->sec2_hmac_md5_succ_cnt);

    sprintf((buffer+strlen(buffer)),"==test sec2 sha 10 times==\n");
    sprintf((buffer+strlen(buffer)),"hmac-sha success:%d\n\n",(int)diag_sec2->sec2_hmac_sha_succ_cnt);

    /*测试完毕,释放内存*/
    if(NULL != diag_sec2)
    {
        free((void*)diag_sec2);
    }
    sprintf((buffer+strlen(buffer)),"\n======\tSec2 Core diag internal end\t ======\n");

    return SEC2_SUCCESS;
}

⌨️ 快捷键说明

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