Skip to content

Commit

Permalink
Updating the AES128 enc/dec code to support ECB mode (default) and ha…
Browse files Browse the repository at this point in the history
…ndle uneven buffer sizes that are not a multiple of 16
  • Loading branch information
maxieds committed Jul 19, 2022
1 parent ccdc36e commit 5c894b8
Showing 1 changed file with 78 additions and 31 deletions.
109 changes: 78 additions & 31 deletions Firmware/Chameleon-Mini/Application/CryptoAES128.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,12 @@ void CryptoAESGetConfigDefaults(CryptoAESConfig_t *ctx) {
ctx->ProcessingMode = CRYPTO_AES_PMODE_ENCIPHER;
ctx->ProcessingDelay = 0;
ctx->StartMode = AES_MANUAL;
ctx->OpMode = CRYPTO_AES_CBC_MODE;
ctx->XorMode = AES_XOR_ON;
if (__CryptoAESOpMode == CRYPTO_AES_CBC_MODE) {
ctx->OpMode = CRYPTO_AES_CBC_MODE;
} else {
ctx->OpMode = CRYPTO_AES_ECB_MODE;
}
}

static void int_callback_aes(void) {}
Expand All @@ -176,7 +180,6 @@ void CryptoAESInitContext(CryptoAESConfig_t *ctx) {
}
aes_software_reset();
memset(__CryptoAES_IVData, 0x00, CRYPTO_AES_BLOCK_SIZE);
__CryptoAESOpMode = ctx->OpMode;
aes_configure(ctx->ProcessingMode, ctx->StartMode, ctx->XorMode);
aes_set_callback(&int_callback_aes);
}
Expand Down Expand Up @@ -204,7 +207,7 @@ static void CryptoAESEncryptBlock(uint8_t *Plaintext, uint8_t *Ciphertext, const
aes_write_inputdata(Plaintext);
aes_start();
do {
// Wait until AES is finished or an error occurs.
/* Wait until AES is finished or an error occurs. */
} while (aes_is_busy());
aes_read_outputdata(Ciphertext);
aes_clear_interrupt_flag();
Expand All @@ -229,40 +232,58 @@ static void CryptoAESDecryptBlock(uint8_t *Plaintext, uint8_t *Ciphertext, const
aes_write_inputdata(Ciphertext);
aes_start();
do {
// Wait until AES is finished or an error occurs.
/* Wait until AES is finished or an error occurs. */
} while (aes_is_busy());
aes_read_outputdata(Plaintext);
aes_clear_interrupt_flag();
aes_clear_error_flag();
}

uint8_t CryptoAESEncryptBuffer(uint16_t Count, uint8_t *Plaintext, uint8_t *Ciphertext,
uint8_t *IV, const uint8_t *Key) {
bool copyIVBuffer = true;
uint8_t *IVIn, const uint8_t *Key) {
uint8_t *IV = IVIn;
if ((Count % CRYPTO_AES_BLOCK_SIZE) != 0) {
return 0xBE;
} else if (IV == NULL) {
}
if (IVIn == NULL) {
memset(__CryptoAES_IVData, 0x00, CRYPTO_AES_BLOCK_SIZE);
IV = &__CryptoAES_IVData[0];
copyIVBuffer = false;
}
CryptoAESBlock_t inputBlock;
size_t bufBlocks = (Count + CRYPTO_AES_BLOCK_SIZE - 1) / CRYPTO_AES_BLOCK_SIZE;
bool unevenBlockSize = (Count % CRYPTO_AES_BLOCK_SIZE) != 0;
for (int blk = 0; blk < bufBlocks; blk++) {
if (__CryptoAESOpMode == CRYPTO_AES_CBC_MODE) {
if (blk == 0) {
memcpy(inputBlock, &Plaintext[0], CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(IV, inputBlock, CRYPTO_AES_BLOCK_SIZE);
if (blk + 1 != bufBlocks || !unevenBlockSize) {
if (blk == 0) {
memcpy(inputBlock, &Plaintext[0], CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(IV, inputBlock, CRYPTO_AES_BLOCK_SIZE);
} else {
memcpy(inputBlock, &Ciphertext[(blk - 1) * CRYPTO_AES_BLOCK_SIZE], CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(&Plaintext[blk * CRYPTO_AES_BLOCK_SIZE], inputBlock, CRYPTO_AES_BLOCK_SIZE);
}
} else {
memcpy(inputBlock, &Ciphertext[(blk - 1) * CRYPTO_AES_BLOCK_SIZE], CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(&Plaintext[blk * CRYPTO_AES_BLOCK_SIZE], inputBlock, CRYPTO_AES_BLOCK_SIZE);
uint8_t numInputUnevenBytes = Count % CRYPTO_AES_BLOCK_SIZE;
memcpy(inputBlock, &Plaintext[blk * CRYPTO_AES_BLOCK_SIZE], numInputUnevenBytes);
memset(&inputBlock[numInputUnevenBytes], 0x00, CRYPTO_AES_BLOCK_SIZE - numInputUnevenBytes);
if (blk == 0) {
CryptoMemoryXOR(IV, inputBlock, CRYPTO_AES_BLOCK_SIZE);
} else {
CryptoMemoryXOR(&Ciphertext[(blk - 1) * CRYPTO_AES_BLOCK_SIZE], inputBlock, CRYPTO_AES_BLOCK_SIZE);
}
}
CryptoAESEncryptBlock(inputBlock, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, Key, true);
if (blk + 1 == bufBlocks && copyIVBuffer) {
if (blk + 1 == bufBlocks) {
memcpy(IV, inputBlock, CRYPTO_AES_BLOCK_SIZE);
}
} else {
memcpy(inputBlock, Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
if (blk + 1 != bufBlocks || !unevenBlockSize) {
memcpy(inputBlock, Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
} else {
uint8_t numInputUnevenBytes = Count % CRYPTO_AES_BLOCK_SIZE;
memcpy(inputBlock, &Plaintext[blk * CRYPTO_AES_BLOCK_SIZE], numInputUnevenBytes);
memset(&inputBlock[numInputUnevenBytes], 0x00, CRYPTO_AES_BLOCK_SIZE - numInputUnevenBytes);
}
CryptoMemoryXOR(IV, inputBlock, CRYPTO_AES_BLOCK_SIZE);
CryptoAESEncryptBlock(inputBlock, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, Key, true);
memcpy(IV, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
Expand All @@ -272,36 +293,62 @@ uint8_t CryptoAESEncryptBuffer(uint16_t Count, uint8_t *Plaintext, uint8_t *Ciph
}

uint8_t CryptoAESDecryptBuffer(uint16_t Count, uint8_t *Plaintext, uint8_t *Ciphertext,
uint8_t *IV, const uint8_t *Key) {
bool copyIVBuffer = true;
uint8_t *IVIn, const uint8_t *Key) {
uint8_t *IV = IVIn;
if ((Count % CRYPTO_AES_BLOCK_SIZE) != 0) {
return 0xBE;
} else if (IV == NULL) {
}
if (IVIn == NULL) {
memset(__CryptoAES_IVData, 0x00, CRYPTO_AES_BLOCK_SIZE);
IV = &__CryptoAES_IVData[0];
copyIVBuffer = false;
}
CryptoAESBlock_t inputBlock;
size_t bufBlocks = (Count + CRYPTO_AES_BLOCK_SIZE - 1) / CRYPTO_AES_BLOCK_SIZE;
bool unevenBlockSize = (Count % CRYPTO_AES_BLOCK_SIZE) != 0;
for (int blk = 0; blk < bufBlocks; blk++) {
if (__CryptoAESOpMode == CRYPTO_AES_CBC_MODE) {
CryptoAESDecryptBlock(inputBlock, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, Key);
if (blk == 0) {
memcpy(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, inputBlock, CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(IV, Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
if (blk + 1 != bufBlocks || !unevenBlockSize) {
CryptoAESDecryptBlock(inputBlock, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, Key);
if (blk == 0) {
memcpy(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, inputBlock, CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(IV, Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
} else {
memcpy(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, inputBlock, CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(&Ciphertext[(blk - 1) * CRYPTO_AES_BLOCK_SIZE],
Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
}
} else {
memcpy(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, inputBlock, CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(&Ciphertext[(blk - 1) * CRYPTO_AES_BLOCK_SIZE],
Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
uint8_t numInputUnevenBytes = Count % CRYPTO_AES_BLOCK_SIZE;
CryptoAESBlock_t inputBlockTemp;
memset(inputBlockTemp, 0x00, CRYPTO_AES_BLOCK_SIZE);
memcpy(inputBlockTemp, &Ciphertext[blk * CRYPTO_AES_BLOCK_SIZE], numInputUnevenBytes);
CryptoAESDecryptBlock(inputBlock, inputBlockTemp, Key);
if (blk == 0) {
memcpy(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, inputBlock, CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(IV, Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
} else {
memcpy(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, inputBlock, CRYPTO_AES_BLOCK_SIZE);
CryptoMemoryXOR(&Ciphertext[(blk - 1) * CRYPTO_AES_BLOCK_SIZE],
Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
}
}
if (blk + 1 == bufBlocks && copyIVBuffer) {
if (blk + 1 == bufBlocks) {
memcpy(IV, inputBlock, CRYPTO_AES_BLOCK_SIZE);
}
} else {
CryptoAESDecryptBlock(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE,
Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, Key);
CryptoMemoryXOR(IV, Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
memcpy(IV, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
if (blk + 1 != bufBlocks || !unevenBlockSize) {
CryptoAESDecryptBlock(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, Key);
CryptoMemoryXOR(IV, Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
memcpy(IV, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
} else {
uint8_t numInputUnevenBytes = Count % CRYPTO_AES_BLOCK_SIZE;
CryptoAESBlock_t inputBlockTemp;
memset(inputBlockTemp, 0x00, CRYPTO_AES_BLOCK_SIZE);
memcpy(inputBlockTemp, Ciphertext + blk * CRYPTO_AES_BLOCK_SIZE, numInputUnevenBytes);
CryptoAESDecryptBlock(Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, inputBlockTemp, Key);
CryptoMemoryXOR(IV, Plaintext + blk * CRYPTO_AES_BLOCK_SIZE, CRYPTO_AES_BLOCK_SIZE);
memcpy(IV, inputBlockTemp, CRYPTO_AES_BLOCK_SIZE);
}
}
}
return 0;
Expand Down

0 comments on commit 5c894b8

Please sign in to comment.