• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    C
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Implementation of AES-128 in pure C. No modes are given. Only one block of encryption and decryption is given here.

AES-128

Build Status Coverity Scan Build Status

C implementation of AES-128. No modes are given. This code has been tested with GCC 4.8.4 and Valgrind-3.11.0 on Intel-i5-3230.

How to read

Reading of the paper AES Proposal: Rijndael first is recommended. If you care more about the implementation, Chapter 4 is enough.

  • Reference implementation: ./aes.c
    • This should be read first. Since it is the reference implementation.
  • Full unroll implementation: ./unroll/aes.c
    • The full unroll impelementation is easy to understand if you have read the reference one.
  • Look up table implementation: ./lut/aes.c
    • Lut may be something difficult. But it is widely used in softwares.
    • gen_dec_key_table.c, gen_enc_table.c and gen_dec_table.c are utils.
    • This code suppose the mechine is little-endian.
  • If you still have interest, the advanced implementations with SIMD instructions or hardware instructions can be found in some famous projects.

Usage

Interface

/**
 * @purpose:            Key schedule for AES-128
 * @par[in]key:         16 bytes of master keys
 * @par[out]roundkeys:  176 bytes of round keys
 */
void aes_key_schedule_128(const uint8_t *key, uint8_t *roundkeys);

/**
 * @purpose:            Encryption. The length of plain and cipher should be one block (16 bytes).
 *                      The plaintext and ciphertext may point to the same memory
 * @par[in]roundkeys:   round keys
 * @par[in]plaintext:   plain text
 * @par[out]ciphertext: cipher text
 */
void aes_encrypt_128(const uint8_t *roundkeys, const uint8_t *plaintext, uint8_t *ciphertext);

/**
 * @purpose:            Decryption. The length of plain and cipher should be one block (16 bytes).
 *                      The ciphertext and plaintext may point to the same memory
 * @par[in]roundkeys:   round keys
 * @par[in]ciphertext:  cipher text
 * @par[out]plaintext:  plain text
 */
void aes_decrypt_128(const uint8_t *roundkeys, const uint8_t *ciphertext, uint8_t *plaintext);

Compile

make

Dependencies

  • The code is written in standard C
  • No dependencies of other libraries

Contributing

If there is anything wrong or if you have any question, just make an issue or email me.

Todo

'Todo' may always be 'To do', so I do not like this word, but...

License

Copyright (c) 2017 LuoPeng

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.