78 #include <adi_processor.h> 83 #include <drivers/crypto/adi_crypto.h> 86 #include "adi_crypto_def.h" 88 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 90 #include <drivers/dma/adi_dma.h> 108 #pragma diag_suppress=Pm123,Pm140,Pm050,Pm088,Pm073,Pm143,Pm152 112 #define CLR_BITS(REG,BITS) ((REG) &= ~(BITS)) 113 #define SET_BITS(REG,BITS) ((REG) |= (BITS)) 114 #define IS_ANY_BIT_SET(REG,BITS) (((REG) & (BITS)) != 0u) 118 #define NUM_DEVICES (1u) 124 #if defined ( __ICCARM__ ) 125 #define __ADI_BYTE_SWAP(X) __REV(X) 126 #elif defined (__GNUC__) 127 #define __ADI_BYTE_SWAP(X) __builtin_bswap32(X) 128 #elif defined (__CC_ARM) 129 #define __ADI_BYTE_SWAP(X) __rev(X) 131 #error "This toolchain is not supported" 137 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 138 static void dmaCallback (
void *pCBParam, uint32_t Event,
void *pArg);
148 static uint32_t u32FromU8p (uint8_t *
const pFourBytes);
153 #if defined (__ADUCM4x50__) 154 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 171 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 176 static void writePioInputData (
ADI_CRYPTO_HANDLE const hDevice, uint32_t
const status);
179 static void readPioOutputData (
ADI_CRYPTO_HANDLE const hDevice, uint32_t
const status);
186 void Crypto_Int_Handler(
void);
187 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 188 void DMA_AES0_IN_Int_Handler (
void);
189 void DMA_AES0_OUT_Int_Handler (
void);
196 static CRYPTO_INFO CryptoDevInfo[] = {
199 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 215 #if (defined (__ADUCM4x50__) && (1 == ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT)) 216 #include "adi_pkstor.c" 220 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 224 static void dmaCallback(
void *pCBParam, uint32_t Event,
void *pArg)
230 uint32_t failingChannel = (uint32_t)pCBParam;
249 if (0u != hDevice->pfCallback) {
250 hDevice->pfCallback (hDevice->pCBParam, (uint32_t)hDevice->dmaErrorCode, (
void*)failingChannel);
254 StopCompute(hDevice);
266 for (x = 0u; x < NUM_DEVICES; x++) {
267 if (CryptoDevInfo[x].hDevice == hDevice) {
297 if ( (0u != (3u & (uint32_t)pBuffer->
pAuthData)) || (0u != (3u & (uint32_t)pBuffer->
pInputData)) || (0u != (3u & (uint32_t)pBuffer->
pOutputData)) ) {
301 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 306 if ( (MAX_CRYPTO_DMA_BYTES < pBuffer->numAuthBytes)
307 || (MAX_CRYPTO_DMA_BYTES < pBuffer->numOutputBytes)
308 || ( (MAX_CRYPTO_DMA_BYTES < pBuffer->numInputBytes)
309 #
if defined (__ADUCM4x50__)
320 #if ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1 330 #if defined (__ADUCM4x50__) 331 #if ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1 344 #if ADI_CRYPTO_ENABLE_CMAC_SUPPORT == 1 364 #if ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1 367 if ((pBuffer->
CounterInit & (0xFFF00000u)) != 0u) {
373 #if ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1 376 if ( ((pBuffer->
CounterInit & (0xFFFF0000u)) != 0u)
431 if (nDeviceNum >= NUM_DEVICES) {
435 if ((pMemory == NULL) || (phDevice == NULL)) {
443 if (CryptoDevInfo[nDeviceNum].hDevice != NULL) {
460 hDevice->pDev = CryptoDevInfo[nDeviceNum].pDev;
463 hDevice->pDevInfo = CryptoDevInfo;
466 CryptoDevInfo[nDeviceNum].hDevice = hDevice;
469 InitializeDevData(hDevice);
474 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 523 if (hDevice->bDeviceEnabled) {
534 for (x=0u; x < NUM_DEVICES; x++) {
535 if (CryptoDevInfo[x].hDevice == hDevice) {
536 CryptoDevInfo[x].hDevice = NULL;
574 ADI_INT_STATUS_ALLOC();
575 ADI_ENTER_CRITICAL_REGION();
576 hDevice->pfCallback = pfCallback;
577 hDevice->pCBParam = pCBParam;
578 ADI_EXIT_CRITICAL_REGION();
621 if (NULL != hDevice->pUserBuffer) {
638 hDevice->pUserBuffer = pBuffer;
679 if (ppBuffer == NULL) {
687 if (NULL != hDevice->pfCallback) {
695 *ppBuffer = hDevice->pUserBuffer;
698 hDevice->pUserBuffer = NULL;
702 result = hDevice->dmaErrorCode;
733 if (pbAvailable == NULL)
743 *pbAvailable = hDevice->bCompletion;
747 result = hDevice->dmaErrorCode;
775 if (bEnable == hDevice->bDeviceEnabled) {
780 if (
true == bEnable) {
785 NVIC_EnableIRQ(CRYPT_EVT_IRQn);
787 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 789 NVIC_EnableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
790 NVIC_EnableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
794 hDevice->bDeviceEnabled =
true;
797 StartCompute(hDevice);
804 NVIC_DisableIRQ(CRYPT_EVT_IRQn);
806 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 808 NVIC_DisableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
809 NVIC_DisableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
813 StopCompute(hDevice);
817 result = hDevice->dmaErrorCode;
826 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 855 if (hDevice->bDeviceEnabled) {
863 hDevice->bDmaEnabled =
true;
866 NVIC_EnableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
867 NVIC_EnableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
872 hDevice->bDmaEnabled =
false;
875 NVIC_DisableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
876 NVIC_DisableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
892 static uint32_t u32FromU8p(uint8_t *
const pFourBytes)
894 return ( (((uint32_t)pFourBytes[3]) << 24ul) | (((uint32_t)pFourBytes[2]) << 16ul) | (((uint32_t)pFourBytes[1]) << 8ul) | (((uint32_t)pFourBytes[0])) );
901 uint32_t
volatile *pKeyReg = pREG_CRYPT0_AESKEY0;
902 uint8_t *pUserKey = pKey;
903 uint32_t numKeyWords;
906 CLR_BITS(*pREG_CRYPT0_CFG, BITM_CRYPT_CFG_AESKEYLEN);
907 SET_BITS(*pREG_CRYPT0_CFG, (uint32_t)keyLen);
923 for (uint32_t count = 0u; count < numKeyWords; count++) {
924 *pKeyReg = u32FromU8p(pUserKey);
926 pUserKey +=
sizeof(uint32_t);
930 #if defined (__ADUCM4x50__) 931 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 936 uint32_t
volatile *pKeyReg = pREG_CRYPT0_KUW0;
937 uint8_t *pUserKey = pKey;
938 const uint32_t numKeyWords = 16u;
945 SET_BITS(hDevice->pDev->CFG, (uint32_t)ADI_PK_KUW_LEN_512);
948 for (uint32_t count = 0u; count < numKeyWords; count++) {
949 *pKeyReg = u32FromU8p(pUserKey);
951 pUserKey +=
sizeof(uint32_t);
964 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 965 #if (ADI_CRYPTO_ENABLE_DMA == 1) 966 hDevice->bDmaEnabled =
true;
967 NVIC_EnableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
968 NVIC_EnableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
970 hDevice->bDmaEnabled =
false;
971 NVIC_DisableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
972 NVIC_DisableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
976 hDevice->bDmaEnabled =
false;
985 hDevice->bCompletion =
false;
988 CRYPTO_COMPUTE* pCompute = &hDevice->Computation;
991 hDevice->pDev->STAT = hDevice->pDev->STAT;
994 hDevice->pDev->CFG = 0u;
996 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 999 SET_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_SHAINIT);
1003 SET_BITS(hDevice->pDev->CFG,
1004 ( (uint32_t)pCompute->eCipherMode
1005 #
if defined (__ADUCM4x50__)
1006 | (uint32_t)pCompute->eKeyByteSwap
1007 | (uint32_t)pCompute->eShaByteSwap
1009 | (uint32_t)pCompute->eAesByteSwap
1010 | (uint32_t)pCompute->eAesKeyLen
1011 | (uint32_t)pCompute->eCodingMode
1015 #if defined (__ADUCM4x50__) 1016 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1018 if ((ADI_CRYPTO_MODE_HMAC == pCompute->eCipherMode) && (NULL != pCompute->pHmacKey)) {
1020 loadHmacKey(hDevice, pCompute->pHmacKey);
1026 #if (CRYPTO_SUPPORT_KEY_REQUIRED) 1028 #if (defined (__ADUCM4x50__) && (1 == ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT)) 1033 if ( (
true == pCompute->bUsePKSTOR) && ((ADI_PK_KUW_LEN_128 == pCompute->pkKuwLen) || (ADI_PK_KUW_LEN_256 == pCompute->pkKuwLen)) )
1036 adi_crypto_pk_EnablePKSTOR (hDevice,
true);
1037 adi_crypto_pk_SetKuwLen (hDevice, pCompute->pkKuwLen);
1038 adi_crypto_pk_LoadDeviceKey (hDevice);
1039 adi_crypto_pk_RetrieveKey (hDevice, pCompute->pkIndex);
1040 adi_crypto_pk_UnwrapKuwReg (hDevice);
1041 adi_crypto_pk_UseDecryptedKey (hDevice);
1042 adi_crypto_pk_EnablePKSTOR (hDevice,
false);
1048 if (NULL != pCompute->pAesKey) {
1049 loadAesKey(pCompute->pAesKey, pCompute->eAesKeyLen);
1055 #if (ADI_CRYPTO_ENABLE_CMAC_SUPPORT == 1) 1059 hDevice->pDev->DATALEN = pCompute->numInputBytesRemaining / CRYPTO_INPUT_SIZE_IN_BYTES;
1063 #if (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) 1066 hDevice->pDev->PREFIXLEN = pCompute->numAuthBytesRemaining / CRYPTO_INPUT_SIZE_IN_BYTES;
1067 hDevice->pDev->DATALEN = pCompute->numInputBytesRemaining / CRYPTO_INPUT_SIZE_IN_BYTES;
1068 hDevice->pDev->CCM_NUM_VALID_BYTES = pCompute->numValidBytes;
1072 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1077 assert (NULL != pCompute->pNonceIV);
1080 hDevice->pDev->CNTRINIT = pCompute->CounterInit;
1082 hDevice->pDev->NONCE0 = u32FromU8p(&pCompute->pNonceIV[0]);
1083 hDevice->pDev->NONCE1 = u32FromU8p(&pCompute->pNonceIV[4]);
1084 hDevice->pDev->NONCE2 = u32FromU8p(&pCompute->pNonceIV[8]);
1086 hDevice->pDev->NONCE3 = ((uint32_t)pCompute->pNonceIV[12] << 0u) | ((uint32_t)pCompute->pNonceIV[13] << 8u);
1088 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) 1092 hDevice->pDev->NONCE3 |= ( ((uint32_t)pCompute->pNonceIV[14] << 16u) | ((uint32_t)pCompute->pNonceIV[15] << 24u) );
1098 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1101 if ( (
true == hDevice->bDmaEnabled)
1103 #
if defined (__ADUCM4x50__)
1104 && (ADI_CRYPTO_MODE_HMAC != pCompute->eCipherMode)
1107 || (4u < pCompute->numInputBytesRemaining)
1112 programDMA(hDevice);
1115 switch (pCompute->eCipherMode) {
1116 #if defined (__ADUCM4x50__) 1117 case ADI_CRYPTO_MODE_HMAC:
1119 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_HMACDONEEN | BITM_CRYPT_INTEN_INOVREN));
1120 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1125 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_SHADONEN | BITM_CRYPT_INTEN_INOVREN));
1126 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1130 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_OUTDMAEN | BITM_CRYPT_CFG_INDMAEN));
1135 SET_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_BLKEN);
1137 #if defined (__ADUCM4x50__) 1138 if(ADI_CRYPTO_MODE_HMAC == pCompute->eCipherMode)
1140 SET_BITS(hDevice->pDev->STAT, (uint32_t)BITP_CRYPT_STAT_HMACMSGRDY);
1148 switch (pCompute->eCipherMode) {
1149 #if defined (__ADUCM4x50__) 1150 case ADI_CRYPTO_MODE_HMAC:
1156 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_HMACDONEEN | BITM_CRYPT_INTEN_HMACMSGRDYEN | BITM_CRYPT_INTEN_INOVREN));
1161 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_SHADONEN | BITM_CRYPT_INTEN_INOVREN));
1164 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_INOVREN | BITM_CRYPT_INTEN_OUTRDYEN | BITM_CRYPT_INTEN_INRDYEN));
1169 SET_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_BLKEN);
1171 #if defined (__ADUCM4x50__) 1177 if (ADI_CRYPTO_MODE_HMAC != pCompute->eCipherMode)
1181 writePioInputData(hDevice, hDevice->pDev->STAT);
1191 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1193 CLR_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN | BITM_CRYPT_CFG_OUTDMAEN));
1197 hDevice->pDev->INTEN = 0u;
1200 FlushInputOutputRegisters(hDevice);
1203 CLR_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_BLKEN);
1206 hDevice->bDeviceEnabled =
false;
1210 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1213 CRYPTO_COMPUTE* pCompute = &hDevice->Computation;
1215 uint32_t channelBit;
1216 uint32_t num32BitWords;
1219 channelBit = 1u << hDevice->pDevInfo->dmaInputChanNum;
1220 pADI_DMA0->SRCADDR_CLR = channelBit;
1221 pADI_DMA0->DSTADDR_CLR = channelBit;
1222 pADI_DMA0->EN_SET = channelBit;
1223 pADI_DMA0->RMSK_CLR = channelBit;
1225 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1227 if (0u != pCompute->pNextAuthInput) {
1234 pADI_DMA0->ALT_CLR = channelBit;
1235 pCCD = pPrimaryCCD + hDevice->pDevInfo->dmaInputChanNum;
1238 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextAuthInput +
sizeof(uint32_t) * (pCompute->numAuthBytesRemaining / FIFO_WIDTH_IN_BYTES - 1u);
1239 pCCD->
DMADSTEND = (uint32_t)&hDevice->pDev->INBUF;
1242 num32BitWords = pCompute->numAuthBytesRemaining /
sizeof(uint32_t);
1243 if (0u == pCompute->numInputBytesRemaining) {
1250 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1251 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_BASIC << DMA_BITP_CTL_CYCLE_CTL) );
1260 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1261 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_PING_PONG << DMA_BITP_CTL_CYCLE_CTL) );
1264 pADI_DMA0->PRI_CLR = channelBit;
1265 pCCD = pAlternateCCD + hDevice->pDevInfo->dmaInputChanNum;
1268 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput +
sizeof(uint32_t) * (pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES - 1u);
1269 pCCD->
DMADSTEND = (uint32_t)&hDevice->pDev->INBUF;
1272 num32BitWords = pCompute->numInputBytesRemaining /
sizeof(uint32_t);
1278 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1279 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_BASIC << DMA_BITP_CTL_CYCLE_CTL) );
1287 pADI_DMA0->ALT_CLR = channelBit;
1288 pCCD = pPrimaryCCD + hDevice->pDevInfo->dmaInputChanNum;
1291 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1298 num32BitWords = pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES;
1299 if (pCompute->numInputBytesRemaining > MAX_CRYPTO_DMA_BYTES) {
1300 num32BitWords = MAX_CRYPTO_DMA_BYTES / FIFO_WIDTH_IN_BYTES;
1301 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput +
sizeof(uint32_t) * (num32BitWords - 1u);
1303 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput +
sizeof(uint32_t) * (pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES - 2u);
1304 num32BitWords = (pCompute->numInputBytesRemaining - (pCompute->numInputBytesRemaining %
sizeof(uint32_t))) /
sizeof(uint32_t) - 1u;
1310 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1311 if (ADI_CRYPTO_MODE_HMAC == pCompute->eCipherMode) {
1313 num32BitWords = pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES;
1314 if (pCompute->numInputBytesRemaining > MAX_CRYPTO_DMA_BYTES) {
1315 num32BitWords = MAX_CRYPTO_DMA_BYTES / FIFO_WIDTH_IN_BYTES;
1317 num32BitWords = pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES;
1319 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput + FIFO_WIDTH_IN_BYTES * (num32BitWords - 1u);
1325 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput +
sizeof(uint32_t) * ( pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES - 1u);
1326 num32BitWords = pCompute->numInputBytesRemaining /
sizeof(uint32_t);
1329 pCCD->
DMADSTEND = (uint32_t)&hDevice->pDev->INBUF;
1337 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1338 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_BASIC << DMA_BITP_CTL_CYCLE_CTL) );
1342 #if CRYPTO_SUPPORT_MODE_ANY_NON_SHA 1345 #if defined (__ADUCM4x50__) 1346 && (ADI_CRYPTO_MODE_HMAC != pCompute->eCipherMode)
1352 channelBit = 1u << hDevice->pDevInfo->dmaOutputChanNum;
1355 pADI_DMA0->SRCADDR_CLR = channelBit;
1356 pADI_DMA0->DSTADDR_CLR = channelBit;
1357 pADI_DMA0->EN_SET = channelBit;
1358 pADI_DMA0->RMSK_CLR = channelBit;
1360 pADI_DMA0->ALT_CLR = channelBit;
1361 pCCD = pPrimaryCCD + hDevice->pDevInfo->dmaOutputChanNum;
1365 pCCD->
DMASRCEND = (uint32_t)&hDevice->pDev->OUTBUF;
1366 pCCD->
DMADSTEND = (uint32_t)pCompute->pNextOutput +
sizeof(uint32_t) * (pCompute->numOutputBytesRemaining / FIFO_WIDTH_IN_BYTES - 1u);
1369 num32BitWords = pCompute->numOutputBytesRemaining /
sizeof(uint32_t);
1375 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1376 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_BASIC << DMA_BITP_CTL_CYCLE_CTL) );
1385 static void writePioInputData(
ADI_CRYPTO_HANDLE const hDevice, uint32_t
const status)
1387 CRYPTO_COMPUTE* pCompute = &hDevice->Computation;
1388 uint32_t numWritable = FIFO_DEPTH - ((status & BITM_CRYPT_STAT_INWORDS) >> BITP_CRYPT_STAT_INWORDS);
1390 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1392 if (0u != pCompute->numAuthBytesRemaining) {
1395 while ((0u != numWritable) && (0u != pCompute->numAuthBytesRemaining)) {
1396 hDevice->pDev->INBUF = *pCompute->pNextAuthInput;
1397 pCompute->pNextAuthInput++;
1398 pCompute->numAuthBytesRemaining -= FIFO_WIDTH_IN_BYTES;
1406 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1414 if (pCompute->numInputBytesRemaining >= SHA_CHUNK_MAX_BYTES)
1417 for (uint8_t i = 0u; i < SHA_CHUNK_MAX_WORDS; i++) {
1418 hDevice->pDev->INBUF = *pCompute->pNextInput;
1419 pCompute->pNextInput++;
1422 if(pCompute->numShaBitsRemaining >= SHA_CHUNK_MAX_BITS)
1423 pCompute->numShaBitsRemaining -= SHA_CHUNK_MAX_BITS;
1425 pCompute->numShaBitsRemaining = 0;
1427 pCompute->numInputBytesRemaining -= SHA_CHUNK_MAX_BYTES;
1432 while (FIFO_WIDTH_IN_BITS <= pCompute->numShaBitsRemaining) {
1433 hDevice->pDev->INBUF = *pCompute->pNextInput;
1434 pCompute->pNextInput++;
1435 pCompute->numShaBitsRemaining -= FIFO_WIDTH_IN_BITS;
1438 hDevice->pDev->SHA_LAST_WORD = (pCompute->numShaBitsRemaining << BITP_CRYPT_SHA_LAST_WORD_O_BITS_VALID) | BITM_CRYPT_SHA_LAST_WORD_O_LAST_WORD;
1441 if (0u == pCompute->numShaBitsRemaining) {
1443 hDevice->pDev->INBUF = 0u;
1446 hDevice->pDev->INBUF = *pCompute->pNextInput;
1447 pCompute->pNextInput++;
1450 pCompute->numShaBitsRemaining = 0u;
1451 pCompute->numInputBytesRemaining = 0u;
1454 pCompute->numOutputBytesRemaining -= SHA_OUTPUT_SIZE_IN_BYTES;
1459 #if defined (__ADUCM4x50__) 1460 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1462 if (ADI_CRYPTO_MODE_HMAC == pCompute->eCipherMode) {
1469 if (pCompute->numInputBytesRemaining >= HMAC_CHUNK_MAX_BYTES)
1472 for (uint8_t i = 0u; i < HMAC_CHUNK_MAX_WORDS; i++) {
1473 hDevice->pDev->INBUF = *pCompute->pNextInput;
1474 pCompute->pNextInput++;
1477 if(pCompute->numShaBitsRemaining >= HMAC_CHUNK_MAX_BITS)
1478 pCompute->numShaBitsRemaining -= HMAC_CHUNK_MAX_BITS;
1480 pCompute->numShaBitsRemaining = 0;
1482 pCompute->numInputBytesRemaining -= HMAC_CHUNK_MAX_BYTES;
1487 while (FIFO_WIDTH_IN_BITS <= pCompute->numShaBitsRemaining) {
1488 hDevice->pDev->INBUF = *pCompute->pNextInput;
1489 pCompute->pNextInput++;
1490 pCompute->numShaBitsRemaining -= FIFO_WIDTH_IN_BITS;
1493 hDevice->pDev->SHA_LAST_WORD = (pCompute->numShaBitsRemaining << BITP_CRYPT_SHA_LAST_WORD_O_BITS_VALID) | BITM_CRYPT_SHA_LAST_WORD_O_LAST_WORD;
1496 if (0u == pCompute->numShaBitsRemaining) {
1498 hDevice->pDev->INBUF = 0u;
1501 hDevice->pDev->INBUF = *pCompute->pNextInput;
1502 pCompute->pNextInput++;
1505 pCompute->numShaBitsRemaining = 0u;
1506 pCompute->numInputBytesRemaining = 0u;
1509 pCompute->numOutputBytesRemaining -= SHA_OUTPUT_SIZE_IN_BYTES;
1517 while ((0u != numWritable) && (0u != pCompute->numInputBytesRemaining)) {
1518 hDevice->pDev->INBUF = *pCompute->pNextInput;
1519 pCompute->pNextInput++;
1520 pCompute->numInputBytesRemaining -= FIFO_WIDTH_IN_BYTES;
1528 static void readPioOutputData(
ADI_CRYPTO_HANDLE const hDevice, uint32_t
const status)
1530 CRYPTO_COMPUTE *pCompute = &hDevice->Computation;
1531 uint32_t numReadable;
1533 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) || (defined (__ADUCM4x50__) && (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1)) 1535 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) && ((!defined (__ADUCM4x50__)) || (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 0)) 1538 #elif (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 0) && defined (__ADUCM4x50__) && (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1540 if (pCompute->eCipherMode == ADI_CRYPTO_MODE_HMAC)
1543 if ((pCompute->eCipherMode ==
ADI_CRYPTO_MODE_SHA) || (pCompute->eCipherMode == ADI_CRYPTO_MODE_HMAC))
1546 #if defined (__ADUCM4x50__) 1547 if (IS_ANY_BIT_SET(status, (BITM_CRYPT_STAT_SHADONE | BITM_CRYPT_STAT_HMACDONE))) {
1549 if (IS_ANY_BIT_SET(status, (BITM_CRYPT_STAT_SHADONE))) {
1552 if (0u == pCompute->numOutputBytesRemaining) {
1553 #if ADI_CRYPTO_SHA_OUTPUT_FORMAT == 0 1554 pCompute->pNextOutput[0] = hDevice->pDev->SHAH7;
1555 pCompute->pNextOutput[1] = hDevice->pDev->SHAH6;
1556 pCompute->pNextOutput[2] = hDevice->pDev->SHAH5;
1557 pCompute->pNextOutput[3] = hDevice->pDev->SHAH4;
1558 pCompute->pNextOutput[4] = hDevice->pDev->SHAH3;
1559 pCompute->pNextOutput[5] = hDevice->pDev->SHAH2;
1560 pCompute->pNextOutput[6] = hDevice->pDev->SHAH1;
1561 pCompute->pNextOutput[7] = hDevice->pDev->SHAH0;
1563 pCompute->pNextOutput[0] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH0);
1564 pCompute->pNextOutput[1] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH1);
1565 pCompute->pNextOutput[2] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH2);
1566 pCompute->pNextOutput[3] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH3);
1567 pCompute->pNextOutput[4] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH4);
1568 pCompute->pNextOutput[5] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH5);
1569 pCompute->pNextOutput[6] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH6);
1570 pCompute->pNextOutput[7] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH7);
1579 if (IS_ANY_BIT_SET(status, BITM_CRYPT_STAT_OUTRDY)) {
1580 numReadable = ((status & BITM_CRYPT_STAT_OUTWORDS) >> BITP_CRYPT_STAT_OUTWORDS);
1581 while ((0u != numReadable) && (0u != pCompute->numOutputBytesRemaining)) {
1582 *pCompute->pNextOutput = hDevice->pDev->OUTBUF;
1583 pCompute->pNextOutput++;
1584 pCompute->numOutputBytesRemaining -= FIFO_WIDTH_IN_BYTES;
1591 if (0u == pCompute->numOutputBytesRemaining) {
1592 hDevice->bCompletion =
true;
1601 SET_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_INFLUSH | BITM_CRYPT_CFG_OUTFLUSH);
1602 CLR_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_INFLUSH | BITM_CRYPT_CFG_OUTFLUSH);
1609 void Crypto_Int_Handler(
void)
1614 CRYPTO_COMPUTE *pCompute = &hDevice->Computation;
1615 uint32_t status = hDevice->pDev->STAT;
1619 hDevice->pDev->STAT = status;
1622 if (IS_ANY_BIT_SET(status, BITM_CRYPT_STAT_INOVR)) {
1625 if (0u != hDevice->pfCallback) {
1630 StopCompute(hDevice);
1639 readPioOutputData(hDevice, status);
1641 if (
false == hDevice->bCompletion) {
1644 if ((pCompute->eCipherMode !=
ADI_CRYPTO_MODE_SHA) || (hDevice->bDmaEnabled ==
false) || (pCompute->numInputBytesRemaining == 0u))
1646 writePioInputData(hDevice, status);
1654 if (0u != hDevice->pfCallback) {
1657 if (0u != (BITM_CRYPT_STAT_INOVR & status)) {
1661 switch (hDevice->Computation.eCipherMode) {
1662 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) 1665 #if (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) 1668 #if (ADI_CRYPTO_ENABLE_CMAC_SUPPORT == 1) 1671 #if (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1674 #if (ADI_CRYPTO_ENABLE_ECB_SUPPORT == 1) 1677 #if defined (__ADUCM4x50__) 1678 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1679 case ADI_CRYPTO_MODE_HMAC:
event = ADI_CRYPTO_EVENT_STATUS_HMAC_DONE;
break;
1682 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1690 hDevice->pfCallback(hDevice->pCBParam, event, (
void*)hDevice->pUserBuffer);
1694 hDevice->pUserBuffer = NULL;
1698 hDevice->pDev->INTEN = 0u;
1708 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1710 void DMA_AES0_IN_Int_Handler (
void)
1715 CRYPTO_COMPUTE *pCompute = &hDevice->Computation;
1717 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1721 uint32_t numTotalBytes = pCompute->numInputBytesRemaining;
1722 uint32_t num32BitWords = (numTotalBytes - (numTotalBytes %
sizeof(uint32_t))) /
sizeof(uint32_t) - 1u;
1723 if (pCompute->numInputBytesRemaining > MAX_CRYPTO_DMA_BYTES) {
1724 numTotalBytes = MAX_CRYPTO_DMA_BYTES;
1725 num32BitWords = MAX_CRYPTO_DMA_BYTES /
sizeof(uint32_t);
1727 pCompute->numInputBytesRemaining -= num32BitWords*4u;
1728 pCompute->numShaBitsRemaining -= num32BitWords*32u;
1729 pCompute->pNextInput += num32BitWords;
1731 if (8u < pCompute->numInputBytesRemaining) {
1734 CLR_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1735 programDMA(hDevice);
1736 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1740 if ((numTotalBytes % SHA_CHUNK_MAX_BYTES) == 0u)
1743 hDevice->pDev->INBUF = *pCompute->pNextInput;
1745 pCompute->numInputBytesRemaining = 0u;
1746 pCompute->numShaBitsRemaining = 0u;
1751 writePioInputData(hDevice, hDevice->pDev->STAT);
1757 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1758 if (ADI_CRYPTO_MODE_HMAC == pCompute->eCipherMode) {
1761 uint32_t numTotalBytes = pCompute->numInputBytesRemaining;
1762 uint32_t num32BitWords = numTotalBytes / FIFO_WIDTH_IN_BYTES;
1763 if (pCompute->numInputBytesRemaining > MAX_CRYPTO_DMA_BYTES) {
1764 numTotalBytes = MAX_CRYPTO_DMA_BYTES;
1765 num32BitWords = numTotalBytes / FIFO_WIDTH_IN_BYTES;
1767 pCompute->numInputBytesRemaining -= num32BitWords * FIFO_WIDTH_IN_BYTES;
1768 pCompute->numShaBitsRemaining -= num32BitWords * FIFO_WIDTH_IN_BITS;
1769 pCompute->pNextInput += num32BitWords;
1771 if (4u < pCompute->numInputBytesRemaining) {
1774 CLR_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1775 programDMA(hDevice);
1776 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1779 writePioInputData(hDevice, hDevice->pDev->STAT);
1792 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1794 void DMA_AES0_OUT_Int_Handler (
void)
1798 uint32_t status = hDevice->pDev->STAT;
1804 if (0u != hDevice->pfCallback) {
1807 if (0u != (BITM_CRYPT_STAT_INOVR & status)) {
1811 switch (hDevice->Computation.eCipherMode) {
1812 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) 1815 #if (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) 1818 #if (ADI_CRYPTO_ENABLE_CMAC_SUPPORT == 1) 1821 #if (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1824 #if (ADI_CRYPTO_ENABLE_ECB_SUPPORT == 1) 1827 #if defined (__ADUCM4x50__) 1828 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1829 case ADI_CRYPTO_MODE_HMAC:
event = ADI_CRYPTO_EVENT_STATUS_HMAC_DONE;
break;
1832 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1840 hDevice->pfCallback(hDevice->pCBParam, event, (
void*)hDevice->pUserBuffer);
1844 hDevice->pUserBuffer = NULL;
1848 hDevice->bCompletion =
true;
1851 hDevice->pDev->STAT = status;
ADI_DMA_RESULT adi_dma_RegisterCallback(DMA_CHANn_TypeDef const eChannelID, ADI_CALLBACK const pfCallback, void *const pCBParam)
Register a call-back function for a DMA channel.
ADI_CRYPTO_AES_BYTE_SWAP eAesByteSwap
ADI_CRYPTO_CODING_MODE eCodingMode
ADI_CRYPTO_RESULT adi_crypto_GetBuffer(ADI_CRYPTO_HANDLE const hDevice, ADI_CRYPTO_TRANSACTION **const ppBuffer)
Get the submitted transaction buffer back from the driver.
ADI_CRYPTO_AES_KEY_LEN eAesKeyLen
ADI_CRYPTO_RESULT adi_crypto_RegisterCallback(ADI_CRYPTO_HANDLE const hDevice, ADI_CALLBACK const pfCallback, void *const pCBParam)
Register a user callback function.
ADI_CRYPTO_RESULT adi_crypto_Enable(ADI_CRYPTO_HANDLE const hDevice, bool const bEnable)
Enable/Disable the device. Enabling the device causes the submitted buffer to be processed.
#define ADI_CRYPTO_MEMORY_SIZE
ADI_CRYPTO_RESULT adi_crypto_Open(uint32_t const nDeviceNum, void *const pMemory, uint32_t const nMemorySize, ADI_CRYPTO_HANDLE *const phDevice)
Opens a Crypto device instance.
ADI_CRYPTO_RESULT adi_crypto_SubmitBuffer(ADI_CRYPTO_HANDLE const hDevice, ADI_CRYPTO_TRANSACTION *const pBuffer)
Submit a Crypto transaction buffer for processing.
void adi_dma_Init(void)
Initialize the DMA peripheral.
struct __ADI_CRYPTO_DEV_DATA_TYPE * ADI_CRYPTO_HANDLE
ADI_CRYPTO_RESULT adi_crypto_IsBufferAvailable(ADI_CRYPTO_HANDLE const hDevice, bool *const pbAvailable)
Peek function to know whether a submitted transaction is complete.
ADI_CRYPTO_RESULT adi_crypto_Close(ADI_CRYPTO_HANDLE const hDevice)
Close the given device instance.
ADI_CRYPTO_CIPHER_MODE eCipherMode