暫無描述

aestab.h 5.2KB

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