"Fossies" - the Fresh Open Source Software Archive

Member "src/Crypto/AesSmall.h" (10 Oct 2018, 5398 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AesSmall.h" see the Fossies "Dox" file reference documentation.

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