Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
45 #if (M460_CRYPTO_CIPHER_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
58 temp = CRPT->AES_CTL & ~CRPT_AES_CTL_KEYSZ_Pos;
67 CRPT->AES_KEY[0] = context->
ek[0];
68 CRPT->AES_KEY[1] = context->
ek[1];
69 CRPT->AES_KEY[2] = context->
ek[2];
70 CRPT->AES_KEY[3] = context->
ek[3];
72 else if(context->
nr == 12)
78 CRPT->AES_KEY[0] = context->
ek[0];
79 CRPT->AES_KEY[1] = context->
ek[1];
80 CRPT->AES_KEY[2] = context->
ek[2];
81 CRPT->AES_KEY[3] = context->
ek[3];
82 CRPT->AES_KEY[4] = context->
ek[4];
83 CRPT->AES_KEY[5] = context->
ek[5];
91 CRPT->AES_KEY[0] = context->
ek[0];
92 CRPT->AES_KEY[1] = context->
ek[1];
93 CRPT->AES_KEY[2] = context->
ek[2];
94 CRPT->AES_KEY[3] = context->
ek[3];
95 CRPT->AES_KEY[4] = context->
ek[4];
96 CRPT->AES_KEY[5] = context->
ek[5];
97 CRPT->AES_KEY[6] = context->
ek[6];
98 CRPT->AES_KEY[7] = context->
ek[7];
114 CRPT->AES_CTL |= CRPT_AES_CTL_DMALAST_Msk | CRPT_AES_CTL_START_Msk;
117 while((CRPT->AES_STS & CRPT_AES_STS_INBUFFULL_Msk) != 0)
122 CRPT->AES_DATIN = __UNALIGNED_UINT32_READ(input);
123 CRPT->AES_DATIN = __UNALIGNED_UINT32_READ(input + 4);
124 CRPT->AES_DATIN = __UNALIGNED_UINT32_READ(input + 8);
125 CRPT->AES_DATIN = __UNALIGNED_UINT32_READ(input + 12);
128 while((CRPT->AES_STS & CRPT_AES_STS_OUTBUFEMPTY_Msk) != 0)
133 temp = CRPT->AES_DATOUT;
134 __UNALIGNED_UINT32_WRITE(output, temp);
135 temp = CRPT->AES_DATOUT;
136 __UNALIGNED_UINT32_WRITE(output + 4, temp);
137 temp = CRPT->AES_DATOUT;
138 __UNALIGNED_UINT32_WRITE(output + 8, temp);
139 temp = CRPT->AES_DATOUT;
140 __UNALIGNED_UINT32_WRITE(output + 12, temp);
143 CRPT->AES_IV[0] = CRPT->AES_FDBCK[0];
144 CRPT->AES_IV[1] = CRPT->AES_FDBCK[1];
145 CRPT->AES_IV[2] = CRPT->AES_FDBCK[2];
146 CRPT->AES_IV[3] = CRPT->AES_FDBCK[3];
161 uint8_t *output,
size_t length, uint32_t opmode)
169 SYS->IPRST0 |= SYS_IPRST0_CRPTRST_Msk;
170 SYS->IPRST0 &= ~SYS_IPRST0_CRPTRST_Msk;
173 CRPT->AES_CTL = CRPT_AES_CTL_KEYPRT_Msk | CRPT_AES_CTL_INSWAP_Msk |
174 CRPT_AES_CTL_OUTSWAP_Msk | opmode;
221 temp = CRPT->AES_FDBCK[0];
223 temp = CRPT->AES_FDBCK[1];
225 temp = CRPT->AES_FDBCK[2];
227 temp = CRPT->AES_FDBCK[3];
232 CRPT->AES_CTL |= CRPT_AES_CTL_STOP_Msk;
252 if(context == NULL || key == NULL)
261 else if(keyLen == 24)
266 else if(keyLen == 32)
281 for(i = 0; i < keyLen; i++)
321 #if (ECB_SUPPORT == ENABLED)
334 const uint8_t *
p, uint8_t *
c,
size_t length)
353 CRPT_AES_CTL_ENCRPT_Msk);
398 const uint8_t *
c, uint8_t *
p,
size_t length)
450 #if (CBC_SUPPORT == ENABLED)
464 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
483 CRPT_AES_CTL_ENCRPT_Msk);
541 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
608 #if (CFB_SUPPORT == ENABLED)
623 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
641 CRPT_AES_CTL_ENCRPT_Msk);
657 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
676 for(i = 0; i <
n; i++)
716 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
749 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
772 for(i = 0; i <
n; i++)
795 #if (OFB_SUPPORT == ENABLED)
810 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
828 CRPT_AES_CTL_ENCRPT_Msk);
844 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
863 for(i = 0; i <
n; i++)
890 #if (CTR_SUPPORT == ENABLED)
905 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
939 CRPT_AES_CTL_ENCRPT_Msk);
966 for(i = 0; i <
n; i++)
992 #if (GCM_SUPPORT == ENABLED)
1007 for(temp = inc, i = 0; i <= 3; i++)
1010 temp += ctr[15 - i];
1011 ctr[15 - i] = temp & 0xFF;
1033 const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
size_t length,
1034 uint8_t *
t,
size_t tLen)
1051 if(tLen < 4 || tLen > 16)
1068 for(i = 0; i < ivLen; i +=
n)
1071 n =
MIN(ivLen - i, 16);
1097 for(i = 0; i < aLen; i +=
n)
1100 n =
MIN(aLen - i, 16);
1114 for(i = 0; i <
length; i +=
n)
1138 for(i = 0; i <
length; i +=
n)
1153 for(i = 0; i <
length; i +=
n)
1197 const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
size_t length,
1198 const uint8_t *
t,
size_t tLen)
1217 if(tLen < 4 || tLen > 16)
1234 for(i = 0; i < ivLen; i +=
n)
1237 n =
MIN(ivLen - i, 16);
1263 for(i = 0; i < aLen; i +=
n)
1266 n =
MIN(aLen - i, 16);
1274 for(i = 0; i <
length; i +=
n)
1291 for(i = 0; i <
length; i +=
n)
1315 for(i = 0; i <
length; i +=
n)
1344 for(
mask = 0,
n = 0;
n < tLen;
n++)
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.
CipherAlgoDecryptBlock decryptBlock
void aesLoadKey(AesContext *context)
Load AES key.
error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
#define CRPT_AES_CTL_OPMODE_OFB
Collection of AEAD algorithms.
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_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 cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
void gcmIncCounter(uint8_t *ctr)
Increment counter block.
#define CRPT_AES_CTL_OPMODE_CFB
CipherAlgoEncryptBlock encryptBlock
void gcmIncBlock(uint8_t *ctr, uint32_t inc)
Increment counter block.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
#define osMemcpy(dest, src, length)
__weak_func void gcmMul(GcmContext *context, uint8_t *x)
Multiplication operation in GF(2^128)
#define CRPT_AES_CTL_OPMODE_ECB
M460 cipher hardware accelerator.
void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using AES algorithm.
@ ERROR_FAILURE
Generic error code.
#define CRPT_AES_CTL_KEYSZ_192B
@ ERROR_INVALID_KEY_LENGTH
General definitions for cryptographic algorithms.
Block cipher modes of operation.
void aesProcessData(AesContext *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t opmode)
Perform AES encryption or decryption.
const CipherAlgo * cipherAlgo
Cipher algorithm.
#define CRPT_AES_CTL_KEYSZ_256B
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 CRPT_AES_CTL_OPMODE_CTR
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.
void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
void gcmXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n)
XOR operation.
#define CRPT_AES_CTL_KEYSZ_128B
M460 hardware cryptographic accelerator.
Common interface for encryption algorithms.
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.
#define CRPT_AES_CTL_OPMODE_CBC
#define osMemset(p, value, length)
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 * cipherContext
Cipher algorithm context.
void ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
Increment counter block.
error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
void aesProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using AES algorithm.
#define osMemmove(dest, src, length)