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);
544 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
619 #if (CFB_SUPPORT == ENABLED)
634 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
652 CRPT_AES_CTL_ENCRPT_Msk);
668 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
687 for(i = 0; i <
n; i++)
727 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
760 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
783 for(i = 0; i <
n; i++)
806 #if (OFB_SUPPORT == ENABLED)
821 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
839 CRPT_AES_CTL_ENCRPT_Msk);
855 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
874 for(i = 0; i <
n; i++)
901 #if (CTR_SUPPORT == ENABLED)
916 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
950 CRPT_AES_CTL_ENCRPT_Msk);
977 for(i = 0; i <
n; i++)
1003 #if (GCM_SUPPORT == ENABLED)
1018 for(temp = inc, i = 0; i <= 3; i++)
1021 temp += ctr[15 - i];
1022 ctr[15 - i] = temp & 0xFF;
1044 const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
size_t length,
1045 uint8_t *
t,
size_t tLen)
1062 if(tLen < 4 || tLen > 16)
1079 for(i = 0; i < ivLen; i +=
n)
1082 n =
MIN(ivLen - i, 16);
1108 for(i = 0; i < aLen; i +=
n)
1111 n =
MIN(aLen - i, 16);
1125 for(i = 0; i <
length; i +=
n)
1149 for(i = 0; i <
length; i +=
n)
1164 for(i = 0; i <
length; i +=
n)
1208 const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
size_t length,
1209 const uint8_t *
t,
size_t tLen)
1228 if(tLen < 4 || tLen > 16)
1245 for(i = 0; i < ivLen; i +=
n)
1248 n =
MIN(ivLen - i, 16);
1274 for(i = 0; i < aLen; i +=
n)
1277 n =
MIN(aLen - i, 16);
1285 for(i = 0; i <
length; i +=
n)
1302 for(i = 0; i <
length; i +=
n)
1326 for(i = 0; i <
length; i +=
n)
1355 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_ECB
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.
#define CRPT_AES_CTL_KEYSZ_128B
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.
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)
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_OPMODE_CFB
@ 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.
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_OFB
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.
#define CRPT_AES_CTL_KEYSZ_256B
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_192B
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 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.
#define CRPT_AES_CTL_OPMODE_CBC
void * cipherContext
Cipher algorithm context.
#define CRPT_AES_CTL_OPMODE_CTR
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)