Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "stm32mp13xx.h"
36 #include "stm32mp13xx_hal.h"
46 #if (STM32MP13XX_CRYPTO_CIPHER_SUPPORT == ENABLED)
57 __HAL_RCC_CRYP1_CLK_ENABLE();
64 #if (DES_SUPPORT == ENABLED)
77 uint8_t *output,
size_t length, uint32_t mode)
90 CRYP1->K1LR = context->
ks[0];
91 CRYP1->K1RR = context->
ks[1];
102 CRYP1->CR |= CRYP_CR_FFLUSH;
104 CRYP1->CR |= CRYP_CR_CRYPEN;
110 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
119 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
164 if(context == NULL || key == NULL)
210 #if (DES3_SUPPORT == ENABLED)
223 uint8_t *output,
size_t length, uint32_t mode)
236 CRYP1->K1LR = context->
k1.
ks[0];
237 CRYP1->K1RR = context->
k1.
ks[1];
238 CRYP1->K2LR = context->
k2.
ks[0];
239 CRYP1->K2RR = context->
k2.
ks[1];
240 CRYP1->K3LR = context->
k3.
ks[0];
241 CRYP1->K3RR = context->
k3.
ks[1];
252 CRYP1->CR |= CRYP_CR_FFLUSH;
254 CRYP1->CR |= CRYP_CR_CRYPEN;
260 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
269 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
314 if(context == NULL || key == NULL)
329 else if(keyLen == 16)
341 else if(keyLen == 24)
393 #if (AES_SUPPORT == ENABLED)
405 temp = CRYP1->CR & ~CRYP_CR_KEYSIZE;
408 if(context->
nr == 10)
414 CRYP1->K2LR = context->
ek[0];
415 CRYP1->K2RR = context->
ek[1];
416 CRYP1->K3LR = context->
ek[2];
417 CRYP1->K3RR = context->
ek[3];
419 else if(context->
nr == 12)
425 CRYP1->K1LR = context->
ek[0];
426 CRYP1->K1RR = context->
ek[1];
427 CRYP1->K2LR = context->
ek[2];
428 CRYP1->K2RR = context->
ek[3];
429 CRYP1->K3LR = context->
ek[4];
430 CRYP1->K3RR = context->
ek[5];
438 CRYP1->K0LR = context->
ek[0];
439 CRYP1->K0RR = context->
ek[1];
440 CRYP1->K1LR = context->
ek[2];
441 CRYP1->K1RR = context->
ek[3];
442 CRYP1->K2LR = context->
ek[4];
443 CRYP1->K2RR = context->
ek[5];
444 CRYP1->K3LR = context->
ek[6];
445 CRYP1->K3RR = context->
ek[7];
461 uint8_t *output,
size_t length, uint32_t mode)
472 if((mode & CRYP_CR_ALGODIR) != 0)
479 CRYP1->CR |= CRYP_CR_CRYPEN;
482 while((CRYP1->SR & CRYP_SR_BUSY) != 0)
487 temp = CRYP1->CR & ~CRYP_CR_ALGOMODE;
488 CRYP1->CR = temp | mode;
509 CRYP1->CR |= CRYP_CR_FFLUSH;
511 CRYP1->CR |= CRYP_CR_CRYPEN;
517 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
528 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
558 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
563 CRYP1->DIN = buffer[0];
564 CRYP1->DIN = buffer[1];
565 CRYP1->DIN = buffer[2];
566 CRYP1->DIN = buffer[3];
569 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
574 buffer[0] = CRYP1->DOUT;
575 buffer[1] = CRYP1->DOUT;
576 buffer[2] = CRYP1->DOUT;
577 buffer[3] = CRYP1->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)
1297 temp = CRYP1->CR & ~CRYP_CR_ALGOMODE;
1301 temp = CRYP1->CR & ~CRYP_CR_GCM_CCMPH;
1314 CRYP1->CR |= CRYP_CR_CRYPEN;
1318 while((CRYP1->CR & CRYP_CR_CRYPEN) != 0)
1323 temp = CRYP1->CR & ~CRYP_CR_GCM_CCMPH;
1327 CRYP1->CR |= CRYP_CR_FFLUSH;
1329 CRYP1->CR |= CRYP_CR_CRYPEN;
1335 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1357 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1362 CRYP1->DIN = buffer[0];
1363 CRYP1->DIN = buffer[1];
1364 CRYP1->DIN = buffer[2];
1365 CRYP1->DIN = buffer[3];
1370 while((CRYP1->SR & CRYP_SR_BUSY) != 0)
1375 CRYP1->CR &= ~CRYP_CR_CRYPEN;
1378 temp = CRYP1->CR & ~CRYP_CR_GCM_CCMPH;
1382 temp = CRYP1->CR & ~CRYP_CR_ALGODIR;
1386 CRYP1->CR |= CRYP_CR_CRYPEN;
1392 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1403 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
1430 temp = CRYP1->CR & ~CRYP_CR_NPBLB;
1434 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1439 CRYP1->DIN = buffer[0];
1440 CRYP1->DIN = buffer[1];
1441 CRYP1->DIN = buffer[2];
1442 CRYP1->DIN = buffer[3];
1445 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
1450 buffer[0] = CRYP1->DOUT;
1451 buffer[1] = CRYP1->DOUT;
1452 buffer[2] = CRYP1->DOUT;
1453 buffer[3] = CRYP1->DOUT;
1461 while((CRYP1->SR & CRYP_SR_BUSY) != 0)
1466 temp = CRYP1->CR & ~CRYP_CR_GCM_CCMPH;
1480 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
1512 void *cipherContext)
1515 if(context == NULL || cipherContext == NULL)
1547 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
1548 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
1550 uint8_t authTag[16];
1561 if(tLen < 4 || tLen > 16)
1592 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
1593 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
1597 uint8_t authTag[16];
1608 if(tLen < 4 || tLen > 16)
1613 authTag, CRYP_CR_ALGODIR);
1616 for(
mask = 0, i = 0; i < tLen; i++)
1618 mask |= authTag[i] ^
t[i];
1626 #if (CCM_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
1642 size_t aLen,
const uint8_t *input, uint8_t *output,
size_t length,
1643 uint8_t *
t, uint32_t mode)
1657 temp = CRYP1->CR & ~CRYP_CR_ALGOMODE;
1661 temp = CRYP1->CR & ~CRYP_CR_GCM_CCMPH;
1668 qLen = (b0[0] & 0x07) + 1;
1672 osMemset(buffer + 16 - qLen, 0, qLen);
1675 buffer[0] = (uint8_t) (qLen - 1);
1681 CRYP1->IV0RR =
LOAD32BE(buffer + 4);
1682 CRYP1->IV1LR =
LOAD32BE(buffer + 8);
1683 CRYP1->IV1RR =
LOAD32BE(buffer + 12);
1686 CRYP1->CR |= CRYP_CR_CRYPEN;
1696 while((CRYP1->CR & CRYP_CR_CRYPEN) != 0)
1701 temp = CRYP1->CR & ~CRYP_CR_GCM_CCMPH;
1705 CRYP1->CR |= CRYP_CR_FFLUSH;
1707 CRYP1->CR |= CRYP_CR_CRYPEN;
1723 n =
MIN(aLen, 16 - 2);
1737 n =
MIN(aLen, 16 - 6);
1743 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1751 CRYP1->DIN =
LOAD32LE(buffer + 12);
1762 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1786 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1794 CRYP1->DIN =
LOAD32LE(buffer + 12);
1799 while((CRYP1->SR & CRYP_SR_BUSY) != 0)
1804 CRYP1->CR &= ~CRYP_CR_CRYPEN;
1807 temp = CRYP1->CR & ~CRYP_CR_GCM_CCMPH;
1811 temp = CRYP1->CR & ~CRYP_CR_ALGODIR;
1815 CRYP1->CR |= CRYP_CR_CRYPEN;
1821 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1832 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
1862 temp = CRYP1->CR & ~CRYP_CR_NPBLB;
1866 while((CRYP1->SR & CRYP_SR_IFNF) == 0)
1874 CRYP1->DIN =
LOAD32LE(buffer + 12);
1877 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
1898 while((CRYP1->SR & CRYP_SR_BUSY) != 0)
1904 temp = CRYP1->CR & ~(CRYP_CR_GCM_CCMPH | CRYP_CR_ALGODIR);
1909 osMemset(buffer + 16 - qLen, 0, qLen);
1912 buffer[0] = (uint8_t) (qLen - 1);
1918 CRYP1->DIN =
LOAD32BE(buffer + 12);
1921 while((CRYP1->SR & CRYP_SR_OFNE) == 0)
1961 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
1962 size_t length, uint8_t *
t,
size_t tLen)
1966 uint8_t authTag[16];
2010 size_t nLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
2011 size_t length,
const uint8_t *
t,
size_t tLen)
2017 uint8_t authTag[16];
2037 for(
mask = 0, i = 0; i < tLen; i++)
2039 mask |= authTag[i] ^
t[i];
OsMutex stm32mp13xxCryptoMutex
void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using DES algorithm.
CipherAlgoDecryptBlock decryptBlock
error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
Collection of AEAD algorithms.
#define CRYP_CR_ALGOMODE_DES_ECB
error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
#define CRYP_CR_GCM_CCMPH_INIT
#define CRYP_CR_GCM_CCMPH_PAYLOAD
#define CRYP_CR_KEYSIZE_128B
#define CRYP_CR_DATATYPE_8B
error_t gcmDecrypt(GcmContext *context, const uint8_t *iv, size_t ivLen, const uint8_t *a, size_t aLen, const uint8_t *c, uint8_t *p, size_t length, const uint8_t *t, size_t tLen)
Authenticated decryption using GCM.
void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using Triple DES algorithm.
#define CRYP_CR_ALGOMODE_AES_ECB
CipherAlgoEncryptBlock encryptBlock
void ccmProcessData(AesContext *context, const uint8_t *b0, const uint8_t *a, size_t aLen, const uint8_t *input, uint8_t *output, size_t length, uint8_t *t, uint32_t mode)
Perform AES-CCM encryption or decryption.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
#define osMemcpy(dest, src, length)
#define CRYP_CR_GCM_CCMPH_FINAL
error_t ccmDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *n, size_t nLen, const uint8_t *a, size_t aLen, const uint8_t *c, uint8_t *p, size_t length, const uint8_t *t, size_t tLen)
Authenticated decryption using CCM.
@ ERROR_FAILURE
Generic error code.
#define CRYP_CR_ALGOMODE_AES_CCM
#define CRYP_CR_ALGOMODE_AES_CBC
error_t ccmFormatBlock0(size_t q, const uint8_t *n, size_t nLen, size_t aLen, size_t tLen, uint8_t *b)
Format first block B(0)
error_t crypInit(void)
CRYP module initialization.
error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
Initialize a DES context using the supplied key.
#define CRYP_CR_GCM_CCMPH_HEADER
@ ERROR_INVALID_KEY_LENGTH
General definitions for cryptographic algorithms.
#define CRYP_CR_ALGOMODE_TDES_ECB
void aesLoadKey(AesContext *context)
Load AES key.
error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
Initialize a Triple DES context using the supplied key.
Block cipher modes of operation.
void des3ProcessData(Des3Context *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t mode)
Perform Triple DES encryption or decryption.
const CipherAlgo * cipherAlgo
Cipher algorithm.
error_t gcmEncrypt(GcmContext *context, const uint8_t *iv, size_t ivLen, const uint8_t *a, size_t aLen, const uint8_t *p, uint8_t *c, size_t length, uint8_t *t, size_t tLen)
Authenticated encryption using GCM.
STM32MP13 hardware cryptographic accelerator.
error_t ccmEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *n, size_t nLen, const uint8_t *a, size_t aLen, const uint8_t *p, uint8_t *c, size_t length, uint8_t *t, size_t tLen)
Authenticated encryption using CCM.
#define CRYP_CR_ALGOMODE_AES_CTR
error_t cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using DES algorithm.
Triple DES algorithm context.
void gcmProcessData(AesContext *context, const uint8_t *iv, const uint8_t *a, size_t aLen, const uint8_t *input, uint8_t *output, size_t length, uint8_t *t, uint32_t mode)
Perform AES-GCM encryption or decryption.
#define CRYP_CR_ALGOMODE_DES_CBC
#define CRYP_CR_KEYSIZE_256B
error_t cbcEncrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CBC encryption.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
#define CRYP_CR_ALGOMODE_AES_GCM
Common interface for encryption algorithms.
void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
#define CRYP_CR_ALGOMODE_TDES_CBC
void aesProcessData(AesContext *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t mode)
Perform AES encryption or decryption.
void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using AES algorithm.
#define osMemset(p, value, length)
STM32MP13 cipher hardware accelerator.
void * cipherContext
Cipher algorithm context.
#define CRYP_CR_ALGOMODE_AES_KEY
void ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
Increment counter block.
void desProcessData(DesContext *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t mode)
Perform DES encryption or decryption.
void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using Triple DES algorithm.
error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
#define CRYP_CR_KEYSIZE_192B
error_t ctrEncrypt(const CipherAlgo *cipher, void *context, uint_t m, uint8_t *t, const uint8_t *p, uint8_t *c, size_t length)
CTR encryption.