|
2 | 2 | * @file libxutils/src/crypt/aes.h |
3 | 3 | * |
4 | 4 | * 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) |
6 | 6 | * |
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. |
9 | 11 | */ |
10 | 12 |
|
11 | 13 | #ifndef __XUTILS_AES_H__ |
|
19 | 21 | extern "C" { |
20 | 22 | #endif |
21 | 23 |
|
| 24 | +#define XAES_RKEY_SIZE 240 |
22 | 25 | #define XAES_BLOCK_SIZE 16 |
23 | | -#define XAES_RKEY_SIZE 64 |
24 | 26 |
|
25 | 27 | 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 */ |
28 | 29 | 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); |
38 | 43 |
|
39 | 44 | #ifdef __cplusplus |
40 | 45 | } |
|
0 commit comments