32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "stm32f4xx.h"
36 #include "stm32f4xx_hal.h"
46 #if (STM32F4XX_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 && defined(CRYP_CR_ALGOMODE_AES_GCM))
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)
1298 temp = CRYP->CR & ~CRYP_CR_ALGOMODE;
1302 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1315 CRYP->CR |= CRYP_CR_CRYPEN;
1319 while((CRYP->CR & CRYP_CR_CRYPEN) != 0)
1324 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1328 CRYP->CR |= CRYP_CR_FFLUSH;
1330 CRYP->CR |= CRYP_CR_CRYPEN;
1336 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1341 CRYP->DIN = __UNALIGNED_UINT32_READ(
a);
1342 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 4);
1343 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 8);
1344 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 12);
1358 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1363 CRYP->DIN = buffer[0];
1364 CRYP->DIN = buffer[1];
1365 CRYP->DIN = buffer[2];
1366 CRYP->DIN = buffer[3];
1371 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1376 CRYP->CR &= ~CRYP_CR_CRYPEN;
1379 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1383 temp = CRYP->CR & ~CRYP_CR_ALGODIR;
1387 CRYP->CR |= CRYP_CR_CRYPEN;
1393 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1398 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
1399 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
1400 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 8);
1401 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 12);
1404 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1410 __UNALIGNED_UINT32_WRITE(output, temp);
1412 __UNALIGNED_UINT32_WRITE(output + 4, temp);
1414 __UNALIGNED_UINT32_WRITE(output + 8, temp);
1416 __UNALIGNED_UINT32_WRITE(output + 12, temp);
1431 if((mode & CRYP_CR_ALGODIR) == 0)
1434 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1439 h[0] = CRYP->CSGCM0R;
1440 h[1] = CRYP->CSGCM1R;
1441 h[2] = CRYP->CSGCM2R;
1442 h[3] = CRYP->CSGCM3R;
1446 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1451 CRYP->DIN = buffer[0];
1452 CRYP->DIN = buffer[1];
1453 CRYP->DIN = buffer[2];
1454 CRYP->DIN = buffer[3];
1457 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1462 buffer[0] = CRYP->DOUT;
1463 buffer[1] = CRYP->DOUT;
1464 buffer[2] = CRYP->DOUT;
1465 buffer[3] = CRYP->DOUT;
1471 if((mode & CRYP_CR_ALGODIR) == 0)
1477 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1482 CRYP->CR |= CRYP_CR_ALGODIR;
1485 CRYP->CSGCM0R =
h[0];
1486 CRYP->CSGCM1R =
h[1];
1487 CRYP->CSGCM2R =
h[2];
1488 CRYP->CSGCM3R =
h[3];
1491 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1496 CRYP->DIN = buffer[0];
1497 CRYP->DIN = buffer[1];
1498 CRYP->DIN = buffer[2];
1499 CRYP->DIN = buffer[3];
1502 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1507 buffer[0] = CRYP->DOUT;
1508 buffer[1] = CRYP->DOUT;
1509 buffer[2] = CRYP->DOUT;
1510 buffer[3] = CRYP->DOUT;
1516 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1521 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1535 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1542 __UNALIGNED_UINT32_WRITE(
t, temp);
1544 __UNALIGNED_UINT32_WRITE(
t + 4, temp);
1546 __UNALIGNED_UINT32_WRITE(
t + 8, temp);
1548 __UNALIGNED_UINT32_WRITE(
t + 12, temp);
1567 void *cipherContext)
1570 if(context == NULL || cipherContext == NULL)
1602 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
1603 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
1605 uint8_t authTag[16];
1616 if(tLen < 4 || tLen > 16)
1647 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
1648 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
1652 uint8_t authTag[16];
1663 if(tLen < 4 || tLen > 16)
1668 authTag, CRYP_CR_ALGODIR);
1671 for(
mask = 0, i = 0; i < tLen; i++)
1673 mask |= authTag[i] ^
t[i];
1681 #if (CCM_SUPPORT == ENABLED && AES_SUPPORT == ENABLED && defined(CRYP_CR_ALGOMODE_AES_CCM))
1697 size_t aLen,
const uint8_t *input, uint8_t *output,
size_t length,
1698 uint8_t *
t, uint32_t mode)
1713 temp = CRYP->CR & ~CRYP_CR_ALGOMODE;
1717 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1724 qLen = (b0[0] & 0x07) + 1;
1728 osMemset(buffer + 16 - qLen, 0, qLen);
1731 buffer[0] = (uint8_t) (qLen - 1);
1737 CRYP->IV0RR =
LOAD32BE(buffer + 4);
1738 CRYP->IV1LR =
LOAD32BE(buffer + 8);
1739 CRYP->IV1RR =
LOAD32BE(buffer + 12);
1742 CRYP->CR |= CRYP_CR_CRYPEN;
1745 CRYP->DIN = __UNALIGNED_UINT32_READ(b0);
1746 CRYP->DIN = __UNALIGNED_UINT32_READ(b0 + 4);
1747 CRYP->DIN = __UNALIGNED_UINT32_READ(b0 + 8);
1748 CRYP->DIN = __UNALIGNED_UINT32_READ(b0 + 12);
1752 while((CRYP->CR & CRYP_CR_CRYPEN) != 0)
1757 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1761 CRYP->CR |= CRYP_CR_FFLUSH;
1763 CRYP->CR |= CRYP_CR_CRYPEN;
1779 n =
MIN(aLen, 16 - 2);
1793 n =
MIN(aLen, 16 - 6);
1799 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1804 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1805 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1806 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1807 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1818 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1823 CRYP->DIN = __UNALIGNED_UINT32_READ(
a);
1824 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 4);
1825 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 8);
1826 CRYP->DIN = __UNALIGNED_UINT32_READ(
a + 12);
1842 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1847 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1848 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1849 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1850 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1855 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1860 CRYP->CR &= ~CRYP_CR_CRYPEN;
1863 temp = CRYP->CR & ~CRYP_CR_GCM_CCMPH;
1867 temp = CRYP->CR & ~CRYP_CR_ALGODIR;
1871 CRYP->CR |= CRYP_CR_CRYPEN;
1877 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1882 CRYP->DIN = __UNALIGNED_UINT32_READ(input);
1883 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 4);
1884 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 8);
1885 CRYP->DIN = __UNALIGNED_UINT32_READ(input + 12);
1888 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1894 __UNALIGNED_UINT32_WRITE(output, temp);
1896 __UNALIGNED_UINT32_WRITE(output + 4, temp);
1898 __UNALIGNED_UINT32_WRITE(output + 8, temp);
1900 __UNALIGNED_UINT32_WRITE(output + 12, temp);
1918 if((mode & CRYP_CR_ALGODIR) != 0)
1921 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1926 y[0] = CRYP->CSGCMCCM0R;
1927 y[1] = CRYP->CSGCMCCM1R;
1928 y[2] = CRYP->CSGCMCCM2R;
1929 y[3] = CRYP->CSGCMCCM3R;
1933 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1938 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1939 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1940 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1941 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1944 while((CRYP->SR & CRYP_SR_OFNE) == 0)
1950 __UNALIGNED_UINT32_WRITE(buffer, temp);
1952 __UNALIGNED_UINT32_WRITE(buffer + 4, temp);
1954 __UNALIGNED_UINT32_WRITE(buffer + 8, temp);
1956 __UNALIGNED_UINT32_WRITE(buffer + 12, temp);
1963 if((mode & CRYP_CR_ALGODIR) != 0)
1969 while((CRYP->SR & CRYP_SR_BUSY) != 0)
1974 CRYP->CR &= ~CRYP_CR_ALGODIR;
1977 CRYP->CSGCMCCM0R = y[0];
1978 CRYP->CSGCMCCM1R = y[1];
1979 CRYP->CSGCMCCM2R = y[2];
1980 CRYP->CSGCMCCM3R = y[3];
1983 while((CRYP->SR & CRYP_SR_IFNF) == 0)
1988 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
1989 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
1990 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
1991 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
1994 while((CRYP->SR & CRYP_SR_OFNE) == 0)
2008 while((CRYP->SR & CRYP_SR_BUSY) != 0)
2014 temp = CRYP->CR & ~(CRYP_CR_GCM_CCMPH | CRYP_CR_ALGODIR);
2019 osMemset(buffer + 16 - qLen, 0, qLen);
2022 buffer[0] = (uint8_t) (qLen - 1);
2025 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer);
2026 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 4);
2027 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 8);
2028 CRYP->DIN = __UNALIGNED_UINT32_READ(buffer + 12);
2031 while((CRYP->SR & CRYP_SR_OFNE) == 0)
2038 __UNALIGNED_UINT32_WRITE(
t, temp);
2040 __UNALIGNED_UINT32_WRITE(
t + 4, temp);
2042 __UNALIGNED_UINT32_WRITE(
t + 8, temp);
2044 __UNALIGNED_UINT32_WRITE(
t + 12, temp);
2071 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
2072 size_t length, uint8_t *
t,
size_t tLen)
2076 uint8_t authTag[16];
2120 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
2121 size_t length,
const uint8_t *
t,
size_t tLen)
2127 uint8_t authTag[16];
2147 for(
mask = 0, i = 0; i < tLen; i++)
2149 mask |= authTag[i] ^
t[i];