Example #1
0
/*****************************************************************************
* 函 数 名  : crypto_rsa_decrypt
*
* 功能描述  : 使用保存在NV中的改制用RSA公钥,对输入的数据解密。
*
* 输入参数  : cipher_data: 待解密数据。
*             cipher_len:  待解密数据长度。(byte)
*             rsa_key:     RSA公钥buffer指针
*             rsa_len:     RSA公钥buffer长度。(byte)
*             len:         解密后的数据的存放buffer的buffer size。(byte)(没有检查)
*
* 输出参数  : data:        解密后的数据的存放buffer。
*             len:         解密后的数据的实际长度。(byte)
*
* 返 回 值  : BSP_OK:      解密成功。
*             BSP_ERROR:   解密失败。
*
* 其它说明  : len为输入/输出参数,传入的len变量所用内存必须可写回。
*             所以避免直接传入类似sizeof()的函数调用结果。
*
*****************************************************************************/
int crypto_rsa_decrypt_o (char *cipher_data, int cipher_len, char *rsa_key, int rsa_klen, char *data, int *len)
{
    //rsa_public_key* p_rsa_key = (rsa_public_key*)rsa_key;/*模A*/
    rsa_public_key* p_rsa_key = NULL;

    if(cipher_data == NULL || rsa_key == NULL || data == NULL || len == NULL)
    {
        security_print("ERROR crypto_rsa_decrypt: param is NULL pointer!\n");
        return BSP_ERROR;
    }

    if(cipher_len <=0 || rsa_klen != RSA_KEY_LEN)
    {
        security_print("ERROR crypto_rsa_decrypt: param is invalid!\n");
        return BSP_ERROR;
    }

    p_rsa_key = (rsa_public_key*)(rsa_key+rsa_klen/2);/*模B*/

    if(0 != rsa_public_decrypt((UINT8*)cipher_data,cipher_len,(UINT8*)data,(UINT16*)len,p_rsa_key))
    {
        security_print("ERROR crypto_rsa_decrypt: rsa_public_decrypt failed!\n");
        return BSP_ERROR;
    }

    return BSP_OK;
}
Example #2
0
kal_bool che_sw_rsa(STCHE* che_context, CHE_ACTION act, kal_uint8* data_src, kal_uint8* data_dst, kal_int32 length, kal_bool last_block){
	rsa_context rsa;
    memset( &rsa, 0, sizeof( rsa ) );
    rsa.len = length;
    mpi_read( &rsa.N , che_context->modulusN, 16, che_context->modulusNLen );
    mpi_read( &rsa.E , che_context->pubExp, 16, che_context->pubExpLen );
    mpi_read( &rsa.D , che_context->privExpD, 16, che_context->privExpDLen );
    mpi_read( &rsa.P , che_context->primeP, 16, che_context->primePLen );
    mpi_read( &rsa.Q , che_context->primeQ, 16,che_context->primeQLen );
    mpi_read( &rsa.DP, che_context->dModPm1, 16,che_context->dModPm1Len );
    mpi_read( &rsa.DQ, che_context->dModQm1, 16,che_context->dModQm1Len );
    mpi_read( &rsa.QP, che_context->qInvModP, 16,che_context->qInvModPLen );
	if( rsa_check_pubkey(  &rsa ) != 0 || rsa_check_privkey( &rsa ) != 0 ){
	    ASSERT(0);
	}
	switch (act){
       case RSA_PUBLIC_ENC: 
         if( rsa_public_encrypt( &rsa, data_src, length, data_dst, length ) != 0 ){
             ASSERT(0);
		     }
       break;
       case RSA_PUBLIC_DEC: 
         if( rsa_public_decrypt( &rsa, data_src, length, data_dst, length ) != 0 ){
             ASSERT(0);
		 }
       break;
       case RSA_PRIVATE_ENC: 
         if( rsa_private_encrypt( &rsa, data_src, length, data_dst, length ) != 0 ){
             ASSERT(0);
		 }
       break;
	   case RSA_PRIVATE_DEC: 
         if( rsa_private_decrypt( &rsa, data_src, length, data_dst, length ) != 0 ){
             ASSERT(0);
		 }
	   break;
       default:
         return KAL_FALSE;
    }
    return KAL_TRUE;
}