1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * --------------------------------------------------------------------------- 3*eda14cbcSMatt Macy * Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved. 4*eda14cbcSMatt Macy * 5*eda14cbcSMatt Macy * LICENSE TERMS 6*eda14cbcSMatt Macy * 7*eda14cbcSMatt Macy * The free distribution and use of this software is allowed (with or without 8*eda14cbcSMatt Macy * changes) provided that: 9*eda14cbcSMatt Macy * 10*eda14cbcSMatt Macy * 1. source code distributions include the above copyright notice, this 11*eda14cbcSMatt Macy * list of conditions and the following disclaimer; 12*eda14cbcSMatt Macy * 13*eda14cbcSMatt Macy * 2. binary distributions include the above copyright notice, this list 14*eda14cbcSMatt Macy * of conditions and the following disclaimer in their documentation; 15*eda14cbcSMatt Macy * 16*eda14cbcSMatt Macy * 3. the name of the copyright holder is not used to endorse products 17*eda14cbcSMatt Macy * built using this software without specific written permission. 18*eda14cbcSMatt Macy * 19*eda14cbcSMatt Macy * DISCLAIMER 20*eda14cbcSMatt Macy * 21*eda14cbcSMatt Macy * This software is provided 'as is' with no explicit or implied warranties 22*eda14cbcSMatt Macy * in respect of its properties, including, but not limited to, correctness 23*eda14cbcSMatt Macy * and/or fitness for purpose. 24*eda14cbcSMatt Macy * --------------------------------------------------------------------------- 25*eda14cbcSMatt Macy * Issue Date: 20/12/2007 26*eda14cbcSMatt Macy * 27*eda14cbcSMatt Macy * This file contains the code for declaring the tables needed to implement 28*eda14cbcSMatt Macy * AES. The file aesopt.h is assumed to be included before this header file. 29*eda14cbcSMatt Macy * If there are no global variables, the definitions here can be used to put 30*eda14cbcSMatt Macy * the AES tables in a structure so that a pointer can then be added to the 31*eda14cbcSMatt Macy * AES context to pass them to the AES routines that need them. If this 32*eda14cbcSMatt Macy * facility is used, the calling program has to ensure that this pointer is 33*eda14cbcSMatt Macy * managed appropriately. In particular, the value of the t_dec(in, it) item 34*eda14cbcSMatt Macy * in the table structure must be set to zero in order to ensure that the 35*eda14cbcSMatt Macy * tables are initialised. In practice the three code sequences in aeskey.c 36*eda14cbcSMatt Macy * that control the calls to aes_init() and the aes_init() routine itself will 37*eda14cbcSMatt Macy * have to be changed for a specific implementation. If global variables are 38*eda14cbcSMatt Macy * available it will generally be preferable to use them with the precomputed 39*eda14cbcSMatt Macy * FIXED_TABLES option that uses static global tables. 40*eda14cbcSMatt Macy * 41*eda14cbcSMatt Macy * The following defines can be used to control the way the tables 42*eda14cbcSMatt Macy * are defined, initialised and used in embedded environments that 43*eda14cbcSMatt Macy * require special features for these purposes 44*eda14cbcSMatt Macy * 45*eda14cbcSMatt Macy * the 't_dec' construction is used to declare fixed table arrays 46*eda14cbcSMatt Macy * the 't_set' construction is used to set fixed table values 47*eda14cbcSMatt Macy * the 't_use' construction is used to access fixed table values 48*eda14cbcSMatt Macy * 49*eda14cbcSMatt Macy * 256 byte tables: 50*eda14cbcSMatt Macy * 51*eda14cbcSMatt Macy * t_xxx(s, box) => forward S box 52*eda14cbcSMatt Macy * t_xxx(i, box) => inverse S box 53*eda14cbcSMatt Macy * 54*eda14cbcSMatt Macy * 256 32-bit word OR 4 x 256 32-bit word tables: 55*eda14cbcSMatt Macy * 56*eda14cbcSMatt Macy * t_xxx(f, n) => forward normal round 57*eda14cbcSMatt Macy * t_xxx(f, l) => forward last round 58*eda14cbcSMatt Macy * t_xxx(i, n) => inverse normal round 59*eda14cbcSMatt Macy * t_xxx(i, l) => inverse last round 60*eda14cbcSMatt Macy * t_xxx(l, s) => key schedule table 61*eda14cbcSMatt Macy * t_xxx(i, m) => key schedule table 62*eda14cbcSMatt Macy * 63*eda14cbcSMatt Macy * Other variables and tables: 64*eda14cbcSMatt Macy * 65*eda14cbcSMatt Macy * t_xxx(r, c) => the rcon table 66*eda14cbcSMatt Macy */ 67*eda14cbcSMatt Macy 68*eda14cbcSMatt Macy /* 69*eda14cbcSMatt Macy * OpenSolaris OS modifications 70*eda14cbcSMatt Macy * 71*eda14cbcSMatt Macy * 1. Added __cplusplus and _AESTAB_H header guards 72*eda14cbcSMatt Macy * 2. Added header file sys/types.h 73*eda14cbcSMatt Macy * 3. Remove code defined for _MSC_VER 74*eda14cbcSMatt Macy * 4. Changed all variables to "static const" 75*eda14cbcSMatt Macy * 5. Changed uint_8t and uint_32t to uint8_t and uint32_t 76*eda14cbcSMatt Macy * 6. Cstyled and hdrchk code 77*eda14cbcSMatt Macy */ 78*eda14cbcSMatt Macy 79*eda14cbcSMatt Macy #ifndef _AESTAB_H 80*eda14cbcSMatt Macy #define _AESTAB_H 81*eda14cbcSMatt Macy 82*eda14cbcSMatt Macy #ifdef __cplusplus 83*eda14cbcSMatt Macy extern "C" { 84*eda14cbcSMatt Macy #endif 85*eda14cbcSMatt Macy 86*eda14cbcSMatt Macy #include <sys/types.h> 87*eda14cbcSMatt Macy 88*eda14cbcSMatt Macy #define t_dec(m, n) t_##m##n 89*eda14cbcSMatt Macy #define t_set(m, n) t_##m##n 90*eda14cbcSMatt Macy #define t_use(m, n) t_##m##n 91*eda14cbcSMatt Macy 92*eda14cbcSMatt Macy #if defined(DO_TABLES) && defined(FIXED_TABLES) 93*eda14cbcSMatt Macy #define d_1(t, n, b, e) static const t n[256] = b(e) 94*eda14cbcSMatt Macy #define d_4(t, n, b, e, f, g, h) static const t n[4][256] = \ 95*eda14cbcSMatt Macy {b(e), b(f), b(g), b(h)} 96*eda14cbcSMatt Macy static const uint32_t t_dec(r, c)[RC_LENGTH] = rc_data(w0); 97*eda14cbcSMatt Macy #else 98*eda14cbcSMatt Macy #define d_1(t, n, b, e) static const t n[256] 99*eda14cbcSMatt Macy #define d_4(t, n, b, e, f, g, h) static const t n[4][256] 100*eda14cbcSMatt Macy static const uint32_t t_dec(r, c)[RC_LENGTH]; 101*eda14cbcSMatt Macy #endif 102*eda14cbcSMatt Macy 103*eda14cbcSMatt Macy #if defined(SBX_SET) 104*eda14cbcSMatt Macy d_1(uint8_t, t_dec(s, box), sb_data, h0); 105*eda14cbcSMatt Macy #endif 106*eda14cbcSMatt Macy #if defined(ISB_SET) 107*eda14cbcSMatt Macy d_1(uint8_t, t_dec(i, box), isb_data, h0); 108*eda14cbcSMatt Macy #endif 109*eda14cbcSMatt Macy 110*eda14cbcSMatt Macy #if defined(FT1_SET) 111*eda14cbcSMatt Macy d_1(uint32_t, t_dec(f, n), sb_data, u0); 112*eda14cbcSMatt Macy #endif 113*eda14cbcSMatt Macy #if defined(FT4_SET) 114*eda14cbcSMatt Macy d_4(uint32_t, t_dec(f, n), sb_data, u0, u1, u2, u3); 115*eda14cbcSMatt Macy #endif 116*eda14cbcSMatt Macy 117*eda14cbcSMatt Macy #if defined(FL1_SET) 118*eda14cbcSMatt Macy d_1(uint32_t, t_dec(f, l), sb_data, w0); 119*eda14cbcSMatt Macy #endif 120*eda14cbcSMatt Macy #if defined(FL4_SET) 121*eda14cbcSMatt Macy d_4(uint32_t, t_dec(f, l), sb_data, w0, w1, w2, w3); 122*eda14cbcSMatt Macy #endif 123*eda14cbcSMatt Macy 124*eda14cbcSMatt Macy #if defined(IT1_SET) 125*eda14cbcSMatt Macy d_1(uint32_t, t_dec(i, n), isb_data, v0); 126*eda14cbcSMatt Macy #endif 127*eda14cbcSMatt Macy #if defined(IT4_SET) 128*eda14cbcSMatt Macy d_4(uint32_t, t_dec(i, n), isb_data, v0, v1, v2, v3); 129*eda14cbcSMatt Macy #endif 130*eda14cbcSMatt Macy 131*eda14cbcSMatt Macy #if defined(IL1_SET) 132*eda14cbcSMatt Macy d_1(uint32_t, t_dec(i, l), isb_data, w0); 133*eda14cbcSMatt Macy #endif 134*eda14cbcSMatt Macy #if defined(IL4_SET) 135*eda14cbcSMatt Macy d_4(uint32_t, t_dec(i, l), isb_data, w0, w1, w2, w3); 136*eda14cbcSMatt Macy #endif 137*eda14cbcSMatt Macy 138*eda14cbcSMatt Macy #if defined(LS1_SET) 139*eda14cbcSMatt Macy #if defined(FL1_SET) 140*eda14cbcSMatt Macy #undef LS1_SET 141*eda14cbcSMatt Macy #else 142*eda14cbcSMatt Macy d_1(uint32_t, t_dec(l, s), sb_data, w0); 143*eda14cbcSMatt Macy #endif 144*eda14cbcSMatt Macy #endif 145*eda14cbcSMatt Macy 146*eda14cbcSMatt Macy #if defined(LS4_SET) 147*eda14cbcSMatt Macy #if defined(FL4_SET) 148*eda14cbcSMatt Macy #undef LS4_SET 149*eda14cbcSMatt Macy #else 150*eda14cbcSMatt Macy d_4(uint32_t, t_dec(l, s), sb_data, w0, w1, w2, w3); 151*eda14cbcSMatt Macy #endif 152*eda14cbcSMatt Macy #endif 153*eda14cbcSMatt Macy 154*eda14cbcSMatt Macy #if defined(IM1_SET) 155*eda14cbcSMatt Macy d_1(uint32_t, t_dec(i, m), mm_data, v0); 156*eda14cbcSMatt Macy #endif 157*eda14cbcSMatt Macy #if defined(IM4_SET) 158*eda14cbcSMatt Macy d_4(uint32_t, t_dec(i, m), mm_data, v0, v1, v2, v3); 159*eda14cbcSMatt Macy #endif 160*eda14cbcSMatt Macy 161*eda14cbcSMatt Macy #ifdef __cplusplus 162*eda14cbcSMatt Macy } 163*eda14cbcSMatt Macy #endif 164*eda14cbcSMatt Macy 165*eda14cbcSMatt Macy #endif /* _AESTAB_H */ 166