32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "stm32h7xx.h"
36 #include "stm32h7xx_hal.h"
46 #if (STM32H7XX_CRYPTO_CIPHER_SUPPORT == ENABLED)
57 __HAL_RCC_CRYP_CLK_ENABLE();
64 #if (DES_SUPPORT == ENABLED)
77 uint8_t *output,
size_t length, uint32_t mode)
90 CRYP->K1LR = context->
ks[0];
91 CRYP->K1RR = context->
ks[1];
102 CRYP->CR |= CRYP_CR_FFLUSH;
104 CRYP->CR |= CRYP_CR_CRYPEN;
110 while((CRYP->SR & CRYP_SR_IFNF) == 0)
115 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
116 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
119 while((CRYP->SR & CRYP_SR_OFNE) == 0)
125 __UNALIGNED_UINT32_WRITE(output, temp);
127 __UNALIGNED_UINT32_WRITE(output + 4, temp);
164 if(context == NULL || key == NULL)
210 #if (DES3_SUPPORT == ENABLED)
223 uint8_t *output,
size_t length, uint32_t mode)
236 CRYP->K1LR = context->
k1.
ks[0];
237 CRYP->K1RR = context->
k1.
ks[1];
238 CRYP->K2LR = context->
k2.
ks[0];
239 CRYP->K2RR = context->
k2.
ks[1];
240 CRYP->K3LR = context->
k3.
ks[0];
241 CRYP->K3RR = context->
k3.
ks[1];
252 CRYP->CR |= CRYP_CR_FFLUSH;
254 CRYP->CR |= CRYP_CR_CRYPEN;
260 while((CRYP->SR & CRYP_SR_IFNF) == 0)
265 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
266 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
269 while((CRYP->SR & CRYP_SR_OFNE) == 0)
275 __UNALIGNED_UINT32_WRITE(output, temp);
277 __UNALIGNED_UINT32_WRITE(output + 4, temp);
314 if(context == NULL || key == NULL)
329 else if(keyLen == 16)
341 else if(keyLen == 24)
393 #if (AES_SUPPORT == ENABLED)
405 temp = CRYP->CR & ~CRYP_CR_KEYSIZE;
408 if(context->
nr == 10)
414 CRYP->K2LR = context->
ek[0];
415 CRYP->K2RR = context->
ek[1];
416 CRYP->K3LR = context->
ek[2];
417 CRYP->K3RR = context->
ek[3];
419 else if(context->
nr == 12)
425 CRYP->K1LR = context->
ek[0];
426 CRYP->K1RR = context->
ek[1];
427 CRYP->K2LR = context->
ek[2];
428 CRYP->K2RR = context->
ek[3];
429 CRYP->K3LR = context->
ek[4];
430 CRYP->K3RR = context->
ek[5];
438 CRYP->K0LR = context->
ek[0];
439 CRYP->K0RR = context->
ek[1];
440 CRYP->K1LR = context->
ek[2];
441 CRYP->K1RR = context->
ek[3];
442 CRYP->K2LR = context->
ek[4];
443 CRYP->K2RR = context->
ek[5];
444 CRYP->K3LR = context->
ek[6];
445 CRYP->K3RR = context->
ek[7];
461 uint8_t *output,
size_t length, uint32_t mode)
472 if((mode & CRYP_CR_ALGODIR) != 0)
479 CRYP->CR |= CRYP_CR_CRYPEN;
482 while((CRYP->SR & CRYP_SR_BUSY) != 0)
487 temp = CRYP->CR & ~CRYP_CR_ALGOMODE;
488 CRYP->CR = temp | mode;
509 CRYP->CR |= CRYP_CR_FFLUSH;
511 CRYP->CR |= CRYP_CR_CRYPEN;
517 while((CRYP->SR & CRYP_SR_IFNF) == 0)
522 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
523 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
524 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 8);
525 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 12);
528 while((CRYP->SR & CRYP_SR_OFNE) == 0)
534 __UNALIGNED_UINT32_WRITE(output, temp);
536 __UNALIGNED_UINT32_WRITE(output + 4, temp);
538 __UNALIGNED_UINT32_WRITE(output + 8, temp);
540 __UNALIGNED_UINT32_WRITE(output + 12, temp);
558 while((CRYP->SR & CRYP_SR_IFNF) == 0)
563 CRYP->DIN = buffer[0];
564 CRYP->DIN = buffer[1];
565 CRYP->DIN = buffer[2];
566 CRYP->DIN = buffer[3];
569 while((CRYP->SR & CRYP_SR_OFNE) == 0)
574 buffer[0] = CRYP->DOUT;
575 buffer[1] = CRYP->DOUT;
576 buffer[2] = CRYP->DOUT;
577 buffer[3] = CRYP->DOUT;
618 if(context == NULL || key == NULL)
627 else if(keyLen == 24)
632 else if(keyLen == 32)
647 for(i = 0; i < keyLen; i++)
687 #if (ECB_SUPPORT == ENABLED)
700 const uint8_t *
p, uint8_t *
c,
size_t length)
707 #if (DES_SUPPORT == ENABLED)
729 #if (DES3_SUPPORT == ENABLED)
751 #if (AES_SUPPORT == ENABLED)
810 const uint8_t *
c, uint8_t *
p,
size_t length)
817 #if (DES_SUPPORT == ENABLED)
840 #if (DES3_SUPPORT == ENABLED)
863 #if (AES_SUPPORT == ENABLED)
912 #if (CBC_SUPPORT == ENABLED)
926 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
933 #if (DES_SUPPORT == ENABLED)
955 #if (DES3_SUPPORT == ENABLED)
977 #if (AES_SUPPORT == ENABLED)
1009 c[i] =
p[i] ^
iv[i];
1049 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
1056 #if (DES_SUPPORT == ENABLED)
1079 #if (DES3_SUPPORT == ENABLED)
1102 #if (AES_SUPPORT == ENABLED)
1166 #if (CTR_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
1181 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
1240 for(i = 0; i <
n; i++)
1266 #if (GCM_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
1282 const uint8_t *
a,
size_t aLen,
const uint8_t *input, uint8_t *output,
1283 size_t length, uint8_t *
t, uint32_t mode)
1289 #if !defined (CRYP_VER_2_2)
1300 temp = CRYP->CR & ~CRYP_CR_ALGOMODE;
1304 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1317 CRYP->CR |= CRYP_CR_CRYPEN;
1321 while((CRYP->CR & CRYP_CR_CRYPEN) != 0)
1326 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1330 CRYP->CR |= CRYP_CR_FFLUSH;
1332 CRYP->CR |= CRYP_CR_CRYPEN;
1338 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1343 CRYP->DIN = __UNALIGNED_UINT32_READ(
a);
1344 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 4);
1345 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 8);
1346 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 12);
1360 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1365 CRYP->DIN = buffer[0];
1366 CRYP->DIN = buffer[1];
1367 CRYP->DIN = buffer[2];
1368 CRYP->DIN = buffer[3];
1373 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1378 CRYP->CR &= ~CRYP_CR_CRYPEN;
1381 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1385 temp = CRYP->CR & ~CRYP_CR_ALGODIR;
1389 CRYP->CR |= CRYP_CR_CRYPEN;
1395 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1400 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
1401 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
1402 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 8);
1403 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 12);
1406 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1412 __UNALIGNED_UINT32_WRITE(output, temp);
1414 __UNALIGNED_UINT32_WRITE(output + 4, temp);
1416 __UNALIGNED_UINT32_WRITE(output + 8, temp);
1418 __UNALIGNED_UINT32_WRITE(output + 12, temp);
1432 #if !defined (CRYP_VER_2_2)
1434 if(HAL_GetREVID() < REV_ID_B)
1437 if((mode & CRYP_CR_ALGODIR) == 0)
1440 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1445 h[0] = CRYP->CSGCM0R;
1446 h[1] = CRYP->CSGCM1R;
1447 h[2] = CRYP->CSGCM2R;
1448 h[3] = CRYP->CSGCM3R;
1455 temp = CRYP->CR & ~CRYP_CR_NPBLB;
1460 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1465 CRYP->DIN = buffer[0];
1466 CRYP->DIN = buffer[1];
1467 CRYP->DIN = buffer[2];
1468 CRYP->DIN = buffer[3];
1471 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1476 buffer[0] = CRYP->DOUT;
1477 buffer[1] = CRYP->DOUT;
1478 buffer[2] = CRYP->DOUT;
1479 buffer[3] = CRYP->DOUT;
1484 #if !defined (CRYP_VER_2_2)
1486 if(HAL_GetREVID() < REV_ID_B)
1489 if((mode & CRYP_CR_ALGODIR) == 0)
1495 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1500 CRYP->CR |= CRYP_CR_ALGODIR;
1503 CRYP->CSGCM0R =
h[0];
1504 CRYP->CSGCM1R =
h[1];
1505 CRYP->CSGCM2R =
h[2];
1506 CRYP->CSGCM3R =
h[3];
1509 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1514 CRYP->DIN = buffer[0];
1515 CRYP->DIN = buffer[1];
1516 CRYP->DIN = buffer[2];
1517 CRYP->DIN = buffer[3];
1520 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1525 buffer[0] = CRYP->DOUT;
1526 buffer[1] = CRYP->DOUT;
1527 buffer[2] = CRYP->DOUT;
1528 buffer[3] = CRYP->DOUT;
1536 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1541 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1544 #if !defined (CRYP_VER_2_2)
1546 if(HAL_GetREVID() < REV_ID_B)
1571 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1578 __UNALIGNED_UINT32_WRITE(
t, temp);
1580 __UNALIGNED_UINT32_WRITE(
t + 4, temp);
1582 __UNALIGNED_UINT32_WRITE(
t + 8, temp);
1584 __UNALIGNED_UINT32_WRITE(
t + 12, temp);
1603 void *cipherContext)
1606 if(context == NULL || cipherContext == NULL)
1638 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
1639 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
1641 uint8_t authTag[16];
1652 if(tLen < 4 || tLen > 16)
1683 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
1684 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
1688 uint8_t authTag[16];
1699 if(tLen < 4 || tLen > 16)
1704 authTag, CRYP_CR_ALGODIR);
1707 for(
mask = 0, i = 0; i < tLen; i++)
1709 mask |= authTag[i] ^
t[i];
1717 #if (CCM_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
1733 size_t aLen,
const uint8_t *input, uint8_t *output,
size_t length,
1734 uint8_t *
t, uint32_t mode)
1740 #if !defined (CRYP_VER_2_2)
1751 temp = CRYP->CR & ~CRYP_CR_ALGOMODE;
1755 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1762 qLen = (b0[0] & 0x07) + 1;
1766 osMemset(buffer + 16 - qLen, 0, qLen);
1769 buffer[0] = (uint8_t) (qLen - 1);
1775 CRYP->IV0RR =
LOAD32BE(buffer + 4);
1776 CRYP->IV1LR =
LOAD32BE(buffer + 8);
1777 CRYP->IV1RR =
LOAD32BE(buffer + 12);
1780 CRYP->CR |= CRYP_CR_CRYPEN;
1782 #if !defined (CRYP_VER_2_2)
1784 if(HAL_GetREVID() < REV_ID_B)
1804 while((CRYP->CR & CRYP_CR_CRYPEN) != 0)
1809 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1813 CRYP->CR |= CRYP_CR_FFLUSH;
1815 CRYP->CR |= CRYP_CR_CRYPEN;
1831 n =
MIN(aLen, 16 - 2);
1845 n =
MIN(aLen, 16 - 6);
1851 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1856 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1857 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1858 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1859 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1870 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1875 CRYP->DIN = __UNALIGNED_UINT32_READ(
a);
1876 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 4);
1877 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 8);
1878 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 12);
1894 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1899 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1900 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1901 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1902 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1907 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1912 CRYP->CR &= ~CRYP_CR_CRYPEN;
1915 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1919 temp = CRYP->CR & ~CRYP_CR_ALGODIR;
1923 CRYP->CR |= CRYP_CR_CRYPEN;
1929 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1934 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
1935 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
1936 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 8);
1937 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 12);
1940 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1946 __UNALIGNED_UINT32_WRITE(output, temp);
1948 __UNALIGNED_UINT32_WRITE(output + 4, temp);
1950 __UNALIGNED_UINT32_WRITE(output + 8, temp);
1952 __UNALIGNED_UINT32_WRITE(output + 12, temp);
1969 #if !defined (CRYP_VER_2_2)
1971 if(HAL_GetREVID() < REV_ID_B)
1974 if((mode & CRYP_CR_ALGODIR) != 0)
1977 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1982 y[0] = CRYP->CSGCMCCM0R;
1983 y[1] = CRYP->CSGCMCCM1R;
1984 y[2] = CRYP->CSGCMCCM2R;
1985 y[3] = CRYP->CSGCMCCM3R;
1992 temp = CRYP->CR & ~CRYP_CR_NPBLB;
1997 while((CRYP->SR & CRYP_SR_IFNF) == 0)
2002 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
2003 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
2004 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
2005 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
2008 while((CRYP->SR & CRYP_SR_OFNE) == 0)
2014 __UNALIGNED_UINT32_WRITE(buffer, temp);
2016 __UNALIGNED_UINT32_WRITE(buffer + 4, temp);
2018 __UNALIGNED_UINT32_WRITE(buffer + 8, temp);
2020 __UNALIGNED_UINT32_WRITE(buffer + 12, temp);
2026 #if !defined (CRYP_VER_2_2)
2028 if(HAL_GetREVID() < REV_ID_B)
2031 if((mode & CRYP_CR_ALGODIR) != 0)
2037 while((CRYP->SR & CRYP_SR_BUSY) != 0)
2042 CRYP->CR &= ~CRYP_CR_ALGODIR;
2045 CRYP->CSGCMCCM0R = y[0];
2046 CRYP->CSGCMCCM1R = y[1];
2047 CRYP->CSGCMCCM2R = y[2];
2048 CRYP->CSGCMCCM3R = y[3];
2051 while((CRYP->SR & CRYP_SR_IFNF) == 0)
2056 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
2057 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
2058 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
2059 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
2062 while((CRYP->SR & CRYP_SR_OFNE) == 0)
2078 while((CRYP->SR & CRYP_SR_BUSY) != 0)
2084 temp = CRYP->CR & ~(CRYP_CR_GCM_CCMPH | CRYP_CR_ALGODIR);
2089 osMemset(buffer + 16 - qLen, 0, qLen);
2092 buffer[0] = (uint8_t) (qLen - 1);
2094 #if !defined (CRYP_VER_2_2)
2096 if(HAL_GetREVID() < REV_ID_B)
2115 while((CRYP->SR & CRYP_SR_OFNE) == 0)
2122 __UNALIGNED_UINT32_WRITE(
t, temp);
2124 __UNALIGNED_UINT32_WRITE(
t + 4, temp);
2126 __UNALIGNED_UINT32_WRITE(
t + 8, temp);
2128 __UNALIGNED_UINT32_WRITE(
t + 12, temp);
2155 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
2156 size_t length, uint8_t *
t,
size_t tLen)
2160 uint8_t authTag[16];
2204 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
2205 size_t length,
const uint8_t *
t,
size_t tLen)
2211 uint8_t authTag[16];
2231 for(
mask = 0, i = 0; i < tLen; i++)
2233 mask |= authTag[i] ^
t[i];