📄 ubsrsa.c
字号:
ubsec_MemAddress_t PhysAddr; PacketIndex = pMCR->NumberOfPackets; pRSACtx = &pMCR->KeyContextList[PacketIndex]->CtxCmdBuf.Pri_RSA_CtxCmdBuf; pPacket = &(pMCR->PacketArray[PacketIndex]); /* Set up the current packet */ RTL_MemZero(pRSACtx,sizeof(*pRSACtx)); pRSACtx->q_length = (unsigned short)CPU_TO_CTRL_SHORT(pRSAParams->PrimeQ.KeyLength) ; pRSACtx->p_length = (unsigned short)CPU_TO_CTRL_SHORT(pRSAParams->PrimeP.KeyLength) ; /* * All parameters need to be aligned on the * same length so we use the length of the * largest. */ /* Both P & Q Must be normalized and aligned on the largest length. */ if ((pRSAParams->PrimeQ.KeyLength > pRSAParams->PrimeP.KeyLength)) ParamLen=pRSAParams->PrimeQ.KeyLength; else ParamLen=pRSAParams->PrimeP.KeyLength; if (ParamLen <=256) ParamLen=256; else if (ParamLen <= 384) ParamLen=384; else if (ParamLen <= 512) ParamLen=512; else#ifdef UBSEC_582x_CLASS_DEVICE if (ParamLen <= 768) ParamLen=768; else if (ParamLen <= 1024) ParamLen=1024; else#endif return(UBSEC_STATUS_INVALID_PARAMETER);#ifndef UBSEC_HW_NORMALIZE NormBits = ubsec_NormalizeDataTo(&pRSAParams->PrimeP,ParamLen); if (NormBits) ubsec_ShiftData(&pRSAParams->PrimeEdp, NormBits); NormBits = ubsec_NormalizeDataTo(&pRSAParams->PrimeQ,ParamLen); if (NormBits) { ubsec_ShiftData(&pRSAParams->PrimeEdq, NormBits); ubsec_ShiftData(&pRSAParams->Pinv, NormBits); } /* Return the number of bits the result will have to be shifted by. */ NormBits=((ParamLen*2) -(pRSAParams->PrimeQ.KeyLength+pRSAParams->PrimeP.KeyLength)); if (NormBits) { pRSAParams->OutputKeyInfo.KeyLength = pRSAParams->PrimeQ.KeyLength+pRSAParams->PrimeP.KeyLength; } pMCR->KeyContextList[PacketIndex]->NormBits=NormBits;#else NormBits=((ParamLen*2) -(pRSAParams->PrimeQ.KeyLength+pRSAParams->PrimeP.KeyLength)); pMCR->KeyContextList[PacketIndex]->NormBits=NormBits; pRSAParams->OutputKeyInfo.KeyLength=ParamLen*2;#endif pRSAParams->InputKeyInfo.KeyLength = 2*ParamLen; /* Pad it out.*/ pRSAParams->OutputKeyInfo.KeyLength=2*ParamLen; pMCR->KeyContextList[PacketIndex]->ResultKey[0] = &pRSAParams->OutputKeyInfo; /* Save here for post-command finishing */ pMCR->KeyContextList[PacketIndex]->ResultKey[1] = NULL; /* Not used */ pMCR->KeyContextList[PacketIndex]->ResultRNG = NULL; /* Not used */ ParamLen/=8; /* p setup */#ifndef UBSEC_HW_NORMALIZE #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[0], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->PrimeP.KeyValue), ParamLen/4); #else RTL_Memcpy( &pRSACtx->CtxParams[0],OS_GetVirtualAddress(pRSAParams->PrimeP.KeyValue), ParamLen); #endif /* UBS_ENABLE_KEY_SWAP */#else #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[0], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->PrimeP.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->PrimeP.KeyLength)/32); #else RTL_Memcpy( &pRSACtx->CtxParams[0],OS_GetVirtualAddress(pRSAParams->PrimeP.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->PrimeP.KeyLength)/8); #endif /* UBS_ENABLE_KEY_SWAP */#endif Offset=ParamLen; /* q setup */#ifndef UBSEC_HW_NORMALIZE #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[Offset/4], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->PrimeQ.KeyValue), ParamLen/4); #else RTL_Memcpy( &pRSACtx->CtxParams[Offset/4],OS_GetVirtualAddress(pRSAParams->PrimeQ.KeyValue), ParamLen); #endif /* UBS_ENABLE_KEY_SWAP */#else #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[Offset/4], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->PrimeQ.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->PrimeQ.KeyLength)/32); #else RTL_Memcpy( &pRSACtx->CtxParams[Offset/4],OS_GetVirtualAddress(pRSAParams->PrimeQ.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->PrimeQ.KeyLength)/8); #endif /* UBS_ENABLE_KEY_SWAP */#endif Offset+=ParamLen; /* dp setup */#ifndef UBSEC_HW_NORMALIZE #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[Offset/4], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->PrimeEdp.KeyValue), ParamLen/4); #else RTL_Memcpy( &pRSACtx->CtxParams[Offset/4],OS_GetVirtualAddress(pRSAParams->PrimeEdp.KeyValue), ParamLen); #endif /* UBS_ENABLE_KEY_SWAP */#else #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[Offset/4], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->PrimeEdp.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->PrimeEdp.KeyLength)/32); #else RTL_Memcpy( &pRSACtx->CtxParams[Offset/4],OS_GetVirtualAddress(pRSAParams->PrimeEdp.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->PrimeEdp.KeyLength)/8); #endif /* UBS_ENABLE_KEY_SWAP */#endif Offset+=ParamLen; /* dq setup */#ifndef UBSEC_HW_NORMALIZE #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[Offset/4], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->PrimeEdq.KeyValue), ParamLen/4); #else RTL_Memcpy( &pRSACtx->CtxParams[Offset/4],OS_GetVirtualAddress(pRSAParams->PrimeEdq.KeyValue), ParamLen); #endif /* UBS_ENABLE_KEY_SWAP */#else #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[Offset/4], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->PrimeEdq.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->PrimeEdq.KeyLength)/32); #else RTL_Memcpy( &pRSACtx->CtxParams[Offset/4],OS_GetVirtualAddress(pRSAParams->PrimeEdq.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->PrimeEdq.KeyLength)/8); #endif /* UBS_ENABLE_KEY_SWAP */#endif Offset+=ParamLen; /* pinv setup */#ifndef UBSEC_HW_NORMALIZE #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[Offset/4], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->Pinv.KeyValue), ParamLen/4); #else RTL_Memcpy( &pRSACtx->CtxParams[Offset/4],OS_GetVirtualAddress(pRSAParams->Pinv.KeyValue), ParamLen); #endif /* UBS_ENABLE_KEY_SWAP */#else #if defined(UBS_ENABLE_KEY_SWAP) copywords( (UBS_UINT32 *)&pRSACtx->CtxParams[Offset/4], (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->Pinv.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->Pinv.KeyLength)/32); #else RTL_Memcpy( &pRSACtx->CtxParams[Offset/4],OS_GetVirtualAddress(pRSAParams->Pinv.KeyValue), ROUNDUP_TO_32_BIT(pRSAParams->Pinv.KeyLength)/8); #endif /* UBS_ENABLE_KEY_SWAP */#endif Offset+=ParamLen; pMCR->KeyContextList[PacketIndex]->cmd_structure_length+=(Offset);#ifdef UBSDBG /* Print out the context information if required */ { int WordLen,i; WordLen=(pMCR->KeyContextList[PacketIndex]->cmd_structure_length-RSA_STATIC_PRIVATE_CONTEXT_SIZE)/4; Dbg_Print(DBG_RSAKEY,( "ubsec: ---- RSA Private P-Q Length [%d] P-D Length [%d] \n[",pRSACtx->q_length,pRSACtx->p_length)); Dbg_Print(DBG_RSAKEY,( "ubsec: ---- ParamLen %d Context Len %d Value -\n[", CTRL_TO_CPU_SHORT(ParamLen),pMCR->KeyContextList[PacketIndex]->cmd_structure_length )); for ( i=0 ; i < WordLen ; i++) { Dbg_Print(DBG_RSAKEY,( "%08x ",SYS_TO_BE_LONG(pRSACtx->CtxParams[i]))); } Dbg_Print(DBG_RSAKEY,( "]\n")); }#endif /* Input Buffer setup for RSA Private */ FragPtr=(DataBufChainList_pt)&pPacket->InputHead; PhysAddr=(ubsec_MemAddress_t)(OS_GetPhysicalAddress(pRSAParams->InputKeyInfo.KeyValue));#if defined(UBS_ENABLE_KEY_SWAP) longkey = (UBS_UINT32 *)OS_GetVirtualAddress(pRSAParams->InputKeyInfo.KeyValue); for (element = 0 ; element < ROUNDUP_TO_32_BIT(pRSAParams->InputKeyInfo.KeyLength)/32 ; element++) longkey[element] = BYTESWAPLONG(longkey[element]);#endif /* UBS_ENABLE_KEY_SWAP */ FragPtr->DataAddress = CPU_TO_CTRL_LONG( (UBS_UINT32)PhysAddr ); DataLength=(pRSAParams->InputKeyInfo.KeyLength+7)/8; FragPtr->DataLength = CPU_TO_CTRL_SHORT( (unsigned short)DataLength ); /* Copy (endian-adjusted) fragment length into MCR structure */ pPacket->PacketLength = FragPtr->DataLength; Dbg_Print(DBG_FRAG_SYNC,( "ubsec: RSA_SetupPrivateParams Sync InputKeyInfo Fragment to Device (0x%08X,%d,%d)\n", pRSAParams->InputKeyInfo.KeyValue, 0, DataLength)); OS_SyncToDevice(pRSAParams->InputKeyInfo.KeyValue, 0, DataLength); Dbg_Print(DBG_RSAKEY,( "RSA Private Key, InputKeyInfo: FragI <%d,%08x %08x>\n", DataLength, CTRL_TO_CPU_LONG( FragPtr->DataAddress ), FragPtr)); FragPtr->pNext = 0; /* Output Buffer setup for RSA Public */ FragPtr=(DataBufChainList_pt)&pPacket->OutputHead; PhysAddr=(ubsec_MemAddress_t)(OS_GetPhysicalAddress(pRSAParams->OutputKeyInfo.KeyValue)); FragPtr->DataAddress = CPU_TO_CTRL_LONG( (UBS_UINT32)PhysAddr ); DataLength=(pRSAParams->OutputKeyInfo.KeyLength+7)/8; FragPtr->DataLength = CPU_TO_CTRL_SHORT( (unsigned short)DataLength ); Dbg_Print(DBG_RSAKEY,( "RSA Private Key, OutputKeyInfo: FragO <%d,%08x %08x>\n", DataLength, CTRL_TO_CPU_LONG( FragPtr->DataAddress ), FragPtr)); FragPtr->pNext = 0; pRSAParams->OutputKeyInfo.KeyLength-=NormBits; return(UBSEC_STATUS_SUCCESS);#else return(UBSEC_STATUS_NO_DEVICE);#endif}#endif /* UBSEC_PKEY_SUPPORT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -