Home | History | Annotate | Download | only in smp
      1 /*
      2  ---------------------------------------------------------------------------
      3  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
      4 
      5  LICENSE TERMS
      6 
      7  The redistribution and use of this software (with or without changes)
      8  is allowed without the payment of fees or royalties provided that:
      9 
     10   1. source code distributions include the above copyright notice, this
     11      list of conditions and the following disclaimer;
     12 
     13   2. binary distributions include the above copyright notice, this list
     14      of conditions and the following disclaimer in their documentation;
     15 
     16   3. the name of the copyright holder is not used to endorse products
     17      built using this software without specific written permission.
     18 
     19  DISCLAIMER
     20 
     21  This software is provided 'as is' with no explicit or implied warranties
     22  in respect of its properties, including, but not limited to, correctness
     23  and/or fitness for purpose.
     24  ---------------------------------------------------------------------------
     25  Issue 09/09/2006
     26 
     27  This is an AES implementation that uses only 8-bit byte operations on the
     28  cipher state.
     29  */
     30 
     31 #ifndef AES_H
     32 #define AES_H
     33 
     34 #if 1
     35 #  define AES_ENC_PREKEYED  /* AES encryption with a precomputed key schedule  */
     36 #endif
     37 #if 1
     38 #  define AES_DEC_PREKEYED  /* AES decryption with a precomputed key schedule  */
     39 #endif
     40 #if 1
     41 #  define AES_ENC_128_OTFK  /* AES encryption with 'on the fly' 128 bit keying */
     42 #endif
     43 #if 1
     44 #  define AES_DEC_128_OTFK  /* AES decryption with 'on the fly' 128 bit keying */
     45 #endif
     46 #if 1
     47 #  define AES_ENC_256_OTFK  /* AES encryption with 'on the fly' 256 bit keying */
     48 #endif
     49 #if 1
     50 #  define AES_DEC_256_OTFK  /* AES decryption with 'on the fly' 256 bit keying */
     51 #endif
     52 
     53 #define N_ROW                   4
     54 #define N_COL                   4
     55 #define N_BLOCK   (N_ROW * N_COL)
     56 #define N_MAX_ROUNDS           14
     57 
     58 typedef unsigned char uint_8t;
     59 
     60 typedef uint_8t return_type;
     61 
     62 /*  Warning: The key length for 256 bit keys overflows a byte
     63     (see comment below)
     64 */
     65 
     66 typedef uint_8t length_type;
     67 
     68 typedef struct
     69 {   uint_8t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
     70     uint_8t rnd;
     71 } aes_context;
     72 
     73 /*  The following calls are for a precomputed key schedule
     74 
     75     NOTE: If the length_type used for the key length is an
     76     unsigned 8-bit character, a key length of 256 bits must
     77     be entered as a length in bytes (valid inputs are hence
     78     128, 192, 16, 24 and 32).
     79 */
     80 
     81 #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
     82 
     83 return_type aes_set_key( const unsigned char key[],
     84                          length_type keylen,
     85                          aes_context ctx[1] );
     86 #endif
     87 
     88 #if defined( AES_ENC_PREKEYED )
     89 
     90 return_type aes_encrypt( const unsigned char in[N_BLOCK],
     91                          unsigned char out[N_BLOCK],
     92                          const aes_context ctx[1] );
     93 
     94 return_type aes_cbc_encrypt( const unsigned char *in,
     95                          unsigned char *out,
     96                          int n_block,
     97                          unsigned char iv[N_BLOCK],
     98                          const aes_context ctx[1] );
     99 #endif
    100 
    101 #if defined( AES_DEC_PREKEYED )
    102 
    103 return_type aes_decrypt( const unsigned char in[N_BLOCK],
    104                          unsigned char out[N_BLOCK],
    105                          const aes_context ctx[1] );
    106 
    107 return_type aes_cbc_decrypt( const unsigned char *in,
    108                          unsigned char *out,
    109                          int n_block,
    110                          unsigned char iv[N_BLOCK],
    111                          const aes_context ctx[1] );
    112 #endif
    113 
    114 /*  The following calls are for 'on the fly' keying.  In this case the
    115     encryption and decryption keys are different.
    116 
    117     The encryption subroutines take a key in an array of bytes in
    118     key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
    119     192, and 256 bits respectively.  They then encrypts the input
    120     data, in[] with this key and put the reult in the output array
    121     out[].  In addition, the second key array, o_key[L], is used
    122     to output the key that is needed by the decryption subroutine
    123     to reverse the encryption operation.  The two key arrays can
    124     be the same array but in this case the original key will be
    125     overwritten.
    126 
    127     In the same way, the decryption subroutines output keys that
    128     can be used to reverse their effect when used for encryption.
    129 
    130     Only 128 and 256 bit keys are supported in these 'on the fly'
    131     modes.
    132 */
    133 
    134 #if defined( AES_ENC_128_OTFK )
    135 void aes_encrypt_128( const unsigned char in[N_BLOCK],
    136                       unsigned char out[N_BLOCK],
    137                       const unsigned char key[N_BLOCK],
    138                       uint_8t o_key[N_BLOCK] );
    139 #endif
    140 
    141 #if defined( AES_DEC_128_OTFK )
    142 void aes_decrypt_128( const unsigned char in[N_BLOCK],
    143                       unsigned char out[N_BLOCK],
    144                       const unsigned char key[N_BLOCK],
    145                       unsigned char o_key[N_BLOCK] );
    146 #endif
    147 
    148 #if defined( AES_ENC_256_OTFK )
    149 void aes_encrypt_256( const unsigned char in[N_BLOCK],
    150                       unsigned char out[N_BLOCK],
    151                       const unsigned char key[2 * N_BLOCK],
    152                       unsigned char o_key[2 * N_BLOCK] );
    153 #endif
    154 
    155 #if defined( AES_DEC_256_OTFK )
    156 void aes_decrypt_256( const unsigned char in[N_BLOCK],
    157                       unsigned char out[N_BLOCK],
    158                       const unsigned char key[2 * N_BLOCK],
    159                       unsigned char o_key[2 * N_BLOCK] );
    160 #endif
    161 
    162 #endif
    163