Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
45 #if (SAMA5D2_CRYPTO_CIPHER_SUPPORT == ENABLED)
59 p = (uint32_t *) input;
60 TDES->TDES_IDATAR[0] =
p[0];
61 TDES->TDES_IDATAR[1] =
p[1];
64 TDES->TDES_CR = TDES_CR_START;
67 while((TDES->TDES_ISR & TDES_ISR_DATRDY) == 0)
72 p = (uint32_t *) output;
73 p[0] = TDES->TDES_ODATAR[0];
74 p[1] = TDES->TDES_ODATAR[1];
78 #if (DES_SUPPORT == ENABLED)
91 uint8_t *output,
size_t length, uint32_t mode)
99 TDES->TDES_CR = TDES_CR_SWRST;
102 TDES->TDES_MR = TDES_MR_SMOD_MANUAL_START | TDES_MR_TDESMOD(0) | mode;
105 TDES->TDES_KEY1WR[0] = context->
ks[0];
106 TDES->TDES_KEY1WR[1] = context->
ks[1];
113 TDES->TDES_IVR[0] =
p[0];
114 TDES->TDES_IVR[1] =
p[1];
161 if(context == NULL || key == NULL)
187 TDES_MR_CIPHER | TDES_MR_OPMOD_ECB);
206 #if (DES3_SUPPORT == ENABLED)
219 uint8_t *output,
size_t length, uint32_t mode)
227 TDES->TDES_CR = TDES_CR_SWRST;
230 TDES->TDES_MR = TDES_MR_SMOD_MANUAL_START | TDES_MR_TDESMOD(1) | mode;
233 TDES->TDES_KEY1WR[0] = context->
k1.
ks[0];
234 TDES->TDES_KEY1WR[1] = context->
k1.
ks[1];
235 TDES->TDES_KEY2WR[0] = context->
k2.
ks[0];
236 TDES->TDES_KEY2WR[1] = context->
k2.
ks[1];
237 TDES->TDES_KEY3WR[0] = context->
k3.
ks[0];
238 TDES->TDES_KEY3WR[1] = context->
k3.
ks[1];
245 TDES->TDES_IVR[0] =
p[0];
246 TDES->TDES_IVR[1] =
p[1];
293 if(context == NULL || key == NULL)
305 else if(keyLen == 16)
314 else if(keyLen == 24)
344 TDES_MR_CIPHER | TDES_MR_OPMOD_ECB);
363 #if (XTEA_SUPPORT == ENABLED)
376 p = (uint32_t *) input;
377 TDES->TDES_IDATAR[1] =
p[0];
378 TDES->TDES_IDATAR[0] =
p[1];
381 TDES->TDES_CR = TDES_CR_START;
384 while((TDES->TDES_ISR & TDES_ISR_DATRDY) == 0)
389 p = (uint32_t *) output;
390 p[0] = TDES->TDES_ODATAR[1];
391 p[1] = TDES->TDES_ODATAR[0];
406 uint8_t *output,
size_t length, uint32_t mode)
414 TDES->TDES_CR = TDES_CR_SWRST;
417 TDES->TDES_MR = TDES_MR_SMOD_MANUAL_START | TDES_MR_TDESMOD(2) | mode;
423 TDES->TDES_KEY2WR[1] = context->
k[0];
424 TDES->TDES_KEY2WR[0] = context->
k[1];
425 TDES->TDES_KEY1WR[1] = context->
k[2];
426 TDES->TDES_KEY1WR[0] = context->
k[3];
433 TDES->TDES_IVR[1] =
p[0];
434 TDES->TDES_IVR[0] =
p[1];
481 if(context == NULL || key == NULL)
507 TDES_MR_CIPHER | TDES_MR_OPMOD_ECB);
526 #if (AES_SUPPORT == ENABLED)
538 temp = AES->AES_MR & ~AES_MR_KEYSIZE_Msk;
541 if(context->
nr == 10)
544 AES->AES_MR = temp | AES_MR_KEYSIZE_AES128;
547 AES->AES_KEYWR[0] = context->
ek[0];
548 AES->AES_KEYWR[1] = context->
ek[1];
549 AES->AES_KEYWR[2] = context->
ek[2];
550 AES->AES_KEYWR[3] = context->
ek[3];
552 else if(context->
nr == 12)
555 AES->AES_MR = temp | AES_MR_KEYSIZE_AES192;
558 AES->AES_KEYWR[0] = context->
ek[0];
559 AES->AES_KEYWR[1] = context->
ek[1];
560 AES->AES_KEYWR[2] = context->
ek[2];
561 AES->AES_KEYWR[3] = context->
ek[3];
562 AES->AES_KEYWR[4] = context->
ek[4];
563 AES->AES_KEYWR[5] = context->
ek[5];
568 AES->AES_MR = temp | AES_MR_KEYSIZE_AES256;
571 AES->AES_KEYWR[0] = context->
ek[0];
572 AES->AES_KEYWR[1] = context->
ek[1];
573 AES->AES_KEYWR[2] = context->
ek[2];
574 AES->AES_KEYWR[3] = context->
ek[3];
575 AES->AES_KEYWR[4] = context->
ek[4];
576 AES->AES_KEYWR[5] = context->
ek[5];
577 AES->AES_KEYWR[6] = context->
ek[6];
578 AES->AES_KEYWR[7] = context->
ek[7];
594 p = (uint32_t *) input;
595 AES->AES_IDATAR[0] =
p[0];
596 AES->AES_IDATAR[1] =
p[1];
597 AES->AES_IDATAR[2] =
p[2];
598 AES->AES_IDATAR[3] =
p[3];
601 AES->AES_CR = AES_CR_START;
604 while((AES->AES_ISR & AES_ISR_DATRDY) == 0)
609 p = (uint32_t *) output;
610 p[0] = AES->AES_ODATAR[0];
611 p[1] = AES->AES_ODATAR[1];
612 p[2] = AES->AES_ODATAR[2];
613 p[3] = AES->AES_ODATAR[3];
628 uint8_t *output,
size_t length, uint32_t mode)
636 AES->AES_CR = AES_CR_SWRST;
639 AES->AES_MR = AES_MR_SMOD_MANUAL_START | mode;
648 AES->AES_IVR[0] =
p[0];
649 AES->AES_IVR[1] =
p[1];
650 AES->AES_IVR[2] =
p[2];
651 AES->AES_IVR[3] =
p[3];
698 if(context == NULL || key == NULL)
707 else if(keyLen == 24)
712 else if(keyLen == 32)
761 #if (ECB_SUPPORT == ENABLED)
774 const uint8_t *
p, uint8_t *
c,
size_t length)
781 #if (DES_SUPPORT == ENABLED)
804 #if (DES3_SUPPORT == ENABLED)
827 #if (XTEA_SUPPORT == ENABLED)
850 #if (AES_SUPPORT == ENABLED)
910 const uint8_t *
c, uint8_t *
p,
size_t length)
917 #if (DES_SUPPORT == ENABLED)
939 #if (DES3_SUPPORT == ENABLED)
961 #if (XTEA_SUPPORT == ENABLED)
983 #if (AES_SUPPORT == ENABLED)
1031 #if (CBC_SUPPORT == ENABLED)
1045 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
1052 #if (DES_SUPPORT == ENABLED)
1078 #if (DES3_SUPPORT == ENABLED)
1104 #if (XTEA_SUPPORT == ENABLED)
1130 #if (AES_SUPPORT == ENABLED)
1166 c[i] =
p[i] ^
iv[i];
1206 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
1213 #if (DES_SUPPORT == ENABLED)
1243 #if (DES3_SUPPORT == ENABLED)
1273 #if (XTEA_SUPPORT == ENABLED)
1303 #if (AES_SUPPORT == ENABLED)
1374 #if (CFB_SUPPORT == ENABLED)
1389 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
1396 #if (DES_SUPPORT == ENABLED)
1408 TDES_MR_OPMOD_CFB | TDES_MR_CFBS_SIZE_64BIT);
1423 #if (DES3_SUPPORT == ENABLED)
1435 TDES_MR_OPMOD_CFB | TDES_MR_CFBS_SIZE_64BIT);
1450 #if (XTEA_SUPPORT == ENABLED)
1462 TDES_MR_OPMOD_CFB | TDES_MR_CFBS_SIZE_64BIT);
1477 #if (AES_SUPPORT == ENABLED)
1489 AES_MR_OPMOD_CFB | AES_MR_CFBS_SIZE_128BIT);
1507 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
1526 for(i = 0; i <
n; i++)
1566 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
1573 #if (DES_SUPPORT == ENABLED)
1585 TDES_MR_CFBS_SIZE_64BIT);
1600 #if (DES3_SUPPORT == ENABLED)
1612 TDES_MR_CFBS_SIZE_64BIT);
1627 #if (XTEA_SUPPORT == ENABLED)
1639 TDES_MR_CFBS_SIZE_64BIT);
1654 #if (AES_SUPPORT == ENABLED)
1666 AES_MR_CFBS_SIZE_128BIT);
1684 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
1707 for(i = 0; i <
n; i++)
1730 #if (OFB_SUPPORT == ENABLED)
1745 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
1752 #if (DES_SUPPORT == ENABLED)
1779 #if (DES3_SUPPORT == ENABLED)
1806 #if (XTEA_SUPPORT == ENABLED)
1833 #if (AES_SUPPORT == ENABLED)
1863 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
1882 for(i = 0; i <
n; i++)
1909 #if (GCM_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
1921 p = (uint32_t *)
data;
1922 AES->AES_IDATAR[0] =
p[0];
1923 AES->AES_IDATAR[1] =
p[1];
1924 AES->AES_IDATAR[2] =
p[2];
1925 AES->AES_IDATAR[3] =
p[3];
1928 AES->AES_CR = AES_CR_START;
1932 while((AES->AES_ISR & AES_ISR_DATRDY) == 0)
1952 const uint8_t *
a,
size_t aLen,
const uint8_t *input, uint8_t *output,
1953 size_t length, uint8_t *
t, uint32_t mode)
1962 AES->AES_CR = AES_CR_SWRST;
1965 if(aLen > 0 ||
length > 0)
1968 AES->AES_MR |= AES_MR_SMOD_MANUAL_START | AES_MR_OPMOD_GCM |
1969 AES_MR_GTAGEN | mode;
1978 while((AES->AES_ISR & AES_ISR_DATRDY) == 0)
1990 AES->AES_AADLENR = aLen;
2042 while((AES->AES_ISR & AES_ISR_TAGRDY) == 0)
2048 temp = AES->AES_TAGR[0];
2050 temp = AES->AES_TAGR[1];
2052 temp = AES->AES_TAGR[2];
2054 temp = AES->AES_TAGR[3];
2060 AES->AES_MR |= AES_MR_SMOD_MANUAL_START | AES_MR_OPMOD_CTR |
2094 void *cipherContext)
2097 if(context == NULL || cipherContext == NULL)
2129 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
2130 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
2132 uint8_t authTag[16];
2143 if(tLen < 4 || tLen > 16)
2148 authTag, AES_MR_CIPHER);
2174 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
2175 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
2179 uint8_t authTag[16];
2190 if(tLen < 4 || tLen > 16)
2198 for(
mask = 0, i = 0; i < tLen; i++)
2200 mask |= authTag[i] ^
t[i];
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 xteaEncryptBlock(XteaContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using XTEA algorithm.
void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using Triple DES algorithm.
CipherAlgoDecryptBlock decryptBlock
OsMutex sama5d2CryptoMutex
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.
Collection of AEAD algorithms.
SAMA5D2 cipher hardware accelerator.
void desDecryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using DES algorithm.
void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using AES algorithm.
CipherAlgoEncryptBlock encryptBlock
void aesProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using AES algorithm.
error_t cfbEncrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CFB encryption.
error_t ofbEncrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
OFB encryption.
error_t xteaInit(XteaContext *context, const uint8_t *key, size_t keyLen)
Initialize a XTEA context using the supplied key.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
#define osMemcpy(dest, src, length)
void xteaProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using XTEA algorithm.
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.
error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
@ ERROR_FAILURE
Generic error code.
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.
void gcmUpdateGhash(const uint8_t *data)
Update GHASH value.
@ ERROR_INVALID_KEY_LENGTH
General definitions for cryptographic algorithms.
Block cipher modes of operation.
const CipherAlgo * cipherAlgo
Cipher algorithm.
error_t cfbDecrypt(const CipherAlgo *cipher, void *context, uint_t s, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CFB decryption.
error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLen)
Initialize a Triple DES context using the supplied key.
void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using Triple DES algorithm.
void xteaDecryptBlock(XteaContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 8-byte block using XTEA algorithm.
error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
Initialize a DES context using the supplied key.
Triple DES algorithm context.
SAMA5D2 hardware cryptographic accelerator.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
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 aesLoadKey(AesContext *context)
Load AES key.
void xteaProcessData(XteaContext *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t mode)
Perform XTEA encryption or decryption.
Common interface for encryption algorithms.
void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
void desProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using DES algorithm.
void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using DES algorithm.
error_t cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
#define osMemset(p, value, length)
error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
void * cipherContext
Cipher algorithm context.
error_t cbcEncrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CBC encryption.
error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
#define osMemmove(dest, src, length)
error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
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.