Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
45 #if (SAME70_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 AES_REGS->AES_CR = AES_CR_START_Msk;
124 while((AES_REGS->AES_ISR & AES_ISR_DATRDY_Msk) == 0)
129 p = (uint32_t *) output;
130 p[0] = AES_REGS->AES_ODATAR[0];
131 p[1] = AES_REGS->AES_ODATAR[1];
132 p[2] = AES_REGS->AES_ODATAR[2];
133 p[3] = AES_REGS->AES_ODATAR[3];
148 uint8_t *output,
size_t length, uint32_t mode)
156 AES_REGS->AES_CR = AES_CR_SWRST_Msk;
159 AES_REGS->AES_MR = AES_MR_SMOD_MANUAL_START | mode;
168 AES_REGS->AES_IVR[0] =
p[0];
169 AES_REGS->AES_IVR[1] =
p[1];
170 AES_REGS->AES_IVR[2] =
p[2];
171 AES_REGS->AES_IVR[3] =
p[3];
218 if(context == NULL || key == NULL)
227 else if(keyLen == 24)
232 else if(keyLen == 32)
262 AES_MR_CIPHER_Msk | AES_MR_OPMOD_ECB);
281 #if (ECB_SUPPORT == ENABLED)
294 const uint8_t *
p, uint8_t *
c,
size_t length)
358 const uint8_t *
c, uint8_t *
p,
size_t length)
410 #if (CBC_SUPPORT == ENABLED)
424 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
504 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
579 #if (CFB_SUPPORT == ENABLED)
594 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
612 AES_MR_OPMOD_CFB | AES_MR_CFBS_SIZE_128BIT);
628 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
647 for(i = 0; i <
n; i++)
687 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
705 AES_MR_CFBS_SIZE_128BIT);
721 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
744 for(i = 0; i <
n; i++)
767 #if (OFB_SUPPORT == ENABLED)
782 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
816 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
835 for(i = 0; i <
n; i++)
862 #if (CTR_SUPPORT == ENABLED)
877 uint8_t *
t,
const uint8_t *
p, uint8_t *
c,
size_t length)
934 for(i = 0; i <
n; i++)
960 #if (GCM_SUPPORT == ENABLED)
972 p = (uint32_t *)
data;
973 AES_REGS->AES_IDATAR[0] =
p[0];
974 AES_REGS->AES_IDATAR[1] =
p[1];
975 AES_REGS->AES_IDATAR[2] =
p[2];
976 AES_REGS->AES_IDATAR[3] =
p[3];
979 AES_REGS->AES_CR = AES_CR_START_Msk;
983 while((AES_REGS->AES_ISR & AES_ISR_DATRDY_Msk) == 0)
1003 const uint8_t *
a,
size_t aLen,
const uint8_t *input, uint8_t *output,
1004 size_t length, uint8_t *
t, uint32_t mode)
1013 AES_REGS->AES_CR = AES_CR_SWRST_Msk;
1016 if(aLen > 0 ||
length > 0)
1019 AES_REGS->AES_MR |= AES_MR_SMOD_MANUAL_START | AES_MR_OPMOD_GCM |
1020 AES_MR_GTAGEN_Msk | mode;
1029 while((AES_REGS->AES_ISR & AES_ISR_DATRDY_Msk) == 0)
1038 AES_REGS->AES_IVR[3] =
BETOH32(2);
1041 AES_REGS->AES_AADLENR = aLen;
1042 AES_REGS->AES_CLENR =
length;
1093 while((AES_REGS->AES_ISR & AES_ISR_TAGRDY_Msk) == 0)
1099 temp = AES_REGS->AES_TAGR[0];
1101 temp = AES_REGS->AES_TAGR[1];
1103 temp = AES_REGS->AES_TAGR[2];
1105 temp = AES_REGS->AES_TAGR[3];
1111 AES_REGS->AES_MR |= AES_MR_SMOD_MANUAL_START | AES_MR_OPMOD_CTR |
1122 AES_REGS->AES_IVR[3] =
BETOH32(1);
1145 void *cipherContext)
1148 if(context == NULL || cipherContext == NULL)
1180 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
1181 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
1183 uint8_t authTag[16];
1194 if(tLen < 4 || tLen > 16)
1199 authTag, AES_MR_CIPHER_Msk);
1225 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
1226 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
1230 uint8_t authTag[16];
1241 if(tLen < 4 || tLen > 16)
1249 for(
mask = 0, i = 0; i < tLen; i++)
1251 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.
OsMutex same70CryptoMutex
CipherAlgoDecryptBlock decryptBlock
error_t ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
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.
void aesProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using AES algorithm.
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.
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.
CipherAlgoEncryptBlock encryptBlock
void aesLoadKey(AesContext *context)
Load AES key.
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)
@ ERROR_FAILURE
Generic error code.
@ ERROR_INVALID_KEY_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.
General definitions for cryptographic algorithms.
Block cipher modes of operation.
void aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
const CipherAlgo * cipherAlgo
Cipher algorithm.
error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
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 aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
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.
void gcmUpdateGhash(const uint8_t *data)
Update GHASH value.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Common interface for encryption algorithms.
SAME70 cipher hardware accelerator.
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.
SAME70 hardware cryptographic accelerator.
#define osMemset(p, value, length)
void * cipherContext
Cipher algorithm context.
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 ctrIncBlock(uint8_t *ctr, uint32_t inc, size_t blockSize, size_t m)
Increment counter block.
#define osMemmove(dest, src, length)