47 #include <adi_processor.h> 52 #include <drivers/crypto/adi_crypto.h> 55 #include "adi_crypto_def.h" 57 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 59 #include <drivers/dma/adi_dma.h> 77 #pragma diag_suppress=Pm123,Pm140,Pm050,Pm088,Pm073,Pm143,Pm152 81 #define CLR_BITS(REG,BITS) ((REG) &= ~(BITS)) 82 #define SET_BITS(REG,BITS) ((REG) |= (BITS)) 83 #define IS_ANY_BIT_SET(REG,BITS) (((REG) & (BITS)) != 0u) 87 #define NUM_DEVICES (1u) 93 #if defined ( __ICCARM__ ) 94 #define __ADI_BYTE_SWAP(X) __REV(X) 95 #elif defined (__GNUC__) 96 #define __ADI_BYTE_SWAP(X) __builtin_bswap32(X) 97 #elif defined (__CC_ARM) 98 #define __ADI_BYTE_SWAP(X) __rev(X) 100 #error "This toolchain is not supported" 106 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 107 static void dmaCallback (
void *pCBParam, uint32_t Event,
void *pArg);
117 static uint32_t u32FromU8p (uint8_t *
const pFourBytes);
122 #if defined (__ADUCM4x50__) 123 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 140 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 145 static void writePioInputData (
ADI_CRYPTO_HANDLE const hDevice, uint32_t
const status);
148 static void readPioOutputData (
ADI_CRYPTO_HANDLE const hDevice, uint32_t
const status);
155 void Crypto_Int_Handler(
void);
156 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 157 void DMA_AES0_IN_Int_Handler (
void);
158 void DMA_AES0_OUT_Int_Handler (
void);
165 static CRYPTO_INFO CryptoDevInfo[] = {
168 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 184 #if (defined (__ADUCM4x50__) && (1 == ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT)) 185 #include "adi_pkstor.c" 189 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 193 static void dmaCallback(
void *pCBParam, uint32_t Event,
void *pArg)
199 uint32_t failingChannel = (uint32_t)pCBParam;
218 if (0u != hDevice->pfCallback) {
219 hDevice->pfCallback (hDevice->pCBParam, (uint32_t)hDevice->dmaErrorCode, (
void*)failingChannel);
223 StopCompute(hDevice);
235 for (x = 0u; x < NUM_DEVICES; x++) {
236 if (CryptoDevInfo[x].hDevice == hDevice) {
266 if ( (0u != (3u & (uint32_t)pBuffer->
pAuthData)) || (0u != (3u & (uint32_t)pBuffer->
pInputData)) || (0u != (3u & (uint32_t)pBuffer->
pOutputData)) ) {
270 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 275 if ( (MAX_CRYPTO_DMA_BYTES < pBuffer->numAuthBytes)
276 || (MAX_CRYPTO_DMA_BYTES < pBuffer->numOutputBytes)
277 || ( (MAX_CRYPTO_DMA_BYTES < pBuffer->numInputBytes)
278 #
if defined (__ADUCM4x50__)
289 #if ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1 299 #if defined (__ADUCM4x50__) 300 #if ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1 313 #if ADI_CRYPTO_ENABLE_CMAC_SUPPORT == 1 333 #if ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1 336 if ((pBuffer->
CounterInit & (0xFFF00000u)) != 0u) {
342 #if ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1 345 if ( ((pBuffer->
CounterInit & (0xFFFF0000u)) != 0u)
400 if (nDeviceNum >= NUM_DEVICES) {
404 if ((pMemory == NULL) || (phDevice == NULL)) {
412 if (CryptoDevInfo[nDeviceNum].hDevice != NULL) {
429 hDevice->pDev = CryptoDevInfo[nDeviceNum].pDev;
432 hDevice->pDevInfo = CryptoDevInfo;
435 CryptoDevInfo[nDeviceNum].hDevice = hDevice;
438 InitializeDevData(hDevice);
443 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 492 if (hDevice->bDeviceEnabled) {
503 for (x=0u; x < NUM_DEVICES; x++) {
504 if (CryptoDevInfo[x].hDevice == hDevice) {
505 CryptoDevInfo[x].hDevice = NULL;
543 ADI_INT_STATUS_ALLOC();
544 ADI_ENTER_CRITICAL_REGION();
545 hDevice->pfCallback = pfCallback;
546 hDevice->pCBParam = pCBParam;
547 ADI_EXIT_CRITICAL_REGION();
590 if (NULL != hDevice->pUserBuffer) {
607 hDevice->pUserBuffer = pBuffer;
648 if (ppBuffer == NULL) {
656 if (NULL != hDevice->pfCallback) {
664 *ppBuffer = hDevice->pUserBuffer;
667 hDevice->pUserBuffer = NULL;
671 result = hDevice->dmaErrorCode;
702 if (pbAvailable == NULL)
712 *pbAvailable = hDevice->bCompletion;
716 result = hDevice->dmaErrorCode;
744 if (bEnable == hDevice->bDeviceEnabled) {
749 if (
true == bEnable) {
754 NVIC_EnableIRQ(CRYPT_EVT_IRQn);
756 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 758 NVIC_EnableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
759 NVIC_EnableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
763 hDevice->bDeviceEnabled =
true;
766 StartCompute(hDevice);
773 NVIC_DisableIRQ(CRYPT_EVT_IRQn);
775 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 777 NVIC_DisableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
778 NVIC_DisableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
782 StopCompute(hDevice);
786 result = hDevice->dmaErrorCode;
795 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 824 if (hDevice->bDeviceEnabled) {
832 hDevice->bDmaEnabled =
true;
835 NVIC_EnableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
836 NVIC_EnableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
841 hDevice->bDmaEnabled =
false;
844 NVIC_DisableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
845 NVIC_DisableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
861 static uint32_t u32FromU8p(uint8_t *
const pFourBytes)
863 return ( (((uint32_t)pFourBytes[3]) << 24ul) | (((uint32_t)pFourBytes[2]) << 16ul) | (((uint32_t)pFourBytes[1]) << 8ul) | (((uint32_t)pFourBytes[0])) );
870 uint32_t
volatile *pKeyReg = pREG_CRYPT0_AESKEY0;
871 uint8_t *pUserKey = pKey;
872 uint32_t numKeyWords;
875 CLR_BITS(*pREG_CRYPT0_CFG, BITM_CRYPT_CFG_AESKEYLEN);
876 SET_BITS(*pREG_CRYPT0_CFG, (uint32_t)keyLen);
892 for (uint32_t count = 0u; count < numKeyWords; count++) {
893 *pKeyReg = u32FromU8p(pUserKey);
895 pUserKey +=
sizeof(uint32_t);
899 #if defined (__ADUCM4x50__) 900 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 905 uint32_t
volatile *pKeyReg = pREG_CRYPT0_KUW0;
906 uint8_t *pUserKey = pKey;
907 const uint32_t numKeyWords = 16u;
914 SET_BITS(hDevice->pDev->CFG, (uint32_t)ADI_PK_KUW_LEN_512);
917 for (uint32_t count = 0u; count < numKeyWords; count++) {
918 *pKeyReg = u32FromU8p(pUserKey);
920 pUserKey +=
sizeof(uint32_t);
933 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 934 #if (ADI_CRYPTO_ENABLE_DMA == 1) 935 hDevice->bDmaEnabled =
true;
936 NVIC_EnableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
937 NVIC_EnableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
939 hDevice->bDmaEnabled =
false;
940 NVIC_DisableIRQ(hDevice->pDevInfo->dmaInputIrqNum);
941 NVIC_DisableIRQ(hDevice->pDevInfo->dmaOutputIrqNum);
945 hDevice->bDmaEnabled =
false;
954 hDevice->bCompletion =
false;
957 CRYPTO_COMPUTE* pCompute = &hDevice->Computation;
960 const uint32_t status = hDevice->pDev->STAT;
961 hDevice->pDev->STAT = status;
964 hDevice->pDev->CFG = 0u;
966 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 969 SET_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_SHAINIT);
973 SET_BITS(hDevice->pDev->CFG,
974 ( (uint32_t)pCompute->eCipherMode
975 #
if defined (__ADUCM4x50__)
976 | (uint32_t)pCompute->eKeyByteSwap
977 | (uint32_t)pCompute->eShaByteSwap
979 | (uint32_t)pCompute->eAesByteSwap
980 | (uint32_t)pCompute->eAesKeyLen
981 | (uint32_t)pCompute->eCodingMode
985 #if defined (__ADUCM4x50__) 986 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 990 loadHmacKey(hDevice, pCompute->pHmacKey);
996 #if (CRYPTO_SUPPORT_KEY_REQUIRED) 998 #if (defined (__ADUCM4x50__) && (1 == ADI_CRYPTO_ENABLE_PKSTOR_SUPPORT)) 1003 if ( (
true == pCompute->bUsePKSTOR) && ((ADI_PK_KUW_LEN_128 == pCompute->pkKuwLen) || (ADI_PK_KUW_LEN_256 == pCompute->pkKuwLen)) )
1006 adi_crypto_pk_EnablePKSTOR (hDevice,
true);
1007 adi_crypto_pk_SetKuwLen (hDevice, pCompute->pkKuwLen);
1008 adi_crypto_pk_LoadDeviceKey (hDevice);
1009 adi_crypto_pk_RetrieveKey (hDevice, pCompute->pkIndex);
1010 adi_crypto_pk_UnwrapKuwReg (hDevice);
1011 adi_crypto_pk_UseDecryptedKey (hDevice);
1012 adi_crypto_pk_EnablePKSTOR (hDevice,
false);
1018 if (NULL != pCompute->pAesKey) {
1019 loadAesKey(pCompute->pAesKey, pCompute->eAesKeyLen);
1025 #if (ADI_CRYPTO_ENABLE_CMAC_SUPPORT == 1) 1029 hDevice->pDev->DATALEN = pCompute->numInputBytesRemaining / CRYPTO_INPUT_SIZE_IN_BYTES;
1033 #if (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) 1036 hDevice->pDev->PREFIXLEN = pCompute->numAuthBytesRemaining / CRYPTO_INPUT_SIZE_IN_BYTES;
1037 hDevice->pDev->DATALEN = pCompute->numInputBytesRemaining / CRYPTO_INPUT_SIZE_IN_BYTES;
1038 hDevice->pDev->CCM_NUM_VALID_BYTES = pCompute->numValidBytes;
1042 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1047 assert (NULL != pCompute->pNonceIV);
1050 hDevice->pDev->CNTRINIT = pCompute->CounterInit;
1052 hDevice->pDev->NONCE0 = u32FromU8p(&pCompute->pNonceIV[0]);
1053 hDevice->pDev->NONCE1 = u32FromU8p(&pCompute->pNonceIV[4]);
1054 hDevice->pDev->NONCE2 = u32FromU8p(&pCompute->pNonceIV[8]);
1056 hDevice->pDev->NONCE3 = ((uint32_t)pCompute->pNonceIV[12] << 0u) | ((uint32_t)pCompute->pNonceIV[13] << 8u);
1058 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) 1062 hDevice->pDev->NONCE3 |= ( ((uint32_t)pCompute->pNonceIV[14] << 16u) | ((uint32_t)pCompute->pNonceIV[15] << 24u) );
1068 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1071 if ( (
true == hDevice->bDmaEnabled)
1073 #
if defined (__ADUCM4x50__)
1077 || (4u < pCompute->numInputBytesRemaining)
1082 programDMA(hDevice);
1085 switch (pCompute->eCipherMode) {
1086 #if defined (__ADUCM4x50__) 1089 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_HMACDONEEN | BITM_CRYPT_INTEN_INOVREN));
1090 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1095 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_SHADONEN | BITM_CRYPT_INTEN_INOVREN));
1096 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1100 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_OUTDMAEN | BITM_CRYPT_CFG_INDMAEN));
1105 SET_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_BLKEN);
1107 #if defined (__ADUCM4x50__) 1110 SET_BITS(hDevice->pDev->STAT, (uint32_t)BITP_CRYPT_STAT_HMACMSGRDY);
1118 switch (pCompute->eCipherMode) {
1119 #if defined (__ADUCM4x50__) 1126 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_HMACDONEEN | BITM_CRYPT_INTEN_HMACMSGRDYEN | BITM_CRYPT_INTEN_INOVREN));
1131 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_SHADONEN | BITM_CRYPT_INTEN_INOVREN));
1134 SET_BITS(hDevice->pDev->INTEN, (BITM_CRYPT_INTEN_INOVREN | BITM_CRYPT_INTEN_OUTRDYEN | BITM_CRYPT_INTEN_INRDYEN));
1139 SET_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_BLKEN);
1141 #if defined (__ADUCM4x50__) 1151 writePioInputData(hDevice, hDevice->pDev->STAT);
1161 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1163 CLR_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN | BITM_CRYPT_CFG_OUTDMAEN));
1167 hDevice->pDev->INTEN = 0u;
1170 FlushInputOutputRegisters(hDevice);
1173 CLR_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_BLKEN);
1176 hDevice->bDeviceEnabled =
false;
1180 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1183 CRYPTO_COMPUTE* pCompute = &hDevice->Computation;
1185 uint32_t channelBit;
1186 uint32_t num32BitWords;
1189 channelBit = 1u << hDevice->pDevInfo->dmaInputChanNum;
1190 pADI_DMA0->SRCADDR_CLR = channelBit;
1191 pADI_DMA0->DSTADDR_CLR = channelBit;
1192 pADI_DMA0->EN_SET = channelBit;
1193 pADI_DMA0->RMSK_CLR = channelBit;
1195 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1197 if (0u != pCompute->pNextAuthInput) {
1204 pADI_DMA0->ALT_CLR = channelBit;
1205 pCCD = pPrimaryCCD + hDevice->pDevInfo->dmaInputChanNum;
1208 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextAuthInput +
sizeof(uint32_t) * (pCompute->numAuthBytesRemaining / FIFO_WIDTH_IN_BYTES - 1u);
1209 pCCD->
DMADSTEND = (uint32_t)&hDevice->pDev->INBUF;
1212 num32BitWords = pCompute->numAuthBytesRemaining /
sizeof(uint32_t);
1213 if (0u == pCompute->numInputBytesRemaining) {
1220 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1221 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_BASIC << DMA_BITP_CTL_CYCLE_CTL) );
1230 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1231 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_PING_PONG << DMA_BITP_CTL_CYCLE_CTL) );
1234 pADI_DMA0->PRI_CLR = channelBit;
1235 pCCD = pAlternateCCD + hDevice->pDevInfo->dmaInputChanNum;
1238 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput +
sizeof(uint32_t) * (pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES - 1u);
1239 pCCD->
DMADSTEND = (uint32_t)&hDevice->pDev->INBUF;
1242 num32BitWords = pCompute->numInputBytesRemaining /
sizeof(uint32_t);
1248 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1249 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_BASIC << DMA_BITP_CTL_CYCLE_CTL) );
1257 pADI_DMA0->ALT_CLR = channelBit;
1258 pCCD = pPrimaryCCD + hDevice->pDevInfo->dmaInputChanNum;
1261 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1268 num32BitWords = pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES;
1269 if (pCompute->numInputBytesRemaining > MAX_CRYPTO_DMA_BYTES) {
1270 num32BitWords = MAX_CRYPTO_DMA_BYTES / FIFO_WIDTH_IN_BYTES;
1271 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput +
sizeof(uint32_t) * (num32BitWords - 1u);
1273 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput +
sizeof(uint32_t) * (pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES - 2u);
1274 num32BitWords = (pCompute->numInputBytesRemaining - (pCompute->numInputBytesRemaining %
sizeof(uint32_t))) /
sizeof(uint32_t) - 1u;
1280 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1283 num32BitWords = pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES;
1284 if (pCompute->numInputBytesRemaining > MAX_CRYPTO_DMA_BYTES) {
1285 num32BitWords = MAX_CRYPTO_DMA_BYTES / FIFO_WIDTH_IN_BYTES;
1287 num32BitWords = pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES;
1289 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput + FIFO_WIDTH_IN_BYTES * (num32BitWords - 1u);
1295 pCCD->
DMASRCEND = (uint32_t)pCompute->pNextInput +
sizeof(uint32_t) * ( pCompute->numInputBytesRemaining / FIFO_WIDTH_IN_BYTES - 1u);
1296 num32BitWords = pCompute->numInputBytesRemaining /
sizeof(uint32_t);
1299 pCCD->
DMADSTEND = (uint32_t)&hDevice->pDev->INBUF;
1307 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1308 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_BASIC << DMA_BITP_CTL_CYCLE_CTL) );
1312 #if CRYPTO_SUPPORT_MODE_ANY_NON_SHA 1315 #if defined (__ADUCM4x50__) 1322 channelBit = 1u << hDevice->pDevInfo->dmaOutputChanNum;
1325 pADI_DMA0->SRCADDR_CLR = channelBit;
1326 pADI_DMA0->DSTADDR_CLR = channelBit;
1327 pADI_DMA0->EN_SET = channelBit;
1328 pADI_DMA0->RMSK_CLR = channelBit;
1330 pADI_DMA0->ALT_CLR = channelBit;
1331 pCCD = pPrimaryCCD + hDevice->pDevInfo->dmaOutputChanNum;
1335 pCCD->
DMASRCEND = (uint32_t)&hDevice->pDev->OUTBUF;
1336 pCCD->
DMADSTEND = (uint32_t)pCompute->pNextOutput +
sizeof(uint32_t) * (pCompute->numOutputBytesRemaining / FIFO_WIDTH_IN_BYTES - 1u);
1339 num32BitWords = pCompute->numOutputBytesRemaining /
sizeof(uint32_t);
1345 | (uint32_t)((num32BitWords - 1u) << DMA_BITP_CTL_N_MINUS_1)
1346 | ((uint32_t)DMA_ENUM_CTL_CYCLE_CTL_BASIC << DMA_BITP_CTL_CYCLE_CTL) );
1355 static void writePioInputData(
ADI_CRYPTO_HANDLE const hDevice, uint32_t
const status)
1357 CRYPTO_COMPUTE* pCompute = &hDevice->Computation;
1358 uint32_t numWritable = FIFO_DEPTH - ((status & BITM_CRYPT_STAT_INWORDS) >> BITP_CRYPT_STAT_INWORDS);
1360 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) || (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1362 if (0u != pCompute->numAuthBytesRemaining) {
1365 while ((0u != numWritable) && (0u != pCompute->numAuthBytesRemaining)) {
1366 hDevice->pDev->INBUF = *pCompute->pNextAuthInput;
1367 pCompute->pNextAuthInput++;
1368 pCompute->numAuthBytesRemaining -= FIFO_WIDTH_IN_BYTES;
1376 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1384 if (pCompute->numInputBytesRemaining >= SHA_CHUNK_MAX_BYTES)
1387 for (uint8_t i = 0u; i < SHA_CHUNK_MAX_WORDS; i++) {
1388 hDevice->pDev->INBUF = *pCompute->pNextInput;
1389 pCompute->pNextInput++;
1392 if(pCompute->numShaBitsRemaining >= SHA_CHUNK_MAX_BITS)
1393 pCompute->numShaBitsRemaining -= SHA_CHUNK_MAX_BITS;
1395 pCompute->numShaBitsRemaining = 0;
1397 pCompute->numInputBytesRemaining -= SHA_CHUNK_MAX_BYTES;
1402 while (FIFO_WIDTH_IN_BITS <= pCompute->numShaBitsRemaining) {
1403 hDevice->pDev->INBUF = *pCompute->pNextInput;
1404 pCompute->pNextInput++;
1405 pCompute->numShaBitsRemaining -= FIFO_WIDTH_IN_BITS;
1408 hDevice->pDev->SHA_LAST_WORD = (pCompute->numShaBitsRemaining << BITP_CRYPT_SHA_LAST_WORD_O_BITS_VALID) | BITM_CRYPT_SHA_LAST_WORD_O_LAST_WORD;
1411 if (0u == pCompute->numShaBitsRemaining) {
1413 hDevice->pDev->INBUF = 0u;
1416 hDevice->pDev->INBUF = *pCompute->pNextInput;
1417 pCompute->pNextInput++;
1420 pCompute->numShaBitsRemaining = 0u;
1421 pCompute->numInputBytesRemaining = 0u;
1424 pCompute->numOutputBytesRemaining -= SHA_OUTPUT_SIZE_IN_BYTES;
1429 #if defined (__ADUCM4x50__) 1430 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1439 if (pCompute->numInputBytesRemaining >= HMAC_CHUNK_MAX_BYTES)
1442 for (uint8_t i = 0u; i < HMAC_CHUNK_MAX_WORDS; i++) {
1443 hDevice->pDev->INBUF = *pCompute->pNextInput;
1444 pCompute->pNextInput++;
1447 if(pCompute->numShaBitsRemaining >= HMAC_CHUNK_MAX_BITS)
1448 pCompute->numShaBitsRemaining -= HMAC_CHUNK_MAX_BITS;
1450 pCompute->numShaBitsRemaining = 0;
1452 pCompute->numInputBytesRemaining -= HMAC_CHUNK_MAX_BYTES;
1457 while (FIFO_WIDTH_IN_BITS <= pCompute->numShaBitsRemaining) {
1458 hDevice->pDev->INBUF = *pCompute->pNextInput;
1459 pCompute->pNextInput++;
1460 pCompute->numShaBitsRemaining -= FIFO_WIDTH_IN_BITS;
1463 hDevice->pDev->SHA_LAST_WORD = (pCompute->numShaBitsRemaining << BITP_CRYPT_SHA_LAST_WORD_O_BITS_VALID) | BITM_CRYPT_SHA_LAST_WORD_O_LAST_WORD;
1466 if (0u == pCompute->numShaBitsRemaining) {
1468 hDevice->pDev->INBUF = 0u;
1471 hDevice->pDev->INBUF = *pCompute->pNextInput;
1472 pCompute->pNextInput++;
1475 pCompute->numShaBitsRemaining = 0u;
1476 pCompute->numInputBytesRemaining = 0u;
1479 pCompute->numOutputBytesRemaining -= SHA_OUTPUT_SIZE_IN_BYTES;
1487 while ((0u != numWritable) && (0u != pCompute->numInputBytesRemaining)) {
1488 hDevice->pDev->INBUF = *pCompute->pNextInput;
1489 pCompute->pNextInput++;
1490 pCompute->numInputBytesRemaining -= FIFO_WIDTH_IN_BYTES;
1498 static void readPioOutputData(
ADI_CRYPTO_HANDLE const hDevice, uint32_t
const status)
1500 CRYPTO_COMPUTE *pCompute = &hDevice->Computation;
1501 uint32_t numReadable;
1503 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) || (defined (__ADUCM4x50__) && (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1)) 1505 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) && ((!defined (__ADUCM4x50__)) || (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 0)) 1508 #elif (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 0) && defined (__ADUCM4x50__) && (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1516 #if defined (__ADUCM4x50__) 1517 if (IS_ANY_BIT_SET(status, (BITM_CRYPT_STAT_SHADONE | BITM_CRYPT_STAT_HMACDONE))) {
1519 if (IS_ANY_BIT_SET(status, (BITM_CRYPT_STAT_SHADONE))) {
1522 if (0u == pCompute->numOutputBytesRemaining) {
1523 #if ADI_CRYPTO_SHA_OUTPUT_FORMAT == 0 1524 pCompute->pNextOutput[0] = hDevice->pDev->SHAH7;
1525 pCompute->pNextOutput[1] = hDevice->pDev->SHAH6;
1526 pCompute->pNextOutput[2] = hDevice->pDev->SHAH5;
1527 pCompute->pNextOutput[3] = hDevice->pDev->SHAH4;
1528 pCompute->pNextOutput[4] = hDevice->pDev->SHAH3;
1529 pCompute->pNextOutput[5] = hDevice->pDev->SHAH2;
1530 pCompute->pNextOutput[6] = hDevice->pDev->SHAH1;
1531 pCompute->pNextOutput[7] = hDevice->pDev->SHAH0;
1533 pCompute->pNextOutput[0] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH0);
1534 pCompute->pNextOutput[1] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH1);
1535 pCompute->pNextOutput[2] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH2);
1536 pCompute->pNextOutput[3] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH3);
1537 pCompute->pNextOutput[4] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH4);
1538 pCompute->pNextOutput[5] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH5);
1539 pCompute->pNextOutput[6] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH6);
1540 pCompute->pNextOutput[7] = __ADI_BYTE_SWAP(hDevice->pDev->SHAH7);
1549 if (IS_ANY_BIT_SET(status, BITM_CRYPT_STAT_OUTRDY)) {
1550 numReadable = ((status & BITM_CRYPT_STAT_OUTWORDS) >> BITP_CRYPT_STAT_OUTWORDS);
1551 while ((0u != numReadable) && (0u != pCompute->numOutputBytesRemaining)) {
1552 *pCompute->pNextOutput = hDevice->pDev->OUTBUF;
1553 pCompute->pNextOutput++;
1554 pCompute->numOutputBytesRemaining -= FIFO_WIDTH_IN_BYTES;
1561 if (0u == pCompute->numOutputBytesRemaining) {
1562 hDevice->bCompletion =
true;
1571 SET_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_INFLUSH | BITM_CRYPT_CFG_OUTFLUSH);
1572 CLR_BITS(hDevice->pDev->CFG, BITM_CRYPT_CFG_INFLUSH | BITM_CRYPT_CFG_OUTFLUSH);
1579 void Crypto_Int_Handler(
void)
1584 CRYPTO_COMPUTE *pCompute = &hDevice->Computation;
1585 uint32_t status = hDevice->pDev->STAT;
1589 hDevice->pDev->STAT = status;
1592 if (IS_ANY_BIT_SET(status, BITM_CRYPT_STAT_INOVR)) {
1595 if (0u != hDevice->pfCallback) {
1600 StopCompute(hDevice);
1609 readPioOutputData(hDevice, status);
1611 if (
false == hDevice->bCompletion) {
1614 if ((pCompute->eCipherMode !=
ADI_CRYPTO_MODE_SHA) || (hDevice->bDmaEnabled ==
false) || (pCompute->numInputBytesRemaining == 0u))
1616 writePioInputData(hDevice, status);
1624 if (0u != hDevice->pfCallback) {
1627 if (0u != (BITM_CRYPT_STAT_INOVR & status)) {
1631 switch (hDevice->Computation.eCipherMode) {
1632 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) 1635 #if (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) 1638 #if (ADI_CRYPTO_ENABLE_CMAC_SUPPORT == 1) 1641 #if (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1644 #if (ADI_CRYPTO_ENABLE_ECB_SUPPORT == 1) 1647 #if defined (__ADUCM4x50__) 1648 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1652 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1660 hDevice->pfCallback(hDevice->pCBParam, event, (
void*)hDevice->pUserBuffer);
1664 hDevice->pUserBuffer = NULL;
1668 hDevice->pDev->INTEN = 0u;
1678 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1680 void DMA_AES0_IN_Int_Handler (
void)
1685 CRYPTO_COMPUTE *pCompute = &hDevice->Computation;
1687 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1691 uint32_t numTotalBytes = pCompute->numInputBytesRemaining;
1692 uint32_t num32BitWords = (numTotalBytes - (numTotalBytes %
sizeof(uint32_t))) /
sizeof(uint32_t) - 1u;
1693 if (pCompute->numInputBytesRemaining > MAX_CRYPTO_DMA_BYTES) {
1694 numTotalBytes = MAX_CRYPTO_DMA_BYTES;
1695 num32BitWords = MAX_CRYPTO_DMA_BYTES /
sizeof(uint32_t);
1697 pCompute->numInputBytesRemaining -= num32BitWords*4u;
1698 pCompute->numShaBitsRemaining -= num32BitWords*32u;
1699 pCompute->pNextInput += num32BitWords;
1701 if (8u < pCompute->numInputBytesRemaining) {
1704 CLR_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1705 programDMA(hDevice);
1706 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1710 if ((numTotalBytes % SHA_CHUNK_MAX_BYTES) == 0u)
1713 hDevice->pDev->INBUF = *pCompute->pNextInput;
1715 pCompute->numInputBytesRemaining = 0u;
1716 pCompute->numShaBitsRemaining = 0u;
1721 writePioInputData(hDevice, hDevice->pDev->STAT);
1727 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1731 uint32_t numTotalBytes = pCompute->numInputBytesRemaining;
1732 uint32_t num32BitWords = numTotalBytes / FIFO_WIDTH_IN_BYTES;
1733 if (pCompute->numInputBytesRemaining > MAX_CRYPTO_DMA_BYTES) {
1734 numTotalBytes = MAX_CRYPTO_DMA_BYTES;
1735 num32BitWords = numTotalBytes / FIFO_WIDTH_IN_BYTES;
1737 pCompute->numInputBytesRemaining -= num32BitWords * FIFO_WIDTH_IN_BYTES;
1738 pCompute->numShaBitsRemaining -= num32BitWords * FIFO_WIDTH_IN_BITS;
1739 pCompute->pNextInput += num32BitWords;
1741 if (4u < pCompute->numInputBytesRemaining) {
1744 CLR_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1745 programDMA(hDevice);
1746 SET_BITS(hDevice->pDev->CFG, (BITM_CRYPT_CFG_INDMAEN));
1749 writePioInputData(hDevice, hDevice->pDev->STAT);
1762 #if (ADI_CRYPTO_ENABLE_DMA_SUPPORT == 1) 1764 void DMA_AES0_OUT_Int_Handler (
void)
1768 uint32_t status = hDevice->pDev->STAT;
1774 if (0u != hDevice->pfCallback) {
1777 if (0u != (BITM_CRYPT_STAT_INOVR & status)) {
1781 switch (hDevice->Computation.eCipherMode) {
1782 #if (ADI_CRYPTO_ENABLE_CBC_SUPPORT == 1) 1785 #if (ADI_CRYPTO_ENABLE_CCM_SUPPORT == 1) 1788 #if (ADI_CRYPTO_ENABLE_CMAC_SUPPORT == 1) 1791 #if (ADI_CRYPTO_ENABLE_CTR_SUPPORT == 1) 1794 #if (ADI_CRYPTO_ENABLE_ECB_SUPPORT == 1) 1797 #if defined (__ADUCM4x50__) 1798 #if (ADI_CRYPTO_ENABLE_HMAC_SUPPORT == 1) 1802 #if (ADI_CRYPTO_ENABLE_SHA_SUPPORT == 1) 1810 hDevice->pfCallback(hDevice->pCBParam, event, (
void*)hDevice->pUserBuffer);
1814 hDevice->pUserBuffer = NULL;
1818 hDevice->bCompletion =
true;
1821 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