📄 ubsmath.c
字号:
/**************************************************/ /* ParamA setup (all ops). Input buffer fragment. */ /**************************************************/ /* Now do the Input parameter A (1st input buffer). All math ops have one. */ FragPtr=(DataBufChainList_pt)&pPacket->InputHead; PhysAddr=(ubsec_MemAddress_t)(OS_GetPhysicalAddress(pParams->ParamA.KeyValue)); FragPtr->DataAddress = CPU_TO_CTRL_LONG( (UBS_UINT32)PhysAddr );#if defined(UBS_ENABLE_KEY_SWAP) longkey = (UBS_UINT32 *)OS_GetVirtualAddress(pParams->ParamA.KeyValue); for (element = 0 ; element < ROUNDUP_TO_32_BIT(pParams->ParamA.KeyLength)/32 ; element++) longkey[element] = BYTESWAPLONG(longkey[element]);#endif /* UBS_ENABLE_KEY_SWAP */ DataLength=NormalizeLen/8; FragPtr->DataLength = CPU_TO_CTRL_SHORT( (unsigned short)DataLength ); Dbg_Print(DBG_FRAG_SYNC,( "ubsec: ubsec_MathCommand Sync ParamA Fragment to Device (0x%08X,%d,%d)\n", pParams->ParamA.KeyValue, 0, DataLength)); OS_SyncToDevice(pParams->ParamA.KeyValue, 0, DataLength); Dbg_Print(DBG_MATH,( "Input Param 1: <%d,%08x (%08x)>\n", DataLength, CTRL_TO_CPU_LONG( FragPtr->DataAddress ), FragPtr)); /* End ParamA input fragment setup */ /****************************************************************/ /* ParamB setup (all ops except MODREM). Input buffer fragment. */ /****************************************************************/ if (pCommand->Command != UBSEC_MATH_MODREM) {#ifndef UBSEC_HW_NORMALIZE ubsec_ShiftData(&pParams->ParamB, NrmBits);#endif /* get the next fragment pointer */ NextFragPtr=&pMCR->InputFragmentList[PacketIndex*(UBSEC_MAX_FRAGMENTS)]; FragPtr->pNext =NextFragPtr->PhysicalAddress; FragPtr=NextFragPtr; /* The second input data buffer has 2nd math parameter (B or E) */ PhysAddr=(ubsec_MemAddress_t)(OS_GetPhysicalAddress(pParams->ParamB.KeyValue)); FragPtr->DataAddress = CPU_TO_CTRL_LONG( (UBS_UINT32)PhysAddr );#if defined(UBS_ENABLE_KEY_SWAP) longkey = (UBS_UINT32 *)OS_GetVirtualAddress(pParams->ParamB.KeyValue); for (element = 0 ; element < ROUNDUP_TO_32_BIT(pParams->ParamB.KeyLength)/32 ; element++) longkey[element] = BYTESWAPLONG(longkey[element]);#endif /* UBS_ENABLE_KEY_SWAP */#if 0 DataLength=ROUNDUP_TO_32_BIT(pParams->ParamB.KeyLength+NrmBits);#else if (pCommand->Command==UBSEC_MATH_MODEXP) { /* Exponent parameter fragment sizes need to be byte multiples only */ DataLength = (pParams->ParamB.KeyLength + 7)/8; } else { /* Non-exponent parameter fragment sizes need to be equal to the "key size" (in bytes) */ DataLength = NormalizeLen/8; }#endif FragPtr->DataLength = CPU_TO_CTRL_SHORT( (unsigned short)DataLength ); Dbg_Print(DBG_FRAG_SYNC,( "ubsec: ubsec_MathCommand Sync ParamB Fragment to Device (0x%08X,%d,%d)\n", pParams->ParamB.KeyValue, 0, DataLength)); OS_SyncToDevice(pParams->ParamB.KeyValue, 0, DataLength); Dbg_Print(DBG_MATH,( "Input Param 2: <%d,%08x (%08x)>\n", DataLength, CTRL_TO_CPU_LONG( FragPtr->DataAddress ), FragPtr)); if (pCommand->Command != UBSEC_MATH_DBLMODEXP) { /* No more input fragments */ FragPtr->pNext=0; /* Terminate the input fragment descriptor list */#ifndef STATIC_F_LIST /* Sync single fragment descriptor (external to MCR) */ FragPtr->pNext=0; /* Terminate the input fragment descriptor list before syncing. */ Dbg_Print(DBG_FRAG_SYNC,( "ubsec: ubsec_MathCommand Sync IFrag Descriptor to Device (0x%08X,%d,%d)\n", pMCR->InputFragmentListHandle, PacketIndex*(UBSEC_MAX_FRAGMENTS)*sizeof(DataBufChainList_t), sizeof(DataBufChainList_t))); OS_SyncToDevice(pMCR->InputFragmentListHandle, PacketIndex*(UBSEC_MAX_FRAGMENTS)*sizeof(DataBufChainList_t), sizeof(DataBufChainList_t));#endif } } /* End ParamB input fragment setup */ if (pCommand->Command == UBSEC_MATH_DBLMODEXP) { /*********************************************************/ /* ParamC setup (DBLMODEXP only). Input buffer fragment. */ /*********************************************************/ /* get the next fragment pointer */ NextFragPtr=&FragPtr[1]; FragPtr->pNext =NextFragPtr->PhysicalAddress; FragPtr=NextFragPtr; /* The third input data buffer has 2nd DBLMODEXP base (A1) */ PhysAddr=(ubsec_MemAddress_t)(OS_GetPhysicalAddress(pParams->ParamC.KeyValue)); FragPtr->DataAddress = CPU_TO_CTRL_LONG( (UBS_UINT32)PhysAddr );#if defined(UBS_ENABLE_KEY_SWAP) longkey = (UBS_UINT32 *)OS_GetVirtualAddress(pParams->ParamC.KeyValue); for (element = 0 ; element < ROUNDUP_TO_32_BIT(pParams->ParamC.KeyLength)/32 ; element++) longkey[element] = BYTESWAPLONG(longkey[element]);#endif /* UBS_ENABLE_KEY_SWAP */ DataLength = NormalizeLen/8; FragPtr->DataLength = CPU_TO_CTRL_SHORT( (unsigned short)DataLength ); Dbg_Print(DBG_FRAG_SYNC,( "ubsec: ubsec_MathCommand Sync ParamC Fragment to Device (0x%08X,%d,%d)\n", pParams->ParamC.KeyValue, 0, DataLength)); OS_SyncToDevice(pParams->ParamC.KeyValue, 0, DataLength); Dbg_Print(DBG_MATH,( "Input Param 3: <%d,%08x (%08x)>\n", DataLength, CTRL_TO_CPU_LONG( FragPtr->DataAddress ), FragPtr)); /* End ParamC input fragment setup */ /*********************************************************/ /* ParamD setup (DBLMODEXP only). Input buffer fragment. */ /*********************************************************/#ifndef UBSEC_HW_NORMALIZE ubsec_ShiftData(&pParams->ParamD, NrmBits);#endif /* get the next fragment pointer */ NextFragPtr=&FragPtr[1]; FragPtr->pNext =NextFragPtr->PhysicalAddress; FragPtr=NextFragPtr; /* The fourth input data buffer has 2nd DBLMODEXP exponent (E1) */ PhysAddr=(ubsec_MemAddress_t)(OS_GetPhysicalAddress(pParams->ParamD.KeyValue)); FragPtr->DataAddress = CPU_TO_CTRL_LONG( (UBS_UINT32)PhysAddr );#if defined(UBS_ENABLE_KEY_SWAP) longkey = (UBS_UINT32 *)OS_GetVirtualAddress(pParams->ParamD.KeyValue); for (element = 0 ; element < ROUNDUP_TO_32_BIT(pParams->ParamD.KeyLength)/32 ; element++) longkey[element] = BYTESWAPLONG(longkey[element]);#endif /* UBS_ENABLE_KEY_SWAP */ DataLength = NormalizeLen/8; FragPtr->DataLength = CPU_TO_CTRL_SHORT( (unsigned short)DataLength ); Dbg_Print(DBG_FRAG_SYNC,( "ubsec: ubsec_MathCommand Sync ParamD Fragment to Device (0x%08X,%d,%d)\n", pParams->ParamD.KeyValue, 0, DataLength)); OS_SyncToDevice(pParams->ParamD.KeyValue, 0, DataLength); Dbg_Print(DBG_MATH,( "Input Param 4: <%d,%08x (%08x)>\n", DataLength, CTRL_TO_CPU_LONG( FragPtr->DataAddress ), FragPtr)); /* End ParamD input fragment setup */ FragPtr->pNext=0; /* Terminate the input fragment descriptor list. */#ifndef STATIC_F_LIST /* sync the three fragment descriptors for ParamB, ParamC and ParamD */ Dbg_Print(DBG_FRAG_SYNC,( "ubsec: ubsec_MathCommand Sync IFrag Descriptors to Device (0x%08X,%d,%d)\n", pMCR->InputFragmentListHandle, PacketIndex*(UBSEC_MAX_FRAGMENTS)*sizeof(DataBufChainList_t), 3*sizeof(DataBufChainList_t))); OS_SyncToDevice(pMCR->InputFragmentListHandle, PacketIndex*(UBSEC_MAX_FRAGMENTS)*sizeof(DataBufChainList_t), 3*sizeof(DataBufChainList_t));#endif } /* end ParamC and ParamD setup (DBLMODEXP only) */ /* (At this point, the input fragment descriptor list has been terminated already) */ /* Now do the Output data buffer. All operations have at least one */ FragPtr=(DataBufChainList_pt)&pPacket->OutputHead; PhysAddr=(ubsec_MemAddress_t)(OS_GetPhysicalAddress(pParams->Result.KeyValue)); FragPtr->DataAddress = CPU_TO_CTRL_LONG( (UBS_UINT32)PhysAddr ); DataLength=NormalizeLen/8; FragPtr->DataLength = CPU_TO_CTRL_SHORT( (unsigned short)DataLength );#ifdef UBSDBG /* Sanity check debug info for conditions that will hang the chip. */ if ( (CTRL_TO_CPU_LONG( FragPtr->DataAddress )) & 0x03) { Dbg_Print(DBG_FATAL,("ubsec:MATH #########INVALID OUTPUT ADDRESS %08x\n", CTRL_TO_CPU_LONG(FragPtr->DataAddress))); Status=UBSEC_STATUS_INVALID_PARAMETER; goto Error_Return; } if ((DataLength) & 0x03) { Dbg_Print(DBG_FATAL,("ubsec:MATH #########INVALID OUTPUT LENGTH %08x\n", DataLength)); Status=UBSEC_STATUS_INVALID_PARAMETER; goto Error_Return; }#endif if (pCommand->Command == UBSEC_MATH_DBLMODEXP) { NextFragPtr=&pMCR->OutputFragmentList[PacketIndex*(UBSEC_MAX_FRAGMENTS)]; FragPtr->pNext =NextFragPtr->PhysicalAddress; FragPtr =NextFragPtr; PhysAddr=(ubsec_MemAddress_t)(OS_GetPhysicalAddress(pParams->Result2.KeyValue)); FragPtr->DataAddress = CPU_TO_CTRL_LONG( (UBS_UINT32)PhysAddr ); DataLength=NormalizeLen/8; FragPtr->DataLength = CPU_TO_CTRL_SHORT( (unsigned short)DataLength );#ifndef STATIC_F_LIST FragPtr->pNext=0; /* Terminate the output fragment descriptor list before syncing */ Dbg_Print(DBG_FRAG_SYNC,( "ubsec: ubsec_MathCommand Sync 2nd OFrag Descriptor to Device (0x%08X,%d,%d)\n", pMCR->OutputFragmentListHandle, PacketIndex*(UBSEC_MAX_FRAGMENTS)*sizeof(DataBufChainList_t), sizeof(DataBufChainList_t))); OS_SyncToDevice(pMCR->OutputFragmentListHandle, PacketIndex*(UBSEC_MAX_FRAGMENTS)*sizeof(DataBufChainList_t), sizeof(DataBufChainList_t));#endif#ifdef UBSDBG /* Sanity check debug info for conditions that will hang the chip. */ if ( (CTRL_TO_CPU_LONG( FragPtr->DataAddress )) & 0x03) { Dbg_Print(DBG_FATAL,("ubsec:MATH DBLMODEXP#########INVALID OUTPUT ADDRESS %08x\n", CTRL_TO_CPU_LONG(FragPtr->DataAddress))); Status=UBSEC_STATUS_INVALID_PARAMETER; goto Error_Return; } if ((DataLength) & 0x03) { Dbg_Print(DBG_FATAL,("ubsec:MATH DBLMODEXP#########INVALID OUTPUT LENGTH %08x\n", DataLength)); Status=UBSEC_STATUS_INVALID_PARAMETER; goto Error_Return; }#endif } /* end second output buffer setup (DBLMODEXP only) */ FragPtr->pNext=0; /* Terminate the output fragment descriptor list (if not already done). */ Dbg_Print(DBG_MATH,( "Result : <%d,%08x (%08x)>\n", DataLength, CTRL_TO_CPU_LONG( FragPtr->DataAddress ), FragPtr)); /* Save the user callback information. We use an intermediate callback for LongKey numbers */ /* Always save the user callback parameters (to be called by the intermediate callback) */ pContext->UserCallback = pCommand->CompletionCallback; pContext->UserContext = pCommand->CommandContext; /* The intermediate callback needs to get this command context passed to it */ pCompletionContext->CompletionCallback = KeyFinishResult; pCompletionContext->CommandContext = (unsigned long)pContext;#if (UBS_CPU_ATTRIBUTE != UBS_CRYPTONET_ATTRIBUTE) pContext->cmd_structure_length= CPU_TO_CTRL_SHORT(pContext->cmd_structure_length);#endif /* For key (math) MCRs, contexts are accessed by an array of handles. */ /* This means that memory for each context was separately allocated. */ /* Therefore we must sync each context separately as it is built. */ Dbg_Print(DBG_CNTXT_SYNC,( "ubsec: ubsec_MathCommand Sync Context to Device (0x%08X,%d,%d)\n", pMCR->ContextListHandle[PacketIndex], 0, CTRL_TO_CPU_SHORT(pContext->cmd_structure_length))); OS_SyncToDevice(pMCR->ContextListHandle[PacketIndex], 0, CTRL_TO_CPU_SHORT(pContext->cmd_structure_length)); /* Now inc the number of packets and prepare for the next command. */ pMCR->NumberOfPackets++; pCommand++; PacketIndex++; pPacket++; pContext++; } /* For NumCommands-- */ /* * If we are here then the MCR is built. * Push it to the device. */ *NumCommands=CommandIndex; /* Update number completed */ PushMCR(pDevice);#ifdef BLOCK /* Wait for all outstanding to complete */ while ((Status=WaitForCompletion(pDevice,(unsigned long)1000000,UBSEC_KEY_LIST)) == UBSEC_STATUS_SUCCESS); if (Status!=UBSEC_STATUS_TIMEOUT) /* We are nested, return success */ Status=UBSEC_STATUS_SUCCESS; Error_Return:#else Error_Return: /* Label to make sure that IRQs are enabled. */#ifdef COMPLETE_ON_COMMAND_THREAD ubsec_PollDevice(pDevice); /* Try to complete some & cut down on ints */#endif#endif OS_LeaveCriticalSection(pDevice,SaveConfig); return(Status);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -