Small portable AES128/192/256 in C

Overview

CI

Tiny AES in C

This is a small and portable implementation of the AES ECB, CTR and CBC encryption algorithms written in C.

You can override the default key-size of 128 bit with 192 or 256 bit by defining the symbols AES192 or AES256 in aes.h.

The API is very simple and looks like this (I am using C99 <stdint.h>-style annotated types):

/* 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);

/* 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);

Important notes:

  • No padding is provided so for CBC and ECB all buffers should be multiples of 16 bytes. For padding PKCS7 is recommendable.
  • ECB mode is considered unsafe for most uses and is not implemented in streaming mode. If you need this mode, call the function for every block of 16 bytes you need encrypted. See wikipedia's article on ECB for more details.
  • This library is designed for small code size and simplicity, intended for cases where small binary size, low memory footprint and portability is more important than high performance. If speed is a concern, you can try more complex libraries, e.g. Mbed TLS, OpenSSL etc.

You can choose to use any or all of the modes-of-operations, by defining the symbols CBC, CTR or ECB in aes.h (read the comments for clarification).

C++ users should #include aes.hpp instead of aes.h

There is no built-in error checking or protection from out-of-bounds memory access errors as a result of malicious input.

The module uses less than 200 bytes of RAM and 1-2K ROM when compiled for ARM, but YMMV depending on which modes are enabled.

It is one of the smallest implementations in C I've seen yet, but do contact me if you know of something smaller (or have improvements to the code here).

I've successfully used the code on 64bit x86, 32bit ARM and 8 bit AVR platforms.

GCC size output when only CTR mode is compiled for ARM:

$ arm-none-eabi-gcc -Os -DCBC=0 -DECB=0 -DCTR=1 -c aes.c
$ size aes.o
   text    data     bss     dec     hex filename
   1171       0       0    1171     493 aes.o

.. and when compiling for the THUMB instruction set, we end up well below 1K in code size.

$ arm-none-eabi-gcc -Os -mthumb -DCBC=0 -DECB=0 -DCTR=1 -c aes.c
$ size aes.o
   text    data     bss     dec     hex filename
    903       0       0     903     387 aes.o

I am using the Free Software Foundation, ARM GCC compiler:

$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (4.8.4-1+11-1) 4.8.4 20141219 (release)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This implementation is verified against the data in:

National Institute of Standards and Technology Special Publication 800-38A 2001 ED Appendix F: Example Vectors for Modes of Operation of the AES.

The other appendices in the document are valuable for implementation details on e.g. padding, generation of IVs and nonces in CTR-mode etc.

A heartfelt thank-you to all the nice people out there who have contributed to this project.

All material in this repository is in the public domain.

Issues
  • AES reversing

    AES reversing

    I'm testing with tiny-AES-c and I now have a question which is related to AES in general. Sorry I'm note sure if I can ask this kind of question here. If this wasn't the right place, please just /ignore my question and close it. The question: if someone want to attack some AES-CBC encripted data to gain the key, does they have any advantage if both a CRC32 of the original (plain) data is available along with CRC polynomial used? Thanks

    opened by skater-boy 0
  • aes.h comment error

    aes.h comment error

    https://github.com/kokke/tiny-AES-c/blob/f06ac37fc31dfdaca2e0d9bec83f90d5663c319b/aes.h#L65

    #endif // #if defined(ECB) && (ECB == !)

    I guess it should be #endif // #if defined(ECB) && (ECB == 1)

    opened by wsw109 0
  • How to pass the data type as string for key and plain_test varibles  in test_encrypt_ecb_verbose method

    How to pass the data type as string for key and plain_test varibles in test_encrypt_ecb_verbose method

    HI

    In the method test_encrypt_ecb_verbose, how can we use the key and plain text variable as string input. eg: key[16]={"1234567890123456"}; plain_text[16]={''0987654321123456"}; //Can we use plain text as 16 instead of 64 as array size?

    Thanks

    opened by Yogeshkc7 2
  • Use PROGMEM for lookup tables on AVR platforms

    Use PROGMEM for lookup tables on AVR platforms

    This PR moves the static sbox, rsbox and Rcon substitution boxes from the SRAM into the Flash memory on AVR microcontrollers, which is typically larger. This frees up half a kilobyte of RAM.

    The PROGMEM attribute is used to specify the memory location, and the pgm_read_byte to access the Flash memory.

    opened by StarGate01 9
Releases(v1.0.0)
LibreSSL Portable itself. This includes the build scaffold and compatibility layer that builds portable LibreSSL from the OpenBSD source code.

LibreSSL Portable itself. This includes the build scaffold and compatibility layer that builds portable LibreSSL from the OpenBSD source code.

OpenBSD LibreSSL Portable 1.1k Jul 1, 2022
Mbedcrypto - a portable, small, easy to use and fast c++14 library for cryptography.

mbedcrypto mbedcrypto is a portable, small, easy to use, feature rich and fast c++14 library for cryptography based on fantastic and clean mbedtlsnote

amir zamani 37 Jun 16, 2022
A modern, portable, easy to use crypto library.

Sodium is a new, easy-to-use software library for encryption, decryption, signatures, password hashing and more. It is a portable, cross-compilable, i

Frank Denis 10.2k Jun 25, 2022
An open source, portable, easy to use, readable and flexible SSL library

README for Mbed TLS Mbed TLS is a C library that implements cryptographic primitives, X.509 certificate manipulation and the SSL/TLS and DTLS protocol

Arm Mbed 3.6k Jun 30, 2022
A small HOTP/TOTP SHA1 client written in C, depending only on libcrypto (OpenSSL)

A small HOTP/TOTP SHA1 client written in C, depending only on libcrypto (OpenSSL)

null 3 Jan 21, 2022
Small collection of tools written in C for ECC and bitcoin

ecctools Small collection of tools written in C for ECC and bitcoin Why this programs are written in C language? Well i like C language because compil

Luis Alberto 18 Jun 15, 2022
A small library for Asymmetric cryptography, otherwise known as public-key cryptography.

crypto-library A small library for Asymmetric cryptography, otherwise known as public-key cryptography. Self study Resources: https://en.wikipedia.org

thescientist 1 Dec 16, 2021
UnrealKey is a tool for automatically finding the AES-256 decryption keys for Unreal Engine 4 encrypted pak files.

UnrealKey UnrealKey is a tool for automatically finding the AES-256 decryption keys for Unreal Engine 4 encrypted pak files.

Devin Acker 22 Apr 24, 2022
3D GPUs Strange Attractors and Hypercomplex Fractals explorer - up to 256 Million particles in RealTime

glChAoS.P ⋅ wglChAoS.P - Ver 1.5.3 glChAoS.P / wglChAoS.P ⋅ opengl / webgl ⋅ Chaotic Attractors of Slight (dot) Particles RealTime 3D Strange Attracto

Michele Morrone 680 Jun 26, 2022
LibreSSL Portable itself. This includes the build scaffold and compatibility layer that builds portable LibreSSL from the OpenBSD source code.

LibreSSL Portable itself. This includes the build scaffold and compatibility layer that builds portable LibreSSL from the OpenBSD source code.

OpenBSD LibreSSL Portable 1.1k Jul 1, 2022
A small and portable INI file library with read/write support

minIni minIni is a portable and configurable library for reading and writing ".INI" files. At just below 900 lines of commented source code, minIni tr

Thiadmer Riemersma 257 Jun 20, 2022
Skylark Edit is a customizable text/hex editor. Small, Portable, Fast.

Skylark Edit is written in C, a high performance text/hex editor. Embedded Database-client/Redis-client/Lua-engine. You can run Lua scripts and SQL files directly.

hua andy 91 Jun 27, 2022
C.impl is a small portable C interpreter integrated with a line text editor

C.impl C.impl is a small portable C interpreter integrated with a line text editor, originally developed for the ELLO 1A computer: http://ello.cc The

KnivD 19 Jun 9, 2022
F Graphics Library (FGL) is a small graphics C++ portable library for LCD displays on embedded systems

F Graphics Library (FGL) Full documentation: fgl.docsforge.com (By Filipe Chagas) F Graphics Library is a C++ library that I created for use in embedd

Filipe Chagas 8 Dec 14, 2021
Mbedcrypto - a portable, small, easy to use and fast c++14 library for cryptography.

mbedcrypto mbedcrypto is a portable, small, easy to use, feature rich and fast c++14 library for cryptography based on fantastic and clean mbedtlsnote

amir zamani 37 Jun 16, 2022
A small fast portable speech synthesis system

Flite is an open source small fast run-time text to speech engine. It is the latest addition to the suite of free software synthesis tools including University of Edinburgh's Festival Speech Synthesis System and Carnegie Mellon University's FestVox project, tools, scripts and documentation for building synthetic voices.

CMU Festvox Project 554 Jun 23, 2022
C++11 header-only library that offers small vector, small flat map/set/multimap/multiset.

sfl library This is header-only C++11 library that offers several new containers: small_vector small_flat_set small_flat_map small_flat_multiset small

null 10 Jun 30, 2022
Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more

Apache MXNet (incubating) for Deep Learning Apache MXNet is a deep learning framework designed for both efficiency and flexibility. It allows you to m

The Apache Software Foundation 20k Jun 23, 2022
A modern, portable, easy to use crypto library.

Sodium is a new, easy-to-use software library for encryption, decryption, signatures, password hashing and more. It is a portable, cross-compilable, i

Frank Denis 10.2k Jun 25, 2022
An open source, portable, easy to use, readable and flexible SSL library

README for Mbed TLS Mbed TLS is a C library that implements cryptographic primitives, X.509 certificate manipulation and the SSL/TLS and DTLS protocol

Arm Mbed 3.6k Jun 30, 2022
Simple and portable (but not inflexible) GUI library in C that uses the native GUI technologies of each platform it supports.

libui: a portable GUI library for C This README is being written. Status It has come to my attention that I have not been particularly clear about how

Pietro Gagliardi 10.3k Jun 30, 2022
Very low footprint JSON parser written in portable ANSI C

Very low footprint JSON parser written in portable ANSI C. BSD licensed with no dependencies (i.e. just drop the C file into your project) Never recur

James McLaughlin 1.2k Jun 15, 2022
Portable, simple and extensible C++ logging library

Plog - portable, simple and extensible C++ logging library Pretty powerful logging library in about 1000 lines of code Introduction Hello log! Feature

Sergey Podobry 1.5k Jun 30, 2022
Scalable, Portable and Distributed Gradient Boosting (GBDT, GBRT or GBM) Library, for Python, R, Java, Scala, C++ and more. Runs on single machine, Hadoop, Spark, Dask, Flink and DataFlow

eXtreme Gradient Boosting Community | Documentation | Resources | Contributors | Release Notes XGBoost is an optimized distributed gradient boosting l

Distributed (Deep) Machine Learning Community 22.9k Jul 1, 2022
LibTomMath is a free open source portable number theoretic multiple-precision integer library written entirely in C.

libtommath This is the git repository for LibTomMath, a free open source portable number theoretic multiple-precision integer (MPI) library written en

libtom 520 Jun 25, 2022
libass is a portable subtitle renderer for the ASS/SSA (Advanced Substation Alpha/Substation Alpha) subtitle format.

libass libass is a portable subtitle renderer for the ASS/SSA (Advanced Substation Alpha/Substation Alpha) subtitle format. It is mostly compatible wi

null 660 Jun 25, 2022
A portable MQTT C client for embedded systems and PCs alike.

MQTT-C is an MQTT v3.1.1 client written in C. MQTT is a lightweight publisher-subscriber-based messaging protocol that is commonly used in IoT and net

Liam Bindle 500 Jun 22, 2022