tiny-aes-c Documentation

repository·master·Indexed 26 days ago

https://github.com/kokke/tiny-aes-c

A small, portable, and lightweight C implementation of AES supporting ECB, CTR, and CBC modes. Designed for embedded systems prioritizing small binary size and low memory footprint over high performance. Supports 128-bit, 192-bit, and 256-bit key sizes via configuration symbols.

Tokens
788
Snippets
2
Records
4
Agent score
40%

What's inside tiny-aes-c

  1. Configure AES key size and modes of operation

    master

    You can customize the AES implementation by defining specific symbols in aes.h before compilation:

    Key Size

    The default key size is 128-bit. To use other sizes, define:

    • AES192 for 192-bit
    • AES256 for 256-bit

    Modes of Operation

    To enable specific modes, define the corresponding symbols:

    • CBC
    • CTR
    • ECB
  2. Initialize AES context

    master

    Use the following functions to set up the AES_ctx structure. The library uses C99 <stdint.h> annotated types.

    • AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key): Initializes the context with a provided key.
    • AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv): Initializes the context with a key and an Initialization Vector (IV).
    • AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv): Resets the IV at a random point during operation.
    /* Initialize context calling one of: */
    void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
    void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
    
    /* ... or reset IV at random point: */
    void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
  3. Encrypt and decrypt using AES modes

    master

    Once the context is initialized, use the mode-specific functions to process data.

    Important Requirements:

    • Padding: The library does not provide built-in padding. For CBC and ECB modes, all buffers must be multiples of 16 bytes. It is recommended to use [PKCS7] padding manually.
    • ECB Safety: ECB mode is considered unsafe for most uses and does not support streaming. If using ECB, call the function for every 16-byte block required.
    • Error Checking: There is no built-in error checking or protection against out-of-bounds memory access from malicious input.
    /* Then start encrypting and decrypting with the functions below: */
    void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
    void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
    
    void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
    void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
    
    /* Same function for encrypting as for decrypting in CTR mode */
    void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);