Skip to content

Commit

Permalink
Changed zero padding with PKCS7
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Dec 9, 2024
1 parent 6fdbf8e commit ab6dd3c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 72 deletions.
116 changes: 45 additions & 71 deletions src/crypt/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -613,125 +613,99 @@ void XAES_DecryptBlock(xaes_context_t *pCtx, uint8_t output[XAES_BLOCK_SIZE], co
uint8_t* XAES_Encrypt(xaes_context_t *pCtx, const uint8_t *pInput, size_t *pLength)
{
if (pInput == NULL || pLength == NULL || !(*pLength)) return NULL;
uint8_t *pInputPtr = NULL;
size_t nDataLength = 0;
size_t nPaddingLen = ((*pLength / XAES_BLOCK_SIZE) + 1) * XAES_BLOCK_SIZE;
size_t nOriginalLen = *pLength;

uint8_t *pOutput = (uint8_t*)malloc(nPaddingLen + 1);
if (!pOutput) return NULL;

uint8_t iv[XAES_BLOCK_SIZE];
memcpy(iv, pCtx->IV, sizeof(iv));

if (*pLength % XAES_BLOCK_SIZE)
{
size_t nCount = *pLength / XAES_BLOCK_SIZE;
size_t nPart = *pLength - nCount * XAES_BLOCK_SIZE;
nDataLength = *pLength + (XAES_BLOCK_SIZE - nPart);
}

if (nDataLength)
{
pInputPtr = (uint8_t*)malloc(nDataLength + 1);
if (pInputPtr == NULL) return NULL;

memcpy(pInputPtr, pInput, *pLength);
memset(pInputPtr + *pLength, 0, nDataLength - *pLength);
}

const uint8_t *pData = pInputPtr != NULL ? pInputPtr : pInput;
nDataLength = nDataLength ? nDataLength : *pLength;

uint8_t *pOutput = (uint8_t*)malloc(nDataLength + 1);
if (pOutput == NULL)
{
free(pInputPtr);
return NULL;
}
// Copy input data and add PKCS#7 padding
memcpy(pOutput, pInput, nOriginalLen);
uint8_t nPadding = nPaddingLen - nOriginalLen;
memset(pOutput + nOriginalLen, nPadding, nPadding);

uint8_t *pOffset = pOutput;
size_t nDataLeft = nDataLength;
size_t nDataLeft = nPaddingLen;

while (nDataLeft > 0)
{
uint8_t i;
size_t i;
for (i = 0; i < XAES_BLOCK_SIZE; i++)
pOffset[i] = pData[i] ^ iv[i];
pOffset[i] ^= iv[i];

XAES_EncryptBlock(pCtx, pOffset, pOffset);
memcpy(iv, pOffset, XAES_BLOCK_SIZE);

nDataLeft -= XAES_BLOCK_SIZE;
pOffset += XAES_BLOCK_SIZE;
pData += XAES_BLOCK_SIZE;
}

*pLength = nDataLength;
*pLength = nPaddingLen;
pOutput[*pLength] = '\0';

free(pInputPtr);
return pOutput;
}

uint8_t* XAES_Decrypt(xaes_context_t *pCtx, const uint8_t *pInput, size_t *pLength)
uint8_t* XAES_Decrypt(xaes_context_t *pCtx, const uint8_t *pInput, size_t *pLength)
{
if (pInput == NULL || pLength == NULL || !(*pLength)) return NULL;
uint8_t *pInputPtr = NULL;
size_t nDataLength = 0;

uint8_t iv[XAES_BLOCK_SIZE];
memcpy(iv, pCtx->IV, sizeof(iv));
size_t nInputLength = *pLength;
if (nInputLength % XAES_BLOCK_SIZE != 0) return NULL;

if (*pLength % XAES_BLOCK_SIZE)
{
size_t nCount = *pLength / XAES_BLOCK_SIZE;
size_t nPart = *pLength - nCount * XAES_BLOCK_SIZE;
nDataLength = *pLength + (XAES_BLOCK_SIZE - nPart);
}

if (nDataLength)
{
pInputPtr = (uint8_t*)malloc(nDataLength + 1);
if (pInputPtr == NULL) return NULL;
uint8_t *pOutput = (uint8_t*)malloc(nInputLength + 1);
if (!pOutput) return NULL;

memcpy(pInputPtr, pInput, *pLength);
memset(pInputPtr + *pLength, 0, nDataLength - *pLength);
}

const uint8_t *pData = pInputPtr != NULL ? pInputPtr : pInput;
nDataLength = nDataLength ? nDataLength : *pLength;

uint8_t *pOutput = (uint8_t*)malloc(nDataLength + 1);
if (pOutput == NULL)
{
free(pInputPtr);
return NULL;
}
uint8_t iv[XAES_BLOCK_SIZE];
memcpy(iv, pCtx->IV, sizeof(iv));

const uint8_t *pData = pInput;
uint8_t *pOffset = pOutput;
size_t nDataLeft = nDataLength;
size_t nDataLeft = nInputLength;

while (nDataLeft > 0)
{
uint8_t i, ivTmp[XAES_BLOCK_SIZE];
uint8_t ivTmp[XAES_BLOCK_SIZE];
memcpy(ivTmp, pData, XAES_BLOCK_SIZE);
XAES_DecryptBlock(pCtx, pOffset, pData);

size_t i;
for (i = 0; i < XAES_BLOCK_SIZE; i++)
pOffset[i] = pOffset[i] ^ iv[i];
pOffset[i] ^= iv[i];

memcpy(iv, ivTmp, XAES_BLOCK_SIZE);
nDataLeft -= XAES_BLOCK_SIZE;
pOffset += XAES_BLOCK_SIZE;
pData += XAES_BLOCK_SIZE;
}

pOutput[nDataLength] = '\0';
*pLength = nDataLength;
// Remove PKCS#7 padding
uint8_t nPadding = pOutput[nInputLength - 1];
if (!nPadding || nPadding > XAES_BLOCK_SIZE)
{
free(pOutput);
return NULL;
}

size_t i;
for (i = 0; i < nPadding; i++)
{
if (pOutput[nInputLength - 1 - i] != nPadding)
{
free(pOutput);
return NULL;
}
}

while (nDataLength && pOutput[nDataLength] == '\0') nDataLength--;
if (*pLength != nDataLength) *pLength = nDataLength + 1;
*pLength = nInputLength - nPadding;
pOutput[*pLength] = '\0';

free(pInputPtr);
return pOutput;
}

////////////////////////////////////////////////////////////////////////////////////////
// END OF: libxutils implementation to encode and decode input buffers of various sizes
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
2 changes: 1 addition & 1 deletion src/xver.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#define XUTILS_VERSION_MAX 2
#define XUTILS_VERSION_MIN 6
#define XUTILS_BUILD_NUMBER 15
#define XUTILS_BUILD_NUMBER 16

#ifdef __cplusplus
extern "C" {
Expand Down

0 comments on commit ab6dd3c

Please sign in to comment.