Changeset d7dadcb4 in mainline for uspace/lib/crypto/aes.c


Ignore:
Timestamp:
2015-04-08T10:54:31Z (9 years ago)
Author:
Jan Kolarik <kolarik@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
053fc2b
Parents:
1dcc0b9
Message:

RC4 and MD5 algorithm used in TKIP/WEP security suites

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/crypto/aes.c

    r1dcc0b9 rd7dadcb4  
    2929/** @file aes.c
    3030 *
    31  * Implementation of AES algorithm.
     31 * Implementation of AES-128 symmetric cipher cryptographic algorithm.
    3232 *
    3333 * Based on FIPS 197.
    3434 */
    3535
    36 #include <stdio.h>
     36#include <stdbool.h>
    3737#include <errno.h>
    3838#include <mem.h>
     
    6060
    6161/* Precomputed values for AES sub_byte transformation. */
    62 static const uint8_t sub_byte_array[BLOCK_LEN][BLOCK_LEN] = {
     62static const uint8_t sbox[BLOCK_LEN][BLOCK_LEN] = {
    6363        {
    6464                0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
     
    128128
    129129/* Precomputed values for AES inv_sub_byte transformation. */
    130 static uint8_t inv_sub_byte_array[BLOCK_LEN][BLOCK_LEN] = {
     130static uint8_t inv_sbox[BLOCK_LEN][BLOCK_LEN] = {
    131131        {
    132132                0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38,
     
    202202};
    203203
     204/**
     205 * Perform substitution transformation on given byte.
     206 *
     207 * @param byte Input byte.
     208 * @param inv Flag indicating whether to use inverse table.
     209 *
     210 * @return Substituted value.
     211 */
    204212static uint8_t sub_byte(uint8_t byte, bool inv)
    205213{
     
    208216       
    209217        if(!inv) {
    210                 return sub_byte_array[i][j];
     218                return sbox[i][j];
    211219        } else {
    212                 return inv_sub_byte_array[i][j];
    213         }
    214 }
    215 
     220                return inv_sbox[i][j];
     221        }
     222}
     223
     224/**
     225 * Perform substitution transformation on state table.
     226 *
     227 * @param state State table to be modified.
     228 * @param inv Flag indicating whether to use inverse table.
     229 */
    216230static void sub_bytes(uint8_t state[ELEMS][ELEMS], bool inv)
    217231{
     
    226240}
    227241
     242/**
     243 * Perform shift rows transformation on state table.
     244 *
     245 * @param state State table to be modified.
     246 */
    228247static void shift_rows(uint8_t state[ELEMS][ELEMS])
    229248{
     
    237256}
    238257
     258/**
     259 * Perform inverted shift rows transformation on state table.
     260 *
     261 * @param state State table to be modified.
     262 */
    239263static void inv_shift_rows(uint8_t state[ELEMS][ELEMS])
    240264{
     
    248272}
    249273
     274/**
     275 * Multiplication in GF(2^8).
     276 *
     277 * @param x First factor.
     278 * @param y Second factor.
     279 *
     280 * @return Multiplication of given factors in GF(2^8).
     281 */
    250282static uint8_t galois_mult(uint8_t x, uint8_t y) {
    251283        uint8_t result = 0;
     
    265297}
    266298
     299/**
     300 * Perform mix columns transformation on state table.
     301 *
     302 * @param state State table to be modified.
     303 */
    267304static void mix_columns(uint8_t state[ELEMS][ELEMS])
    268305{
     
    294331}
    295332
     333/**
     334 * Perform inverted mix columns transformation on state table.
     335 *
     336 * @param state State table to be modified.
     337 */
    296338static void inv_mix_columns(uint8_t state[ELEMS][ELEMS])
    297339{
     
    323365}
    324366
     367/**
     368 * Perform round key transformation on state table.
     369 *
     370 * @param state State table to be modified.
     371 * @param round_key Round key to be applied on state table.
     372 */
    325373static void add_round_key(uint8_t state[ELEMS][ELEMS], uint32_t *round_key)
    326374{
     
    338386}
    339387
     388/**
     389 * Perform substitution transformation on given word.
     390 *
     391 * @param byte Input word.
     392 *
     393 * @return Substituted word.
     394 */
    340395static uint32_t sub_word(uint32_t word)
    341396{
     
    350405}
    351406
     407/**
     408 * Perform left rotation by one byte on given word.
     409 *
     410 * @param byte Input word.
     411 *
     412 * @return Rotated word.
     413 */
    352414static uint32_t rot_word(uint32_t word)
    353415{
     
    355417}
    356418
     419/**
     420 * Key expansion procedure for AES algorithm.
     421 *
     422 * @param key Input key.
     423 * @param key_exp Result key expansion.
     424 */
    357425static void key_expansion(uint8_t *key, uint32_t *key_exp)
    358426{
     
    379447}
    380448
     449/**
     450 * AES-128 encryption algorithm.
     451 *
     452 * @param key Input key.
     453 * @param input Input data sequence to be encrypted.
     454 * @param output Encrypted data sequence.
     455 *
     456 * @return EINVAL when input or key not specified, ENOMEM when pointer for
     457 * output is not allocated, otherwise EOK. 
     458 */
    381459int aes_encrypt(uint8_t *key, uint8_t *input, uint8_t *output)
    382460{
     
    404482        for(size_t k = 1; k <= ROUNDS; k++) {
    405483                sub_bytes(state, false);
    406                 for(size_t i = 0; i < ELEMS; i++) {
    407                         for(size_t j = 0; j < ELEMS; j++) {
    408                                 printf("STATE SUB %d %d : %x\n", i, j, state[i][j]);
    409                         }
    410                 }
    411484                shift_rows(state);
    412                 for(size_t i = 0; i < ELEMS; i++) {
    413                         for(size_t j = 0; j < ELEMS; j++) {
    414                                 printf("STATE SHIFT %d %d : %x\n", i, j, state[i][j]);
    415                         }
    416                 }
    417485                if(k < ROUNDS)
    418486                        mix_columns(state);
     
    430498}
    431499
     500/**
     501 * AES-128 decryption algorithm.
     502 *
     503 * @param key Input key.
     504 * @param input Input data sequence to be decrypted.
     505 * @param output Decrypted data sequence.
     506 *
     507 * @return EINVAL when input or key not specified, ENOMEM when pointer for
     508 * output is not allocated, otherwise EOK. 
     509 */
    432510int aes_decrypt(uint8_t *key, uint8_t *input, uint8_t *output)
    433511{
Note: See TracChangeset for help on using the changeset viewer.