Sat Aug 6 00:39:19 2011

Asterisk developer's documentation


aestab.c

Go to the documentation of this file.
00001 /*
00002  ---------------------------------------------------------------------------
00003  Copyright (c) 2003, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
00004  All rights reserved.
00005 
00006  LICENSE TERMS
00007 
00008  The free distribution and use of this software in both source and binary
00009  form is allowed (with or without changes) provided that:
00010 
00011    1. distributions of this source code include the above copyright
00012       notice, this list of conditions and the following disclaimer;
00013 
00014    2. distributions in binary form include the above copyright
00015       notice, this list of conditions and the following disclaimer
00016       in the documentation and/or other associated materials;
00017 
00018    3. the copyright holder's name is not used to endorse products
00019       built using this software without specific written permission.
00020 
00021  ALTERNATIVELY, provided that this notice is retained in full, this product
00022  may be distributed under the terms of the GNU General Public License (GPL),
00023  in which case the provisions of the GPL apply INSTEAD OF those given above.
00024 
00025  DISCLAIMER
00026 
00027  This software is provided 'as is' with no explicit or implied warranties
00028  in respect of its properties, including, but not limited to, correctness
00029  and/or fitness for purpose.
00030  ---------------------------------------------------------------------------
00031  Issue Date: 26/08/2003
00032 
00033 */
00034 
00035 #if defined(__cplusplus)
00036 extern "C"
00037 {
00038 #endif
00039 
00040 #define DO_TABLES
00041 
00042 #include "aesopt.h"
00043 
00044 #if defined(FIXED_TABLES)
00045 
00046 /* implemented in case of wrong call for fixed tables */
00047 
00048 void gen_tabs(void)
00049 {
00050 }
00051 
00052 #else   /* dynamic table generation */
00053 
00054 #if !defined(FF_TABLES)
00055 
00056 /*  Generate the tables for the dynamic table option
00057 
00058     It will generally be sensible to use tables to compute finite
00059     field multiplies and inverses but where memory is scarse this
00060     code might sometimes be better. But it only has effect during
00061     initialisation so its pretty unimportant in overall terms.
00062 */
00063 
00064 /*  return 2 ^ (n - 1) where n is the bit number of the highest bit
00065     set in x with x in the range 1 < x < 0x00000200.   This form is
00066     used so that locals within fi can be bytes rather than words
00067 */
00068 
00069 static aes_08t hibit(const aes_32t x)
00070 {   aes_08t r = (aes_08t)((x >> 1) | (x >> 2));
00071 
00072     r |= (r >> 2);
00073     r |= (r >> 4);
00074     return (r + 1) >> 1;
00075 }
00076 
00077 /* return the inverse of the finite field element x */
00078 
00079 static aes_08t fi(const aes_08t x)
00080 {   aes_08t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
00081 
00082     if(x < 2) return x;
00083 
00084     for(;;)
00085     {
00086         if(!n1) return v1;
00087 
00088         while(n2 >= n1)
00089         {
00090             n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
00091         }
00092 
00093         if(!n2) return v2;
00094 
00095         while(n1 >= n2)
00096         {
00097             n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
00098         }
00099     }
00100 }
00101 
00102 #endif
00103 
00104 /* The forward and inverse affine transformations used in the S-box */
00105 
00106 #define fwd_affine(x) \
00107     (w = (aes_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(aes_08t)(w^(w>>8)))
00108 
00109 #define inv_affine(x) \
00110     (w = (aes_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(aes_08t)(w^(w>>8)))
00111 
00112 static int init = 0;
00113 
00114 void gen_tabs(void)
00115 {   aes_32t  i, w;
00116 
00117 #if defined(FF_TABLES)
00118 
00119     aes_08t  pow[512], log[256];
00120 
00121     if(init) return;
00122     /*  log and power tables for GF(2^8) finite field with
00123         WPOLY as modular polynomial - the simplest primitive
00124         root is 0x03, used here to generate the tables
00125     */
00126 
00127     i = 0; w = 1;
00128     do
00129     {
00130         pow[i] = (aes_08t)w;
00131         pow[i + 255] = (aes_08t)w;
00132         log[w] = (aes_08t)i++;
00133         w ^=  (w << 1) ^ (w & 0x80 ? WPOLY : 0);
00134     }
00135     while (w != 1);
00136 
00137 #else
00138     if(init) return;
00139 #endif
00140 
00141     for(i = 0, w = 1; i < RC_LENGTH; ++i)
00142     {
00143         t_set(r,c)[i] = bytes2word(w, 0, 0, 0);
00144         w = f2(w);
00145     }
00146 
00147     for(i = 0; i < 256; ++i)
00148     {   aes_08t    b;
00149 
00150         b = fwd_affine(fi((aes_08t)i));
00151         w = bytes2word(f2(b), b, b, f3(b));
00152 
00153 #ifdef  SBX_SET
00154         t_set(s,box)[i] = b;
00155 #endif
00156 
00157 #ifdef  FT1_SET                 /* tables for a normal encryption round */
00158         t_set(f,n)[i] = w;
00159 #endif
00160 #ifdef  FT4_SET
00161         t_set(f,n)[0][i] = w;
00162         t_set(f,n)[1][i] = upr(w,1);
00163         t_set(f,n)[2][i] = upr(w,2);
00164         t_set(f,n)[3][i] = upr(w,3);
00165 #endif
00166         w = bytes2word(b, 0, 0, 0);
00167 
00168 #ifdef  FL1_SET                 /* tables for last encryption round (may also   */
00169         t_set(f,l)[i] = w;        /* be used in the key schedule)                 */
00170 #endif
00171 #ifdef  FL4_SET
00172         t_set(f,l)[0][i] = w;
00173         t_set(f,l)[1][i] = upr(w,1);
00174         t_set(f,l)[2][i] = upr(w,2);
00175         t_set(f,l)[3][i] = upr(w,3);
00176 #endif
00177 
00178 #ifdef  LS1_SET                 /* table for key schedule if t_set(f,l) above is    */
00179         t_set(l,s)[i] = w;      /* not of the required form                     */
00180 #endif
00181 #ifdef  LS4_SET
00182         t_set(l,s)[0][i] = w;
00183         t_set(l,s)[1][i] = upr(w,1);
00184         t_set(l,s)[2][i] = upr(w,2);
00185         t_set(l,s)[3][i] = upr(w,3);
00186 #endif
00187 
00188         b = fi(inv_affine((aes_08t)i));
00189         w = bytes2word(fe(b), f9(b), fd(b), fb(b));
00190 
00191 #ifdef  IM1_SET                 /* tables for the inverse mix column operation  */
00192         t_set(i,m)[b] = w;
00193 #endif
00194 #ifdef  IM4_SET
00195         t_set(i,m)[0][b] = w;
00196         t_set(i,m)[1][b] = upr(w,1);
00197         t_set(i,m)[2][b] = upr(w,2);
00198         t_set(i,m)[3][b] = upr(w,3);
00199 #endif
00200 
00201 #ifdef  ISB_SET
00202         t_set(i,box)[i] = b;
00203 #endif
00204 #ifdef  IT1_SET                 /* tables for a normal decryption round */
00205         t_set(i,n)[i] = w;
00206 #endif
00207 #ifdef  IT4_SET
00208         t_set(i,n)[0][i] = w;
00209         t_set(i,n)[1][i] = upr(w,1);
00210         t_set(i,n)[2][i] = upr(w,2);
00211         t_set(i,n)[3][i] = upr(w,3);
00212 #endif
00213         w = bytes2word(b, 0, 0, 0);
00214 #ifdef  IL1_SET                 /* tables for last decryption round */
00215         t_set(i,l)[i] = w;
00216 #endif
00217 #ifdef  IL4_SET
00218         t_set(i,l)[0][i] = w;
00219         t_set(i,l)[1][i] = upr(w,1);
00220         t_set(i,l)[2][i] = upr(w,2);
00221         t_set(i,l)[3][i] = upr(w,3);
00222 #endif
00223     }
00224     init = 1;
00225 }
00226 
00227 #endif
00228 
00229 #if defined(__cplusplus)
00230 }
00231 #endif
00232 

Generated on Sat Aug 6 00:39:19 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7