Thu Apr 28 2011 16:56:39

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 #ifndef HAVE_CRYPTO
00041 
00042 #define DO_TABLES
00043 
00044 #include "aesopt.h"
00045 
00046 #if defined(FIXED_TABLES)
00047 
00048 /* implemented in case of wrong call for fixed tables */
00049 
00050 void gen_tabs(void)
00051 {
00052 }
00053 
00054 #else   /* dynamic table generation */
00055 
00056 #if !defined(FF_TABLES)
00057 
00058 /*  Generate the tables for the dynamic table option
00059 
00060     It will generally be sensible to use tables to compute finite
00061     field multiplies and inverses but where memory is scarse this
00062     code might sometimes be better. But it only has effect during
00063     initialisation so its pretty unimportant in overall terms.
00064 */
00065 
00066 /*  return 2 ^ (n - 1) where n is the bit number of the highest bit
00067     set in x with x in the range 1 < x < 0x00000200.   This form is
00068     used so that locals within fi can be bytes rather than words
00069 */
00070 
00071 static aes_08t hibit(const aes_32t x)
00072 {   aes_08t r = (aes_08t)((x >> 1) | (x >> 2));
00073 
00074     r |= (r >> 2);
00075     r |= (r >> 4);
00076     return (r + 1) >> 1;
00077 }
00078 
00079 /* return the inverse of the finite field element x */
00080 
00081 static aes_08t fi(const aes_08t x)
00082 {   aes_08t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
00083 
00084     if(x < 2) return x;
00085 
00086     for(;;)
00087     {
00088         if(!n1) return v1;
00089 
00090         while(n2 >= n1)
00091         {
00092             n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
00093         }
00094 
00095         if(!n2) return v2;
00096 
00097         while(n1 >= n2)
00098         {
00099             n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
00100         }
00101     }
00102 }
00103 
00104 #endif
00105 
00106 /* The forward and inverse affine transformations used in the S-box */
00107 
00108 #define fwd_affine(x) \
00109     (w = (aes_32t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(aes_08t)(w^(w>>8)))
00110 
00111 #define inv_affine(x) \
00112     (w = (aes_32t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(aes_08t)(w^(w>>8)))
00113 
00114 static int init = 0;
00115 
00116 void gen_tabs(void)
00117 {   aes_32t  i, w;
00118 
00119 #if defined(FF_TABLES)
00120 
00121     aes_08t  pow[512], log[256];
00122 
00123     if(init) return;
00124     /*  log and power tables for GF(2^8) finite field with
00125         WPOLY as modular polynomial - the simplest primitive
00126         root is 0x03, used here to generate the tables
00127     */
00128 
00129     i = 0; w = 1;
00130     do
00131     {
00132         pow[i] = (aes_08t)w;
00133         pow[i + 255] = (aes_08t)w;
00134         log[w] = (aes_08t)i++;
00135         w ^=  (w << 1) ^ (w & 0x80 ? WPOLY : 0);
00136     }
00137     while (w != 1);
00138 
00139 #else
00140     if(init) return;
00141 #endif
00142 
00143     for(i = 0, w = 1; i < RC_LENGTH; ++i)
00144     {
00145         t_set(r,c)[i] = bytes2word(w, 0, 0, 0);
00146         w = f2(w);
00147     }
00148 
00149     for(i = 0; i < 256; ++i)
00150     {   aes_08t    b;
00151 
00152         b = fwd_affine(fi((aes_08t)i));
00153         w = bytes2word(f2(b), b, b, f3(b));
00154 
00155 #ifdef  SBX_SET
00156         t_set(s,box)[i] = b;
00157 #endif
00158 
00159 #ifdef  FT1_SET                 /* tables for a normal encryption round */
00160         t_set(f,n)[i] = w;
00161 #endif
00162 #ifdef  FT4_SET
00163         t_set(f,n)[0][i] = w;
00164         t_set(f,n)[1][i] = upr(w,1);
00165         t_set(f,n)[2][i] = upr(w,2);
00166         t_set(f,n)[3][i] = upr(w,3);
00167 #endif
00168         w = bytes2word(b, 0, 0, 0);
00169 
00170 #ifdef  FL1_SET                 /* tables for last encryption round (may also   */
00171         t_set(f,l)[i] = w;        /* be used in the key schedule)                 */
00172 #endif
00173 #ifdef  FL4_SET
00174         t_set(f,l)[0][i] = w;
00175         t_set(f,l)[1][i] = upr(w,1);
00176         t_set(f,l)[2][i] = upr(w,2);
00177         t_set(f,l)[3][i] = upr(w,3);
00178 #endif
00179 
00180 #ifdef  LS1_SET                 /* table for key schedule if t_set(f,l) above is    */
00181         t_set(l,s)[i] = w;      /* not of the required form                     */
00182 #endif
00183 #ifdef  LS4_SET
00184         t_set(l,s)[0][i] = w;
00185         t_set(l,s)[1][i] = upr(w,1);
00186         t_set(l,s)[2][i] = upr(w,2);
00187         t_set(l,s)[3][i] = upr(w,3);
00188 #endif
00189 
00190         b = fi(inv_affine((aes_08t)i));
00191         w = bytes2word(fe(b), f9(b), fd(b), fb(b));
00192 
00193 #ifdef  IM1_SET                 /* tables for the inverse mix column operation  */
00194         t_set(i,m)[b] = w;
00195 #endif
00196 #ifdef  IM4_SET
00197         t_set(i,m)[0][b] = w;
00198         t_set(i,m)[1][b] = upr(w,1);
00199         t_set(i,m)[2][b] = upr(w,2);
00200         t_set(i,m)[3][b] = upr(w,3);
00201 #endif
00202 
00203 #ifdef  ISB_SET
00204         t_set(i,box)[i] = b;
00205 #endif
00206 #ifdef  IT1_SET                 /* tables for a normal decryption round */
00207         t_set(i,n)[i] = w;
00208 #endif
00209 #ifdef  IT4_SET
00210         t_set(i,n)[0][i] = w;
00211         t_set(i,n)[1][i] = upr(w,1);
00212         t_set(i,n)[2][i] = upr(w,2);
00213         t_set(i,n)[3][i] = upr(w,3);
00214 #endif
00215         w = bytes2word(b, 0, 0, 0);
00216 #ifdef  IL1_SET                 /* tables for last decryption round */
00217         t_set(i,l)[i] = w;
00218 #endif
00219 #ifdef  IL4_SET
00220         t_set(i,l)[0][i] = w;
00221         t_set(i,l)[1][i] = upr(w,1);
00222         t_set(i,l)[2][i] = upr(w,2);
00223         t_set(i,l)[3][i] = upr(w,3);
00224 #endif
00225     }
00226     init = 1;
00227 }
00228 
00229 #endif
00230 
00231 #endif /* !HAVE_CRYPTO */
00232 
00233 #if defined(__cplusplus)
00234 }
00235 #endif
00236