Skip to content

Commit 0010ca2

Browse files
authored
Switched AES library from FIPS to tiny-AES (#11)
* Added CBC mode chaining with PKCS7 padding * Refactored tiny-AES-c and adjusted API * Fixed interrupt flag initialization in xsrc
1 parent 2de2930 commit 0010ca2

File tree

7 files changed

+388
-612
lines changed

7 files changed

+388
-612
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ examples/files.copy
5656
examples/build/
5757
tools/build/
5858
build/
59-
.vscode/
6059
*.log
6160

6261
# Debug files
6362
*.dmp
6463
*.stackdump
6564
vgcore.*
65+
66+
# Editors and agents
67+
.claude/
68+
.cursor/
69+
.vscode/
70+
.idea/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#### Cryptography:
3232
- [Implementation of various encrypt/decrypt algorithms](https://github.com/kala13x/libxutils/blob/main/src/crypt/crypt.h)
3333
- [Base64 and Base64Url encrypt/decrypt functions](https://github.com/kala13x/libxutils/blob/main/src/crypt/base64.h)
34-
- [AES based on FIPS-197 implementation by Christophe Devine](https://github.com/kala13x/libxutils/blob/main/src/crypt/aes.h)
34+
- [AES based on tiny-AES-c, added CBC mode with PKCS7 padding](https://github.com/kala13x/libxutils/blob/main/src/crypt/aes.h)
3535
- [Implementation of HMAC algorithm with SHA256 and MD5](https://github.com/kala13x/libxutils/blob/main/src/crypt/hmac.h)
3636
- [RSA implementation based on OpenSSL library](https://github.com/kala13x/libxutils/blob/main/src/crypt/rsa.h)
3737
- [Implementation of SHA256 calculations](https://github.com/kala13x/libxutils/blob/main/src/crypt/sha256.h)

src/crypt/aes.c

Lines changed: 353 additions & 588 deletions
Large diffs are not rendered by default.

src/crypt/aes.h

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
* @file libxutils/src/crypt/aes.h
33
*
44
* This source is part of "libxutils" project
5-
* 2015-2020 Sun Dro (s.kalatoz@gmail.com)
5+
* 2015-2025 Sun Dro (s.kalatoz@gmail.com)
66
*
7-
* @brief Implementation of Advanced Encryption Standard
8-
* based on FIPS-197 implementation by Christophe Devine.
7+
* @brief Implementation of AES encryption based on tiny-AES-c project,
8+
* which was released under The Unlicense (public domain dedication).
9+
*
10+
* Modified: Refactored code, adjusted API, and added CBC mode with PKCS#7 padding.
911
*/
1012

1113
#ifndef __XUTILS_AES_H__
@@ -19,22 +21,25 @@
1921
extern "C" {
2022
#endif
2123

24+
#define XAES_RKEY_SIZE 240
2225
#define XAES_BLOCK_SIZE 16
23-
#define XAES_RKEY_SIZE 64
2426

2527
typedef struct AESContext {
26-
uint32_t encKeys[XAES_RKEY_SIZE]; /* Dncryption round keys */
27-
uint32_t decKeys[XAES_RKEY_SIZE]; /* Decryption round keys */
28+
uint8_t roundKey[XAES_RKEY_SIZE]; /* Encrypt/Decrypt round key */
2829
uint8_t IV[XAES_BLOCK_SIZE]; /* Initialization vector */
29-
size_t nRounds; /* Number of rounds */
30-
} xaes_context_t;
31-
32-
void XAES_SetKey(xaes_context_t *pCtx, const uint8_t *pKey, size_t nSize, const uint8_t *pIV);
33-
void XAES_EncryptBlock(xaes_context_t *pCtx, uint8_t output[XAES_BLOCK_SIZE], const uint8_t input[XAES_BLOCK_SIZE]);
34-
void XAES_DecryptBlock(xaes_context_t *pCtx, uint8_t output[XAES_BLOCK_SIZE], const uint8_t input[XAES_BLOCK_SIZE]);
35-
36-
uint8_t* XAES_Encrypt(xaes_context_t *pCtx, const uint8_t *pInput, size_t *pLength);
37-
uint8_t* XAES_Decrypt(xaes_context_t *pCtx, const uint8_t *pInput, size_t *pLength);
30+
uint8_t nSelfContainedIV; /* Flag to indicate if IV is self-contained in data */
31+
size_t nKeySize; /* Key size in bits */
32+
uint8_t nNB; /* Number of columns (32-bit words) comprising the State */
33+
uint8_t nNK; /* Number of 32-bit words comprising the Cipher Key */
34+
uint8_t nNR; /* Number of rounds */
35+
} xaes_ctx_t;
36+
37+
int XAES_SetKey(xaes_ctx_t *pCtx, const uint8_t *pKey, size_t nSize, const uint8_t *pIV, uint8_t nSelfContainedIV);
38+
void XAES_EncryptBlock(const xaes_ctx_t* pCtx, uint8_t* pBuffer);
39+
void XAES_DecryptBlock(const xaes_ctx_t* pCtx, uint8_t* pBuffer);
40+
41+
uint8_t* XAES_Encrypt(xaes_ctx_t *pCtx, const uint8_t *pInput, size_t *pLength);
42+
uint8_t* XAES_Decrypt(xaes_ctx_t *pCtx, const uint8_t *pInput, size_t *pLength);
3843

3944
#ifdef __cplusplus
4045
}

src/crypt/crypt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ uint8_t* XCrypt_AES(const uint8_t *pInput, size_t *pLength, const uint8_t *pKey,
196196
if (pInput == NULL || pKey == NULL || !nKeyLen ||
197197
pLength == NULL || !(*pLength)) return NULL;
198198

199-
xaes_context_t ctx;
200-
XAES_SetKey(&ctx, pKey, nKeyLen, pIV);
199+
xaes_ctx_t ctx;
200+
XAES_SetKey(&ctx, pKey, nKeyLen, pIV, XFALSE);
201201
return XAES_Encrypt(&ctx, pInput, pLength);
202202
}
203203

@@ -206,8 +206,8 @@ uint8_t* XDecrypt_AES(const uint8_t *pInput, size_t *pLength, const uint8_t *pKe
206206
if (pInput == NULL || pKey == NULL || !nKeyLen ||
207207
pLength == NULL || !(*pLength)) return NULL;
208208

209-
xaes_context_t ctx;
210-
XAES_SetKey(&ctx, pKey, nKeyLen, pIV);
209+
xaes_ctx_t ctx;
210+
XAES_SetKey(&ctx, pKey, nKeyLen, pIV, XFALSE);
211211
return XAES_Decrypt(&ctx, pInput, pLength);
212212
}
213213

src/sys/srch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ void XSearch_ClearCb(xarray_data_t *pArrData)
405405

406406
void XSearch_Init(xsearch_t *pSrcCtx, const char *pFileName)
407407
{
408+
pSrcCtx->nInterrupted = XFALSE;
408409
pSrcCtx->pInterrupted = &pSrcCtx->nInterrupted;
409410
pSrcCtx->bInsensitive = XFALSE;
410411
pSrcCtx->bSearchLines = XFALSE;

tools/xcrypt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define XCRYPT_VER_MIN 1
2727
#define XCRYPT_BUILD_NUM 25
2828

29-
#define XAES_KEY_LENGTH 256
29+
#define XAES_KEY_SIZE 256
3030
#define XHEX_COLUMNS 16
3131
extern char *optarg;
3232

@@ -501,9 +501,9 @@ static xbool_t XCrypt_ParseArgs(xcrypt_args_t *pArgs, int argc, char *argv[])
501501
{
502502
#ifdef _XUTILS_USE_SSL
503503
pArgs->nKeySize = xstrused(pArgs->sPair) ?
504-
XRSA_KEY_SIZE : XAES_KEY_LENGTH;
504+
XRSA_KEY_SIZE : XAES_KEY_SIZE;
505505
#else
506-
pArgs->nKeySize = XAES_KEY_LENGTH;
506+
pArgs->nKeySize = XAES_KEY_SIZE;
507507
#endif
508508
}
509509

0 commit comments

Comments
 (0)