32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "stm32h7rsxx.h"
36 #include "stm32h7rsxx_hal.h"
46 #if (STM32H7RSXX_CRYPTO_CIPHER_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
57 __HAL_RCC_CRYP_CLK_ENABLE();
74 temp = CRYP->CR & ~CRYP_CR_KEYSIZE;
83 CRYP->K2LR = context->
ek[0];
84 CRYP->K2RR = context->
ek[1];
85 CRYP->K3LR = context->
ek[2];
86 CRYP->K3RR = context->
ek[3];
88 else if(context->
nr == 12)
94 CRYP->K1LR = context->
ek[0];
95 CRYP->K1RR = context->
ek[1];
96 CRYP->K2LR = context->
ek[2];
97 CRYP->K2RR = context->
ek[3];
98 CRYP->K3LR = context->
ek[4];
99 CRYP->K3RR = context->
ek[5];
107 CRYP->K0LR = context->
ek[0];
108 CRYP->K0RR = context->
ek[1];
109 CRYP->K1LR = context->
ek[2];
110 CRYP->K1RR = context->
ek[3];
111 CRYP->K2LR = context->
ek[4];
112 CRYP->K2RR = context->
ek[5];
113 CRYP->K3LR = context->
ek[6];
114 CRYP->K3RR = context->
ek[7];
130 uint8_t *output,
size_t length, uint32_t mode)
141 if((mode & CRYP_CR_ALGODIR) != 0)
148 CRYP->CR |= CRYP_CR_CRYPEN;
151 while((CRYP->SR & CRYP_SR_BUSY) != 0)
156 temp = CRYP->CR & ~CRYP_CR_ALGOMODE;
157 CRYP->CR = temp | mode;
178 CRYP->CR |= CRYP_CR_FFLUSH;
180 CRYP->CR |= CRYP_CR_CRYPEN;
186 while((CRYP->SR & CRYP_SR_IFNF) == 0)
191 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
192 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
193 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 8);
194 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 12);
197 while((CRYP->SR & CRYP_SR_OFNE) == 0)
203 __UNALIGNED_UINT32_WRITE(output, temp);
205 __UNALIGNED_UINT32_WRITE(output + 4, temp);
207 __UNALIGNED_UINT32_WRITE(output + 8, temp);
209 __UNALIGNED_UINT32_WRITE(output + 12, temp);
227 while((CRYP->SR & CRYP_SR_IFNF) == 0)
232 CRYP->DIN = buffer[0];
233 CRYP->DIN = buffer[1];
234 CRYP->DIN = buffer[2];
235 CRYP->DIN = buffer[3];
238 while((CRYP->SR & CRYP_SR_OFNE) == 0)
243 buffer[0] = CRYP->DOUT;
244 buffer[1] = CRYP->DOUT;
245 buffer[2] = CRYP->DOUT;
246 buffer[3] = CRYP->DOUT;
287 if(context == NULL || key == NULL)
296 else if(keyLen == 24)
301 else if(keyLen == 32)
316 for(i = 0; i < keyLen; i++)
356 #if (ECB_SUPPORT == ENABLED)
369 const uint8_t *
p, uint8_t *
c,
size_t length)
432 const uint8_t *
c, uint8_t *
p,
size_t length)
485 #if (CBC_SUPPORT == ENABLED)
499 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
575 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
643 #if (CTR_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
658 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
717 for(i = 0; i <
n; i++)
743 #if (GCM_SUPPORT == ENABLED)
759 const uint8_t *
a,
size_t aLen,
const uint8_t *input, uint8_t *output,
760 size_t length, uint8_t *
t, uint32_t mode)
774 temp = CRYP->CR & ~CRYP_CR_ALGOMODE;
778 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
791 CRYP->CR |= CRYP_CR_CRYPEN;
795 while((CRYP->CR & CRYP_CR_CRYPEN) != 0)
800 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
804 CRYP->CR |= CRYP_CR_FFLUSH;
806 CRYP->CR |= CRYP_CR_CRYPEN;
812 while((CRYP->SR & CRYP_SR_IFNF) == 0)
817 CRYP->DIN = __UNALIGNED_UINT32_READ(
a);
818 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 4);
819 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 8);
820 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 12);
834 while((CRYP->SR & CRYP_SR_IFNF) == 0)
839 CRYP->DIN = buffer[0];
840 CRYP->DIN = buffer[1];
841 CRYP->DIN = buffer[2];
842 CRYP->DIN = buffer[3];
847 while((CRYP->SR & CRYP_SR_BUSY) != 0)
852 CRYP->CR &= ~CRYP_CR_CRYPEN;
855 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
859 temp = CRYP->CR & ~CRYP_CR_ALGODIR;
863 CRYP->CR |= CRYP_CR_CRYPEN;
869 while((CRYP->SR & CRYP_SR_IFNF) == 0)
874 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
875 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
876 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 8);
877 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 12);
880 while((CRYP->SR & CRYP_SR_OFNE) == 0)
886 __UNALIGNED_UINT32_WRITE(output, temp);
888 __UNALIGNED_UINT32_WRITE(output + 4, temp);
890 __UNALIGNED_UINT32_WRITE(output + 8, temp);
892 __UNALIGNED_UINT32_WRITE(output + 12, temp);
907 temp = CRYP->CR & ~CRYP_CR_NPBLB;
911 while((CRYP->SR & CRYP_SR_IFNF) == 0)
916 CRYP->DIN = buffer[0];
917 CRYP->DIN = buffer[1];
918 CRYP->DIN = buffer[2];
919 CRYP->DIN = buffer[3];
922 while((CRYP->SR & CRYP_SR_OFNE) == 0)
927 buffer[0] = CRYP->DOUT;
928 buffer[1] = CRYP->DOUT;
929 buffer[2] = CRYP->DOUT;
930 buffer[3] = CRYP->DOUT;
938 while((CRYP->SR & CRYP_SR_BUSY) != 0)
943 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
957 while((CRYP->SR & CRYP_SR_OFNE) == 0)
964 __UNALIGNED_UINT32_WRITE(
t, temp);
966 __UNALIGNED_UINT32_WRITE(
t + 4, temp);
968 __UNALIGNED_UINT32_WRITE(
t + 8, temp);
970 __UNALIGNED_UINT32_WRITE(
t + 12, temp);
992 if(context == NULL || cipherContext == NULL)
1024 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
1025 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
1027 uint8_t authTag[16];
1038 if(tLen < 4 || tLen > 16)
1069 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
1070 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
1074 uint8_t authTag[16];
1085 if(tLen < 4 || tLen > 16)
1090 authTag, CRYP_CR_ALGODIR);
1093 for(
mask = 0, i = 0; i < tLen; i++)
1095 mask |= authTag[i] ^
t[i];
1103 #if (CCM_SUPPORT == ENABLED)
1119 size_t aLen,
const uint8_t *input, uint8_t *output,
size_t length,
1120 uint8_t *
t, uint32_t mode)
1134 temp = CRYP->CR & ~CRYP_CR_ALGOMODE;
1138 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1145 qLen = (b0[0] & 0x07) + 1;
1149 osMemset(buffer + 16 - qLen, 0, qLen);
1152 buffer[0] = (uint8_t) (qLen - 1);
1158 CRYP->IV0RR =
LOAD32BE(buffer + 4);
1159 CRYP->IV1LR =
LOAD32BE(buffer + 8);
1160 CRYP->IV1RR =
LOAD32BE(buffer + 12);
1163 CRYP->CR |= CRYP_CR_CRYPEN;
1173 while((CRYP->CR & CRYP_CR_CRYPEN) != 0)
1178 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1182 CRYP->CR |= CRYP_CR_FFLUSH;
1184 CRYP->CR |= CRYP_CR_CRYPEN;
1200 n =
MIN(aLen, 16 - 2);
1214 n =
MIN(aLen, 16 - 6);
1220 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1225 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1226 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1227 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1228 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1239 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1244 CRYP->DIN = __UNALIGNED_UINT32_READ(
a);
1245 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 4);
1246 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 8);
1247 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 12);
1263 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1268 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1269 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1270 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1271 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1276 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1281 CRYP->CR &= ~CRYP_CR_CRYPEN;
1284 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1288 temp = CRYP->CR & ~CRYP_CR_ALGODIR;
1292 CRYP->CR |= CRYP_CR_CRYPEN;
1298 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1303 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
1304 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
1305 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 8);
1306 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 12);
1309 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1315 __UNALIGNED_UINT32_WRITE(output, temp);
1317 __UNALIGNED_UINT32_WRITE(output + 4, temp);
1319 __UNALIGNED_UINT32_WRITE(output + 8, temp);
1321 __UNALIGNED_UINT32_WRITE(output + 12, temp);
1339 temp = CRYP->CR & ~CRYP_CR_NPBLB;
1343 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1348 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1349 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1350 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1351 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1354 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1360 __UNALIGNED_UINT32_WRITE(buffer, temp);
1362 __UNALIGNED_UINT32_WRITE(buffer + 4, temp);
1364 __UNALIGNED_UINT32_WRITE(buffer + 8, temp);
1366 __UNALIGNED_UINT32_WRITE(buffer + 12, temp);
1375 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1381 temp = CRYP->CR & ~(CRYP_CR_GCM_CCMPH | CRYP_CR_ALGODIR);
1386 osMemset(buffer + 16 - qLen, 0, qLen);
1389 buffer[0] = (uint8_t) (qLen - 1);
1398 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1405 __UNALIGNED_UINT32_WRITE(
t, temp);
1407 __UNALIGNED_UINT32_WRITE(
t + 4, temp);
1409 __UNALIGNED_UINT32_WRITE(
t + 8, temp);
1411 __UNALIGNED_UINT32_WRITE(
t + 12, temp);
1438 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
1439 size_t length, uint8_t *
t,
size_t tLen)
1443 uint8_t authTag[16];
1487 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
1488 size_t length,
const uint8_t *
t,
size_t tLen)
1494 uint8_t authTag[16];
1514 for(
mask = 0, i = 0; i < tLen; i++)
1516 mask |= authTag[i] ^
t[i];