Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "mxc_device.h"
47 #if (MAX32690_CRYPTO_CIPHER_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
60 temp = MXC_CTB->cipher_ctrl & ~(MXC_F_CTB_CIPHER_CTRL_CIPHER |
61 MXC_F_CTB_CIPHER_CTRL_SRC);
64 temp |= MXC_S_CTB_CIPHER_CTRL_SRC_CIPHERKEY;
70 MXC_CTB->cipher_ctrl = temp | MXC_S_CTB_CIPHER_CTRL_CIPHER_AES128;
73 MXC_CTB->cipher_key[0] = context->
ek[0];
74 MXC_CTB->cipher_key[1] = context->
ek[1];
75 MXC_CTB->cipher_key[2] = context->
ek[2];
76 MXC_CTB->cipher_key[3] = context->
ek[3];
78 else if(context->
nr == 12)
81 MXC_CTB->cipher_ctrl = temp | MXC_S_CTB_CIPHER_CTRL_CIPHER_AES192;
84 MXC_CTB->cipher_key[0] = context->
ek[0];
85 MXC_CTB->cipher_key[1] = context->
ek[1];
86 MXC_CTB->cipher_key[2] = context->
ek[2];
87 MXC_CTB->cipher_key[3] = context->
ek[3];
88 MXC_CTB->cipher_key[4] = context->
ek[4];
89 MXC_CTB->cipher_key[5] = context->
ek[5];
94 MXC_CTB->cipher_ctrl = temp | MXC_S_CTB_CIPHER_CTRL_CIPHER_AES256;
97 MXC_CTB->cipher_key[0] = context->
ek[0];
98 MXC_CTB->cipher_key[1] = context->
ek[1];
99 MXC_CTB->cipher_key[2] = context->
ek[2];
100 MXC_CTB->cipher_key[3] = context->
ek[3];
101 MXC_CTB->cipher_key[4] = context->
ek[4];
102 MXC_CTB->cipher_key[5] = context->
ek[5];
103 MXC_CTB->cipher_key[6] = context->
ek[6];
104 MXC_CTB->cipher_key[7] = context->
ek[7];
121 uint8_t *output,
size_t length, uint32_t mode,
bool_t encrypt)
129 MXC_CTB->ctrl = MXC_F_CTB_CTRL_RST;
133 while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_RDY) == 0)
138 MXC_CTB->ctrl |= MXC_F_CTB_CTRL_FLAG_MODE;
140 MXC_CTB->ctrl |= MXC_F_CTB_CTRL_CPH_DONE | MXC_F_CTB_CTRL_GLS_DONE;
143 MXC_CTB->cipher_ctrl = (mode << MXC_F_CTB_CIPHER_CTRL_MODE_POS) &
144 MXC_F_CTB_CIPHER_CTRL_MODE;
153 MXC_CTB->cipher_init[0] = __UNALIGNED_UINT32_READ(
iv);
154 MXC_CTB->cipher_init[1] = __UNALIGNED_UINT32_READ(
iv + 4);
155 MXC_CTB->cipher_init[2] = __UNALIGNED_UINT32_READ(
iv + 8);
156 MXC_CTB->cipher_init[3] = __UNALIGNED_UINT32_READ(
iv + 12);
162 MXC_CTB->cipher_ctrl |= MXC_F_CTB_CIPHER_CTRL_ENC;
166 MXC_CTB->cipher_ctrl &= ~MXC_F_CTB_CIPHER_CTRL_ENC;
173 MXC_CTB->din[0] = __UNALIGNED_UINT32_READ(input);
174 MXC_CTB->din[1] = __UNALIGNED_UINT32_READ(input + 4);
175 MXC_CTB->din[2] = __UNALIGNED_UINT32_READ(input + 8);
176 MXC_CTB->din[3] = __UNALIGNED_UINT32_READ(input + 12);
179 while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_CPH_DONE) == 0)
184 temp = MXC_CTB->dout[0];
185 __UNALIGNED_UINT32_WRITE(output, temp);
186 temp = MXC_CTB->dout[1];
187 __UNALIGNED_UINT32_WRITE(output + 4, temp);
188 temp = MXC_CTB->dout[2];
189 __UNALIGNED_UINT32_WRITE(output + 8, temp);
190 temp = MXC_CTB->dout[3];
191 __UNALIGNED_UINT32_WRITE(output + 12, temp);
194 MXC_CTB->ctrl |= MXC_F_CTB_CTRL_CPH_DONE;
212 MXC_CTB->din[0] = buffer[0];
213 MXC_CTB->din[1] = buffer[1];
214 MXC_CTB->din[2] = buffer[2];
215 MXC_CTB->din[3] = buffer[3];
218 while((MXC_CTB->ctrl & MXC_F_CTB_CTRL_CPH_DONE) == 0)
223 buffer[0] = MXC_CTB->dout[0];
224 buffer[1] = MXC_CTB->dout[1];
225 buffer[2] = MXC_CTB->dout[2];
226 buffer[3] = MXC_CTB->dout[3];
229 MXC_CTB->ctrl |= MXC_F_CTB_CTRL_CPH_DONE;
236 temp = MXC_CTB->cipher_ctrl & ~MXC_F_CTB_CIPHER_CTRL_CIPHER;
237 MXC_CTB->cipher_ctrl = temp |= MXC_S_CTB_CIPHER_CTRL_CIPHER_DIS;
255 if(context == NULL || key == NULL)
264 else if(keyLen == 24)
269 else if(keyLen == 32)
299 MXC_CTB_MODE_ECB,
TRUE);
314 MXC_CTB_MODE_ECB,
FALSE);
318 #if (ECB_SUPPORT == ENABLED)
331 const uint8_t *
p, uint8_t *
c,
size_t length)
394 const uint8_t *
c, uint8_t *
p,
size_t length)
446 #if (CBC_SUPPORT == ENABLED)
460 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
539 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
614 #if (CFB_SUPPORT == ENABLED)
629 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
662 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
681 for(i = 0; i <
n; i++)
721 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
754 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
777 for(i = 0; i <
n; i++)
800 #if (OFB_SUPPORT == ENABLED)
815 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
848 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
867 for(i = 0; i <
n; i++)
894 #if (CTR_SUPPORT == ENABLED)
909 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
965 for(i = 0; i <
n; i++)
991 #if (GCM_SUPPORT == ENABLED)
1009 const uint8_t *
a,
size_t aLen,
const uint8_t *
p, uint8_t *
c,
size_t length,
1010 uint8_t *
t,
size_t tLen)
1027 if(tLen < 4 || tLen > 16)
1044 for(i = 0; i < ivLen; i +=
n)
1047 n =
MIN(ivLen - i, 16);
1073 for(i = 0; i < aLen; i +=
n)
1076 n =
MIN(aLen - i, 16);
1096 for(i = 0; i <
length; i +=
n)
1111 for(i = 0; i <
length; i +=
n)
1155 const uint8_t *
a,
size_t aLen,
const uint8_t *
c, uint8_t *
p,
size_t length,
1156 const uint8_t *
t,
size_t tLen)
1175 if(tLen < 4 || tLen > 16)
1192 for(i = 0; i < ivLen; i +=
n)
1195 n =
MIN(ivLen - i, 16);
1221 for(i = 0; i < aLen; i +=
n)
1224 n =
MIN(aLen - i, 16);
1232 for(i = 0; i <
length; i +=
n)
1255 for(i = 0; i <
length; i +=
n)
1284 for(
mask = 0,
n = 0;
n < tLen;
n++)
MAX32690 hardware cryptographic accelerator.
CipherAlgoDecryptBlock decryptBlock
Collection of AEAD algorithms.
void aesLoadKey(AesContext *context)
Load AES key.
void gcmIncCounter(uint8_t *ctr)
Increment counter block.
MAX32690 cipher hardware accelerator.
CipherAlgoEncryptBlock encryptBlock
error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
error_t cbcEncrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *p, uint8_t *c, size_t length)
CBC encryption.
@ 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)
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_FAILURE
Generic error code.
error_t cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
@ ERROR_INVALID_KEY_LENGTH
error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
General definitions for cryptographic algorithms.
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 ctrEncrypt(const CipherAlgo *cipher, void *context, uint_t m, uint8_t *t, const uint8_t *p, uint8_t *c, size_t length)
CTR encryption.
Block cipher modes of operation.
const CipherAlgo * cipherAlgo
Cipher algorithm.
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 osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
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.
Common interface for encryption algorithms.
void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using AES 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.
OsMutex max32690CryptoMutex
#define osMemset(p, value, length)
error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
void * cipherContext
Cipher algorithm context.
void aesProcessData(AesContext *context, uint8_t *iv, const uint8_t *input, uint8_t *output, size_t length, uint32_t mode, bool_t encrypt)
Perform AES encryption or decryption.
void ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
Increment counter block.
#define osMemmove(dest, src, length)
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.