Go to the documentation of this file.
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
44 #if (SAM4C_CRYPTO_CIPHER_SUPPORT == ENABLED && AES_SUPPORT == ENABLED)
57 temp = AES->AES_MR & ~AES_MR_KEYSIZE_Msk;
63 AES->AES_MR = temp | AES_MR_KEYSIZE_AES128;
66 AES->AES_KEYWR[0] = context->
ek[0];
67 AES->AES_KEYWR[1] = context->
ek[1];
68 AES->AES_KEYWR[2] = context->
ek[2];
69 AES->AES_KEYWR[3] = context->
ek[3];
71 else if(context->
nr == 12)
74 AES->AES_MR = temp | AES_MR_KEYSIZE_AES192;
77 AES->AES_KEYWR[0] = context->
ek[0];
78 AES->AES_KEYWR[1] = context->
ek[1];
79 AES->AES_KEYWR[2] = context->
ek[2];
80 AES->AES_KEYWR[3] = context->
ek[3];
81 AES->AES_KEYWR[4] = context->
ek[4];
82 AES->AES_KEYWR[5] = context->
ek[5];
87 AES->AES_MR = temp | AES_MR_KEYSIZE_AES256;
90 AES->AES_KEYWR[0] = context->
ek[0];
91 AES->AES_KEYWR[1] = context->
ek[1];
92 AES->AES_KEYWR[2] = context->
ek[2];
93 AES->AES_KEYWR[3] = context->
ek[3];
94 AES->AES_KEYWR[4] = context->
ek[4];
95 AES->AES_KEYWR[5] = context->
ek[5];
96 AES->AES_KEYWR[6] = context->
ek[6];
97 AES->AES_KEYWR[7] = context->
ek[7];
113 p = (uint32_t *) input;
114 AES->AES_IDATAR[0] =
p[0];
115 AES->AES_IDATAR[1] =
p[1];
116 AES->AES_IDATAR[2] =
p[2];
117 AES->AES_IDATAR[3] =
p[3];
120 while((AES->AES_ISR & AES_ISR_DATRDY) == 0)
125 p = (uint32_t *) output;
126 p[0] = AES->AES_ODATAR[0];
127 p[1] = AES->AES_ODATAR[1];
128 p[2] = AES->AES_ODATAR[2];
129 p[3] = AES->AES_ODATAR[3];
144 uint8_t *output,
size_t length, uint32_t mode)
152 AES->AES_CR = AES_CR_SWRST;
155 AES->AES_MR = AES_MR_SMOD_AUTO_START | mode;
164 AES->AES_IVR[0] =
p[0];
165 AES->AES_IVR[1] =
p[1];
166 AES->AES_IVR[2] =
p[2];
167 AES->AES_IVR[3] =
p[3];
214 if(context == NULL || key == NULL)
223 else if(keyLen == 24)
228 else if(keyLen == 32)
277 #if (ECB_SUPPORT == ENABLED)
290 const uint8_t *
p, uint8_t *
c,
size_t length)
354 const uint8_t *
c, uint8_t *
p,
size_t length)
406 #if (CBC_SUPPORT == ENABLED)
420 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
500 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
575 #if (CFB_SUPPORT == ENABLED)
590 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
608 AES_MR_OPMOD_CFB | AES_MR_CFBS_SIZE_128BIT);
624 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
643 for(i = 0; i <
n; i++)
683 uint8_t *
iv,
const uint8_t *
c, uint8_t *
p,
size_t length)
701 AES_MR_CFBS_SIZE_128BIT);
717 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
740 for(i = 0; i <
n; i++)
763 #if (OFB_SUPPORT == ENABLED)
778 uint8_t *
iv,
const uint8_t *
p, uint8_t *
c,
size_t length)
812 if((
s % 8) == 0 &&
s >= 1 &&
s <= (cipher->
blockSize * 8))
831 for(i = 0; i <
n; i++)
858 #if (GCM_SUPPORT == ENABLED)
870 p = (uint32_t *)
data;
871 AES->AES_IDATAR[0] =
p[0];
872 AES->AES_IDATAR[1] =
p[1];
873 AES->AES_IDATAR[2] =
p[2];
874 AES->AES_IDATAR[3] =
p[3];
878 while((AES->AES_ISR & AES_ISR_DATRDY) == 0)
898 const uint8_t *
a,
size_t aLen,
const uint8_t *input, uint8_t *output,
899 size_t length, uint8_t *
t, uint32_t mode)
908 AES->AES_CR = AES_CR_SWRST;
911 if(aLen > 0 ||
length > 0)
914 AES->AES_MR |= AES_MR_SMOD_AUTO_START | AES_MR_OPMOD_GCM |
915 AES_MR_GTAGEN | mode;
924 while((AES->AES_ISR & AES_ISR_DATRDY) == 0)
936 AES->AES_AADLENR = aLen;
988 while((AES->AES_ISR & AES_ISR_TAGRDY) == 0)
994 temp = AES->AES_TAGR[0];
996 temp = AES->AES_TAGR[1];
998 temp = AES->AES_TAGR[2];
1000 temp = AES->AES_TAGR[3];
1006 AES->AES_MR |= AES_MR_SMOD_AUTO_START | AES_MR_OPMOD_CTR |
1040 void *cipherContext)
1043 if(context == NULL || cipherContext == NULL)
1075 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
p,
1076 uint8_t *
c,
size_t length, uint8_t *
t,
size_t tLen)
1078 uint8_t authTag[16];
1089 if(tLen < 4 || tLen > 16)
1094 authTag, AES_MR_CIPHER);
1120 size_t ivLen,
const uint8_t *
a,
size_t aLen,
const uint8_t *
c,
1121 uint8_t *
p,
size_t length,
const uint8_t *
t,
size_t tLen)
1125 uint8_t authTag[16];
1136 if(tLen < 4 || tLen > 16)
1144 for(
mask = 0, i = 0; i < tLen; i++)
1146 mask |= authTag[i] ^
t[i];
error_t ecbDecrypt(const CipherAlgo *cipher, void *context, const uint8_t *c, uint8_t *p, size_t length)
ECB decryption.
void aesProcessDataBlock(const uint8_t *input, uint8_t *output)
Encrypt/decrypt a 16-byte block using AES algorithm.
CipherAlgoDecryptBlock decryptBlock
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 aesEncryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using AES algorithm.
Collection of AEAD algorithms.
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 ecbEncrypt(const CipherAlgo *cipher, void *context, const uint8_t *p, uint8_t *c, size_t length)
ECB encryption.
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.
SAM4C cipher hardware accelerator.
CipherAlgoEncryptBlock encryptBlock
@ ERROR_INVALID_PARAMETER
Invalid parameter.
#define osMemcpy(dest, src, length)
void aesDecryptBlock(AesContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using AES algorithm.
void aesLoadKey(AesContext *context)
Load AES key.
error_t cbcDecrypt(const CipherAlgo *cipher, void *context, uint8_t *iv, const uint8_t *c, uint8_t *p, size_t length)
CBC decryption.
@ ERROR_FAILURE
Generic error code.
@ ERROR_INVALID_KEY_LENGTH
General definitions for cryptographic algorithms.
Block cipher modes of operation.
const CipherAlgo * cipherAlgo
Cipher algorithm.
SAM4C hardware cryptographic accelerator.
error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
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.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
error_t aesInit(AesContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
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 osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Common interface for encryption algorithms.
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 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.
#define osMemmove(dest, src, length)
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.