32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
45 #if (GD32W5XX_CRYPTO_CIPHER_SUPPORT == ENABLED)
56 rcu_periph_clock_enable(RCU_CAU);
63 #if (DES_SUPPORT == ENABLED)
76 uint8_t *output,
size_t length, uint32_t mode)
84 CAU_CTL = CAU_SWAPPING_8BIT;
89 CAU_KEY1H = context->
ks[0];
90 CAU_KEY1L = context->
ks[1];
101 CAU_CTL |= CAU_CTL_FFLUSH;
103 CAU_CTL |= CAU_CTL_CAUEN;
109 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
114 CAU_DI = __UNALIGNED_UINT32_READ(input);
115 CAU_DI = __UNALIGNED_UINT32_READ(input + 4);
118 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
124 __UNALIGNED_UINT32_WRITE(output, temp);
126 __UNALIGNED_UINT32_WRITE(output + 4, temp);
163 if(context == NULL || key == NULL)
190 CAU_MODE_DES_ECB | CAU_ENCRYPT);
205 CAU_MODE_DES_ECB | CAU_DECRYPT);
209 #if (DES3_SUPPORT == ENABLED)
222 uint8_t *output,
size_t length, uint32_t mode)
230 CAU_CTL = CAU_SWAPPING_8BIT;
235 CAU_KEY1H = context->
k1.
ks[0];
236 CAU_KEY1L = context->
k1.
ks[1];
237 CAU_KEY2H = context->
k2.
ks[0];
238 CAU_KEY2L = context->
k2.
ks[1];
239 CAU_KEY3H = context->
k3.
ks[0];
240 CAU_KEY3L = context->
k3.
ks[1];
251 CAU_CTL |= CAU_CTL_FFLUSH;
253 CAU_CTL |= CAU_CTL_CAUEN;
259 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
264 CAU_DI = __UNALIGNED_UINT32_READ(input);
265 CAU_DI = __UNALIGNED_UINT32_READ(input + 4);
268 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
274 __UNALIGNED_UINT32_WRITE(output, temp);
276 __UNALIGNED_UINT32_WRITE(output + 4, temp);
313 if(context == NULL || key == NULL)
328 else if(keyLen == 16)
340 else if(keyLen == 24)
373 CAU_MODE_TDES_ECB | CAU_ENCRYPT);
388 CAU_MODE_TDES_ECB | CAU_DECRYPT);
392 #if (AES_SUPPORT == ENABLED)
404 temp = CAU_CTL & ~CAU_CTL_KEYM;
407 if(context->
nr == 10)
410 CAU_CTL = temp | CAU_KEYSIZE_128BIT;
413 CAU_KEY2H = context->
ek[0];
414 CAU_KEY2L = context->
ek[1];
415 CAU_KEY3H = context->
ek[2];
416 CAU_KEY3L = context->
ek[3];
418 else if(context->
nr == 12)
421 CAU_CTL = temp | CAU_KEYSIZE_192BIT;
424 CAU_KEY1H = context->
ek[0];
425 CAU_KEY1L = context->
ek[1];
426 CAU_KEY2H = context->
ek[2];
427 CAU_KEY2L = context->
ek[3];
428 CAU_KEY3H = context->
ek[4];
429 CAU_KEY3L = context->
ek[5];
434 CAU_CTL = temp | CAU_KEYSIZE_256BIT;
437 CAU_KEY0H = context->
ek[0];
438 CAU_KEY0L = context->
ek[1];
439 CAU_KEY1H = context->
ek[2];
440 CAU_KEY1L = context->
ek[3];
441 CAU_KEY2H = context->
ek[4];
442 CAU_KEY2L = context->
ek[5];
443 CAU_KEY3H = context->
ek[6];
444 CAU_KEY3L = context->
ek[7];
460 uint8_t *output,
size_t length, uint32_t mode)
468 CAU_CTL = CAU_SWAPPING_8BIT;
471 if((mode & CAU_CTL_CAUDIR) != 0)
474 CAU_CTL |= CAU_MODE_AES_KEY | CAU_CTL_CAUDIR;
478 CAU_CTL |= CAU_CTL_CAUEN;
481 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
486 temp = CAU_CTL & ~CAU_CTL_ALGM;
487 CAU_CTL = temp | mode;
508 CAU_CTL |= CAU_CTL_FFLUSH;
510 CAU_CTL |= CAU_CTL_CAUEN;
516 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
521 CAU_DI = __UNALIGNED_UINT32_READ(input);
522 CAU_DI = __UNALIGNED_UINT32_READ(input + 4);
523 CAU_DI = __UNALIGNED_UINT32_READ(input + 8);
524 CAU_DI = __UNALIGNED_UINT32_READ(input + 12);
527 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
533 __UNALIGNED_UINT32_WRITE(output, temp);
535 __UNALIGNED_UINT32_WRITE(output + 4, temp);
537 __UNALIGNED_UINT32_WRITE(output + 8, temp);
539 __UNALIGNED_UINT32_WRITE(output + 12, temp);
557 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
568 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
617 if(context == NULL || key == NULL)
626 else if(keyLen == 24)
631 else if(keyLen == 32)
646 for(i = 0; i < keyLen; i++)
667 CAU_MODE_AES_ECB | CAU_ENCRYPT);
682 CAU_MODE_AES_ECB | CAU_DECRYPT);
686 #if (ECB_SUPPORT == ENABLED)
699 const uint8_t *
p, uint8_t *
c,
size_t length)
706 #if (DES_SUPPORT == ENABLED)
729 #if (DES3_SUPPORT == ENABLED)
752 #if (AES_SUPPORT == ENABLED)
812 const uint8_t *
c, uint8_t *
p,
size_t length)
819 #if (DES_SUPPORT == ENABLED)
842 #if (DES3_SUPPORT == ENABLED)
865 #if (AES_SUPPORT == ENABLED)
914 #if (CBC_SUPPORT == ENABLED)
928 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
935 #if (DES_SUPPORT == ENABLED)
958 #if (DES3_SUPPORT == ENABLED)
981 #if (AES_SUPPORT == ENABLED)
1014 c[i] =
p[i] ^
iv[i];
1054 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
1061 #if (DES_SUPPORT == ENABLED)
1084 #if (DES3_SUPPORT == ENABLED)
1107 #if (AES_SUPPORT == ENABLED)
1171 #if (CTR_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
1186 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
1247 for(i = 0; i <
n; i++)
1273 #if (GCM_SUPPORT == ENABLED && AES_SUPPORT == ENABLED && defined(CAU_MODE_AES_GCM))
1289 const uint8_t *
a,
size_t aLen,
const uint8_t *input, uint8_t *output,
1290 size_t length, uint8_t *
t, uint32_t mode)
1302 CAU_CTL = CAU_SWAPPING_8BIT;
1305 temp = CAU_CTL & ~CAU_CTL_ALGM;
1306 CAU_CTL = temp | CAU_MODE_AES_GCM;
1309 temp = CAU_CTL & ~CAU_CTL_GCM_CCMPH;
1310 CAU_CTL = temp | CAU_PREPARE_PHASE;
1322 CAU_CTL |= CAU_CTL_CAUEN;
1326 while((CAU_CTL & CAU_CTL_CAUEN) != 0)
1331 temp = CAU_CTL & ~CAU_CTL_GCM_CCMPH;
1332 CAU_CTL = temp | CAU_AAD_PHASE;
1335 CAU_CTL |= CAU_CTL_FFLUSH;
1337 CAU_CTL |= CAU_CTL_CAUEN;
1343 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1348 CAU_DI = __UNALIGNED_UINT32_READ(
a);
1349 CAU_DI = __UNALIGNED_UINT32_READ(
a + 4);
1350 CAU_DI = __UNALIGNED_UINT32_READ(
a + 8);
1351 CAU_DI = __UNALIGNED_UINT32_READ(
a + 12);
1365 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1378 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
1383 CAU_CTL &= ~CAU_CTL_CAUEN;
1387 temp = CAU_CTL & ~CAU_CTL_GCM_CCMPH;
1388 CAU_CTL = temp | CAU_ENCRYPT_DECRYPT_PHASE;
1391 temp = CAU_CTL & ~CAU_DECRYPT;
1395 CAU_CTL |= CAU_CTL_CAUEN;
1401 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1406 CAU_DI = __UNALIGNED_UINT32_READ(input);
1407 CAU_DI = __UNALIGNED_UINT32_READ(input + 4);
1408 CAU_DI = __UNALIGNED_UINT32_READ(input + 8);
1409 CAU_DI = __UNALIGNED_UINT32_READ(input + 12);
1412 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
1418 __UNALIGNED_UINT32_WRITE(output, temp);
1420 __UNALIGNED_UINT32_WRITE(output + 4, temp);
1422 __UNALIGNED_UINT32_WRITE(output + 8, temp);
1424 __UNALIGNED_UINT32_WRITE(output + 12, temp);
1439 if((mode & CAU_CTL_CAUDIR) == 0)
1442 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
1447 h[0] = CAU_GCMCTXSx(0);
1448 h[1] = CAU_GCMCTXSx(1);
1449 h[2] = CAU_GCMCTXSx(2);
1450 h[3] = CAU_GCMCTXSx(3);
1454 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1465 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
1479 if((mode & CAU_CTL_CAUDIR) == 0)
1485 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
1490 CAU_CTL |= CAU_DECRYPT;
1493 CAU_GCMCTXSx(0) =
h[0];
1494 CAU_GCMCTXSx(1) =
h[1];
1495 CAU_GCMCTXSx(2) =
h[2];
1496 CAU_GCMCTXSx(3) =
h[3];
1499 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1510 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
1524 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
1529 temp = CAU_CTL & ~CAU_CTL_GCM_CCMPH;
1530 CAU_CTL = temp | CAU_TAG_PHASE;
1543 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
1550 __UNALIGNED_UINT32_WRITE(
t, temp);
1552 __UNALIGNED_UINT32_WRITE(
t + 4, temp);
1554 __UNALIGNED_UINT32_WRITE(
t + 8, temp);
1556 __UNALIGNED_UINT32_WRITE(
t + 12, temp);
1575 void *cipherContext)
1606 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
1607 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
1609 uint8_t authTag[16];
1620 if(tLen < 4 || tLen > 16)
1651 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
1652 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
1656 uint8_t authTag[16];
1667 if(tLen < 4 || tLen > 16)
1672 authTag, CAU_DECRYPT);
1675 for(
mask = 0, i = 0; i < tLen; i++)
1677 mask |= authTag[i] ^
t[i];
1685 #if (CCM_SUPPORT == ENABLED && AES_SUPPORT == ENABLED && defined(CAU_MODE_AES_CCM))
1701 size_t aLen,
const uint8_t *input, uint8_t *output,
size_t length,
1702 uint8_t *
t, uint32_t mode)
1714 CAU_CTL = CAU_SWAPPING_8BIT;
1717 temp = CAU_CTL & ~CAU_CTL_ALGM;
1718 CAU_CTL = temp | CAU_MODE_AES_CCM;
1721 temp = CAU_CTL & ~CAU_CTL_GCM_CCMPH;
1722 CAU_CTL = temp | CAU_PREPARE_PHASE;
1728 qLen = (b0[0] & 0x07) + 1;
1732 osMemset(buffer + 16 - qLen, 0, qLen);
1735 buffer[0] = (uint8_t) (qLen - 1);
1746 CAU_CTL |= CAU_CTL_CAUEN;
1749 CAU_DI = __UNALIGNED_UINT32_READ(b0);
1750 CAU_DI = __UNALIGNED_UINT32_READ(b0 + 4);
1751 CAU_DI = __UNALIGNED_UINT32_READ(b0 + 8);
1752 CAU_DI = __UNALIGNED_UINT32_READ(b0 + 12);
1756 while((CAU_CTL & CAU_CTL_CAUEN) != 0)
1761 temp = CAU_CTL & ~CAU_CTL_GCM_CCMPH;
1762 CAU_CTL = temp | CAU_AAD_PHASE;
1765 CAU_CTL |= CAU_CTL_FFLUSH;
1767 CAU_CTL |= CAU_CTL_CAUEN;
1783 n =
MIN(aLen, 16 - 2);
1797 n =
MIN(aLen, 16 - 6);
1803 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1808 CAU_DI = __UNALIGNED_UINT32_READ(buffer);
1809 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 4);
1810 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 8);
1811 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 12);
1822 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1827 CAU_DI = __UNALIGNED_UINT32_READ(
a);
1828 CAU_DI = __UNALIGNED_UINT32_READ(
a + 4);
1829 CAU_DI = __UNALIGNED_UINT32_READ(
a + 8);
1830 CAU_DI = __UNALIGNED_UINT32_READ(
a + 12);
1846 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1851 CAU_DI = __UNALIGNED_UINT32_READ(buffer);
1852 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 4);
1853 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 8);
1854 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 12);
1859 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
1864 CAU_CTL &= ~CAU_CTL_CAUEN;
1868 temp = CAU_CTL & ~CAU_CTL_GCM_CCMPH;
1869 CAU_CTL = temp | CAU_ENCRYPT_DECRYPT_PHASE;
1872 temp = CAU_CTL & ~CAU_DECRYPT;
1876 CAU_CTL |= CAU_CTL_CAUEN;
1882 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1887 CAU_DI = __UNALIGNED_UINT32_READ(input);
1888 CAU_DI = __UNALIGNED_UINT32_READ(input + 4);
1889 CAU_DI = __UNALIGNED_UINT32_READ(input + 8);
1890 CAU_DI = __UNALIGNED_UINT32_READ(input + 12);
1893 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
1899 __UNALIGNED_UINT32_WRITE(output, temp);
1901 __UNALIGNED_UINT32_WRITE(output + 4, temp);
1903 __UNALIGNED_UINT32_WRITE(output + 8, temp);
1905 __UNALIGNED_UINT32_WRITE(output + 12, temp);
1923 if((mode & CAU_CTL_CAUDIR) != 0)
1926 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
1931 y[0] = CAU_GCMCCMCTXSx(0);
1932 y[1] = CAU_GCMCCMCTXSx(1);
1933 y[2] = CAU_GCMCCMCTXSx(2);
1934 y[3] = CAU_GCMCCMCTXSx(3);
1938 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1943 CAU_DI = __UNALIGNED_UINT32_READ(buffer);
1944 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 4);
1945 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 8);
1946 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 12);
1949 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
1955 __UNALIGNED_UINT32_WRITE(buffer, temp);
1957 __UNALIGNED_UINT32_WRITE(buffer + 4, temp);
1959 __UNALIGNED_UINT32_WRITE(buffer + 8, temp);
1961 __UNALIGNED_UINT32_WRITE(buffer + 12, temp);
1968 if((mode & CAU_CTL_CAUDIR) != 0)
1974 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
1979 CAU_CTL &= ~CAU_DECRYPT;
1982 CAU_GCMCCMCTXSx(0) = y[0];
1983 CAU_GCMCCMCTXSx(1) = y[1];
1984 CAU_GCMCCMCTXSx(2) = y[2];
1985 CAU_GCMCCMCTXSx(3) = y[3];
1988 while((CAU_STAT0 & CAU_STAT0_INF) == 0)
1993 CAU_DI = __UNALIGNED_UINT32_READ(buffer);
1994 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 4);
1995 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 8);
1996 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 12);
1999 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
2013 while((CAU_STAT0 & CAU_STAT0_BUSY) != 0)
2018 temp = CAU_CTL & ~(CAU_CTL_GCM_CCMPH | CAU_DECRYPT);
2019 CAU_CTL = temp | CAU_TAG_PHASE;
2023 osMemset(buffer + 16 - qLen, 0, qLen);
2026 buffer[0] = (uint8_t) (qLen - 1);
2029 CAU_DI = __UNALIGNED_UINT32_READ(buffer);
2030 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 4);
2031 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 8);
2032 CAU_DI = __UNALIGNED_UINT32_READ(buffer + 12);
2035 while((CAU_STAT0 & CAU_STAT0_ONE) == 0)
2042 __UNALIGNED_UINT32_WRITE(
t, temp);
2044 __UNALIGNED_UINT32_WRITE(
t + 4, temp);
2046 __UNALIGNED_UINT32_WRITE(
t + 8, temp);
2048 __UNALIGNED_UINT32_WRITE(
t + 12, temp);
2075 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
2076 size_t length, uint8_t *
t,
size_t tLen)
2080 uint8_t authTag[16];
2083 if(cipher == NULL || context == NULL)
2124 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
2125 size_t length,
const uint8_t *
t,
size_t tLen)
2131 uint8_t authTag[16];
2134 if(cipher == NULL || context == NULL)
2151 for(
mask = 0, i = 0; i < tLen; i++)
2153 mask |= authTag[i] ^
t[i];