32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "hw_sce_private.h"
36 #include "hw_sce_ra_private.h"
37 #include "hw_sce_aes_private.h"
47 #if (RA4_CRYPTO_CIPHER_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
62 uint8_t *output,
size_t length, uint32_t command)
80 status = HW_SCE_Aes128EncryptDecryptInitSub(&keyType, &command,
81 context->
ek, (
const uint32_t *)
iv);
83 else if(context->
nr == 12)
85 status = HW_SCE_Aes192EncryptDecryptInitSub(&command,
86 context->
ek, (
const uint32_t *)
iv);
88 else if(context->
nr == 14)
90 status = HW_SCE_Aes256EncryptDecryptInitSub(&keyType, &command,
91 context->
ek, (
const uint32_t *)
iv);
95 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
99 if(status == FSP_SUCCESS)
105 if(context->
nr == 10)
107 HW_SCE_Aes128EncryptDecryptUpdateSub((
const uint32_t *) input,
108 (uint32_t *) output, (
length -
n) / 4);
110 else if(context->
nr == 12)
112 HW_SCE_Aes192EncryptDecryptUpdateSub((
const uint32_t *) input,
113 (uint32_t *) output, (
length -
n) / 4);
117 HW_SCE_Aes256EncryptDecryptUpdateSub((
const uint32_t *) input,
118 (uint32_t *) output, (
length -
n) / 4);
129 if(context->
nr == 10)
131 HW_SCE_Aes128EncryptDecryptUpdateSub(
block,
block,
134 else if(context->
nr == 12)
136 HW_SCE_Aes192EncryptDecryptUpdateSub(
block,
block,
141 HW_SCE_Aes256EncryptDecryptUpdateSub(
block,
block,
151 if(status == FSP_SUCCESS)
154 if(context->
nr == 10)
156 status = HW_SCE_Aes128EncryptDecryptFinalSub();
158 else if(context->
nr == 12)
160 status = HW_SCE_Aes192EncryptDecryptFinalSub();
162 else if(context->
nr == 14)
164 status = HW_SCE_Aes256EncryptDecryptFinalSub();
168 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
173 if(status != FSP_SUCCESS)
199 if(context == NULL || key == NULL)
212 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
213 SCE_OEM_CMD_AES128, NULL, NULL, key, context->
ek);
215 else if(keyLen == 24)
221 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
222 SCE_OEM_CMD_AES192, NULL, NULL, key, context->
ek);
224 else if(keyLen == 32)
230 status = HW_SCE_GenerateOemKeyIndexPrivate(SCE_OEM_KEY_TYPE_PLAIN,
231 SCE_OEM_CMD_AES256, NULL, NULL, key, context->
ek);
236 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
258 SCE_AES_IN_DATA_CMD_ECB_ENCRYPTION);
273 SCE_AES_IN_DATA_CMD_ECB_DECRYPTION);
277 #if (ECB_SUPPORT == ENABLED)
290 const uint8_t *
p, uint8_t *
c,
size_t length)
309 SCE_AES_IN_DATA_CMD_ECB_ENCRYPTION);
354 const uint8_t *
c, uint8_t *
p,
size_t length)
373 SCE_AES_IN_DATA_CMD_ECB_DECRYPTION);
407 #if (CBC_SUPPORT == ENABLED)
421 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
440 SCE_AES_IN_DATA_CMD_CBC_ENCRYPTION);
505 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
529 SCE_AES_IN_DATA_CMD_CBC_DECRYPTION);
585 #if (CTR_SUPPORT == ENABLED)
600 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
621 while(
length > 0 && !error)
630 SCE_AES_IN_DATA_CMD_CTR_ENCRYPTION_DECRYPTION);
657 for(i = 0; i <
n; i++)
683 #if (GCM_SUPPORT == ENABLED && BSP_FEATURE_CRYPTO_HAS_SCE9 != 0)
697 if(context == NULL || cipherContext == NULL)
729 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
730 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
751 if(tLen < 4 || tLen > 16)
771 if(aesContext->
nr == 10)
773 status = HW_SCE_Aes128GcmEncryptInitSub(&keyType, aesContext->
ek, temp);
775 else if(aesContext->
nr == 12)
777 status = HW_SCE_Aes192GcmEncryptInitSub(aesContext->
ek, temp);
779 else if(aesContext->
nr == 14)
781 status = HW_SCE_Aes256GcmEncryptInitSub(&keyType, aesContext->
ek, temp);
785 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
789 if(status == FSP_SUCCESS)
801 if(aesContext->
nr == 10)
803 HW_SCE_Aes128GcmEncryptUpdateAADSub((uint32_t *)
a,
n / 4);
805 else if(aesContext->
nr == 12)
807 HW_SCE_Aes192GcmEncryptUpdateAADSub((uint32_t *)
a,
n / 4);
811 HW_SCE_Aes256GcmEncryptUpdateAADSub((uint32_t *)
a,
n / 4);
826 if(aesContext->
nr == 10)
828 HW_SCE_Aes128GcmEncryptUpdateAADSub(
block, 1);
830 else if(aesContext->
nr == 12)
832 HW_SCE_Aes192GcmEncryptUpdateAADSub(
block, 1);
836 HW_SCE_Aes256GcmEncryptUpdateAADSub(
block, 1);
841 if(aesContext->
nr == 10)
843 HW_SCE_Aes128GcmEncryptUpdateTransitionSub();
845 else if(aesContext->
nr == 12)
847 HW_SCE_Aes192GcmEncryptUpdateTransitionSub();
851 HW_SCE_Aes256GcmEncryptUpdateTransitionSub();
864 if(aesContext->
nr == 10)
866 HW_SCE_Aes128GcmEncryptUpdateSub((uint32_t *)
p, (uint32_t *)
c,
869 else if(aesContext->
nr == 12)
871 HW_SCE_Aes192GcmEncryptUpdateSub((uint32_t *)
p, (uint32_t *)
c,
876 HW_SCE_Aes256GcmEncryptUpdateSub((uint32_t *)
p, (uint32_t *)
c,
903 if(aesContext->
nr == 10)
905 status = HW_SCE_Aes128GcmEncryptFinalSub(
block, temp, temp + 2,
908 else if(aesContext->
nr == 12)
910 status = HW_SCE_Aes192GcmEncryptFinalSub(
block, temp, temp + 2,
913 else if(aesContext->
nr == 14)
915 status = HW_SCE_Aes256GcmEncryptFinalSub(
block, temp, temp + 2,
920 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
925 if(status == FSP_SUCCESS)
957 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
958 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
979 if(tLen < 4 || tLen > 16)
999 if(aesContext->
nr == 10)
1001 status = HW_SCE_Aes128GcmDecryptInitSub(&keyType, aesContext->
ek, temp);
1003 else if(aesContext->
nr == 12)
1005 status = HW_SCE_Aes192GcmDecryptInitSub(aesContext->
ek, temp);
1007 else if(aesContext->
nr == 14)
1009 status = HW_SCE_Aes256GcmDecryptInitSub(&keyType, aesContext->
ek, temp);
1013 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
1017 if(status == FSP_SUCCESS)
1029 if(aesContext->
nr == 10)
1031 HW_SCE_Aes128GcmDecryptUpdateAADSub((uint32_t *)
a,
n / 4);
1033 else if(aesContext->
nr == 12)
1035 HW_SCE_Aes192GcmDecryptUpdateAADSub((uint32_t *)
a,
n / 4);
1039 HW_SCE_Aes256GcmDecryptUpdateAADSub((uint32_t *)
a,
n / 4);
1054 if(aesContext->
nr == 10)
1056 HW_SCE_Aes128GcmDecryptUpdateAADSub(
block, 1);
1058 else if(aesContext->
nr == 12)
1060 HW_SCE_Aes192GcmDecryptUpdateAADSub(
block, 1);
1064 HW_SCE_Aes256GcmDecryptUpdateAADSub(
block, 1);
1069 if(aesContext->
nr == 10)
1071 HW_SCE_Aes128GcmDecryptUpdateTransitionSub();
1073 else if(aesContext->
nr == 12)
1075 HW_SCE_Aes192GcmDecryptUpdateTransitionSub();
1079 HW_SCE_Aes256GcmDecryptUpdateTransitionSub();
1092 if(aesContext->
nr == 10)
1094 HW_SCE_Aes128GcmDecryptUpdateSub((uint32_t *)
c, (uint32_t *)
p,
1097 else if(aesContext->
nr == 12)
1099 HW_SCE_Aes192GcmDecryptUpdateSub((uint32_t *)
c, (uint32_t *)
p,
1104 HW_SCE_Aes256GcmDecryptUpdateSub((uint32_t *)
c, (uint32_t *)
p,
1134 osMemset(authTag, 0,
sizeof(authTag));
1138 if(aesContext->
nr == 10)
1140 status = HW_SCE_Aes128GcmDecryptFinalSub(
block, temp, temp + 2,
1141 authTag, temp + 4,
block);
1143 else if(aesContext->
nr == 12)
1145 status = HW_SCE_Aes192GcmDecryptFinalSub(
block, temp, temp + 2,
1146 authTag, temp + 4,
block);
1148 else if(aesContext->
nr == 14)
1150 status = HW_SCE_Aes256GcmDecryptFinalSub(
block, temp, temp + 2,
1151 authTag, temp + 4,
block);
1155 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
1160 if(status == FSP_SUCCESS)
1174 #if (CCM_SUPPORT == ENABLED)
1193 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
1194 size_t length, uint8_t *
t,
size_t tLen)
1206 uint32_t authTag[4];
1219 if(aLen > (
sizeof(header) - 18))
1233 osMemset(header, 0,
sizeof(header));
1250 osMemcpy(header + headerLen + 2,
a, aLen);
1252 headerLen += 2 + aLen;
1262 if(aesContext->
nr == 10)
1264 status = HW_SCE_Aes128CcmEncryptInitSubGeneral(&keyType, &dataType,
1265 &command, &textLen, aesContext->
ek,
block, (uint32_t *) header,
1266 &
seqNum, (headerLen + 3) / 4);
1268 else if(aesContext->
nr == 12)
1270 status = HW_SCE_Aes192CcmEncryptInitSubGeneral(&keyType, &dataType,
1271 &command, &textLen, aesContext->
ek,
block, (uint32_t *) header,
1272 &
seqNum, (headerLen + 3) / 4);
1274 else if(aesContext->
nr == 14)
1276 status = HW_SCE_Aes256CcmEncryptInitSubGeneral(&keyType, &dataType,
1277 &command, &textLen, aesContext->
ek,
block, (uint32_t *) header,
1278 &
seqNum, (headerLen + 3) / 4);
1282 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
1286 if(status == FSP_SUCCESS)
1295 if(aesContext->
nr == 10)
1297 HW_SCE_Aes128CcmEncryptUpdateSub((uint32_t *)
p, (uint32_t *)
c,
1300 else if(aesContext->
nr == 12)
1302 HW_SCE_Aes192CcmEncryptUpdateSub((uint32_t *)
p, (uint32_t *)
c,
1307 HW_SCE_Aes256CcmEncryptUpdateSub((uint32_t *)
p, (uint32_t *)
c,
1326 if(aesContext->
nr == 10)
1328 status = HW_SCE_Aes128CcmEncryptFinalSubGeneral(
block, &textLen,
1331 else if(aesContext->
nr == 12)
1333 status = HW_SCE_Aes192CcmEncryptFinalSub(
block, &textLen,
block,
1336 else if(aesContext->
nr == 14)
1338 status = HW_SCE_Aes256CcmEncryptFinalSub(
block, &textLen,
block,
1343 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
1348 if(status == FSP_SUCCESS)
1381 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
1382 size_t length,
const uint8_t *
t,
size_t tLen)
1391 uint32_t authTagLen;
1395 uint32_t authTag[4];
1408 if(aLen > (
sizeof(header) - 18))
1423 osMemset(header, 0,
sizeof(header));
1440 osMemcpy(header + headerLen + 2,
a, aLen);
1442 headerLen += 2 + aLen;
1452 if(aesContext->
nr == 10)
1454 status = HW_SCE_Aes128CcmDecryptInitSubGeneral(&keyType, &dataType,
1455 &command, &textLen, &authTagLen, aesContext->
ek,
block,
1456 (uint32_t *) header, &
seqNum, (headerLen + 3) / 4);
1458 else if(aesContext->
nr == 12)
1460 status = HW_SCE_Aes192CcmDecryptInitSubGeneral(&keyType, &dataType,
1461 &command, &textLen, &authTagLen, aesContext->
ek,
block,
1462 (uint32_t *) header, &
seqNum, (headerLen + 3) / 4);
1464 else if(aesContext->
nr == 14)
1466 status = HW_SCE_Aes256CcmDecryptInitSubGeneral(&keyType, &dataType,
1467 &command, &textLen, &authTagLen, aesContext->
ek,
block,
1468 (uint32_t *) header, &
seqNum, (headerLen + 3) / 4);
1472 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
1476 if(status == FSP_SUCCESS)
1485 if(aesContext->
nr == 10)
1487 HW_SCE_Aes128CcmDecryptUpdateSub((uint32_t *)
c, (uint32_t *)
p,
1490 else if(aesContext->
nr == 12)
1492 HW_SCE_Aes192CcmDecryptUpdateSub((uint32_t *)
c, (uint32_t *)
p,
1497 HW_SCE_Aes256CcmDecryptUpdateSub((uint32_t *)
c, (uint32_t *)
p,
1516 osMemset(authTag, 0,
sizeof(authTag));
1520 if(aesContext->
nr == 10)
1522 status = HW_SCE_Aes128CcmDecryptFinalSubGeneral(
block, &textLen,
1523 authTag, &authTagLen,
block);
1525 else if(aesContext->
nr == 12)
1527 status = HW_SCE_Aes192CcmDecryptFinalSub(
block, &textLen,
1528 authTag, &authTagLen,
block);
1530 else if(aesContext->
nr == 14)
1532 status = HW_SCE_Aes256CcmDecryptFinalSub(
block, &textLen,
1533 authTag, &authTagLen,
block);
1537 status = FSP_ERR_CRYPTO_NOT_IMPLEMENTED;
1542 if(status == FSP_SUCCESS)