Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
45 #if (PIC32CX_MT_CRYPTO_CIPHER_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
58 temp = AES_REGS->AES_MR & ~AES_MR_KEYSIZE_Msk;
64 AES_REGS->AES_MR = temp | AES_MR_KEYSIZE_AES128;
67 AES_REGS->AES_KEYWR[0] = context->
ek[0];
68 AES_REGS->AES_KEYWR[1] = context->
ek[1];
69 AES_REGS->AES_KEYWR[2] = context->
ek[2];
70 AES_REGS->AES_KEYWR[3] = context->
ek[3];
72 else if(context->
nr == 12)
75 AES_REGS->AES_MR = temp | AES_MR_KEYSIZE_AES192;
78 AES_REGS->AES_KEYWR[0] = context->
ek[0];
79 AES_REGS->AES_KEYWR[1] = context->
ek[1];
80 AES_REGS->AES_KEYWR[2] = context->
ek[2];
81 AES_REGS->AES_KEYWR[3] = context->
ek[3];
82 AES_REGS->AES_KEYWR[4] = context->
ek[4];
83 AES_REGS->AES_KEYWR[5] = context->
ek[5];
88 AES_REGS->AES_MR = temp | AES_MR_KEYSIZE_AES256;
91 AES_REGS->AES_KEYWR[0] = context->
ek[0];
92 AES_REGS->AES_KEYWR[1] = context->
ek[1];
93 AES_REGS->AES_KEYWR[2] = context->
ek[2];
94 AES_REGS->AES_KEYWR[3] = context->
ek[3];
95 AES_REGS->AES_KEYWR[4] = context->
ek[4];
96 AES_REGS->AES_KEYWR[5] = context->
ek[5];
97 AES_REGS->AES_KEYWR[6] = context->
ek[6];
98 AES_REGS->AES_KEYWR[7] = context->
ek[7];
114 p = (uint32_t *) input;
115 AES_REGS->AES_IDATAR[0] =
p[0];
116 AES_REGS->AES_IDATAR[1] =
p[1];
117 AES_REGS->AES_IDATAR[2] =
p[2];
118 AES_REGS->AES_IDATAR[3] =
p[3];
121 while((AES_REGS->AES_ISR & AES_ISR_DATRDY_Msk) == 0)
126 p = (uint32_t *) output;
127 p[0] = AES_REGS->AES_ODATAR[0];
128 p[1] = AES_REGS->AES_ODATAR[1];
129 p[2] = AES_REGS->AES_ODATAR[2];
130 p[3] = AES_REGS->AES_ODATAR[3];
145 uint8_t *output,
size_t length, uint32_t mode)
153 AES_REGS->AES_CR = AES_CR_SWRST_Msk;
156 AES_REGS->AES_MR = AES_MR_SMOD_AUTO_START | mode;
165 AES_REGS->AES_IVR[0] =
p[0];
166 AES_REGS->AES_IVR[1] =
p[1];
167 AES_REGS->AES_IVR[2] =
p[2];
168 AES_REGS->AES_IVR[3] =
p[3];
215 if(context == NULL || key == NULL)
224 else if(keyLen == 24)
229 else if(keyLen == 32)
259 AES_MR_CIPHER_Msk | AES_MR_OPMOD_ECB);
278 #if (ECB_SUPPORT == ENABLED)
291 const uint8_t *
p, uint8_t *
c,
size_t length)
355 const uint8_t *
c, uint8_t *
p,
size_t length)
407 #if (CBC_SUPPORT == ENABLED)
421 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
501 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
576 #if (CFB_SUPPORT == ENABLED)
591 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
609 AES_MR_OPMOD_CFB | AES_MR_CFBS_SIZE_128BIT);
625 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
644 for(i = 0; i <
n; i++)
684 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
702 AES_MR_CFBS_SIZE_128BIT);
718 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
741 for(i = 0; i <
n; i++)
764 #if (OFB_SUPPORT == ENABLED)
779 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
813 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
832 for(i = 0; i <
n; i++)
859 #if (CTR_SUPPORT == ENABLED)
874 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
931 for(i = 0; i <
n; i++)
957 #if (GCM_SUPPORT == ENABLED)
969 p = (uint32_t *)
data;
970 AES_REGS->AES_IDATAR[0] =
p[0];
971 AES_REGS->AES_IDATAR[1] =
p[1];
972 AES_REGS->AES_IDATAR[2] =
p[2];
973 AES_REGS->AES_IDATAR[3] =
p[3];
977 while((AES_REGS->AES_ISR & AES_ISR_DATRDY_Msk) == 0)
997 const uint8_t *
a,
size_t aLen,
const uint8_t *input, uint8_t *output,
998 size_t length, uint8_t *
t, uint32_t mode)
1007 AES_REGS->AES_CR = AES_CR_SWRST_Msk;
1010 if(aLen > 0 ||
length > 0)
1013 AES_REGS->AES_MR |= AES_MR_SMOD_AUTO_START | AES_MR_OPMOD_GCM |
1014 AES_MR_GTAGEN_Msk | mode;
1023 while((AES_REGS->AES_ISR & AES_ISR_DATRDY_Msk) == 0)
1032 AES_REGS->AES_IVR[3] =
BETOH32(2);
1035 AES_REGS->AES_AADLENR = aLen;
1036 AES_REGS->AES_CLENR =
length;
1087 while((AES_REGS->AES_ISR & AES_ISR_TAGRDY_Msk) == 0)
1093 temp = AES_REGS->AES_TAGR[0];
1095 temp = AES_REGS->AES_TAGR[1];
1097 temp = AES_REGS->AES_TAGR[2];
1099 temp = AES_REGS->AES_TAGR[3];
1105 AES_REGS->AES_MR |= AES_MR_SMOD_AUTO_START | AES_MR_OPMOD_CTR |
1116 AES_REGS->AES_IVR[3] =
BETOH32(1);
1139 void *cipherContext)
1142 if(context == NULL || cipherContext == NULL)
1174 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
1175 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
1177 uint8_t authTag[16];
1188 if(tLen < 4 || tLen > 16)
1193 authTag, AES_MR_CIPHER_Msk);
1219 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
1220 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
1224 uint8_t authTag[16];
1235 if(tLen < 4 || tLen > 16)
1243 for(
mask = 0, i = 0; i < tLen; i++)
1245 mask |= authTag[i] ^
t[i];
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 cbcEncrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CBC encryption.
void aesProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using AES algorithm.
CipherAlgoDecryptBlock decryptBlock
void gcmUpdateGhash(const uint8_t *data)
Update GHASH value.
Collection of AEAD algorithms.
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.
PIC32CX MTC/MTG/MTSH cipher hardware accelerator.
CipherAlgoEncryptBlock encryptBlock
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.
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.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
#define osMemcpy(dest, src, length)
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_FAILURE
Generic error code.
void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using AES algorithm.
@ ERROR_INVALID_KEY_LENGTH
General definitions for cryptographic algorithms.
void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
Block cipher modes of operation.
const CipherAlgo * cipherAlgo
Cipher algorithm.
error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
PIC32CX MTC/MTG/MTSH 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.
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.
Common interface for encryption algorithms.
error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
OsMutex pic32cxmtCryptoMutex
error_t cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
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.
#define osMemset(p, value, length)
void * cipherContext
Cipher algorithm context.
void ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
Increment counter block.
#define osMemmove(dest, src, length)
error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
void aesLoadKey(AesContext *context)
Load AES key.
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.